1 | /* $Id: MediumImpl.cpp 71097 2018-02-22 09:43:25Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox COM class implementation
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2017 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 | #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 | if (mProgress)
|
---|
245 | {
|
---|
246 | mVDIfProgress.pfnProgress = aProgress->i_vdProgressCallback;
|
---|
247 | int vrc = VDInterfaceAdd(&mVDIfProgress.Core,
|
---|
248 | "Medium::Task::vdInterfaceProgress",
|
---|
249 | VDINTERFACETYPE_PROGRESS,
|
---|
250 | mProgress,
|
---|
251 | sizeof(mVDIfProgress),
|
---|
252 | &mVDOperationIfaces);
|
---|
253 | AssertRC(vrc);
|
---|
254 | if (RT_FAILURE(vrc))
|
---|
255 | mRC = E_FAIL;
|
---|
256 | }
|
---|
257 | }
|
---|
258 |
|
---|
259 | // Make all destructors virtual. Just in case.
|
---|
260 | virtual ~Task()
|
---|
261 | {
|
---|
262 | /* send the notification of completion.*/
|
---|
263 | if ( isAsync()
|
---|
264 | && !mProgress.isNull())
|
---|
265 | mProgress->i_notifyComplete(mRC);
|
---|
266 | }
|
---|
267 |
|
---|
268 | HRESULT rc() const { return mRC; }
|
---|
269 | bool isOk() const { return SUCCEEDED(rc()); }
|
---|
270 |
|
---|
271 | const ComPtr<Progress>& GetProgressObject() const {return mProgress;}
|
---|
272 |
|
---|
273 | /**
|
---|
274 | * Runs Medium::Task::executeTask() on the current thread
|
---|
275 | * instead of creating a new one.
|
---|
276 | */
|
---|
277 | HRESULT runNow()
|
---|
278 | {
|
---|
279 | LogFlowFuncEnter();
|
---|
280 |
|
---|
281 | mRC = executeTask();
|
---|
282 |
|
---|
283 | LogFlowFunc(("rc=%Rhrc\n", mRC));
|
---|
284 | LogFlowFuncLeave();
|
---|
285 | return mRC;
|
---|
286 | }
|
---|
287 |
|
---|
288 | /**
|
---|
289 | * Implementation code for the "create base" task.
|
---|
290 | * Used as function for execution from a standalone thread.
|
---|
291 | */
|
---|
292 | void handler()
|
---|
293 | {
|
---|
294 | LogFlowFuncEnter();
|
---|
295 | try
|
---|
296 | {
|
---|
297 | mRC = executeTask(); /* (destructor picks up mRC, see above) */
|
---|
298 | LogFlowFunc(("rc=%Rhrc\n", mRC));
|
---|
299 | }
|
---|
300 | catch (...)
|
---|
301 | {
|
---|
302 | LogRel(("Some exception in the function Medium::Task:handler()\n"));
|
---|
303 | }
|
---|
304 |
|
---|
305 | LogFlowFuncLeave();
|
---|
306 | }
|
---|
307 |
|
---|
308 | PVDINTERFACE mVDOperationIfaces;
|
---|
309 |
|
---|
310 | const ComObjPtr<Medium> mMedium;
|
---|
311 | AutoCaller mMediumCaller;
|
---|
312 |
|
---|
313 | protected:
|
---|
314 | HRESULT mRC;
|
---|
315 |
|
---|
316 | private:
|
---|
317 | virtual HRESULT executeTask() = 0;
|
---|
318 |
|
---|
319 | const ComObjPtr<Progress> mProgress;
|
---|
320 |
|
---|
321 | VDINTERFACEPROGRESS mVDIfProgress;
|
---|
322 |
|
---|
323 | /* Must have a strong VirtualBox reference during a task otherwise the
|
---|
324 | * reference count might drop to 0 while a task is still running. This
|
---|
325 | * would result in weird behavior, including deadlocks due to uninit and
|
---|
326 | * locking order issues. The deadlock often is not detectable because the
|
---|
327 | * uninit uses event semaphores which sabotages deadlock detection. */
|
---|
328 | ComObjPtr<VirtualBox> mVirtualBox;
|
---|
329 | AutoCaller mVirtualBoxCaller;
|
---|
330 | };
|
---|
331 |
|
---|
332 | HRESULT Medium::Task::executeTask()
|
---|
333 | {
|
---|
334 | return E_NOTIMPL;//ReturnComNotImplemented()
|
---|
335 | }
|
---|
336 |
|
---|
337 | class Medium::CreateBaseTask : public Medium::Task
|
---|
338 | {
|
---|
339 | public:
|
---|
340 | CreateBaseTask(Medium *aMedium,
|
---|
341 | Progress *aProgress,
|
---|
342 | uint64_t aSize,
|
---|
343 | MediumVariant_T aVariant)
|
---|
344 | : Medium::Task(aMedium, aProgress),
|
---|
345 | mSize(aSize),
|
---|
346 | mVariant(aVariant)
|
---|
347 | {
|
---|
348 | m_strTaskName = "createBase";
|
---|
349 | }
|
---|
350 |
|
---|
351 | uint64_t mSize;
|
---|
352 | MediumVariant_T mVariant;
|
---|
353 |
|
---|
354 | private:
|
---|
355 | HRESULT executeTask();
|
---|
356 | };
|
---|
357 |
|
---|
358 | class Medium::CreateDiffTask : public Medium::Task
|
---|
359 | {
|
---|
360 | public:
|
---|
361 | CreateDiffTask(Medium *aMedium,
|
---|
362 | Progress *aProgress,
|
---|
363 | Medium *aTarget,
|
---|
364 | MediumVariant_T aVariant,
|
---|
365 | MediumLockList *aMediumLockList,
|
---|
366 | bool fKeepMediumLockList = false)
|
---|
367 | : Medium::Task(aMedium, aProgress),
|
---|
368 | mpMediumLockList(aMediumLockList),
|
---|
369 | mTarget(aTarget),
|
---|
370 | mVariant(aVariant),
|
---|
371 | mTargetCaller(aTarget),
|
---|
372 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
373 | {
|
---|
374 | AssertReturnVoidStmt(aTarget != NULL, mRC = E_FAIL);
|
---|
375 | mRC = mTargetCaller.rc();
|
---|
376 | if (FAILED(mRC))
|
---|
377 | return;
|
---|
378 | m_strTaskName = "createDiff";
|
---|
379 | }
|
---|
380 |
|
---|
381 | ~CreateDiffTask()
|
---|
382 | {
|
---|
383 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
384 | delete mpMediumLockList;
|
---|
385 | }
|
---|
386 |
|
---|
387 | MediumLockList *mpMediumLockList;
|
---|
388 |
|
---|
389 | const ComObjPtr<Medium> mTarget;
|
---|
390 | MediumVariant_T mVariant;
|
---|
391 |
|
---|
392 | private:
|
---|
393 | HRESULT executeTask();
|
---|
394 | AutoCaller mTargetCaller;
|
---|
395 | bool mfKeepMediumLockList;
|
---|
396 | };
|
---|
397 |
|
---|
398 | class Medium::CloneTask : public Medium::Task
|
---|
399 | {
|
---|
400 | public:
|
---|
401 | CloneTask(Medium *aMedium,
|
---|
402 | Progress *aProgress,
|
---|
403 | Medium *aTarget,
|
---|
404 | MediumVariant_T aVariant,
|
---|
405 | Medium *aParent,
|
---|
406 | uint32_t idxSrcImageSame,
|
---|
407 | uint32_t idxDstImageSame,
|
---|
408 | MediumLockList *aSourceMediumLockList,
|
---|
409 | MediumLockList *aTargetMediumLockList,
|
---|
410 | bool fKeepSourceMediumLockList = false,
|
---|
411 | bool fKeepTargetMediumLockList = false)
|
---|
412 | : Medium::Task(aMedium, aProgress),
|
---|
413 | mTarget(aTarget),
|
---|
414 | mParent(aParent),
|
---|
415 | mpSourceMediumLockList(aSourceMediumLockList),
|
---|
416 | mpTargetMediumLockList(aTargetMediumLockList),
|
---|
417 | mVariant(aVariant),
|
---|
418 | midxSrcImageSame(idxSrcImageSame),
|
---|
419 | midxDstImageSame(idxDstImageSame),
|
---|
420 | mTargetCaller(aTarget),
|
---|
421 | mParentCaller(aParent),
|
---|
422 | mfKeepSourceMediumLockList(fKeepSourceMediumLockList),
|
---|
423 | mfKeepTargetMediumLockList(fKeepTargetMediumLockList)
|
---|
424 | {
|
---|
425 | AssertReturnVoidStmt(aTarget != NULL, mRC = E_FAIL);
|
---|
426 | mRC = mTargetCaller.rc();
|
---|
427 | if (FAILED(mRC))
|
---|
428 | return;
|
---|
429 | /* aParent may be NULL */
|
---|
430 | mRC = mParentCaller.rc();
|
---|
431 | if (FAILED(mRC))
|
---|
432 | return;
|
---|
433 | AssertReturnVoidStmt(aSourceMediumLockList != NULL, mRC = E_FAIL);
|
---|
434 | AssertReturnVoidStmt(aTargetMediumLockList != NULL, mRC = E_FAIL);
|
---|
435 | m_strTaskName = "createClone";
|
---|
436 | }
|
---|
437 |
|
---|
438 | ~CloneTask()
|
---|
439 | {
|
---|
440 | if (!mfKeepSourceMediumLockList && mpSourceMediumLockList)
|
---|
441 | delete mpSourceMediumLockList;
|
---|
442 | if (!mfKeepTargetMediumLockList && mpTargetMediumLockList)
|
---|
443 | delete mpTargetMediumLockList;
|
---|
444 | }
|
---|
445 |
|
---|
446 | const ComObjPtr<Medium> mTarget;
|
---|
447 | const ComObjPtr<Medium> mParent;
|
---|
448 | MediumLockList *mpSourceMediumLockList;
|
---|
449 | MediumLockList *mpTargetMediumLockList;
|
---|
450 | MediumVariant_T mVariant;
|
---|
451 | uint32_t midxSrcImageSame;
|
---|
452 | uint32_t midxDstImageSame;
|
---|
453 |
|
---|
454 | private:
|
---|
455 | HRESULT executeTask();
|
---|
456 | AutoCaller mTargetCaller;
|
---|
457 | AutoCaller mParentCaller;
|
---|
458 | bool mfKeepSourceMediumLockList;
|
---|
459 | bool mfKeepTargetMediumLockList;
|
---|
460 | };
|
---|
461 |
|
---|
462 | class Medium::MoveTask : public Medium::Task
|
---|
463 | {
|
---|
464 | public:
|
---|
465 | MoveTask(Medium *aMedium,
|
---|
466 | Progress *aProgress,
|
---|
467 | MediumVariant_T aVariant,
|
---|
468 | MediumLockList *aMediumLockList,
|
---|
469 | bool fKeepMediumLockList = false)
|
---|
470 | : Medium::Task(aMedium, aProgress),
|
---|
471 | mpMediumLockList(aMediumLockList),
|
---|
472 | mVariant(aVariant),
|
---|
473 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
474 | {
|
---|
475 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
476 | m_strTaskName = "createMove";
|
---|
477 | }
|
---|
478 |
|
---|
479 | ~MoveTask()
|
---|
480 | {
|
---|
481 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
482 | delete mpMediumLockList;
|
---|
483 | }
|
---|
484 |
|
---|
485 | MediumLockList *mpMediumLockList;
|
---|
486 | MediumVariant_T mVariant;
|
---|
487 |
|
---|
488 | private:
|
---|
489 | HRESULT executeTask();
|
---|
490 | bool mfKeepMediumLockList;
|
---|
491 | };
|
---|
492 |
|
---|
493 | class Medium::CompactTask : public Medium::Task
|
---|
494 | {
|
---|
495 | public:
|
---|
496 | CompactTask(Medium *aMedium,
|
---|
497 | Progress *aProgress,
|
---|
498 | MediumLockList *aMediumLockList,
|
---|
499 | bool fKeepMediumLockList = false)
|
---|
500 | : Medium::Task(aMedium, aProgress),
|
---|
501 | mpMediumLockList(aMediumLockList),
|
---|
502 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
503 | {
|
---|
504 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
505 | m_strTaskName = "createCompact";
|
---|
506 | }
|
---|
507 |
|
---|
508 | ~CompactTask()
|
---|
509 | {
|
---|
510 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
511 | delete mpMediumLockList;
|
---|
512 | }
|
---|
513 |
|
---|
514 | MediumLockList *mpMediumLockList;
|
---|
515 |
|
---|
516 | private:
|
---|
517 | HRESULT executeTask();
|
---|
518 | bool mfKeepMediumLockList;
|
---|
519 | };
|
---|
520 |
|
---|
521 | class Medium::ResizeTask : public Medium::Task
|
---|
522 | {
|
---|
523 | public:
|
---|
524 | ResizeTask(Medium *aMedium,
|
---|
525 | uint64_t aSize,
|
---|
526 | Progress *aProgress,
|
---|
527 | MediumLockList *aMediumLockList,
|
---|
528 | bool fKeepMediumLockList = false)
|
---|
529 | : Medium::Task(aMedium, aProgress),
|
---|
530 | mSize(aSize),
|
---|
531 | mpMediumLockList(aMediumLockList),
|
---|
532 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
533 | {
|
---|
534 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
535 | m_strTaskName = "createResize";
|
---|
536 | }
|
---|
537 |
|
---|
538 | ~ResizeTask()
|
---|
539 | {
|
---|
540 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
541 | delete mpMediumLockList;
|
---|
542 | }
|
---|
543 |
|
---|
544 | uint64_t mSize;
|
---|
545 | MediumLockList *mpMediumLockList;
|
---|
546 |
|
---|
547 | private:
|
---|
548 | HRESULT executeTask();
|
---|
549 | bool mfKeepMediumLockList;
|
---|
550 | };
|
---|
551 |
|
---|
552 | class Medium::ResetTask : public Medium::Task
|
---|
553 | {
|
---|
554 | public:
|
---|
555 | ResetTask(Medium *aMedium,
|
---|
556 | Progress *aProgress,
|
---|
557 | MediumLockList *aMediumLockList,
|
---|
558 | bool fKeepMediumLockList = false)
|
---|
559 | : Medium::Task(aMedium, aProgress),
|
---|
560 | mpMediumLockList(aMediumLockList),
|
---|
561 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
562 | {
|
---|
563 | m_strTaskName = "createReset";
|
---|
564 | }
|
---|
565 |
|
---|
566 | ~ResetTask()
|
---|
567 | {
|
---|
568 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
569 | delete mpMediumLockList;
|
---|
570 | }
|
---|
571 |
|
---|
572 | MediumLockList *mpMediumLockList;
|
---|
573 |
|
---|
574 | private:
|
---|
575 | HRESULT executeTask();
|
---|
576 | bool mfKeepMediumLockList;
|
---|
577 | };
|
---|
578 |
|
---|
579 | class Medium::DeleteTask : public Medium::Task
|
---|
580 | {
|
---|
581 | public:
|
---|
582 | DeleteTask(Medium *aMedium,
|
---|
583 | Progress *aProgress,
|
---|
584 | MediumLockList *aMediumLockList,
|
---|
585 | bool fKeepMediumLockList = false)
|
---|
586 | : Medium::Task(aMedium, aProgress),
|
---|
587 | mpMediumLockList(aMediumLockList),
|
---|
588 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
589 | {
|
---|
590 | m_strTaskName = "createDelete";
|
---|
591 | }
|
---|
592 |
|
---|
593 | ~DeleteTask()
|
---|
594 | {
|
---|
595 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
596 | delete mpMediumLockList;
|
---|
597 | }
|
---|
598 |
|
---|
599 | MediumLockList *mpMediumLockList;
|
---|
600 |
|
---|
601 | private:
|
---|
602 | HRESULT executeTask();
|
---|
603 | bool mfKeepMediumLockList;
|
---|
604 | };
|
---|
605 |
|
---|
606 | class Medium::MergeTask : public Medium::Task
|
---|
607 | {
|
---|
608 | public:
|
---|
609 | MergeTask(Medium *aMedium,
|
---|
610 | Medium *aTarget,
|
---|
611 | bool fMergeForward,
|
---|
612 | Medium *aParentForTarget,
|
---|
613 | MediumLockList *aChildrenToReparent,
|
---|
614 | Progress *aProgress,
|
---|
615 | MediumLockList *aMediumLockList,
|
---|
616 | bool fKeepMediumLockList = false)
|
---|
617 | : Medium::Task(aMedium, aProgress),
|
---|
618 | mTarget(aTarget),
|
---|
619 | mfMergeForward(fMergeForward),
|
---|
620 | mParentForTarget(aParentForTarget),
|
---|
621 | mpChildrenToReparent(aChildrenToReparent),
|
---|
622 | mpMediumLockList(aMediumLockList),
|
---|
623 | mTargetCaller(aTarget),
|
---|
624 | mParentForTargetCaller(aParentForTarget),
|
---|
625 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
626 | {
|
---|
627 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
628 | m_strTaskName = "createMerge";
|
---|
629 | }
|
---|
630 |
|
---|
631 | ~MergeTask()
|
---|
632 | {
|
---|
633 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
634 | delete mpMediumLockList;
|
---|
635 | if (mpChildrenToReparent)
|
---|
636 | delete mpChildrenToReparent;
|
---|
637 | }
|
---|
638 |
|
---|
639 | const ComObjPtr<Medium> mTarget;
|
---|
640 | bool mfMergeForward;
|
---|
641 | /* When mpChildrenToReparent is null then mParentForTarget is non-null and
|
---|
642 | * vice versa. In other words: they are used in different cases. */
|
---|
643 | const ComObjPtr<Medium> mParentForTarget;
|
---|
644 | MediumLockList *mpChildrenToReparent;
|
---|
645 | MediumLockList *mpMediumLockList;
|
---|
646 |
|
---|
647 | private:
|
---|
648 | HRESULT executeTask();
|
---|
649 | AutoCaller mTargetCaller;
|
---|
650 | AutoCaller mParentForTargetCaller;
|
---|
651 | bool mfKeepMediumLockList;
|
---|
652 | };
|
---|
653 |
|
---|
654 | class Medium::ImportTask : public Medium::Task
|
---|
655 | {
|
---|
656 | public:
|
---|
657 | ImportTask(Medium *aMedium,
|
---|
658 | Progress *aProgress,
|
---|
659 | const char *aFilename,
|
---|
660 | MediumFormat *aFormat,
|
---|
661 | MediumVariant_T aVariant,
|
---|
662 | RTVFSIOSTREAM aVfsIosSrc,
|
---|
663 | Medium *aParent,
|
---|
664 | MediumLockList *aTargetMediumLockList,
|
---|
665 | bool fKeepTargetMediumLockList = false)
|
---|
666 | : Medium::Task(aMedium, aProgress),
|
---|
667 | mFilename(aFilename),
|
---|
668 | mFormat(aFormat),
|
---|
669 | mVariant(aVariant),
|
---|
670 | mParent(aParent),
|
---|
671 | mpTargetMediumLockList(aTargetMediumLockList),
|
---|
672 | mpVfsIoIf(NULL),
|
---|
673 | mParentCaller(aParent),
|
---|
674 | mfKeepTargetMediumLockList(fKeepTargetMediumLockList)
|
---|
675 | {
|
---|
676 | AssertReturnVoidStmt(aTargetMediumLockList != NULL, mRC = E_FAIL);
|
---|
677 | /* aParent may be NULL */
|
---|
678 | mRC = mParentCaller.rc();
|
---|
679 | if (FAILED(mRC))
|
---|
680 | return;
|
---|
681 |
|
---|
682 | mVDImageIfaces = aMedium->m->vdImageIfaces;
|
---|
683 |
|
---|
684 | int vrc = VDIfCreateFromVfsStream(aVfsIosSrc, RTFILE_O_READ, &mpVfsIoIf);
|
---|
685 | AssertRCReturnVoidStmt(vrc, mRC = E_FAIL);
|
---|
686 |
|
---|
687 | vrc = VDInterfaceAdd(&mpVfsIoIf->Core, "Medium::ImportTaskVfsIos",
|
---|
688 | VDINTERFACETYPE_IO, mpVfsIoIf,
|
---|
689 | sizeof(VDINTERFACEIO), &mVDImageIfaces);
|
---|
690 | AssertRCReturnVoidStmt(vrc, mRC = E_FAIL);
|
---|
691 | m_strTaskName = "createImport";
|
---|
692 | }
|
---|
693 |
|
---|
694 | ~ImportTask()
|
---|
695 | {
|
---|
696 | if (!mfKeepTargetMediumLockList && mpTargetMediumLockList)
|
---|
697 | delete mpTargetMediumLockList;
|
---|
698 | if (mpVfsIoIf)
|
---|
699 | {
|
---|
700 | VDIfDestroyFromVfsStream(mpVfsIoIf);
|
---|
701 | mpVfsIoIf = NULL;
|
---|
702 | }
|
---|
703 | }
|
---|
704 |
|
---|
705 | Utf8Str mFilename;
|
---|
706 | ComObjPtr<MediumFormat> mFormat;
|
---|
707 | MediumVariant_T mVariant;
|
---|
708 | const ComObjPtr<Medium> mParent;
|
---|
709 | MediumLockList *mpTargetMediumLockList;
|
---|
710 | PVDINTERFACE mVDImageIfaces;
|
---|
711 | PVDINTERFACEIO mpVfsIoIf; /**< Pointer to the VFS I/O stream to VD I/O interface wrapper. */
|
---|
712 |
|
---|
713 | private:
|
---|
714 | HRESULT executeTask();
|
---|
715 | AutoCaller mParentCaller;
|
---|
716 | bool mfKeepTargetMediumLockList;
|
---|
717 | };
|
---|
718 |
|
---|
719 | class Medium::EncryptTask : public Medium::Task
|
---|
720 | {
|
---|
721 | public:
|
---|
722 | EncryptTask(Medium *aMedium,
|
---|
723 | const com::Utf8Str &strNewPassword,
|
---|
724 | const com::Utf8Str &strCurrentPassword,
|
---|
725 | const com::Utf8Str &strCipher,
|
---|
726 | const com::Utf8Str &strNewPasswordId,
|
---|
727 | Progress *aProgress,
|
---|
728 | MediumLockList *aMediumLockList)
|
---|
729 | : Medium::Task(aMedium, aProgress),
|
---|
730 | mstrNewPassword(strNewPassword),
|
---|
731 | mstrCurrentPassword(strCurrentPassword),
|
---|
732 | mstrCipher(strCipher),
|
---|
733 | mstrNewPasswordId(strNewPasswordId),
|
---|
734 | mpMediumLockList(aMediumLockList)
|
---|
735 | {
|
---|
736 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
737 | /* aParent may be NULL */
|
---|
738 | mRC = mParentCaller.rc();
|
---|
739 | if (FAILED(mRC))
|
---|
740 | return;
|
---|
741 |
|
---|
742 | mVDImageIfaces = aMedium->m->vdImageIfaces;
|
---|
743 | m_strTaskName = "createEncrypt";
|
---|
744 | }
|
---|
745 |
|
---|
746 | ~EncryptTask()
|
---|
747 | {
|
---|
748 | if (mstrNewPassword.length())
|
---|
749 | RTMemWipeThoroughly(mstrNewPassword.mutableRaw(), mstrNewPassword.length(), 10 /* cPasses */);
|
---|
750 | if (mstrCurrentPassword.length())
|
---|
751 | RTMemWipeThoroughly(mstrCurrentPassword.mutableRaw(), mstrCurrentPassword.length(), 10 /* cPasses */);
|
---|
752 |
|
---|
753 | /* Keep any errors which might be set when deleting the lock list. */
|
---|
754 | ErrorInfoKeeper eik;
|
---|
755 | delete mpMediumLockList;
|
---|
756 | }
|
---|
757 |
|
---|
758 | Utf8Str mstrNewPassword;
|
---|
759 | Utf8Str mstrCurrentPassword;
|
---|
760 | Utf8Str mstrCipher;
|
---|
761 | Utf8Str mstrNewPasswordId;
|
---|
762 | MediumLockList *mpMediumLockList;
|
---|
763 | PVDINTERFACE mVDImageIfaces;
|
---|
764 |
|
---|
765 | private:
|
---|
766 | HRESULT executeTask();
|
---|
767 | AutoCaller mParentCaller;
|
---|
768 | };
|
---|
769 |
|
---|
770 | /**
|
---|
771 | * Settings for a crypto filter instance.
|
---|
772 | */
|
---|
773 | struct Medium::CryptoFilterSettings
|
---|
774 | {
|
---|
775 | CryptoFilterSettings()
|
---|
776 | : fCreateKeyStore(false),
|
---|
777 | pszPassword(NULL),
|
---|
778 | pszKeyStore(NULL),
|
---|
779 | pszKeyStoreLoad(NULL),
|
---|
780 | pbDek(NULL),
|
---|
781 | cbDek(0),
|
---|
782 | pszCipher(NULL),
|
---|
783 | pszCipherReturned(NULL)
|
---|
784 | { }
|
---|
785 |
|
---|
786 | bool fCreateKeyStore;
|
---|
787 | const char *pszPassword;
|
---|
788 | char *pszKeyStore;
|
---|
789 | const char *pszKeyStoreLoad;
|
---|
790 |
|
---|
791 | const uint8_t *pbDek;
|
---|
792 | size_t cbDek;
|
---|
793 | const char *pszCipher;
|
---|
794 |
|
---|
795 | /** The cipher returned by the crypto filter. */
|
---|
796 | char *pszCipherReturned;
|
---|
797 |
|
---|
798 | PVDINTERFACE vdFilterIfaces;
|
---|
799 |
|
---|
800 | VDINTERFACECONFIG vdIfCfg;
|
---|
801 | VDINTERFACECRYPTO vdIfCrypto;
|
---|
802 | };
|
---|
803 |
|
---|
804 | /**
|
---|
805 | * Implementation code for the "create base" task.
|
---|
806 | */
|
---|
807 | HRESULT Medium::CreateBaseTask::executeTask()
|
---|
808 | {
|
---|
809 | return mMedium->i_taskCreateBaseHandler(*this);
|
---|
810 | }
|
---|
811 |
|
---|
812 | /**
|
---|
813 | * Implementation code for the "create diff" task.
|
---|
814 | */
|
---|
815 | HRESULT Medium::CreateDiffTask::executeTask()
|
---|
816 | {
|
---|
817 | return mMedium->i_taskCreateDiffHandler(*this);
|
---|
818 | }
|
---|
819 |
|
---|
820 | /**
|
---|
821 | * Implementation code for the "clone" task.
|
---|
822 | */
|
---|
823 | HRESULT Medium::CloneTask::executeTask()
|
---|
824 | {
|
---|
825 | return mMedium->i_taskCloneHandler(*this);
|
---|
826 | }
|
---|
827 |
|
---|
828 | /**
|
---|
829 | * Implementation code for the "move" task.
|
---|
830 | */
|
---|
831 | HRESULT Medium::MoveTask::executeTask()
|
---|
832 | {
|
---|
833 | return mMedium->i_taskMoveHandler(*this);
|
---|
834 | }
|
---|
835 |
|
---|
836 | /**
|
---|
837 | * Implementation code for the "compact" task.
|
---|
838 | */
|
---|
839 | HRESULT Medium::CompactTask::executeTask()
|
---|
840 | {
|
---|
841 | return mMedium->i_taskCompactHandler(*this);
|
---|
842 | }
|
---|
843 |
|
---|
844 | /**
|
---|
845 | * Implementation code for the "resize" task.
|
---|
846 | */
|
---|
847 | HRESULT Medium::ResizeTask::executeTask()
|
---|
848 | {
|
---|
849 | return mMedium->i_taskResizeHandler(*this);
|
---|
850 | }
|
---|
851 |
|
---|
852 |
|
---|
853 | /**
|
---|
854 | * Implementation code for the "reset" task.
|
---|
855 | */
|
---|
856 | HRESULT Medium::ResetTask::executeTask()
|
---|
857 | {
|
---|
858 | return mMedium->i_taskResetHandler(*this);
|
---|
859 | }
|
---|
860 |
|
---|
861 | /**
|
---|
862 | * Implementation code for the "delete" task.
|
---|
863 | */
|
---|
864 | HRESULT Medium::DeleteTask::executeTask()
|
---|
865 | {
|
---|
866 | return mMedium->i_taskDeleteHandler(*this);
|
---|
867 | }
|
---|
868 |
|
---|
869 | /**
|
---|
870 | * Implementation code for the "merge" task.
|
---|
871 | */
|
---|
872 | HRESULT Medium::MergeTask::executeTask()
|
---|
873 | {
|
---|
874 | return mMedium->i_taskMergeHandler(*this);
|
---|
875 | }
|
---|
876 |
|
---|
877 | /**
|
---|
878 | * Implementation code for the "import" task.
|
---|
879 | */
|
---|
880 | HRESULT Medium::ImportTask::executeTask()
|
---|
881 | {
|
---|
882 | return mMedium->i_taskImportHandler(*this);
|
---|
883 | }
|
---|
884 |
|
---|
885 | /**
|
---|
886 | * Implementation code for the "encrypt" task.
|
---|
887 | */
|
---|
888 | HRESULT Medium::EncryptTask::executeTask()
|
---|
889 | {
|
---|
890 | return mMedium->i_taskEncryptHandler(*this);
|
---|
891 | }
|
---|
892 |
|
---|
893 | ////////////////////////////////////////////////////////////////////////////////
|
---|
894 | //
|
---|
895 | // Medium constructor / destructor
|
---|
896 | //
|
---|
897 | ////////////////////////////////////////////////////////////////////////////////
|
---|
898 |
|
---|
899 | DEFINE_EMPTY_CTOR_DTOR(Medium)
|
---|
900 |
|
---|
901 | HRESULT Medium::FinalConstruct()
|
---|
902 | {
|
---|
903 | m = new Data;
|
---|
904 |
|
---|
905 | /* Initialize the callbacks of the VD error interface */
|
---|
906 | m->vdIfError.pfnError = i_vdErrorCall;
|
---|
907 | m->vdIfError.pfnMessage = NULL;
|
---|
908 |
|
---|
909 | /* Initialize the callbacks of the VD config interface */
|
---|
910 | m->vdIfConfig.pfnAreKeysValid = i_vdConfigAreKeysValid;
|
---|
911 | m->vdIfConfig.pfnQuerySize = i_vdConfigQuerySize;
|
---|
912 | m->vdIfConfig.pfnQuery = i_vdConfigQuery;
|
---|
913 | m->vdIfConfig.pfnQueryBytes = NULL;
|
---|
914 |
|
---|
915 | /* Initialize the callbacks of the VD TCP interface (we always use the host
|
---|
916 | * IP stack for now) */
|
---|
917 | m->vdIfTcpNet.pfnSocketCreate = i_vdTcpSocketCreate;
|
---|
918 | m->vdIfTcpNet.pfnSocketDestroy = i_vdTcpSocketDestroy;
|
---|
919 | m->vdIfTcpNet.pfnClientConnect = i_vdTcpClientConnect;
|
---|
920 | m->vdIfTcpNet.pfnClientClose = i_vdTcpClientClose;
|
---|
921 | m->vdIfTcpNet.pfnIsClientConnected = i_vdTcpIsClientConnected;
|
---|
922 | m->vdIfTcpNet.pfnSelectOne = i_vdTcpSelectOne;
|
---|
923 | m->vdIfTcpNet.pfnRead = i_vdTcpRead;
|
---|
924 | m->vdIfTcpNet.pfnWrite = i_vdTcpWrite;
|
---|
925 | m->vdIfTcpNet.pfnSgWrite = i_vdTcpSgWrite;
|
---|
926 | m->vdIfTcpNet.pfnFlush = i_vdTcpFlush;
|
---|
927 | m->vdIfTcpNet.pfnSetSendCoalescing = i_vdTcpSetSendCoalescing;
|
---|
928 | m->vdIfTcpNet.pfnGetLocalAddress = i_vdTcpGetLocalAddress;
|
---|
929 | m->vdIfTcpNet.pfnGetPeerAddress = i_vdTcpGetPeerAddress;
|
---|
930 | m->vdIfTcpNet.pfnSelectOneEx = NULL;
|
---|
931 | m->vdIfTcpNet.pfnPoke = NULL;
|
---|
932 |
|
---|
933 | /* Initialize the per-disk interface chain (could be done more globally,
|
---|
934 | * but it's not wasting much time or space so it's not worth it). */
|
---|
935 | int vrc;
|
---|
936 | vrc = VDInterfaceAdd(&m->vdIfError.Core,
|
---|
937 | "Medium::vdInterfaceError",
|
---|
938 | VDINTERFACETYPE_ERROR, this,
|
---|
939 | sizeof(VDINTERFACEERROR), &m->vdDiskIfaces);
|
---|
940 | AssertRCReturn(vrc, E_FAIL);
|
---|
941 |
|
---|
942 | /* Initialize the per-image interface chain */
|
---|
943 | vrc = VDInterfaceAdd(&m->vdIfConfig.Core,
|
---|
944 | "Medium::vdInterfaceConfig",
|
---|
945 | VDINTERFACETYPE_CONFIG, this,
|
---|
946 | sizeof(VDINTERFACECONFIG), &m->vdImageIfaces);
|
---|
947 | AssertRCReturn(vrc, E_FAIL);
|
---|
948 |
|
---|
949 | vrc = VDInterfaceAdd(&m->vdIfTcpNet.Core,
|
---|
950 | "Medium::vdInterfaceTcpNet",
|
---|
951 | VDINTERFACETYPE_TCPNET, this,
|
---|
952 | sizeof(VDINTERFACETCPNET), &m->vdImageIfaces);
|
---|
953 | AssertRCReturn(vrc, E_FAIL);
|
---|
954 |
|
---|
955 | return BaseFinalConstruct();
|
---|
956 | }
|
---|
957 |
|
---|
958 | void Medium::FinalRelease()
|
---|
959 | {
|
---|
960 | uninit();
|
---|
961 |
|
---|
962 | delete m;
|
---|
963 |
|
---|
964 | BaseFinalRelease();
|
---|
965 | }
|
---|
966 |
|
---|
967 | /**
|
---|
968 | * Initializes an empty hard disk object without creating or opening an associated
|
---|
969 | * storage unit.
|
---|
970 | *
|
---|
971 | * This gets called by VirtualBox::CreateMedium() in which case uuidMachineRegistry
|
---|
972 | * is empty since starting with VirtualBox 4.0, we no longer add opened media to a
|
---|
973 | * registry automatically (this is deferred until the medium is attached to a machine).
|
---|
974 | *
|
---|
975 | * This also gets called when VirtualBox creates diff images; in this case uuidMachineRegistry
|
---|
976 | * is set to the registry of the parent image to make sure they all end up in the same
|
---|
977 | * file.
|
---|
978 | *
|
---|
979 | * For hard disks that don't have the MediumFormatCapabilities_CreateFixed or
|
---|
980 | * MediumFormatCapabilities_CreateDynamic capability (and therefore cannot be created or deleted
|
---|
981 | * with the means of VirtualBox) the associated storage unit is assumed to be
|
---|
982 | * ready for use so the state of the hard disk object will be set to Created.
|
---|
983 | *
|
---|
984 | * @param aVirtualBox VirtualBox object.
|
---|
985 | * @param aFormat
|
---|
986 | * @param aLocation Storage unit location.
|
---|
987 | * @param uuidMachineRegistry The registry to which this medium should be added
|
---|
988 | * (global registry UUID or machine UUID or empty if none).
|
---|
989 | * @param aDeviceType Device Type.
|
---|
990 | */
|
---|
991 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
992 | const Utf8Str &aFormat,
|
---|
993 | const Utf8Str &aLocation,
|
---|
994 | const Guid &uuidMachineRegistry,
|
---|
995 | const DeviceType_T aDeviceType)
|
---|
996 | {
|
---|
997 | AssertReturn(aVirtualBox != NULL, E_FAIL);
|
---|
998 | AssertReturn(!aFormat.isEmpty(), E_FAIL);
|
---|
999 |
|
---|
1000 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1001 | AutoInitSpan autoInitSpan(this);
|
---|
1002 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1003 |
|
---|
1004 | HRESULT rc = S_OK;
|
---|
1005 |
|
---|
1006 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1007 |
|
---|
1008 | if (uuidMachineRegistry.isValid() && !uuidMachineRegistry.isZero())
|
---|
1009 | m->llRegistryIDs.push_back(uuidMachineRegistry);
|
---|
1010 |
|
---|
1011 | /* no storage yet */
|
---|
1012 | m->state = MediumState_NotCreated;
|
---|
1013 |
|
---|
1014 | /* cannot be a host drive */
|
---|
1015 | m->hostDrive = false;
|
---|
1016 |
|
---|
1017 | m->devType = aDeviceType;
|
---|
1018 |
|
---|
1019 | /* No storage unit is created yet, no need to call Medium::i_queryInfo */
|
---|
1020 |
|
---|
1021 | rc = i_setFormat(aFormat);
|
---|
1022 | if (FAILED(rc)) return rc;
|
---|
1023 |
|
---|
1024 | rc = i_setLocation(aLocation);
|
---|
1025 | if (FAILED(rc)) return rc;
|
---|
1026 |
|
---|
1027 | if (!(m->formatObj->i_getCapabilities() & ( MediumFormatCapabilities_CreateFixed
|
---|
1028 | | MediumFormatCapabilities_CreateDynamic))
|
---|
1029 | )
|
---|
1030 | {
|
---|
1031 | /* Storage for mediums of this format can neither be explicitly
|
---|
1032 | * created by VirtualBox nor deleted, so we place the medium to
|
---|
1033 | * Inaccessible state here and also add it to the registry. The
|
---|
1034 | * state means that one has to use RefreshState() to update the
|
---|
1035 | * medium format specific fields. */
|
---|
1036 | m->state = MediumState_Inaccessible;
|
---|
1037 | // create new UUID
|
---|
1038 | unconst(m->id).create();
|
---|
1039 |
|
---|
1040 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1041 | ComObjPtr<Medium> pMedium;
|
---|
1042 |
|
---|
1043 | /*
|
---|
1044 | * Check whether the UUID is taken already and create a new one
|
---|
1045 | * if required.
|
---|
1046 | * Try this only a limited amount of times in case the PRNG is broken
|
---|
1047 | * in some way to prevent an endless loop.
|
---|
1048 | */
|
---|
1049 | for (unsigned i = 0; i < 5; i++)
|
---|
1050 | {
|
---|
1051 | bool fInUse;
|
---|
1052 |
|
---|
1053 | fInUse = m->pVirtualBox->i_isMediaUuidInUse(m->id, aDeviceType);
|
---|
1054 | if (fInUse)
|
---|
1055 | {
|
---|
1056 | // create new UUID
|
---|
1057 | unconst(m->id).create();
|
---|
1058 | }
|
---|
1059 | else
|
---|
1060 | break;
|
---|
1061 | }
|
---|
1062 |
|
---|
1063 | rc = m->pVirtualBox->i_registerMedium(this, &pMedium, treeLock);
|
---|
1064 | Assert(this == pMedium || FAILED(rc));
|
---|
1065 | }
|
---|
1066 |
|
---|
1067 | /* Confirm a successful initialization when it's the case */
|
---|
1068 | if (SUCCEEDED(rc))
|
---|
1069 | autoInitSpan.setSucceeded();
|
---|
1070 |
|
---|
1071 | return rc;
|
---|
1072 | }
|
---|
1073 |
|
---|
1074 | /**
|
---|
1075 | * Initializes the medium object by opening the storage unit at the specified
|
---|
1076 | * location. The enOpenMode parameter defines whether the medium will be opened
|
---|
1077 | * read/write or read-only.
|
---|
1078 | *
|
---|
1079 | * This gets called by VirtualBox::OpenMedium() and also by
|
---|
1080 | * Machine::AttachDevice() and createImplicitDiffs() when new diff
|
---|
1081 | * images are created.
|
---|
1082 | *
|
---|
1083 | * There is no registry for this case since starting with VirtualBox 4.0, we
|
---|
1084 | * no longer add opened media to a registry automatically (this is deferred
|
---|
1085 | * until the medium is attached to a machine).
|
---|
1086 | *
|
---|
1087 | * For hard disks, the UUID, format and the parent of this medium will be
|
---|
1088 | * determined when reading the medium storage unit. For DVD and floppy images,
|
---|
1089 | * which have no UUIDs in their storage units, new UUIDs are created.
|
---|
1090 | * If the detected or set parent is not known to VirtualBox, then this method
|
---|
1091 | * will fail.
|
---|
1092 | *
|
---|
1093 | * @param aVirtualBox VirtualBox object.
|
---|
1094 | * @param aLocation Storage unit location.
|
---|
1095 | * @param enOpenMode Whether to open the medium read/write or read-only.
|
---|
1096 | * @param fForceNewUuid Whether a new UUID should be set to avoid duplicates.
|
---|
1097 | * @param aDeviceType Device type of medium.
|
---|
1098 | */
|
---|
1099 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1100 | const Utf8Str &aLocation,
|
---|
1101 | HDDOpenMode enOpenMode,
|
---|
1102 | bool fForceNewUuid,
|
---|
1103 | DeviceType_T aDeviceType)
|
---|
1104 | {
|
---|
1105 | AssertReturn(aVirtualBox, E_INVALIDARG);
|
---|
1106 | AssertReturn(!aLocation.isEmpty(), E_INVALIDARG);
|
---|
1107 |
|
---|
1108 | HRESULT rc = S_OK;
|
---|
1109 |
|
---|
1110 | {
|
---|
1111 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1112 | AutoInitSpan autoInitSpan(this);
|
---|
1113 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1114 |
|
---|
1115 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1116 |
|
---|
1117 | /* there must be a storage unit */
|
---|
1118 | m->state = MediumState_Created;
|
---|
1119 |
|
---|
1120 | /* remember device type for correct unregistering later */
|
---|
1121 | m->devType = aDeviceType;
|
---|
1122 |
|
---|
1123 | /* cannot be a host drive */
|
---|
1124 | m->hostDrive = false;
|
---|
1125 |
|
---|
1126 | /* remember the open mode (defaults to ReadWrite) */
|
---|
1127 | m->hddOpenMode = enOpenMode;
|
---|
1128 |
|
---|
1129 | if (aDeviceType == DeviceType_DVD)
|
---|
1130 | m->type = MediumType_Readonly;
|
---|
1131 | else if (aDeviceType == DeviceType_Floppy)
|
---|
1132 | m->type = MediumType_Writethrough;
|
---|
1133 |
|
---|
1134 | rc = i_setLocation(aLocation);
|
---|
1135 | if (FAILED(rc)) return rc;
|
---|
1136 |
|
---|
1137 | /* get all the information about the medium from the storage unit */
|
---|
1138 | if (fForceNewUuid)
|
---|
1139 | unconst(m->uuidImage).create();
|
---|
1140 |
|
---|
1141 | m->state = MediumState_Inaccessible;
|
---|
1142 | m->strLastAccessError = tr("Accessibility check was not yet performed");
|
---|
1143 |
|
---|
1144 | /* Confirm a successful initialization before the call to i_queryInfo.
|
---|
1145 | * Otherwise we can end up with a AutoCaller deadlock because the
|
---|
1146 | * medium becomes visible but is not marked as initialized. Causes
|
---|
1147 | * locking trouble (e.g. trying to save media registries) which is
|
---|
1148 | * hard to solve. */
|
---|
1149 | autoInitSpan.setSucceeded();
|
---|
1150 | }
|
---|
1151 |
|
---|
1152 | /* we're normal code from now on, no longer init */
|
---|
1153 | AutoCaller autoCaller(this);
|
---|
1154 | if (FAILED(autoCaller.rc()))
|
---|
1155 | return autoCaller.rc();
|
---|
1156 |
|
---|
1157 | /* need to call i_queryInfo immediately to correctly place the medium in
|
---|
1158 | * the respective media tree and update other information such as uuid */
|
---|
1159 | rc = i_queryInfo(fForceNewUuid /* fSetImageId */, false /* fSetParentId */,
|
---|
1160 | autoCaller);
|
---|
1161 | if (SUCCEEDED(rc))
|
---|
1162 | {
|
---|
1163 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1164 |
|
---|
1165 | /* if the storage unit is not accessible, it's not acceptable for the
|
---|
1166 | * newly opened media so convert this into an error */
|
---|
1167 | if (m->state == MediumState_Inaccessible)
|
---|
1168 | {
|
---|
1169 | Assert(!m->strLastAccessError.isEmpty());
|
---|
1170 | rc = setError(E_FAIL, "%s", m->strLastAccessError.c_str());
|
---|
1171 | alock.release();
|
---|
1172 | autoCaller.release();
|
---|
1173 | uninit();
|
---|
1174 | }
|
---|
1175 | else
|
---|
1176 | {
|
---|
1177 | AssertStmt(!m->id.isZero(),
|
---|
1178 | alock.release(); autoCaller.release(); uninit(); return E_FAIL);
|
---|
1179 |
|
---|
1180 | /* storage format must be detected by Medium::i_queryInfo if the
|
---|
1181 | * medium is accessible */
|
---|
1182 | AssertStmt(!m->strFormat.isEmpty(),
|
---|
1183 | alock.release(); autoCaller.release(); uninit(); return E_FAIL);
|
---|
1184 | }
|
---|
1185 | }
|
---|
1186 | else
|
---|
1187 | {
|
---|
1188 | /* opening this image failed, mark the object as dead */
|
---|
1189 | autoCaller.release();
|
---|
1190 | uninit();
|
---|
1191 | }
|
---|
1192 |
|
---|
1193 | return rc;
|
---|
1194 | }
|
---|
1195 |
|
---|
1196 | /**
|
---|
1197 | * Initializes the medium object by loading its data from the given settings
|
---|
1198 | * node. The medium will always be opened read/write.
|
---|
1199 | *
|
---|
1200 | * In this case, since we're loading from a registry, uuidMachineRegistry is
|
---|
1201 | * always set: it's either the global registry UUID or a machine UUID when
|
---|
1202 | * loading from a per-machine registry.
|
---|
1203 | *
|
---|
1204 | * @param aParent Parent medium disk or NULL for a root (base) medium.
|
---|
1205 | * @param aDeviceType Device type of the medium.
|
---|
1206 | * @param uuidMachineRegistry The registry to which this medium should be
|
---|
1207 | * added (global registry UUID or machine UUID).
|
---|
1208 | * @param data Configuration settings.
|
---|
1209 | * @param strMachineFolder The machine folder with which to resolve relative paths;
|
---|
1210 | * if empty, then we use the VirtualBox home directory
|
---|
1211 | *
|
---|
1212 | * @note Locks the medium tree for writing.
|
---|
1213 | */
|
---|
1214 | HRESULT Medium::initOne(Medium *aParent,
|
---|
1215 | DeviceType_T aDeviceType,
|
---|
1216 | const Guid &uuidMachineRegistry,
|
---|
1217 | const settings::Medium &data,
|
---|
1218 | const Utf8Str &strMachineFolder)
|
---|
1219 | {
|
---|
1220 | HRESULT rc;
|
---|
1221 |
|
---|
1222 | if (uuidMachineRegistry.isValid() && !uuidMachineRegistry.isZero())
|
---|
1223 | m->llRegistryIDs.push_back(uuidMachineRegistry);
|
---|
1224 |
|
---|
1225 | /* register with VirtualBox/parent early, since uninit() will
|
---|
1226 | * unconditionally unregister on failure */
|
---|
1227 | if (aParent)
|
---|
1228 | {
|
---|
1229 | // differencing medium: add to parent
|
---|
1230 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1231 | // no need to check maximum depth as settings reading did it
|
---|
1232 | i_setParent(aParent);
|
---|
1233 | }
|
---|
1234 |
|
---|
1235 | /* see below why we don't call Medium::i_queryInfo (and therefore treat
|
---|
1236 | * the medium as inaccessible for now */
|
---|
1237 | m->state = MediumState_Inaccessible;
|
---|
1238 | m->strLastAccessError = tr("Accessibility check was not yet performed");
|
---|
1239 |
|
---|
1240 | /* required */
|
---|
1241 | unconst(m->id) = data.uuid;
|
---|
1242 |
|
---|
1243 | /* assume not a host drive */
|
---|
1244 | m->hostDrive = false;
|
---|
1245 |
|
---|
1246 | /* optional */
|
---|
1247 | m->strDescription = data.strDescription;
|
---|
1248 |
|
---|
1249 | /* required */
|
---|
1250 | if (aDeviceType == DeviceType_HardDisk)
|
---|
1251 | {
|
---|
1252 | AssertReturn(!data.strFormat.isEmpty(), E_FAIL);
|
---|
1253 | rc = i_setFormat(data.strFormat);
|
---|
1254 | if (FAILED(rc)) return rc;
|
---|
1255 | }
|
---|
1256 | else
|
---|
1257 | {
|
---|
1258 | /// @todo handle host drive settings here as well?
|
---|
1259 | if (!data.strFormat.isEmpty())
|
---|
1260 | rc = i_setFormat(data.strFormat);
|
---|
1261 | else
|
---|
1262 | rc = i_setFormat("RAW");
|
---|
1263 | if (FAILED(rc)) return rc;
|
---|
1264 | }
|
---|
1265 |
|
---|
1266 | /* optional, only for diffs, default is false; we can only auto-reset
|
---|
1267 | * diff media so they must have a parent */
|
---|
1268 | if (aParent != NULL)
|
---|
1269 | m->autoReset = data.fAutoReset;
|
---|
1270 | else
|
---|
1271 | m->autoReset = false;
|
---|
1272 |
|
---|
1273 | /* properties (after setting the format as it populates the map). Note that
|
---|
1274 | * if some properties are not supported but present in the settings file,
|
---|
1275 | * they will still be read and accessible (for possible backward
|
---|
1276 | * compatibility; we can also clean them up from the XML upon next
|
---|
1277 | * XML format version change if we wish) */
|
---|
1278 | for (settings::StringsMap::const_iterator it = data.properties.begin();
|
---|
1279 | it != data.properties.end();
|
---|
1280 | ++it)
|
---|
1281 | {
|
---|
1282 | const Utf8Str &name = it->first;
|
---|
1283 | const Utf8Str &value = it->second;
|
---|
1284 | m->mapProperties[name] = value;
|
---|
1285 | }
|
---|
1286 |
|
---|
1287 | /* try to decrypt an optional iSCSI initiator secret */
|
---|
1288 | settings::StringsMap::const_iterator itCph = data.properties.find("InitiatorSecretEncrypted");
|
---|
1289 | if ( itCph != data.properties.end()
|
---|
1290 | && !itCph->second.isEmpty())
|
---|
1291 | {
|
---|
1292 | Utf8Str strPlaintext;
|
---|
1293 | int vrc = m->pVirtualBox->i_decryptSetting(&strPlaintext, itCph->second);
|
---|
1294 | if (RT_SUCCESS(vrc))
|
---|
1295 | m->mapProperties["InitiatorSecret"] = strPlaintext;
|
---|
1296 | }
|
---|
1297 |
|
---|
1298 | Utf8Str strFull;
|
---|
1299 | if (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
1300 | {
|
---|
1301 | // compose full path of the medium, if it's not fully qualified...
|
---|
1302 | // slightly convoluted logic here. If the caller has given us a
|
---|
1303 | // machine folder, then a relative path will be relative to that:
|
---|
1304 | if ( !strMachineFolder.isEmpty()
|
---|
1305 | && !RTPathStartsWithRoot(data.strLocation.c_str())
|
---|
1306 | )
|
---|
1307 | {
|
---|
1308 | strFull = strMachineFolder;
|
---|
1309 | strFull += RTPATH_SLASH;
|
---|
1310 | strFull += data.strLocation;
|
---|
1311 | }
|
---|
1312 | else
|
---|
1313 | {
|
---|
1314 | // Otherwise use the old VirtualBox "make absolute path" logic:
|
---|
1315 | rc = m->pVirtualBox->i_calculateFullPath(data.strLocation, strFull);
|
---|
1316 | if (FAILED(rc)) return rc;
|
---|
1317 | }
|
---|
1318 | }
|
---|
1319 | else
|
---|
1320 | strFull = data.strLocation;
|
---|
1321 |
|
---|
1322 | rc = i_setLocation(strFull);
|
---|
1323 | if (FAILED(rc)) return rc;
|
---|
1324 |
|
---|
1325 | if (aDeviceType == DeviceType_HardDisk)
|
---|
1326 | {
|
---|
1327 | /* type is only for base hard disks */
|
---|
1328 | if (m->pParent.isNull())
|
---|
1329 | m->type = data.hdType;
|
---|
1330 | }
|
---|
1331 | else if (aDeviceType == DeviceType_DVD)
|
---|
1332 | m->type = MediumType_Readonly;
|
---|
1333 | else
|
---|
1334 | m->type = MediumType_Writethrough;
|
---|
1335 |
|
---|
1336 | /* remember device type for correct unregistering later */
|
---|
1337 | m->devType = aDeviceType;
|
---|
1338 |
|
---|
1339 | LogFlowThisFunc(("m->strLocationFull='%s', m->strFormat=%s, m->id={%RTuuid}\n",
|
---|
1340 | m->strLocationFull.c_str(), m->strFormat.c_str(), m->id.raw()));
|
---|
1341 |
|
---|
1342 | return S_OK;
|
---|
1343 | }
|
---|
1344 |
|
---|
1345 | /**
|
---|
1346 | * Initializes the medium object and its children by loading its data from the
|
---|
1347 | * given settings node. The medium will always be opened read/write.
|
---|
1348 | *
|
---|
1349 | * In this case, since we're loading from a registry, uuidMachineRegistry is
|
---|
1350 | * always set: it's either the global registry UUID or a machine UUID when
|
---|
1351 | * loading from a per-machine registry.
|
---|
1352 | *
|
---|
1353 | * @param aVirtualBox VirtualBox object.
|
---|
1354 | * @param aParent Parent medium disk or NULL for a root (base) medium.
|
---|
1355 | * @param aDeviceType Device type of the medium.
|
---|
1356 | * @param uuidMachineRegistry The registry to which this medium should be added
|
---|
1357 | * (global registry UUID or machine UUID).
|
---|
1358 | * @param data Configuration settings.
|
---|
1359 | * @param strMachineFolder The machine folder with which to resolve relative
|
---|
1360 | * paths; if empty, then we use the VirtualBox home directory
|
---|
1361 | * @param mediaTreeLock Autolock.
|
---|
1362 | *
|
---|
1363 | * @note Locks the medium tree for writing.
|
---|
1364 | */
|
---|
1365 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1366 | Medium *aParent,
|
---|
1367 | DeviceType_T aDeviceType,
|
---|
1368 | const Guid &uuidMachineRegistry,
|
---|
1369 | const settings::Medium &data,
|
---|
1370 | const Utf8Str &strMachineFolder,
|
---|
1371 | AutoWriteLock &mediaTreeLock)
|
---|
1372 | {
|
---|
1373 | using namespace settings;
|
---|
1374 |
|
---|
1375 | Assert(aVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
1376 | AssertReturn(aVirtualBox, E_INVALIDARG);
|
---|
1377 |
|
---|
1378 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1379 | AutoInitSpan autoInitSpan(this);
|
---|
1380 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1381 |
|
---|
1382 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1383 |
|
---|
1384 | // Do not inline this method call, as the purpose of having this separate
|
---|
1385 | // is to save on stack size. Less local variables are the key for reaching
|
---|
1386 | // deep recursion levels with small stack (XPCOM/g++ without optimization).
|
---|
1387 | HRESULT rc = initOne(aParent, aDeviceType, uuidMachineRegistry, data, strMachineFolder);
|
---|
1388 |
|
---|
1389 |
|
---|
1390 | /* Don't call Medium::i_queryInfo for registered media to prevent the calling
|
---|
1391 | * thread (i.e. the VirtualBox server startup thread) from an unexpected
|
---|
1392 | * freeze but mark it as initially inaccessible instead. The vital UUID,
|
---|
1393 | * location and format properties are read from the registry file above; to
|
---|
1394 | * get the actual state and the rest of the data, the user will have to call
|
---|
1395 | * COMGETTER(State). */
|
---|
1396 |
|
---|
1397 | /* load all children */
|
---|
1398 | for (settings::MediaList::const_iterator it = data.llChildren.begin();
|
---|
1399 | it != data.llChildren.end();
|
---|
1400 | ++it)
|
---|
1401 | {
|
---|
1402 | const settings::Medium &med = *it;
|
---|
1403 |
|
---|
1404 | ComObjPtr<Medium> pMedium;
|
---|
1405 | pMedium.createObject();
|
---|
1406 | rc = pMedium->init(aVirtualBox,
|
---|
1407 | this, // parent
|
---|
1408 | aDeviceType,
|
---|
1409 | uuidMachineRegistry,
|
---|
1410 | med, // child data
|
---|
1411 | strMachineFolder,
|
---|
1412 | mediaTreeLock);
|
---|
1413 | if (FAILED(rc)) break;
|
---|
1414 |
|
---|
1415 | rc = m->pVirtualBox->i_registerMedium(pMedium, &pMedium, mediaTreeLock);
|
---|
1416 | if (FAILED(rc)) break;
|
---|
1417 | }
|
---|
1418 |
|
---|
1419 | /* Confirm a successful initialization when it's the case */
|
---|
1420 | if (SUCCEEDED(rc))
|
---|
1421 | autoInitSpan.setSucceeded();
|
---|
1422 |
|
---|
1423 | return rc;
|
---|
1424 | }
|
---|
1425 |
|
---|
1426 | /**
|
---|
1427 | * Initializes the medium object by providing the host drive information.
|
---|
1428 | * Not used for anything but the host floppy/host DVD case.
|
---|
1429 | *
|
---|
1430 | * There is no registry for this case.
|
---|
1431 | *
|
---|
1432 | * @param aVirtualBox VirtualBox object.
|
---|
1433 | * @param aDeviceType Device type of the medium.
|
---|
1434 | * @param aLocation Location of the host drive.
|
---|
1435 | * @param aDescription Comment for this host drive.
|
---|
1436 | *
|
---|
1437 | * @note Locks VirtualBox lock for writing.
|
---|
1438 | */
|
---|
1439 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1440 | DeviceType_T aDeviceType,
|
---|
1441 | const Utf8Str &aLocation,
|
---|
1442 | const Utf8Str &aDescription /* = Utf8Str::Empty */)
|
---|
1443 | {
|
---|
1444 | ComAssertRet(aDeviceType == DeviceType_DVD || aDeviceType == DeviceType_Floppy, E_INVALIDARG);
|
---|
1445 | ComAssertRet(!aLocation.isEmpty(), E_INVALIDARG);
|
---|
1446 |
|
---|
1447 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1448 | AutoInitSpan autoInitSpan(this);
|
---|
1449 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1450 |
|
---|
1451 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1452 |
|
---|
1453 | // We do not store host drives in VirtualBox.xml or anywhere else, so if we want
|
---|
1454 | // host drives to be identifiable by UUID and not give the drive a different UUID
|
---|
1455 | // every time VirtualBox starts, we need to fake a reproducible UUID here:
|
---|
1456 | RTUUID uuid;
|
---|
1457 | RTUuidClear(&uuid);
|
---|
1458 | if (aDeviceType == DeviceType_DVD)
|
---|
1459 | memcpy(&uuid.au8[0], "DVD", 3);
|
---|
1460 | else
|
---|
1461 | memcpy(&uuid.au8[0], "FD", 2);
|
---|
1462 | /* use device name, adjusted to the end of uuid, shortened if necessary */
|
---|
1463 | size_t lenLocation = aLocation.length();
|
---|
1464 | if (lenLocation > 12)
|
---|
1465 | memcpy(&uuid.au8[4], aLocation.c_str() + (lenLocation - 12), 12);
|
---|
1466 | else
|
---|
1467 | memcpy(&uuid.au8[4 + 12 - lenLocation], aLocation.c_str(), lenLocation);
|
---|
1468 | unconst(m->id) = uuid;
|
---|
1469 |
|
---|
1470 | if (aDeviceType == DeviceType_DVD)
|
---|
1471 | m->type = MediumType_Readonly;
|
---|
1472 | else
|
---|
1473 | m->type = MediumType_Writethrough;
|
---|
1474 | m->devType = aDeviceType;
|
---|
1475 | m->state = MediumState_Created;
|
---|
1476 | m->hostDrive = true;
|
---|
1477 | HRESULT rc = i_setFormat("RAW");
|
---|
1478 | if (FAILED(rc)) return rc;
|
---|
1479 | rc = i_setLocation(aLocation);
|
---|
1480 | if (FAILED(rc)) return rc;
|
---|
1481 | m->strDescription = aDescription;
|
---|
1482 |
|
---|
1483 | autoInitSpan.setSucceeded();
|
---|
1484 | return S_OK;
|
---|
1485 | }
|
---|
1486 |
|
---|
1487 | /**
|
---|
1488 | * Uninitializes the instance.
|
---|
1489 | *
|
---|
1490 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
1491 | *
|
---|
1492 | * @note All children of this medium get uninitialized by calling their
|
---|
1493 | * uninit() methods.
|
---|
1494 | */
|
---|
1495 | void Medium::uninit()
|
---|
1496 | {
|
---|
1497 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1498 | * the pVirtualBox reference, and in this case we don't need to continue.
|
---|
1499 | * Normally this would be handled through the AutoUninitSpan magic, however
|
---|
1500 | * this cannot be done at this point as the media tree must be locked
|
---|
1501 | * before reaching the AutoUninitSpan, otherwise deadlocks can happen.
|
---|
1502 | *
|
---|
1503 | * NOTE: The tree lock is higher priority than the medium caller and medium
|
---|
1504 | * object locks, i.e. the medium caller may have to be released and be
|
---|
1505 | * re-acquired in the right place later. See Medium::getParent() for sample
|
---|
1506 | * code how to do this safely. */
|
---|
1507 | VirtualBox *pVirtualBox = m->pVirtualBox;
|
---|
1508 | if (!pVirtualBox)
|
---|
1509 | return;
|
---|
1510 |
|
---|
1511 | /* Caller must not hold the object or media tree lock over uninit(). */
|
---|
1512 | Assert(!isWriteLockOnCurrentThread());
|
---|
1513 | Assert(!pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
1514 |
|
---|
1515 | AutoWriteLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1516 |
|
---|
1517 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
1518 | AutoUninitSpan autoUninitSpan(this);
|
---|
1519 | if (autoUninitSpan.uninitDone())
|
---|
1520 | return;
|
---|
1521 |
|
---|
1522 | if (!m->formatObj.isNull())
|
---|
1523 | m->formatObj.setNull();
|
---|
1524 |
|
---|
1525 | if (m->state == MediumState_Deleting)
|
---|
1526 | {
|
---|
1527 | /* This medium has been already deleted (directly or as part of a
|
---|
1528 | * merge). Reparenting has already been done. */
|
---|
1529 | Assert(m->pParent.isNull());
|
---|
1530 | }
|
---|
1531 | else
|
---|
1532 | {
|
---|
1533 | MediaList llChildren(m->llChildren);
|
---|
1534 | m->llChildren.clear();
|
---|
1535 | autoUninitSpan.setSucceeded();
|
---|
1536 |
|
---|
1537 | while (!llChildren.empty())
|
---|
1538 | {
|
---|
1539 | ComObjPtr<Medium> pChild = llChildren.front();
|
---|
1540 | llChildren.pop_front();
|
---|
1541 | pChild->m->pParent.setNull();
|
---|
1542 | treeLock.release();
|
---|
1543 | pChild->uninit();
|
---|
1544 | treeLock.acquire();
|
---|
1545 | }
|
---|
1546 |
|
---|
1547 | if (m->pParent)
|
---|
1548 | {
|
---|
1549 | // this is a differencing disk: then remove it from the parent's children list
|
---|
1550 | i_deparent();
|
---|
1551 | }
|
---|
1552 | }
|
---|
1553 |
|
---|
1554 | unconst(m->pVirtualBox) = NULL;
|
---|
1555 | }
|
---|
1556 |
|
---|
1557 | /**
|
---|
1558 | * Internal helper that removes "this" from the list of children of its
|
---|
1559 | * parent. Used in uninit() and other places when reparenting is necessary.
|
---|
1560 | *
|
---|
1561 | * The caller must hold the medium tree lock!
|
---|
1562 | */
|
---|
1563 | void Medium::i_deparent()
|
---|
1564 | {
|
---|
1565 | MediaList &llParent = m->pParent->m->llChildren;
|
---|
1566 | for (MediaList::iterator it = llParent.begin();
|
---|
1567 | it != llParent.end();
|
---|
1568 | ++it)
|
---|
1569 | {
|
---|
1570 | Medium *pParentsChild = *it;
|
---|
1571 | if (this == pParentsChild)
|
---|
1572 | {
|
---|
1573 | llParent.erase(it);
|
---|
1574 | break;
|
---|
1575 | }
|
---|
1576 | }
|
---|
1577 | m->pParent.setNull();
|
---|
1578 | }
|
---|
1579 |
|
---|
1580 | /**
|
---|
1581 | * Internal helper that removes "this" from the list of children of its
|
---|
1582 | * parent. Used in uninit() and other places when reparenting is necessary.
|
---|
1583 | *
|
---|
1584 | * The caller must hold the medium tree lock!
|
---|
1585 | */
|
---|
1586 | void Medium::i_setParent(const ComObjPtr<Medium> &pParent)
|
---|
1587 | {
|
---|
1588 | m->pParent = pParent;
|
---|
1589 | if (pParent)
|
---|
1590 | pParent->m->llChildren.push_back(this);
|
---|
1591 | }
|
---|
1592 |
|
---|
1593 |
|
---|
1594 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1595 | //
|
---|
1596 | // IMedium public methods
|
---|
1597 | //
|
---|
1598 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1599 |
|
---|
1600 | HRESULT Medium::getId(com::Guid &aId)
|
---|
1601 | {
|
---|
1602 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1603 |
|
---|
1604 | aId = m->id;
|
---|
1605 |
|
---|
1606 | return S_OK;
|
---|
1607 | }
|
---|
1608 |
|
---|
1609 | HRESULT Medium::getDescription(com::Utf8Str &aDescription)
|
---|
1610 | {
|
---|
1611 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1612 |
|
---|
1613 | aDescription = m->strDescription;
|
---|
1614 |
|
---|
1615 | return S_OK;
|
---|
1616 | }
|
---|
1617 |
|
---|
1618 | HRESULT Medium::setDescription(const com::Utf8Str &aDescription)
|
---|
1619 | {
|
---|
1620 | /// @todo update m->strDescription and save the global registry (and local
|
---|
1621 | /// registries of portable VMs referring to this medium), this will also
|
---|
1622 | /// require to add the mRegistered flag to data
|
---|
1623 |
|
---|
1624 | HRESULT rc = S_OK;
|
---|
1625 |
|
---|
1626 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
1627 |
|
---|
1628 | try
|
---|
1629 | {
|
---|
1630 | // locking: we need the tree lock first because we access parent pointers
|
---|
1631 | // and we need to write-lock the media involved
|
---|
1632 | uint32_t cHandles = 2;
|
---|
1633 | LockHandle* pHandles[2] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
1634 | this->lockHandle() };
|
---|
1635 |
|
---|
1636 | AutoWriteLock alock(cHandles,
|
---|
1637 | pHandles
|
---|
1638 | COMMA_LOCKVAL_SRC_POS);
|
---|
1639 |
|
---|
1640 | /* Build the lock list. */
|
---|
1641 | alock.release();
|
---|
1642 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
1643 | this /* pToLockWrite */,
|
---|
1644 | true /* fMediumLockWriteAll */,
|
---|
1645 | NULL,
|
---|
1646 | *pMediumLockList);
|
---|
1647 | alock.acquire();
|
---|
1648 |
|
---|
1649 | if (FAILED(rc))
|
---|
1650 | {
|
---|
1651 | throw setError(rc,
|
---|
1652 | tr("Failed to create medium lock list for '%s'"),
|
---|
1653 | i_getLocationFull().c_str());
|
---|
1654 | }
|
---|
1655 |
|
---|
1656 | alock.release();
|
---|
1657 | rc = pMediumLockList->Lock();
|
---|
1658 | alock.acquire();
|
---|
1659 |
|
---|
1660 | if (FAILED(rc))
|
---|
1661 | {
|
---|
1662 | throw setError(rc,
|
---|
1663 | tr("Failed to lock media '%s'"),
|
---|
1664 | i_getLocationFull().c_str());
|
---|
1665 | }
|
---|
1666 |
|
---|
1667 | /* Set a new description */
|
---|
1668 | if (SUCCEEDED(rc))
|
---|
1669 | {
|
---|
1670 | m->strDescription = aDescription;
|
---|
1671 | }
|
---|
1672 |
|
---|
1673 | // save the settings
|
---|
1674 | alock.release();
|
---|
1675 | i_markRegistriesModified();
|
---|
1676 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
1677 | }
|
---|
1678 | catch (HRESULT aRC) { rc = aRC; }
|
---|
1679 |
|
---|
1680 | delete pMediumLockList;
|
---|
1681 |
|
---|
1682 | return rc;
|
---|
1683 | }
|
---|
1684 |
|
---|
1685 | HRESULT Medium::getState(MediumState_T *aState)
|
---|
1686 | {
|
---|
1687 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1688 | *aState = m->state;
|
---|
1689 |
|
---|
1690 | return S_OK;
|
---|
1691 | }
|
---|
1692 |
|
---|
1693 | HRESULT Medium::getVariant(std::vector<MediumVariant_T> &aVariant)
|
---|
1694 | {
|
---|
1695 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1696 |
|
---|
1697 | const size_t cBits = sizeof(MediumVariant_T) * 8;
|
---|
1698 | aVariant.resize(cBits);
|
---|
1699 | for (size_t i = 0; i < cBits; ++i)
|
---|
1700 | aVariant[i] = (MediumVariant_T)(m->variant & RT_BIT(i));
|
---|
1701 |
|
---|
1702 | return S_OK;
|
---|
1703 | }
|
---|
1704 |
|
---|
1705 | HRESULT Medium::getLocation(com::Utf8Str &aLocation)
|
---|
1706 | {
|
---|
1707 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1708 |
|
---|
1709 | aLocation = m->strLocationFull;
|
---|
1710 |
|
---|
1711 | return S_OK;
|
---|
1712 | }
|
---|
1713 |
|
---|
1714 | HRESULT Medium::getName(com::Utf8Str &aName)
|
---|
1715 | {
|
---|
1716 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1717 |
|
---|
1718 | aName = i_getName();
|
---|
1719 |
|
---|
1720 | return S_OK;
|
---|
1721 | }
|
---|
1722 |
|
---|
1723 | HRESULT Medium::getDeviceType(DeviceType_T *aDeviceType)
|
---|
1724 | {
|
---|
1725 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1726 |
|
---|
1727 | *aDeviceType = m->devType;
|
---|
1728 |
|
---|
1729 | return S_OK;
|
---|
1730 | }
|
---|
1731 |
|
---|
1732 | HRESULT Medium::getHostDrive(BOOL *aHostDrive)
|
---|
1733 | {
|
---|
1734 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1735 |
|
---|
1736 | *aHostDrive = m->hostDrive;
|
---|
1737 |
|
---|
1738 | return S_OK;
|
---|
1739 | }
|
---|
1740 |
|
---|
1741 | HRESULT Medium::getSize(LONG64 *aSize)
|
---|
1742 | {
|
---|
1743 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1744 |
|
---|
1745 | *aSize = m->size;
|
---|
1746 |
|
---|
1747 | return S_OK;
|
---|
1748 | }
|
---|
1749 |
|
---|
1750 | HRESULT Medium::getFormat(com::Utf8Str &aFormat)
|
---|
1751 | {
|
---|
1752 | /* no need to lock, m->strFormat is const */
|
---|
1753 |
|
---|
1754 | aFormat = m->strFormat;
|
---|
1755 | return S_OK;
|
---|
1756 | }
|
---|
1757 |
|
---|
1758 | HRESULT Medium::getMediumFormat(ComPtr<IMediumFormat> &aMediumFormat)
|
---|
1759 | {
|
---|
1760 | /* no need to lock, m->formatObj is const */
|
---|
1761 | m->formatObj.queryInterfaceTo(aMediumFormat.asOutParam());
|
---|
1762 |
|
---|
1763 | return S_OK;
|
---|
1764 | }
|
---|
1765 |
|
---|
1766 | HRESULT Medium::getType(AutoCaller &autoCaller, MediumType_T *aType)
|
---|
1767 | {
|
---|
1768 | NOREF(autoCaller);
|
---|
1769 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1770 |
|
---|
1771 | *aType = m->type;
|
---|
1772 |
|
---|
1773 | return S_OK;
|
---|
1774 | }
|
---|
1775 |
|
---|
1776 | HRESULT Medium::setType(AutoCaller &autoCaller, MediumType_T aType)
|
---|
1777 | {
|
---|
1778 | autoCaller.release();
|
---|
1779 |
|
---|
1780 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1781 | * the pVirtualBox reference, see #uninit(). */
|
---|
1782 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
1783 |
|
---|
1784 | // we access m->pParent
|
---|
1785 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
1786 |
|
---|
1787 | autoCaller.add();
|
---|
1788 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1789 |
|
---|
1790 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1791 |
|
---|
1792 | switch (m->state)
|
---|
1793 | {
|
---|
1794 | case MediumState_Created:
|
---|
1795 | case MediumState_Inaccessible:
|
---|
1796 | break;
|
---|
1797 | default:
|
---|
1798 | return i_setStateError();
|
---|
1799 | }
|
---|
1800 |
|
---|
1801 | if (m->type == aType)
|
---|
1802 | {
|
---|
1803 | /* Nothing to do */
|
---|
1804 | return S_OK;
|
---|
1805 | }
|
---|
1806 |
|
---|
1807 | DeviceType_T devType = i_getDeviceType();
|
---|
1808 | // DVD media can only be readonly.
|
---|
1809 | if (devType == DeviceType_DVD && aType != MediumType_Readonly)
|
---|
1810 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1811 | tr("Cannot change the type of DVD medium '%s'"),
|
---|
1812 | m->strLocationFull.c_str());
|
---|
1813 | // Floppy media can only be writethrough or readonly.
|
---|
1814 | if ( devType == DeviceType_Floppy
|
---|
1815 | && aType != MediumType_Writethrough
|
---|
1816 | && aType != MediumType_Readonly)
|
---|
1817 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1818 | tr("Cannot change the type of floppy medium '%s'"),
|
---|
1819 | m->strLocationFull.c_str());
|
---|
1820 |
|
---|
1821 | /* cannot change the type of a differencing medium */
|
---|
1822 | if (m->pParent)
|
---|
1823 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1824 | tr("Cannot change the type of medium '%s' because it is a differencing medium"),
|
---|
1825 | m->strLocationFull.c_str());
|
---|
1826 |
|
---|
1827 | /* Cannot change the type of a medium being in use by more than one VM.
|
---|
1828 | * If the change is to Immutable or MultiAttach then it must not be
|
---|
1829 | * directly attached to any VM, otherwise the assumptions about indirect
|
---|
1830 | * attachment elsewhere are violated and the VM becomes inaccessible.
|
---|
1831 | * Attaching an immutable medium triggers the diff creation, and this is
|
---|
1832 | * vital for the correct operation. */
|
---|
1833 | if ( m->backRefs.size() > 1
|
---|
1834 | || ( ( aType == MediumType_Immutable
|
---|
1835 | || aType == MediumType_MultiAttach)
|
---|
1836 | && m->backRefs.size() > 0))
|
---|
1837 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1838 | tr("Cannot change the type of medium '%s' because it is attached to %d virtual machines"),
|
---|
1839 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
1840 |
|
---|
1841 | switch (aType)
|
---|
1842 | {
|
---|
1843 | case MediumType_Normal:
|
---|
1844 | case MediumType_Immutable:
|
---|
1845 | case MediumType_MultiAttach:
|
---|
1846 | {
|
---|
1847 | /* normal can be easily converted to immutable and vice versa even
|
---|
1848 | * if they have children as long as they are not attached to any
|
---|
1849 | * machine themselves */
|
---|
1850 | break;
|
---|
1851 | }
|
---|
1852 | case MediumType_Writethrough:
|
---|
1853 | case MediumType_Shareable:
|
---|
1854 | case MediumType_Readonly:
|
---|
1855 | {
|
---|
1856 | /* cannot change to writethrough, shareable or readonly
|
---|
1857 | * if there are children */
|
---|
1858 | if (i_getChildren().size() != 0)
|
---|
1859 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
1860 | tr("Cannot change type for medium '%s' since it has %d child media"),
|
---|
1861 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
1862 | if (aType == MediumType_Shareable)
|
---|
1863 | {
|
---|
1864 | MediumVariant_T variant = i_getVariant();
|
---|
1865 | if (!(variant & MediumVariant_Fixed))
|
---|
1866 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1867 | tr("Cannot change type for medium '%s' to 'Shareable' since it is a dynamic medium storage unit"),
|
---|
1868 | m->strLocationFull.c_str());
|
---|
1869 | }
|
---|
1870 | else if (aType == MediumType_Readonly && devType == DeviceType_HardDisk)
|
---|
1871 | {
|
---|
1872 | // Readonly hard disks are not allowed, this medium type is reserved for
|
---|
1873 | // DVDs and floppy images at the moment. Later we might allow readonly hard
|
---|
1874 | // disks, but that's extremely unusual and many guest OSes will have trouble.
|
---|
1875 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1876 | tr("Cannot change type for medium '%s' to 'Readonly' since it is a hard disk"),
|
---|
1877 | m->strLocationFull.c_str());
|
---|
1878 | }
|
---|
1879 | break;
|
---|
1880 | }
|
---|
1881 | default:
|
---|
1882 | AssertFailedReturn(E_FAIL);
|
---|
1883 | }
|
---|
1884 |
|
---|
1885 | if (aType == MediumType_MultiAttach)
|
---|
1886 | {
|
---|
1887 | // This type is new with VirtualBox 4.0 and therefore requires settings
|
---|
1888 | // version 1.11 in the settings backend. Unfortunately it is not enough to do
|
---|
1889 | // the usual routine in MachineConfigFile::bumpSettingsVersionIfNeeded() for
|
---|
1890 | // two reasons: The medium type is a property of the media registry tree, which
|
---|
1891 | // can reside in the global config file (for pre-4.0 media); we would therefore
|
---|
1892 | // possibly need to bump the global config version. We don't want to do that though
|
---|
1893 | // because that might make downgrading to pre-4.0 impossible.
|
---|
1894 | // As a result, we can only use these two new types if the medium is NOT in the
|
---|
1895 | // global registry:
|
---|
1896 | const Guid &uuidGlobalRegistry = m->pVirtualBox->i_getGlobalRegistryId();
|
---|
1897 | if (i_isInRegistry(uuidGlobalRegistry))
|
---|
1898 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1899 | tr("Cannot change type for medium '%s': the media type 'MultiAttach' can only be used "
|
---|
1900 | "on media registered with a machine that was created with VirtualBox 4.0 or later"),
|
---|
1901 | m->strLocationFull.c_str());
|
---|
1902 | }
|
---|
1903 |
|
---|
1904 | m->type = aType;
|
---|
1905 |
|
---|
1906 | // save the settings
|
---|
1907 | mlock.release();
|
---|
1908 | treeLock.release();
|
---|
1909 | i_markRegistriesModified();
|
---|
1910 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
1911 |
|
---|
1912 | return S_OK;
|
---|
1913 | }
|
---|
1914 |
|
---|
1915 | HRESULT Medium::getAllowedTypes(std::vector<MediumType_T> &aAllowedTypes)
|
---|
1916 | {
|
---|
1917 | NOREF(aAllowedTypes);
|
---|
1918 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1919 |
|
---|
1920 | ReturnComNotImplemented();
|
---|
1921 | }
|
---|
1922 |
|
---|
1923 | HRESULT Medium::getParent(AutoCaller &autoCaller, ComPtr<IMedium> &aParent)
|
---|
1924 | {
|
---|
1925 | autoCaller.release();
|
---|
1926 |
|
---|
1927 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1928 | * the pVirtualBox reference, see #uninit(). */
|
---|
1929 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
1930 |
|
---|
1931 | /* we access m->pParent */
|
---|
1932 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
1933 |
|
---|
1934 | autoCaller.add();
|
---|
1935 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1936 |
|
---|
1937 | m->pParent.queryInterfaceTo(aParent.asOutParam());
|
---|
1938 |
|
---|
1939 | return S_OK;
|
---|
1940 | }
|
---|
1941 |
|
---|
1942 | HRESULT Medium::getChildren(AutoCaller &autoCaller, std::vector<ComPtr<IMedium> > &aChildren)
|
---|
1943 | {
|
---|
1944 | autoCaller.release();
|
---|
1945 |
|
---|
1946 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1947 | * the pVirtualBox reference, see #uninit(). */
|
---|
1948 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
1949 |
|
---|
1950 | /* we access children */
|
---|
1951 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
1952 |
|
---|
1953 | autoCaller.add();
|
---|
1954 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1955 |
|
---|
1956 | MediaList children(this->i_getChildren());
|
---|
1957 | aChildren.resize(children.size());
|
---|
1958 | size_t i = 0;
|
---|
1959 | for (MediaList::const_iterator it = children.begin(); it != children.end(); ++it, ++i)
|
---|
1960 | (*it).queryInterfaceTo(aChildren[i].asOutParam());
|
---|
1961 | return S_OK;
|
---|
1962 | }
|
---|
1963 |
|
---|
1964 | HRESULT Medium::getBase(AutoCaller &autoCaller, ComPtr<IMedium> &aBase)
|
---|
1965 | {
|
---|
1966 | autoCaller.release();
|
---|
1967 |
|
---|
1968 | /* i_getBase() will do callers/locking */
|
---|
1969 | i_getBase().queryInterfaceTo(aBase.asOutParam());
|
---|
1970 |
|
---|
1971 | return S_OK;
|
---|
1972 | }
|
---|
1973 |
|
---|
1974 | HRESULT Medium::getReadOnly(AutoCaller &autoCaller, BOOL *aReadOnly)
|
---|
1975 | {
|
---|
1976 | autoCaller.release();
|
---|
1977 |
|
---|
1978 | /* isReadOnly() will do locking */
|
---|
1979 | *aReadOnly = i_isReadOnly();
|
---|
1980 |
|
---|
1981 | return S_OK;
|
---|
1982 | }
|
---|
1983 |
|
---|
1984 | HRESULT Medium::getLogicalSize(LONG64 *aLogicalSize)
|
---|
1985 | {
|
---|
1986 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1987 |
|
---|
1988 | *aLogicalSize = m->logicalSize;
|
---|
1989 |
|
---|
1990 | return S_OK;
|
---|
1991 | }
|
---|
1992 |
|
---|
1993 | HRESULT Medium::getAutoReset(BOOL *aAutoReset)
|
---|
1994 | {
|
---|
1995 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1996 |
|
---|
1997 | if (m->pParent.isNull())
|
---|
1998 | *aAutoReset = FALSE;
|
---|
1999 | else
|
---|
2000 | *aAutoReset = m->autoReset;
|
---|
2001 |
|
---|
2002 | return S_OK;
|
---|
2003 | }
|
---|
2004 |
|
---|
2005 | HRESULT Medium::setAutoReset(BOOL aAutoReset)
|
---|
2006 | {
|
---|
2007 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2008 |
|
---|
2009 | if (m->pParent.isNull())
|
---|
2010 | return setError(VBOX_E_NOT_SUPPORTED,
|
---|
2011 | tr("Medium '%s' is not differencing"),
|
---|
2012 | m->strLocationFull.c_str());
|
---|
2013 |
|
---|
2014 | if (m->autoReset != !!aAutoReset)
|
---|
2015 | {
|
---|
2016 | m->autoReset = !!aAutoReset;
|
---|
2017 |
|
---|
2018 | // save the settings
|
---|
2019 | mlock.release();
|
---|
2020 | i_markRegistriesModified();
|
---|
2021 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2022 | }
|
---|
2023 |
|
---|
2024 | return S_OK;
|
---|
2025 | }
|
---|
2026 |
|
---|
2027 | HRESULT Medium::getLastAccessError(com::Utf8Str &aLastAccessError)
|
---|
2028 | {
|
---|
2029 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2030 |
|
---|
2031 | aLastAccessError = m->strLastAccessError;
|
---|
2032 |
|
---|
2033 | return S_OK;
|
---|
2034 | }
|
---|
2035 |
|
---|
2036 | HRESULT Medium::getMachineIds(std::vector<com::Guid> &aMachineIds)
|
---|
2037 | {
|
---|
2038 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2039 |
|
---|
2040 | if (m->backRefs.size() != 0)
|
---|
2041 | {
|
---|
2042 | BackRefList brlist(m->backRefs);
|
---|
2043 | aMachineIds.resize(brlist.size());
|
---|
2044 | size_t i = 0;
|
---|
2045 | for (BackRefList::const_iterator it = brlist.begin(); it != brlist.end(); ++it, ++i)
|
---|
2046 | aMachineIds[i] = it->machineId;
|
---|
2047 | }
|
---|
2048 |
|
---|
2049 | return S_OK;
|
---|
2050 | }
|
---|
2051 |
|
---|
2052 | HRESULT Medium::setIds(AutoCaller &autoCaller,
|
---|
2053 | BOOL aSetImageId,
|
---|
2054 | const com::Guid &aImageId,
|
---|
2055 | BOOL aSetParentId,
|
---|
2056 | const com::Guid &aParentId)
|
---|
2057 | {
|
---|
2058 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2059 |
|
---|
2060 | switch (m->state)
|
---|
2061 | {
|
---|
2062 | case MediumState_Created:
|
---|
2063 | break;
|
---|
2064 | default:
|
---|
2065 | return i_setStateError();
|
---|
2066 | }
|
---|
2067 |
|
---|
2068 | Guid imageId, parentId;
|
---|
2069 | if (aSetImageId)
|
---|
2070 | {
|
---|
2071 | if (aImageId.isZero())
|
---|
2072 | imageId.create();
|
---|
2073 | else
|
---|
2074 | {
|
---|
2075 | imageId = aImageId;
|
---|
2076 | if (!imageId.isValid())
|
---|
2077 | return setError(E_INVALIDARG, tr("Argument %s is invalid"), "aImageId");
|
---|
2078 | }
|
---|
2079 | }
|
---|
2080 | if (aSetParentId)
|
---|
2081 | {
|
---|
2082 | if (aParentId.isZero())
|
---|
2083 | parentId.create();
|
---|
2084 | else
|
---|
2085 | parentId = aParentId;
|
---|
2086 | }
|
---|
2087 |
|
---|
2088 | unconst(m->uuidImage) = imageId;
|
---|
2089 | unconst(m->uuidParentImage) = parentId;
|
---|
2090 |
|
---|
2091 | // must not hold any locks before calling Medium::i_queryInfo
|
---|
2092 | alock.release();
|
---|
2093 |
|
---|
2094 | HRESULT rc = i_queryInfo(!!aSetImageId /* fSetImageId */,
|
---|
2095 | !!aSetParentId /* fSetParentId */,
|
---|
2096 | autoCaller);
|
---|
2097 |
|
---|
2098 | return rc;
|
---|
2099 | }
|
---|
2100 |
|
---|
2101 | HRESULT Medium::refreshState(AutoCaller &autoCaller, MediumState_T *aState)
|
---|
2102 | {
|
---|
2103 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2104 |
|
---|
2105 | HRESULT rc = S_OK;
|
---|
2106 |
|
---|
2107 | switch (m->state)
|
---|
2108 | {
|
---|
2109 | case MediumState_Created:
|
---|
2110 | case MediumState_Inaccessible:
|
---|
2111 | case MediumState_LockedRead:
|
---|
2112 | {
|
---|
2113 | // must not hold any locks before calling Medium::i_queryInfo
|
---|
2114 | alock.release();
|
---|
2115 |
|
---|
2116 | rc = i_queryInfo(false /* fSetImageId */, false /* fSetParentId */,
|
---|
2117 | autoCaller);
|
---|
2118 |
|
---|
2119 | alock.acquire();
|
---|
2120 | break;
|
---|
2121 | }
|
---|
2122 | default:
|
---|
2123 | break;
|
---|
2124 | }
|
---|
2125 |
|
---|
2126 | *aState = m->state;
|
---|
2127 |
|
---|
2128 | return rc;
|
---|
2129 | }
|
---|
2130 |
|
---|
2131 | HRESULT Medium::getSnapshotIds(const com::Guid &aMachineId,
|
---|
2132 | std::vector<com::Guid> &aSnapshotIds)
|
---|
2133 | {
|
---|
2134 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2135 |
|
---|
2136 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
2137 | it != m->backRefs.end(); ++it)
|
---|
2138 | {
|
---|
2139 | if (it->machineId == aMachineId)
|
---|
2140 | {
|
---|
2141 | size_t size = it->llSnapshotIds.size();
|
---|
2142 |
|
---|
2143 | /* if the medium is attached to the machine in the current state, we
|
---|
2144 | * return its ID as the first element of the array */
|
---|
2145 | if (it->fInCurState)
|
---|
2146 | ++size;
|
---|
2147 |
|
---|
2148 | if (size > 0)
|
---|
2149 | {
|
---|
2150 | aSnapshotIds.resize(size);
|
---|
2151 |
|
---|
2152 | size_t j = 0;
|
---|
2153 | if (it->fInCurState)
|
---|
2154 | aSnapshotIds[j++] = it->machineId.toUtf16();
|
---|
2155 |
|
---|
2156 | for(GuidList::const_iterator jt = it->llSnapshotIds.begin(); jt != it->llSnapshotIds.end(); ++jt, ++j)
|
---|
2157 | aSnapshotIds[j] = (*jt);
|
---|
2158 | }
|
---|
2159 |
|
---|
2160 | break;
|
---|
2161 | }
|
---|
2162 | }
|
---|
2163 |
|
---|
2164 | return S_OK;
|
---|
2165 | }
|
---|
2166 |
|
---|
2167 | HRESULT Medium::lockRead(ComPtr<IToken> &aToken)
|
---|
2168 | {
|
---|
2169 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2170 |
|
---|
2171 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
2172 | if (m->queryInfoRunning)
|
---|
2173 | {
|
---|
2174 | /* Must not hold the media tree lock, as Medium::i_queryInfo needs this
|
---|
2175 | * lock and thus we would run into a deadlock here. */
|
---|
2176 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
2177 | while (m->queryInfoRunning)
|
---|
2178 | {
|
---|
2179 | alock.release();
|
---|
2180 | /* must not hold the object lock now */
|
---|
2181 | Assert(!isWriteLockOnCurrentThread());
|
---|
2182 | {
|
---|
2183 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
2184 | }
|
---|
2185 | alock.acquire();
|
---|
2186 | }
|
---|
2187 | }
|
---|
2188 |
|
---|
2189 | HRESULT rc = S_OK;
|
---|
2190 |
|
---|
2191 | switch (m->state)
|
---|
2192 | {
|
---|
2193 | case MediumState_Created:
|
---|
2194 | case MediumState_Inaccessible:
|
---|
2195 | case MediumState_LockedRead:
|
---|
2196 | {
|
---|
2197 | ++m->readers;
|
---|
2198 |
|
---|
2199 | ComAssertMsgBreak(m->readers != 0, ("Counter overflow"), rc = E_FAIL);
|
---|
2200 |
|
---|
2201 | /* Remember pre-lock state */
|
---|
2202 | if (m->state != MediumState_LockedRead)
|
---|
2203 | m->preLockState = m->state;
|
---|
2204 |
|
---|
2205 | LogFlowThisFunc(("Okay - prev state=%d readers=%d\n", m->state, m->readers));
|
---|
2206 | m->state = MediumState_LockedRead;
|
---|
2207 |
|
---|
2208 | ComObjPtr<MediumLockToken> pToken;
|
---|
2209 | rc = pToken.createObject();
|
---|
2210 | if (SUCCEEDED(rc))
|
---|
2211 | rc = pToken->init(this, false /* fWrite */);
|
---|
2212 | if (FAILED(rc))
|
---|
2213 | {
|
---|
2214 | --m->readers;
|
---|
2215 | if (m->readers == 0)
|
---|
2216 | m->state = m->preLockState;
|
---|
2217 | return rc;
|
---|
2218 | }
|
---|
2219 |
|
---|
2220 | pToken.queryInterfaceTo(aToken.asOutParam());
|
---|
2221 | break;
|
---|
2222 | }
|
---|
2223 | default:
|
---|
2224 | {
|
---|
2225 | LogFlowThisFunc(("Failing - state=%d\n", m->state));
|
---|
2226 | rc = i_setStateError();
|
---|
2227 | break;
|
---|
2228 | }
|
---|
2229 | }
|
---|
2230 |
|
---|
2231 | return rc;
|
---|
2232 | }
|
---|
2233 |
|
---|
2234 | /**
|
---|
2235 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
2236 | * in-process calls).
|
---|
2237 | */
|
---|
2238 | HRESULT Medium::i_unlockRead(MediumState_T *aState)
|
---|
2239 | {
|
---|
2240 | AutoCaller autoCaller(this);
|
---|
2241 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2242 |
|
---|
2243 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2244 |
|
---|
2245 | HRESULT rc = S_OK;
|
---|
2246 |
|
---|
2247 | switch (m->state)
|
---|
2248 | {
|
---|
2249 | case MediumState_LockedRead:
|
---|
2250 | {
|
---|
2251 | ComAssertMsgBreak(m->readers != 0, ("Counter underflow"), rc = E_FAIL);
|
---|
2252 | --m->readers;
|
---|
2253 |
|
---|
2254 | /* Reset the state after the last reader */
|
---|
2255 | if (m->readers == 0)
|
---|
2256 | {
|
---|
2257 | m->state = m->preLockState;
|
---|
2258 | /* There are cases where we inject the deleting state into
|
---|
2259 | * a medium locked for reading. Make sure #unmarkForDeletion()
|
---|
2260 | * gets the right state afterwards. */
|
---|
2261 | if (m->preLockState == MediumState_Deleting)
|
---|
2262 | m->preLockState = MediumState_Created;
|
---|
2263 | }
|
---|
2264 |
|
---|
2265 | LogFlowThisFunc(("new state=%d\n", m->state));
|
---|
2266 | break;
|
---|
2267 | }
|
---|
2268 | default:
|
---|
2269 | {
|
---|
2270 | LogFlowThisFunc(("Failing - state=%d\n", m->state));
|
---|
2271 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2272 | tr("Medium '%s' is not locked for reading"),
|
---|
2273 | m->strLocationFull.c_str());
|
---|
2274 | break;
|
---|
2275 | }
|
---|
2276 | }
|
---|
2277 |
|
---|
2278 | /* return the current state after */
|
---|
2279 | if (aState)
|
---|
2280 | *aState = m->state;
|
---|
2281 |
|
---|
2282 | return rc;
|
---|
2283 | }
|
---|
2284 | HRESULT Medium::lockWrite(ComPtr<IToken> &aToken)
|
---|
2285 | {
|
---|
2286 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2287 |
|
---|
2288 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
2289 | if (m->queryInfoRunning)
|
---|
2290 | {
|
---|
2291 | /* Must not hold the media tree lock, as Medium::i_queryInfo needs this
|
---|
2292 | * lock and thus we would run into a deadlock here. */
|
---|
2293 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
2294 | while (m->queryInfoRunning)
|
---|
2295 | {
|
---|
2296 | alock.release();
|
---|
2297 | /* must not hold the object lock now */
|
---|
2298 | Assert(!isWriteLockOnCurrentThread());
|
---|
2299 | {
|
---|
2300 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
2301 | }
|
---|
2302 | alock.acquire();
|
---|
2303 | }
|
---|
2304 | }
|
---|
2305 |
|
---|
2306 | HRESULT rc = S_OK;
|
---|
2307 |
|
---|
2308 | switch (m->state)
|
---|
2309 | {
|
---|
2310 | case MediumState_Created:
|
---|
2311 | case MediumState_Inaccessible:
|
---|
2312 | {
|
---|
2313 | m->preLockState = m->state;
|
---|
2314 |
|
---|
2315 | LogFlowThisFunc(("Okay - prev state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2316 | m->state = MediumState_LockedWrite;
|
---|
2317 |
|
---|
2318 | ComObjPtr<MediumLockToken> pToken;
|
---|
2319 | rc = pToken.createObject();
|
---|
2320 | if (SUCCEEDED(rc))
|
---|
2321 | rc = pToken->init(this, true /* fWrite */);
|
---|
2322 | if (FAILED(rc))
|
---|
2323 | {
|
---|
2324 | m->state = m->preLockState;
|
---|
2325 | return rc;
|
---|
2326 | }
|
---|
2327 |
|
---|
2328 | pToken.queryInterfaceTo(aToken.asOutParam());
|
---|
2329 | break;
|
---|
2330 | }
|
---|
2331 | default:
|
---|
2332 | {
|
---|
2333 | LogFlowThisFunc(("Failing - state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2334 | rc = i_setStateError();
|
---|
2335 | break;
|
---|
2336 | }
|
---|
2337 | }
|
---|
2338 |
|
---|
2339 | return rc;
|
---|
2340 | }
|
---|
2341 |
|
---|
2342 | /**
|
---|
2343 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
2344 | * in-process calls).
|
---|
2345 | */
|
---|
2346 | HRESULT Medium::i_unlockWrite(MediumState_T *aState)
|
---|
2347 | {
|
---|
2348 | AutoCaller autoCaller(this);
|
---|
2349 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2350 |
|
---|
2351 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2352 |
|
---|
2353 | HRESULT rc = S_OK;
|
---|
2354 |
|
---|
2355 | switch (m->state)
|
---|
2356 | {
|
---|
2357 | case MediumState_LockedWrite:
|
---|
2358 | {
|
---|
2359 | m->state = m->preLockState;
|
---|
2360 | /* There are cases where we inject the deleting state into
|
---|
2361 | * a medium locked for writing. Make sure #unmarkForDeletion()
|
---|
2362 | * gets the right state afterwards. */
|
---|
2363 | if (m->preLockState == MediumState_Deleting)
|
---|
2364 | m->preLockState = MediumState_Created;
|
---|
2365 | LogFlowThisFunc(("new state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2366 | break;
|
---|
2367 | }
|
---|
2368 | default:
|
---|
2369 | {
|
---|
2370 | LogFlowThisFunc(("Failing - state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2371 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2372 | tr("Medium '%s' is not locked for writing"),
|
---|
2373 | m->strLocationFull.c_str());
|
---|
2374 | break;
|
---|
2375 | }
|
---|
2376 | }
|
---|
2377 |
|
---|
2378 | /* return the current state after */
|
---|
2379 | if (aState)
|
---|
2380 | *aState = m->state;
|
---|
2381 |
|
---|
2382 | return rc;
|
---|
2383 | }
|
---|
2384 |
|
---|
2385 | HRESULT Medium::close(AutoCaller &aAutoCaller)
|
---|
2386 | {
|
---|
2387 | // make a copy of VirtualBox pointer which gets nulled by uninit()
|
---|
2388 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
2389 |
|
---|
2390 | MultiResult mrc = i_close(aAutoCaller);
|
---|
2391 |
|
---|
2392 | pVirtualBox->i_saveModifiedRegistries();
|
---|
2393 |
|
---|
2394 | return mrc;
|
---|
2395 | }
|
---|
2396 |
|
---|
2397 | HRESULT Medium::getProperty(const com::Utf8Str &aName,
|
---|
2398 | com::Utf8Str &aValue)
|
---|
2399 | {
|
---|
2400 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2401 |
|
---|
2402 | settings::StringsMap::const_iterator it = m->mapProperties.find(aName);
|
---|
2403 | if (it == m->mapProperties.end())
|
---|
2404 | {
|
---|
2405 | if (!aName.startsWith("Special/"))
|
---|
2406 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2407 | tr("Property '%s' does not exist"), aName.c_str());
|
---|
2408 | else
|
---|
2409 | /* be more silent here */
|
---|
2410 | return VBOX_E_OBJECT_NOT_FOUND;
|
---|
2411 | }
|
---|
2412 |
|
---|
2413 | aValue = it->second;
|
---|
2414 |
|
---|
2415 | return S_OK;
|
---|
2416 | }
|
---|
2417 |
|
---|
2418 | HRESULT Medium::setProperty(const com::Utf8Str &aName,
|
---|
2419 | const com::Utf8Str &aValue)
|
---|
2420 | {
|
---|
2421 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2422 |
|
---|
2423 | switch (m->state)
|
---|
2424 | {
|
---|
2425 | case MediumState_Created:
|
---|
2426 | case MediumState_Inaccessible:
|
---|
2427 | break;
|
---|
2428 | default:
|
---|
2429 | return i_setStateError();
|
---|
2430 | }
|
---|
2431 |
|
---|
2432 | settings::StringsMap::iterator it = m->mapProperties.find(aName);
|
---|
2433 | if ( !aName.startsWith("Special/")
|
---|
2434 | && !i_isPropertyForFilter(aName))
|
---|
2435 | {
|
---|
2436 | if (it == m->mapProperties.end())
|
---|
2437 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2438 | tr("Property '%s' does not exist"),
|
---|
2439 | aName.c_str());
|
---|
2440 | it->second = aValue;
|
---|
2441 | }
|
---|
2442 | else
|
---|
2443 | {
|
---|
2444 | if (it == m->mapProperties.end())
|
---|
2445 | {
|
---|
2446 | if (!aValue.isEmpty())
|
---|
2447 | m->mapProperties[aName] = aValue;
|
---|
2448 | }
|
---|
2449 | else
|
---|
2450 | {
|
---|
2451 | if (!aValue.isEmpty())
|
---|
2452 | it->second = aValue;
|
---|
2453 | else
|
---|
2454 | m->mapProperties.erase(it);
|
---|
2455 | }
|
---|
2456 | }
|
---|
2457 |
|
---|
2458 | // save the settings
|
---|
2459 | mlock.release();
|
---|
2460 | i_markRegistriesModified();
|
---|
2461 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2462 |
|
---|
2463 | return S_OK;
|
---|
2464 | }
|
---|
2465 |
|
---|
2466 | HRESULT Medium::getProperties(const com::Utf8Str &aNames,
|
---|
2467 | std::vector<com::Utf8Str> &aReturnNames,
|
---|
2468 | std::vector<com::Utf8Str> &aReturnValues)
|
---|
2469 | {
|
---|
2470 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2471 |
|
---|
2472 | /// @todo make use of aNames according to the documentation
|
---|
2473 | NOREF(aNames);
|
---|
2474 |
|
---|
2475 | aReturnNames.resize(m->mapProperties.size());
|
---|
2476 | aReturnValues.resize(m->mapProperties.size());
|
---|
2477 | size_t i = 0;
|
---|
2478 | for (settings::StringsMap::const_iterator it = m->mapProperties.begin();
|
---|
2479 | it != m->mapProperties.end();
|
---|
2480 | ++it, ++i)
|
---|
2481 | {
|
---|
2482 | aReturnNames[i] = it->first;
|
---|
2483 | aReturnValues[i] = it->second;
|
---|
2484 | }
|
---|
2485 | return S_OK;
|
---|
2486 | }
|
---|
2487 |
|
---|
2488 | HRESULT Medium::setProperties(const std::vector<com::Utf8Str> &aNames,
|
---|
2489 | const std::vector<com::Utf8Str> &aValues)
|
---|
2490 | {
|
---|
2491 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2492 |
|
---|
2493 | /* first pass: validate names */
|
---|
2494 | for (size_t i = 0;
|
---|
2495 | i < aNames.size();
|
---|
2496 | ++i)
|
---|
2497 | {
|
---|
2498 | Utf8Str strName(aNames[i]);
|
---|
2499 | if ( !strName.startsWith("Special/")
|
---|
2500 | && !i_isPropertyForFilter(strName)
|
---|
2501 | && m->mapProperties.find(strName) == m->mapProperties.end())
|
---|
2502 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2503 | tr("Property '%s' does not exist"), strName.c_str());
|
---|
2504 | }
|
---|
2505 |
|
---|
2506 | /* second pass: assign */
|
---|
2507 | for (size_t i = 0;
|
---|
2508 | i < aNames.size();
|
---|
2509 | ++i)
|
---|
2510 | {
|
---|
2511 | Utf8Str strName(aNames[i]);
|
---|
2512 | Utf8Str strValue(aValues[i]);
|
---|
2513 | settings::StringsMap::iterator it = m->mapProperties.find(strName);
|
---|
2514 | if ( !strName.startsWith("Special/")
|
---|
2515 | && !i_isPropertyForFilter(strName))
|
---|
2516 | {
|
---|
2517 | AssertReturn(it != m->mapProperties.end(), E_FAIL);
|
---|
2518 | it->second = strValue;
|
---|
2519 | }
|
---|
2520 | else
|
---|
2521 | {
|
---|
2522 | if (it == m->mapProperties.end())
|
---|
2523 | {
|
---|
2524 | if (!strValue.isEmpty())
|
---|
2525 | m->mapProperties[strName] = strValue;
|
---|
2526 | }
|
---|
2527 | else
|
---|
2528 | {
|
---|
2529 | if (!strValue.isEmpty())
|
---|
2530 | it->second = strValue;
|
---|
2531 | else
|
---|
2532 | m->mapProperties.erase(it);
|
---|
2533 | }
|
---|
2534 | }
|
---|
2535 | }
|
---|
2536 |
|
---|
2537 | // save the settings
|
---|
2538 | mlock.release();
|
---|
2539 | i_markRegistriesModified();
|
---|
2540 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2541 |
|
---|
2542 | return S_OK;
|
---|
2543 | }
|
---|
2544 | HRESULT Medium::createBaseStorage(LONG64 aLogicalSize,
|
---|
2545 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2546 | ComPtr<IProgress> &aProgress)
|
---|
2547 | {
|
---|
2548 | if (aLogicalSize < 0)
|
---|
2549 | return setError(E_INVALIDARG, tr("The medium size argument (%lld) is negative"), aLogicalSize);
|
---|
2550 |
|
---|
2551 | HRESULT rc = S_OK;
|
---|
2552 | ComObjPtr<Progress> pProgress;
|
---|
2553 | Medium::Task *pTask = NULL;
|
---|
2554 |
|
---|
2555 | try
|
---|
2556 | {
|
---|
2557 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2558 |
|
---|
2559 | ULONG mediumVariantFlags = 0;
|
---|
2560 |
|
---|
2561 | if (aVariant.size())
|
---|
2562 | {
|
---|
2563 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
2564 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
2565 | }
|
---|
2566 |
|
---|
2567 | mediumVariantFlags &= ((unsigned)~MediumVariant_Diff);
|
---|
2568 |
|
---|
2569 | if ( !(mediumVariantFlags & MediumVariant_Fixed)
|
---|
2570 | && !(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_CreateDynamic))
|
---|
2571 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2572 | tr("Medium format '%s' does not support dynamic storage creation"),
|
---|
2573 | m->strFormat.c_str());
|
---|
2574 |
|
---|
2575 | if ( (mediumVariantFlags & MediumVariant_Fixed)
|
---|
2576 | && !(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_CreateFixed))
|
---|
2577 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2578 | tr("Medium format '%s' does not support fixed storage creation"),
|
---|
2579 | m->strFormat.c_str());
|
---|
2580 |
|
---|
2581 | if (m->state != MediumState_NotCreated)
|
---|
2582 | throw i_setStateError();
|
---|
2583 |
|
---|
2584 | pProgress.createObject();
|
---|
2585 | rc = pProgress->init(m->pVirtualBox,
|
---|
2586 | static_cast<IMedium*>(this),
|
---|
2587 | (mediumVariantFlags & MediumVariant_Fixed)
|
---|
2588 | ? BstrFmt(tr("Creating fixed medium storage unit '%s'"), m->strLocationFull.c_str()).raw()
|
---|
2589 | : BstrFmt(tr("Creating dynamic medium storage unit '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
2590 | TRUE /* aCancelable */);
|
---|
2591 | if (FAILED(rc))
|
---|
2592 | throw rc;
|
---|
2593 |
|
---|
2594 | /* setup task object to carry out the operation asynchronously */
|
---|
2595 | pTask = new Medium::CreateBaseTask(this, pProgress, aLogicalSize,
|
---|
2596 | (MediumVariant_T)mediumVariantFlags);
|
---|
2597 | //(MediumVariant_T)aVariant);
|
---|
2598 | rc = pTask->rc();
|
---|
2599 | AssertComRC(rc);
|
---|
2600 | if (FAILED(rc))
|
---|
2601 | throw rc;
|
---|
2602 |
|
---|
2603 | m->state = MediumState_Creating;
|
---|
2604 | }
|
---|
2605 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2606 |
|
---|
2607 | if (SUCCEEDED(rc))
|
---|
2608 | {
|
---|
2609 | rc = pTask->createThread();
|
---|
2610 |
|
---|
2611 | if (SUCCEEDED(rc))
|
---|
2612 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2613 | }
|
---|
2614 | else if (pTask != NULL)
|
---|
2615 | delete pTask;
|
---|
2616 |
|
---|
2617 | return rc;
|
---|
2618 | }
|
---|
2619 |
|
---|
2620 | HRESULT Medium::deleteStorage(ComPtr<IProgress> &aProgress)
|
---|
2621 | {
|
---|
2622 | ComObjPtr<Progress> pProgress;
|
---|
2623 |
|
---|
2624 | MultiResult mrc = i_deleteStorage(&pProgress,
|
---|
2625 | false /* aWait */);
|
---|
2626 | /* Must save the registries in any case, since an entry was removed. */
|
---|
2627 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2628 |
|
---|
2629 | if (SUCCEEDED(mrc))
|
---|
2630 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2631 |
|
---|
2632 | return mrc;
|
---|
2633 | }
|
---|
2634 |
|
---|
2635 | HRESULT Medium::createDiffStorage(AutoCaller &autoCaller,
|
---|
2636 | const ComPtr<IMedium> &aTarget,
|
---|
2637 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2638 | ComPtr<IProgress> &aProgress)
|
---|
2639 | {
|
---|
2640 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
2641 | * to lock order violations, it probably causes lock order issues related
|
---|
2642 | * to the AutoCaller usage. */
|
---|
2643 | IMedium *aT = aTarget;
|
---|
2644 | ComObjPtr<Medium> diff = static_cast<Medium*>(aT);
|
---|
2645 |
|
---|
2646 | autoCaller.release();
|
---|
2647 |
|
---|
2648 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
2649 | * the pVirtualBox reference, see #uninit(). */
|
---|
2650 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
2651 |
|
---|
2652 | // we access m->pParent
|
---|
2653 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
2654 |
|
---|
2655 | autoCaller.add();
|
---|
2656 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2657 |
|
---|
2658 | AutoMultiWriteLock2 alock(this->lockHandle(), diff->lockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
2659 |
|
---|
2660 | if (m->type == MediumType_Writethrough)
|
---|
2661 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2662 | tr("Medium type of '%s' is Writethrough"),
|
---|
2663 | m->strLocationFull.c_str());
|
---|
2664 | else if (m->type == MediumType_Shareable)
|
---|
2665 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2666 | tr("Medium type of '%s' is Shareable"),
|
---|
2667 | m->strLocationFull.c_str());
|
---|
2668 | else if (m->type == MediumType_Readonly)
|
---|
2669 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2670 | tr("Medium type of '%s' is Readonly"),
|
---|
2671 | m->strLocationFull.c_str());
|
---|
2672 |
|
---|
2673 | /* Apply the normal locking logic to the entire chain. */
|
---|
2674 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
2675 | alock.release();
|
---|
2676 | treeLock.release();
|
---|
2677 | HRESULT rc = diff->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2678 | diff /* pToLockWrite */,
|
---|
2679 | false /* fMediumLockWriteAll */,
|
---|
2680 | this,
|
---|
2681 | *pMediumLockList);
|
---|
2682 | treeLock.acquire();
|
---|
2683 | alock.acquire();
|
---|
2684 | if (FAILED(rc))
|
---|
2685 | {
|
---|
2686 | delete pMediumLockList;
|
---|
2687 | return rc;
|
---|
2688 | }
|
---|
2689 |
|
---|
2690 | alock.release();
|
---|
2691 | treeLock.release();
|
---|
2692 | rc = pMediumLockList->Lock();
|
---|
2693 | treeLock.acquire();
|
---|
2694 | alock.acquire();
|
---|
2695 | if (FAILED(rc))
|
---|
2696 | {
|
---|
2697 | delete pMediumLockList;
|
---|
2698 |
|
---|
2699 | return setError(rc, tr("Could not lock medium when creating diff '%s'"),
|
---|
2700 | diff->i_getLocationFull().c_str());
|
---|
2701 | }
|
---|
2702 |
|
---|
2703 | Guid parentMachineRegistry;
|
---|
2704 | if (i_getFirstRegistryMachineId(parentMachineRegistry))
|
---|
2705 | {
|
---|
2706 | /* since this medium has been just created it isn't associated yet */
|
---|
2707 | diff->m->llRegistryIDs.push_back(parentMachineRegistry);
|
---|
2708 | alock.release();
|
---|
2709 | treeLock.release();
|
---|
2710 | diff->i_markRegistriesModified();
|
---|
2711 | treeLock.acquire();
|
---|
2712 | alock.acquire();
|
---|
2713 | }
|
---|
2714 |
|
---|
2715 | alock.release();
|
---|
2716 | treeLock.release();
|
---|
2717 |
|
---|
2718 | ComObjPtr<Progress> pProgress;
|
---|
2719 |
|
---|
2720 | ULONG mediumVariantFlags = 0;
|
---|
2721 |
|
---|
2722 | if (aVariant.size())
|
---|
2723 | {
|
---|
2724 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
2725 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
2726 | }
|
---|
2727 |
|
---|
2728 | rc = i_createDiffStorage(diff, (MediumVariant_T)mediumVariantFlags, pMediumLockList,
|
---|
2729 | &pProgress, false /* aWait */);
|
---|
2730 | if (FAILED(rc))
|
---|
2731 | delete pMediumLockList;
|
---|
2732 | else
|
---|
2733 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2734 |
|
---|
2735 | return rc;
|
---|
2736 | }
|
---|
2737 |
|
---|
2738 | HRESULT Medium::mergeTo(const ComPtr<IMedium> &aTarget,
|
---|
2739 | ComPtr<IProgress> &aProgress)
|
---|
2740 | {
|
---|
2741 |
|
---|
2742 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
2743 | * to lock order violations, it probably causes lock order issues related
|
---|
2744 | * to the AutoCaller usage. */
|
---|
2745 | IMedium *aT = aTarget;
|
---|
2746 |
|
---|
2747 | ComAssertRet(aT != this, E_INVALIDARG);
|
---|
2748 |
|
---|
2749 | ComObjPtr<Medium> pTarget = static_cast<Medium*>(aT);
|
---|
2750 |
|
---|
2751 | bool fMergeForward = false;
|
---|
2752 | ComObjPtr<Medium> pParentForTarget;
|
---|
2753 | MediumLockList *pChildrenToReparent = NULL;
|
---|
2754 | MediumLockList *pMediumLockList = NULL;
|
---|
2755 |
|
---|
2756 | HRESULT rc = S_OK;
|
---|
2757 |
|
---|
2758 | rc = i_prepareMergeTo(pTarget, NULL, NULL, true, fMergeForward,
|
---|
2759 | pParentForTarget, pChildrenToReparent, pMediumLockList);
|
---|
2760 | if (FAILED(rc)) return rc;
|
---|
2761 |
|
---|
2762 | ComObjPtr<Progress> pProgress;
|
---|
2763 |
|
---|
2764 | rc = i_mergeTo(pTarget, fMergeForward, pParentForTarget, pChildrenToReparent,
|
---|
2765 | pMediumLockList, &pProgress, false /* aWait */);
|
---|
2766 | if (FAILED(rc))
|
---|
2767 | i_cancelMergeTo(pChildrenToReparent, pMediumLockList);
|
---|
2768 | else
|
---|
2769 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2770 |
|
---|
2771 | return rc;
|
---|
2772 | }
|
---|
2773 |
|
---|
2774 | HRESULT Medium::cloneToBase(const ComPtr<IMedium> &aTarget,
|
---|
2775 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2776 | ComPtr<IProgress> &aProgress)
|
---|
2777 | {
|
---|
2778 | int rc = S_OK;
|
---|
2779 |
|
---|
2780 | rc = cloneTo(aTarget, aVariant, NULL, aProgress);
|
---|
2781 | return rc;
|
---|
2782 | }
|
---|
2783 |
|
---|
2784 | HRESULT Medium::cloneTo(const ComPtr<IMedium> &aTarget,
|
---|
2785 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2786 | const ComPtr<IMedium> &aParent,
|
---|
2787 | ComPtr<IProgress> &aProgress)
|
---|
2788 | {
|
---|
2789 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
2790 | * to lock order violations, it probably causes lock order issues related
|
---|
2791 | * to the AutoCaller usage. */
|
---|
2792 | ComAssertRet(aTarget != this, E_INVALIDARG);
|
---|
2793 |
|
---|
2794 | IMedium *aT = aTarget;
|
---|
2795 | ComObjPtr<Medium> pTarget = static_cast<Medium*>(aT);
|
---|
2796 | ComObjPtr<Medium> pParent;
|
---|
2797 | if (aParent)
|
---|
2798 | {
|
---|
2799 | IMedium *aP = aParent;
|
---|
2800 | pParent = static_cast<Medium*>(aP);
|
---|
2801 | }
|
---|
2802 |
|
---|
2803 | HRESULT rc = S_OK;
|
---|
2804 | ComObjPtr<Progress> pProgress;
|
---|
2805 | Medium::Task *pTask = NULL;
|
---|
2806 |
|
---|
2807 | try
|
---|
2808 | {
|
---|
2809 | // locking: we need the tree lock first because we access parent pointers
|
---|
2810 | // and we need to write-lock the media involved
|
---|
2811 | uint32_t cHandles = 3;
|
---|
2812 | LockHandle* pHandles[4] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
2813 | this->lockHandle(),
|
---|
2814 | pTarget->lockHandle() };
|
---|
2815 | /* Only add parent to the lock if it is not null */
|
---|
2816 | if (!pParent.isNull())
|
---|
2817 | pHandles[cHandles++] = pParent->lockHandle();
|
---|
2818 | AutoWriteLock alock(cHandles,
|
---|
2819 | pHandles
|
---|
2820 | COMMA_LOCKVAL_SRC_POS);
|
---|
2821 |
|
---|
2822 | if ( pTarget->m->state != MediumState_NotCreated
|
---|
2823 | && pTarget->m->state != MediumState_Created)
|
---|
2824 | throw pTarget->i_setStateError();
|
---|
2825 |
|
---|
2826 | /* Build the source lock list. */
|
---|
2827 | MediumLockList *pSourceMediumLockList(new MediumLockList());
|
---|
2828 | alock.release();
|
---|
2829 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2830 | NULL /* pToLockWrite */,
|
---|
2831 | false /* fMediumLockWriteAll */,
|
---|
2832 | NULL,
|
---|
2833 | *pSourceMediumLockList);
|
---|
2834 | alock.acquire();
|
---|
2835 | if (FAILED(rc))
|
---|
2836 | {
|
---|
2837 | delete pSourceMediumLockList;
|
---|
2838 | throw rc;
|
---|
2839 | }
|
---|
2840 |
|
---|
2841 | /* Build the target lock list (including the to-be parent chain). */
|
---|
2842 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
2843 | alock.release();
|
---|
2844 | rc = pTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2845 | pTarget /* pToLockWrite */,
|
---|
2846 | false /* fMediumLockWriteAll */,
|
---|
2847 | pParent,
|
---|
2848 | *pTargetMediumLockList);
|
---|
2849 | alock.acquire();
|
---|
2850 | if (FAILED(rc))
|
---|
2851 | {
|
---|
2852 | delete pSourceMediumLockList;
|
---|
2853 | delete pTargetMediumLockList;
|
---|
2854 | throw rc;
|
---|
2855 | }
|
---|
2856 |
|
---|
2857 | alock.release();
|
---|
2858 | rc = pSourceMediumLockList->Lock();
|
---|
2859 | alock.acquire();
|
---|
2860 | if (FAILED(rc))
|
---|
2861 | {
|
---|
2862 | delete pSourceMediumLockList;
|
---|
2863 | delete pTargetMediumLockList;
|
---|
2864 | throw setError(rc,
|
---|
2865 | tr("Failed to lock source media '%s'"),
|
---|
2866 | i_getLocationFull().c_str());
|
---|
2867 | }
|
---|
2868 | alock.release();
|
---|
2869 | rc = pTargetMediumLockList->Lock();
|
---|
2870 | alock.acquire();
|
---|
2871 | if (FAILED(rc))
|
---|
2872 | {
|
---|
2873 | delete pSourceMediumLockList;
|
---|
2874 | delete pTargetMediumLockList;
|
---|
2875 | throw setError(rc,
|
---|
2876 | tr("Failed to lock target media '%s'"),
|
---|
2877 | pTarget->i_getLocationFull().c_str());
|
---|
2878 | }
|
---|
2879 |
|
---|
2880 | pProgress.createObject();
|
---|
2881 | rc = pProgress->init(m->pVirtualBox,
|
---|
2882 | static_cast <IMedium *>(this),
|
---|
2883 | BstrFmt(tr("Creating clone medium '%s'"), pTarget->m->strLocationFull.c_str()).raw(),
|
---|
2884 | TRUE /* aCancelable */);
|
---|
2885 | if (FAILED(rc))
|
---|
2886 | {
|
---|
2887 | delete pSourceMediumLockList;
|
---|
2888 | delete pTargetMediumLockList;
|
---|
2889 | throw rc;
|
---|
2890 | }
|
---|
2891 |
|
---|
2892 | ULONG mediumVariantFlags = 0;
|
---|
2893 |
|
---|
2894 | if (aVariant.size())
|
---|
2895 | {
|
---|
2896 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
2897 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
2898 | }
|
---|
2899 |
|
---|
2900 | /* setup task object to carry out the operation asynchronously */
|
---|
2901 | pTask = new Medium::CloneTask(this, pProgress, pTarget,
|
---|
2902 | (MediumVariant_T)mediumVariantFlags,
|
---|
2903 | pParent, UINT32_MAX, UINT32_MAX,
|
---|
2904 | pSourceMediumLockList, pTargetMediumLockList);
|
---|
2905 | rc = pTask->rc();
|
---|
2906 | AssertComRC(rc);
|
---|
2907 | if (FAILED(rc))
|
---|
2908 | throw rc;
|
---|
2909 |
|
---|
2910 | if (pTarget->m->state == MediumState_NotCreated)
|
---|
2911 | pTarget->m->state = MediumState_Creating;
|
---|
2912 | }
|
---|
2913 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2914 |
|
---|
2915 | if (SUCCEEDED(rc))
|
---|
2916 | {
|
---|
2917 | rc = pTask->createThread();
|
---|
2918 |
|
---|
2919 | if (SUCCEEDED(rc))
|
---|
2920 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2921 | }
|
---|
2922 | else if (pTask != NULL)
|
---|
2923 | delete pTask;
|
---|
2924 |
|
---|
2925 | return rc;
|
---|
2926 | }
|
---|
2927 |
|
---|
2928 | HRESULT Medium::setLocation(const com::Utf8Str &aLocation, ComPtr<IProgress> &aProgress)
|
---|
2929 | {
|
---|
2930 |
|
---|
2931 | ComObjPtr<Medium> pParent;
|
---|
2932 | ComObjPtr<Progress> pProgress;
|
---|
2933 | HRESULT rc = S_OK;
|
---|
2934 | Medium::Task *pTask = NULL;
|
---|
2935 |
|
---|
2936 | try
|
---|
2937 | {
|
---|
2938 | /// @todo NEWMEDIA for file names, add the default extension if no extension
|
---|
2939 | /// is present (using the information from the VD backend which also implies
|
---|
2940 | /// that one more parameter should be passed to setLocation() requesting
|
---|
2941 | /// that functionality since it is only allowed when called from this method
|
---|
2942 |
|
---|
2943 | /// @todo NEWMEDIA rename the file and set m->location on success, then save
|
---|
2944 | /// the global registry (and local registries of portable VMs referring to
|
---|
2945 | /// this medium), this will also require to add the mRegistered flag to data
|
---|
2946 |
|
---|
2947 | // locking: we need the tree lock first because we access parent pointers
|
---|
2948 | // and we need to write-lock the media involved
|
---|
2949 | uint32_t cHandles = 2;
|
---|
2950 | LockHandle* pHandles[2] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
2951 | this->lockHandle() };
|
---|
2952 |
|
---|
2953 | AutoWriteLock alock(cHandles,
|
---|
2954 | pHandles
|
---|
2955 | COMMA_LOCKVAL_SRC_POS);
|
---|
2956 |
|
---|
2957 | /* play with locations */
|
---|
2958 | {
|
---|
2959 | /* get source path and filename */
|
---|
2960 | Utf8Str sourcePath = i_getLocationFull();
|
---|
2961 | Utf8Str sourceFName = i_getName();
|
---|
2962 |
|
---|
2963 | if (aLocation.isEmpty())
|
---|
2964 | {
|
---|
2965 | rc = setError(VERR_PATH_ZERO_LENGTH,
|
---|
2966 | tr("Medium '%s' can't be moved. Destination path is empty."),
|
---|
2967 | i_getLocationFull().c_str());
|
---|
2968 | throw rc;
|
---|
2969 | }
|
---|
2970 |
|
---|
2971 | /* extract destination path and filename */
|
---|
2972 | Utf8Str destPath(aLocation);
|
---|
2973 | Utf8Str destFName(destPath);
|
---|
2974 | destFName.stripPath();
|
---|
2975 |
|
---|
2976 | Utf8Str suffix(destFName);
|
---|
2977 | suffix.stripSuffix();
|
---|
2978 |
|
---|
2979 | if (suffix.equals(destFName) && !destFName.isEmpty())
|
---|
2980 | {
|
---|
2981 | /*
|
---|
2982 | * The target path has no filename: Either "/path/to/new/location" or
|
---|
2983 | * just "newname" (no trailing backslash or there is no filename with
|
---|
2984 | * extension(suffix)).
|
---|
2985 | */
|
---|
2986 | if (destPath.equals(destFName))
|
---|
2987 | {
|
---|
2988 | /* new path contains only "newname", no path, no extension */
|
---|
2989 | destFName.append(RTPathSuffix(sourceFName.c_str()));
|
---|
2990 | destPath = destFName;
|
---|
2991 | }
|
---|
2992 | else
|
---|
2993 | {
|
---|
2994 | /* new path looks like "/path/to/new/location" */
|
---|
2995 | destFName.setNull();
|
---|
2996 | destPath.append(RTPATH_SLASH);
|
---|
2997 | }
|
---|
2998 | }
|
---|
2999 |
|
---|
3000 | if (destFName.isEmpty())
|
---|
3001 | {
|
---|
3002 | /* No target name */
|
---|
3003 | destPath.append(sourceFName);
|
---|
3004 | }
|
---|
3005 | else
|
---|
3006 | {
|
---|
3007 | if (destPath.equals(destFName))
|
---|
3008 | {
|
---|
3009 | /*
|
---|
3010 | * The target path contains of only a filename without a directory.
|
---|
3011 | * Move the medium within the source directory to the new name
|
---|
3012 | * (actually rename operation).
|
---|
3013 | * Scratches sourcePath!
|
---|
3014 | */
|
---|
3015 | destPath = sourcePath.stripFilename().append(RTPATH_SLASH).append(destFName);
|
---|
3016 | }
|
---|
3017 | suffix = i_getFormat();
|
---|
3018 | if (suffix.compare("RAW", Utf8Str::CaseInsensitive) == 0)
|
---|
3019 | {
|
---|
3020 | DeviceType_T devType = i_getDeviceType();
|
---|
3021 | switch (devType)
|
---|
3022 | {
|
---|
3023 | case DeviceType_DVD:
|
---|
3024 | suffix = "iso";
|
---|
3025 | break;
|
---|
3026 | case DeviceType_Floppy:
|
---|
3027 | suffix = "img";
|
---|
3028 | break;
|
---|
3029 | default:
|
---|
3030 | rc = setError(VERR_NOT_A_FILE,
|
---|
3031 | tr("Medium '%s' has RAW type. \"Move\" operation isn't supported for this type."),
|
---|
3032 | i_getLocationFull().c_str());
|
---|
3033 | throw rc;
|
---|
3034 | }
|
---|
3035 | }
|
---|
3036 | else if (suffix.compare("Parallels", Utf8Str::CaseInsensitive) == 0)
|
---|
3037 | {
|
---|
3038 | suffix = "hdd";
|
---|
3039 | }
|
---|
3040 |
|
---|
3041 | /* Set the target extension like on the source. Any conversions are prohibited */
|
---|
3042 | suffix.toLower();
|
---|
3043 | destPath.stripSuffix().append('.').append(suffix);
|
---|
3044 | }
|
---|
3045 |
|
---|
3046 | /* Simple check for existence */
|
---|
3047 | if (RTFileExists(destPath.c_str()))
|
---|
3048 | {
|
---|
3049 | rc = setError(VBOX_E_FILE_ERROR,
|
---|
3050 | tr("The given path '%s' is an existing file. Delete or rename this file."),
|
---|
3051 | destPath.c_str());
|
---|
3052 | throw rc;
|
---|
3053 | }
|
---|
3054 |
|
---|
3055 | if (!i_isMediumFormatFile())
|
---|
3056 | {
|
---|
3057 | rc = setError(VERR_NOT_A_FILE,
|
---|
3058 | tr("Medium '%s' isn't a file object. \"Move\" operation isn't supported."),
|
---|
3059 | i_getLocationFull().c_str());
|
---|
3060 | throw rc;
|
---|
3061 | }
|
---|
3062 | /* Path must be absolute */
|
---|
3063 | if (!RTPathStartsWithRoot(destPath.c_str()))
|
---|
3064 | {
|
---|
3065 | rc = setError(VBOX_E_FILE_ERROR,
|
---|
3066 | tr("The given path '%s' is not fully qualified"),
|
---|
3067 | destPath.c_str());
|
---|
3068 | throw rc;
|
---|
3069 | }
|
---|
3070 | /* Check path for a new file object */
|
---|
3071 | rc = VirtualBox::i_ensureFilePathExists(destPath, true);
|
---|
3072 | if (FAILED(rc))
|
---|
3073 | throw rc;
|
---|
3074 |
|
---|
3075 | /* Set needed variables for "moving" procedure. It'll be used later in separate thread task */
|
---|
3076 | rc = i_preparationForMoving(destPath);
|
---|
3077 | if (FAILED(rc))
|
---|
3078 | {
|
---|
3079 | rc = setError(VERR_NO_CHANGE,
|
---|
3080 | tr("Medium '%s' is already in the correct location"),
|
---|
3081 | i_getLocationFull().c_str());
|
---|
3082 | throw rc;
|
---|
3083 | }
|
---|
3084 | }
|
---|
3085 |
|
---|
3086 | /* Check VMs which have this medium attached to*/
|
---|
3087 | std::vector<com::Guid> aMachineIds;
|
---|
3088 | rc = getMachineIds(aMachineIds);
|
---|
3089 | std::vector<com::Guid>::const_iterator currMachineID = aMachineIds.begin();
|
---|
3090 | std::vector<com::Guid>::const_iterator lastMachineID = aMachineIds.end();
|
---|
3091 |
|
---|
3092 | while (currMachineID != lastMachineID)
|
---|
3093 | {
|
---|
3094 | Guid id(*currMachineID);
|
---|
3095 | ComObjPtr<Machine> aMachine;
|
---|
3096 |
|
---|
3097 | alock.release();
|
---|
3098 | rc = m->pVirtualBox->i_findMachine(id, false, true, &aMachine);
|
---|
3099 | alock.acquire();
|
---|
3100 |
|
---|
3101 | if (SUCCEEDED(rc))
|
---|
3102 | {
|
---|
3103 | ComObjPtr<SessionMachine> sm;
|
---|
3104 | ComPtr<IInternalSessionControl> ctl;
|
---|
3105 |
|
---|
3106 | alock.release();
|
---|
3107 | bool ses = aMachine->i_isSessionOpenVM(sm, &ctl);
|
---|
3108 | alock.acquire();
|
---|
3109 |
|
---|
3110 | if (ses)
|
---|
3111 | {
|
---|
3112 | rc = setError(VERR_VM_UNEXPECTED_VM_STATE,
|
---|
3113 | tr("At least the VM '%s' to whom this medium '%s' attached has currently an opened session. Stop all VMs before relocating this medium"),
|
---|
3114 | id.toString().c_str(),
|
---|
3115 | i_getLocationFull().c_str());
|
---|
3116 | throw rc;
|
---|
3117 | }
|
---|
3118 | }
|
---|
3119 | ++currMachineID;
|
---|
3120 | }
|
---|
3121 |
|
---|
3122 | /* Build the source lock list. */
|
---|
3123 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3124 | alock.release();
|
---|
3125 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
3126 | this /* pToLockWrite */,
|
---|
3127 | true /* fMediumLockWriteAll */,
|
---|
3128 | NULL,
|
---|
3129 | *pMediumLockList);
|
---|
3130 | alock.acquire();
|
---|
3131 | if (FAILED(rc))
|
---|
3132 | {
|
---|
3133 | delete pMediumLockList;
|
---|
3134 | throw setError(rc,
|
---|
3135 | tr("Failed to create medium lock list for '%s'"),
|
---|
3136 | i_getLocationFull().c_str());
|
---|
3137 | }
|
---|
3138 | alock.release();
|
---|
3139 | rc = pMediumLockList->Lock();
|
---|
3140 | alock.acquire();
|
---|
3141 | if (FAILED(rc))
|
---|
3142 | {
|
---|
3143 | delete pMediumLockList;
|
---|
3144 | throw setError(rc,
|
---|
3145 | tr("Failed to lock media '%s'"),
|
---|
3146 | i_getLocationFull().c_str());
|
---|
3147 | }
|
---|
3148 |
|
---|
3149 | pProgress.createObject();
|
---|
3150 | rc = pProgress->init(m->pVirtualBox,
|
---|
3151 | static_cast <IMedium *>(this),
|
---|
3152 | BstrFmt(tr("Moving medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3153 | TRUE /* aCancelable */);
|
---|
3154 |
|
---|
3155 | /* Do the disk moving. */
|
---|
3156 | if (SUCCEEDED(rc))
|
---|
3157 | {
|
---|
3158 | ULONG mediumVariantFlags = i_getVariant();
|
---|
3159 |
|
---|
3160 | /* setup task object to carry out the operation asynchronously */
|
---|
3161 | pTask = new Medium::MoveTask(this, pProgress,
|
---|
3162 | (MediumVariant_T)mediumVariantFlags,
|
---|
3163 | pMediumLockList);
|
---|
3164 | rc = pTask->rc();
|
---|
3165 | AssertComRC(rc);
|
---|
3166 | if (FAILED(rc))
|
---|
3167 | throw rc;
|
---|
3168 | }
|
---|
3169 |
|
---|
3170 | }
|
---|
3171 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3172 |
|
---|
3173 | if (SUCCEEDED(rc))
|
---|
3174 | {
|
---|
3175 | rc = pTask->createThread();
|
---|
3176 |
|
---|
3177 | if (SUCCEEDED(rc))
|
---|
3178 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3179 | }
|
---|
3180 | else
|
---|
3181 | {
|
---|
3182 | if (pTask)
|
---|
3183 | delete pTask;
|
---|
3184 | }
|
---|
3185 |
|
---|
3186 | return rc;
|
---|
3187 | }
|
---|
3188 |
|
---|
3189 | HRESULT Medium::compact(ComPtr<IProgress> &aProgress)
|
---|
3190 | {
|
---|
3191 | HRESULT rc = S_OK;
|
---|
3192 | ComObjPtr<Progress> pProgress;
|
---|
3193 | Medium::Task *pTask = NULL;
|
---|
3194 |
|
---|
3195 | try
|
---|
3196 | {
|
---|
3197 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3198 |
|
---|
3199 | /* Build the medium lock list. */
|
---|
3200 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3201 | alock.release();
|
---|
3202 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
3203 | this /* pToLockWrite */,
|
---|
3204 | false /* fMediumLockWriteAll */,
|
---|
3205 | NULL,
|
---|
3206 | *pMediumLockList);
|
---|
3207 | alock.acquire();
|
---|
3208 | if (FAILED(rc))
|
---|
3209 | {
|
---|
3210 | delete pMediumLockList;
|
---|
3211 | throw rc;
|
---|
3212 | }
|
---|
3213 |
|
---|
3214 | alock.release();
|
---|
3215 | rc = pMediumLockList->Lock();
|
---|
3216 | alock.acquire();
|
---|
3217 | if (FAILED(rc))
|
---|
3218 | {
|
---|
3219 | delete pMediumLockList;
|
---|
3220 | throw setError(rc,
|
---|
3221 | tr("Failed to lock media when compacting '%s'"),
|
---|
3222 | i_getLocationFull().c_str());
|
---|
3223 | }
|
---|
3224 |
|
---|
3225 | pProgress.createObject();
|
---|
3226 | rc = pProgress->init(m->pVirtualBox,
|
---|
3227 | static_cast <IMedium *>(this),
|
---|
3228 | BstrFmt(tr("Compacting medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3229 | TRUE /* aCancelable */);
|
---|
3230 | if (FAILED(rc))
|
---|
3231 | {
|
---|
3232 | delete pMediumLockList;
|
---|
3233 | throw rc;
|
---|
3234 | }
|
---|
3235 |
|
---|
3236 | /* setup task object to carry out the operation asynchronously */
|
---|
3237 | pTask = new Medium::CompactTask(this, pProgress, pMediumLockList);
|
---|
3238 | rc = pTask->rc();
|
---|
3239 | AssertComRC(rc);
|
---|
3240 | if (FAILED(rc))
|
---|
3241 | throw rc;
|
---|
3242 | }
|
---|
3243 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3244 |
|
---|
3245 | if (SUCCEEDED(rc))
|
---|
3246 | {
|
---|
3247 | rc = pTask->createThread();
|
---|
3248 |
|
---|
3249 | if (SUCCEEDED(rc))
|
---|
3250 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3251 | }
|
---|
3252 | else if (pTask != NULL)
|
---|
3253 | delete pTask;
|
---|
3254 |
|
---|
3255 | return rc;
|
---|
3256 | }
|
---|
3257 |
|
---|
3258 | HRESULT Medium::resize(LONG64 aLogicalSize,
|
---|
3259 | ComPtr<IProgress> &aProgress)
|
---|
3260 | {
|
---|
3261 | HRESULT rc = S_OK;
|
---|
3262 | ComObjPtr<Progress> pProgress;
|
---|
3263 | Medium::Task *pTask = NULL;
|
---|
3264 |
|
---|
3265 | try
|
---|
3266 | {
|
---|
3267 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3268 |
|
---|
3269 | /* Build the medium lock list. */
|
---|
3270 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3271 | alock.release();
|
---|
3272 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
3273 | this /* pToLockWrite */,
|
---|
3274 | false /* fMediumLockWriteAll */,
|
---|
3275 | NULL,
|
---|
3276 | *pMediumLockList);
|
---|
3277 | alock.acquire();
|
---|
3278 | if (FAILED(rc))
|
---|
3279 | {
|
---|
3280 | delete pMediumLockList;
|
---|
3281 | throw rc;
|
---|
3282 | }
|
---|
3283 |
|
---|
3284 | alock.release();
|
---|
3285 | rc = pMediumLockList->Lock();
|
---|
3286 | alock.acquire();
|
---|
3287 | if (FAILED(rc))
|
---|
3288 | {
|
---|
3289 | delete pMediumLockList;
|
---|
3290 | throw setError(rc,
|
---|
3291 | tr("Failed to lock media when compacting '%s'"),
|
---|
3292 | i_getLocationFull().c_str());
|
---|
3293 | }
|
---|
3294 |
|
---|
3295 | pProgress.createObject();
|
---|
3296 | rc = pProgress->init(m->pVirtualBox,
|
---|
3297 | static_cast <IMedium *>(this),
|
---|
3298 | BstrFmt(tr("Compacting medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3299 | TRUE /* aCancelable */);
|
---|
3300 | if (FAILED(rc))
|
---|
3301 | {
|
---|
3302 | delete pMediumLockList;
|
---|
3303 | throw rc;
|
---|
3304 | }
|
---|
3305 |
|
---|
3306 | /* setup task object to carry out the operation asynchronously */
|
---|
3307 | pTask = new Medium::ResizeTask(this, aLogicalSize, pProgress, pMediumLockList);
|
---|
3308 | rc = pTask->rc();
|
---|
3309 | AssertComRC(rc);
|
---|
3310 | if (FAILED(rc))
|
---|
3311 | throw rc;
|
---|
3312 | }
|
---|
3313 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3314 |
|
---|
3315 | if (SUCCEEDED(rc))
|
---|
3316 | {
|
---|
3317 | rc = pTask->createThread();
|
---|
3318 |
|
---|
3319 | if (SUCCEEDED(rc))
|
---|
3320 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3321 | }
|
---|
3322 | else if (pTask != NULL)
|
---|
3323 | delete pTask;
|
---|
3324 |
|
---|
3325 | return rc;
|
---|
3326 | }
|
---|
3327 |
|
---|
3328 | HRESULT Medium::reset(AutoCaller &autoCaller, ComPtr<IProgress> &aProgress)
|
---|
3329 | {
|
---|
3330 | HRESULT rc = S_OK;
|
---|
3331 | ComObjPtr<Progress> pProgress;
|
---|
3332 | Medium::Task *pTask = NULL;
|
---|
3333 |
|
---|
3334 | try
|
---|
3335 | {
|
---|
3336 | autoCaller.release();
|
---|
3337 |
|
---|
3338 | /* It is possible that some previous/concurrent uninit has already
|
---|
3339 | * cleared the pVirtualBox reference, see #uninit(). */
|
---|
3340 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
3341 |
|
---|
3342 | /* canClose() needs the tree lock */
|
---|
3343 | AutoMultiWriteLock2 multilock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL,
|
---|
3344 | this->lockHandle()
|
---|
3345 | COMMA_LOCKVAL_SRC_POS);
|
---|
3346 |
|
---|
3347 | autoCaller.add();
|
---|
3348 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
3349 |
|
---|
3350 | LogFlowThisFunc(("ENTER for medium %s\n", m->strLocationFull.c_str()));
|
---|
3351 |
|
---|
3352 | if (m->pParent.isNull())
|
---|
3353 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3354 | tr("Medium type of '%s' is not differencing"),
|
---|
3355 | m->strLocationFull.c_str());
|
---|
3356 |
|
---|
3357 | rc = i_canClose();
|
---|
3358 | if (FAILED(rc))
|
---|
3359 | throw rc;
|
---|
3360 |
|
---|
3361 | /* Build the medium lock list. */
|
---|
3362 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3363 | multilock.release();
|
---|
3364 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
3365 | this /* pToLockWrite */,
|
---|
3366 | false /* fMediumLockWriteAll */,
|
---|
3367 | NULL,
|
---|
3368 | *pMediumLockList);
|
---|
3369 | multilock.acquire();
|
---|
3370 | if (FAILED(rc))
|
---|
3371 | {
|
---|
3372 | delete pMediumLockList;
|
---|
3373 | throw rc;
|
---|
3374 | }
|
---|
3375 |
|
---|
3376 | multilock.release();
|
---|
3377 | rc = pMediumLockList->Lock();
|
---|
3378 | multilock.acquire();
|
---|
3379 | if (FAILED(rc))
|
---|
3380 | {
|
---|
3381 | delete pMediumLockList;
|
---|
3382 | throw setError(rc,
|
---|
3383 | tr("Failed to lock media when resetting '%s'"),
|
---|
3384 | i_getLocationFull().c_str());
|
---|
3385 | }
|
---|
3386 |
|
---|
3387 | pProgress.createObject();
|
---|
3388 | rc = pProgress->init(m->pVirtualBox,
|
---|
3389 | static_cast<IMedium*>(this),
|
---|
3390 | BstrFmt(tr("Resetting differencing medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3391 | FALSE /* aCancelable */);
|
---|
3392 | if (FAILED(rc))
|
---|
3393 | throw rc;
|
---|
3394 |
|
---|
3395 | /* setup task object to carry out the operation asynchronously */
|
---|
3396 | pTask = new Medium::ResetTask(this, pProgress, pMediumLockList);
|
---|
3397 | rc = pTask->rc();
|
---|
3398 | AssertComRC(rc);
|
---|
3399 | if (FAILED(rc))
|
---|
3400 | throw rc;
|
---|
3401 | }
|
---|
3402 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3403 |
|
---|
3404 | if (SUCCEEDED(rc))
|
---|
3405 | {
|
---|
3406 | rc = pTask->createThread();
|
---|
3407 |
|
---|
3408 | if (SUCCEEDED(rc))
|
---|
3409 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3410 | }
|
---|
3411 | else if (pTask != NULL)
|
---|
3412 | delete pTask;
|
---|
3413 |
|
---|
3414 | LogFlowThisFunc(("LEAVE, rc=%Rhrc\n", rc));
|
---|
3415 |
|
---|
3416 | return rc;
|
---|
3417 | }
|
---|
3418 |
|
---|
3419 | HRESULT Medium::changeEncryption(const com::Utf8Str &aCurrentPassword, const com::Utf8Str &aCipher,
|
---|
3420 | const com::Utf8Str &aNewPassword, const com::Utf8Str &aNewPasswordId,
|
---|
3421 | ComPtr<IProgress> &aProgress)
|
---|
3422 | {
|
---|
3423 | HRESULT rc = S_OK;
|
---|
3424 | ComObjPtr<Progress> pProgress;
|
---|
3425 | Medium::Task *pTask = NULL;
|
---|
3426 |
|
---|
3427 | try
|
---|
3428 | {
|
---|
3429 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3430 |
|
---|
3431 | DeviceType_T devType = i_getDeviceType();
|
---|
3432 | /* Cannot encrypt DVD or floppy images so far. */
|
---|
3433 | if ( devType == DeviceType_DVD
|
---|
3434 | || devType == DeviceType_Floppy)
|
---|
3435 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3436 | tr("Cannot encrypt DVD or Floppy medium '%s'"),
|
---|
3437 | m->strLocationFull.c_str());
|
---|
3438 |
|
---|
3439 | /* Cannot encrypt media which are attached to more than one virtual machine. */
|
---|
3440 | if (m->backRefs.size() > 1)
|
---|
3441 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3442 | tr("Cannot encrypt medium '%s' because it is attached to %d virtual machines"),
|
---|
3443 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
3444 |
|
---|
3445 | if (i_getChildren().size() != 0)
|
---|
3446 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3447 | tr("Cannot encrypt medium '%s' because it has %d children"),
|
---|
3448 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
3449 |
|
---|
3450 | /* Build the medium lock list. */
|
---|
3451 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3452 | alock.release();
|
---|
3453 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
3454 | this /* pToLockWrite */,
|
---|
3455 | true /* fMediumLockAllWrite */,
|
---|
3456 | NULL,
|
---|
3457 | *pMediumLockList);
|
---|
3458 | alock.acquire();
|
---|
3459 | if (FAILED(rc))
|
---|
3460 | {
|
---|
3461 | delete pMediumLockList;
|
---|
3462 | throw rc;
|
---|
3463 | }
|
---|
3464 |
|
---|
3465 | alock.release();
|
---|
3466 | rc = pMediumLockList->Lock();
|
---|
3467 | alock.acquire();
|
---|
3468 | if (FAILED(rc))
|
---|
3469 | {
|
---|
3470 | delete pMediumLockList;
|
---|
3471 | throw setError(rc,
|
---|
3472 | tr("Failed to lock media for encryption '%s'"),
|
---|
3473 | i_getLocationFull().c_str());
|
---|
3474 | }
|
---|
3475 |
|
---|
3476 | /*
|
---|
3477 | * Check all media in the chain to not contain any branches or references to
|
---|
3478 | * other virtual machines, we support encrypting only a list of differencing media at the moment.
|
---|
3479 | */
|
---|
3480 | MediumLockList::Base::const_iterator mediumListBegin = pMediumLockList->GetBegin();
|
---|
3481 | MediumLockList::Base::const_iterator mediumListEnd = pMediumLockList->GetEnd();
|
---|
3482 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
3483 | it != mediumListEnd;
|
---|
3484 | ++it)
|
---|
3485 | {
|
---|
3486 | const MediumLock &mediumLock = *it;
|
---|
3487 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
3488 | AutoReadLock mediumReadLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
3489 |
|
---|
3490 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
3491 |
|
---|
3492 | if (pMedium->m->backRefs.size() > 1)
|
---|
3493 | {
|
---|
3494 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3495 | tr("Cannot encrypt medium '%s' because it is attached to %d virtual machines"),
|
---|
3496 | pMedium->m->strLocationFull.c_str(), pMedium->m->backRefs.size());
|
---|
3497 | break;
|
---|
3498 | }
|
---|
3499 | else if (pMedium->i_getChildren().size() > 1)
|
---|
3500 | {
|
---|
3501 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3502 | tr("Cannot encrypt medium '%s' because it has %d children"),
|
---|
3503 | pMedium->m->strLocationFull.c_str(), pMedium->i_getChildren().size());
|
---|
3504 | break;
|
---|
3505 | }
|
---|
3506 | }
|
---|
3507 |
|
---|
3508 | if (FAILED(rc))
|
---|
3509 | {
|
---|
3510 | delete pMediumLockList;
|
---|
3511 | throw rc;
|
---|
3512 | }
|
---|
3513 |
|
---|
3514 | const char *pszAction = "Encrypting";
|
---|
3515 | if ( aCurrentPassword.isNotEmpty()
|
---|
3516 | && aCipher.isEmpty())
|
---|
3517 | pszAction = "Decrypting";
|
---|
3518 |
|
---|
3519 | pProgress.createObject();
|
---|
3520 | rc = pProgress->init(m->pVirtualBox,
|
---|
3521 | static_cast <IMedium *>(this),
|
---|
3522 | BstrFmt(tr("%s medium '%s'"), pszAction, m->strLocationFull.c_str()).raw(),
|
---|
3523 | TRUE /* aCancelable */);
|
---|
3524 | if (FAILED(rc))
|
---|
3525 | {
|
---|
3526 | delete pMediumLockList;
|
---|
3527 | throw rc;
|
---|
3528 | }
|
---|
3529 |
|
---|
3530 | /* setup task object to carry out the operation asynchronously */
|
---|
3531 | pTask = new Medium::EncryptTask(this, aNewPassword, aCurrentPassword,
|
---|
3532 | aCipher, aNewPasswordId, pProgress, pMediumLockList);
|
---|
3533 | rc = pTask->rc();
|
---|
3534 | AssertComRC(rc);
|
---|
3535 | if (FAILED(rc))
|
---|
3536 | throw rc;
|
---|
3537 | }
|
---|
3538 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3539 |
|
---|
3540 | if (SUCCEEDED(rc))
|
---|
3541 | {
|
---|
3542 | rc = pTask->createThread();
|
---|
3543 |
|
---|
3544 | if (SUCCEEDED(rc))
|
---|
3545 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3546 | }
|
---|
3547 | else if (pTask != NULL)
|
---|
3548 | delete pTask;
|
---|
3549 |
|
---|
3550 | return rc;
|
---|
3551 | }
|
---|
3552 |
|
---|
3553 | HRESULT Medium::getEncryptionSettings(com::Utf8Str &aCipher, com::Utf8Str &aPasswordId)
|
---|
3554 | {
|
---|
3555 | #ifndef VBOX_WITH_EXTPACK
|
---|
3556 | RT_NOREF(aCipher, aPasswordId);
|
---|
3557 | #endif
|
---|
3558 | HRESULT rc = S_OK;
|
---|
3559 |
|
---|
3560 | try
|
---|
3561 | {
|
---|
3562 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
3563 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3564 |
|
---|
3565 | /* Check whether encryption is configured for this medium. */
|
---|
3566 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
3567 | if (it == pBase->m->mapProperties.end())
|
---|
3568 | throw VBOX_E_NOT_SUPPORTED;
|
---|
3569 |
|
---|
3570 | # ifdef VBOX_WITH_EXTPACK
|
---|
3571 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
3572 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
3573 | {
|
---|
3574 | /* Load the plugin */
|
---|
3575 | Utf8Str strPlugin;
|
---|
3576 | rc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
3577 | if (SUCCEEDED(rc))
|
---|
3578 | {
|
---|
3579 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
3580 | if (RT_FAILURE(vrc))
|
---|
3581 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3582 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
3583 | i_vdError(vrc).c_str());
|
---|
3584 | }
|
---|
3585 | else
|
---|
3586 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3587 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
3588 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
3589 | }
|
---|
3590 | else
|
---|
3591 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3592 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
3593 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
3594 |
|
---|
3595 | PVDISK pDisk = NULL;
|
---|
3596 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
3597 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
3598 |
|
---|
3599 | Medium::CryptoFilterSettings CryptoSettings;
|
---|
3600 |
|
---|
3601 | i_taskEncryptSettingsSetup(&CryptoSettings, NULL, it->second.c_str(), NULL, false /* fCreateKeyStore */);
|
---|
3602 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ | VD_FILTER_FLAGS_INFO, CryptoSettings.vdFilterIfaces);
|
---|
3603 | if (RT_FAILURE(vrc))
|
---|
3604 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3605 | tr("Failed to load the encryption filter: %s"),
|
---|
3606 | i_vdError(vrc).c_str());
|
---|
3607 |
|
---|
3608 | it = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
3609 | if (it == pBase->m->mapProperties.end())
|
---|
3610 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3611 | tr("Image is configured for encryption but doesn't has a KeyId set"));
|
---|
3612 |
|
---|
3613 | aPasswordId = it->second.c_str();
|
---|
3614 | aCipher = CryptoSettings.pszCipherReturned;
|
---|
3615 | RTStrFree(CryptoSettings.pszCipherReturned);
|
---|
3616 |
|
---|
3617 | VDDestroy(pDisk);
|
---|
3618 | # else
|
---|
3619 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3620 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
3621 | # endif
|
---|
3622 | }
|
---|
3623 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3624 |
|
---|
3625 | return rc;
|
---|
3626 | }
|
---|
3627 |
|
---|
3628 | HRESULT Medium::checkEncryptionPassword(const com::Utf8Str &aPassword)
|
---|
3629 | {
|
---|
3630 | HRESULT rc = S_OK;
|
---|
3631 |
|
---|
3632 | try
|
---|
3633 | {
|
---|
3634 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
3635 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3636 |
|
---|
3637 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
3638 | if (it == pBase->m->mapProperties.end())
|
---|
3639 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3640 | tr("The image is not configured for encryption"));
|
---|
3641 |
|
---|
3642 | if (aPassword.isEmpty())
|
---|
3643 | throw setError(E_INVALIDARG,
|
---|
3644 | tr("The given password must not be empty"));
|
---|
3645 |
|
---|
3646 | # ifdef VBOX_WITH_EXTPACK
|
---|
3647 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
3648 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
3649 | {
|
---|
3650 | /* Load the plugin */
|
---|
3651 | Utf8Str strPlugin;
|
---|
3652 | rc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
3653 | if (SUCCEEDED(rc))
|
---|
3654 | {
|
---|
3655 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
3656 | if (RT_FAILURE(vrc))
|
---|
3657 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3658 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
3659 | i_vdError(vrc).c_str());
|
---|
3660 | }
|
---|
3661 | else
|
---|
3662 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3663 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
3664 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
3665 | }
|
---|
3666 | else
|
---|
3667 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3668 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
3669 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
3670 |
|
---|
3671 | PVDISK pDisk = NULL;
|
---|
3672 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
3673 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
3674 |
|
---|
3675 | Medium::CryptoFilterSettings CryptoSettings;
|
---|
3676 |
|
---|
3677 | i_taskEncryptSettingsSetup(&CryptoSettings, NULL, it->second.c_str(), aPassword.c_str(),
|
---|
3678 | false /* fCreateKeyStore */);
|
---|
3679 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ, CryptoSettings.vdFilterIfaces);
|
---|
3680 | if (vrc == VERR_VD_PASSWORD_INCORRECT)
|
---|
3681 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
3682 | tr("The given password is incorrect"));
|
---|
3683 | else if (RT_FAILURE(vrc))
|
---|
3684 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3685 | tr("Failed to load the encryption filter: %s"),
|
---|
3686 | i_vdError(vrc).c_str());
|
---|
3687 |
|
---|
3688 | VDDestroy(pDisk);
|
---|
3689 | # else
|
---|
3690 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3691 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
3692 | # endif
|
---|
3693 | }
|
---|
3694 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3695 |
|
---|
3696 | return rc;
|
---|
3697 | }
|
---|
3698 |
|
---|
3699 | ////////////////////////////////////////////////////////////////////////////////
|
---|
3700 | //
|
---|
3701 | // Medium public internal methods
|
---|
3702 | //
|
---|
3703 | ////////////////////////////////////////////////////////////////////////////////
|
---|
3704 |
|
---|
3705 | /**
|
---|
3706 | * Internal method to return the medium's parent medium. Must have caller + locking!
|
---|
3707 | * @return
|
---|
3708 | */
|
---|
3709 | const ComObjPtr<Medium>& Medium::i_getParent() const
|
---|
3710 | {
|
---|
3711 | return m->pParent;
|
---|
3712 | }
|
---|
3713 |
|
---|
3714 | /**
|
---|
3715 | * Internal method to return the medium's list of child media. Must have caller + locking!
|
---|
3716 | * @return
|
---|
3717 | */
|
---|
3718 | const MediaList& Medium::i_getChildren() const
|
---|
3719 | {
|
---|
3720 | return m->llChildren;
|
---|
3721 | }
|
---|
3722 |
|
---|
3723 | /**
|
---|
3724 | * Internal method to return the medium's GUID. Must have caller + locking!
|
---|
3725 | * @return
|
---|
3726 | */
|
---|
3727 | const Guid& Medium::i_getId() const
|
---|
3728 | {
|
---|
3729 | return m->id;
|
---|
3730 | }
|
---|
3731 |
|
---|
3732 | /**
|
---|
3733 | * Internal method to return the medium's state. Must have caller + locking!
|
---|
3734 | * @return
|
---|
3735 | */
|
---|
3736 | MediumState_T Medium::i_getState() const
|
---|
3737 | {
|
---|
3738 | return m->state;
|
---|
3739 | }
|
---|
3740 |
|
---|
3741 | /**
|
---|
3742 | * Internal method to return the medium's variant. Must have caller + locking!
|
---|
3743 | * @return
|
---|
3744 | */
|
---|
3745 | MediumVariant_T Medium::i_getVariant() const
|
---|
3746 | {
|
---|
3747 | return m->variant;
|
---|
3748 | }
|
---|
3749 |
|
---|
3750 | /**
|
---|
3751 | * Internal method which returns true if this medium represents a host drive.
|
---|
3752 | * @return
|
---|
3753 | */
|
---|
3754 | bool Medium::i_isHostDrive() const
|
---|
3755 | {
|
---|
3756 | return m->hostDrive;
|
---|
3757 | }
|
---|
3758 |
|
---|
3759 | /**
|
---|
3760 | * Internal method to return the medium's full location. Must have caller + locking!
|
---|
3761 | * @return
|
---|
3762 | */
|
---|
3763 | const Utf8Str& Medium::i_getLocationFull() const
|
---|
3764 | {
|
---|
3765 | return m->strLocationFull;
|
---|
3766 | }
|
---|
3767 |
|
---|
3768 | /**
|
---|
3769 | * Internal method to return the medium's format string. Must have caller + locking!
|
---|
3770 | * @return
|
---|
3771 | */
|
---|
3772 | const Utf8Str& Medium::i_getFormat() const
|
---|
3773 | {
|
---|
3774 | return m->strFormat;
|
---|
3775 | }
|
---|
3776 |
|
---|
3777 | /**
|
---|
3778 | * Internal method to return the medium's format object. Must have caller + locking!
|
---|
3779 | * @return
|
---|
3780 | */
|
---|
3781 | const ComObjPtr<MediumFormat>& Medium::i_getMediumFormat() const
|
---|
3782 | {
|
---|
3783 | return m->formatObj;
|
---|
3784 | }
|
---|
3785 |
|
---|
3786 | /**
|
---|
3787 | * Internal method that returns true if the medium is represented by a file on the host disk
|
---|
3788 | * (and not iSCSI or something).
|
---|
3789 | * @return
|
---|
3790 | */
|
---|
3791 | bool Medium::i_isMediumFormatFile() const
|
---|
3792 | {
|
---|
3793 | if ( m->formatObj
|
---|
3794 | && (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
3795 | )
|
---|
3796 | return true;
|
---|
3797 | return false;
|
---|
3798 | }
|
---|
3799 |
|
---|
3800 | /**
|
---|
3801 | * Internal method to return the medium's size. Must have caller + locking!
|
---|
3802 | * @return
|
---|
3803 | */
|
---|
3804 | uint64_t Medium::i_getSize() const
|
---|
3805 | {
|
---|
3806 | return m->size;
|
---|
3807 | }
|
---|
3808 |
|
---|
3809 | /**
|
---|
3810 | * Internal method to return the medium's size. Must have caller + locking!
|
---|
3811 | * @return
|
---|
3812 | */
|
---|
3813 | uint64_t Medium::i_getLogicalSize() const
|
---|
3814 | {
|
---|
3815 | return m->logicalSize;
|
---|
3816 | }
|
---|
3817 |
|
---|
3818 | /**
|
---|
3819 | * Returns the medium device type. Must have caller + locking!
|
---|
3820 | * @return
|
---|
3821 | */
|
---|
3822 | DeviceType_T Medium::i_getDeviceType() const
|
---|
3823 | {
|
---|
3824 | return m->devType;
|
---|
3825 | }
|
---|
3826 |
|
---|
3827 | /**
|
---|
3828 | * Returns the medium type. Must have caller + locking!
|
---|
3829 | * @return
|
---|
3830 | */
|
---|
3831 | MediumType_T Medium::i_getType() const
|
---|
3832 | {
|
---|
3833 | return m->type;
|
---|
3834 | }
|
---|
3835 |
|
---|
3836 | /**
|
---|
3837 | * Returns a short version of the location attribute.
|
---|
3838 | *
|
---|
3839 | * @note Must be called from under this object's read or write lock.
|
---|
3840 | */
|
---|
3841 | Utf8Str Medium::i_getName()
|
---|
3842 | {
|
---|
3843 | Utf8Str name = RTPathFilename(m->strLocationFull.c_str());
|
---|
3844 | return name;
|
---|
3845 | }
|
---|
3846 |
|
---|
3847 | /**
|
---|
3848 | * This adds the given UUID to the list of media registries in which this
|
---|
3849 | * medium should be registered. The UUID can either be a machine UUID,
|
---|
3850 | * to add a machine registry, or the global registry UUID as returned by
|
---|
3851 | * VirtualBox::getGlobalRegistryId().
|
---|
3852 | *
|
---|
3853 | * Note that for hard disks, this method does nothing if the medium is
|
---|
3854 | * already in another registry to avoid having hard disks in more than
|
---|
3855 | * one registry, which causes trouble with keeping diff images in sync.
|
---|
3856 | * See getFirstRegistryMachineId() for details.
|
---|
3857 | *
|
---|
3858 | * @param id
|
---|
3859 | * @return true if the registry was added; false if the given id was already on the list.
|
---|
3860 | */
|
---|
3861 | bool Medium::i_addRegistry(const Guid& id)
|
---|
3862 | {
|
---|
3863 | AutoCaller autoCaller(this);
|
---|
3864 | if (FAILED(autoCaller.rc()))
|
---|
3865 | return false;
|
---|
3866 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3867 |
|
---|
3868 | bool fAdd = true;
|
---|
3869 |
|
---|
3870 | // hard disks cannot be in more than one registry
|
---|
3871 | if ( m->devType == DeviceType_HardDisk
|
---|
3872 | && m->llRegistryIDs.size() > 0)
|
---|
3873 | fAdd = false;
|
---|
3874 |
|
---|
3875 | // no need to add the UUID twice
|
---|
3876 | if (fAdd)
|
---|
3877 | {
|
---|
3878 | for (GuidList::const_iterator it = m->llRegistryIDs.begin();
|
---|
3879 | it != m->llRegistryIDs.end();
|
---|
3880 | ++it)
|
---|
3881 | {
|
---|
3882 | if ((*it) == id)
|
---|
3883 | {
|
---|
3884 | fAdd = false;
|
---|
3885 | break;
|
---|
3886 | }
|
---|
3887 | }
|
---|
3888 | }
|
---|
3889 |
|
---|
3890 | if (fAdd)
|
---|
3891 | m->llRegistryIDs.push_back(id);
|
---|
3892 |
|
---|
3893 | return fAdd;
|
---|
3894 | }
|
---|
3895 |
|
---|
3896 | /**
|
---|
3897 | * This adds the given UUID to the list of media registries in which this
|
---|
3898 | * medium should be registered. The UUID can either be a machine UUID,
|
---|
3899 | * to add a machine registry, or the global registry UUID as returned by
|
---|
3900 | * VirtualBox::getGlobalRegistryId(). This recurses over all children.
|
---|
3901 | *
|
---|
3902 | * Note that for hard disks, this method does nothing if the medium is
|
---|
3903 | * already in another registry to avoid having hard disks in more than
|
---|
3904 | * one registry, which causes trouble with keeping diff images in sync.
|
---|
3905 | * See getFirstRegistryMachineId() for details.
|
---|
3906 | *
|
---|
3907 | * @note the caller must hold the media tree lock for reading.
|
---|
3908 | *
|
---|
3909 | * @param id
|
---|
3910 | * @return true if the registry was added; false if the given id was already on the list.
|
---|
3911 | */
|
---|
3912 | bool Medium::i_addRegistryRecursive(const Guid &id)
|
---|
3913 | {
|
---|
3914 | AutoCaller autoCaller(this);
|
---|
3915 | if (FAILED(autoCaller.rc()))
|
---|
3916 | return false;
|
---|
3917 |
|
---|
3918 | bool fAdd = i_addRegistry(id);
|
---|
3919 |
|
---|
3920 | // protected by the medium tree lock held by our original caller
|
---|
3921 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
3922 | it != i_getChildren().end();
|
---|
3923 | ++it)
|
---|
3924 | {
|
---|
3925 | Medium *pChild = *it;
|
---|
3926 | fAdd |= pChild->i_addRegistryRecursive(id);
|
---|
3927 | }
|
---|
3928 |
|
---|
3929 | return fAdd;
|
---|
3930 | }
|
---|
3931 |
|
---|
3932 | /**
|
---|
3933 | * Removes the given UUID from the list of media registry UUIDs of this medium.
|
---|
3934 | *
|
---|
3935 | * @param id
|
---|
3936 | * @return true if the UUID was found or false if not.
|
---|
3937 | */
|
---|
3938 | bool Medium::i_removeRegistry(const Guid &id)
|
---|
3939 | {
|
---|
3940 | AutoCaller autoCaller(this);
|
---|
3941 | if (FAILED(autoCaller.rc()))
|
---|
3942 | return false;
|
---|
3943 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3944 |
|
---|
3945 | bool fRemove = false;
|
---|
3946 |
|
---|
3947 | /// @todo r=klaus eliminate this code, replace it by using find.
|
---|
3948 | for (GuidList::iterator it = m->llRegistryIDs.begin();
|
---|
3949 | it != m->llRegistryIDs.end();
|
---|
3950 | ++it)
|
---|
3951 | {
|
---|
3952 | if ((*it) == id)
|
---|
3953 | {
|
---|
3954 | // getting away with this as the iterator isn't used after
|
---|
3955 | m->llRegistryIDs.erase(it);
|
---|
3956 | fRemove = true;
|
---|
3957 | break;
|
---|
3958 | }
|
---|
3959 | }
|
---|
3960 |
|
---|
3961 | return fRemove;
|
---|
3962 | }
|
---|
3963 |
|
---|
3964 | /**
|
---|
3965 | * Removes the given UUID from the list of media registry UUIDs, for this
|
---|
3966 | * medium and all its children recursively.
|
---|
3967 | *
|
---|
3968 | * @note the caller must hold the media tree lock for reading.
|
---|
3969 | *
|
---|
3970 | * @param id
|
---|
3971 | * @return true if the UUID was found or false if not.
|
---|
3972 | */
|
---|
3973 | bool Medium::i_removeRegistryRecursive(const Guid &id)
|
---|
3974 | {
|
---|
3975 | AutoCaller autoCaller(this);
|
---|
3976 | if (FAILED(autoCaller.rc()))
|
---|
3977 | return false;
|
---|
3978 |
|
---|
3979 | bool fRemove = i_removeRegistry(id);
|
---|
3980 |
|
---|
3981 | // protected by the medium tree lock held by our original caller
|
---|
3982 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
3983 | it != i_getChildren().end();
|
---|
3984 | ++it)
|
---|
3985 | {
|
---|
3986 | Medium *pChild = *it;
|
---|
3987 | fRemove |= pChild->i_removeRegistryRecursive(id);
|
---|
3988 | }
|
---|
3989 |
|
---|
3990 | return fRemove;
|
---|
3991 | }
|
---|
3992 |
|
---|
3993 | /**
|
---|
3994 | * Returns true if id is in the list of media registries for this medium.
|
---|
3995 | *
|
---|
3996 | * Must have caller + read locking!
|
---|
3997 | *
|
---|
3998 | * @param id
|
---|
3999 | * @return
|
---|
4000 | */
|
---|
4001 | bool Medium::i_isInRegistry(const Guid &id)
|
---|
4002 | {
|
---|
4003 | /// @todo r=klaus eliminate this code, replace it by using find.
|
---|
4004 | for (GuidList::const_iterator it = m->llRegistryIDs.begin();
|
---|
4005 | it != m->llRegistryIDs.end();
|
---|
4006 | ++it)
|
---|
4007 | {
|
---|
4008 | if (*it == id)
|
---|
4009 | return true;
|
---|
4010 | }
|
---|
4011 |
|
---|
4012 | return false;
|
---|
4013 | }
|
---|
4014 |
|
---|
4015 | /**
|
---|
4016 | * Internal method to return the medium's first registry machine (i.e. the machine in whose
|
---|
4017 | * machine XML this medium is listed).
|
---|
4018 | *
|
---|
4019 | * Every attached medium must now (4.0) reside in at least one media registry, which is identified
|
---|
4020 | * by a UUID. This is either a machine UUID if the machine is from 4.0 or newer, in which case
|
---|
4021 | * machines have their own media registries, or it is the pseudo-UUID of the VirtualBox
|
---|
4022 | * object if the machine is old and still needs the global registry in VirtualBox.xml.
|
---|
4023 | *
|
---|
4024 | * By definition, hard disks may only be in one media registry, in which all its children
|
---|
4025 | * will be stored as well. Otherwise we run into problems with having keep multiple registries
|
---|
4026 | * in sync. (This is the "cloned VM" case in which VM1 may link to the disks of VM2; in this
|
---|
4027 | * case, only VM2's registry is used for the disk in question.)
|
---|
4028 | *
|
---|
4029 | * If there is no medium registry, particularly if the medium has not been attached yet, this
|
---|
4030 | * does not modify uuid and returns false.
|
---|
4031 | *
|
---|
4032 | * ISOs and RAWs, by contrast, can be in more than one repository to make things easier for
|
---|
4033 | * the user.
|
---|
4034 | *
|
---|
4035 | * Must have caller + locking!
|
---|
4036 | *
|
---|
4037 | * @param uuid Receives first registry machine UUID, if available.
|
---|
4038 | * @return true if uuid was set.
|
---|
4039 | */
|
---|
4040 | bool Medium::i_getFirstRegistryMachineId(Guid &uuid) const
|
---|
4041 | {
|
---|
4042 | if (m->llRegistryIDs.size())
|
---|
4043 | {
|
---|
4044 | uuid = m->llRegistryIDs.front();
|
---|
4045 | return true;
|
---|
4046 | }
|
---|
4047 | return false;
|
---|
4048 | }
|
---|
4049 |
|
---|
4050 | /**
|
---|
4051 | * Marks all the registries in which this medium is registered as modified.
|
---|
4052 | */
|
---|
4053 | void Medium::i_markRegistriesModified()
|
---|
4054 | {
|
---|
4055 | AutoCaller autoCaller(this);
|
---|
4056 | if (FAILED(autoCaller.rc())) return;
|
---|
4057 |
|
---|
4058 | // Get local copy, as keeping the lock over VirtualBox::markRegistryModified
|
---|
4059 | // causes trouble with the lock order
|
---|
4060 | GuidList llRegistryIDs;
|
---|
4061 | {
|
---|
4062 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4063 | llRegistryIDs = m->llRegistryIDs;
|
---|
4064 | }
|
---|
4065 |
|
---|
4066 | autoCaller.release();
|
---|
4067 |
|
---|
4068 | /* Save the error information now, the implicit restore when this goes
|
---|
4069 | * out of scope will throw away spurious additional errors created below. */
|
---|
4070 | ErrorInfoKeeper eik;
|
---|
4071 | for (GuidList::const_iterator it = llRegistryIDs.begin();
|
---|
4072 | it != llRegistryIDs.end();
|
---|
4073 | ++it)
|
---|
4074 | {
|
---|
4075 | m->pVirtualBox->i_markRegistryModified(*it);
|
---|
4076 | }
|
---|
4077 | }
|
---|
4078 |
|
---|
4079 | /**
|
---|
4080 | * Adds the given machine and optionally the snapshot to the list of the objects
|
---|
4081 | * this medium is attached to.
|
---|
4082 | *
|
---|
4083 | * @param aMachineId Machine ID.
|
---|
4084 | * @param aSnapshotId Snapshot ID; when non-empty, adds a snapshot attachment.
|
---|
4085 | */
|
---|
4086 | HRESULT Medium::i_addBackReference(const Guid &aMachineId,
|
---|
4087 | const Guid &aSnapshotId /*= Guid::Empty*/)
|
---|
4088 | {
|
---|
4089 | AssertReturn(aMachineId.isValid(), E_FAIL);
|
---|
4090 |
|
---|
4091 | LogFlowThisFunc(("ENTER, aMachineId: {%RTuuid}, aSnapshotId: {%RTuuid}\n", aMachineId.raw(), aSnapshotId.raw()));
|
---|
4092 |
|
---|
4093 | AutoCaller autoCaller(this);
|
---|
4094 | AssertComRCReturnRC(autoCaller.rc());
|
---|
4095 |
|
---|
4096 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4097 |
|
---|
4098 | switch (m->state)
|
---|
4099 | {
|
---|
4100 | case MediumState_Created:
|
---|
4101 | case MediumState_Inaccessible:
|
---|
4102 | case MediumState_LockedRead:
|
---|
4103 | case MediumState_LockedWrite:
|
---|
4104 | break;
|
---|
4105 |
|
---|
4106 | default:
|
---|
4107 | return i_setStateError();
|
---|
4108 | }
|
---|
4109 |
|
---|
4110 | if (m->numCreateDiffTasks > 0)
|
---|
4111 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4112 | tr("Cannot attach medium '%s' {%RTuuid}: %u differencing child media are being created"),
|
---|
4113 | m->strLocationFull.c_str(),
|
---|
4114 | m->id.raw(),
|
---|
4115 | m->numCreateDiffTasks);
|
---|
4116 |
|
---|
4117 | BackRefList::iterator it = std::find_if(m->backRefs.begin(),
|
---|
4118 | m->backRefs.end(),
|
---|
4119 | BackRef::EqualsTo(aMachineId));
|
---|
4120 | if (it == m->backRefs.end())
|
---|
4121 | {
|
---|
4122 | BackRef ref(aMachineId, aSnapshotId);
|
---|
4123 | m->backRefs.push_back(ref);
|
---|
4124 |
|
---|
4125 | return S_OK;
|
---|
4126 | }
|
---|
4127 |
|
---|
4128 | // if the caller has not supplied a snapshot ID, then we're attaching
|
---|
4129 | // to a machine a medium which represents the machine's current state,
|
---|
4130 | // so set the flag
|
---|
4131 |
|
---|
4132 | if (aSnapshotId.isZero())
|
---|
4133 | {
|
---|
4134 | /* sanity: no duplicate attachments */
|
---|
4135 | if (it->fInCurState)
|
---|
4136 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4137 | tr("Cannot attach medium '%s' {%RTuuid}: medium is already associated with the current state of machine uuid {%RTuuid}!"),
|
---|
4138 | m->strLocationFull.c_str(),
|
---|
4139 | m->id.raw(),
|
---|
4140 | aMachineId.raw());
|
---|
4141 | it->fInCurState = true;
|
---|
4142 |
|
---|
4143 | return S_OK;
|
---|
4144 | }
|
---|
4145 |
|
---|
4146 | // otherwise: a snapshot medium is being attached
|
---|
4147 |
|
---|
4148 | /* sanity: no duplicate attachments */
|
---|
4149 | for (GuidList::const_iterator jt = it->llSnapshotIds.begin();
|
---|
4150 | jt != it->llSnapshotIds.end();
|
---|
4151 | ++jt)
|
---|
4152 | {
|
---|
4153 | const Guid &idOldSnapshot = *jt;
|
---|
4154 |
|
---|
4155 | if (idOldSnapshot == aSnapshotId)
|
---|
4156 | {
|
---|
4157 | #ifdef DEBUG
|
---|
4158 | i_dumpBackRefs();
|
---|
4159 | #endif
|
---|
4160 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4161 | tr("Cannot attach medium '%s' {%RTuuid} from snapshot '%RTuuid': medium is already in use by this snapshot!"),
|
---|
4162 | m->strLocationFull.c_str(),
|
---|
4163 | m->id.raw(),
|
---|
4164 | aSnapshotId.raw());
|
---|
4165 | }
|
---|
4166 | }
|
---|
4167 |
|
---|
4168 | it->llSnapshotIds.push_back(aSnapshotId);
|
---|
4169 | // Do not touch fInCurState, as the image may be attached to the current
|
---|
4170 | // state *and* a snapshot, otherwise we lose the current state association!
|
---|
4171 |
|
---|
4172 | LogFlowThisFuncLeave();
|
---|
4173 |
|
---|
4174 | return S_OK;
|
---|
4175 | }
|
---|
4176 |
|
---|
4177 | /**
|
---|
4178 | * Removes the given machine and optionally the snapshot from the list of the
|
---|
4179 | * objects this medium is attached to.
|
---|
4180 | *
|
---|
4181 | * @param aMachineId Machine ID.
|
---|
4182 | * @param aSnapshotId Snapshot ID; when non-empty, removes the snapshot
|
---|
4183 | * attachment.
|
---|
4184 | */
|
---|
4185 | HRESULT Medium::i_removeBackReference(const Guid &aMachineId,
|
---|
4186 | const Guid &aSnapshotId /*= Guid::Empty*/)
|
---|
4187 | {
|
---|
4188 | AssertReturn(aMachineId.isValid(), E_FAIL);
|
---|
4189 |
|
---|
4190 | AutoCaller autoCaller(this);
|
---|
4191 | AssertComRCReturnRC(autoCaller.rc());
|
---|
4192 |
|
---|
4193 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4194 |
|
---|
4195 | BackRefList::iterator it =
|
---|
4196 | std::find_if(m->backRefs.begin(), m->backRefs.end(),
|
---|
4197 | BackRef::EqualsTo(aMachineId));
|
---|
4198 | AssertReturn(it != m->backRefs.end(), E_FAIL);
|
---|
4199 |
|
---|
4200 | if (aSnapshotId.isZero())
|
---|
4201 | {
|
---|
4202 | /* remove the current state attachment */
|
---|
4203 | it->fInCurState = false;
|
---|
4204 | }
|
---|
4205 | else
|
---|
4206 | {
|
---|
4207 | /* remove the snapshot attachment */
|
---|
4208 | GuidList::iterator jt = std::find(it->llSnapshotIds.begin(),
|
---|
4209 | it->llSnapshotIds.end(),
|
---|
4210 | aSnapshotId);
|
---|
4211 |
|
---|
4212 | AssertReturn(jt != it->llSnapshotIds.end(), E_FAIL);
|
---|
4213 | it->llSnapshotIds.erase(jt);
|
---|
4214 | }
|
---|
4215 |
|
---|
4216 | /* if the backref becomes empty, remove it */
|
---|
4217 | if (it->fInCurState == false && it->llSnapshotIds.size() == 0)
|
---|
4218 | m->backRefs.erase(it);
|
---|
4219 |
|
---|
4220 | return S_OK;
|
---|
4221 | }
|
---|
4222 |
|
---|
4223 | /**
|
---|
4224 | * Internal method to return the medium's list of backrefs. Must have caller + locking!
|
---|
4225 | * @return
|
---|
4226 | */
|
---|
4227 | const Guid* Medium::i_getFirstMachineBackrefId() const
|
---|
4228 | {
|
---|
4229 | if (!m->backRefs.size())
|
---|
4230 | return NULL;
|
---|
4231 |
|
---|
4232 | return &m->backRefs.front().machineId;
|
---|
4233 | }
|
---|
4234 |
|
---|
4235 | /**
|
---|
4236 | * Internal method which returns a machine that either this medium or one of its children
|
---|
4237 | * is attached to. This is used for finding a replacement media registry when an existing
|
---|
4238 | * media registry is about to be deleted in VirtualBox::unregisterMachine().
|
---|
4239 | *
|
---|
4240 | * Must have caller + locking, *and* caller must hold the media tree lock!
|
---|
4241 | * @return
|
---|
4242 | */
|
---|
4243 | const Guid* Medium::i_getAnyMachineBackref() const
|
---|
4244 | {
|
---|
4245 | if (m->backRefs.size())
|
---|
4246 | return &m->backRefs.front().machineId;
|
---|
4247 |
|
---|
4248 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
4249 | it != i_getChildren().end();
|
---|
4250 | ++it)
|
---|
4251 | {
|
---|
4252 | Medium *pChild = *it;
|
---|
4253 | // recurse for this child
|
---|
4254 | const Guid* puuid;
|
---|
4255 | if ((puuid = pChild->i_getAnyMachineBackref()))
|
---|
4256 | return puuid;
|
---|
4257 | }
|
---|
4258 |
|
---|
4259 | return NULL;
|
---|
4260 | }
|
---|
4261 |
|
---|
4262 | const Guid* Medium::i_getFirstMachineBackrefSnapshotId() const
|
---|
4263 | {
|
---|
4264 | if (!m->backRefs.size())
|
---|
4265 | return NULL;
|
---|
4266 |
|
---|
4267 | const BackRef &ref = m->backRefs.front();
|
---|
4268 | if (ref.llSnapshotIds.empty())
|
---|
4269 | return NULL;
|
---|
4270 |
|
---|
4271 | return &ref.llSnapshotIds.front();
|
---|
4272 | }
|
---|
4273 |
|
---|
4274 | size_t Medium::i_getMachineBackRefCount() const
|
---|
4275 | {
|
---|
4276 | return m->backRefs.size();
|
---|
4277 | }
|
---|
4278 |
|
---|
4279 | #ifdef DEBUG
|
---|
4280 | /**
|
---|
4281 | * Debugging helper that gets called after VirtualBox initialization that writes all
|
---|
4282 | * machine backreferences to the debug log.
|
---|
4283 | */
|
---|
4284 | void Medium::i_dumpBackRefs()
|
---|
4285 | {
|
---|
4286 | AutoCaller autoCaller(this);
|
---|
4287 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4288 |
|
---|
4289 | LogFlowThisFunc(("Dumping backrefs for medium '%s':\n", m->strLocationFull.c_str()));
|
---|
4290 |
|
---|
4291 | for (BackRefList::iterator it2 = m->backRefs.begin();
|
---|
4292 | it2 != m->backRefs.end();
|
---|
4293 | ++it2)
|
---|
4294 | {
|
---|
4295 | const BackRef &ref = *it2;
|
---|
4296 | LogFlowThisFunc((" Backref from machine {%RTuuid} (fInCurState: %d)\n", ref.machineId.raw(), ref.fInCurState));
|
---|
4297 |
|
---|
4298 | for (GuidList::const_iterator jt2 = it2->llSnapshotIds.begin();
|
---|
4299 | jt2 != it2->llSnapshotIds.end();
|
---|
4300 | ++jt2)
|
---|
4301 | {
|
---|
4302 | const Guid &id = *jt2;
|
---|
4303 | LogFlowThisFunc((" Backref from snapshot {%RTuuid}\n", id.raw()));
|
---|
4304 | }
|
---|
4305 | }
|
---|
4306 | }
|
---|
4307 | #endif
|
---|
4308 |
|
---|
4309 | /**
|
---|
4310 | * Checks if the given change of \a aOldPath to \a aNewPath affects the location
|
---|
4311 | * of this media and updates it if necessary to reflect the new location.
|
---|
4312 | *
|
---|
4313 | * @param strOldPath Old path (full).
|
---|
4314 | * @param strNewPath New path (full).
|
---|
4315 | *
|
---|
4316 | * @note Locks this object for writing.
|
---|
4317 | */
|
---|
4318 | HRESULT Medium::i_updatePath(const Utf8Str &strOldPath, const Utf8Str &strNewPath)
|
---|
4319 | {
|
---|
4320 | AssertReturn(!strOldPath.isEmpty(), E_FAIL);
|
---|
4321 | AssertReturn(!strNewPath.isEmpty(), E_FAIL);
|
---|
4322 |
|
---|
4323 | AutoCaller autoCaller(this);
|
---|
4324 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4325 |
|
---|
4326 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4327 |
|
---|
4328 | LogFlowThisFunc(("locationFull.before='%s'\n", m->strLocationFull.c_str()));
|
---|
4329 |
|
---|
4330 | const char *pcszMediumPath = m->strLocationFull.c_str();
|
---|
4331 |
|
---|
4332 | if (RTPathStartsWith(pcszMediumPath, strOldPath.c_str()))
|
---|
4333 | {
|
---|
4334 | Utf8Str newPath(strNewPath);
|
---|
4335 | newPath.append(pcszMediumPath + strOldPath.length());
|
---|
4336 | unconst(m->strLocationFull) = newPath;
|
---|
4337 |
|
---|
4338 | LogFlowThisFunc(("locationFull.after='%s'\n", m->strLocationFull.c_str()));
|
---|
4339 | // we changed something
|
---|
4340 | return S_OK;
|
---|
4341 | }
|
---|
4342 |
|
---|
4343 | // no change was necessary, signal error which the caller needs to interpret
|
---|
4344 | return VBOX_E_FILE_ERROR;
|
---|
4345 | }
|
---|
4346 |
|
---|
4347 | /**
|
---|
4348 | * Returns the base medium of the media chain this medium is part of.
|
---|
4349 | *
|
---|
4350 | * The base medium is found by walking up the parent-child relationship axis.
|
---|
4351 | * If the medium doesn't have a parent (i.e. it's a base medium), it
|
---|
4352 | * returns itself in response to this method.
|
---|
4353 | *
|
---|
4354 | * @param aLevel Where to store the number of ancestors of this medium
|
---|
4355 | * (zero for the base), may be @c NULL.
|
---|
4356 | *
|
---|
4357 | * @note Locks medium tree for reading.
|
---|
4358 | */
|
---|
4359 | ComObjPtr<Medium> Medium::i_getBase(uint32_t *aLevel /*= NULL*/)
|
---|
4360 | {
|
---|
4361 | ComObjPtr<Medium> pBase;
|
---|
4362 |
|
---|
4363 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
4364 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
4365 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
4366 | if (!pVirtualBox)
|
---|
4367 | return pBase;
|
---|
4368 |
|
---|
4369 | /* we access m->pParent */
|
---|
4370 | AutoReadLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4371 |
|
---|
4372 | AutoCaller autoCaller(this);
|
---|
4373 | AssertReturn(autoCaller.isOk(), pBase);
|
---|
4374 |
|
---|
4375 | pBase = this;
|
---|
4376 | uint32_t level = 0;
|
---|
4377 |
|
---|
4378 | if (m->pParent)
|
---|
4379 | {
|
---|
4380 | for (;;)
|
---|
4381 | {
|
---|
4382 | AutoCaller baseCaller(pBase);
|
---|
4383 | AssertReturn(baseCaller.isOk(), pBase);
|
---|
4384 |
|
---|
4385 | if (pBase->m->pParent.isNull())
|
---|
4386 | break;
|
---|
4387 |
|
---|
4388 | pBase = pBase->m->pParent;
|
---|
4389 | ++level;
|
---|
4390 | }
|
---|
4391 | }
|
---|
4392 |
|
---|
4393 | if (aLevel != NULL)
|
---|
4394 | *aLevel = level;
|
---|
4395 |
|
---|
4396 | return pBase;
|
---|
4397 | }
|
---|
4398 |
|
---|
4399 | /**
|
---|
4400 | * Returns the depth of this medium in the media chain.
|
---|
4401 | *
|
---|
4402 | * @note Locks medium tree for reading.
|
---|
4403 | */
|
---|
4404 | uint32_t Medium::i_getDepth()
|
---|
4405 | {
|
---|
4406 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
4407 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
4408 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
4409 | if (!pVirtualBox)
|
---|
4410 | return 1;
|
---|
4411 |
|
---|
4412 | /* we access m->pParent */
|
---|
4413 | AutoReadLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4414 |
|
---|
4415 | uint32_t cDepth = 0;
|
---|
4416 | ComObjPtr<Medium> pMedium(this);
|
---|
4417 | while (!pMedium.isNull())
|
---|
4418 | {
|
---|
4419 | AutoCaller autoCaller(this);
|
---|
4420 | AssertReturn(autoCaller.isOk(), cDepth + 1);
|
---|
4421 |
|
---|
4422 | pMedium = pMedium->m->pParent;
|
---|
4423 | cDepth++;
|
---|
4424 | }
|
---|
4425 |
|
---|
4426 | return cDepth;
|
---|
4427 | }
|
---|
4428 |
|
---|
4429 | /**
|
---|
4430 | * Returns @c true if this medium cannot be modified because it has
|
---|
4431 | * dependents (children) or is part of the snapshot. Related to the medium
|
---|
4432 | * type and posterity, not to the current media state.
|
---|
4433 | *
|
---|
4434 | * @note Locks this object and medium tree for reading.
|
---|
4435 | */
|
---|
4436 | bool Medium::i_isReadOnly()
|
---|
4437 | {
|
---|
4438 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
4439 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
4440 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
4441 | if (!pVirtualBox)
|
---|
4442 | return false;
|
---|
4443 |
|
---|
4444 | /* we access children */
|
---|
4445 | AutoReadLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4446 |
|
---|
4447 | AutoCaller autoCaller(this);
|
---|
4448 | AssertComRCReturn(autoCaller.rc(), false);
|
---|
4449 |
|
---|
4450 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4451 |
|
---|
4452 | switch (m->type)
|
---|
4453 | {
|
---|
4454 | case MediumType_Normal:
|
---|
4455 | {
|
---|
4456 | if (i_getChildren().size() != 0)
|
---|
4457 | return true;
|
---|
4458 |
|
---|
4459 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
4460 | it != m->backRefs.end(); ++it)
|
---|
4461 | if (it->llSnapshotIds.size() != 0)
|
---|
4462 | return true;
|
---|
4463 |
|
---|
4464 | if (m->variant & MediumVariant_VmdkStreamOptimized)
|
---|
4465 | return true;
|
---|
4466 |
|
---|
4467 | return false;
|
---|
4468 | }
|
---|
4469 | case MediumType_Immutable:
|
---|
4470 | case MediumType_MultiAttach:
|
---|
4471 | return true;
|
---|
4472 | case MediumType_Writethrough:
|
---|
4473 | case MediumType_Shareable:
|
---|
4474 | case MediumType_Readonly: /* explicit readonly media has no diffs */
|
---|
4475 | return false;
|
---|
4476 | default:
|
---|
4477 | break;
|
---|
4478 | }
|
---|
4479 |
|
---|
4480 | AssertFailedReturn(false);
|
---|
4481 | }
|
---|
4482 |
|
---|
4483 | /**
|
---|
4484 | * Internal method to return the medium's size. Must have caller + locking!
|
---|
4485 | * @return
|
---|
4486 | */
|
---|
4487 | void Medium::i_updateId(const Guid &id)
|
---|
4488 | {
|
---|
4489 | unconst(m->id) = id;
|
---|
4490 | }
|
---|
4491 |
|
---|
4492 | /**
|
---|
4493 | * Saves the settings of one medium.
|
---|
4494 | *
|
---|
4495 | * @note Caller MUST take care of the medium tree lock and caller.
|
---|
4496 | *
|
---|
4497 | * @param data Settings struct to be updated.
|
---|
4498 | * @param strHardDiskFolder Folder for which paths should be relative.
|
---|
4499 | */
|
---|
4500 | void Medium::i_saveSettingsOne(settings::Medium &data, const Utf8Str &strHardDiskFolder)
|
---|
4501 | {
|
---|
4502 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4503 |
|
---|
4504 | data.uuid = m->id;
|
---|
4505 |
|
---|
4506 | // make path relative if needed
|
---|
4507 | if ( !strHardDiskFolder.isEmpty()
|
---|
4508 | && RTPathStartsWith(m->strLocationFull.c_str(), strHardDiskFolder.c_str())
|
---|
4509 | )
|
---|
4510 | data.strLocation = m->strLocationFull.substr(strHardDiskFolder.length() + 1);
|
---|
4511 | else
|
---|
4512 | data.strLocation = m->strLocationFull;
|
---|
4513 | data.strFormat = m->strFormat;
|
---|
4514 |
|
---|
4515 | /* optional, only for diffs, default is false */
|
---|
4516 | if (m->pParent)
|
---|
4517 | data.fAutoReset = m->autoReset;
|
---|
4518 | else
|
---|
4519 | data.fAutoReset = false;
|
---|
4520 |
|
---|
4521 | /* optional */
|
---|
4522 | data.strDescription = m->strDescription;
|
---|
4523 |
|
---|
4524 | /* optional properties */
|
---|
4525 | data.properties.clear();
|
---|
4526 |
|
---|
4527 | /* handle iSCSI initiator secrets transparently */
|
---|
4528 | bool fHaveInitiatorSecretEncrypted = false;
|
---|
4529 | Utf8Str strCiphertext;
|
---|
4530 | settings::StringsMap::const_iterator itPln = m->mapProperties.find("InitiatorSecret");
|
---|
4531 | if ( itPln != m->mapProperties.end()
|
---|
4532 | && !itPln->second.isEmpty())
|
---|
4533 | {
|
---|
4534 | /* Encrypt the plain secret. If that does not work (i.e. no or wrong settings key
|
---|
4535 | * specified), just use the encrypted secret (if there is any). */
|
---|
4536 | int rc = m->pVirtualBox->i_encryptSetting(itPln->second, &strCiphertext);
|
---|
4537 | if (RT_SUCCESS(rc))
|
---|
4538 | fHaveInitiatorSecretEncrypted = true;
|
---|
4539 | }
|
---|
4540 | for (settings::StringsMap::const_iterator it = m->mapProperties.begin();
|
---|
4541 | it != m->mapProperties.end();
|
---|
4542 | ++it)
|
---|
4543 | {
|
---|
4544 | /* only save properties that have non-default values */
|
---|
4545 | if (!it->second.isEmpty())
|
---|
4546 | {
|
---|
4547 | const Utf8Str &name = it->first;
|
---|
4548 | const Utf8Str &value = it->second;
|
---|
4549 | /* do NOT store the plain InitiatorSecret */
|
---|
4550 | if ( !fHaveInitiatorSecretEncrypted
|
---|
4551 | || !name.equals("InitiatorSecret"))
|
---|
4552 | data.properties[name] = value;
|
---|
4553 | }
|
---|
4554 | }
|
---|
4555 | if (fHaveInitiatorSecretEncrypted)
|
---|
4556 | data.properties["InitiatorSecretEncrypted"] = strCiphertext;
|
---|
4557 |
|
---|
4558 | /* only for base media */
|
---|
4559 | if (m->pParent.isNull())
|
---|
4560 | data.hdType = m->type;
|
---|
4561 | }
|
---|
4562 |
|
---|
4563 | /**
|
---|
4564 | * Saves medium data by putting it into the provided data structure.
|
---|
4565 | * Recurses over all children to save their settings, too.
|
---|
4566 | *
|
---|
4567 | * @param data Settings struct to be updated.
|
---|
4568 | * @param strHardDiskFolder Folder for which paths should be relative.
|
---|
4569 | *
|
---|
4570 | * @note Locks this object, medium tree and children for reading.
|
---|
4571 | */
|
---|
4572 | HRESULT Medium::i_saveSettings(settings::Medium &data,
|
---|
4573 | const Utf8Str &strHardDiskFolder)
|
---|
4574 | {
|
---|
4575 | /* we access m->pParent */
|
---|
4576 | AutoReadLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4577 |
|
---|
4578 | AutoCaller autoCaller(this);
|
---|
4579 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4580 |
|
---|
4581 | i_saveSettingsOne(data, strHardDiskFolder);
|
---|
4582 |
|
---|
4583 | /* save all children */
|
---|
4584 | settings::MediaList &llSettingsChildren = data.llChildren;
|
---|
4585 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
4586 | it != i_getChildren().end();
|
---|
4587 | ++it)
|
---|
4588 | {
|
---|
4589 | // Use the element straight in the list to reduce both unnecessary
|
---|
4590 | // deep copying (when unwinding the recursion the entire medium
|
---|
4591 | // settings sub-tree is copied) and the stack footprint (the settings
|
---|
4592 | // need almost 1K, and there can be VMs with long image chains.
|
---|
4593 | llSettingsChildren.push_back(settings::Medium::Empty);
|
---|
4594 | HRESULT rc = (*it)->i_saveSettings(llSettingsChildren.back(), strHardDiskFolder);
|
---|
4595 | if (FAILED(rc))
|
---|
4596 | {
|
---|
4597 | llSettingsChildren.pop_back();
|
---|
4598 | return rc;
|
---|
4599 | }
|
---|
4600 | }
|
---|
4601 |
|
---|
4602 | return S_OK;
|
---|
4603 | }
|
---|
4604 |
|
---|
4605 | /**
|
---|
4606 | * Constructs a medium lock list for this medium. The lock is not taken.
|
---|
4607 | *
|
---|
4608 | * @note Caller MUST NOT hold the media tree or medium lock.
|
---|
4609 | *
|
---|
4610 | * @param fFailIfInaccessible If true, this fails with an error if a medium is inaccessible. If false,
|
---|
4611 | * inaccessible media are silently skipped and not locked (i.e. their state remains "Inaccessible");
|
---|
4612 | * this is necessary for a VM's removable media VM startup for which we do not want to fail.
|
---|
4613 | * @param pToLockWrite If not NULL, associate a write lock with this medium object.
|
---|
4614 | * @param fMediumLockWriteAll Whether to associate a write lock to all other media too.
|
---|
4615 | * @param pToBeParent Medium which will become the parent of this medium.
|
---|
4616 | * @param mediumLockList Where to store the resulting list.
|
---|
4617 | */
|
---|
4618 | HRESULT Medium::i_createMediumLockList(bool fFailIfInaccessible,
|
---|
4619 | Medium *pToLockWrite,
|
---|
4620 | bool fMediumLockWriteAll,
|
---|
4621 | Medium *pToBeParent,
|
---|
4622 | MediumLockList &mediumLockList)
|
---|
4623 | {
|
---|
4624 | /** @todo r=klaus this needs to be reworked, as the code below uses
|
---|
4625 | * i_getParent without holding the tree lock, and changing this is
|
---|
4626 | * a significant amount of effort. */
|
---|
4627 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
4628 | Assert(!isWriteLockOnCurrentThread());
|
---|
4629 |
|
---|
4630 | AutoCaller autoCaller(this);
|
---|
4631 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4632 |
|
---|
4633 | HRESULT rc = S_OK;
|
---|
4634 |
|
---|
4635 | /* paranoid sanity checking if the medium has a to-be parent medium */
|
---|
4636 | if (pToBeParent)
|
---|
4637 | {
|
---|
4638 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4639 | ComAssertRet(i_getParent().isNull(), E_FAIL);
|
---|
4640 | ComAssertRet(i_getChildren().size() == 0, E_FAIL);
|
---|
4641 | }
|
---|
4642 |
|
---|
4643 | ErrorInfoKeeper eik;
|
---|
4644 | MultiResult mrc(S_OK);
|
---|
4645 |
|
---|
4646 | ComObjPtr<Medium> pMedium = this;
|
---|
4647 | while (!pMedium.isNull())
|
---|
4648 | {
|
---|
4649 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
4650 |
|
---|
4651 | /* Accessibility check must be first, otherwise locking interferes
|
---|
4652 | * with getting the medium state. Lock lists are not created for
|
---|
4653 | * fun, and thus getting the medium status is no luxury. */
|
---|
4654 | MediumState_T mediumState = pMedium->i_getState();
|
---|
4655 | if (mediumState == MediumState_Inaccessible)
|
---|
4656 | {
|
---|
4657 | alock.release();
|
---|
4658 | rc = pMedium->i_queryInfo(false /* fSetImageId */, false /* fSetParentId */,
|
---|
4659 | autoCaller);
|
---|
4660 | alock.acquire();
|
---|
4661 | if (FAILED(rc)) return rc;
|
---|
4662 |
|
---|
4663 | mediumState = pMedium->i_getState();
|
---|
4664 | if (mediumState == MediumState_Inaccessible)
|
---|
4665 | {
|
---|
4666 | // ignore inaccessible ISO media and silently return S_OK,
|
---|
4667 | // otherwise VM startup (esp. restore) may fail without good reason
|
---|
4668 | if (!fFailIfInaccessible)
|
---|
4669 | return S_OK;
|
---|
4670 |
|
---|
4671 | // otherwise report an error
|
---|
4672 | Bstr error;
|
---|
4673 | rc = pMedium->COMGETTER(LastAccessError)(error.asOutParam());
|
---|
4674 | if (FAILED(rc)) return rc;
|
---|
4675 |
|
---|
4676 | /* collect multiple errors */
|
---|
4677 | eik.restore();
|
---|
4678 | Assert(!error.isEmpty());
|
---|
4679 | mrc = setError(E_FAIL,
|
---|
4680 | "%ls",
|
---|
4681 | error.raw());
|
---|
4682 | // error message will be something like
|
---|
4683 | // "Could not open the medium ... VD: error VERR_FILE_NOT_FOUND opening image file ... (VERR_FILE_NOT_FOUND).
|
---|
4684 | eik.fetch();
|
---|
4685 | }
|
---|
4686 | }
|
---|
4687 |
|
---|
4688 | if (pMedium == pToLockWrite)
|
---|
4689 | mediumLockList.Prepend(pMedium, true);
|
---|
4690 | else
|
---|
4691 | mediumLockList.Prepend(pMedium, fMediumLockWriteAll);
|
---|
4692 |
|
---|
4693 | pMedium = pMedium->i_getParent();
|
---|
4694 | if (pMedium.isNull() && pToBeParent)
|
---|
4695 | {
|
---|
4696 | pMedium = pToBeParent;
|
---|
4697 | pToBeParent = NULL;
|
---|
4698 | }
|
---|
4699 | }
|
---|
4700 |
|
---|
4701 | return mrc;
|
---|
4702 | }
|
---|
4703 |
|
---|
4704 | /**
|
---|
4705 | * Creates a new differencing storage unit using the format of the given target
|
---|
4706 | * medium and the location. Note that @c aTarget must be NotCreated.
|
---|
4707 | *
|
---|
4708 | * The @a aMediumLockList parameter contains the associated medium lock list,
|
---|
4709 | * which must be in locked state. If @a aWait is @c true then the caller is
|
---|
4710 | * responsible for unlocking.
|
---|
4711 | *
|
---|
4712 | * If @a aProgress is not NULL but the object it points to is @c null then a
|
---|
4713 | * new progress object will be created and assigned to @a *aProgress on
|
---|
4714 | * success, otherwise the existing progress object is used. If @a aProgress is
|
---|
4715 | * NULL, then no progress object is created/used at all.
|
---|
4716 | *
|
---|
4717 | * When @a aWait is @c false, this method will create a thread to perform the
|
---|
4718 | * create operation asynchronously and will return immediately. Otherwise, it
|
---|
4719 | * will perform the operation on the calling thread and will not return to the
|
---|
4720 | * caller until the operation is completed. Note that @a aProgress cannot be
|
---|
4721 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
4722 | *
|
---|
4723 | * @param aTarget Target medium.
|
---|
4724 | * @param aVariant Precise medium variant to create.
|
---|
4725 | * @param aMediumLockList List of media which should be locked.
|
---|
4726 | * @param aProgress Where to find/store a Progress object to track
|
---|
4727 | * operation completion.
|
---|
4728 | * @param aWait @c true if this method should block instead of
|
---|
4729 | * creating an asynchronous thread.
|
---|
4730 | *
|
---|
4731 | * @note Locks this object and @a aTarget for writing.
|
---|
4732 | */
|
---|
4733 | HRESULT Medium::i_createDiffStorage(ComObjPtr<Medium> &aTarget,
|
---|
4734 | MediumVariant_T aVariant,
|
---|
4735 | MediumLockList *aMediumLockList,
|
---|
4736 | ComObjPtr<Progress> *aProgress,
|
---|
4737 | bool aWait)
|
---|
4738 | {
|
---|
4739 | AssertReturn(!aTarget.isNull(), E_FAIL);
|
---|
4740 | AssertReturn(aMediumLockList, E_FAIL);
|
---|
4741 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
4742 |
|
---|
4743 | AutoCaller autoCaller(this);
|
---|
4744 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4745 |
|
---|
4746 | AutoCaller targetCaller(aTarget);
|
---|
4747 | if (FAILED(targetCaller.rc())) return targetCaller.rc();
|
---|
4748 |
|
---|
4749 | HRESULT rc = S_OK;
|
---|
4750 | ComObjPtr<Progress> pProgress;
|
---|
4751 | Medium::Task *pTask = NULL;
|
---|
4752 |
|
---|
4753 | try
|
---|
4754 | {
|
---|
4755 | AutoMultiWriteLock2 alock(this, aTarget COMMA_LOCKVAL_SRC_POS);
|
---|
4756 |
|
---|
4757 | ComAssertThrow( m->type != MediumType_Writethrough
|
---|
4758 | && m->type != MediumType_Shareable
|
---|
4759 | && m->type != MediumType_Readonly, E_FAIL);
|
---|
4760 | ComAssertThrow(m->state == MediumState_LockedRead, E_FAIL);
|
---|
4761 |
|
---|
4762 | if (aTarget->m->state != MediumState_NotCreated)
|
---|
4763 | throw aTarget->i_setStateError();
|
---|
4764 |
|
---|
4765 | /* Check that the medium is not attached to the current state of
|
---|
4766 | * any VM referring to it. */
|
---|
4767 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
4768 | it != m->backRefs.end();
|
---|
4769 | ++it)
|
---|
4770 | {
|
---|
4771 | if (it->fInCurState)
|
---|
4772 | {
|
---|
4773 | /* Note: when a VM snapshot is being taken, all normal media
|
---|
4774 | * attached to the VM in the current state will be, as an
|
---|
4775 | * exception, also associated with the snapshot which is about
|
---|
4776 | * to create (see SnapshotMachine::init()) before deassociating
|
---|
4777 | * them from the current state (which takes place only on
|
---|
4778 | * success in Machine::fixupHardDisks()), so that the size of
|
---|
4779 | * snapshotIds will be 1 in this case. The extra condition is
|
---|
4780 | * used to filter out this legal situation. */
|
---|
4781 | if (it->llSnapshotIds.size() == 0)
|
---|
4782 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4783 | 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"),
|
---|
4784 | m->strLocationFull.c_str(), it->machineId.raw());
|
---|
4785 |
|
---|
4786 | Assert(it->llSnapshotIds.size() == 1);
|
---|
4787 | }
|
---|
4788 | }
|
---|
4789 |
|
---|
4790 | if (aProgress != NULL)
|
---|
4791 | {
|
---|
4792 | /* use the existing progress object... */
|
---|
4793 | pProgress = *aProgress;
|
---|
4794 |
|
---|
4795 | /* ...but create a new one if it is null */
|
---|
4796 | if (pProgress.isNull())
|
---|
4797 | {
|
---|
4798 | pProgress.createObject();
|
---|
4799 | rc = pProgress->init(m->pVirtualBox,
|
---|
4800 | static_cast<IMedium*>(this),
|
---|
4801 | BstrFmt(tr("Creating differencing medium storage unit '%s'"),
|
---|
4802 | aTarget->m->strLocationFull.c_str()).raw(),
|
---|
4803 | TRUE /* aCancelable */);
|
---|
4804 | if (FAILED(rc))
|
---|
4805 | throw rc;
|
---|
4806 | }
|
---|
4807 | }
|
---|
4808 |
|
---|
4809 | /* setup task object to carry out the operation sync/async */
|
---|
4810 | pTask = new Medium::CreateDiffTask(this, pProgress, aTarget, aVariant,
|
---|
4811 | aMediumLockList,
|
---|
4812 | aWait /* fKeepMediumLockList */);
|
---|
4813 | rc = pTask->rc();
|
---|
4814 | AssertComRC(rc);
|
---|
4815 | if (FAILED(rc))
|
---|
4816 | throw rc;
|
---|
4817 |
|
---|
4818 | /* register a task (it will deregister itself when done) */
|
---|
4819 | ++m->numCreateDiffTasks;
|
---|
4820 | Assert(m->numCreateDiffTasks != 0); /* overflow? */
|
---|
4821 |
|
---|
4822 | aTarget->m->state = MediumState_Creating;
|
---|
4823 | }
|
---|
4824 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4825 |
|
---|
4826 | if (SUCCEEDED(rc))
|
---|
4827 | {
|
---|
4828 | if (aWait)
|
---|
4829 | {
|
---|
4830 | rc = pTask->runNow();
|
---|
4831 |
|
---|
4832 | delete pTask;
|
---|
4833 | }
|
---|
4834 | else
|
---|
4835 | rc = pTask->createThread();
|
---|
4836 |
|
---|
4837 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
4838 | *aProgress = pProgress;
|
---|
4839 | }
|
---|
4840 | else if (pTask != NULL)
|
---|
4841 | delete pTask;
|
---|
4842 |
|
---|
4843 | return rc;
|
---|
4844 | }
|
---|
4845 |
|
---|
4846 | /**
|
---|
4847 | * Returns a preferred format for differencing media.
|
---|
4848 | */
|
---|
4849 | Utf8Str Medium::i_getPreferredDiffFormat()
|
---|
4850 | {
|
---|
4851 | AutoCaller autoCaller(this);
|
---|
4852 | AssertComRCReturn(autoCaller.rc(), Utf8Str::Empty);
|
---|
4853 |
|
---|
4854 | /* check that our own format supports diffs */
|
---|
4855 | if (!(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_Differencing))
|
---|
4856 | {
|
---|
4857 | /* use the default format if not */
|
---|
4858 | Utf8Str tmp;
|
---|
4859 | m->pVirtualBox->i_getDefaultHardDiskFormat(tmp);
|
---|
4860 | return tmp;
|
---|
4861 | }
|
---|
4862 |
|
---|
4863 | /* m->strFormat is const, no need to lock */
|
---|
4864 | return m->strFormat;
|
---|
4865 | }
|
---|
4866 |
|
---|
4867 | /**
|
---|
4868 | * Returns a preferred variant for differencing media.
|
---|
4869 | */
|
---|
4870 | MediumVariant_T Medium::i_getPreferredDiffVariant()
|
---|
4871 | {
|
---|
4872 | AutoCaller autoCaller(this);
|
---|
4873 | AssertComRCReturn(autoCaller.rc(), MediumVariant_Standard);
|
---|
4874 |
|
---|
4875 | /* check that our own format supports diffs */
|
---|
4876 | if (!(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_Differencing))
|
---|
4877 | return MediumVariant_Standard;
|
---|
4878 |
|
---|
4879 | /* m->variant is const, no need to lock */
|
---|
4880 | ULONG mediumVariantFlags = (ULONG)m->variant;
|
---|
4881 | mediumVariantFlags &= ~(MediumVariant_Fixed | MediumVariant_VmdkStreamOptimized);
|
---|
4882 | mediumVariantFlags |= MediumVariant_Diff;
|
---|
4883 | return (MediumVariant_T)mediumVariantFlags;
|
---|
4884 | }
|
---|
4885 |
|
---|
4886 | /**
|
---|
4887 | * Implementation for the public Medium::Close() with the exception of calling
|
---|
4888 | * VirtualBox::saveRegistries(), in case someone wants to call this for several
|
---|
4889 | * media.
|
---|
4890 | *
|
---|
4891 | * After this returns with success, uninit() has been called on the medium, and
|
---|
4892 | * the object is no longer usable ("not ready" state).
|
---|
4893 | *
|
---|
4894 | * @param autoCaller AutoCaller instance which must have been created on the caller's
|
---|
4895 | * stack for this medium. This gets released hereupon
|
---|
4896 | * which the Medium instance gets uninitialized.
|
---|
4897 | * @return
|
---|
4898 | */
|
---|
4899 | HRESULT Medium::i_close(AutoCaller &autoCaller)
|
---|
4900 | {
|
---|
4901 | // must temporarily drop the caller, need the tree lock first
|
---|
4902 | autoCaller.release();
|
---|
4903 |
|
---|
4904 | // we're accessing parent/child and backrefs, so lock the tree first, then ourselves
|
---|
4905 | AutoMultiWriteLock2 multilock(&m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
4906 | this->lockHandle()
|
---|
4907 | COMMA_LOCKVAL_SRC_POS);
|
---|
4908 |
|
---|
4909 | autoCaller.add();
|
---|
4910 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4911 |
|
---|
4912 | LogFlowFunc(("ENTER for %s\n", i_getLocationFull().c_str()));
|
---|
4913 |
|
---|
4914 | bool wasCreated = true;
|
---|
4915 |
|
---|
4916 | switch (m->state)
|
---|
4917 | {
|
---|
4918 | case MediumState_NotCreated:
|
---|
4919 | wasCreated = false;
|
---|
4920 | break;
|
---|
4921 | case MediumState_Created:
|
---|
4922 | case MediumState_Inaccessible:
|
---|
4923 | break;
|
---|
4924 | default:
|
---|
4925 | return i_setStateError();
|
---|
4926 | }
|
---|
4927 |
|
---|
4928 | if (m->backRefs.size() != 0)
|
---|
4929 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4930 | tr("Medium '%s' cannot be closed because it is still attached to %d virtual machines"),
|
---|
4931 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
4932 |
|
---|
4933 | // perform extra media-dependent close checks
|
---|
4934 | HRESULT rc = i_canClose();
|
---|
4935 | if (FAILED(rc)) return rc;
|
---|
4936 |
|
---|
4937 | m->fClosing = true;
|
---|
4938 |
|
---|
4939 | if (wasCreated)
|
---|
4940 | {
|
---|
4941 | // remove from the list of known media before performing actual
|
---|
4942 | // uninitialization (to keep the media registry consistent on
|
---|
4943 | // failure to do so)
|
---|
4944 | rc = i_unregisterWithVirtualBox();
|
---|
4945 | if (FAILED(rc)) return rc;
|
---|
4946 |
|
---|
4947 | multilock.release();
|
---|
4948 | // Release the AutoCaller now, as otherwise uninit() will simply hang.
|
---|
4949 | // Needs to be done before mark the registries as modified and saving
|
---|
4950 | // the registry, as otherwise there may be a deadlock with someone else
|
---|
4951 | // closing this object while we're in i_saveModifiedRegistries(), which
|
---|
4952 | // needs the media tree lock, which the other thread holds until after
|
---|
4953 | // uninit() below.
|
---|
4954 | autoCaller.release();
|
---|
4955 | i_markRegistriesModified();
|
---|
4956 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
4957 | }
|
---|
4958 | else
|
---|
4959 | {
|
---|
4960 | multilock.release();
|
---|
4961 | // release the AutoCaller, as otherwise uninit() will simply hang
|
---|
4962 | autoCaller.release();
|
---|
4963 | }
|
---|
4964 |
|
---|
4965 | // Keep the locks held until after uninit, as otherwise the consistency
|
---|
4966 | // of the medium tree cannot be guaranteed.
|
---|
4967 | uninit();
|
---|
4968 |
|
---|
4969 | LogFlowFuncLeave();
|
---|
4970 |
|
---|
4971 | return rc;
|
---|
4972 | }
|
---|
4973 |
|
---|
4974 | /**
|
---|
4975 | * Deletes the medium storage unit.
|
---|
4976 | *
|
---|
4977 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
4978 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
4979 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
4980 | * progress object is created/used at all.
|
---|
4981 | *
|
---|
4982 | * When @a aWait is @c false, this method will create a thread to perform the
|
---|
4983 | * delete operation asynchronously and will return immediately. Otherwise, it
|
---|
4984 | * will perform the operation on the calling thread and will not return to the
|
---|
4985 | * caller until the operation is completed. Note that @a aProgress cannot be
|
---|
4986 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
4987 | *
|
---|
4988 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
4989 | * completion.
|
---|
4990 | * @param aWait @c true if this method should block instead of creating
|
---|
4991 | * an asynchronous thread.
|
---|
4992 | *
|
---|
4993 | * @note Locks mVirtualBox and this object for writing. Locks medium tree for
|
---|
4994 | * writing.
|
---|
4995 | */
|
---|
4996 | HRESULT Medium::i_deleteStorage(ComObjPtr<Progress> *aProgress,
|
---|
4997 | bool aWait)
|
---|
4998 | {
|
---|
4999 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
5000 | * to lock order violations, it probably causes lock order issues related
|
---|
5001 | * to the AutoCaller usage. */
|
---|
5002 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
5003 |
|
---|
5004 | AutoCaller autoCaller(this);
|
---|
5005 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
5006 |
|
---|
5007 | HRESULT rc = S_OK;
|
---|
5008 | ComObjPtr<Progress> pProgress;
|
---|
5009 | Medium::Task *pTask = NULL;
|
---|
5010 |
|
---|
5011 | try
|
---|
5012 | {
|
---|
5013 | /* we're accessing the media tree, and canClose() needs it too */
|
---|
5014 | AutoMultiWriteLock2 multilock(&m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
5015 | this->lockHandle()
|
---|
5016 | COMMA_LOCKVAL_SRC_POS);
|
---|
5017 | LogFlowThisFunc(("aWait=%RTbool locationFull=%s\n", aWait, i_getLocationFull().c_str() ));
|
---|
5018 |
|
---|
5019 | if ( !(m->formatObj->i_getCapabilities() & ( MediumFormatCapabilities_CreateDynamic
|
---|
5020 | | MediumFormatCapabilities_CreateFixed)))
|
---|
5021 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
5022 | tr("Medium format '%s' does not support storage deletion"),
|
---|
5023 | m->strFormat.c_str());
|
---|
5024 |
|
---|
5025 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
5026 | /** @todo r=klaus would be great if this could be moved to the async
|
---|
5027 | * part of the operation as it can take quite a while */
|
---|
5028 | if (m->queryInfoRunning)
|
---|
5029 | {
|
---|
5030 | while (m->queryInfoRunning)
|
---|
5031 | {
|
---|
5032 | multilock.release();
|
---|
5033 | /* Must not hold the media tree lock or the object lock, as
|
---|
5034 | * Medium::i_queryInfo needs this lock and thus we would run
|
---|
5035 | * into a deadlock here. */
|
---|
5036 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
5037 | Assert(!isWriteLockOnCurrentThread());
|
---|
5038 | {
|
---|
5039 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
5040 | }
|
---|
5041 | multilock.acquire();
|
---|
5042 | }
|
---|
5043 | }
|
---|
5044 |
|
---|
5045 | /* Note that we are fine with Inaccessible state too: a) for symmetry
|
---|
5046 | * with create calls and b) because it doesn't really harm to try, if
|
---|
5047 | * it is really inaccessible, the delete operation will fail anyway.
|
---|
5048 | * Accepting Inaccessible state is especially important because all
|
---|
5049 | * registered media are initially Inaccessible upon VBoxSVC startup
|
---|
5050 | * until COMGETTER(RefreshState) is called. Accept Deleting state
|
---|
5051 | * because some callers need to put the medium in this state early
|
---|
5052 | * to prevent races. */
|
---|
5053 | switch (m->state)
|
---|
5054 | {
|
---|
5055 | case MediumState_Created:
|
---|
5056 | case MediumState_Deleting:
|
---|
5057 | case MediumState_Inaccessible:
|
---|
5058 | break;
|
---|
5059 | default:
|
---|
5060 | throw i_setStateError();
|
---|
5061 | }
|
---|
5062 |
|
---|
5063 | if (m->backRefs.size() != 0)
|
---|
5064 | {
|
---|
5065 | Utf8Str strMachines;
|
---|
5066 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
5067 | it != m->backRefs.end();
|
---|
5068 | ++it)
|
---|
5069 | {
|
---|
5070 | const BackRef &b = *it;
|
---|
5071 | if (strMachines.length())
|
---|
5072 | strMachines.append(", ");
|
---|
5073 | strMachines.append(b.machineId.toString().c_str());
|
---|
5074 | }
|
---|
5075 | #ifdef DEBUG
|
---|
5076 | i_dumpBackRefs();
|
---|
5077 | #endif
|
---|
5078 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5079 | tr("Cannot delete storage: medium '%s' is still attached to the following %d virtual machine(s): %s"),
|
---|
5080 | m->strLocationFull.c_str(),
|
---|
5081 | m->backRefs.size(),
|
---|
5082 | strMachines.c_str());
|
---|
5083 | }
|
---|
5084 |
|
---|
5085 | rc = i_canClose();
|
---|
5086 | if (FAILED(rc))
|
---|
5087 | throw rc;
|
---|
5088 |
|
---|
5089 | /* go to Deleting state, so that the medium is not actually locked */
|
---|
5090 | if (m->state != MediumState_Deleting)
|
---|
5091 | {
|
---|
5092 | rc = i_markForDeletion();
|
---|
5093 | if (FAILED(rc))
|
---|
5094 | throw rc;
|
---|
5095 | }
|
---|
5096 |
|
---|
5097 | /* Build the medium lock list. */
|
---|
5098 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
5099 | multilock.release();
|
---|
5100 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5101 | this /* pToLockWrite */,
|
---|
5102 | false /* fMediumLockWriteAll */,
|
---|
5103 | NULL,
|
---|
5104 | *pMediumLockList);
|
---|
5105 | multilock.acquire();
|
---|
5106 | if (FAILED(rc))
|
---|
5107 | {
|
---|
5108 | delete pMediumLockList;
|
---|
5109 | throw rc;
|
---|
5110 | }
|
---|
5111 |
|
---|
5112 | multilock.release();
|
---|
5113 | rc = pMediumLockList->Lock();
|
---|
5114 | multilock.acquire();
|
---|
5115 | if (FAILED(rc))
|
---|
5116 | {
|
---|
5117 | delete pMediumLockList;
|
---|
5118 | throw setError(rc,
|
---|
5119 | tr("Failed to lock media when deleting '%s'"),
|
---|
5120 | i_getLocationFull().c_str());
|
---|
5121 | }
|
---|
5122 |
|
---|
5123 | /* try to remove from the list of known media before performing
|
---|
5124 | * actual deletion (we favor the consistency of the media registry
|
---|
5125 | * which would have been broken if unregisterWithVirtualBox() failed
|
---|
5126 | * after we successfully deleted the storage) */
|
---|
5127 | rc = i_unregisterWithVirtualBox();
|
---|
5128 | if (FAILED(rc))
|
---|
5129 | throw rc;
|
---|
5130 | // no longer need lock
|
---|
5131 | multilock.release();
|
---|
5132 | i_markRegistriesModified();
|
---|
5133 |
|
---|
5134 | if (aProgress != NULL)
|
---|
5135 | {
|
---|
5136 | /* use the existing progress object... */
|
---|
5137 | pProgress = *aProgress;
|
---|
5138 |
|
---|
5139 | /* ...but create a new one if it is null */
|
---|
5140 | if (pProgress.isNull())
|
---|
5141 | {
|
---|
5142 | pProgress.createObject();
|
---|
5143 | rc = pProgress->init(m->pVirtualBox,
|
---|
5144 | static_cast<IMedium*>(this),
|
---|
5145 | BstrFmt(tr("Deleting medium storage unit '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
5146 | FALSE /* aCancelable */);
|
---|
5147 | if (FAILED(rc))
|
---|
5148 | throw rc;
|
---|
5149 | }
|
---|
5150 | }
|
---|
5151 |
|
---|
5152 | /* setup task object to carry out the operation sync/async */
|
---|
5153 | pTask = new Medium::DeleteTask(this, pProgress, pMediumLockList);
|
---|
5154 | rc = pTask->rc();
|
---|
5155 | AssertComRC(rc);
|
---|
5156 | if (FAILED(rc))
|
---|
5157 | throw rc;
|
---|
5158 | }
|
---|
5159 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5160 |
|
---|
5161 | if (SUCCEEDED(rc))
|
---|
5162 | {
|
---|
5163 | if (aWait)
|
---|
5164 | {
|
---|
5165 | rc = pTask->runNow();
|
---|
5166 |
|
---|
5167 | delete pTask;
|
---|
5168 | }
|
---|
5169 | else
|
---|
5170 | rc = pTask->createThread();
|
---|
5171 |
|
---|
5172 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
5173 | *aProgress = pProgress;
|
---|
5174 |
|
---|
5175 | }
|
---|
5176 | else
|
---|
5177 | {
|
---|
5178 | if (pTask)
|
---|
5179 | delete pTask;
|
---|
5180 |
|
---|
5181 | /* Undo deleting state if necessary. */
|
---|
5182 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5183 | /* Make sure that any error signalled by unmarkForDeletion() is not
|
---|
5184 | * ending up in the error list (if the caller uses MultiResult). It
|
---|
5185 | * usually is spurious, as in most cases the medium hasn't been marked
|
---|
5186 | * for deletion when the error was thrown above. */
|
---|
5187 | ErrorInfoKeeper eik;
|
---|
5188 | i_unmarkForDeletion();
|
---|
5189 | }
|
---|
5190 |
|
---|
5191 | return rc;
|
---|
5192 | }
|
---|
5193 |
|
---|
5194 | /**
|
---|
5195 | * Mark a medium for deletion.
|
---|
5196 | *
|
---|
5197 | * @note Caller must hold the write lock on this medium!
|
---|
5198 | */
|
---|
5199 | HRESULT Medium::i_markForDeletion()
|
---|
5200 | {
|
---|
5201 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5202 | switch (m->state)
|
---|
5203 | {
|
---|
5204 | case MediumState_Created:
|
---|
5205 | case MediumState_Inaccessible:
|
---|
5206 | m->preLockState = m->state;
|
---|
5207 | m->state = MediumState_Deleting;
|
---|
5208 | return S_OK;
|
---|
5209 | default:
|
---|
5210 | return i_setStateError();
|
---|
5211 | }
|
---|
5212 | }
|
---|
5213 |
|
---|
5214 | /**
|
---|
5215 | * Removes the "mark for deletion".
|
---|
5216 | *
|
---|
5217 | * @note Caller must hold the write lock on this medium!
|
---|
5218 | */
|
---|
5219 | HRESULT Medium::i_unmarkForDeletion()
|
---|
5220 | {
|
---|
5221 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5222 | switch (m->state)
|
---|
5223 | {
|
---|
5224 | case MediumState_Deleting:
|
---|
5225 | m->state = m->preLockState;
|
---|
5226 | return S_OK;
|
---|
5227 | default:
|
---|
5228 | return i_setStateError();
|
---|
5229 | }
|
---|
5230 | }
|
---|
5231 |
|
---|
5232 | /**
|
---|
5233 | * Mark a medium for deletion which is in locked state.
|
---|
5234 | *
|
---|
5235 | * @note Caller must hold the write lock on this medium!
|
---|
5236 | */
|
---|
5237 | HRESULT Medium::i_markLockedForDeletion()
|
---|
5238 | {
|
---|
5239 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5240 | if ( ( m->state == MediumState_LockedRead
|
---|
5241 | || m->state == MediumState_LockedWrite)
|
---|
5242 | && m->preLockState == MediumState_Created)
|
---|
5243 | {
|
---|
5244 | m->preLockState = MediumState_Deleting;
|
---|
5245 | return S_OK;
|
---|
5246 | }
|
---|
5247 | else
|
---|
5248 | return i_setStateError();
|
---|
5249 | }
|
---|
5250 |
|
---|
5251 | /**
|
---|
5252 | * Removes the "mark for deletion" for a medium in locked state.
|
---|
5253 | *
|
---|
5254 | * @note Caller must hold the write lock on this medium!
|
---|
5255 | */
|
---|
5256 | HRESULT Medium::i_unmarkLockedForDeletion()
|
---|
5257 | {
|
---|
5258 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5259 | if ( ( m->state == MediumState_LockedRead
|
---|
5260 | || m->state == MediumState_LockedWrite)
|
---|
5261 | && m->preLockState == MediumState_Deleting)
|
---|
5262 | {
|
---|
5263 | m->preLockState = MediumState_Created;
|
---|
5264 | return S_OK;
|
---|
5265 | }
|
---|
5266 | else
|
---|
5267 | return i_setStateError();
|
---|
5268 | }
|
---|
5269 |
|
---|
5270 | /**
|
---|
5271 | * Queries the preferred merge direction from this to the other medium, i.e.
|
---|
5272 | * the one which requires the least amount of I/O and therefore time and
|
---|
5273 | * disk consumption.
|
---|
5274 | *
|
---|
5275 | * @returns Status code.
|
---|
5276 | * @retval E_FAIL in case determining the merge direction fails for some reason,
|
---|
5277 | * for example if getting the size of the media fails. There is no
|
---|
5278 | * error set though and the caller is free to continue to find out
|
---|
5279 | * what was going wrong later. Leaves fMergeForward unset.
|
---|
5280 | * @retval VBOX_E_INVALID_OBJECT_STATE if both media are not related to each other
|
---|
5281 | * An error is set.
|
---|
5282 | * @param pOther The other medium to merge with.
|
---|
5283 | * @param fMergeForward Resulting preferred merge direction (out).
|
---|
5284 | */
|
---|
5285 | HRESULT Medium::i_queryPreferredMergeDirection(const ComObjPtr<Medium> &pOther,
|
---|
5286 | bool &fMergeForward)
|
---|
5287 | {
|
---|
5288 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
5289 | * to lock order violations, it probably causes lock order issues related
|
---|
5290 | * to the AutoCaller usage. Likewise the code using this method seems
|
---|
5291 | * problematic. */
|
---|
5292 | AssertReturn(pOther != NULL, E_FAIL);
|
---|
5293 | AssertReturn(pOther != this, E_FAIL);
|
---|
5294 |
|
---|
5295 | AutoCaller autoCaller(this);
|
---|
5296 | AssertComRCReturnRC(autoCaller.rc());
|
---|
5297 |
|
---|
5298 | AutoCaller otherCaller(pOther);
|
---|
5299 | AssertComRCReturnRC(otherCaller.rc());
|
---|
5300 |
|
---|
5301 | HRESULT rc = S_OK;
|
---|
5302 | bool fThisParent = false; /**<< Flag whether this medium is the parent of pOther. */
|
---|
5303 |
|
---|
5304 | try
|
---|
5305 | {
|
---|
5306 | // locking: we need the tree lock first because we access parent pointers
|
---|
5307 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5308 |
|
---|
5309 | /* more sanity checking and figuring out the current merge direction */
|
---|
5310 | ComObjPtr<Medium> pMedium = i_getParent();
|
---|
5311 | while (!pMedium.isNull() && pMedium != pOther)
|
---|
5312 | pMedium = pMedium->i_getParent();
|
---|
5313 | if (pMedium == pOther)
|
---|
5314 | fThisParent = false;
|
---|
5315 | else
|
---|
5316 | {
|
---|
5317 | pMedium = pOther->i_getParent();
|
---|
5318 | while (!pMedium.isNull() && pMedium != this)
|
---|
5319 | pMedium = pMedium->i_getParent();
|
---|
5320 | if (pMedium == this)
|
---|
5321 | fThisParent = true;
|
---|
5322 | else
|
---|
5323 | {
|
---|
5324 | Utf8Str tgtLoc;
|
---|
5325 | {
|
---|
5326 | AutoReadLock alock(pOther COMMA_LOCKVAL_SRC_POS);
|
---|
5327 | tgtLoc = pOther->i_getLocationFull();
|
---|
5328 | }
|
---|
5329 |
|
---|
5330 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5331 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5332 | tr("Media '%s' and '%s' are unrelated"),
|
---|
5333 | m->strLocationFull.c_str(), tgtLoc.c_str());
|
---|
5334 | }
|
---|
5335 | }
|
---|
5336 |
|
---|
5337 | /*
|
---|
5338 | * Figure out the preferred merge direction. The current way is to
|
---|
5339 | * get the current sizes of file based images and select the merge
|
---|
5340 | * direction depending on the size.
|
---|
5341 | *
|
---|
5342 | * Can't use the VD API to get current size here as the media might
|
---|
5343 | * be write locked by a running VM. Resort to RTFileQuerySize().
|
---|
5344 | */
|
---|
5345 | int vrc = VINF_SUCCESS;
|
---|
5346 | uint64_t cbMediumThis = 0;
|
---|
5347 | uint64_t cbMediumOther = 0;
|
---|
5348 |
|
---|
5349 | if (i_isMediumFormatFile() && pOther->i_isMediumFormatFile())
|
---|
5350 | {
|
---|
5351 | vrc = RTFileQuerySize(this->i_getLocationFull().c_str(), &cbMediumThis);
|
---|
5352 | if (RT_SUCCESS(vrc))
|
---|
5353 | {
|
---|
5354 | vrc = RTFileQuerySize(pOther->i_getLocationFull().c_str(),
|
---|
5355 | &cbMediumOther);
|
---|
5356 | }
|
---|
5357 |
|
---|
5358 | if (RT_FAILURE(vrc))
|
---|
5359 | rc = E_FAIL;
|
---|
5360 | else
|
---|
5361 | {
|
---|
5362 | /*
|
---|
5363 | * Check which merge direction might be more optimal.
|
---|
5364 | * This method is not bullet proof of course as there might
|
---|
5365 | * be overlapping blocks in the images so the file size is
|
---|
5366 | * not the best indicator but it is good enough for our purpose
|
---|
5367 | * and everything else is too complicated, especially when the
|
---|
5368 | * media are used by a running VM.
|
---|
5369 | */
|
---|
5370 | bool fMergeIntoThis = cbMediumThis > cbMediumOther;
|
---|
5371 | fMergeForward = fMergeIntoThis != fThisParent;
|
---|
5372 | }
|
---|
5373 | }
|
---|
5374 | }
|
---|
5375 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5376 |
|
---|
5377 | return rc;
|
---|
5378 | }
|
---|
5379 |
|
---|
5380 | /**
|
---|
5381 | * Prepares this (source) medium, target medium and all intermediate media
|
---|
5382 | * for the merge operation.
|
---|
5383 | *
|
---|
5384 | * This method is to be called prior to calling the #mergeTo() to perform
|
---|
5385 | * necessary consistency checks and place involved media to appropriate
|
---|
5386 | * states. If #mergeTo() is not called or fails, the state modifications
|
---|
5387 | * performed by this method must be undone by #i_cancelMergeTo().
|
---|
5388 | *
|
---|
5389 | * See #mergeTo() for more information about merging.
|
---|
5390 | *
|
---|
5391 | * @param pTarget Target medium.
|
---|
5392 | * @param aMachineId Allowed machine attachment. NULL means do not check.
|
---|
5393 | * @param aSnapshotId Allowed snapshot attachment. NULL or empty UUID means
|
---|
5394 | * do not check.
|
---|
5395 | * @param fLockMedia Flag whether to lock the medium lock list or not.
|
---|
5396 | * If set to false and the medium lock list locking fails
|
---|
5397 | * later you must call #i_cancelMergeTo().
|
---|
5398 | * @param fMergeForward Resulting merge direction (out).
|
---|
5399 | * @param pParentForTarget New parent for target medium after merge (out).
|
---|
5400 | * @param aChildrenToReparent Medium lock list containing all children of the
|
---|
5401 | * source which will have to be reparented to the target
|
---|
5402 | * after merge (out).
|
---|
5403 | * @param aMediumLockList Medium locking information (out).
|
---|
5404 | *
|
---|
5405 | * @note Locks medium tree for reading. Locks this object, aTarget and all
|
---|
5406 | * intermediate media for writing.
|
---|
5407 | */
|
---|
5408 | HRESULT Medium::i_prepareMergeTo(const ComObjPtr<Medium> &pTarget,
|
---|
5409 | const Guid *aMachineId,
|
---|
5410 | const Guid *aSnapshotId,
|
---|
5411 | bool fLockMedia,
|
---|
5412 | bool &fMergeForward,
|
---|
5413 | ComObjPtr<Medium> &pParentForTarget,
|
---|
5414 | MediumLockList * &aChildrenToReparent,
|
---|
5415 | MediumLockList * &aMediumLockList)
|
---|
5416 | {
|
---|
5417 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
5418 | * to lock order violations, it probably causes lock order issues related
|
---|
5419 | * to the AutoCaller usage. Likewise the code using this method seems
|
---|
5420 | * problematic. */
|
---|
5421 | AssertReturn(pTarget != NULL, E_FAIL);
|
---|
5422 | AssertReturn(pTarget != this, E_FAIL);
|
---|
5423 |
|
---|
5424 | AutoCaller autoCaller(this);
|
---|
5425 | AssertComRCReturnRC(autoCaller.rc());
|
---|
5426 |
|
---|
5427 | AutoCaller targetCaller(pTarget);
|
---|
5428 | AssertComRCReturnRC(targetCaller.rc());
|
---|
5429 |
|
---|
5430 | HRESULT rc = S_OK;
|
---|
5431 | fMergeForward = false;
|
---|
5432 | pParentForTarget.setNull();
|
---|
5433 | Assert(aChildrenToReparent == NULL);
|
---|
5434 | aChildrenToReparent = NULL;
|
---|
5435 | Assert(aMediumLockList == NULL);
|
---|
5436 | aMediumLockList = NULL;
|
---|
5437 |
|
---|
5438 | try
|
---|
5439 | {
|
---|
5440 | // locking: we need the tree lock first because we access parent pointers
|
---|
5441 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5442 |
|
---|
5443 | /* more sanity checking and figuring out the merge direction */
|
---|
5444 | ComObjPtr<Medium> pMedium = i_getParent();
|
---|
5445 | while (!pMedium.isNull() && pMedium != pTarget)
|
---|
5446 | pMedium = pMedium->i_getParent();
|
---|
5447 | if (pMedium == pTarget)
|
---|
5448 | fMergeForward = false;
|
---|
5449 | else
|
---|
5450 | {
|
---|
5451 | pMedium = pTarget->i_getParent();
|
---|
5452 | while (!pMedium.isNull() && pMedium != this)
|
---|
5453 | pMedium = pMedium->i_getParent();
|
---|
5454 | if (pMedium == this)
|
---|
5455 | fMergeForward = true;
|
---|
5456 | else
|
---|
5457 | {
|
---|
5458 | Utf8Str tgtLoc;
|
---|
5459 | {
|
---|
5460 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5461 | tgtLoc = pTarget->i_getLocationFull();
|
---|
5462 | }
|
---|
5463 |
|
---|
5464 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5465 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5466 | tr("Media '%s' and '%s' are unrelated"),
|
---|
5467 | m->strLocationFull.c_str(), tgtLoc.c_str());
|
---|
5468 | }
|
---|
5469 | }
|
---|
5470 |
|
---|
5471 | /* Build the lock list. */
|
---|
5472 | aMediumLockList = new MediumLockList();
|
---|
5473 | treeLock.release();
|
---|
5474 | if (fMergeForward)
|
---|
5475 | rc = pTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5476 | pTarget /* pToLockWrite */,
|
---|
5477 | false /* fMediumLockWriteAll */,
|
---|
5478 | NULL,
|
---|
5479 | *aMediumLockList);
|
---|
5480 | else
|
---|
5481 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5482 | pTarget /* pToLockWrite */,
|
---|
5483 | false /* fMediumLockWriteAll */,
|
---|
5484 | NULL,
|
---|
5485 | *aMediumLockList);
|
---|
5486 | treeLock.acquire();
|
---|
5487 | if (FAILED(rc))
|
---|
5488 | throw rc;
|
---|
5489 |
|
---|
5490 | /* Sanity checking, must be after lock list creation as it depends on
|
---|
5491 | * valid medium states. The medium objects must be accessible. Only
|
---|
5492 | * do this if immediate locking is requested, otherwise it fails when
|
---|
5493 | * we construct a medium lock list for an already running VM. Snapshot
|
---|
5494 | * deletion uses this to simplify its life. */
|
---|
5495 | if (fLockMedia)
|
---|
5496 | {
|
---|
5497 | {
|
---|
5498 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5499 | if (m->state != MediumState_Created)
|
---|
5500 | throw i_setStateError();
|
---|
5501 | }
|
---|
5502 | {
|
---|
5503 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5504 | if (pTarget->m->state != MediumState_Created)
|
---|
5505 | throw pTarget->i_setStateError();
|
---|
5506 | }
|
---|
5507 | }
|
---|
5508 |
|
---|
5509 | /* check medium attachment and other sanity conditions */
|
---|
5510 | if (fMergeForward)
|
---|
5511 | {
|
---|
5512 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5513 | if (i_getChildren().size() > 1)
|
---|
5514 | {
|
---|
5515 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5516 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
5517 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
5518 | }
|
---|
5519 | /* One backreference is only allowed if the machine ID is not empty
|
---|
5520 | * and it matches the machine the medium is attached to (including
|
---|
5521 | * the snapshot ID if not empty). */
|
---|
5522 | if ( m->backRefs.size() != 0
|
---|
5523 | && ( !aMachineId
|
---|
5524 | || m->backRefs.size() != 1
|
---|
5525 | || aMachineId->isZero()
|
---|
5526 | || *i_getFirstMachineBackrefId() != *aMachineId
|
---|
5527 | || ( (!aSnapshotId || !aSnapshotId->isZero())
|
---|
5528 | && *i_getFirstMachineBackrefSnapshotId() != *aSnapshotId)))
|
---|
5529 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5530 | tr("Medium '%s' is attached to %d virtual machines"),
|
---|
5531 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
5532 | if (m->type == MediumType_Immutable)
|
---|
5533 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5534 | tr("Medium '%s' is immutable"),
|
---|
5535 | m->strLocationFull.c_str());
|
---|
5536 | if (m->type == MediumType_MultiAttach)
|
---|
5537 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5538 | tr("Medium '%s' is multi-attach"),
|
---|
5539 | m->strLocationFull.c_str());
|
---|
5540 | }
|
---|
5541 | else
|
---|
5542 | {
|
---|
5543 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5544 | if (pTarget->i_getChildren().size() > 1)
|
---|
5545 | {
|
---|
5546 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5547 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
5548 | pTarget->m->strLocationFull.c_str(),
|
---|
5549 | pTarget->i_getChildren().size());
|
---|
5550 | }
|
---|
5551 | if (pTarget->m->type == MediumType_Immutable)
|
---|
5552 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5553 | tr("Medium '%s' is immutable"),
|
---|
5554 | pTarget->m->strLocationFull.c_str());
|
---|
5555 | if (pTarget->m->type == MediumType_MultiAttach)
|
---|
5556 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5557 | tr("Medium '%s' is multi-attach"),
|
---|
5558 | pTarget->m->strLocationFull.c_str());
|
---|
5559 | }
|
---|
5560 | ComObjPtr<Medium> pLast(fMergeForward ? (Medium *)pTarget : this);
|
---|
5561 | ComObjPtr<Medium> pLastIntermediate = pLast->i_getParent();
|
---|
5562 | for (pLast = pLastIntermediate;
|
---|
5563 | !pLast.isNull() && pLast != pTarget && pLast != this;
|
---|
5564 | pLast = pLast->i_getParent())
|
---|
5565 | {
|
---|
5566 | AutoReadLock alock(pLast COMMA_LOCKVAL_SRC_POS);
|
---|
5567 | if (pLast->i_getChildren().size() > 1)
|
---|
5568 | {
|
---|
5569 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5570 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
5571 | pLast->m->strLocationFull.c_str(),
|
---|
5572 | pLast->i_getChildren().size());
|
---|
5573 | }
|
---|
5574 | if (pLast->m->backRefs.size() != 0)
|
---|
5575 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5576 | tr("Medium '%s' is attached to %d virtual machines"),
|
---|
5577 | pLast->m->strLocationFull.c_str(),
|
---|
5578 | pLast->m->backRefs.size());
|
---|
5579 |
|
---|
5580 | }
|
---|
5581 |
|
---|
5582 | /* Update medium states appropriately */
|
---|
5583 | {
|
---|
5584 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5585 |
|
---|
5586 | if (m->state == MediumState_Created)
|
---|
5587 | {
|
---|
5588 | rc = i_markForDeletion();
|
---|
5589 | if (FAILED(rc))
|
---|
5590 | throw rc;
|
---|
5591 | }
|
---|
5592 | else
|
---|
5593 | {
|
---|
5594 | if (fLockMedia)
|
---|
5595 | throw i_setStateError();
|
---|
5596 | else if ( m->state == MediumState_LockedWrite
|
---|
5597 | || m->state == MediumState_LockedRead)
|
---|
5598 | {
|
---|
5599 | /* Either mark it for deletion in locked state or allow
|
---|
5600 | * others to have done so. */
|
---|
5601 | if (m->preLockState == MediumState_Created)
|
---|
5602 | i_markLockedForDeletion();
|
---|
5603 | else if (m->preLockState != MediumState_Deleting)
|
---|
5604 | throw i_setStateError();
|
---|
5605 | }
|
---|
5606 | else
|
---|
5607 | throw i_setStateError();
|
---|
5608 | }
|
---|
5609 | }
|
---|
5610 |
|
---|
5611 | if (fMergeForward)
|
---|
5612 | {
|
---|
5613 | /* we will need parent to reparent target */
|
---|
5614 | pParentForTarget = i_getParent();
|
---|
5615 | }
|
---|
5616 | else
|
---|
5617 | {
|
---|
5618 | /* we will need to reparent children of the source */
|
---|
5619 | aChildrenToReparent = new MediumLockList();
|
---|
5620 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
5621 | it != i_getChildren().end();
|
---|
5622 | ++it)
|
---|
5623 | {
|
---|
5624 | pMedium = *it;
|
---|
5625 | aChildrenToReparent->Append(pMedium, true /* fLockWrite */);
|
---|
5626 | }
|
---|
5627 | if (fLockMedia && aChildrenToReparent)
|
---|
5628 | {
|
---|
5629 | treeLock.release();
|
---|
5630 | rc = aChildrenToReparent->Lock();
|
---|
5631 | treeLock.acquire();
|
---|
5632 | if (FAILED(rc))
|
---|
5633 | throw rc;
|
---|
5634 | }
|
---|
5635 | }
|
---|
5636 | for (pLast = pLastIntermediate;
|
---|
5637 | !pLast.isNull() && pLast != pTarget && pLast != this;
|
---|
5638 | pLast = pLast->i_getParent())
|
---|
5639 | {
|
---|
5640 | AutoWriteLock alock(pLast COMMA_LOCKVAL_SRC_POS);
|
---|
5641 | if (pLast->m->state == MediumState_Created)
|
---|
5642 | {
|
---|
5643 | rc = pLast->i_markForDeletion();
|
---|
5644 | if (FAILED(rc))
|
---|
5645 | throw rc;
|
---|
5646 | }
|
---|
5647 | else
|
---|
5648 | throw pLast->i_setStateError();
|
---|
5649 | }
|
---|
5650 |
|
---|
5651 | /* Tweak the lock list in the backward merge case, as the target
|
---|
5652 | * isn't marked to be locked for writing yet. */
|
---|
5653 | if (!fMergeForward)
|
---|
5654 | {
|
---|
5655 | MediumLockList::Base::iterator lockListBegin =
|
---|
5656 | aMediumLockList->GetBegin();
|
---|
5657 | MediumLockList::Base::iterator lockListEnd =
|
---|
5658 | aMediumLockList->GetEnd();
|
---|
5659 | ++lockListEnd;
|
---|
5660 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
5661 | it != lockListEnd;
|
---|
5662 | ++it)
|
---|
5663 | {
|
---|
5664 | MediumLock &mediumLock = *it;
|
---|
5665 | if (mediumLock.GetMedium() == pTarget)
|
---|
5666 | {
|
---|
5667 | HRESULT rc2 = mediumLock.UpdateLock(true);
|
---|
5668 | AssertComRC(rc2);
|
---|
5669 | break;
|
---|
5670 | }
|
---|
5671 | }
|
---|
5672 | }
|
---|
5673 |
|
---|
5674 | if (fLockMedia)
|
---|
5675 | {
|
---|
5676 | treeLock.release();
|
---|
5677 | rc = aMediumLockList->Lock();
|
---|
5678 | treeLock.acquire();
|
---|
5679 | if (FAILED(rc))
|
---|
5680 | {
|
---|
5681 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5682 | throw setError(rc,
|
---|
5683 | tr("Failed to lock media when merging to '%s'"),
|
---|
5684 | pTarget->i_getLocationFull().c_str());
|
---|
5685 | }
|
---|
5686 | }
|
---|
5687 | }
|
---|
5688 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5689 |
|
---|
5690 | if (FAILED(rc))
|
---|
5691 | {
|
---|
5692 | if (aMediumLockList)
|
---|
5693 | {
|
---|
5694 | delete aMediumLockList;
|
---|
5695 | aMediumLockList = NULL;
|
---|
5696 | }
|
---|
5697 | if (aChildrenToReparent)
|
---|
5698 | {
|
---|
5699 | delete aChildrenToReparent;
|
---|
5700 | aChildrenToReparent = NULL;
|
---|
5701 | }
|
---|
5702 | }
|
---|
5703 |
|
---|
5704 | return rc;
|
---|
5705 | }
|
---|
5706 |
|
---|
5707 | /**
|
---|
5708 | * Merges this medium to the specified medium which must be either its
|
---|
5709 | * direct ancestor or descendant.
|
---|
5710 | *
|
---|
5711 | * Given this medium is SOURCE and the specified medium is TARGET, we will
|
---|
5712 | * get two variants of the merge operation:
|
---|
5713 | *
|
---|
5714 | * forward merge
|
---|
5715 | * ------------------------->
|
---|
5716 | * [Extra] <- SOURCE <- Intermediate <- TARGET
|
---|
5717 | * Any Del Del LockWr
|
---|
5718 | *
|
---|
5719 | *
|
---|
5720 | * backward merge
|
---|
5721 | * <-------------------------
|
---|
5722 | * TARGET <- Intermediate <- SOURCE <- [Extra]
|
---|
5723 | * LockWr Del Del LockWr
|
---|
5724 | *
|
---|
5725 | * Each diagram shows the involved media on the media chain where
|
---|
5726 | * SOURCE and TARGET belong. Under each medium there is a state value which
|
---|
5727 | * the medium must have at a time of the mergeTo() call.
|
---|
5728 | *
|
---|
5729 | * The media in the square braces may be absent (e.g. when the forward
|
---|
5730 | * operation takes place and SOURCE is the base medium, or when the backward
|
---|
5731 | * merge operation takes place and TARGET is the last child in the chain) but if
|
---|
5732 | * they present they are involved too as shown.
|
---|
5733 | *
|
---|
5734 | * Neither the source medium nor intermediate media may be attached to
|
---|
5735 | * any VM directly or in the snapshot, otherwise this method will assert.
|
---|
5736 | *
|
---|
5737 | * The #i_prepareMergeTo() method must be called prior to this method to place
|
---|
5738 | * all involved to necessary states and perform other consistency checks.
|
---|
5739 | *
|
---|
5740 | * If @a aWait is @c true then this method will perform the operation on the
|
---|
5741 | * calling thread and will not return to the caller until the operation is
|
---|
5742 | * completed. When this method succeeds, all intermediate medium objects in
|
---|
5743 | * the chain will be uninitialized, the state of the target medium (and all
|
---|
5744 | * involved extra media) will be restored. @a aMediumLockList will not be
|
---|
5745 | * deleted, whether the operation is successful or not. The caller has to do
|
---|
5746 | * this if appropriate. Note that this (source) medium is not uninitialized
|
---|
5747 | * because of possible AutoCaller instances held by the caller of this method
|
---|
5748 | * on the current thread. It's therefore the responsibility of the caller to
|
---|
5749 | * call Medium::uninit() after releasing all callers.
|
---|
5750 | *
|
---|
5751 | * If @a aWait is @c false then this method will create a thread to perform the
|
---|
5752 | * operation asynchronously and will return immediately. If the operation
|
---|
5753 | * succeeds, the thread will uninitialize the source medium object and all
|
---|
5754 | * intermediate medium objects in the chain, reset the state of the target
|
---|
5755 | * medium (and all involved extra media) and delete @a aMediumLockList.
|
---|
5756 | * If the operation fails, the thread will only reset the states of all
|
---|
5757 | * involved media and delete @a aMediumLockList.
|
---|
5758 | *
|
---|
5759 | * When this method fails (regardless of the @a aWait mode), it is a caller's
|
---|
5760 | * responsibility to undo state changes and delete @a aMediumLockList using
|
---|
5761 | * #i_cancelMergeTo().
|
---|
5762 | *
|
---|
5763 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
5764 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
5765 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
5766 | * progress object is created/used at all. Note that @a aProgress cannot be
|
---|
5767 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
5768 | *
|
---|
5769 | * @param pTarget Target medium.
|
---|
5770 | * @param fMergeForward Merge direction.
|
---|
5771 | * @param pParentForTarget New parent for target medium after merge.
|
---|
5772 | * @param aChildrenToReparent List of children of the source which will have
|
---|
5773 | * to be reparented to the target after merge.
|
---|
5774 | * @param aMediumLockList Medium locking information.
|
---|
5775 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
5776 | * completion.
|
---|
5777 | * @param aWait @c true if this method should block instead of creating
|
---|
5778 | * an asynchronous thread.
|
---|
5779 | *
|
---|
5780 | * @note Locks the tree lock for writing. Locks the media from the chain
|
---|
5781 | * for writing.
|
---|
5782 | */
|
---|
5783 | HRESULT Medium::i_mergeTo(const ComObjPtr<Medium> &pTarget,
|
---|
5784 | bool fMergeForward,
|
---|
5785 | const ComObjPtr<Medium> &pParentForTarget,
|
---|
5786 | MediumLockList *aChildrenToReparent,
|
---|
5787 | MediumLockList *aMediumLockList,
|
---|
5788 | ComObjPtr<Progress> *aProgress,
|
---|
5789 | bool aWait)
|
---|
5790 | {
|
---|
5791 | AssertReturn(pTarget != NULL, E_FAIL);
|
---|
5792 | AssertReturn(pTarget != this, E_FAIL);
|
---|
5793 | AssertReturn(aMediumLockList != NULL, E_FAIL);
|
---|
5794 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
5795 |
|
---|
5796 | AutoCaller autoCaller(this);
|
---|
5797 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
5798 |
|
---|
5799 | AutoCaller targetCaller(pTarget);
|
---|
5800 | AssertComRCReturnRC(targetCaller.rc());
|
---|
5801 |
|
---|
5802 | HRESULT rc = S_OK;
|
---|
5803 | ComObjPtr<Progress> pProgress;
|
---|
5804 | Medium::Task *pTask = NULL;
|
---|
5805 |
|
---|
5806 | try
|
---|
5807 | {
|
---|
5808 | if (aProgress != NULL)
|
---|
5809 | {
|
---|
5810 | /* use the existing progress object... */
|
---|
5811 | pProgress = *aProgress;
|
---|
5812 |
|
---|
5813 | /* ...but create a new one if it is null */
|
---|
5814 | if (pProgress.isNull())
|
---|
5815 | {
|
---|
5816 | Utf8Str tgtName;
|
---|
5817 | {
|
---|
5818 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5819 | tgtName = pTarget->i_getName();
|
---|
5820 | }
|
---|
5821 |
|
---|
5822 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5823 |
|
---|
5824 | pProgress.createObject();
|
---|
5825 | rc = pProgress->init(m->pVirtualBox,
|
---|
5826 | static_cast<IMedium*>(this),
|
---|
5827 | BstrFmt(tr("Merging medium '%s' to '%s'"),
|
---|
5828 | i_getName().c_str(),
|
---|
5829 | tgtName.c_str()).raw(),
|
---|
5830 | TRUE /* aCancelable */);
|
---|
5831 | if (FAILED(rc))
|
---|
5832 | throw rc;
|
---|
5833 | }
|
---|
5834 | }
|
---|
5835 |
|
---|
5836 | /* setup task object to carry out the operation sync/async */
|
---|
5837 | pTask = new Medium::MergeTask(this, pTarget, fMergeForward,
|
---|
5838 | pParentForTarget, aChildrenToReparent,
|
---|
5839 | pProgress, aMediumLockList,
|
---|
5840 | aWait /* fKeepMediumLockList */);
|
---|
5841 | rc = pTask->rc();
|
---|
5842 | AssertComRC(rc);
|
---|
5843 | if (FAILED(rc))
|
---|
5844 | throw rc;
|
---|
5845 | }
|
---|
5846 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5847 |
|
---|
5848 | if (SUCCEEDED(rc))
|
---|
5849 | {
|
---|
5850 | if (aWait)
|
---|
5851 | {
|
---|
5852 | rc = pTask->runNow();
|
---|
5853 |
|
---|
5854 | delete pTask;
|
---|
5855 | }
|
---|
5856 | else
|
---|
5857 | rc = pTask->createThread();
|
---|
5858 |
|
---|
5859 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
5860 | *aProgress = pProgress;
|
---|
5861 | }
|
---|
5862 | else if (pTask != NULL)
|
---|
5863 | delete pTask;
|
---|
5864 |
|
---|
5865 | return rc;
|
---|
5866 | }
|
---|
5867 |
|
---|
5868 | /**
|
---|
5869 | * Undoes what #i_prepareMergeTo() did. Must be called if #mergeTo() is not
|
---|
5870 | * called or fails. Frees memory occupied by @a aMediumLockList and unlocks
|
---|
5871 | * the medium objects in @a aChildrenToReparent.
|
---|
5872 | *
|
---|
5873 | * @param aChildrenToReparent List of children of the source which will have
|
---|
5874 | * to be reparented to the target after merge.
|
---|
5875 | * @param aMediumLockList Medium locking information.
|
---|
5876 | *
|
---|
5877 | * @note Locks the media from the chain for writing.
|
---|
5878 | */
|
---|
5879 | void Medium::i_cancelMergeTo(MediumLockList *aChildrenToReparent,
|
---|
5880 | MediumLockList *aMediumLockList)
|
---|
5881 | {
|
---|
5882 | AutoCaller autoCaller(this);
|
---|
5883 | AssertComRCReturnVoid(autoCaller.rc());
|
---|
5884 |
|
---|
5885 | AssertReturnVoid(aMediumLockList != NULL);
|
---|
5886 |
|
---|
5887 | /* Revert media marked for deletion to previous state. */
|
---|
5888 | HRESULT rc;
|
---|
5889 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
5890 | aMediumLockList->GetBegin();
|
---|
5891 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
5892 | aMediumLockList->GetEnd();
|
---|
5893 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
5894 | it != mediumListEnd;
|
---|
5895 | ++it)
|
---|
5896 | {
|
---|
5897 | const MediumLock &mediumLock = *it;
|
---|
5898 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
5899 | AutoWriteLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
5900 |
|
---|
5901 | if (pMedium->m->state == MediumState_Deleting)
|
---|
5902 | {
|
---|
5903 | rc = pMedium->i_unmarkForDeletion();
|
---|
5904 | AssertComRC(rc);
|
---|
5905 | }
|
---|
5906 | else if ( ( pMedium->m->state == MediumState_LockedWrite
|
---|
5907 | || pMedium->m->state == MediumState_LockedRead)
|
---|
5908 | && pMedium->m->preLockState == MediumState_Deleting)
|
---|
5909 | {
|
---|
5910 | rc = pMedium->i_unmarkLockedForDeletion();
|
---|
5911 | AssertComRC(rc);
|
---|
5912 | }
|
---|
5913 | }
|
---|
5914 |
|
---|
5915 | /* the destructor will do the work */
|
---|
5916 | delete aMediumLockList;
|
---|
5917 |
|
---|
5918 | /* unlock the children which had to be reparented, the destructor will do
|
---|
5919 | * the work */
|
---|
5920 | if (aChildrenToReparent)
|
---|
5921 | delete aChildrenToReparent;
|
---|
5922 | }
|
---|
5923 |
|
---|
5924 | /**
|
---|
5925 | * Fix the parent UUID of all children to point to this medium as their
|
---|
5926 | * parent.
|
---|
5927 | */
|
---|
5928 | HRESULT Medium::i_fixParentUuidOfChildren(MediumLockList *pChildrenToReparent)
|
---|
5929 | {
|
---|
5930 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
5931 | * to lock order violations, it probably causes lock order issues related
|
---|
5932 | * to the AutoCaller usage. Likewise the code using this method seems
|
---|
5933 | * problematic. */
|
---|
5934 | Assert(!isWriteLockOnCurrentThread());
|
---|
5935 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
5936 | MediumLockList mediumLockList;
|
---|
5937 | HRESULT rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5938 | NULL /* pToLockWrite */,
|
---|
5939 | false /* fMediumLockWriteAll */,
|
---|
5940 | this,
|
---|
5941 | mediumLockList);
|
---|
5942 | AssertComRCReturnRC(rc);
|
---|
5943 |
|
---|
5944 | try
|
---|
5945 | {
|
---|
5946 | PVDISK hdd;
|
---|
5947 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
5948 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
5949 |
|
---|
5950 | try
|
---|
5951 | {
|
---|
5952 | MediumLockList::Base::iterator lockListBegin =
|
---|
5953 | mediumLockList.GetBegin();
|
---|
5954 | MediumLockList::Base::iterator lockListEnd =
|
---|
5955 | mediumLockList.GetEnd();
|
---|
5956 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
5957 | it != lockListEnd;
|
---|
5958 | ++it)
|
---|
5959 | {
|
---|
5960 | MediumLock &mediumLock = *it;
|
---|
5961 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
5962 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
5963 |
|
---|
5964 | // open the medium
|
---|
5965 | vrc = VDOpen(hdd,
|
---|
5966 | pMedium->m->strFormat.c_str(),
|
---|
5967 | pMedium->m->strLocationFull.c_str(),
|
---|
5968 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
5969 | pMedium->m->vdImageIfaces);
|
---|
5970 | if (RT_FAILURE(vrc))
|
---|
5971 | throw vrc;
|
---|
5972 | }
|
---|
5973 |
|
---|
5974 | MediumLockList::Base::iterator childrenBegin = pChildrenToReparent->GetBegin();
|
---|
5975 | MediumLockList::Base::iterator childrenEnd = pChildrenToReparent->GetEnd();
|
---|
5976 | for (MediumLockList::Base::iterator it = childrenBegin;
|
---|
5977 | it != childrenEnd;
|
---|
5978 | ++it)
|
---|
5979 | {
|
---|
5980 | Medium *pMedium = it->GetMedium();
|
---|
5981 | /* VD_OPEN_FLAGS_INFO since UUID is wrong yet */
|
---|
5982 | vrc = VDOpen(hdd,
|
---|
5983 | pMedium->m->strFormat.c_str(),
|
---|
5984 | pMedium->m->strLocationFull.c_str(),
|
---|
5985 | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
5986 | pMedium->m->vdImageIfaces);
|
---|
5987 | if (RT_FAILURE(vrc))
|
---|
5988 | throw vrc;
|
---|
5989 |
|
---|
5990 | vrc = VDSetParentUuid(hdd, VD_LAST_IMAGE, m->id.raw());
|
---|
5991 | if (RT_FAILURE(vrc))
|
---|
5992 | throw vrc;
|
---|
5993 |
|
---|
5994 | vrc = VDClose(hdd, false /* fDelete */);
|
---|
5995 | if (RT_FAILURE(vrc))
|
---|
5996 | throw vrc;
|
---|
5997 | }
|
---|
5998 | }
|
---|
5999 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6000 | catch (int aVRC)
|
---|
6001 | {
|
---|
6002 | rc = setError(E_FAIL,
|
---|
6003 | tr("Could not update medium UUID references to parent '%s' (%s)"),
|
---|
6004 | m->strLocationFull.c_str(),
|
---|
6005 | i_vdError(aVRC).c_str());
|
---|
6006 | }
|
---|
6007 |
|
---|
6008 | VDDestroy(hdd);
|
---|
6009 | }
|
---|
6010 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6011 |
|
---|
6012 | return rc;
|
---|
6013 | }
|
---|
6014 |
|
---|
6015 | /**
|
---|
6016 | *
|
---|
6017 | * @note Similar code exists in i_taskExportHandler.
|
---|
6018 | */
|
---|
6019 | HRESULT Medium::i_addRawToFss(const char *aFilename, SecretKeyStore *pKeyStore, RTVFSFSSTREAM hVfsFssDst,
|
---|
6020 | const ComObjPtr<Progress> &aProgress, bool fSparse)
|
---|
6021 | {
|
---|
6022 | AutoCaller autoCaller(this);
|
---|
6023 | HRESULT hrc = autoCaller.rc();
|
---|
6024 | if (SUCCEEDED(hrc))
|
---|
6025 | {
|
---|
6026 | /*
|
---|
6027 | * Get a readonly hdd for this medium.
|
---|
6028 | */
|
---|
6029 | Medium::CryptoFilterSettings CryptoSettingsRead;
|
---|
6030 | MediumLockList SourceMediumLockList;
|
---|
6031 | PVDISK pHdd;
|
---|
6032 | hrc = i_openHddForReading(pKeyStore, &pHdd, &SourceMediumLockList, &CryptoSettingsRead);
|
---|
6033 | if (SUCCEEDED(hrc))
|
---|
6034 | {
|
---|
6035 | /*
|
---|
6036 | * Create a VFS file interface to the HDD and attach a progress wrapper
|
---|
6037 | * that monitors the progress reading of the raw image. The image will
|
---|
6038 | * be read twice if hVfsFssDst does sparse processing.
|
---|
6039 | */
|
---|
6040 | RTVFSFILE hVfsFileDisk = NIL_RTVFSFILE;
|
---|
6041 | int vrc = VDCreateVfsFileFromDisk(pHdd, 0 /*fFlags*/, &hVfsFileDisk);
|
---|
6042 | if (RT_SUCCESS(vrc))
|
---|
6043 | {
|
---|
6044 | RTVFSFILE hVfsFileProgress = NIL_RTVFSFILE;
|
---|
6045 | vrc = RTVfsCreateProgressForFile(hVfsFileDisk, aProgress->i_iprtProgressCallback, &*aProgress,
|
---|
6046 | RTVFSPROGRESS_F_CANCELABLE | RTVFSPROGRESS_F_FORWARD_SEEK_AS_READ,
|
---|
6047 | VDGetSize(pHdd, VD_LAST_IMAGE) * (fSparse ? 2 : 1) /*cbExpectedRead*/,
|
---|
6048 | 0 /*cbExpectedWritten*/, &hVfsFileProgress);
|
---|
6049 | RTVfsFileRelease(hVfsFileDisk);
|
---|
6050 | if (RT_SUCCESS(vrc))
|
---|
6051 | {
|
---|
6052 | RTVFSOBJ hVfsObj = RTVfsObjFromFile(hVfsFileProgress);
|
---|
6053 | RTVfsFileRelease(hVfsFileProgress);
|
---|
6054 |
|
---|
6055 | vrc = RTVfsFsStrmAdd(hVfsFssDst, aFilename, hVfsObj, 0 /*fFlags*/);
|
---|
6056 | RTVfsObjRelease(hVfsObj);
|
---|
6057 | if (RT_FAILURE(vrc))
|
---|
6058 | hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Failed to add '%s' to output (%Rrc)"), aFilename, vrc);
|
---|
6059 | }
|
---|
6060 | else
|
---|
6061 | hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
6062 | tr("RTVfsCreateProgressForFile failed when processing '%s' (%Rrc)"), aFilename, vrc);
|
---|
6063 | }
|
---|
6064 | else
|
---|
6065 | hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("VDCreateVfsFileFromDisk failed for '%s' (%Rrc)"), aFilename, vrc);
|
---|
6066 | VDDestroy(pHdd);
|
---|
6067 | }
|
---|
6068 | }
|
---|
6069 | return hrc;
|
---|
6070 | }
|
---|
6071 |
|
---|
6072 | /**
|
---|
6073 | * Used by IAppliance to export disk images.
|
---|
6074 | *
|
---|
6075 | * @param aFilename Filename to create (UTF8).
|
---|
6076 | * @param aFormat Medium format for creating @a aFilename.
|
---|
6077 | * @param aVariant Which exact image format variant to use for the
|
---|
6078 | * destination image.
|
---|
6079 | * @param pKeyStore The optional key store for decrypting the data for
|
---|
6080 | * encrypted media during the export.
|
---|
6081 | * @param hVfsIosDst The destination I/O stream object.
|
---|
6082 | * @param aProgress Progress object to use.
|
---|
6083 | * @return
|
---|
6084 | *
|
---|
6085 | * @note The source format is defined by the Medium instance.
|
---|
6086 | */
|
---|
6087 | HRESULT Medium::i_exportFile(const char *aFilename,
|
---|
6088 | const ComObjPtr<MediumFormat> &aFormat,
|
---|
6089 | MediumVariant_T aVariant,
|
---|
6090 | SecretKeyStore *pKeyStore,
|
---|
6091 | RTVFSIOSTREAM hVfsIosDst,
|
---|
6092 | const ComObjPtr<Progress> &aProgress)
|
---|
6093 | {
|
---|
6094 | AssertPtrReturn(aFilename, E_INVALIDARG);
|
---|
6095 | AssertReturn(aFormat.isNotNull(), E_INVALIDARG);
|
---|
6096 | AssertReturn(aProgress.isNotNull(), E_INVALIDARG);
|
---|
6097 |
|
---|
6098 | AutoCaller autoCaller(this);
|
---|
6099 | HRESULT hrc = autoCaller.rc();
|
---|
6100 | if (SUCCEEDED(hrc))
|
---|
6101 | {
|
---|
6102 | /*
|
---|
6103 | * Setup VD interfaces.
|
---|
6104 | */
|
---|
6105 | PVDINTERFACE pVDImageIfaces = m->vdImageIfaces;
|
---|
6106 | PVDINTERFACEIO pVfsIoIf;
|
---|
6107 | int vrc = VDIfCreateFromVfsStream(hVfsIosDst, RTFILE_O_WRITE, &pVfsIoIf);
|
---|
6108 | if (RT_SUCCESS(vrc))
|
---|
6109 | {
|
---|
6110 | vrc = VDInterfaceAdd(&pVfsIoIf->Core, "Medium::ExportTaskVfsIos", VDINTERFACETYPE_IO,
|
---|
6111 | pVfsIoIf, sizeof(VDINTERFACEIO), &pVDImageIfaces);
|
---|
6112 | if (RT_SUCCESS(vrc))
|
---|
6113 | {
|
---|
6114 | /*
|
---|
6115 | * Get a readonly hdd for this medium (source).
|
---|
6116 | */
|
---|
6117 | Medium::CryptoFilterSettings CryptoSettingsRead;
|
---|
6118 | MediumLockList SourceMediumLockList;
|
---|
6119 | PVDISK pSrcHdd;
|
---|
6120 | hrc = i_openHddForReading(pKeyStore, &pSrcHdd, &SourceMediumLockList, &CryptoSettingsRead);
|
---|
6121 | if (SUCCEEDED(hrc))
|
---|
6122 | {
|
---|
6123 | /*
|
---|
6124 | * Create the target medium.
|
---|
6125 | */
|
---|
6126 | Utf8Str strDstFormat(aFormat->i_getId());
|
---|
6127 |
|
---|
6128 | /* ensure the target directory exists */
|
---|
6129 | uint64_t fDstCapabilities = aFormat->i_getCapabilities();
|
---|
6130 | if (fDstCapabilities & MediumFormatCapabilities_File)
|
---|
6131 | {
|
---|
6132 | Utf8Str strDstLocation(aFilename);
|
---|
6133 | hrc = VirtualBox::i_ensureFilePathExists(strDstLocation.c_str(),
|
---|
6134 | !(aVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
6135 | }
|
---|
6136 | if (SUCCEEDED(hrc))
|
---|
6137 | {
|
---|
6138 | PVDISK pDstHdd;
|
---|
6139 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDstHdd);
|
---|
6140 | if (RT_SUCCESS(vrc))
|
---|
6141 | {
|
---|
6142 | /*
|
---|
6143 | * Create an interface for getting progress callbacks.
|
---|
6144 | */
|
---|
6145 | VDINTERFACEPROGRESS ProgressIf = VDINTERFACEPROGRESS_INITALIZER(aProgress->i_vdProgressCallback);
|
---|
6146 | PVDINTERFACE pProgress = NULL;
|
---|
6147 | vrc = VDInterfaceAdd(&ProgressIf.Core, "export-progress", VDINTERFACETYPE_PROGRESS,
|
---|
6148 | &*aProgress, sizeof(ProgressIf), &pProgress);
|
---|
6149 | AssertRC(vrc);
|
---|
6150 |
|
---|
6151 | /*
|
---|
6152 | * Do the exporting.
|
---|
6153 | */
|
---|
6154 | vrc = VDCopy(pSrcHdd,
|
---|
6155 | VD_LAST_IMAGE,
|
---|
6156 | pDstHdd,
|
---|
6157 | strDstFormat.c_str(),
|
---|
6158 | aFilename,
|
---|
6159 | false /* fMoveByRename */,
|
---|
6160 | 0 /* cbSize */,
|
---|
6161 | aVariant & ~MediumVariant_NoCreateDir,
|
---|
6162 | NULL /* pDstUuid */,
|
---|
6163 | VD_OPEN_FLAGS_NORMAL | VD_OPEN_FLAGS_SEQUENTIAL,
|
---|
6164 | pProgress,
|
---|
6165 | pVDImageIfaces,
|
---|
6166 | NULL);
|
---|
6167 | if (RT_SUCCESS(vrc))
|
---|
6168 | hrc = S_OK;
|
---|
6169 | else
|
---|
6170 | hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Could not create the exported medium '%s'%s"),
|
---|
6171 | aFilename, i_vdError(vrc).c_str());
|
---|
6172 | VDDestroy(pDstHdd);
|
---|
6173 | }
|
---|
6174 | else
|
---|
6175 | hrc = setErrorVrc(vrc);
|
---|
6176 | }
|
---|
6177 | }
|
---|
6178 | VDDestroy(pSrcHdd);
|
---|
6179 | }
|
---|
6180 | else
|
---|
6181 | hrc = setErrorVrc(vrc, "VDInterfaceAdd -> %Rrc", vrc);
|
---|
6182 | VDIfDestroyFromVfsStream(pVfsIoIf);
|
---|
6183 | }
|
---|
6184 | else
|
---|
6185 | hrc = setErrorVrc(vrc, "VDIfCreateFromVfsStream -> %Rrc", vrc);
|
---|
6186 | }
|
---|
6187 | return hrc;
|
---|
6188 | }
|
---|
6189 |
|
---|
6190 | /**
|
---|
6191 | * Used by IAppliance to import disk images.
|
---|
6192 | *
|
---|
6193 | * @param aFilename Filename to read (UTF8).
|
---|
6194 | * @param aFormat Medium format for reading @a aFilename.
|
---|
6195 | * @param aVariant Which exact image format variant to use
|
---|
6196 | * for the destination image.
|
---|
6197 | * @param aVfsIosSrc Handle to the source I/O stream.
|
---|
6198 | * @param aParent Parent medium. May be NULL.
|
---|
6199 | * @param aProgress Progress object to use.
|
---|
6200 | * @return
|
---|
6201 | * @note The destination format is defined by the Medium instance.
|
---|
6202 | *
|
---|
6203 | * @todo The only consumer of this method (Appliance::i_importOneDiskImage) is
|
---|
6204 | * already on a worker thread, so perhaps consider bypassing the thread
|
---|
6205 | * here and run in the task synchronously? VBoxSVC has enough threads as
|
---|
6206 | * it is...
|
---|
6207 | */
|
---|
6208 | HRESULT Medium::i_importFile(const char *aFilename,
|
---|
6209 | const ComObjPtr<MediumFormat> &aFormat,
|
---|
6210 | MediumVariant_T aVariant,
|
---|
6211 | RTVFSIOSTREAM aVfsIosSrc,
|
---|
6212 | const ComObjPtr<Medium> &aParent,
|
---|
6213 | const ComObjPtr<Progress> &aProgress)
|
---|
6214 | {
|
---|
6215 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
6216 | * to lock order violations, it probably causes lock order issues related
|
---|
6217 | * to the AutoCaller usage. */
|
---|
6218 | AssertPtrReturn(aFilename, E_INVALIDARG);
|
---|
6219 | AssertReturn(!aFormat.isNull(), E_INVALIDARG);
|
---|
6220 | AssertReturn(!aProgress.isNull(), E_INVALIDARG);
|
---|
6221 |
|
---|
6222 | AutoCaller autoCaller(this);
|
---|
6223 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
6224 |
|
---|
6225 | HRESULT rc = S_OK;
|
---|
6226 | Medium::Task *pTask = NULL;
|
---|
6227 |
|
---|
6228 | try
|
---|
6229 | {
|
---|
6230 | // locking: we need the tree lock first because we access parent pointers
|
---|
6231 | // and we need to write-lock the media involved
|
---|
6232 | uint32_t cHandles = 2;
|
---|
6233 | LockHandle* pHandles[3] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
6234 | this->lockHandle() };
|
---|
6235 | /* Only add parent to the lock if it is not null */
|
---|
6236 | if (!aParent.isNull())
|
---|
6237 | pHandles[cHandles++] = aParent->lockHandle();
|
---|
6238 | AutoWriteLock alock(cHandles,
|
---|
6239 | pHandles
|
---|
6240 | COMMA_LOCKVAL_SRC_POS);
|
---|
6241 |
|
---|
6242 | if ( m->state != MediumState_NotCreated
|
---|
6243 | && m->state != MediumState_Created)
|
---|
6244 | throw i_setStateError();
|
---|
6245 |
|
---|
6246 | /* Build the target lock list. */
|
---|
6247 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
6248 | alock.release();
|
---|
6249 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
6250 | this /* pToLockWrite */,
|
---|
6251 | false /* fMediumLockWriteAll */,
|
---|
6252 | aParent,
|
---|
6253 | *pTargetMediumLockList);
|
---|
6254 | alock.acquire();
|
---|
6255 | if (FAILED(rc))
|
---|
6256 | {
|
---|
6257 | delete pTargetMediumLockList;
|
---|
6258 | throw rc;
|
---|
6259 | }
|
---|
6260 |
|
---|
6261 | alock.release();
|
---|
6262 | rc = pTargetMediumLockList->Lock();
|
---|
6263 | alock.acquire();
|
---|
6264 | if (FAILED(rc))
|
---|
6265 | {
|
---|
6266 | delete pTargetMediumLockList;
|
---|
6267 | throw setError(rc,
|
---|
6268 | tr("Failed to lock target media '%s'"),
|
---|
6269 | i_getLocationFull().c_str());
|
---|
6270 | }
|
---|
6271 |
|
---|
6272 | /* setup task object to carry out the operation asynchronously */
|
---|
6273 | pTask = new Medium::ImportTask(this, aProgress, aFilename, aFormat, aVariant,
|
---|
6274 | aVfsIosSrc, aParent, pTargetMediumLockList);
|
---|
6275 | rc = pTask->rc();
|
---|
6276 | AssertComRC(rc);
|
---|
6277 | if (FAILED(rc))
|
---|
6278 | throw rc;
|
---|
6279 |
|
---|
6280 | if (m->state == MediumState_NotCreated)
|
---|
6281 | m->state = MediumState_Creating;
|
---|
6282 | }
|
---|
6283 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6284 |
|
---|
6285 | if (SUCCEEDED(rc))
|
---|
6286 | rc = pTask->createThread();
|
---|
6287 | else if (pTask != NULL)
|
---|
6288 | delete pTask;
|
---|
6289 |
|
---|
6290 | return rc;
|
---|
6291 | }
|
---|
6292 |
|
---|
6293 | /**
|
---|
6294 | * Internal version of the public CloneTo API which allows to enable certain
|
---|
6295 | * optimizations to improve speed during VM cloning.
|
---|
6296 | *
|
---|
6297 | * @param aTarget Target medium
|
---|
6298 | * @param aVariant Which exact image format variant to use
|
---|
6299 | * for the destination image.
|
---|
6300 | * @param aParent Parent medium. May be NULL.
|
---|
6301 | * @param aProgress Progress object to use.
|
---|
6302 | * @param idxSrcImageSame The last image in the source chain which has the
|
---|
6303 | * same content as the given image in the destination
|
---|
6304 | * chain. Use UINT32_MAX to disable this optimization.
|
---|
6305 | * @param idxDstImageSame The last image in the destination chain which has the
|
---|
6306 | * same content as the given image in the source chain.
|
---|
6307 | * Use UINT32_MAX to disable this optimization.
|
---|
6308 | * @return
|
---|
6309 | */
|
---|
6310 | HRESULT Medium::i_cloneToEx(const ComObjPtr<Medium> &aTarget, ULONG aVariant,
|
---|
6311 | const ComObjPtr<Medium> &aParent, IProgress **aProgress,
|
---|
6312 | uint32_t idxSrcImageSame, uint32_t idxDstImageSame)
|
---|
6313 | {
|
---|
6314 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
6315 | * to lock order violations, it probably causes lock order issues related
|
---|
6316 | * to the AutoCaller usage. */
|
---|
6317 | CheckComArgNotNull(aTarget);
|
---|
6318 | CheckComArgOutPointerValid(aProgress);
|
---|
6319 | ComAssertRet(aTarget != this, E_INVALIDARG);
|
---|
6320 |
|
---|
6321 | AutoCaller autoCaller(this);
|
---|
6322 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
6323 |
|
---|
6324 | HRESULT rc = S_OK;
|
---|
6325 | ComObjPtr<Progress> pProgress;
|
---|
6326 | Medium::Task *pTask = NULL;
|
---|
6327 |
|
---|
6328 | try
|
---|
6329 | {
|
---|
6330 | // locking: we need the tree lock first because we access parent pointers
|
---|
6331 | // and we need to write-lock the media involved
|
---|
6332 | uint32_t cHandles = 3;
|
---|
6333 | LockHandle* pHandles[4] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
6334 | this->lockHandle(),
|
---|
6335 | aTarget->lockHandle() };
|
---|
6336 | /* Only add parent to the lock if it is not null */
|
---|
6337 | if (!aParent.isNull())
|
---|
6338 | pHandles[cHandles++] = aParent->lockHandle();
|
---|
6339 | AutoWriteLock alock(cHandles,
|
---|
6340 | pHandles
|
---|
6341 | COMMA_LOCKVAL_SRC_POS);
|
---|
6342 |
|
---|
6343 | if ( aTarget->m->state != MediumState_NotCreated
|
---|
6344 | && aTarget->m->state != MediumState_Created)
|
---|
6345 | throw aTarget->i_setStateError();
|
---|
6346 |
|
---|
6347 | /* Build the source lock list. */
|
---|
6348 | MediumLockList *pSourceMediumLockList(new MediumLockList());
|
---|
6349 | alock.release();
|
---|
6350 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
6351 | NULL /* pToLockWrite */,
|
---|
6352 | false /* fMediumLockWriteAll */,
|
---|
6353 | NULL,
|
---|
6354 | *pSourceMediumLockList);
|
---|
6355 | alock.acquire();
|
---|
6356 | if (FAILED(rc))
|
---|
6357 | {
|
---|
6358 | delete pSourceMediumLockList;
|
---|
6359 | throw rc;
|
---|
6360 | }
|
---|
6361 |
|
---|
6362 | /* Build the target lock list (including the to-be parent chain). */
|
---|
6363 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
6364 | alock.release();
|
---|
6365 | rc = aTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
6366 | aTarget /* pToLockWrite */,
|
---|
6367 | false /* fMediumLockWriteAll */,
|
---|
6368 | aParent,
|
---|
6369 | *pTargetMediumLockList);
|
---|
6370 | alock.acquire();
|
---|
6371 | if (FAILED(rc))
|
---|
6372 | {
|
---|
6373 | delete pSourceMediumLockList;
|
---|
6374 | delete pTargetMediumLockList;
|
---|
6375 | throw rc;
|
---|
6376 | }
|
---|
6377 |
|
---|
6378 | alock.release();
|
---|
6379 | rc = pSourceMediumLockList->Lock();
|
---|
6380 | alock.acquire();
|
---|
6381 | if (FAILED(rc))
|
---|
6382 | {
|
---|
6383 | delete pSourceMediumLockList;
|
---|
6384 | delete pTargetMediumLockList;
|
---|
6385 | throw setError(rc,
|
---|
6386 | tr("Failed to lock source media '%s'"),
|
---|
6387 | i_getLocationFull().c_str());
|
---|
6388 | }
|
---|
6389 | alock.release();
|
---|
6390 | rc = pTargetMediumLockList->Lock();
|
---|
6391 | alock.acquire();
|
---|
6392 | if (FAILED(rc))
|
---|
6393 | {
|
---|
6394 | delete pSourceMediumLockList;
|
---|
6395 | delete pTargetMediumLockList;
|
---|
6396 | throw setError(rc,
|
---|
6397 | tr("Failed to lock target media '%s'"),
|
---|
6398 | aTarget->i_getLocationFull().c_str());
|
---|
6399 | }
|
---|
6400 |
|
---|
6401 | pProgress.createObject();
|
---|
6402 | rc = pProgress->init(m->pVirtualBox,
|
---|
6403 | static_cast <IMedium *>(this),
|
---|
6404 | BstrFmt(tr("Creating clone medium '%s'"), aTarget->m->strLocationFull.c_str()).raw(),
|
---|
6405 | TRUE /* aCancelable */);
|
---|
6406 | if (FAILED(rc))
|
---|
6407 | {
|
---|
6408 | delete pSourceMediumLockList;
|
---|
6409 | delete pTargetMediumLockList;
|
---|
6410 | throw rc;
|
---|
6411 | }
|
---|
6412 |
|
---|
6413 | /* setup task object to carry out the operation asynchronously */
|
---|
6414 | pTask = new Medium::CloneTask(this, pProgress, aTarget,
|
---|
6415 | (MediumVariant_T)aVariant,
|
---|
6416 | aParent, idxSrcImageSame,
|
---|
6417 | idxDstImageSame, pSourceMediumLockList,
|
---|
6418 | pTargetMediumLockList);
|
---|
6419 | rc = pTask->rc();
|
---|
6420 | AssertComRC(rc);
|
---|
6421 | if (FAILED(rc))
|
---|
6422 | throw rc;
|
---|
6423 |
|
---|
6424 | if (aTarget->m->state == MediumState_NotCreated)
|
---|
6425 | aTarget->m->state = MediumState_Creating;
|
---|
6426 | }
|
---|
6427 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6428 |
|
---|
6429 | if (SUCCEEDED(rc))
|
---|
6430 | {
|
---|
6431 | rc = pTask->createThread();
|
---|
6432 |
|
---|
6433 | if (SUCCEEDED(rc))
|
---|
6434 | pProgress.queryInterfaceTo(aProgress);
|
---|
6435 | }
|
---|
6436 | else if (pTask != NULL)
|
---|
6437 | delete pTask;
|
---|
6438 |
|
---|
6439 | return rc;
|
---|
6440 | }
|
---|
6441 |
|
---|
6442 | /**
|
---|
6443 | * Returns the key identifier for this medium if encryption is configured.
|
---|
6444 | *
|
---|
6445 | * @returns Key identifier or empty string if no encryption is configured.
|
---|
6446 | */
|
---|
6447 | const Utf8Str& Medium::i_getKeyId()
|
---|
6448 | {
|
---|
6449 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
6450 |
|
---|
6451 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6452 |
|
---|
6453 | settings::StringsMap::const_iterator it = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
6454 | if (it == pBase->m->mapProperties.end())
|
---|
6455 | return Utf8Str::Empty;
|
---|
6456 |
|
---|
6457 | return it->second;
|
---|
6458 | }
|
---|
6459 |
|
---|
6460 | /**
|
---|
6461 | * Returns all filter related properties.
|
---|
6462 | *
|
---|
6463 | * @returns COM status code.
|
---|
6464 | * @param aReturnNames Where to store the properties names on success.
|
---|
6465 | * @param aReturnValues Where to store the properties values on success.
|
---|
6466 | */
|
---|
6467 | HRESULT Medium::i_getFilterProperties(std::vector<com::Utf8Str> &aReturnNames,
|
---|
6468 | std::vector<com::Utf8Str> &aReturnValues)
|
---|
6469 | {
|
---|
6470 | std::vector<com::Utf8Str> aPropNames;
|
---|
6471 | std::vector<com::Utf8Str> aPropValues;
|
---|
6472 | HRESULT hrc = getProperties(Utf8Str(""), aPropNames, aPropValues);
|
---|
6473 |
|
---|
6474 | if (SUCCEEDED(hrc))
|
---|
6475 | {
|
---|
6476 | unsigned cReturnSize = 0;
|
---|
6477 | aReturnNames.resize(0);
|
---|
6478 | aReturnValues.resize(0);
|
---|
6479 | for (unsigned idx = 0; idx < aPropNames.size(); idx++)
|
---|
6480 | {
|
---|
6481 | if (i_isPropertyForFilter(aPropNames[idx]))
|
---|
6482 | {
|
---|
6483 | aReturnNames.resize(cReturnSize + 1);
|
---|
6484 | aReturnValues.resize(cReturnSize + 1);
|
---|
6485 | aReturnNames[cReturnSize] = aPropNames[idx];
|
---|
6486 | aReturnValues[cReturnSize] = aPropValues[idx];
|
---|
6487 | cReturnSize++;
|
---|
6488 | }
|
---|
6489 | }
|
---|
6490 | }
|
---|
6491 |
|
---|
6492 | return hrc;
|
---|
6493 | }
|
---|
6494 |
|
---|
6495 | /**
|
---|
6496 | * Preparation to move this medium to a new location
|
---|
6497 | *
|
---|
6498 | * @param aLocation Location of the storage unit. If the location is a FS-path,
|
---|
6499 | * then it can be relative to the VirtualBox home directory.
|
---|
6500 | *
|
---|
6501 | * @note Must be called from under this object's write lock.
|
---|
6502 | */
|
---|
6503 | HRESULT Medium::i_preparationForMoving(const Utf8Str &aLocation)
|
---|
6504 | {
|
---|
6505 | HRESULT rc = E_FAIL;
|
---|
6506 |
|
---|
6507 | if (i_getLocationFull() != aLocation)
|
---|
6508 | {
|
---|
6509 | m->strNewLocationFull = aLocation;
|
---|
6510 | m->fMoveThisMedium = true;
|
---|
6511 | rc = S_OK;
|
---|
6512 | }
|
---|
6513 |
|
---|
6514 | return rc;
|
---|
6515 | }
|
---|
6516 |
|
---|
6517 | /**
|
---|
6518 | * Checking whether current operation "moving" or not
|
---|
6519 | */
|
---|
6520 | bool Medium::i_isMoveOperation(const ComObjPtr<Medium> &aTarget) const
|
---|
6521 | {
|
---|
6522 | RT_NOREF(aTarget);
|
---|
6523 | return (m->fMoveThisMedium == true) ? true:false;
|
---|
6524 | }
|
---|
6525 |
|
---|
6526 | bool Medium::i_resetMoveOperationData()
|
---|
6527 | {
|
---|
6528 | m->strNewLocationFull.setNull();
|
---|
6529 | m->fMoveThisMedium = false;
|
---|
6530 | return true;
|
---|
6531 | }
|
---|
6532 |
|
---|
6533 | Utf8Str Medium::i_getNewLocationForMoving() const
|
---|
6534 | {
|
---|
6535 | if (m->fMoveThisMedium == true)
|
---|
6536 | return m->strNewLocationFull;
|
---|
6537 | else
|
---|
6538 | return Utf8Str();
|
---|
6539 | }
|
---|
6540 | ////////////////////////////////////////////////////////////////////////////////
|
---|
6541 | //
|
---|
6542 | // Private methods
|
---|
6543 | //
|
---|
6544 | ////////////////////////////////////////////////////////////////////////////////
|
---|
6545 |
|
---|
6546 | /**
|
---|
6547 | * Queries information from the medium.
|
---|
6548 | *
|
---|
6549 | * As a result of this call, the accessibility state and data members such as
|
---|
6550 | * size and description will be updated with the current information.
|
---|
6551 | *
|
---|
6552 | * @note This method may block during a system I/O call that checks storage
|
---|
6553 | * accessibility.
|
---|
6554 | *
|
---|
6555 | * @note Caller MUST NOT hold the media tree or medium lock.
|
---|
6556 | *
|
---|
6557 | * @note Locks m->pParent for reading. Locks this object for writing.
|
---|
6558 | *
|
---|
6559 | * @param fSetImageId Whether to reset the UUID contained in the image file
|
---|
6560 | * to the UUID in the medium instance data (see SetIDs())
|
---|
6561 | * @param fSetParentId Whether to reset the parent UUID contained in the image
|
---|
6562 | * file to the parent UUID in the medium instance data (see
|
---|
6563 | * SetIDs())
|
---|
6564 | * @param autoCaller
|
---|
6565 | * @return
|
---|
6566 | */
|
---|
6567 | HRESULT Medium::i_queryInfo(bool fSetImageId, bool fSetParentId, AutoCaller &autoCaller)
|
---|
6568 | {
|
---|
6569 | Assert(!isWriteLockOnCurrentThread());
|
---|
6570 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6571 |
|
---|
6572 | if ( ( m->state != MediumState_Created
|
---|
6573 | && m->state != MediumState_Inaccessible
|
---|
6574 | && m->state != MediumState_LockedRead)
|
---|
6575 | || m->fClosing)
|
---|
6576 | return E_FAIL;
|
---|
6577 |
|
---|
6578 | HRESULT rc = S_OK;
|
---|
6579 |
|
---|
6580 | int vrc = VINF_SUCCESS;
|
---|
6581 |
|
---|
6582 | /* check if a blocking i_queryInfo() call is in progress on some other thread,
|
---|
6583 | * and wait for it to finish if so instead of querying data ourselves */
|
---|
6584 | if (m->queryInfoRunning)
|
---|
6585 | {
|
---|
6586 | Assert( m->state == MediumState_LockedRead
|
---|
6587 | || m->state == MediumState_LockedWrite);
|
---|
6588 |
|
---|
6589 | while (m->queryInfoRunning)
|
---|
6590 | {
|
---|
6591 | alock.release();
|
---|
6592 | /* must not hold the object lock now */
|
---|
6593 | Assert(!isWriteLockOnCurrentThread());
|
---|
6594 | {
|
---|
6595 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
6596 | }
|
---|
6597 | alock.acquire();
|
---|
6598 | }
|
---|
6599 |
|
---|
6600 | return S_OK;
|
---|
6601 | }
|
---|
6602 |
|
---|
6603 | bool success = false;
|
---|
6604 | Utf8Str lastAccessError;
|
---|
6605 |
|
---|
6606 | /* are we dealing with a new medium constructed using the existing
|
---|
6607 | * location? */
|
---|
6608 | bool isImport = m->id.isZero();
|
---|
6609 | unsigned uOpenFlags = VD_OPEN_FLAGS_INFO;
|
---|
6610 |
|
---|
6611 | /* Note that we don't use VD_OPEN_FLAGS_READONLY when opening new
|
---|
6612 | * media because that would prevent necessary modifications
|
---|
6613 | * when opening media of some third-party formats for the first
|
---|
6614 | * time in VirtualBox (such as VMDK for which VDOpen() needs to
|
---|
6615 | * generate an UUID if it is missing) */
|
---|
6616 | if ( m->hddOpenMode == OpenReadOnly
|
---|
6617 | || m->type == MediumType_Readonly
|
---|
6618 | || (!isImport && !fSetImageId && !fSetParentId)
|
---|
6619 | )
|
---|
6620 | uOpenFlags |= VD_OPEN_FLAGS_READONLY;
|
---|
6621 |
|
---|
6622 | /* Open shareable medium with the appropriate flags */
|
---|
6623 | if (m->type == MediumType_Shareable)
|
---|
6624 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
6625 |
|
---|
6626 | /* Lock the medium, which makes the behavior much more consistent, must be
|
---|
6627 | * done before dropping the object lock and setting queryInfoRunning. */
|
---|
6628 | ComPtr<IToken> pToken;
|
---|
6629 | if (uOpenFlags & (VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SHAREABLE))
|
---|
6630 | rc = LockRead(pToken.asOutParam());
|
---|
6631 | else
|
---|
6632 | rc = LockWrite(pToken.asOutParam());
|
---|
6633 | if (FAILED(rc)) return rc;
|
---|
6634 |
|
---|
6635 | /* Copies of the input state fields which are not read-only,
|
---|
6636 | * as we're dropping the lock. CAUTION: be extremely careful what
|
---|
6637 | * you do with the contents of this medium object, as you will
|
---|
6638 | * create races if there are concurrent changes. */
|
---|
6639 | Utf8Str format(m->strFormat);
|
---|
6640 | Utf8Str location(m->strLocationFull);
|
---|
6641 | ComObjPtr<MediumFormat> formatObj = m->formatObj;
|
---|
6642 |
|
---|
6643 | /* "Output" values which can't be set because the lock isn't held
|
---|
6644 | * at the time the values are determined. */
|
---|
6645 | Guid mediumId = m->id;
|
---|
6646 | uint64_t mediumSize = 0;
|
---|
6647 | uint64_t mediumLogicalSize = 0;
|
---|
6648 |
|
---|
6649 | /* Flag whether a base image has a non-zero parent UUID and thus
|
---|
6650 | * need repairing after it was closed again. */
|
---|
6651 | bool fRepairImageZeroParentUuid = false;
|
---|
6652 |
|
---|
6653 | ComObjPtr<VirtualBox> pVirtualBox = m->pVirtualBox;
|
---|
6654 |
|
---|
6655 | /* must be set before leaving the object lock the first time */
|
---|
6656 | m->queryInfoRunning = true;
|
---|
6657 |
|
---|
6658 | /* must leave object lock now, because a lock from a higher lock class
|
---|
6659 | * is needed and also a lengthy operation is coming */
|
---|
6660 | alock.release();
|
---|
6661 | autoCaller.release();
|
---|
6662 |
|
---|
6663 | /* Note that taking the queryInfoSem after leaving the object lock above
|
---|
6664 | * can lead to short spinning of the loops waiting for i_queryInfo() to
|
---|
6665 | * complete. This is unavoidable since the other order causes a lock order
|
---|
6666 | * violation: here it would be requesting the object lock (at the beginning
|
---|
6667 | * of the method), then queryInfoSem, and below the other way round. */
|
---|
6668 | AutoWriteLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
6669 |
|
---|
6670 | /* take the opportunity to have a media tree lock, released initially */
|
---|
6671 | Assert(!isWriteLockOnCurrentThread());
|
---|
6672 | Assert(!pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
6673 | AutoWriteLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
6674 | treeLock.release();
|
---|
6675 |
|
---|
6676 | /* re-take the caller, but not the object lock, to keep uninit away */
|
---|
6677 | autoCaller.add();
|
---|
6678 | if (FAILED(autoCaller.rc()))
|
---|
6679 | {
|
---|
6680 | m->queryInfoRunning = false;
|
---|
6681 | return autoCaller.rc();
|
---|
6682 | }
|
---|
6683 |
|
---|
6684 | try
|
---|
6685 | {
|
---|
6686 | /* skip accessibility checks for host drives */
|
---|
6687 | if (m->hostDrive)
|
---|
6688 | {
|
---|
6689 | success = true;
|
---|
6690 | throw S_OK;
|
---|
6691 | }
|
---|
6692 |
|
---|
6693 | PVDISK hdd;
|
---|
6694 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
6695 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6696 |
|
---|
6697 | try
|
---|
6698 | {
|
---|
6699 | /** @todo This kind of opening of media is assuming that diff
|
---|
6700 | * media can be opened as base media. Should be documented that
|
---|
6701 | * it must work for all medium format backends. */
|
---|
6702 | vrc = VDOpen(hdd,
|
---|
6703 | format.c_str(),
|
---|
6704 | location.c_str(),
|
---|
6705 | uOpenFlags | m->uOpenFlagsDef,
|
---|
6706 | m->vdImageIfaces);
|
---|
6707 | if (RT_FAILURE(vrc))
|
---|
6708 | {
|
---|
6709 | lastAccessError = Utf8StrFmt(tr("Could not open the medium '%s'%s"),
|
---|
6710 | location.c_str(), i_vdError(vrc).c_str());
|
---|
6711 | throw S_OK;
|
---|
6712 | }
|
---|
6713 |
|
---|
6714 | if (formatObj->i_getCapabilities() & MediumFormatCapabilities_Uuid)
|
---|
6715 | {
|
---|
6716 | /* Modify the UUIDs if necessary. The associated fields are
|
---|
6717 | * not modified by other code, so no need to copy. */
|
---|
6718 | if (fSetImageId)
|
---|
6719 | {
|
---|
6720 | alock.acquire();
|
---|
6721 | vrc = VDSetUuid(hdd, 0, m->uuidImage.raw());
|
---|
6722 | alock.release();
|
---|
6723 | if (RT_FAILURE(vrc))
|
---|
6724 | {
|
---|
6725 | lastAccessError = Utf8StrFmt(tr("Could not update the UUID of medium '%s'%s"),
|
---|
6726 | location.c_str(), i_vdError(vrc).c_str());
|
---|
6727 | throw S_OK;
|
---|
6728 | }
|
---|
6729 | mediumId = m->uuidImage;
|
---|
6730 | }
|
---|
6731 | if (fSetParentId)
|
---|
6732 | {
|
---|
6733 | alock.acquire();
|
---|
6734 | vrc = VDSetParentUuid(hdd, 0, m->uuidParentImage.raw());
|
---|
6735 | alock.release();
|
---|
6736 | if (RT_FAILURE(vrc))
|
---|
6737 | {
|
---|
6738 | lastAccessError = Utf8StrFmt(tr("Could not update the parent UUID of medium '%s'%s"),
|
---|
6739 | location.c_str(), i_vdError(vrc).c_str());
|
---|
6740 | throw S_OK;
|
---|
6741 | }
|
---|
6742 | }
|
---|
6743 | /* zap the information, these are no long-term members */
|
---|
6744 | alock.acquire();
|
---|
6745 | unconst(m->uuidImage).clear();
|
---|
6746 | unconst(m->uuidParentImage).clear();
|
---|
6747 | alock.release();
|
---|
6748 |
|
---|
6749 | /* check the UUID */
|
---|
6750 | RTUUID uuid;
|
---|
6751 | vrc = VDGetUuid(hdd, 0, &uuid);
|
---|
6752 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6753 |
|
---|
6754 | if (isImport)
|
---|
6755 | {
|
---|
6756 | mediumId = uuid;
|
---|
6757 |
|
---|
6758 | if (mediumId.isZero() && (m->hddOpenMode == OpenReadOnly))
|
---|
6759 | // only when importing a VDMK that has no UUID, create one in memory
|
---|
6760 | mediumId.create();
|
---|
6761 | }
|
---|
6762 | else
|
---|
6763 | {
|
---|
6764 | Assert(!mediumId.isZero());
|
---|
6765 |
|
---|
6766 | if (mediumId != uuid)
|
---|
6767 | {
|
---|
6768 | /** @todo r=klaus this always refers to VirtualBox.xml as the medium registry, even for new VMs */
|
---|
6769 | lastAccessError = Utf8StrFmt(
|
---|
6770 | tr("UUID {%RTuuid} of the medium '%s' does not match the value {%RTuuid} stored in the media registry ('%s')"),
|
---|
6771 | &uuid,
|
---|
6772 | location.c_str(),
|
---|
6773 | mediumId.raw(),
|
---|
6774 | pVirtualBox->i_settingsFilePath().c_str());
|
---|
6775 | throw S_OK;
|
---|
6776 | }
|
---|
6777 | }
|
---|
6778 | }
|
---|
6779 | else
|
---|
6780 | {
|
---|
6781 | /* the backend does not support storing UUIDs within the
|
---|
6782 | * underlying storage so use what we store in XML */
|
---|
6783 |
|
---|
6784 | if (fSetImageId)
|
---|
6785 | {
|
---|
6786 | /* set the UUID if an API client wants to change it */
|
---|
6787 | alock.acquire();
|
---|
6788 | mediumId = m->uuidImage;
|
---|
6789 | alock.release();
|
---|
6790 | }
|
---|
6791 | else if (isImport)
|
---|
6792 | {
|
---|
6793 | /* generate an UUID for an imported UUID-less medium */
|
---|
6794 | mediumId.create();
|
---|
6795 | }
|
---|
6796 | }
|
---|
6797 |
|
---|
6798 | /* set the image uuid before the below parent uuid handling code
|
---|
6799 | * might place it somewhere in the media tree, so that the medium
|
---|
6800 | * UUID is valid at this point */
|
---|
6801 | alock.acquire();
|
---|
6802 | if (isImport || fSetImageId)
|
---|
6803 | unconst(m->id) = mediumId;
|
---|
6804 | alock.release();
|
---|
6805 |
|
---|
6806 | /* get the medium variant */
|
---|
6807 | unsigned uImageFlags;
|
---|
6808 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
6809 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6810 | alock.acquire();
|
---|
6811 | m->variant = (MediumVariant_T)uImageFlags;
|
---|
6812 | alock.release();
|
---|
6813 |
|
---|
6814 | /* check/get the parent uuid and update corresponding state */
|
---|
6815 | if (uImageFlags & VD_IMAGE_FLAGS_DIFF)
|
---|
6816 | {
|
---|
6817 | RTUUID parentId;
|
---|
6818 | vrc = VDGetParentUuid(hdd, 0, &parentId);
|
---|
6819 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6820 |
|
---|
6821 | /* streamOptimized VMDK images are only accepted as base
|
---|
6822 | * images, as this allows automatic repair of OVF appliances.
|
---|
6823 | * Since such images don't support random writes they will not
|
---|
6824 | * be created for diff images. Only an overly smart user might
|
---|
6825 | * manually create this case. Too bad for him. */
|
---|
6826 | if ( (isImport || fSetParentId)
|
---|
6827 | && !(uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
|
---|
6828 | {
|
---|
6829 | /* the parent must be known to us. Note that we freely
|
---|
6830 | * call locking methods of mVirtualBox and parent, as all
|
---|
6831 | * relevant locks must be already held. There may be no
|
---|
6832 | * concurrent access to the just opened medium on other
|
---|
6833 | * threads yet (and init() will fail if this method reports
|
---|
6834 | * MediumState_Inaccessible) */
|
---|
6835 |
|
---|
6836 | ComObjPtr<Medium> pParent;
|
---|
6837 | if (RTUuidIsNull(&parentId))
|
---|
6838 | rc = VBOX_E_OBJECT_NOT_FOUND;
|
---|
6839 | else
|
---|
6840 | rc = pVirtualBox->i_findHardDiskById(Guid(parentId), false /* aSetError */, &pParent);
|
---|
6841 | if (FAILED(rc))
|
---|
6842 | {
|
---|
6843 | if (fSetImageId && !fSetParentId)
|
---|
6844 | {
|
---|
6845 | /* If the image UUID gets changed for an existing
|
---|
6846 | * image then the parent UUID can be stale. In such
|
---|
6847 | * cases clear the parent information. The parent
|
---|
6848 | * information may/will be re-set later if the
|
---|
6849 | * API client wants to adjust a complete medium
|
---|
6850 | * hierarchy one by one. */
|
---|
6851 | rc = S_OK;
|
---|
6852 | alock.acquire();
|
---|
6853 | RTUuidClear(&parentId);
|
---|
6854 | vrc = VDSetParentUuid(hdd, 0, &parentId);
|
---|
6855 | alock.release();
|
---|
6856 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6857 | }
|
---|
6858 | else
|
---|
6859 | {
|
---|
6860 | lastAccessError = Utf8StrFmt(tr("Parent medium with UUID {%RTuuid} of the medium '%s' is not found in the media registry ('%s')"),
|
---|
6861 | &parentId, location.c_str(),
|
---|
6862 | pVirtualBox->i_settingsFilePath().c_str());
|
---|
6863 | throw S_OK;
|
---|
6864 | }
|
---|
6865 | }
|
---|
6866 |
|
---|
6867 | /* must drop the caller before taking the tree lock */
|
---|
6868 | autoCaller.release();
|
---|
6869 | /* we set m->pParent & children() */
|
---|
6870 | treeLock.acquire();
|
---|
6871 | autoCaller.add();
|
---|
6872 | if (FAILED(autoCaller.rc()))
|
---|
6873 | throw autoCaller.rc();
|
---|
6874 |
|
---|
6875 | if (m->pParent)
|
---|
6876 | i_deparent();
|
---|
6877 |
|
---|
6878 | if (!pParent.isNull())
|
---|
6879 | if (pParent->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
6880 | {
|
---|
6881 | AutoReadLock plock(pParent COMMA_LOCKVAL_SRC_POS);
|
---|
6882 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
6883 | tr("Cannot open differencing image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
6884 | pParent->m->strLocationFull.c_str());
|
---|
6885 | }
|
---|
6886 | i_setParent(pParent);
|
---|
6887 |
|
---|
6888 | treeLock.release();
|
---|
6889 | }
|
---|
6890 | else
|
---|
6891 | {
|
---|
6892 | /* must drop the caller before taking the tree lock */
|
---|
6893 | autoCaller.release();
|
---|
6894 | /* we access m->pParent */
|
---|
6895 | treeLock.acquire();
|
---|
6896 | autoCaller.add();
|
---|
6897 | if (FAILED(autoCaller.rc()))
|
---|
6898 | throw autoCaller.rc();
|
---|
6899 |
|
---|
6900 | /* check that parent UUIDs match. Note that there's no need
|
---|
6901 | * for the parent's AutoCaller (our lifetime is bound to
|
---|
6902 | * it) */
|
---|
6903 |
|
---|
6904 | if (m->pParent.isNull())
|
---|
6905 | {
|
---|
6906 | /* Due to a bug in VDCopy() in VirtualBox 3.0.0-3.0.14
|
---|
6907 | * and 3.1.0-3.1.8 there are base images out there
|
---|
6908 | * which have a non-zero parent UUID. No point in
|
---|
6909 | * complaining about them, instead automatically
|
---|
6910 | * repair the problem. Later we can bring back the
|
---|
6911 | * error message, but we should wait until really
|
---|
6912 | * most users have repaired their images, either with
|
---|
6913 | * VBoxFixHdd or this way. */
|
---|
6914 | #if 1
|
---|
6915 | fRepairImageZeroParentUuid = true;
|
---|
6916 | #else /* 0 */
|
---|
6917 | lastAccessError = Utf8StrFmt(
|
---|
6918 | tr("Medium type of '%s' is differencing but it is not associated with any parent medium in the media registry ('%s')"),
|
---|
6919 | location.c_str(),
|
---|
6920 | pVirtualBox->settingsFilePath().c_str());
|
---|
6921 | treeLock.release();
|
---|
6922 | throw S_OK;
|
---|
6923 | #endif /* 0 */
|
---|
6924 | }
|
---|
6925 |
|
---|
6926 | {
|
---|
6927 | autoCaller.release();
|
---|
6928 | AutoReadLock parentLock(m->pParent COMMA_LOCKVAL_SRC_POS);
|
---|
6929 | autoCaller.add();
|
---|
6930 | if (FAILED(autoCaller.rc()))
|
---|
6931 | throw autoCaller.rc();
|
---|
6932 |
|
---|
6933 | if ( !fRepairImageZeroParentUuid
|
---|
6934 | && m->pParent->i_getState() != MediumState_Inaccessible
|
---|
6935 | && m->pParent->i_getId() != parentId)
|
---|
6936 | {
|
---|
6937 | /** @todo r=klaus this always refers to VirtualBox.xml as the medium registry, even for new VMs */
|
---|
6938 | lastAccessError = Utf8StrFmt(
|
---|
6939 | tr("Parent UUID {%RTuuid} of the medium '%s' does not match UUID {%RTuuid} of its parent medium stored in the media registry ('%s')"),
|
---|
6940 | &parentId, location.c_str(),
|
---|
6941 | m->pParent->i_getId().raw(),
|
---|
6942 | pVirtualBox->i_settingsFilePath().c_str());
|
---|
6943 | parentLock.release();
|
---|
6944 | treeLock.release();
|
---|
6945 | throw S_OK;
|
---|
6946 | }
|
---|
6947 | }
|
---|
6948 |
|
---|
6949 | /// @todo NEWMEDIA what to do if the parent is not
|
---|
6950 | /// accessible while the diff is? Probably nothing. The
|
---|
6951 | /// real code will detect the mismatch anyway.
|
---|
6952 |
|
---|
6953 | treeLock.release();
|
---|
6954 | }
|
---|
6955 | }
|
---|
6956 |
|
---|
6957 | mediumSize = VDGetFileSize(hdd, 0);
|
---|
6958 | mediumLogicalSize = VDGetSize(hdd, 0);
|
---|
6959 |
|
---|
6960 | success = true;
|
---|
6961 | }
|
---|
6962 | catch (HRESULT aRC)
|
---|
6963 | {
|
---|
6964 | rc = aRC;
|
---|
6965 | }
|
---|
6966 |
|
---|
6967 | vrc = VDDestroy(hdd);
|
---|
6968 | if (RT_FAILURE(vrc))
|
---|
6969 | {
|
---|
6970 | lastAccessError = Utf8StrFmt(tr("Could not update and close the medium '%s'%s"),
|
---|
6971 | location.c_str(), i_vdError(vrc).c_str());
|
---|
6972 | success = false;
|
---|
6973 | throw S_OK;
|
---|
6974 | }
|
---|
6975 | }
|
---|
6976 | catch (HRESULT aRC)
|
---|
6977 | {
|
---|
6978 | rc = aRC;
|
---|
6979 | }
|
---|
6980 |
|
---|
6981 | autoCaller.release();
|
---|
6982 | treeLock.acquire();
|
---|
6983 | autoCaller.add();
|
---|
6984 | if (FAILED(autoCaller.rc()))
|
---|
6985 | {
|
---|
6986 | m->queryInfoRunning = false;
|
---|
6987 | return autoCaller.rc();
|
---|
6988 | }
|
---|
6989 | alock.acquire();
|
---|
6990 |
|
---|
6991 | if (success)
|
---|
6992 | {
|
---|
6993 | m->size = mediumSize;
|
---|
6994 | m->logicalSize = mediumLogicalSize;
|
---|
6995 | m->strLastAccessError.setNull();
|
---|
6996 | }
|
---|
6997 | else
|
---|
6998 | {
|
---|
6999 | m->strLastAccessError = lastAccessError;
|
---|
7000 | Log1WarningFunc(("'%s' is not accessible (error='%s', rc=%Rhrc, vrc=%Rrc)\n",
|
---|
7001 | location.c_str(), m->strLastAccessError.c_str(), rc, vrc));
|
---|
7002 | }
|
---|
7003 |
|
---|
7004 | /* Set the proper state according to the result of the check */
|
---|
7005 | if (success)
|
---|
7006 | m->preLockState = MediumState_Created;
|
---|
7007 | else
|
---|
7008 | m->preLockState = MediumState_Inaccessible;
|
---|
7009 |
|
---|
7010 | /* unblock anyone waiting for the i_queryInfo results */
|
---|
7011 | qlock.release();
|
---|
7012 | m->queryInfoRunning = false;
|
---|
7013 |
|
---|
7014 | pToken->Abandon();
|
---|
7015 | pToken.setNull();
|
---|
7016 |
|
---|
7017 | if (FAILED(rc)) return rc;
|
---|
7018 |
|
---|
7019 | /* If this is a base image which incorrectly has a parent UUID set,
|
---|
7020 | * repair the image now by zeroing the parent UUID. This is only done
|
---|
7021 | * when we have structural information from a config file, on import
|
---|
7022 | * this is not possible. If someone would accidentally call openMedium
|
---|
7023 | * with a diff image before the base is registered this would destroy
|
---|
7024 | * the diff. Not acceptable. */
|
---|
7025 | if (fRepairImageZeroParentUuid)
|
---|
7026 | {
|
---|
7027 | rc = LockWrite(pToken.asOutParam());
|
---|
7028 | if (FAILED(rc)) return rc;
|
---|
7029 |
|
---|
7030 | alock.release();
|
---|
7031 |
|
---|
7032 | try
|
---|
7033 | {
|
---|
7034 | PVDISK hdd;
|
---|
7035 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
7036 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7037 |
|
---|
7038 | try
|
---|
7039 | {
|
---|
7040 | vrc = VDOpen(hdd,
|
---|
7041 | format.c_str(),
|
---|
7042 | location.c_str(),
|
---|
7043 | (uOpenFlags & ~VD_OPEN_FLAGS_READONLY) | m->uOpenFlagsDef,
|
---|
7044 | m->vdImageIfaces);
|
---|
7045 | if (RT_FAILURE(vrc))
|
---|
7046 | throw S_OK;
|
---|
7047 |
|
---|
7048 | RTUUID zeroParentUuid;
|
---|
7049 | RTUuidClear(&zeroParentUuid);
|
---|
7050 | vrc = VDSetParentUuid(hdd, 0, &zeroParentUuid);
|
---|
7051 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7052 | }
|
---|
7053 | catch (HRESULT aRC)
|
---|
7054 | {
|
---|
7055 | rc = aRC;
|
---|
7056 | }
|
---|
7057 |
|
---|
7058 | VDDestroy(hdd);
|
---|
7059 | }
|
---|
7060 | catch (HRESULT aRC)
|
---|
7061 | {
|
---|
7062 | rc = aRC;
|
---|
7063 | }
|
---|
7064 |
|
---|
7065 | pToken->Abandon();
|
---|
7066 | pToken.setNull();
|
---|
7067 | if (FAILED(rc)) return rc;
|
---|
7068 | }
|
---|
7069 |
|
---|
7070 | return rc;
|
---|
7071 | }
|
---|
7072 |
|
---|
7073 | /**
|
---|
7074 | * Performs extra checks if the medium can be closed and returns S_OK in
|
---|
7075 | * this case. Otherwise, returns a respective error message. Called by
|
---|
7076 | * Close() under the medium tree lock and the medium lock.
|
---|
7077 | *
|
---|
7078 | * @note Also reused by Medium::Reset().
|
---|
7079 | *
|
---|
7080 | * @note Caller must hold the media tree write lock!
|
---|
7081 | */
|
---|
7082 | HRESULT Medium::i_canClose()
|
---|
7083 | {
|
---|
7084 | Assert(m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
7085 |
|
---|
7086 | if (i_getChildren().size() != 0)
|
---|
7087 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
7088 | tr("Cannot close medium '%s' because it has %d child media"),
|
---|
7089 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
7090 |
|
---|
7091 | return S_OK;
|
---|
7092 | }
|
---|
7093 |
|
---|
7094 | /**
|
---|
7095 | * Unregisters this medium with mVirtualBox. Called by close() under the medium tree lock.
|
---|
7096 | *
|
---|
7097 | * @note Caller must have locked the media tree lock for writing!
|
---|
7098 | */
|
---|
7099 | HRESULT Medium::i_unregisterWithVirtualBox()
|
---|
7100 | {
|
---|
7101 | /* Note that we need to de-associate ourselves from the parent to let
|
---|
7102 | * VirtualBox::i_unregisterMedium() properly save the registry */
|
---|
7103 |
|
---|
7104 | /* we modify m->pParent and access children */
|
---|
7105 | Assert(m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
7106 |
|
---|
7107 | Medium *pParentBackup = m->pParent;
|
---|
7108 | AssertReturn(i_getChildren().size() == 0, E_FAIL);
|
---|
7109 | if (m->pParent)
|
---|
7110 | i_deparent();
|
---|
7111 |
|
---|
7112 | HRESULT rc = m->pVirtualBox->i_unregisterMedium(this);
|
---|
7113 | if (FAILED(rc))
|
---|
7114 | {
|
---|
7115 | if (pParentBackup)
|
---|
7116 | {
|
---|
7117 | // re-associate with the parent as we are still relatives in the registry
|
---|
7118 | i_setParent(pParentBackup);
|
---|
7119 | }
|
---|
7120 | }
|
---|
7121 |
|
---|
7122 | return rc;
|
---|
7123 | }
|
---|
7124 |
|
---|
7125 | /**
|
---|
7126 | * Like SetProperty but do not trigger a settings store. Only for internal use!
|
---|
7127 | */
|
---|
7128 | HRESULT Medium::i_setPropertyDirect(const Utf8Str &aName, const Utf8Str &aValue)
|
---|
7129 | {
|
---|
7130 | AutoCaller autoCaller(this);
|
---|
7131 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
7132 |
|
---|
7133 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7134 |
|
---|
7135 | switch (m->state)
|
---|
7136 | {
|
---|
7137 | case MediumState_Created:
|
---|
7138 | case MediumState_Inaccessible:
|
---|
7139 | break;
|
---|
7140 | default:
|
---|
7141 | return i_setStateError();
|
---|
7142 | }
|
---|
7143 |
|
---|
7144 | m->mapProperties[aName] = aValue;
|
---|
7145 |
|
---|
7146 | return S_OK;
|
---|
7147 | }
|
---|
7148 |
|
---|
7149 | /**
|
---|
7150 | * Sets the extended error info according to the current media state.
|
---|
7151 | *
|
---|
7152 | * @note Must be called from under this object's write or read lock.
|
---|
7153 | */
|
---|
7154 | HRESULT Medium::i_setStateError()
|
---|
7155 | {
|
---|
7156 | HRESULT rc = E_FAIL;
|
---|
7157 |
|
---|
7158 | switch (m->state)
|
---|
7159 | {
|
---|
7160 | case MediumState_NotCreated:
|
---|
7161 | {
|
---|
7162 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7163 | tr("Storage for the medium '%s' is not created"),
|
---|
7164 | m->strLocationFull.c_str());
|
---|
7165 | break;
|
---|
7166 | }
|
---|
7167 | case MediumState_Created:
|
---|
7168 | {
|
---|
7169 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7170 | tr("Storage for the medium '%s' is already created"),
|
---|
7171 | m->strLocationFull.c_str());
|
---|
7172 | break;
|
---|
7173 | }
|
---|
7174 | case MediumState_LockedRead:
|
---|
7175 | {
|
---|
7176 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7177 | tr("Medium '%s' is locked for reading by another task"),
|
---|
7178 | m->strLocationFull.c_str());
|
---|
7179 | break;
|
---|
7180 | }
|
---|
7181 | case MediumState_LockedWrite:
|
---|
7182 | {
|
---|
7183 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7184 | tr("Medium '%s' is locked for writing by another task"),
|
---|
7185 | m->strLocationFull.c_str());
|
---|
7186 | break;
|
---|
7187 | }
|
---|
7188 | case MediumState_Inaccessible:
|
---|
7189 | {
|
---|
7190 | /* be in sync with Console::powerUpThread() */
|
---|
7191 | if (!m->strLastAccessError.isEmpty())
|
---|
7192 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7193 | tr("Medium '%s' is not accessible. %s"),
|
---|
7194 | m->strLocationFull.c_str(), m->strLastAccessError.c_str());
|
---|
7195 | else
|
---|
7196 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7197 | tr("Medium '%s' is not accessible"),
|
---|
7198 | m->strLocationFull.c_str());
|
---|
7199 | break;
|
---|
7200 | }
|
---|
7201 | case MediumState_Creating:
|
---|
7202 | {
|
---|
7203 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7204 | tr("Storage for the medium '%s' is being created"),
|
---|
7205 | m->strLocationFull.c_str());
|
---|
7206 | break;
|
---|
7207 | }
|
---|
7208 | case MediumState_Deleting:
|
---|
7209 | {
|
---|
7210 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7211 | tr("Storage for the medium '%s' is being deleted"),
|
---|
7212 | m->strLocationFull.c_str());
|
---|
7213 | break;
|
---|
7214 | }
|
---|
7215 | default:
|
---|
7216 | {
|
---|
7217 | AssertFailed();
|
---|
7218 | break;
|
---|
7219 | }
|
---|
7220 | }
|
---|
7221 |
|
---|
7222 | return rc;
|
---|
7223 | }
|
---|
7224 |
|
---|
7225 | /**
|
---|
7226 | * Sets the value of m->strLocationFull. The given location must be a fully
|
---|
7227 | * qualified path; relative paths are not supported here.
|
---|
7228 | *
|
---|
7229 | * As a special exception, if the specified location is a file path that ends with '/'
|
---|
7230 | * then the file name part will be generated by this method automatically in the format
|
---|
7231 | * '{\<uuid\>}.\<ext\>' where \<uuid\> is a fresh UUID that this method will generate
|
---|
7232 | * and assign to this medium, and \<ext\> is the default extension for this
|
---|
7233 | * medium's storage format. Note that this procedure requires the media state to
|
---|
7234 | * be NotCreated and will return a failure otherwise.
|
---|
7235 | *
|
---|
7236 | * @param aLocation Location of the storage unit. If the location is a FS-path,
|
---|
7237 | * then it can be relative to the VirtualBox home directory.
|
---|
7238 | * @param aFormat Optional fallback format if it is an import and the format
|
---|
7239 | * cannot be determined.
|
---|
7240 | *
|
---|
7241 | * @note Must be called from under this object's write lock.
|
---|
7242 | */
|
---|
7243 | HRESULT Medium::i_setLocation(const Utf8Str &aLocation,
|
---|
7244 | const Utf8Str &aFormat /* = Utf8Str::Empty */)
|
---|
7245 | {
|
---|
7246 | AssertReturn(!aLocation.isEmpty(), E_FAIL);
|
---|
7247 |
|
---|
7248 | AutoCaller autoCaller(this);
|
---|
7249 | AssertComRCReturnRC(autoCaller.rc());
|
---|
7250 |
|
---|
7251 | /* formatObj may be null only when initializing from an existing path and
|
---|
7252 | * no format is known yet */
|
---|
7253 | AssertReturn( (!m->strFormat.isEmpty() && !m->formatObj.isNull())
|
---|
7254 | || ( getObjectState().getState() == ObjectState::InInit
|
---|
7255 | && m->state != MediumState_NotCreated
|
---|
7256 | && m->id.isZero()
|
---|
7257 | && m->strFormat.isEmpty()
|
---|
7258 | && m->formatObj.isNull()),
|
---|
7259 | E_FAIL);
|
---|
7260 |
|
---|
7261 | /* are we dealing with a new medium constructed using the existing
|
---|
7262 | * location? */
|
---|
7263 | bool isImport = m->strFormat.isEmpty();
|
---|
7264 |
|
---|
7265 | if ( isImport
|
---|
7266 | || ( (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
7267 | && !m->hostDrive))
|
---|
7268 | {
|
---|
7269 | Guid id;
|
---|
7270 |
|
---|
7271 | Utf8Str locationFull(aLocation);
|
---|
7272 |
|
---|
7273 | if (m->state == MediumState_NotCreated)
|
---|
7274 | {
|
---|
7275 | /* must be a file (formatObj must be already known) */
|
---|
7276 | Assert(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File);
|
---|
7277 |
|
---|
7278 | if (RTPathFilename(aLocation.c_str()) == NULL)
|
---|
7279 | {
|
---|
7280 | /* no file name is given (either an empty string or ends with a
|
---|
7281 | * slash), generate a new UUID + file name if the state allows
|
---|
7282 | * this */
|
---|
7283 |
|
---|
7284 | ComAssertMsgRet(!m->formatObj->i_getFileExtensions().empty(),
|
---|
7285 | ("Must be at least one extension if it is MediumFormatCapabilities_File\n"),
|
---|
7286 | E_FAIL);
|
---|
7287 |
|
---|
7288 | Utf8Str strExt = m->formatObj->i_getFileExtensions().front();
|
---|
7289 | ComAssertMsgRet(!strExt.isEmpty(),
|
---|
7290 | ("Default extension must not be empty\n"),
|
---|
7291 | E_FAIL);
|
---|
7292 |
|
---|
7293 | id.create();
|
---|
7294 |
|
---|
7295 | locationFull = Utf8StrFmt("%s{%RTuuid}.%s",
|
---|
7296 | aLocation.c_str(), id.raw(), strExt.c_str());
|
---|
7297 | }
|
---|
7298 | }
|
---|
7299 |
|
---|
7300 | // we must always have full paths now (if it refers to a file)
|
---|
7301 | if ( ( m->formatObj.isNull()
|
---|
7302 | || m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
7303 | && !RTPathStartsWithRoot(locationFull.c_str()))
|
---|
7304 | return setError(VBOX_E_FILE_ERROR,
|
---|
7305 | tr("The given path '%s' is not fully qualified"),
|
---|
7306 | locationFull.c_str());
|
---|
7307 |
|
---|
7308 | /* detect the backend from the storage unit if importing */
|
---|
7309 | if (isImport)
|
---|
7310 | {
|
---|
7311 | VDTYPE enmType = VDTYPE_INVALID;
|
---|
7312 | char *backendName = NULL;
|
---|
7313 |
|
---|
7314 | int vrc = VINF_SUCCESS;
|
---|
7315 |
|
---|
7316 | /* is it a file? */
|
---|
7317 | {
|
---|
7318 | RTFILE file;
|
---|
7319 | vrc = RTFileOpen(&file, locationFull.c_str(), RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
7320 | if (RT_SUCCESS(vrc))
|
---|
7321 | RTFileClose(file);
|
---|
7322 | }
|
---|
7323 | if (RT_SUCCESS(vrc))
|
---|
7324 | {
|
---|
7325 | vrc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
|
---|
7326 | locationFull.c_str(), &backendName, &enmType);
|
---|
7327 | }
|
---|
7328 | else if ( vrc != VERR_FILE_NOT_FOUND
|
---|
7329 | && vrc != VERR_PATH_NOT_FOUND
|
---|
7330 | && vrc != VERR_ACCESS_DENIED
|
---|
7331 | && locationFull != aLocation)
|
---|
7332 | {
|
---|
7333 | /* assume it's not a file, restore the original location */
|
---|
7334 | locationFull = aLocation;
|
---|
7335 | vrc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
|
---|
7336 | locationFull.c_str(), &backendName, &enmType);
|
---|
7337 | }
|
---|
7338 |
|
---|
7339 | if (RT_FAILURE(vrc))
|
---|
7340 | {
|
---|
7341 | if (vrc == VERR_ACCESS_DENIED)
|
---|
7342 | return setError(VBOX_E_FILE_ERROR,
|
---|
7343 | tr("Permission problem accessing the file for the medium '%s' (%Rrc)"),
|
---|
7344 | locationFull.c_str(), vrc);
|
---|
7345 | else if (vrc == VERR_FILE_NOT_FOUND || vrc == VERR_PATH_NOT_FOUND)
|
---|
7346 | return setError(VBOX_E_FILE_ERROR,
|
---|
7347 | tr("Could not find file for the medium '%s' (%Rrc)"),
|
---|
7348 | locationFull.c_str(), vrc);
|
---|
7349 | else if (aFormat.isEmpty())
|
---|
7350 | return setError(VBOX_E_IPRT_ERROR,
|
---|
7351 | tr("Could not get the storage format of the medium '%s' (%Rrc)"),
|
---|
7352 | locationFull.c_str(), vrc);
|
---|
7353 | else
|
---|
7354 | {
|
---|
7355 | HRESULT rc = i_setFormat(aFormat);
|
---|
7356 | /* setFormat() must not fail since we've just used the backend so
|
---|
7357 | * the format object must be there */
|
---|
7358 | AssertComRCReturnRC(rc);
|
---|
7359 | }
|
---|
7360 | }
|
---|
7361 | else if ( enmType == VDTYPE_INVALID
|
---|
7362 | || m->devType != i_convertToDeviceType(enmType))
|
---|
7363 | {
|
---|
7364 | /*
|
---|
7365 | * The user tried to use a image as a device which is not supported
|
---|
7366 | * by the backend.
|
---|
7367 | */
|
---|
7368 | return setError(E_FAIL,
|
---|
7369 | tr("The medium '%s' can't be used as the requested device type"),
|
---|
7370 | locationFull.c_str());
|
---|
7371 | }
|
---|
7372 | else
|
---|
7373 | {
|
---|
7374 | ComAssertRet(backendName != NULL && *backendName != '\0', E_FAIL);
|
---|
7375 |
|
---|
7376 | HRESULT rc = i_setFormat(backendName);
|
---|
7377 | RTStrFree(backendName);
|
---|
7378 |
|
---|
7379 | /* setFormat() must not fail since we've just used the backend so
|
---|
7380 | * the format object must be there */
|
---|
7381 | AssertComRCReturnRC(rc);
|
---|
7382 | }
|
---|
7383 | }
|
---|
7384 |
|
---|
7385 | m->strLocationFull = locationFull;
|
---|
7386 |
|
---|
7387 | /* is it still a file? */
|
---|
7388 | if ( (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
7389 | && (m->state == MediumState_NotCreated)
|
---|
7390 | )
|
---|
7391 | /* assign a new UUID (this UUID will be used when calling
|
---|
7392 | * VDCreateBase/VDCreateDiff as a wanted UUID). Note that we
|
---|
7393 | * also do that if we didn't generate it to make sure it is
|
---|
7394 | * either generated by us or reset to null */
|
---|
7395 | unconst(m->id) = id;
|
---|
7396 | }
|
---|
7397 | else
|
---|
7398 | m->strLocationFull = aLocation;
|
---|
7399 |
|
---|
7400 | return S_OK;
|
---|
7401 | }
|
---|
7402 |
|
---|
7403 | /**
|
---|
7404 | * Checks that the format ID is valid and sets it on success.
|
---|
7405 | *
|
---|
7406 | * Note that this method will caller-reference the format object on success!
|
---|
7407 | * This reference must be released somewhere to let the MediumFormat object be
|
---|
7408 | * uninitialized.
|
---|
7409 | *
|
---|
7410 | * @note Must be called from under this object's write lock.
|
---|
7411 | */
|
---|
7412 | HRESULT Medium::i_setFormat(const Utf8Str &aFormat)
|
---|
7413 | {
|
---|
7414 | /* get the format object first */
|
---|
7415 | {
|
---|
7416 | SystemProperties *pSysProps = m->pVirtualBox->i_getSystemProperties();
|
---|
7417 | AutoReadLock propsLock(pSysProps COMMA_LOCKVAL_SRC_POS);
|
---|
7418 |
|
---|
7419 | unconst(m->formatObj) = pSysProps->i_mediumFormat(aFormat);
|
---|
7420 | if (m->formatObj.isNull())
|
---|
7421 | return setError(E_INVALIDARG,
|
---|
7422 | tr("Invalid medium storage format '%s'"),
|
---|
7423 | aFormat.c_str());
|
---|
7424 |
|
---|
7425 | /* get properties (preinsert them as keys in the map). Note that the
|
---|
7426 | * map doesn't grow over the object life time since the set of
|
---|
7427 | * properties is meant to be constant. */
|
---|
7428 |
|
---|
7429 | Assert(m->mapProperties.empty());
|
---|
7430 |
|
---|
7431 | for (MediumFormat::PropertyArray::const_iterator it = m->formatObj->i_getProperties().begin();
|
---|
7432 | it != m->formatObj->i_getProperties().end();
|
---|
7433 | ++it)
|
---|
7434 | {
|
---|
7435 | m->mapProperties.insert(std::make_pair(it->strName, Utf8Str::Empty));
|
---|
7436 | }
|
---|
7437 | }
|
---|
7438 |
|
---|
7439 | unconst(m->strFormat) = aFormat;
|
---|
7440 |
|
---|
7441 | return S_OK;
|
---|
7442 | }
|
---|
7443 |
|
---|
7444 | /**
|
---|
7445 | * Converts the Medium device type to the VD type.
|
---|
7446 | */
|
---|
7447 | VDTYPE Medium::i_convertDeviceType()
|
---|
7448 | {
|
---|
7449 | VDTYPE enmType;
|
---|
7450 |
|
---|
7451 | switch (m->devType)
|
---|
7452 | {
|
---|
7453 | case DeviceType_HardDisk:
|
---|
7454 | enmType = VDTYPE_HDD;
|
---|
7455 | break;
|
---|
7456 | case DeviceType_DVD:
|
---|
7457 | enmType = VDTYPE_OPTICAL_DISC;
|
---|
7458 | break;
|
---|
7459 | case DeviceType_Floppy:
|
---|
7460 | enmType = VDTYPE_FLOPPY;
|
---|
7461 | break;
|
---|
7462 | default:
|
---|
7463 | ComAssertFailedRet(VDTYPE_INVALID);
|
---|
7464 | }
|
---|
7465 |
|
---|
7466 | return enmType;
|
---|
7467 | }
|
---|
7468 |
|
---|
7469 | /**
|
---|
7470 | * Converts from the VD type to the medium type.
|
---|
7471 | */
|
---|
7472 | DeviceType_T Medium::i_convertToDeviceType(VDTYPE enmType)
|
---|
7473 | {
|
---|
7474 | DeviceType_T devType;
|
---|
7475 |
|
---|
7476 | switch (enmType)
|
---|
7477 | {
|
---|
7478 | case VDTYPE_HDD:
|
---|
7479 | devType = DeviceType_HardDisk;
|
---|
7480 | break;
|
---|
7481 | case VDTYPE_OPTICAL_DISC:
|
---|
7482 | devType = DeviceType_DVD;
|
---|
7483 | break;
|
---|
7484 | case VDTYPE_FLOPPY:
|
---|
7485 | devType = DeviceType_Floppy;
|
---|
7486 | break;
|
---|
7487 | default:
|
---|
7488 | ComAssertFailedRet(DeviceType_Null);
|
---|
7489 | }
|
---|
7490 |
|
---|
7491 | return devType;
|
---|
7492 | }
|
---|
7493 |
|
---|
7494 | /**
|
---|
7495 | * Internal method which checks whether a property name is for a filter plugin.
|
---|
7496 | */
|
---|
7497 | bool Medium::i_isPropertyForFilter(const com::Utf8Str &aName)
|
---|
7498 | {
|
---|
7499 | /* If the name contains "/" use the part before as a filter name and lookup the filter. */
|
---|
7500 | size_t offSlash;
|
---|
7501 | if ((offSlash = aName.find("/", 0)) != aName.npos)
|
---|
7502 | {
|
---|
7503 | com::Utf8Str strFilter;
|
---|
7504 | com::Utf8Str strKey;
|
---|
7505 |
|
---|
7506 | HRESULT rc = strFilter.assignEx(aName, 0, offSlash);
|
---|
7507 | if (FAILED(rc))
|
---|
7508 | return false;
|
---|
7509 |
|
---|
7510 | rc = strKey.assignEx(aName, offSlash + 1, aName.length() - offSlash - 1); /* Skip slash */
|
---|
7511 | if (FAILED(rc))
|
---|
7512 | return false;
|
---|
7513 |
|
---|
7514 | VDFILTERINFO FilterInfo;
|
---|
7515 | int vrc = VDFilterInfoOne(strFilter.c_str(), &FilterInfo);
|
---|
7516 | if (RT_SUCCESS(vrc))
|
---|
7517 | {
|
---|
7518 | /* Check that the property exists. */
|
---|
7519 | PCVDCONFIGINFO paConfig = FilterInfo.paConfigInfo;
|
---|
7520 | while (paConfig->pszKey)
|
---|
7521 | {
|
---|
7522 | if (strKey.equals(paConfig->pszKey))
|
---|
7523 | return true;
|
---|
7524 | paConfig++;
|
---|
7525 | }
|
---|
7526 | }
|
---|
7527 | }
|
---|
7528 |
|
---|
7529 | return false;
|
---|
7530 | }
|
---|
7531 |
|
---|
7532 | /**
|
---|
7533 | * Returns the last error message collected by the i_vdErrorCall callback and
|
---|
7534 | * resets it.
|
---|
7535 | *
|
---|
7536 | * The error message is returned prepended with a dot and a space, like this:
|
---|
7537 | * <code>
|
---|
7538 | * ". <error_text> (%Rrc)"
|
---|
7539 | * </code>
|
---|
7540 | * to make it easily appendable to a more general error message. The @c %Rrc
|
---|
7541 | * format string is given @a aVRC as an argument.
|
---|
7542 | *
|
---|
7543 | * If there is no last error message collected by i_vdErrorCall or if it is a
|
---|
7544 | * null or empty string, then this function returns the following text:
|
---|
7545 | * <code>
|
---|
7546 | * " (%Rrc)"
|
---|
7547 | * </code>
|
---|
7548 | *
|
---|
7549 | * @note Doesn't do any object locking; it is assumed that the caller makes sure
|
---|
7550 | * the callback isn't called by more than one thread at a time.
|
---|
7551 | *
|
---|
7552 | * @param aVRC VBox error code to use when no error message is provided.
|
---|
7553 | */
|
---|
7554 | Utf8Str Medium::i_vdError(int aVRC)
|
---|
7555 | {
|
---|
7556 | Utf8Str error;
|
---|
7557 |
|
---|
7558 | if (m->vdError.isEmpty())
|
---|
7559 | error = Utf8StrFmt(" (%Rrc)", aVRC);
|
---|
7560 | else
|
---|
7561 | error = Utf8StrFmt(".\n%s", m->vdError.c_str());
|
---|
7562 |
|
---|
7563 | m->vdError.setNull();
|
---|
7564 |
|
---|
7565 | return error;
|
---|
7566 | }
|
---|
7567 |
|
---|
7568 | /**
|
---|
7569 | * Error message callback.
|
---|
7570 | *
|
---|
7571 | * Puts the reported error message to the m->vdError field.
|
---|
7572 | *
|
---|
7573 | * @note Doesn't do any object locking; it is assumed that the caller makes sure
|
---|
7574 | * the callback isn't called by more than one thread at a time.
|
---|
7575 | *
|
---|
7576 | * @param pvUser The opaque data passed on container creation.
|
---|
7577 | * @param rc The VBox error code.
|
---|
7578 | * @param SRC_POS Use RT_SRC_POS.
|
---|
7579 | * @param pszFormat Error message format string.
|
---|
7580 | * @param va Error message arguments.
|
---|
7581 | */
|
---|
7582 | /*static*/
|
---|
7583 | DECLCALLBACK(void) Medium::i_vdErrorCall(void *pvUser, int rc, RT_SRC_POS_DECL,
|
---|
7584 | const char *pszFormat, va_list va)
|
---|
7585 | {
|
---|
7586 | NOREF(pszFile); NOREF(iLine); NOREF(pszFunction); /* RT_SRC_POS_DECL */
|
---|
7587 |
|
---|
7588 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
7589 | AssertReturnVoid(that != NULL);
|
---|
7590 |
|
---|
7591 | if (that->m->vdError.isEmpty())
|
---|
7592 | that->m->vdError =
|
---|
7593 | Utf8StrFmt("%s (%Rrc)", Utf8Str(pszFormat, va).c_str(), rc);
|
---|
7594 | else
|
---|
7595 | that->m->vdError =
|
---|
7596 | Utf8StrFmt("%s.\n%s (%Rrc)", that->m->vdError.c_str(),
|
---|
7597 | Utf8Str(pszFormat, va).c_str(), rc);
|
---|
7598 | }
|
---|
7599 |
|
---|
7600 | /* static */
|
---|
7601 | DECLCALLBACK(bool) Medium::i_vdConfigAreKeysValid(void *pvUser,
|
---|
7602 | const char * /* pszzValid */)
|
---|
7603 | {
|
---|
7604 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
7605 | AssertReturn(that != NULL, false);
|
---|
7606 |
|
---|
7607 | /* we always return true since the only keys we have are those found in
|
---|
7608 | * VDBACKENDINFO */
|
---|
7609 | return true;
|
---|
7610 | }
|
---|
7611 |
|
---|
7612 | /* static */
|
---|
7613 | DECLCALLBACK(int) Medium::i_vdConfigQuerySize(void *pvUser,
|
---|
7614 | const char *pszName,
|
---|
7615 | size_t *pcbValue)
|
---|
7616 | {
|
---|
7617 | AssertReturn(VALID_PTR(pcbValue), VERR_INVALID_POINTER);
|
---|
7618 |
|
---|
7619 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
7620 | AssertReturn(that != NULL, VERR_GENERAL_FAILURE);
|
---|
7621 |
|
---|
7622 | settings::StringsMap::const_iterator it = that->m->mapProperties.find(Utf8Str(pszName));
|
---|
7623 | if (it == that->m->mapProperties.end())
|
---|
7624 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7625 |
|
---|
7626 | /* we interpret null values as "no value" in Medium */
|
---|
7627 | if (it->second.isEmpty())
|
---|
7628 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7629 |
|
---|
7630 | *pcbValue = it->second.length() + 1 /* include terminator */;
|
---|
7631 |
|
---|
7632 | return VINF_SUCCESS;
|
---|
7633 | }
|
---|
7634 |
|
---|
7635 | /* static */
|
---|
7636 | DECLCALLBACK(int) Medium::i_vdConfigQuery(void *pvUser,
|
---|
7637 | const char *pszName,
|
---|
7638 | char *pszValue,
|
---|
7639 | size_t cchValue)
|
---|
7640 | {
|
---|
7641 | AssertReturn(VALID_PTR(pszValue), VERR_INVALID_POINTER);
|
---|
7642 |
|
---|
7643 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
7644 | AssertReturn(that != NULL, VERR_GENERAL_FAILURE);
|
---|
7645 |
|
---|
7646 | settings::StringsMap::const_iterator it = that->m->mapProperties.find(Utf8Str(pszName));
|
---|
7647 | if (it == that->m->mapProperties.end())
|
---|
7648 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7649 |
|
---|
7650 | /* we interpret null values as "no value" in Medium */
|
---|
7651 | if (it->second.isEmpty())
|
---|
7652 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7653 |
|
---|
7654 | const Utf8Str &value = it->second;
|
---|
7655 | if (value.length() >= cchValue)
|
---|
7656 | return VERR_CFGM_NOT_ENOUGH_SPACE;
|
---|
7657 |
|
---|
7658 | memcpy(pszValue, value.c_str(), value.length() + 1);
|
---|
7659 |
|
---|
7660 | return VINF_SUCCESS;
|
---|
7661 | }
|
---|
7662 |
|
---|
7663 | DECLCALLBACK(int) Medium::i_vdTcpSocketCreate(uint32_t fFlags, PVDSOCKET pSock)
|
---|
7664 | {
|
---|
7665 | PVDSOCKETINT pSocketInt = NULL;
|
---|
7666 |
|
---|
7667 | if ((fFlags & VD_INTERFACETCPNET_CONNECT_EXTENDED_SELECT) != 0)
|
---|
7668 | return VERR_NOT_SUPPORTED;
|
---|
7669 |
|
---|
7670 | pSocketInt = (PVDSOCKETINT)RTMemAllocZ(sizeof(VDSOCKETINT));
|
---|
7671 | if (!pSocketInt)
|
---|
7672 | return VERR_NO_MEMORY;
|
---|
7673 |
|
---|
7674 | pSocketInt->hSocket = NIL_RTSOCKET;
|
---|
7675 | *pSock = pSocketInt;
|
---|
7676 | return VINF_SUCCESS;
|
---|
7677 | }
|
---|
7678 |
|
---|
7679 | DECLCALLBACK(int) Medium::i_vdTcpSocketDestroy(VDSOCKET Sock)
|
---|
7680 | {
|
---|
7681 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7682 |
|
---|
7683 | if (pSocketInt->hSocket != NIL_RTSOCKET)
|
---|
7684 | RTTcpClientCloseEx(pSocketInt->hSocket, false /*fGracefulShutdown*/);
|
---|
7685 |
|
---|
7686 | RTMemFree(pSocketInt);
|
---|
7687 |
|
---|
7688 | return VINF_SUCCESS;
|
---|
7689 | }
|
---|
7690 |
|
---|
7691 | DECLCALLBACK(int) Medium::i_vdTcpClientConnect(VDSOCKET Sock, const char *pszAddress, uint32_t uPort,
|
---|
7692 | RTMSINTERVAL cMillies)
|
---|
7693 | {
|
---|
7694 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7695 |
|
---|
7696 | return RTTcpClientConnectEx(pszAddress, uPort, &pSocketInt->hSocket, cMillies, NULL);
|
---|
7697 | }
|
---|
7698 |
|
---|
7699 | DECLCALLBACK(int) Medium::i_vdTcpClientClose(VDSOCKET Sock)
|
---|
7700 | {
|
---|
7701 | int rc = VINF_SUCCESS;
|
---|
7702 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7703 |
|
---|
7704 | rc = RTTcpClientCloseEx(pSocketInt->hSocket, false /*fGracefulShutdown*/);
|
---|
7705 | pSocketInt->hSocket = NIL_RTSOCKET;
|
---|
7706 | return rc;
|
---|
7707 | }
|
---|
7708 |
|
---|
7709 | DECLCALLBACK(bool) Medium::i_vdTcpIsClientConnected(VDSOCKET Sock)
|
---|
7710 | {
|
---|
7711 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7712 | return pSocketInt->hSocket != NIL_RTSOCKET;
|
---|
7713 | }
|
---|
7714 |
|
---|
7715 | DECLCALLBACK(int) Medium::i_vdTcpSelectOne(VDSOCKET Sock, RTMSINTERVAL cMillies)
|
---|
7716 | {
|
---|
7717 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7718 | return RTTcpSelectOne(pSocketInt->hSocket, cMillies);
|
---|
7719 | }
|
---|
7720 |
|
---|
7721 | DECLCALLBACK(int) Medium::i_vdTcpRead(VDSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead)
|
---|
7722 | {
|
---|
7723 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7724 | return RTTcpRead(pSocketInt->hSocket, pvBuffer, cbBuffer, pcbRead);
|
---|
7725 | }
|
---|
7726 |
|
---|
7727 | DECLCALLBACK(int) Medium::i_vdTcpWrite(VDSOCKET Sock, const void *pvBuffer, size_t cbBuffer)
|
---|
7728 | {
|
---|
7729 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7730 | return RTTcpWrite(pSocketInt->hSocket, pvBuffer, cbBuffer);
|
---|
7731 | }
|
---|
7732 |
|
---|
7733 | DECLCALLBACK(int) Medium::i_vdTcpSgWrite(VDSOCKET Sock, PCRTSGBUF pSgBuf)
|
---|
7734 | {
|
---|
7735 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7736 | return RTTcpSgWrite(pSocketInt->hSocket, pSgBuf);
|
---|
7737 | }
|
---|
7738 |
|
---|
7739 | DECLCALLBACK(int) Medium::i_vdTcpFlush(VDSOCKET Sock)
|
---|
7740 | {
|
---|
7741 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7742 | return RTTcpFlush(pSocketInt->hSocket);
|
---|
7743 | }
|
---|
7744 |
|
---|
7745 | DECLCALLBACK(int) Medium::i_vdTcpSetSendCoalescing(VDSOCKET Sock, bool fEnable)
|
---|
7746 | {
|
---|
7747 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7748 | return RTTcpSetSendCoalescing(pSocketInt->hSocket, fEnable);
|
---|
7749 | }
|
---|
7750 |
|
---|
7751 | DECLCALLBACK(int) Medium::i_vdTcpGetLocalAddress(VDSOCKET Sock, PRTNETADDR pAddr)
|
---|
7752 | {
|
---|
7753 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7754 | return RTTcpGetLocalAddress(pSocketInt->hSocket, pAddr);
|
---|
7755 | }
|
---|
7756 |
|
---|
7757 | DECLCALLBACK(int) Medium::i_vdTcpGetPeerAddress(VDSOCKET Sock, PRTNETADDR pAddr)
|
---|
7758 | {
|
---|
7759 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7760 | return RTTcpGetPeerAddress(pSocketInt->hSocket, pAddr);
|
---|
7761 | }
|
---|
7762 |
|
---|
7763 | DECLCALLBACK(bool) Medium::i_vdCryptoConfigAreKeysValid(void *pvUser, const char *pszzValid)
|
---|
7764 | {
|
---|
7765 | /* Just return always true here. */
|
---|
7766 | NOREF(pvUser);
|
---|
7767 | NOREF(pszzValid);
|
---|
7768 | return true;
|
---|
7769 | }
|
---|
7770 |
|
---|
7771 | DECLCALLBACK(int) Medium::i_vdCryptoConfigQuerySize(void *pvUser, const char *pszName, size_t *pcbValue)
|
---|
7772 | {
|
---|
7773 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7774 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7775 | AssertReturn(VALID_PTR(pcbValue), VERR_INVALID_POINTER);
|
---|
7776 |
|
---|
7777 | size_t cbValue = 0;
|
---|
7778 | if (!strcmp(pszName, "Algorithm"))
|
---|
7779 | cbValue = strlen(pSettings->pszCipher) + 1;
|
---|
7780 | else if (!strcmp(pszName, "KeyId"))
|
---|
7781 | cbValue = sizeof("irrelevant");
|
---|
7782 | else if (!strcmp(pszName, "KeyStore"))
|
---|
7783 | {
|
---|
7784 | if (!pSettings->pszKeyStoreLoad)
|
---|
7785 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7786 | cbValue = strlen(pSettings->pszKeyStoreLoad) + 1;
|
---|
7787 | }
|
---|
7788 | else if (!strcmp(pszName, "CreateKeyStore"))
|
---|
7789 | cbValue = 2; /* Single digit + terminator. */
|
---|
7790 | else
|
---|
7791 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7792 |
|
---|
7793 | *pcbValue = cbValue + 1 /* include terminator */;
|
---|
7794 |
|
---|
7795 | return VINF_SUCCESS;
|
---|
7796 | }
|
---|
7797 |
|
---|
7798 | DECLCALLBACK(int) Medium::i_vdCryptoConfigQuery(void *pvUser, const char *pszName,
|
---|
7799 | char *pszValue, size_t cchValue)
|
---|
7800 | {
|
---|
7801 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7802 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7803 | AssertReturn(VALID_PTR(pszValue), VERR_INVALID_POINTER);
|
---|
7804 |
|
---|
7805 | const char *psz = NULL;
|
---|
7806 | if (!strcmp(pszName, "Algorithm"))
|
---|
7807 | psz = pSettings->pszCipher;
|
---|
7808 | else if (!strcmp(pszName, "KeyId"))
|
---|
7809 | psz = "irrelevant";
|
---|
7810 | else if (!strcmp(pszName, "KeyStore"))
|
---|
7811 | psz = pSettings->pszKeyStoreLoad;
|
---|
7812 | else if (!strcmp(pszName, "CreateKeyStore"))
|
---|
7813 | {
|
---|
7814 | if (pSettings->fCreateKeyStore)
|
---|
7815 | psz = "1";
|
---|
7816 | else
|
---|
7817 | psz = "0";
|
---|
7818 | }
|
---|
7819 | else
|
---|
7820 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7821 |
|
---|
7822 | size_t cch = strlen(psz);
|
---|
7823 | if (cch >= cchValue)
|
---|
7824 | return VERR_CFGM_NOT_ENOUGH_SPACE;
|
---|
7825 |
|
---|
7826 | memcpy(pszValue, psz, cch + 1);
|
---|
7827 | return VINF_SUCCESS;
|
---|
7828 | }
|
---|
7829 |
|
---|
7830 | DECLCALLBACK(int) Medium::i_vdCryptoKeyRetain(void *pvUser, const char *pszId,
|
---|
7831 | const uint8_t **ppbKey, size_t *pcbKey)
|
---|
7832 | {
|
---|
7833 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7834 | NOREF(pszId);
|
---|
7835 | NOREF(ppbKey);
|
---|
7836 | NOREF(pcbKey);
|
---|
7837 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7838 | AssertMsgFailedReturn(("This method should not be called here!\n"), VERR_INVALID_STATE);
|
---|
7839 | }
|
---|
7840 |
|
---|
7841 | DECLCALLBACK(int) Medium::i_vdCryptoKeyRelease(void *pvUser, const char *pszId)
|
---|
7842 | {
|
---|
7843 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7844 | NOREF(pszId);
|
---|
7845 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7846 | AssertMsgFailedReturn(("This method should not be called here!\n"), VERR_INVALID_STATE);
|
---|
7847 | }
|
---|
7848 |
|
---|
7849 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStorePasswordRetain(void *pvUser, const char *pszId, const char **ppszPassword)
|
---|
7850 | {
|
---|
7851 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7852 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7853 |
|
---|
7854 | NOREF(pszId);
|
---|
7855 | *ppszPassword = pSettings->pszPassword;
|
---|
7856 | return VINF_SUCCESS;
|
---|
7857 | }
|
---|
7858 |
|
---|
7859 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStorePasswordRelease(void *pvUser, const char *pszId)
|
---|
7860 | {
|
---|
7861 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7862 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7863 | NOREF(pszId);
|
---|
7864 | return VINF_SUCCESS;
|
---|
7865 | }
|
---|
7866 |
|
---|
7867 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStoreSave(void *pvUser, const void *pvKeyStore, size_t cbKeyStore)
|
---|
7868 | {
|
---|
7869 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7870 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7871 |
|
---|
7872 | pSettings->pszKeyStore = (char *)RTMemAllocZ(cbKeyStore);
|
---|
7873 | if (!pSettings->pszKeyStore)
|
---|
7874 | return VERR_NO_MEMORY;
|
---|
7875 |
|
---|
7876 | memcpy(pSettings->pszKeyStore, pvKeyStore, cbKeyStore);
|
---|
7877 | return VINF_SUCCESS;
|
---|
7878 | }
|
---|
7879 |
|
---|
7880 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStoreReturnParameters(void *pvUser, const char *pszCipher,
|
---|
7881 | const uint8_t *pbDek, size_t cbDek)
|
---|
7882 | {
|
---|
7883 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7884 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7885 |
|
---|
7886 | pSettings->pszCipherReturned = RTStrDup(pszCipher);
|
---|
7887 | pSettings->pbDek = pbDek;
|
---|
7888 | pSettings->cbDek = cbDek;
|
---|
7889 |
|
---|
7890 | return pSettings->pszCipherReturned ? VINF_SUCCESS : VERR_NO_MEMORY;
|
---|
7891 | }
|
---|
7892 |
|
---|
7893 | /**
|
---|
7894 | * Creates a read-only VDISK instance for this medium.
|
---|
7895 | *
|
---|
7896 | * @note Caller should not hold any medium related locks as this method will
|
---|
7897 | * acquire the medium lock for writing and others (VirtualBox).
|
---|
7898 | *
|
---|
7899 | * @returns COM status code.
|
---|
7900 | * @param pKeyStore The key store.
|
---|
7901 | * @param ppHdd Where to return the pointer to the VDISK on
|
---|
7902 | * success.
|
---|
7903 | * @param pMediumLockList The lock list to populate and lock. Caller
|
---|
7904 | * is responsible for calling the destructor or
|
---|
7905 | * MediumLockList::Clear() after destroying
|
---|
7906 | * @a *ppHdd
|
---|
7907 | * @param pCryptoSettingsRead The crypto read settings to use for setting
|
---|
7908 | * up decryption of the VDISK. This object
|
---|
7909 | * must be alive until the VDISK is destroyed!
|
---|
7910 | */
|
---|
7911 | HRESULT Medium::i_openHddForReading(SecretKeyStore *pKeyStore, PVDISK *ppHdd, MediumLockList *pMediumLockList,
|
---|
7912 | Medium::CryptoFilterSettings *pCryptoSettingsRead)
|
---|
7913 | {
|
---|
7914 | /*
|
---|
7915 | * Create the media lock list and lock the media.
|
---|
7916 | */
|
---|
7917 | HRESULT hrc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
7918 | NULL /* pToLockWrite */,
|
---|
7919 | false /* fMediumLockWriteAll */,
|
---|
7920 | NULL,
|
---|
7921 | *pMediumLockList);
|
---|
7922 | if (SUCCEEDED(hrc))
|
---|
7923 | hrc = pMediumLockList->Lock();
|
---|
7924 | if (FAILED(hrc))
|
---|
7925 | return hrc;
|
---|
7926 |
|
---|
7927 | /*
|
---|
7928 | * Get the base medium before write locking this medium.
|
---|
7929 | */
|
---|
7930 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
7931 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7932 |
|
---|
7933 | /*
|
---|
7934 | * Create the VDISK instance.
|
---|
7935 | */
|
---|
7936 | PVDISK pHdd;
|
---|
7937 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pHdd);
|
---|
7938 | AssertRCReturn(vrc, E_FAIL);
|
---|
7939 |
|
---|
7940 | /*
|
---|
7941 | * Goto avoidance using try/catch/throw(HRESULT).
|
---|
7942 | */
|
---|
7943 | try
|
---|
7944 | {
|
---|
7945 | settings::StringsMap::iterator itKeyStore = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
7946 | if (itKeyStore != pBase->m->mapProperties.end())
|
---|
7947 | {
|
---|
7948 | settings::StringsMap::iterator itKeyId = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
7949 |
|
---|
7950 | #ifdef VBOX_WITH_EXTPACK
|
---|
7951 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
7952 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
7953 | {
|
---|
7954 | /* Load the plugin */
|
---|
7955 | Utf8Str strPlugin;
|
---|
7956 | hrc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
7957 | if (SUCCEEDED(hrc))
|
---|
7958 | {
|
---|
7959 | vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
7960 | if (RT_FAILURE(vrc))
|
---|
7961 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
7962 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
7963 | i_vdError(vrc).c_str());
|
---|
7964 | }
|
---|
7965 | else
|
---|
7966 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
7967 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
7968 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
7969 | }
|
---|
7970 | else
|
---|
7971 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
7972 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
7973 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
7974 | #else
|
---|
7975 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
7976 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
7977 | #endif
|
---|
7978 |
|
---|
7979 | if (itKeyId == pBase->m->mapProperties.end())
|
---|
7980 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7981 | tr("Image '%s' is configured for encryption but doesn't has a key identifier set"),
|
---|
7982 | pBase->m->strLocationFull.c_str());
|
---|
7983 |
|
---|
7984 | /* Find the proper secret key in the key store. */
|
---|
7985 | if (!pKeyStore)
|
---|
7986 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7987 | tr("Image '%s' is configured for encryption but there is no key store to retrieve the password from"),
|
---|
7988 | pBase->m->strLocationFull.c_str());
|
---|
7989 |
|
---|
7990 | SecretKey *pKey = NULL;
|
---|
7991 | vrc = pKeyStore->retainSecretKey(itKeyId->second, &pKey);
|
---|
7992 | if (RT_FAILURE(vrc))
|
---|
7993 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7994 | tr("Failed to retrieve the secret key with ID \"%s\" from the store (%Rrc)"),
|
---|
7995 | itKeyId->second.c_str(), vrc);
|
---|
7996 |
|
---|
7997 | i_taskEncryptSettingsSetup(pCryptoSettingsRead, NULL, itKeyStore->second.c_str(), (const char *)pKey->getKeyBuffer(),
|
---|
7998 | false /* fCreateKeyStore */);
|
---|
7999 | vrc = VDFilterAdd(pHdd, "CRYPT", VD_FILTER_FLAGS_READ, pCryptoSettingsRead->vdFilterIfaces);
|
---|
8000 | pKeyStore->releaseSecretKey(itKeyId->second);
|
---|
8001 | if (vrc == VERR_VD_PASSWORD_INCORRECT)
|
---|
8002 | throw setError(VBOX_E_PASSWORD_INCORRECT, tr("The password to decrypt the image is incorrect"));
|
---|
8003 | if (RT_FAILURE(vrc))
|
---|
8004 | throw setError(VBOX_E_INVALID_OBJECT_STATE, tr("Failed to load the decryption filter: %s"),
|
---|
8005 | i_vdError(vrc).c_str());
|
---|
8006 | }
|
---|
8007 |
|
---|
8008 | /*
|
---|
8009 | * Open all media in the source chain.
|
---|
8010 | */
|
---|
8011 | MediumLockList::Base::const_iterator sourceListBegin = pMediumLockList->GetBegin();
|
---|
8012 | MediumLockList::Base::const_iterator sourceListEnd = pMediumLockList->GetEnd();
|
---|
8013 | for (MediumLockList::Base::const_iterator it = sourceListBegin; it != sourceListEnd; ++it)
|
---|
8014 | {
|
---|
8015 | const MediumLock &mediumLock = *it;
|
---|
8016 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8017 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8018 |
|
---|
8019 | /* sanity check */
|
---|
8020 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
8021 |
|
---|
8022 | /* Open all media in read-only mode. */
|
---|
8023 | vrc = VDOpen(pHdd,
|
---|
8024 | pMedium->m->strFormat.c_str(),
|
---|
8025 | pMedium->m->strLocationFull.c_str(),
|
---|
8026 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
8027 | pMedium->m->vdImageIfaces);
|
---|
8028 | if (RT_FAILURE(vrc))
|
---|
8029 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8030 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8031 | pMedium->m->strLocationFull.c_str(),
|
---|
8032 | i_vdError(vrc).c_str());
|
---|
8033 | }
|
---|
8034 |
|
---|
8035 | Assert(m->state == MediumState_LockedRead);
|
---|
8036 |
|
---|
8037 | /*
|
---|
8038 | * Done!
|
---|
8039 | */
|
---|
8040 | *ppHdd = pHdd;
|
---|
8041 | return S_OK;
|
---|
8042 | }
|
---|
8043 | catch (HRESULT hrc2)
|
---|
8044 | {
|
---|
8045 | hrc = hrc2;
|
---|
8046 | }
|
---|
8047 |
|
---|
8048 | VDDestroy(pHdd);
|
---|
8049 | return hrc;
|
---|
8050 |
|
---|
8051 | }
|
---|
8052 |
|
---|
8053 | /**
|
---|
8054 | * Implementation code for the "create base" task.
|
---|
8055 | *
|
---|
8056 | * This only gets started from Medium::CreateBaseStorage() and always runs
|
---|
8057 | * asynchronously. As a result, we always save the VirtualBox.xml file when
|
---|
8058 | * we're done here.
|
---|
8059 | *
|
---|
8060 | * @param task
|
---|
8061 | * @return
|
---|
8062 | */
|
---|
8063 | HRESULT Medium::i_taskCreateBaseHandler(Medium::CreateBaseTask &task)
|
---|
8064 | {
|
---|
8065 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
8066 | * to lock order violations, it probably causes lock order issues related
|
---|
8067 | * to the AutoCaller usage. */
|
---|
8068 | HRESULT rc = S_OK;
|
---|
8069 |
|
---|
8070 | /* these parameters we need after creation */
|
---|
8071 | uint64_t size = 0, logicalSize = 0;
|
---|
8072 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
8073 | bool fGenerateUuid = false;
|
---|
8074 |
|
---|
8075 | try
|
---|
8076 | {
|
---|
8077 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8078 |
|
---|
8079 | /* The object may request a specific UUID (through a special form of
|
---|
8080 | * the setLocation() argument). Otherwise we have to generate it */
|
---|
8081 | Guid id = m->id;
|
---|
8082 |
|
---|
8083 | fGenerateUuid = id.isZero();
|
---|
8084 | if (fGenerateUuid)
|
---|
8085 | {
|
---|
8086 | id.create();
|
---|
8087 | /* VirtualBox::i_registerMedium() will need UUID */
|
---|
8088 | unconst(m->id) = id;
|
---|
8089 | }
|
---|
8090 |
|
---|
8091 | Utf8Str format(m->strFormat);
|
---|
8092 | Utf8Str location(m->strLocationFull);
|
---|
8093 | uint64_t capabilities = m->formatObj->i_getCapabilities();
|
---|
8094 | ComAssertThrow(capabilities & ( MediumFormatCapabilities_CreateFixed
|
---|
8095 | | MediumFormatCapabilities_CreateDynamic), E_FAIL);
|
---|
8096 | Assert(m->state == MediumState_Creating);
|
---|
8097 |
|
---|
8098 | PVDISK hdd;
|
---|
8099 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8100 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8101 |
|
---|
8102 | /* unlock before the potentially lengthy operation */
|
---|
8103 | thisLock.release();
|
---|
8104 |
|
---|
8105 | try
|
---|
8106 | {
|
---|
8107 | /* ensure the directory exists */
|
---|
8108 | if (capabilities & MediumFormatCapabilities_File)
|
---|
8109 | {
|
---|
8110 | rc = VirtualBox::i_ensureFilePathExists(location, !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
8111 | if (FAILED(rc))
|
---|
8112 | throw rc;
|
---|
8113 | }
|
---|
8114 |
|
---|
8115 | VDGEOMETRY geo = { 0, 0, 0 }; /* auto-detect */
|
---|
8116 |
|
---|
8117 | vrc = VDCreateBase(hdd,
|
---|
8118 | format.c_str(),
|
---|
8119 | location.c_str(),
|
---|
8120 | task.mSize,
|
---|
8121 | task.mVariant & ~MediumVariant_NoCreateDir,
|
---|
8122 | NULL,
|
---|
8123 | &geo,
|
---|
8124 | &geo,
|
---|
8125 | id.raw(),
|
---|
8126 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
8127 | m->vdImageIfaces,
|
---|
8128 | task.mVDOperationIfaces);
|
---|
8129 | if (RT_FAILURE(vrc))
|
---|
8130 | {
|
---|
8131 | if (vrc == VERR_VD_INVALID_TYPE)
|
---|
8132 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8133 | tr("Parameters for creating the medium storage unit '%s' are invalid%s"),
|
---|
8134 | location.c_str(), i_vdError(vrc).c_str());
|
---|
8135 | else
|
---|
8136 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8137 | tr("Could not create the medium storage unit '%s'%s"),
|
---|
8138 | location.c_str(), i_vdError(vrc).c_str());
|
---|
8139 | }
|
---|
8140 |
|
---|
8141 | size = VDGetFileSize(hdd, 0);
|
---|
8142 | logicalSize = VDGetSize(hdd, 0);
|
---|
8143 | unsigned uImageFlags;
|
---|
8144 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
8145 | if (RT_SUCCESS(vrc))
|
---|
8146 | variant = (MediumVariant_T)uImageFlags;
|
---|
8147 | }
|
---|
8148 | catch (HRESULT aRC) { rc = aRC; }
|
---|
8149 |
|
---|
8150 | VDDestroy(hdd);
|
---|
8151 | }
|
---|
8152 | catch (HRESULT aRC) { rc = aRC; }
|
---|
8153 |
|
---|
8154 | if (SUCCEEDED(rc))
|
---|
8155 | {
|
---|
8156 | /* register with mVirtualBox as the last step and move to
|
---|
8157 | * Created state only on success (leaving an orphan file is
|
---|
8158 | * better than breaking media registry consistency) */
|
---|
8159 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
8160 | ComObjPtr<Medium> pMedium;
|
---|
8161 | rc = m->pVirtualBox->i_registerMedium(this, &pMedium, treeLock);
|
---|
8162 | Assert(pMedium == NULL || this == pMedium);
|
---|
8163 | }
|
---|
8164 |
|
---|
8165 | // re-acquire the lock before changing state
|
---|
8166 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8167 |
|
---|
8168 | if (SUCCEEDED(rc))
|
---|
8169 | {
|
---|
8170 | m->state = MediumState_Created;
|
---|
8171 |
|
---|
8172 | m->size = size;
|
---|
8173 | m->logicalSize = logicalSize;
|
---|
8174 | m->variant = variant;
|
---|
8175 |
|
---|
8176 | thisLock.release();
|
---|
8177 | i_markRegistriesModified();
|
---|
8178 | if (task.isAsync())
|
---|
8179 | {
|
---|
8180 | // in asynchronous mode, save settings now
|
---|
8181 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
8182 | }
|
---|
8183 | }
|
---|
8184 | else
|
---|
8185 | {
|
---|
8186 | /* back to NotCreated on failure */
|
---|
8187 | m->state = MediumState_NotCreated;
|
---|
8188 |
|
---|
8189 | /* reset UUID to prevent it from being reused next time */
|
---|
8190 | if (fGenerateUuid)
|
---|
8191 | unconst(m->id).clear();
|
---|
8192 | }
|
---|
8193 |
|
---|
8194 | return rc;
|
---|
8195 | }
|
---|
8196 |
|
---|
8197 | /**
|
---|
8198 | * Implementation code for the "create diff" task.
|
---|
8199 | *
|
---|
8200 | * This task always gets started from Medium::createDiffStorage() and can run
|
---|
8201 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
8202 | * that function. If we run synchronously, the caller expects the medium
|
---|
8203 | * registry modification to be set before returning; otherwise (in asynchronous
|
---|
8204 | * mode), we save the settings ourselves.
|
---|
8205 | *
|
---|
8206 | * @param task
|
---|
8207 | * @return
|
---|
8208 | */
|
---|
8209 | HRESULT Medium::i_taskCreateDiffHandler(Medium::CreateDiffTask &task)
|
---|
8210 | {
|
---|
8211 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
8212 | * to lock order violations, it probably causes lock order issues related
|
---|
8213 | * to the AutoCaller usage. */
|
---|
8214 | HRESULT rcTmp = S_OK;
|
---|
8215 |
|
---|
8216 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
8217 |
|
---|
8218 | uint64_t size = 0, logicalSize = 0;
|
---|
8219 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
8220 | bool fGenerateUuid = false;
|
---|
8221 |
|
---|
8222 | try
|
---|
8223 | {
|
---|
8224 | if (i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
8225 | {
|
---|
8226 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8227 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8228 | tr("Cannot create differencing image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
8229 | m->strLocationFull.c_str());
|
---|
8230 | }
|
---|
8231 |
|
---|
8232 | /* Lock both in {parent,child} order. */
|
---|
8233 | AutoMultiWriteLock2 mediaLock(this, pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8234 |
|
---|
8235 | /* The object may request a specific UUID (through a special form of
|
---|
8236 | * the setLocation() argument). Otherwise we have to generate it */
|
---|
8237 | Guid targetId = pTarget->m->id;
|
---|
8238 |
|
---|
8239 | fGenerateUuid = targetId.isZero();
|
---|
8240 | if (fGenerateUuid)
|
---|
8241 | {
|
---|
8242 | targetId.create();
|
---|
8243 | /* VirtualBox::i_registerMedium() will need UUID */
|
---|
8244 | unconst(pTarget->m->id) = targetId;
|
---|
8245 | }
|
---|
8246 |
|
---|
8247 | Guid id = m->id;
|
---|
8248 |
|
---|
8249 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
8250 | Utf8Str targetLocation(pTarget->m->strLocationFull);
|
---|
8251 | uint64_t capabilities = pTarget->m->formatObj->i_getCapabilities();
|
---|
8252 | ComAssertThrow(capabilities & MediumFormatCapabilities_CreateDynamic, E_FAIL);
|
---|
8253 |
|
---|
8254 | Assert(pTarget->m->state == MediumState_Creating);
|
---|
8255 | Assert(m->state == MediumState_LockedRead);
|
---|
8256 |
|
---|
8257 | PVDISK hdd;
|
---|
8258 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8259 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8260 |
|
---|
8261 | /* the two media are now protected by their non-default states;
|
---|
8262 | * unlock the media before the potentially lengthy operation */
|
---|
8263 | mediaLock.release();
|
---|
8264 |
|
---|
8265 | try
|
---|
8266 | {
|
---|
8267 | /* Open all media in the target chain but the last. */
|
---|
8268 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
8269 | task.mpMediumLockList->GetBegin();
|
---|
8270 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
8271 | task.mpMediumLockList->GetEnd();
|
---|
8272 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
8273 | it != targetListEnd;
|
---|
8274 | ++it)
|
---|
8275 | {
|
---|
8276 | const MediumLock &mediumLock = *it;
|
---|
8277 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8278 |
|
---|
8279 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8280 |
|
---|
8281 | /* Skip over the target diff medium */
|
---|
8282 | if (pMedium->m->state == MediumState_Creating)
|
---|
8283 | continue;
|
---|
8284 |
|
---|
8285 | /* sanity check */
|
---|
8286 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
8287 |
|
---|
8288 | /* Open all media in appropriate mode. */
|
---|
8289 | vrc = VDOpen(hdd,
|
---|
8290 | pMedium->m->strFormat.c_str(),
|
---|
8291 | pMedium->m->strLocationFull.c_str(),
|
---|
8292 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
8293 | pMedium->m->vdImageIfaces);
|
---|
8294 | if (RT_FAILURE(vrc))
|
---|
8295 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8296 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8297 | pMedium->m->strLocationFull.c_str(),
|
---|
8298 | i_vdError(vrc).c_str());
|
---|
8299 | }
|
---|
8300 |
|
---|
8301 | /* ensure the target directory exists */
|
---|
8302 | if (capabilities & MediumFormatCapabilities_File)
|
---|
8303 | {
|
---|
8304 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
8305 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
8306 | if (FAILED(rc))
|
---|
8307 | throw rc;
|
---|
8308 | }
|
---|
8309 |
|
---|
8310 | vrc = VDCreateDiff(hdd,
|
---|
8311 | targetFormat.c_str(),
|
---|
8312 | targetLocation.c_str(),
|
---|
8313 | (task.mVariant & ~(MediumVariant_NoCreateDir | MediumVariant_VmdkESX)) | VD_IMAGE_FLAGS_DIFF,
|
---|
8314 | NULL,
|
---|
8315 | targetId.raw(),
|
---|
8316 | id.raw(),
|
---|
8317 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
8318 | pTarget->m->vdImageIfaces,
|
---|
8319 | task.mVDOperationIfaces);
|
---|
8320 | if (RT_FAILURE(vrc))
|
---|
8321 | {
|
---|
8322 | if (vrc == VERR_VD_INVALID_TYPE)
|
---|
8323 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8324 | tr("Parameters for creating the differencing medium storage unit '%s' are invalid%s"),
|
---|
8325 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
8326 | else
|
---|
8327 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8328 | tr("Could not create the differencing medium storage unit '%s'%s"),
|
---|
8329 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
8330 | }
|
---|
8331 |
|
---|
8332 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
8333 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
8334 | unsigned uImageFlags;
|
---|
8335 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
8336 | if (RT_SUCCESS(vrc))
|
---|
8337 | variant = (MediumVariant_T)uImageFlags;
|
---|
8338 | }
|
---|
8339 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8340 |
|
---|
8341 | VDDestroy(hdd);
|
---|
8342 | }
|
---|
8343 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8344 |
|
---|
8345 | MultiResult mrc(rcTmp);
|
---|
8346 |
|
---|
8347 | if (SUCCEEDED(mrc))
|
---|
8348 | {
|
---|
8349 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
8350 |
|
---|
8351 | Assert(pTarget->m->pParent.isNull());
|
---|
8352 |
|
---|
8353 | /* associate child with the parent, maximum depth was checked above */
|
---|
8354 | pTarget->i_setParent(this);
|
---|
8355 |
|
---|
8356 | /* diffs for immutable media are auto-reset by default */
|
---|
8357 | bool fAutoReset;
|
---|
8358 | {
|
---|
8359 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
8360 | AutoReadLock block(pBase COMMA_LOCKVAL_SRC_POS);
|
---|
8361 | fAutoReset = (pBase->m->type == MediumType_Immutable);
|
---|
8362 | }
|
---|
8363 | {
|
---|
8364 | AutoWriteLock tlock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8365 | pTarget->m->autoReset = fAutoReset;
|
---|
8366 | }
|
---|
8367 |
|
---|
8368 | /* register with mVirtualBox as the last step and move to
|
---|
8369 | * Created state only on success (leaving an orphan file is
|
---|
8370 | * better than breaking media registry consistency) */
|
---|
8371 | ComObjPtr<Medium> pMedium;
|
---|
8372 | mrc = m->pVirtualBox->i_registerMedium(pTarget, &pMedium, treeLock);
|
---|
8373 | Assert(pTarget == pMedium);
|
---|
8374 |
|
---|
8375 | if (FAILED(mrc))
|
---|
8376 | /* break the parent association on failure to register */
|
---|
8377 | i_deparent();
|
---|
8378 | }
|
---|
8379 |
|
---|
8380 | AutoMultiWriteLock2 mediaLock(this, pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8381 |
|
---|
8382 | if (SUCCEEDED(mrc))
|
---|
8383 | {
|
---|
8384 | pTarget->m->state = MediumState_Created;
|
---|
8385 |
|
---|
8386 | pTarget->m->size = size;
|
---|
8387 | pTarget->m->logicalSize = logicalSize;
|
---|
8388 | pTarget->m->variant = variant;
|
---|
8389 | }
|
---|
8390 | else
|
---|
8391 | {
|
---|
8392 | /* back to NotCreated on failure */
|
---|
8393 | pTarget->m->state = MediumState_NotCreated;
|
---|
8394 |
|
---|
8395 | pTarget->m->autoReset = false;
|
---|
8396 |
|
---|
8397 | /* reset UUID to prevent it from being reused next time */
|
---|
8398 | if (fGenerateUuid)
|
---|
8399 | unconst(pTarget->m->id).clear();
|
---|
8400 | }
|
---|
8401 |
|
---|
8402 | // deregister the task registered in createDiffStorage()
|
---|
8403 | Assert(m->numCreateDiffTasks != 0);
|
---|
8404 | --m->numCreateDiffTasks;
|
---|
8405 |
|
---|
8406 | mediaLock.release();
|
---|
8407 | i_markRegistriesModified();
|
---|
8408 | if (task.isAsync())
|
---|
8409 | {
|
---|
8410 | // in asynchronous mode, save settings now
|
---|
8411 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
8412 | }
|
---|
8413 |
|
---|
8414 | /* Note that in sync mode, it's the caller's responsibility to
|
---|
8415 | * unlock the medium. */
|
---|
8416 |
|
---|
8417 | return mrc;
|
---|
8418 | }
|
---|
8419 |
|
---|
8420 | /**
|
---|
8421 | * Implementation code for the "merge" task.
|
---|
8422 | *
|
---|
8423 | * This task always gets started from Medium::mergeTo() and can run
|
---|
8424 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
8425 | * that function. If we run synchronously, the caller expects the medium
|
---|
8426 | * registry modification to be set before returning; otherwise (in asynchronous
|
---|
8427 | * mode), we save the settings ourselves.
|
---|
8428 | *
|
---|
8429 | * @param task
|
---|
8430 | * @return
|
---|
8431 | */
|
---|
8432 | HRESULT Medium::i_taskMergeHandler(Medium::MergeTask &task)
|
---|
8433 | {
|
---|
8434 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
8435 | * to lock order violations, it probably causes lock order issues related
|
---|
8436 | * to the AutoCaller usage. */
|
---|
8437 | HRESULT rcTmp = S_OK;
|
---|
8438 |
|
---|
8439 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
8440 |
|
---|
8441 | try
|
---|
8442 | {
|
---|
8443 | if (!task.mParentForTarget.isNull())
|
---|
8444 | if (task.mParentForTarget->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
8445 | {
|
---|
8446 | AutoReadLock plock(task.mParentForTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8447 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8448 | tr("Cannot merge image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
8449 | task.mParentForTarget->m->strLocationFull.c_str());
|
---|
8450 | }
|
---|
8451 |
|
---|
8452 | PVDISK hdd;
|
---|
8453 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8454 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8455 |
|
---|
8456 | try
|
---|
8457 | {
|
---|
8458 | // Similar code appears in SessionMachine::onlineMergeMedium, so
|
---|
8459 | // if you make any changes below check whether they are applicable
|
---|
8460 | // in that context as well.
|
---|
8461 |
|
---|
8462 | unsigned uTargetIdx = VD_LAST_IMAGE;
|
---|
8463 | unsigned uSourceIdx = VD_LAST_IMAGE;
|
---|
8464 | /* Open all media in the chain. */
|
---|
8465 | MediumLockList::Base::iterator lockListBegin =
|
---|
8466 | task.mpMediumLockList->GetBegin();
|
---|
8467 | MediumLockList::Base::iterator lockListEnd =
|
---|
8468 | task.mpMediumLockList->GetEnd();
|
---|
8469 | unsigned i = 0;
|
---|
8470 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
8471 | it != lockListEnd;
|
---|
8472 | ++it)
|
---|
8473 | {
|
---|
8474 | MediumLock &mediumLock = *it;
|
---|
8475 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8476 |
|
---|
8477 | if (pMedium == this)
|
---|
8478 | uSourceIdx = i;
|
---|
8479 | else if (pMedium == pTarget)
|
---|
8480 | uTargetIdx = i;
|
---|
8481 |
|
---|
8482 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8483 |
|
---|
8484 | /*
|
---|
8485 | * complex sanity (sane complexity)
|
---|
8486 | *
|
---|
8487 | * The current medium must be in the Deleting (medium is merged)
|
---|
8488 | * or LockedRead (parent medium) state if it is not the target.
|
---|
8489 | * If it is the target it must be in the LockedWrite state.
|
---|
8490 | */
|
---|
8491 | Assert( ( pMedium != pTarget
|
---|
8492 | && ( pMedium->m->state == MediumState_Deleting
|
---|
8493 | || pMedium->m->state == MediumState_LockedRead))
|
---|
8494 | || ( pMedium == pTarget
|
---|
8495 | && pMedium->m->state == MediumState_LockedWrite));
|
---|
8496 | /*
|
---|
8497 | * Medium must be the target, in the LockedRead state
|
---|
8498 | * or Deleting state where it is not allowed to be attached
|
---|
8499 | * to a virtual machine.
|
---|
8500 | */
|
---|
8501 | Assert( pMedium == pTarget
|
---|
8502 | || pMedium->m->state == MediumState_LockedRead
|
---|
8503 | || ( pMedium->m->backRefs.size() == 0
|
---|
8504 | && pMedium->m->state == MediumState_Deleting));
|
---|
8505 | /* The source medium must be in Deleting state. */
|
---|
8506 | Assert( pMedium != this
|
---|
8507 | || pMedium->m->state == MediumState_Deleting);
|
---|
8508 |
|
---|
8509 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
8510 |
|
---|
8511 | if ( pMedium->m->state == MediumState_LockedRead
|
---|
8512 | || pMedium->m->state == MediumState_Deleting)
|
---|
8513 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
8514 | if (pMedium->m->type == MediumType_Shareable)
|
---|
8515 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
8516 |
|
---|
8517 | /* Open the medium */
|
---|
8518 | vrc = VDOpen(hdd,
|
---|
8519 | pMedium->m->strFormat.c_str(),
|
---|
8520 | pMedium->m->strLocationFull.c_str(),
|
---|
8521 | uOpenFlags | m->uOpenFlagsDef,
|
---|
8522 | pMedium->m->vdImageIfaces);
|
---|
8523 | if (RT_FAILURE(vrc))
|
---|
8524 | throw vrc;
|
---|
8525 |
|
---|
8526 | i++;
|
---|
8527 | }
|
---|
8528 |
|
---|
8529 | ComAssertThrow( uSourceIdx != VD_LAST_IMAGE
|
---|
8530 | && uTargetIdx != VD_LAST_IMAGE, E_FAIL);
|
---|
8531 |
|
---|
8532 | vrc = VDMerge(hdd, uSourceIdx, uTargetIdx,
|
---|
8533 | task.mVDOperationIfaces);
|
---|
8534 | if (RT_FAILURE(vrc))
|
---|
8535 | throw vrc;
|
---|
8536 |
|
---|
8537 | /* update parent UUIDs */
|
---|
8538 | if (!task.mfMergeForward)
|
---|
8539 | {
|
---|
8540 | /* we need to update UUIDs of all source's children
|
---|
8541 | * which cannot be part of the container at once so
|
---|
8542 | * add each one in there individually */
|
---|
8543 | if (task.mpChildrenToReparent)
|
---|
8544 | {
|
---|
8545 | MediumLockList::Base::iterator childrenBegin = task.mpChildrenToReparent->GetBegin();
|
---|
8546 | MediumLockList::Base::iterator childrenEnd = task.mpChildrenToReparent->GetEnd();
|
---|
8547 | for (MediumLockList::Base::iterator it = childrenBegin;
|
---|
8548 | it != childrenEnd;
|
---|
8549 | ++it)
|
---|
8550 | {
|
---|
8551 | Medium *pMedium = it->GetMedium();
|
---|
8552 | /* VD_OPEN_FLAGS_INFO since UUID is wrong yet */
|
---|
8553 | vrc = VDOpen(hdd,
|
---|
8554 | pMedium->m->strFormat.c_str(),
|
---|
8555 | pMedium->m->strLocationFull.c_str(),
|
---|
8556 | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
8557 | pMedium->m->vdImageIfaces);
|
---|
8558 | if (RT_FAILURE(vrc))
|
---|
8559 | throw vrc;
|
---|
8560 |
|
---|
8561 | vrc = VDSetParentUuid(hdd, VD_LAST_IMAGE,
|
---|
8562 | pTarget->m->id.raw());
|
---|
8563 | if (RT_FAILURE(vrc))
|
---|
8564 | throw vrc;
|
---|
8565 |
|
---|
8566 | vrc = VDClose(hdd, false /* fDelete */);
|
---|
8567 | if (RT_FAILURE(vrc))
|
---|
8568 | throw vrc;
|
---|
8569 | }
|
---|
8570 | }
|
---|
8571 | }
|
---|
8572 | }
|
---|
8573 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8574 | catch (int aVRC)
|
---|
8575 | {
|
---|
8576 | rcTmp = setError(VBOX_E_FILE_ERROR,
|
---|
8577 | tr("Could not merge the medium '%s' to '%s'%s"),
|
---|
8578 | m->strLocationFull.c_str(),
|
---|
8579 | pTarget->m->strLocationFull.c_str(),
|
---|
8580 | i_vdError(aVRC).c_str());
|
---|
8581 | }
|
---|
8582 |
|
---|
8583 | VDDestroy(hdd);
|
---|
8584 | }
|
---|
8585 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8586 |
|
---|
8587 | ErrorInfoKeeper eik;
|
---|
8588 | MultiResult mrc(rcTmp);
|
---|
8589 | HRESULT rc2;
|
---|
8590 |
|
---|
8591 | if (SUCCEEDED(mrc))
|
---|
8592 | {
|
---|
8593 | /* all media but the target were successfully deleted by
|
---|
8594 | * VDMerge; reparent the last one and uninitialize deleted media. */
|
---|
8595 |
|
---|
8596 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
8597 |
|
---|
8598 | if (task.mfMergeForward)
|
---|
8599 | {
|
---|
8600 | /* first, unregister the target since it may become a base
|
---|
8601 | * medium which needs re-registration */
|
---|
8602 | rc2 = m->pVirtualBox->i_unregisterMedium(pTarget);
|
---|
8603 | AssertComRC(rc2);
|
---|
8604 |
|
---|
8605 | /* then, reparent it and disconnect the deleted branch at both ends
|
---|
8606 | * (chain->parent() is source's parent). Depth check above. */
|
---|
8607 | pTarget->i_deparent();
|
---|
8608 | pTarget->i_setParent(task.mParentForTarget);
|
---|
8609 | if (task.mParentForTarget)
|
---|
8610 | i_deparent();
|
---|
8611 |
|
---|
8612 | /* then, register again */
|
---|
8613 | ComObjPtr<Medium> pMedium;
|
---|
8614 | rc2 = m->pVirtualBox->i_registerMedium(pTarget, &pMedium,
|
---|
8615 | treeLock);
|
---|
8616 | AssertComRC(rc2);
|
---|
8617 | }
|
---|
8618 | else
|
---|
8619 | {
|
---|
8620 | Assert(pTarget->i_getChildren().size() == 1);
|
---|
8621 | Medium *targetChild = pTarget->i_getChildren().front();
|
---|
8622 |
|
---|
8623 | /* disconnect the deleted branch at the elder end */
|
---|
8624 | targetChild->i_deparent();
|
---|
8625 |
|
---|
8626 | /* reparent source's children and disconnect the deleted
|
---|
8627 | * branch at the younger end */
|
---|
8628 | if (task.mpChildrenToReparent)
|
---|
8629 | {
|
---|
8630 | /* obey {parent,child} lock order */
|
---|
8631 | AutoWriteLock sourceLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8632 |
|
---|
8633 | MediumLockList::Base::iterator childrenBegin = task.mpChildrenToReparent->GetBegin();
|
---|
8634 | MediumLockList::Base::iterator childrenEnd = task.mpChildrenToReparent->GetEnd();
|
---|
8635 | for (MediumLockList::Base::iterator it = childrenBegin;
|
---|
8636 | it != childrenEnd;
|
---|
8637 | ++it)
|
---|
8638 | {
|
---|
8639 | Medium *pMedium = it->GetMedium();
|
---|
8640 | AutoWriteLock childLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8641 |
|
---|
8642 | pMedium->i_deparent(); // removes pMedium from source
|
---|
8643 | // no depth check, reduces depth
|
---|
8644 | pMedium->i_setParent(pTarget);
|
---|
8645 | }
|
---|
8646 | }
|
---|
8647 | }
|
---|
8648 |
|
---|
8649 | /* unregister and uninitialize all media removed by the merge */
|
---|
8650 | MediumLockList::Base::iterator lockListBegin =
|
---|
8651 | task.mpMediumLockList->GetBegin();
|
---|
8652 | MediumLockList::Base::iterator lockListEnd =
|
---|
8653 | task.mpMediumLockList->GetEnd();
|
---|
8654 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
8655 | it != lockListEnd;
|
---|
8656 | )
|
---|
8657 | {
|
---|
8658 | MediumLock &mediumLock = *it;
|
---|
8659 | /* Create a real copy of the medium pointer, as the medium
|
---|
8660 | * lock deletion below would invalidate the referenced object. */
|
---|
8661 | const ComObjPtr<Medium> pMedium = mediumLock.GetMedium();
|
---|
8662 |
|
---|
8663 | /* The target and all media not merged (readonly) are skipped */
|
---|
8664 | if ( pMedium == pTarget
|
---|
8665 | || pMedium->m->state == MediumState_LockedRead)
|
---|
8666 | {
|
---|
8667 | ++it;
|
---|
8668 | continue;
|
---|
8669 | }
|
---|
8670 |
|
---|
8671 | rc2 = pMedium->m->pVirtualBox->i_unregisterMedium(pMedium);
|
---|
8672 | AssertComRC(rc2);
|
---|
8673 |
|
---|
8674 | /* now, uninitialize the deleted medium (note that
|
---|
8675 | * due to the Deleting state, uninit() will not touch
|
---|
8676 | * the parent-child relationship so we need to
|
---|
8677 | * uninitialize each disk individually) */
|
---|
8678 |
|
---|
8679 | /* note that the operation initiator medium (which is
|
---|
8680 | * normally also the source medium) is a special case
|
---|
8681 | * -- there is one more caller added by Task to it which
|
---|
8682 | * we must release. Also, if we are in sync mode, the
|
---|
8683 | * caller may still hold an AutoCaller instance for it
|
---|
8684 | * and therefore we cannot uninit() it (it's therefore
|
---|
8685 | * the caller's responsibility) */
|
---|
8686 | if (pMedium == this)
|
---|
8687 | {
|
---|
8688 | Assert(i_getChildren().size() == 0);
|
---|
8689 | Assert(m->backRefs.size() == 0);
|
---|
8690 | task.mMediumCaller.release();
|
---|
8691 | }
|
---|
8692 |
|
---|
8693 | /* Delete the medium lock list entry, which also releases the
|
---|
8694 | * caller added by MergeChain before uninit() and updates the
|
---|
8695 | * iterator to point to the right place. */
|
---|
8696 | rc2 = task.mpMediumLockList->RemoveByIterator(it);
|
---|
8697 | AssertComRC(rc2);
|
---|
8698 |
|
---|
8699 | if (task.isAsync() || pMedium != this)
|
---|
8700 | {
|
---|
8701 | treeLock.release();
|
---|
8702 | pMedium->uninit();
|
---|
8703 | treeLock.acquire();
|
---|
8704 | }
|
---|
8705 | }
|
---|
8706 | }
|
---|
8707 |
|
---|
8708 | i_markRegistriesModified();
|
---|
8709 | if (task.isAsync())
|
---|
8710 | {
|
---|
8711 | // in asynchronous mode, save settings now
|
---|
8712 | eik.restore();
|
---|
8713 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
8714 | eik.fetch();
|
---|
8715 | }
|
---|
8716 |
|
---|
8717 | if (FAILED(mrc))
|
---|
8718 | {
|
---|
8719 | /* Here we come if either VDMerge() failed (in which case we
|
---|
8720 | * assume that it tried to do everything to make a further
|
---|
8721 | * retry possible -- e.g. not deleted intermediate media
|
---|
8722 | * and so on) or VirtualBox::saveRegistries() failed (where we
|
---|
8723 | * should have the original tree but with intermediate storage
|
---|
8724 | * units deleted by VDMerge()). We have to only restore states
|
---|
8725 | * (through the MergeChain dtor) unless we are run synchronously
|
---|
8726 | * in which case it's the responsibility of the caller as stated
|
---|
8727 | * in the mergeTo() docs. The latter also implies that we
|
---|
8728 | * don't own the merge chain, so release it in this case. */
|
---|
8729 | if (task.isAsync())
|
---|
8730 | i_cancelMergeTo(task.mpChildrenToReparent, task.mpMediumLockList);
|
---|
8731 | }
|
---|
8732 |
|
---|
8733 | return mrc;
|
---|
8734 | }
|
---|
8735 |
|
---|
8736 | /**
|
---|
8737 | * Implementation code for the "clone" task.
|
---|
8738 | *
|
---|
8739 | * This only gets started from Medium::CloneTo() and always runs asynchronously.
|
---|
8740 | * As a result, we always save the VirtualBox.xml file when we're done here.
|
---|
8741 | *
|
---|
8742 | * @param task
|
---|
8743 | * @return
|
---|
8744 | */
|
---|
8745 | HRESULT Medium::i_taskCloneHandler(Medium::CloneTask &task)
|
---|
8746 | {
|
---|
8747 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
8748 | * to lock order violations, it probably causes lock order issues related
|
---|
8749 | * to the AutoCaller usage. */
|
---|
8750 | HRESULT rcTmp = S_OK;
|
---|
8751 |
|
---|
8752 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
8753 | const ComObjPtr<Medium> &pParent = task.mParent;
|
---|
8754 |
|
---|
8755 | bool fCreatingTarget = false;
|
---|
8756 |
|
---|
8757 | uint64_t size = 0, logicalSize = 0;
|
---|
8758 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
8759 | bool fGenerateUuid = false;
|
---|
8760 |
|
---|
8761 | try
|
---|
8762 | {
|
---|
8763 | if (!pParent.isNull())
|
---|
8764 | {
|
---|
8765 |
|
---|
8766 | if (pParent->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
8767 | {
|
---|
8768 | AutoReadLock plock(pParent COMMA_LOCKVAL_SRC_POS);
|
---|
8769 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8770 | tr("Cannot clone image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
8771 | pParent->m->strLocationFull.c_str());
|
---|
8772 | }
|
---|
8773 | }
|
---|
8774 |
|
---|
8775 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
8776 | * signal from the task initiator (which releases it only after
|
---|
8777 | * RTThreadCreate()) that we can start the job. */
|
---|
8778 | AutoMultiWriteLock3 thisLock(this, pTarget, pParent COMMA_LOCKVAL_SRC_POS);
|
---|
8779 |
|
---|
8780 | fCreatingTarget = pTarget->m->state == MediumState_Creating;
|
---|
8781 |
|
---|
8782 | /* The object may request a specific UUID (through a special form of
|
---|
8783 | * the setLocation() argument). Otherwise we have to generate it */
|
---|
8784 | Guid targetId = pTarget->m->id;
|
---|
8785 |
|
---|
8786 | fGenerateUuid = targetId.isZero();
|
---|
8787 | if (fGenerateUuid)
|
---|
8788 | {
|
---|
8789 | targetId.create();
|
---|
8790 | /* VirtualBox::registerMedium() will need UUID */
|
---|
8791 | unconst(pTarget->m->id) = targetId;
|
---|
8792 | }
|
---|
8793 |
|
---|
8794 | PVDISK hdd;
|
---|
8795 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8796 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8797 |
|
---|
8798 | try
|
---|
8799 | {
|
---|
8800 | /* Open all media in the source chain. */
|
---|
8801 | MediumLockList::Base::const_iterator sourceListBegin =
|
---|
8802 | task.mpSourceMediumLockList->GetBegin();
|
---|
8803 | MediumLockList::Base::const_iterator sourceListEnd =
|
---|
8804 | task.mpSourceMediumLockList->GetEnd();
|
---|
8805 | for (MediumLockList::Base::const_iterator it = sourceListBegin;
|
---|
8806 | it != sourceListEnd;
|
---|
8807 | ++it)
|
---|
8808 | {
|
---|
8809 | const MediumLock &mediumLock = *it;
|
---|
8810 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8811 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8812 |
|
---|
8813 | /* sanity check */
|
---|
8814 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
8815 |
|
---|
8816 | /** Open all media in read-only mode. */
|
---|
8817 | vrc = VDOpen(hdd,
|
---|
8818 | pMedium->m->strFormat.c_str(),
|
---|
8819 | pMedium->m->strLocationFull.c_str(),
|
---|
8820 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
8821 | pMedium->m->vdImageIfaces);
|
---|
8822 | if (RT_FAILURE(vrc))
|
---|
8823 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8824 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8825 | pMedium->m->strLocationFull.c_str(),
|
---|
8826 | i_vdError(vrc).c_str());
|
---|
8827 | }
|
---|
8828 |
|
---|
8829 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
8830 | Utf8Str targetLocation(pTarget->m->strLocationFull);
|
---|
8831 | uint64_t capabilities = pTarget->m->formatObj->i_getCapabilities();
|
---|
8832 |
|
---|
8833 | Assert( pTarget->m->state == MediumState_Creating
|
---|
8834 | || pTarget->m->state == MediumState_LockedWrite);
|
---|
8835 | Assert(m->state == MediumState_LockedRead);
|
---|
8836 | Assert( pParent.isNull()
|
---|
8837 | || pParent->m->state == MediumState_LockedRead);
|
---|
8838 |
|
---|
8839 | /* unlock before the potentially lengthy operation */
|
---|
8840 | thisLock.release();
|
---|
8841 |
|
---|
8842 | /* ensure the target directory exists */
|
---|
8843 | if (capabilities & MediumFormatCapabilities_File)
|
---|
8844 | {
|
---|
8845 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
8846 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
8847 | if (FAILED(rc))
|
---|
8848 | throw rc;
|
---|
8849 | }
|
---|
8850 |
|
---|
8851 | PVDISK targetHdd;
|
---|
8852 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &targetHdd);
|
---|
8853 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8854 |
|
---|
8855 | try
|
---|
8856 | {
|
---|
8857 | /* Open all media in the target chain. */
|
---|
8858 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
8859 | task.mpTargetMediumLockList->GetBegin();
|
---|
8860 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
8861 | task.mpTargetMediumLockList->GetEnd();
|
---|
8862 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
8863 | it != targetListEnd;
|
---|
8864 | ++it)
|
---|
8865 | {
|
---|
8866 | const MediumLock &mediumLock = *it;
|
---|
8867 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8868 |
|
---|
8869 | /* If the target medium is not created yet there's no
|
---|
8870 | * reason to open it. */
|
---|
8871 | if (pMedium == pTarget && fCreatingTarget)
|
---|
8872 | continue;
|
---|
8873 |
|
---|
8874 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8875 |
|
---|
8876 | /* sanity check */
|
---|
8877 | Assert( pMedium->m->state == MediumState_LockedRead
|
---|
8878 | || pMedium->m->state == MediumState_LockedWrite);
|
---|
8879 |
|
---|
8880 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
8881 | if (pMedium->m->state != MediumState_LockedWrite)
|
---|
8882 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
8883 | if (pMedium->m->type == MediumType_Shareable)
|
---|
8884 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
8885 |
|
---|
8886 | /* Open all media in appropriate mode. */
|
---|
8887 | vrc = VDOpen(targetHdd,
|
---|
8888 | pMedium->m->strFormat.c_str(),
|
---|
8889 | pMedium->m->strLocationFull.c_str(),
|
---|
8890 | uOpenFlags | m->uOpenFlagsDef,
|
---|
8891 | pMedium->m->vdImageIfaces);
|
---|
8892 | if (RT_FAILURE(vrc))
|
---|
8893 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8894 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8895 | pMedium->m->strLocationFull.c_str(),
|
---|
8896 | i_vdError(vrc).c_str());
|
---|
8897 | }
|
---|
8898 |
|
---|
8899 | /* target isn't locked, but no changing data is accessed */
|
---|
8900 | if (task.midxSrcImageSame == UINT32_MAX)
|
---|
8901 | {
|
---|
8902 | vrc = VDCopy(hdd,
|
---|
8903 | VD_LAST_IMAGE,
|
---|
8904 | targetHdd,
|
---|
8905 | targetFormat.c_str(),
|
---|
8906 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
8907 | false /* fMoveByRename */,
|
---|
8908 | 0 /* cbSize */,
|
---|
8909 | task.mVariant & ~MediumVariant_NoCreateDir,
|
---|
8910 | targetId.raw(),
|
---|
8911 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
8912 | NULL /* pVDIfsOperation */,
|
---|
8913 | pTarget->m->vdImageIfaces,
|
---|
8914 | task.mVDOperationIfaces);
|
---|
8915 | }
|
---|
8916 | else
|
---|
8917 | {
|
---|
8918 | vrc = VDCopyEx(hdd,
|
---|
8919 | VD_LAST_IMAGE,
|
---|
8920 | targetHdd,
|
---|
8921 | targetFormat.c_str(),
|
---|
8922 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
8923 | false /* fMoveByRename */,
|
---|
8924 | 0 /* cbSize */,
|
---|
8925 | task.midxSrcImageSame,
|
---|
8926 | task.midxDstImageSame,
|
---|
8927 | task.mVariant & ~MediumVariant_NoCreateDir,
|
---|
8928 | targetId.raw(),
|
---|
8929 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
8930 | NULL /* pVDIfsOperation */,
|
---|
8931 | pTarget->m->vdImageIfaces,
|
---|
8932 | task.mVDOperationIfaces);
|
---|
8933 | }
|
---|
8934 | if (RT_FAILURE(vrc))
|
---|
8935 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8936 | tr("Could not create the clone medium '%s'%s"),
|
---|
8937 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
8938 |
|
---|
8939 | size = VDGetFileSize(targetHdd, VD_LAST_IMAGE);
|
---|
8940 | logicalSize = VDGetSize(targetHdd, VD_LAST_IMAGE);
|
---|
8941 | unsigned uImageFlags;
|
---|
8942 | vrc = VDGetImageFlags(targetHdd, 0, &uImageFlags);
|
---|
8943 | if (RT_SUCCESS(vrc))
|
---|
8944 | variant = (MediumVariant_T)uImageFlags;
|
---|
8945 | }
|
---|
8946 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8947 |
|
---|
8948 | VDDestroy(targetHdd);
|
---|
8949 | }
|
---|
8950 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8951 |
|
---|
8952 | VDDestroy(hdd);
|
---|
8953 | }
|
---|
8954 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8955 |
|
---|
8956 | ErrorInfoKeeper eik;
|
---|
8957 | MultiResult mrc(rcTmp);
|
---|
8958 |
|
---|
8959 | /* Only do the parent changes for newly created media. */
|
---|
8960 | if (SUCCEEDED(mrc) && fCreatingTarget)
|
---|
8961 | {
|
---|
8962 | /* we set m->pParent & children() */
|
---|
8963 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
8964 |
|
---|
8965 | Assert(pTarget->m->pParent.isNull());
|
---|
8966 |
|
---|
8967 | if (pParent)
|
---|
8968 | {
|
---|
8969 | /* Associate the clone with the parent and deassociate
|
---|
8970 | * from VirtualBox. Depth check above. */
|
---|
8971 | pTarget->i_setParent(pParent);
|
---|
8972 |
|
---|
8973 | /* register with mVirtualBox as the last step and move to
|
---|
8974 | * Created state only on success (leaving an orphan file is
|
---|
8975 | * better than breaking media registry consistency) */
|
---|
8976 | eik.restore();
|
---|
8977 | ComObjPtr<Medium> pMedium;
|
---|
8978 | mrc = pParent->m->pVirtualBox->i_registerMedium(pTarget, &pMedium,
|
---|
8979 | treeLock);
|
---|
8980 | Assert( FAILED(mrc)
|
---|
8981 | || pTarget == pMedium);
|
---|
8982 | eik.fetch();
|
---|
8983 |
|
---|
8984 | if (FAILED(mrc))
|
---|
8985 | /* break parent association on failure to register */
|
---|
8986 | pTarget->i_deparent(); // removes target from parent
|
---|
8987 | }
|
---|
8988 | else
|
---|
8989 | {
|
---|
8990 | /* just register */
|
---|
8991 | eik.restore();
|
---|
8992 | ComObjPtr<Medium> pMedium;
|
---|
8993 | mrc = m->pVirtualBox->i_registerMedium(pTarget, &pMedium,
|
---|
8994 | treeLock);
|
---|
8995 | Assert( FAILED(mrc)
|
---|
8996 | || pTarget == pMedium);
|
---|
8997 | eik.fetch();
|
---|
8998 | }
|
---|
8999 | }
|
---|
9000 |
|
---|
9001 | if (fCreatingTarget)
|
---|
9002 | {
|
---|
9003 | AutoWriteLock mLock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
9004 |
|
---|
9005 | if (SUCCEEDED(mrc))
|
---|
9006 | {
|
---|
9007 | pTarget->m->state = MediumState_Created;
|
---|
9008 |
|
---|
9009 | pTarget->m->size = size;
|
---|
9010 | pTarget->m->logicalSize = logicalSize;
|
---|
9011 | pTarget->m->variant = variant;
|
---|
9012 | }
|
---|
9013 | else
|
---|
9014 | {
|
---|
9015 | /* back to NotCreated on failure */
|
---|
9016 | pTarget->m->state = MediumState_NotCreated;
|
---|
9017 |
|
---|
9018 | /* reset UUID to prevent it from being reused next time */
|
---|
9019 | if (fGenerateUuid)
|
---|
9020 | unconst(pTarget->m->id).clear();
|
---|
9021 | }
|
---|
9022 | }
|
---|
9023 |
|
---|
9024 | /* Copy any filter related settings over to the target. */
|
---|
9025 | if (SUCCEEDED(mrc))
|
---|
9026 | {
|
---|
9027 | /* Copy any filter related settings over. */
|
---|
9028 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
9029 | ComObjPtr<Medium> pTargetBase = pTarget->i_getBase();
|
---|
9030 | std::vector<com::Utf8Str> aFilterPropNames;
|
---|
9031 | std::vector<com::Utf8Str> aFilterPropValues;
|
---|
9032 | mrc = pBase->i_getFilterProperties(aFilterPropNames, aFilterPropValues);
|
---|
9033 | if (SUCCEEDED(mrc))
|
---|
9034 | {
|
---|
9035 | /* Go through the properties and add them to the target medium. */
|
---|
9036 | for (unsigned idx = 0; idx < aFilterPropNames.size(); idx++)
|
---|
9037 | {
|
---|
9038 | mrc = pTargetBase->i_setPropertyDirect(aFilterPropNames[idx], aFilterPropValues[idx]);
|
---|
9039 | if (FAILED(mrc)) break;
|
---|
9040 | }
|
---|
9041 |
|
---|
9042 | // now, at the end of this task (always asynchronous), save the settings
|
---|
9043 | if (SUCCEEDED(mrc))
|
---|
9044 | {
|
---|
9045 | // save the settings
|
---|
9046 | i_markRegistriesModified();
|
---|
9047 | /* collect multiple errors */
|
---|
9048 | eik.restore();
|
---|
9049 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
9050 | eik.fetch();
|
---|
9051 | }
|
---|
9052 | }
|
---|
9053 | }
|
---|
9054 |
|
---|
9055 | /* Everything is explicitly unlocked when the task exits,
|
---|
9056 | * as the task destruction also destroys the source chain. */
|
---|
9057 |
|
---|
9058 | /* Make sure the source chain is released early. It could happen
|
---|
9059 | * that we get a deadlock in Appliance::Import when Medium::Close
|
---|
9060 | * is called & the source chain is released at the same time. */
|
---|
9061 | task.mpSourceMediumLockList->Clear();
|
---|
9062 |
|
---|
9063 | return mrc;
|
---|
9064 | }
|
---|
9065 |
|
---|
9066 | /**
|
---|
9067 | * Implementation code for the "move" task.
|
---|
9068 | *
|
---|
9069 | * This only gets started from Medium::SetLocation() and always
|
---|
9070 | * runs asynchronously.
|
---|
9071 | *
|
---|
9072 | * @param task
|
---|
9073 | * @return
|
---|
9074 | */
|
---|
9075 | HRESULT Medium::i_taskMoveHandler(Medium::MoveTask &task)
|
---|
9076 | {
|
---|
9077 |
|
---|
9078 | HRESULT rcOut = S_OK;
|
---|
9079 |
|
---|
9080 | /* pTarget is equal "this" in our case */
|
---|
9081 | const ComObjPtr<Medium> &pTarget = task.mMedium;
|
---|
9082 |
|
---|
9083 | uint64_t size = 0; NOREF(size);
|
---|
9084 | uint64_t logicalSize = 0; NOREF(logicalSize);
|
---|
9085 | MediumVariant_T variant = MediumVariant_Standard; NOREF(variant);
|
---|
9086 |
|
---|
9087 | /*
|
---|
9088 | * it's exactly moving, not cloning
|
---|
9089 | */
|
---|
9090 | if (!i_isMoveOperation(pTarget))
|
---|
9091 | {
|
---|
9092 | HRESULT rc = setError(VBOX_E_FILE_ERROR,
|
---|
9093 | tr("Wrong preconditions for moving the medium %s"),
|
---|
9094 | pTarget->m->strLocationFull.c_str());
|
---|
9095 | return rc;
|
---|
9096 | }
|
---|
9097 |
|
---|
9098 | try
|
---|
9099 | {
|
---|
9100 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
9101 | * signal from the task initiator (which releases it only after
|
---|
9102 | * RTThreadCreate()) that we can start the job. */
|
---|
9103 |
|
---|
9104 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9105 |
|
---|
9106 | PVDISK hdd;
|
---|
9107 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9108 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9109 |
|
---|
9110 | try
|
---|
9111 | {
|
---|
9112 | /* Open all media in the source chain. */
|
---|
9113 | MediumLockList::Base::const_iterator sourceListBegin =
|
---|
9114 | task.mpMediumLockList->GetBegin();
|
---|
9115 | MediumLockList::Base::const_iterator sourceListEnd =
|
---|
9116 | task.mpMediumLockList->GetEnd();
|
---|
9117 | for (MediumLockList::Base::const_iterator it = sourceListBegin;
|
---|
9118 | it != sourceListEnd;
|
---|
9119 | ++it)
|
---|
9120 | {
|
---|
9121 | const MediumLock &mediumLock = *it;
|
---|
9122 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9123 | AutoWriteLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9124 |
|
---|
9125 | /* sanity check */
|
---|
9126 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
9127 |
|
---|
9128 | vrc = VDOpen(hdd,
|
---|
9129 | pMedium->m->strFormat.c_str(),
|
---|
9130 | pMedium->m->strLocationFull.c_str(),
|
---|
9131 | VD_OPEN_FLAGS_NORMAL,
|
---|
9132 | pMedium->m->vdImageIfaces);
|
---|
9133 | if (RT_FAILURE(vrc))
|
---|
9134 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9135 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9136 | pMedium->m->strLocationFull.c_str(),
|
---|
9137 | i_vdError(vrc).c_str());
|
---|
9138 | }
|
---|
9139 |
|
---|
9140 | /* we can directly use pTarget->m->"variables" but for better reading we use local copies */
|
---|
9141 | Guid targetId = pTarget->m->id;
|
---|
9142 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
9143 | uint64_t targetCapabilities = pTarget->m->formatObj->i_getCapabilities();
|
---|
9144 |
|
---|
9145 | /*
|
---|
9146 | * change target location
|
---|
9147 | * m->strNewLocationFull has been set already together with m->fMoveThisMedium in
|
---|
9148 | * i_preparationForMoving()
|
---|
9149 | */
|
---|
9150 | Utf8Str targetLocation = i_getNewLocationForMoving();
|
---|
9151 |
|
---|
9152 | /* unlock before the potentially lengthy operation */
|
---|
9153 | thisLock.release();
|
---|
9154 |
|
---|
9155 | /* ensure the target directory exists */
|
---|
9156 | if (targetCapabilities & MediumFormatCapabilities_File)
|
---|
9157 | {
|
---|
9158 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
9159 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
9160 | if (FAILED(rc))
|
---|
9161 | throw rc;
|
---|
9162 | }
|
---|
9163 |
|
---|
9164 | try
|
---|
9165 | {
|
---|
9166 | vrc = VDCopy(hdd,
|
---|
9167 | VD_LAST_IMAGE,
|
---|
9168 | hdd,
|
---|
9169 | targetFormat.c_str(),
|
---|
9170 | targetLocation.c_str(),
|
---|
9171 | true /* fMoveByRename */,
|
---|
9172 | 0 /* cbSize */,
|
---|
9173 | VD_IMAGE_FLAGS_NONE,
|
---|
9174 | targetId.raw(),
|
---|
9175 | VD_OPEN_FLAGS_NORMAL,
|
---|
9176 | NULL /* pVDIfsOperation */,
|
---|
9177 | NULL,
|
---|
9178 | NULL);
|
---|
9179 | if (RT_FAILURE(vrc))
|
---|
9180 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9181 | tr("Could not move medium '%s'%s"),
|
---|
9182 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
9183 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
9184 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
9185 | unsigned uImageFlags;
|
---|
9186 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
9187 | if (RT_SUCCESS(vrc))
|
---|
9188 | variant = (MediumVariant_T)uImageFlags;
|
---|
9189 |
|
---|
9190 | /*
|
---|
9191 | * set current location, because VDCopy\VDCopyEx doesn't do it.
|
---|
9192 | * also reset moving flag
|
---|
9193 | */
|
---|
9194 | i_resetMoveOperationData();
|
---|
9195 | m->strLocationFull = targetLocation;
|
---|
9196 |
|
---|
9197 | }
|
---|
9198 | catch (HRESULT aRC) { rcOut = aRC; }
|
---|
9199 |
|
---|
9200 | }
|
---|
9201 | catch (HRESULT aRC) { rcOut = aRC; }
|
---|
9202 |
|
---|
9203 | VDDestroy(hdd);
|
---|
9204 | }
|
---|
9205 | catch (HRESULT aRC) { rcOut = aRC; }
|
---|
9206 |
|
---|
9207 | ErrorInfoKeeper eik;
|
---|
9208 | MultiResult mrc(rcOut);
|
---|
9209 |
|
---|
9210 | // now, at the end of this task (always asynchronous), save the settings
|
---|
9211 | if (SUCCEEDED(mrc))
|
---|
9212 | {
|
---|
9213 | // save the settings
|
---|
9214 | i_markRegistriesModified();
|
---|
9215 | /* collect multiple errors */
|
---|
9216 | eik.restore();
|
---|
9217 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
9218 | eik.fetch();
|
---|
9219 | }
|
---|
9220 |
|
---|
9221 | /* Everything is explicitly unlocked when the task exits,
|
---|
9222 | * as the task destruction also destroys the source chain. */
|
---|
9223 |
|
---|
9224 | task.mpMediumLockList->Clear();
|
---|
9225 |
|
---|
9226 | return mrc;
|
---|
9227 | }
|
---|
9228 |
|
---|
9229 | /**
|
---|
9230 | * Implementation code for the "delete" task.
|
---|
9231 | *
|
---|
9232 | * This task always gets started from Medium::deleteStorage() and can run
|
---|
9233 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
9234 | * that function.
|
---|
9235 | *
|
---|
9236 | * @param task
|
---|
9237 | * @return
|
---|
9238 | */
|
---|
9239 | HRESULT Medium::i_taskDeleteHandler(Medium::DeleteTask &task)
|
---|
9240 | {
|
---|
9241 | NOREF(task);
|
---|
9242 | HRESULT rc = S_OK;
|
---|
9243 |
|
---|
9244 | try
|
---|
9245 | {
|
---|
9246 | /* The lock is also used as a signal from the task initiator (which
|
---|
9247 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
9248 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9249 |
|
---|
9250 | PVDISK hdd;
|
---|
9251 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9252 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9253 |
|
---|
9254 | Utf8Str format(m->strFormat);
|
---|
9255 | Utf8Str location(m->strLocationFull);
|
---|
9256 |
|
---|
9257 | /* unlock before the potentially lengthy operation */
|
---|
9258 | Assert(m->state == MediumState_Deleting);
|
---|
9259 | thisLock.release();
|
---|
9260 |
|
---|
9261 | try
|
---|
9262 | {
|
---|
9263 | vrc = VDOpen(hdd,
|
---|
9264 | format.c_str(),
|
---|
9265 | location.c_str(),
|
---|
9266 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
9267 | m->vdImageIfaces);
|
---|
9268 | if (RT_SUCCESS(vrc))
|
---|
9269 | vrc = VDClose(hdd, true /* fDelete */);
|
---|
9270 |
|
---|
9271 | if (RT_FAILURE(vrc) && vrc != VERR_FILE_NOT_FOUND)
|
---|
9272 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9273 | tr("Could not delete the medium storage unit '%s'%s"),
|
---|
9274 | location.c_str(), i_vdError(vrc).c_str());
|
---|
9275 |
|
---|
9276 | }
|
---|
9277 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9278 |
|
---|
9279 | VDDestroy(hdd);
|
---|
9280 | }
|
---|
9281 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9282 |
|
---|
9283 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9284 |
|
---|
9285 | /* go to the NotCreated state even on failure since the storage
|
---|
9286 | * may have been already partially deleted and cannot be used any
|
---|
9287 | * more. One will be able to manually re-open the storage if really
|
---|
9288 | * needed to re-register it. */
|
---|
9289 | m->state = MediumState_NotCreated;
|
---|
9290 |
|
---|
9291 | /* Reset UUID to prevent Create* from reusing it again */
|
---|
9292 | unconst(m->id).clear();
|
---|
9293 |
|
---|
9294 | return rc;
|
---|
9295 | }
|
---|
9296 |
|
---|
9297 | /**
|
---|
9298 | * Implementation code for the "reset" task.
|
---|
9299 | *
|
---|
9300 | * This always gets started asynchronously from Medium::Reset().
|
---|
9301 | *
|
---|
9302 | * @param task
|
---|
9303 | * @return
|
---|
9304 | */
|
---|
9305 | HRESULT Medium::i_taskResetHandler(Medium::ResetTask &task)
|
---|
9306 | {
|
---|
9307 | HRESULT rc = S_OK;
|
---|
9308 |
|
---|
9309 | uint64_t size = 0, logicalSize = 0;
|
---|
9310 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
9311 |
|
---|
9312 | try
|
---|
9313 | {
|
---|
9314 | /* The lock is also used as a signal from the task initiator (which
|
---|
9315 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
9316 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9317 |
|
---|
9318 | /// @todo Below we use a pair of delete/create operations to reset
|
---|
9319 | /// the diff contents but the most efficient way will of course be
|
---|
9320 | /// to add a VDResetDiff() API call
|
---|
9321 |
|
---|
9322 | PVDISK hdd;
|
---|
9323 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9324 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9325 |
|
---|
9326 | Guid id = m->id;
|
---|
9327 | Utf8Str format(m->strFormat);
|
---|
9328 | Utf8Str location(m->strLocationFull);
|
---|
9329 |
|
---|
9330 | Medium *pParent = m->pParent;
|
---|
9331 | Guid parentId = pParent->m->id;
|
---|
9332 | Utf8Str parentFormat(pParent->m->strFormat);
|
---|
9333 | Utf8Str parentLocation(pParent->m->strLocationFull);
|
---|
9334 |
|
---|
9335 | Assert(m->state == MediumState_LockedWrite);
|
---|
9336 |
|
---|
9337 | /* unlock before the potentially lengthy operation */
|
---|
9338 | thisLock.release();
|
---|
9339 |
|
---|
9340 | try
|
---|
9341 | {
|
---|
9342 | /* Open all media in the target chain but the last. */
|
---|
9343 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
9344 | task.mpMediumLockList->GetBegin();
|
---|
9345 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
9346 | task.mpMediumLockList->GetEnd();
|
---|
9347 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
9348 | it != targetListEnd;
|
---|
9349 | ++it)
|
---|
9350 | {
|
---|
9351 | const MediumLock &mediumLock = *it;
|
---|
9352 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9353 |
|
---|
9354 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9355 |
|
---|
9356 | /* sanity check, "this" is checked above */
|
---|
9357 | Assert( pMedium == this
|
---|
9358 | || pMedium->m->state == MediumState_LockedRead);
|
---|
9359 |
|
---|
9360 | /* Open all media in appropriate mode. */
|
---|
9361 | vrc = VDOpen(hdd,
|
---|
9362 | pMedium->m->strFormat.c_str(),
|
---|
9363 | pMedium->m->strLocationFull.c_str(),
|
---|
9364 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
9365 | pMedium->m->vdImageIfaces);
|
---|
9366 | if (RT_FAILURE(vrc))
|
---|
9367 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9368 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9369 | pMedium->m->strLocationFull.c_str(),
|
---|
9370 | i_vdError(vrc).c_str());
|
---|
9371 |
|
---|
9372 | /* Done when we hit the media which should be reset */
|
---|
9373 | if (pMedium == this)
|
---|
9374 | break;
|
---|
9375 | }
|
---|
9376 |
|
---|
9377 | /* first, delete the storage unit */
|
---|
9378 | vrc = VDClose(hdd, true /* fDelete */);
|
---|
9379 | if (RT_FAILURE(vrc))
|
---|
9380 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9381 | tr("Could not delete the medium storage unit '%s'%s"),
|
---|
9382 | location.c_str(), i_vdError(vrc).c_str());
|
---|
9383 |
|
---|
9384 | /* next, create it again */
|
---|
9385 | vrc = VDOpen(hdd,
|
---|
9386 | parentFormat.c_str(),
|
---|
9387 | parentLocation.c_str(),
|
---|
9388 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
9389 | m->vdImageIfaces);
|
---|
9390 | if (RT_FAILURE(vrc))
|
---|
9391 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9392 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9393 | parentLocation.c_str(), i_vdError(vrc).c_str());
|
---|
9394 |
|
---|
9395 | vrc = VDCreateDiff(hdd,
|
---|
9396 | format.c_str(),
|
---|
9397 | location.c_str(),
|
---|
9398 | /// @todo use the same medium variant as before
|
---|
9399 | VD_IMAGE_FLAGS_NONE,
|
---|
9400 | NULL,
|
---|
9401 | id.raw(),
|
---|
9402 | parentId.raw(),
|
---|
9403 | VD_OPEN_FLAGS_NORMAL,
|
---|
9404 | m->vdImageIfaces,
|
---|
9405 | task.mVDOperationIfaces);
|
---|
9406 | if (RT_FAILURE(vrc))
|
---|
9407 | {
|
---|
9408 | if (vrc == VERR_VD_INVALID_TYPE)
|
---|
9409 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9410 | tr("Parameters for creating the differencing medium storage unit '%s' are invalid%s"),
|
---|
9411 | location.c_str(), i_vdError(vrc).c_str());
|
---|
9412 | else
|
---|
9413 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9414 | tr("Could not create the differencing medium storage unit '%s'%s"),
|
---|
9415 | location.c_str(), i_vdError(vrc).c_str());
|
---|
9416 | }
|
---|
9417 |
|
---|
9418 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
9419 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
9420 | unsigned uImageFlags;
|
---|
9421 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
9422 | if (RT_SUCCESS(vrc))
|
---|
9423 | variant = (MediumVariant_T)uImageFlags;
|
---|
9424 | }
|
---|
9425 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9426 |
|
---|
9427 | VDDestroy(hdd);
|
---|
9428 | }
|
---|
9429 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9430 |
|
---|
9431 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9432 |
|
---|
9433 | m->size = size;
|
---|
9434 | m->logicalSize = logicalSize;
|
---|
9435 | m->variant = variant;
|
---|
9436 |
|
---|
9437 | /* Everything is explicitly unlocked when the task exits,
|
---|
9438 | * as the task destruction also destroys the media chain. */
|
---|
9439 |
|
---|
9440 | return rc;
|
---|
9441 | }
|
---|
9442 |
|
---|
9443 | /**
|
---|
9444 | * Implementation code for the "compact" task.
|
---|
9445 | *
|
---|
9446 | * @param task
|
---|
9447 | * @return
|
---|
9448 | */
|
---|
9449 | HRESULT Medium::i_taskCompactHandler(Medium::CompactTask &task)
|
---|
9450 | {
|
---|
9451 | HRESULT rc = S_OK;
|
---|
9452 |
|
---|
9453 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
9454 | * signal from the task initiator (which releases it only after
|
---|
9455 | * RTThreadCreate()) that we can start the job. */
|
---|
9456 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9457 |
|
---|
9458 | try
|
---|
9459 | {
|
---|
9460 | PVDISK hdd;
|
---|
9461 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9462 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9463 |
|
---|
9464 | try
|
---|
9465 | {
|
---|
9466 | /* Open all media in the chain. */
|
---|
9467 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
9468 | task.mpMediumLockList->GetBegin();
|
---|
9469 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
9470 | task.mpMediumLockList->GetEnd();
|
---|
9471 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
9472 | mediumListEnd;
|
---|
9473 | --mediumListLast;
|
---|
9474 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
9475 | it != mediumListEnd;
|
---|
9476 | ++it)
|
---|
9477 | {
|
---|
9478 | const MediumLock &mediumLock = *it;
|
---|
9479 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9480 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9481 |
|
---|
9482 | /* sanity check */
|
---|
9483 | if (it == mediumListLast)
|
---|
9484 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
9485 | else
|
---|
9486 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
9487 |
|
---|
9488 | /* Open all media but last in read-only mode. Do not handle
|
---|
9489 | * shareable media, as compaction and sharing are mutually
|
---|
9490 | * exclusive. */
|
---|
9491 | vrc = VDOpen(hdd,
|
---|
9492 | pMedium->m->strFormat.c_str(),
|
---|
9493 | pMedium->m->strLocationFull.c_str(),
|
---|
9494 | m->uOpenFlagsDef | (it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
9495 | pMedium->m->vdImageIfaces);
|
---|
9496 | if (RT_FAILURE(vrc))
|
---|
9497 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9498 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9499 | pMedium->m->strLocationFull.c_str(),
|
---|
9500 | i_vdError(vrc).c_str());
|
---|
9501 | }
|
---|
9502 |
|
---|
9503 | Assert(m->state == MediumState_LockedWrite);
|
---|
9504 |
|
---|
9505 | Utf8Str location(m->strLocationFull);
|
---|
9506 |
|
---|
9507 | /* unlock before the potentially lengthy operation */
|
---|
9508 | thisLock.release();
|
---|
9509 |
|
---|
9510 | vrc = VDCompact(hdd, VD_LAST_IMAGE, task.mVDOperationIfaces);
|
---|
9511 | if (RT_FAILURE(vrc))
|
---|
9512 | {
|
---|
9513 | if (vrc == VERR_NOT_SUPPORTED)
|
---|
9514 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
9515 | tr("Compacting is not yet supported for medium '%s'"),
|
---|
9516 | location.c_str());
|
---|
9517 | else if (vrc == VERR_NOT_IMPLEMENTED)
|
---|
9518 | throw setError(E_NOTIMPL,
|
---|
9519 | tr("Compacting is not implemented, medium '%s'"),
|
---|
9520 | location.c_str());
|
---|
9521 | else
|
---|
9522 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9523 | tr("Could not compact medium '%s'%s"),
|
---|
9524 | location.c_str(),
|
---|
9525 | i_vdError(vrc).c_str());
|
---|
9526 | }
|
---|
9527 | }
|
---|
9528 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9529 |
|
---|
9530 | VDDestroy(hdd);
|
---|
9531 | }
|
---|
9532 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9533 |
|
---|
9534 | /* Everything is explicitly unlocked when the task exits,
|
---|
9535 | * as the task destruction also destroys the media chain. */
|
---|
9536 |
|
---|
9537 | return rc;
|
---|
9538 | }
|
---|
9539 |
|
---|
9540 | /**
|
---|
9541 | * Implementation code for the "resize" task.
|
---|
9542 | *
|
---|
9543 | * @param task
|
---|
9544 | * @return
|
---|
9545 | */
|
---|
9546 | HRESULT Medium::i_taskResizeHandler(Medium::ResizeTask &task)
|
---|
9547 | {
|
---|
9548 | HRESULT rc = S_OK;
|
---|
9549 |
|
---|
9550 | uint64_t size = 0, logicalSize = 0;
|
---|
9551 |
|
---|
9552 | try
|
---|
9553 | {
|
---|
9554 | /* The lock is also used as a signal from the task initiator (which
|
---|
9555 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
9556 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9557 |
|
---|
9558 | PVDISK hdd;
|
---|
9559 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9560 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9561 |
|
---|
9562 | try
|
---|
9563 | {
|
---|
9564 | /* Open all media in the chain. */
|
---|
9565 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
9566 | task.mpMediumLockList->GetBegin();
|
---|
9567 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
9568 | task.mpMediumLockList->GetEnd();
|
---|
9569 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
9570 | mediumListEnd;
|
---|
9571 | --mediumListLast;
|
---|
9572 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
9573 | it != mediumListEnd;
|
---|
9574 | ++it)
|
---|
9575 | {
|
---|
9576 | const MediumLock &mediumLock = *it;
|
---|
9577 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9578 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9579 |
|
---|
9580 | /* sanity check */
|
---|
9581 | if (it == mediumListLast)
|
---|
9582 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
9583 | else
|
---|
9584 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
9585 |
|
---|
9586 | /* Open all media but last in read-only mode. Do not handle
|
---|
9587 | * shareable media, as compaction and sharing are mutually
|
---|
9588 | * exclusive. */
|
---|
9589 | vrc = VDOpen(hdd,
|
---|
9590 | pMedium->m->strFormat.c_str(),
|
---|
9591 | pMedium->m->strLocationFull.c_str(),
|
---|
9592 | m->uOpenFlagsDef | (it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
9593 | pMedium->m->vdImageIfaces);
|
---|
9594 | if (RT_FAILURE(vrc))
|
---|
9595 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9596 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9597 | pMedium->m->strLocationFull.c_str(),
|
---|
9598 | i_vdError(vrc).c_str());
|
---|
9599 | }
|
---|
9600 |
|
---|
9601 | Assert(m->state == MediumState_LockedWrite);
|
---|
9602 |
|
---|
9603 | Utf8Str location(m->strLocationFull);
|
---|
9604 |
|
---|
9605 | /* unlock before the potentially lengthy operation */
|
---|
9606 | thisLock.release();
|
---|
9607 |
|
---|
9608 | VDGEOMETRY geo = {0, 0, 0}; /* auto */
|
---|
9609 | vrc = VDResize(hdd, task.mSize, &geo, &geo, task.mVDOperationIfaces);
|
---|
9610 | if (RT_FAILURE(vrc))
|
---|
9611 | {
|
---|
9612 | if (vrc == VERR_NOT_SUPPORTED)
|
---|
9613 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
9614 | tr("Resizing to new size %llu is not yet supported for medium '%s'"),
|
---|
9615 | task.mSize, location.c_str());
|
---|
9616 | else if (vrc == VERR_NOT_IMPLEMENTED)
|
---|
9617 | throw setError(E_NOTIMPL,
|
---|
9618 | tr("Resiting is not implemented, medium '%s'"),
|
---|
9619 | location.c_str());
|
---|
9620 | else
|
---|
9621 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9622 | tr("Could not resize medium '%s'%s"),
|
---|
9623 | location.c_str(),
|
---|
9624 | i_vdError(vrc).c_str());
|
---|
9625 | }
|
---|
9626 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
9627 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
9628 | }
|
---|
9629 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9630 |
|
---|
9631 | VDDestroy(hdd);
|
---|
9632 | }
|
---|
9633 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9634 |
|
---|
9635 | if (SUCCEEDED(rc))
|
---|
9636 | {
|
---|
9637 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9638 | m->size = size;
|
---|
9639 | m->logicalSize = logicalSize;
|
---|
9640 | }
|
---|
9641 |
|
---|
9642 | /* Everything is explicitly unlocked when the task exits,
|
---|
9643 | * as the task destruction also destroys the media chain. */
|
---|
9644 |
|
---|
9645 | return rc;
|
---|
9646 | }
|
---|
9647 |
|
---|
9648 | /**
|
---|
9649 | * Implementation code for the "import" task.
|
---|
9650 | *
|
---|
9651 | * This only gets started from Medium::importFile() and always runs
|
---|
9652 | * asynchronously. It potentially touches the media registry, so we
|
---|
9653 | * always save the VirtualBox.xml file when we're done here.
|
---|
9654 | *
|
---|
9655 | * @param task
|
---|
9656 | * @return
|
---|
9657 | */
|
---|
9658 | HRESULT Medium::i_taskImportHandler(Medium::ImportTask &task)
|
---|
9659 | {
|
---|
9660 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
9661 | * to lock order violations, it probably causes lock order issues related
|
---|
9662 | * to the AutoCaller usage. */
|
---|
9663 | HRESULT rcTmp = S_OK;
|
---|
9664 |
|
---|
9665 | const ComObjPtr<Medium> &pParent = task.mParent;
|
---|
9666 |
|
---|
9667 | bool fCreatingTarget = false;
|
---|
9668 |
|
---|
9669 | uint64_t size = 0, logicalSize = 0;
|
---|
9670 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
9671 | bool fGenerateUuid = false;
|
---|
9672 |
|
---|
9673 | try
|
---|
9674 | {
|
---|
9675 | if (!pParent.isNull())
|
---|
9676 | if (pParent->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
9677 | {
|
---|
9678 | AutoReadLock plock(pParent COMMA_LOCKVAL_SRC_POS);
|
---|
9679 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
9680 | tr("Cannot import image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
9681 | pParent->m->strLocationFull.c_str());
|
---|
9682 | }
|
---|
9683 |
|
---|
9684 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
9685 | * signal from the task initiator (which releases it only after
|
---|
9686 | * RTThreadCreate()) that we can start the job. */
|
---|
9687 | AutoMultiWriteLock2 thisLock(this, pParent COMMA_LOCKVAL_SRC_POS);
|
---|
9688 |
|
---|
9689 | fCreatingTarget = m->state == MediumState_Creating;
|
---|
9690 |
|
---|
9691 | /* The object may request a specific UUID (through a special form of
|
---|
9692 | * the setLocation() argument). Otherwise we have to generate it */
|
---|
9693 | Guid targetId = m->id;
|
---|
9694 |
|
---|
9695 | fGenerateUuid = targetId.isZero();
|
---|
9696 | if (fGenerateUuid)
|
---|
9697 | {
|
---|
9698 | targetId.create();
|
---|
9699 | /* VirtualBox::i_registerMedium() will need UUID */
|
---|
9700 | unconst(m->id) = targetId;
|
---|
9701 | }
|
---|
9702 |
|
---|
9703 |
|
---|
9704 | PVDISK hdd;
|
---|
9705 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9706 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9707 |
|
---|
9708 | try
|
---|
9709 | {
|
---|
9710 | /* Open source medium. */
|
---|
9711 | vrc = VDOpen(hdd,
|
---|
9712 | task.mFormat->i_getId().c_str(),
|
---|
9713 | task.mFilename.c_str(),
|
---|
9714 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SEQUENTIAL | m->uOpenFlagsDef,
|
---|
9715 | task.mVDImageIfaces);
|
---|
9716 | if (RT_FAILURE(vrc))
|
---|
9717 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9718 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9719 | task.mFilename.c_str(),
|
---|
9720 | i_vdError(vrc).c_str());
|
---|
9721 |
|
---|
9722 | Utf8Str targetFormat(m->strFormat);
|
---|
9723 | Utf8Str targetLocation(m->strLocationFull);
|
---|
9724 | uint64_t capabilities = task.mFormat->i_getCapabilities();
|
---|
9725 |
|
---|
9726 | Assert( m->state == MediumState_Creating
|
---|
9727 | || m->state == MediumState_LockedWrite);
|
---|
9728 | Assert( pParent.isNull()
|
---|
9729 | || pParent->m->state == MediumState_LockedRead);
|
---|
9730 |
|
---|
9731 | /* unlock before the potentially lengthy operation */
|
---|
9732 | thisLock.release();
|
---|
9733 |
|
---|
9734 | /* ensure the target directory exists */
|
---|
9735 | if (capabilities & MediumFormatCapabilities_File)
|
---|
9736 | {
|
---|
9737 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
9738 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
9739 | if (FAILED(rc))
|
---|
9740 | throw rc;
|
---|
9741 | }
|
---|
9742 |
|
---|
9743 | PVDISK targetHdd;
|
---|
9744 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &targetHdd);
|
---|
9745 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9746 |
|
---|
9747 | try
|
---|
9748 | {
|
---|
9749 | /* Open all media in the target chain. */
|
---|
9750 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
9751 | task.mpTargetMediumLockList->GetBegin();
|
---|
9752 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
9753 | task.mpTargetMediumLockList->GetEnd();
|
---|
9754 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
9755 | it != targetListEnd;
|
---|
9756 | ++it)
|
---|
9757 | {
|
---|
9758 | const MediumLock &mediumLock = *it;
|
---|
9759 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9760 |
|
---|
9761 | /* If the target medium is not created yet there's no
|
---|
9762 | * reason to open it. */
|
---|
9763 | if (pMedium == this && fCreatingTarget)
|
---|
9764 | continue;
|
---|
9765 |
|
---|
9766 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9767 |
|
---|
9768 | /* sanity check */
|
---|
9769 | Assert( pMedium->m->state == MediumState_LockedRead
|
---|
9770 | || pMedium->m->state == MediumState_LockedWrite);
|
---|
9771 |
|
---|
9772 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
9773 | if (pMedium->m->state != MediumState_LockedWrite)
|
---|
9774 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
9775 | if (pMedium->m->type == MediumType_Shareable)
|
---|
9776 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
9777 |
|
---|
9778 | /* Open all media in appropriate mode. */
|
---|
9779 | vrc = VDOpen(targetHdd,
|
---|
9780 | pMedium->m->strFormat.c_str(),
|
---|
9781 | pMedium->m->strLocationFull.c_str(),
|
---|
9782 | uOpenFlags | m->uOpenFlagsDef,
|
---|
9783 | pMedium->m->vdImageIfaces);
|
---|
9784 | if (RT_FAILURE(vrc))
|
---|
9785 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9786 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9787 | pMedium->m->strLocationFull.c_str(),
|
---|
9788 | i_vdError(vrc).c_str());
|
---|
9789 | }
|
---|
9790 |
|
---|
9791 | vrc = VDCopy(hdd,
|
---|
9792 | VD_LAST_IMAGE,
|
---|
9793 | targetHdd,
|
---|
9794 | targetFormat.c_str(),
|
---|
9795 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
9796 | false /* fMoveByRename */,
|
---|
9797 | 0 /* cbSize */,
|
---|
9798 | task.mVariant & ~MediumVariant_NoCreateDir,
|
---|
9799 | targetId.raw(),
|
---|
9800 | VD_OPEN_FLAGS_NORMAL,
|
---|
9801 | NULL /* pVDIfsOperation */,
|
---|
9802 | m->vdImageIfaces,
|
---|
9803 | task.mVDOperationIfaces);
|
---|
9804 | if (RT_FAILURE(vrc))
|
---|
9805 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9806 | tr("Could not create the imported medium '%s'%s"),
|
---|
9807 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
9808 |
|
---|
9809 | size = VDGetFileSize(targetHdd, VD_LAST_IMAGE);
|
---|
9810 | logicalSize = VDGetSize(targetHdd, VD_LAST_IMAGE);
|
---|
9811 | unsigned uImageFlags;
|
---|
9812 | vrc = VDGetImageFlags(targetHdd, 0, &uImageFlags);
|
---|
9813 | if (RT_SUCCESS(vrc))
|
---|
9814 | variant = (MediumVariant_T)uImageFlags;
|
---|
9815 | }
|
---|
9816 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9817 |
|
---|
9818 | VDDestroy(targetHdd);
|
---|
9819 | }
|
---|
9820 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9821 |
|
---|
9822 | VDDestroy(hdd);
|
---|
9823 | }
|
---|
9824 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9825 |
|
---|
9826 | ErrorInfoKeeper eik;
|
---|
9827 | MultiResult mrc(rcTmp);
|
---|
9828 |
|
---|
9829 | /* Only do the parent changes for newly created media. */
|
---|
9830 | if (SUCCEEDED(mrc) && fCreatingTarget)
|
---|
9831 | {
|
---|
9832 | /* we set m->pParent & children() */
|
---|
9833 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
9834 |
|
---|
9835 | Assert(m->pParent.isNull());
|
---|
9836 |
|
---|
9837 | if (pParent)
|
---|
9838 | {
|
---|
9839 | /* Associate the imported medium with the parent and deassociate
|
---|
9840 | * from VirtualBox. Depth check above. */
|
---|
9841 | i_setParent(pParent);
|
---|
9842 |
|
---|
9843 | /* register with mVirtualBox as the last step and move to
|
---|
9844 | * Created state only on success (leaving an orphan file is
|
---|
9845 | * better than breaking media registry consistency) */
|
---|
9846 | eik.restore();
|
---|
9847 | ComObjPtr<Medium> pMedium;
|
---|
9848 | mrc = pParent->m->pVirtualBox->i_registerMedium(this, &pMedium,
|
---|
9849 | treeLock);
|
---|
9850 | Assert(this == pMedium);
|
---|
9851 | eik.fetch();
|
---|
9852 |
|
---|
9853 | if (FAILED(mrc))
|
---|
9854 | /* break parent association on failure to register */
|
---|
9855 | this->i_deparent(); // removes target from parent
|
---|
9856 | }
|
---|
9857 | else
|
---|
9858 | {
|
---|
9859 | /* just register */
|
---|
9860 | eik.restore();
|
---|
9861 | ComObjPtr<Medium> pMedium;
|
---|
9862 | mrc = m->pVirtualBox->i_registerMedium(this, &pMedium, treeLock);
|
---|
9863 | Assert(this == pMedium);
|
---|
9864 | eik.fetch();
|
---|
9865 | }
|
---|
9866 | }
|
---|
9867 |
|
---|
9868 | if (fCreatingTarget)
|
---|
9869 | {
|
---|
9870 | AutoWriteLock mLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9871 |
|
---|
9872 | if (SUCCEEDED(mrc))
|
---|
9873 | {
|
---|
9874 | m->state = MediumState_Created;
|
---|
9875 |
|
---|
9876 | m->size = size;
|
---|
9877 | m->logicalSize = logicalSize;
|
---|
9878 | m->variant = variant;
|
---|
9879 | }
|
---|
9880 | else
|
---|
9881 | {
|
---|
9882 | /* back to NotCreated on failure */
|
---|
9883 | m->state = MediumState_NotCreated;
|
---|
9884 |
|
---|
9885 | /* reset UUID to prevent it from being reused next time */
|
---|
9886 | if (fGenerateUuid)
|
---|
9887 | unconst(m->id).clear();
|
---|
9888 | }
|
---|
9889 | }
|
---|
9890 |
|
---|
9891 | // now, at the end of this task (always asynchronous), save the settings
|
---|
9892 | {
|
---|
9893 | // save the settings
|
---|
9894 | i_markRegistriesModified();
|
---|
9895 | /* collect multiple errors */
|
---|
9896 | eik.restore();
|
---|
9897 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
9898 | eik.fetch();
|
---|
9899 | }
|
---|
9900 |
|
---|
9901 | /* Everything is explicitly unlocked when the task exits,
|
---|
9902 | * as the task destruction also destroys the target chain. */
|
---|
9903 |
|
---|
9904 | /* Make sure the target chain is released early, otherwise it can
|
---|
9905 | * lead to deadlocks with concurrent IAppliance activities. */
|
---|
9906 | task.mpTargetMediumLockList->Clear();
|
---|
9907 |
|
---|
9908 | return mrc;
|
---|
9909 | }
|
---|
9910 |
|
---|
9911 | /**
|
---|
9912 | * Sets up the encryption settings for a filter.
|
---|
9913 | */
|
---|
9914 | void Medium::i_taskEncryptSettingsSetup(CryptoFilterSettings *pSettings, const char *pszCipher,
|
---|
9915 | const char *pszKeyStore, const char *pszPassword,
|
---|
9916 | bool fCreateKeyStore)
|
---|
9917 | {
|
---|
9918 | pSettings->pszCipher = pszCipher;
|
---|
9919 | pSettings->pszPassword = pszPassword;
|
---|
9920 | pSettings->pszKeyStoreLoad = pszKeyStore;
|
---|
9921 | pSettings->fCreateKeyStore = fCreateKeyStore;
|
---|
9922 | pSettings->pbDek = NULL;
|
---|
9923 | pSettings->cbDek = 0;
|
---|
9924 | pSettings->vdFilterIfaces = NULL;
|
---|
9925 |
|
---|
9926 | pSettings->vdIfCfg.pfnAreKeysValid = i_vdCryptoConfigAreKeysValid;
|
---|
9927 | pSettings->vdIfCfg.pfnQuerySize = i_vdCryptoConfigQuerySize;
|
---|
9928 | pSettings->vdIfCfg.pfnQuery = i_vdCryptoConfigQuery;
|
---|
9929 | pSettings->vdIfCfg.pfnQueryBytes = NULL;
|
---|
9930 |
|
---|
9931 | pSettings->vdIfCrypto.pfnKeyRetain = i_vdCryptoKeyRetain;
|
---|
9932 | pSettings->vdIfCrypto.pfnKeyRelease = i_vdCryptoKeyRelease;
|
---|
9933 | pSettings->vdIfCrypto.pfnKeyStorePasswordRetain = i_vdCryptoKeyStorePasswordRetain;
|
---|
9934 | pSettings->vdIfCrypto.pfnKeyStorePasswordRelease = i_vdCryptoKeyStorePasswordRelease;
|
---|
9935 | pSettings->vdIfCrypto.pfnKeyStoreSave = i_vdCryptoKeyStoreSave;
|
---|
9936 | pSettings->vdIfCrypto.pfnKeyStoreReturnParameters = i_vdCryptoKeyStoreReturnParameters;
|
---|
9937 |
|
---|
9938 | int vrc = VDInterfaceAdd(&pSettings->vdIfCfg.Core,
|
---|
9939 | "Medium::vdInterfaceCfgCrypto",
|
---|
9940 | VDINTERFACETYPE_CONFIG, pSettings,
|
---|
9941 | sizeof(VDINTERFACECONFIG), &pSettings->vdFilterIfaces);
|
---|
9942 | AssertRC(vrc);
|
---|
9943 |
|
---|
9944 | vrc = VDInterfaceAdd(&pSettings->vdIfCrypto.Core,
|
---|
9945 | "Medium::vdInterfaceCrypto",
|
---|
9946 | VDINTERFACETYPE_CRYPTO, pSettings,
|
---|
9947 | sizeof(VDINTERFACECRYPTO), &pSettings->vdFilterIfaces);
|
---|
9948 | AssertRC(vrc);
|
---|
9949 | }
|
---|
9950 |
|
---|
9951 | /**
|
---|
9952 | * Implementation code for the "encrypt" task.
|
---|
9953 | *
|
---|
9954 | * @param task
|
---|
9955 | * @return
|
---|
9956 | */
|
---|
9957 | HRESULT Medium::i_taskEncryptHandler(Medium::EncryptTask &task)
|
---|
9958 | {
|
---|
9959 | # ifndef VBOX_WITH_EXTPACK
|
---|
9960 | RT_NOREF(task);
|
---|
9961 | # endif
|
---|
9962 | HRESULT rc = S_OK;
|
---|
9963 |
|
---|
9964 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
9965 | * signal from the task initiator (which releases it only after
|
---|
9966 | * RTThreadCreate()) that we can start the job. */
|
---|
9967 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
9968 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9969 |
|
---|
9970 | try
|
---|
9971 | {
|
---|
9972 | # ifdef VBOX_WITH_EXTPACK
|
---|
9973 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
9974 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
9975 | {
|
---|
9976 | /* Load the plugin */
|
---|
9977 | Utf8Str strPlugin;
|
---|
9978 | rc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
9979 | if (SUCCEEDED(rc))
|
---|
9980 | {
|
---|
9981 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
9982 | if (RT_FAILURE(vrc))
|
---|
9983 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
9984 | tr("Encrypting the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
9985 | i_vdError(vrc).c_str());
|
---|
9986 | }
|
---|
9987 | else
|
---|
9988 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
9989 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
9990 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
9991 | }
|
---|
9992 | else
|
---|
9993 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
9994 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
9995 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
9996 |
|
---|
9997 | PVDISK pDisk = NULL;
|
---|
9998 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
9999 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
10000 |
|
---|
10001 | Medium::CryptoFilterSettings CryptoSettingsRead;
|
---|
10002 | Medium::CryptoFilterSettings CryptoSettingsWrite;
|
---|
10003 |
|
---|
10004 | void *pvBuf = NULL;
|
---|
10005 | const char *pszPasswordNew = NULL;
|
---|
10006 | try
|
---|
10007 | {
|
---|
10008 | /* Set up disk encryption filters. */
|
---|
10009 | if (task.mstrCurrentPassword.isEmpty())
|
---|
10010 | {
|
---|
10011 | /*
|
---|
10012 | * Query whether the medium property indicating that encryption is
|
---|
10013 | * configured is existing.
|
---|
10014 | */
|
---|
10015 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
10016 | if (it != pBase->m->mapProperties.end())
|
---|
10017 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
10018 | tr("The password given for the encrypted image is incorrect"));
|
---|
10019 | }
|
---|
10020 | else
|
---|
10021 | {
|
---|
10022 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
10023 | if (it == pBase->m->mapProperties.end())
|
---|
10024 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10025 | tr("The image is not configured for encryption"));
|
---|
10026 |
|
---|
10027 | i_taskEncryptSettingsSetup(&CryptoSettingsRead, NULL, it->second.c_str(), task.mstrCurrentPassword.c_str(),
|
---|
10028 | false /* fCreateKeyStore */);
|
---|
10029 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ, CryptoSettingsRead.vdFilterIfaces);
|
---|
10030 | if (vrc == VERR_VD_PASSWORD_INCORRECT)
|
---|
10031 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
10032 | tr("The password to decrypt the image is incorrect"));
|
---|
10033 | else if (RT_FAILURE(vrc))
|
---|
10034 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10035 | tr("Failed to load the decryption filter: %s"),
|
---|
10036 | i_vdError(vrc).c_str());
|
---|
10037 | }
|
---|
10038 |
|
---|
10039 | if (task.mstrCipher.isNotEmpty())
|
---|
10040 | {
|
---|
10041 | if ( task.mstrNewPassword.isEmpty()
|
---|
10042 | && task.mstrNewPasswordId.isEmpty()
|
---|
10043 | && task.mstrCurrentPassword.isNotEmpty())
|
---|
10044 | {
|
---|
10045 | /* An empty password and password ID will default to the current password. */
|
---|
10046 | pszPasswordNew = task.mstrCurrentPassword.c_str();
|
---|
10047 | }
|
---|
10048 | else if (task.mstrNewPassword.isEmpty())
|
---|
10049 | throw setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
10050 | tr("A password must be given for the image encryption"));
|
---|
10051 | else if (task.mstrNewPasswordId.isEmpty())
|
---|
10052 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10053 | tr("A valid identifier for the password must be given"));
|
---|
10054 | else
|
---|
10055 | pszPasswordNew = task.mstrNewPassword.c_str();
|
---|
10056 |
|
---|
10057 | i_taskEncryptSettingsSetup(&CryptoSettingsWrite, task.mstrCipher.c_str(), NULL,
|
---|
10058 | pszPasswordNew, true /* fCreateKeyStore */);
|
---|
10059 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_WRITE, CryptoSettingsWrite.vdFilterIfaces);
|
---|
10060 | if (RT_FAILURE(vrc))
|
---|
10061 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10062 | tr("Failed to load the encryption filter: %s"),
|
---|
10063 | i_vdError(vrc).c_str());
|
---|
10064 | }
|
---|
10065 | else if (task.mstrNewPasswordId.isNotEmpty() || task.mstrNewPassword.isNotEmpty())
|
---|
10066 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10067 | tr("The password and password identifier must be empty if the output should be unencrypted"));
|
---|
10068 |
|
---|
10069 | /* Open all media in the chain. */
|
---|
10070 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
10071 | task.mpMediumLockList->GetBegin();
|
---|
10072 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
10073 | task.mpMediumLockList->GetEnd();
|
---|
10074 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
10075 | mediumListEnd;
|
---|
10076 | --mediumListLast;
|
---|
10077 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
10078 | it != mediumListEnd;
|
---|
10079 | ++it)
|
---|
10080 | {
|
---|
10081 | const MediumLock &mediumLock = *it;
|
---|
10082 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
10083 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
10084 |
|
---|
10085 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
10086 |
|
---|
10087 | /* Open all media but last in read-only mode. Do not handle
|
---|
10088 | * shareable media, as compaction and sharing are mutually
|
---|
10089 | * exclusive. */
|
---|
10090 | vrc = VDOpen(pDisk,
|
---|
10091 | pMedium->m->strFormat.c_str(),
|
---|
10092 | pMedium->m->strLocationFull.c_str(),
|
---|
10093 | m->uOpenFlagsDef | (it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
10094 | pMedium->m->vdImageIfaces);
|
---|
10095 | if (RT_FAILURE(vrc))
|
---|
10096 | throw setError(VBOX_E_FILE_ERROR,
|
---|
10097 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
10098 | pMedium->m->strLocationFull.c_str(),
|
---|
10099 | i_vdError(vrc).c_str());
|
---|
10100 | }
|
---|
10101 |
|
---|
10102 | Assert(m->state == MediumState_LockedWrite);
|
---|
10103 |
|
---|
10104 | Utf8Str location(m->strLocationFull);
|
---|
10105 |
|
---|
10106 | /* unlock before the potentially lengthy operation */
|
---|
10107 | thisLock.release();
|
---|
10108 |
|
---|
10109 | vrc = VDPrepareWithFilters(pDisk, task.mVDOperationIfaces);
|
---|
10110 | if (RT_FAILURE(vrc))
|
---|
10111 | throw setError(VBOX_E_FILE_ERROR,
|
---|
10112 | tr("Could not prepare disk images for encryption (%Rrc): %s"),
|
---|
10113 | vrc, i_vdError(vrc).c_str());
|
---|
10114 |
|
---|
10115 | thisLock.acquire();
|
---|
10116 | /* If everything went well set the new key store. */
|
---|
10117 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
10118 | if (it != pBase->m->mapProperties.end())
|
---|
10119 | pBase->m->mapProperties.erase(it);
|
---|
10120 |
|
---|
10121 | /* Delete KeyId if encryption is removed or the password did change. */
|
---|
10122 | if ( task.mstrNewPasswordId.isNotEmpty()
|
---|
10123 | || task.mstrCipher.isEmpty())
|
---|
10124 | {
|
---|
10125 | it = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
10126 | if (it != pBase->m->mapProperties.end())
|
---|
10127 | pBase->m->mapProperties.erase(it);
|
---|
10128 | }
|
---|
10129 |
|
---|
10130 | if (CryptoSettingsWrite.pszKeyStore)
|
---|
10131 | {
|
---|
10132 | pBase->m->mapProperties["CRYPT/KeyStore"] = Utf8Str(CryptoSettingsWrite.pszKeyStore);
|
---|
10133 | if (task.mstrNewPasswordId.isNotEmpty())
|
---|
10134 | pBase->m->mapProperties["CRYPT/KeyId"] = task.mstrNewPasswordId;
|
---|
10135 | }
|
---|
10136 |
|
---|
10137 | if (CryptoSettingsRead.pszCipherReturned)
|
---|
10138 | RTStrFree(CryptoSettingsRead.pszCipherReturned);
|
---|
10139 |
|
---|
10140 | if (CryptoSettingsWrite.pszCipherReturned)
|
---|
10141 | RTStrFree(CryptoSettingsWrite.pszCipherReturned);
|
---|
10142 |
|
---|
10143 | thisLock.release();
|
---|
10144 | pBase->i_markRegistriesModified();
|
---|
10145 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
10146 | }
|
---|
10147 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10148 |
|
---|
10149 | if (pvBuf)
|
---|
10150 | RTMemFree(pvBuf);
|
---|
10151 |
|
---|
10152 | VDDestroy(pDisk);
|
---|
10153 | # else
|
---|
10154 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
10155 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
10156 | # endif
|
---|
10157 | }
|
---|
10158 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10159 |
|
---|
10160 | /* Everything is explicitly unlocked when the task exits,
|
---|
10161 | * as the task destruction also destroys the media chain. */
|
---|
10162 |
|
---|
10163 | return rc;
|
---|
10164 | }
|
---|
10165 |
|
---|
10166 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|