1 | /* $Id: MediumImpl.cpp 67745 2017-07-01 11:26:18Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox COM class implementation
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2016 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 | #include "MediumImpl.h"
|
---|
18 | #include "TokenImpl.h"
|
---|
19 | #include "ProgressImpl.h"
|
---|
20 | #include "SystemPropertiesImpl.h"
|
---|
21 | #include "VirtualBoxImpl.h"
|
---|
22 | #include "ExtPackManagerImpl.h"
|
---|
23 |
|
---|
24 | #include "AutoCaller.h"
|
---|
25 | #include "Logging.h"
|
---|
26 | #include "ThreadTask.h"
|
---|
27 | #include "VBox/com/MultiResult.h"
|
---|
28 | #include "VBox/com/ErrorInfo.h"
|
---|
29 |
|
---|
30 | #include <VBox/err.h>
|
---|
31 | #include <VBox/settings.h>
|
---|
32 |
|
---|
33 | #include <iprt/param.h>
|
---|
34 | #include <iprt/path.h>
|
---|
35 | #include <iprt/file.h>
|
---|
36 | #include <iprt/tcp.h>
|
---|
37 | #include <iprt/cpp/utils.h>
|
---|
38 | #include <iprt/memsafer.h>
|
---|
39 | #include <iprt/base64.h>
|
---|
40 |
|
---|
41 | #include <VBox/vd.h>
|
---|
42 |
|
---|
43 | #include <algorithm>
|
---|
44 | #include <list>
|
---|
45 |
|
---|
46 |
|
---|
47 | typedef std::list<Guid> GuidList;
|
---|
48 |
|
---|
49 |
|
---|
50 | #ifdef VBOX_WITH_EXTPACK
|
---|
51 | static const char g_szVDPlugin[] = "VDPluginCrypt";
|
---|
52 | #endif
|
---|
53 |
|
---|
54 |
|
---|
55 | ////////////////////////////////////////////////////////////////////////////////
|
---|
56 | //
|
---|
57 | // Medium data definition
|
---|
58 | //
|
---|
59 | ////////////////////////////////////////////////////////////////////////////////
|
---|
60 |
|
---|
61 | /** Describes how a machine refers to this medium. */
|
---|
62 | struct BackRef
|
---|
63 | {
|
---|
64 | /** Equality predicate for stdc++. */
|
---|
65 | struct EqualsTo : public std::unary_function <BackRef, bool>
|
---|
66 | {
|
---|
67 | explicit EqualsTo(const Guid &aMachineId) : machineId(aMachineId) {}
|
---|
68 |
|
---|
69 | bool operator()(const argument_type &aThat) const
|
---|
70 | {
|
---|
71 | return aThat.machineId == machineId;
|
---|
72 | }
|
---|
73 |
|
---|
74 | const Guid machineId;
|
---|
75 | };
|
---|
76 |
|
---|
77 | BackRef(const Guid &aMachineId,
|
---|
78 | const Guid &aSnapshotId = Guid::Empty)
|
---|
79 | : machineId(aMachineId),
|
---|
80 | fInCurState(aSnapshotId.isZero())
|
---|
81 | {
|
---|
82 | if (aSnapshotId.isValid() && !aSnapshotId.isZero())
|
---|
83 | llSnapshotIds.push_back(aSnapshotId);
|
---|
84 | }
|
---|
85 |
|
---|
86 | Guid machineId;
|
---|
87 | bool fInCurState : 1;
|
---|
88 | GuidList llSnapshotIds;
|
---|
89 | };
|
---|
90 |
|
---|
91 | typedef std::list<BackRef> BackRefList;
|
---|
92 |
|
---|
93 | struct Medium::Data
|
---|
94 | {
|
---|
95 | Data()
|
---|
96 | : pVirtualBox(NULL),
|
---|
97 | state(MediumState_NotCreated),
|
---|
98 | variant(MediumVariant_Standard),
|
---|
99 | size(0),
|
---|
100 | readers(0),
|
---|
101 | preLockState(MediumState_NotCreated),
|
---|
102 | queryInfoSem(LOCKCLASS_MEDIUMQUERY),
|
---|
103 | queryInfoRunning(false),
|
---|
104 | type(MediumType_Normal),
|
---|
105 | devType(DeviceType_HardDisk),
|
---|
106 | logicalSize(0),
|
---|
107 | hddOpenMode(OpenReadWrite),
|
---|
108 | autoReset(false),
|
---|
109 | hostDrive(false),
|
---|
110 | implicit(false),
|
---|
111 | fClosing(false),
|
---|
112 | uOpenFlagsDef(VD_OPEN_FLAGS_IGNORE_FLUSH),
|
---|
113 | numCreateDiffTasks(0),
|
---|
114 | vdDiskIfaces(NULL),
|
---|
115 | vdImageIfaces(NULL),
|
---|
116 | fMoveThisMedium(false)
|
---|
117 | { }
|
---|
118 |
|
---|
119 | /** weak VirtualBox parent */
|
---|
120 | VirtualBox * const pVirtualBox;
|
---|
121 |
|
---|
122 | // pParent and llChildren are protected by VirtualBox::i_getMediaTreeLockHandle()
|
---|
123 | ComObjPtr<Medium> pParent;
|
---|
124 | MediaList llChildren; // to add a child, just call push_back; to remove
|
---|
125 | // a child, call child->deparent() which does a lookup
|
---|
126 |
|
---|
127 | GuidList llRegistryIDs; // media registries in which this medium is listed
|
---|
128 |
|
---|
129 | const Guid id;
|
---|
130 | Utf8Str strDescription;
|
---|
131 | MediumState_T state;
|
---|
132 | MediumVariant_T variant;
|
---|
133 | Utf8Str strLocationFull;
|
---|
134 | uint64_t size;
|
---|
135 | Utf8Str strLastAccessError;
|
---|
136 |
|
---|
137 | BackRefList backRefs;
|
---|
138 |
|
---|
139 | size_t readers;
|
---|
140 | MediumState_T preLockState;
|
---|
141 |
|
---|
142 | /** Special synchronization for operations which must wait for
|
---|
143 | * Medium::i_queryInfo in another thread to complete. Using a SemRW is
|
---|
144 | * not quite ideal, but at least it is subject to the lock validator,
|
---|
145 | * unlike the SemEventMulti which we had here for many years. Catching
|
---|
146 | * possible deadlocks is more important than a tiny bit of efficiency. */
|
---|
147 | RWLockHandle queryInfoSem;
|
---|
148 | bool queryInfoRunning : 1;
|
---|
149 |
|
---|
150 | const Utf8Str strFormat;
|
---|
151 | ComObjPtr<MediumFormat> formatObj;
|
---|
152 |
|
---|
153 | MediumType_T type;
|
---|
154 | DeviceType_T devType;
|
---|
155 | uint64_t logicalSize;
|
---|
156 |
|
---|
157 | HDDOpenMode hddOpenMode;
|
---|
158 |
|
---|
159 | bool autoReset : 1;
|
---|
160 |
|
---|
161 | /** New UUID to be set on the next Medium::i_queryInfo call. */
|
---|
162 | const Guid uuidImage;
|
---|
163 | /** New parent UUID to be set on the next Medium::i_queryInfo call. */
|
---|
164 | const Guid uuidParentImage;
|
---|
165 |
|
---|
166 | bool hostDrive : 1;
|
---|
167 |
|
---|
168 | settings::StringsMap mapProperties;
|
---|
169 |
|
---|
170 | bool implicit : 1;
|
---|
171 | /** Flag whether the medium is in the process of being closed. */
|
---|
172 | bool fClosing: 1;
|
---|
173 |
|
---|
174 | /** Default flags passed to VDOpen(). */
|
---|
175 | unsigned uOpenFlagsDef;
|
---|
176 |
|
---|
177 | uint32_t numCreateDiffTasks;
|
---|
178 |
|
---|
179 | Utf8Str vdError; /*< Error remembered by the VD error callback. */
|
---|
180 |
|
---|
181 | VDINTERFACEERROR vdIfError;
|
---|
182 |
|
---|
183 | VDINTERFACECONFIG vdIfConfig;
|
---|
184 |
|
---|
185 | VDINTERFACETCPNET vdIfTcpNet;
|
---|
186 |
|
---|
187 | PVDINTERFACE vdDiskIfaces;
|
---|
188 | PVDINTERFACE vdImageIfaces;
|
---|
189 |
|
---|
190 | /** Flag if the medium is going to move to a new
|
---|
191 | * location. */
|
---|
192 | bool fMoveThisMedium;
|
---|
193 | /** new location path */
|
---|
194 | Utf8Str strNewLocationFull;
|
---|
195 | };
|
---|
196 |
|
---|
197 | typedef struct VDSOCKETINT
|
---|
198 | {
|
---|
199 | /** Socket handle. */
|
---|
200 | RTSOCKET hSocket;
|
---|
201 | } VDSOCKETINT, *PVDSOCKETINT;
|
---|
202 |
|
---|
203 | ////////////////////////////////////////////////////////////////////////////////
|
---|
204 | //
|
---|
205 | // Globals
|
---|
206 | //
|
---|
207 | ////////////////////////////////////////////////////////////////////////////////
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Medium::Task class for asynchronous operations.
|
---|
211 | *
|
---|
212 | * @note Instances of this class must be created using new() because the
|
---|
213 | * task thread function will delete them when the task is complete.
|
---|
214 | *
|
---|
215 | * @note The constructor of this class adds a caller on the managed Medium
|
---|
216 | * object which is automatically released upon destruction.
|
---|
217 | */
|
---|
218 | class Medium::Task : public ThreadTask
|
---|
219 | {
|
---|
220 | public:
|
---|
221 | Task(Medium *aMedium, Progress *aProgress)
|
---|
222 | : ThreadTask("Medium::Task"),
|
---|
223 | mVDOperationIfaces(NULL),
|
---|
224 | mMedium(aMedium),
|
---|
225 | mMediumCaller(aMedium),
|
---|
226 | mProgress(aProgress),
|
---|
227 | mVirtualBoxCaller(NULL)
|
---|
228 | {
|
---|
229 | AssertReturnVoidStmt(aMedium, mRC = E_FAIL);
|
---|
230 | mRC = mMediumCaller.rc();
|
---|
231 | if (FAILED(mRC))
|
---|
232 | return;
|
---|
233 |
|
---|
234 | /* Get strong VirtualBox reference, see below. */
|
---|
235 | VirtualBox *pVirtualBox = aMedium->m->pVirtualBox;
|
---|
236 | mVirtualBox = pVirtualBox;
|
---|
237 | mVirtualBoxCaller.attach(pVirtualBox);
|
---|
238 | mRC = mVirtualBoxCaller.rc();
|
---|
239 | if (FAILED(mRC))
|
---|
240 | return;
|
---|
241 |
|
---|
242 | /* Set up a per-operation progress interface, can be used freely (for
|
---|
243 | * binary operations you can use it either on the source or target). */
|
---|
244 | mVDIfProgress.pfnProgress = vdProgressCall;
|
---|
245 | int vrc = VDInterfaceAdd(&mVDIfProgress.Core,
|
---|
246 | "Medium::Task::vdInterfaceProgress",
|
---|
247 | VDINTERFACETYPE_PROGRESS,
|
---|
248 | mProgress,
|
---|
249 | sizeof(VDINTERFACEPROGRESS),
|
---|
250 | &mVDOperationIfaces);
|
---|
251 | AssertRC(vrc);
|
---|
252 | if (RT_FAILURE(vrc))
|
---|
253 | mRC = E_FAIL;
|
---|
254 | }
|
---|
255 |
|
---|
256 | // Make all destructors virtual. Just in case.
|
---|
257 | virtual ~Task()
|
---|
258 | {
|
---|
259 | /* send the notification of completion.*/
|
---|
260 | if ( isAsync()
|
---|
261 | && !mProgress.isNull())
|
---|
262 | mProgress->i_notifyComplete(mRC);
|
---|
263 | }
|
---|
264 |
|
---|
265 | HRESULT rc() const { return mRC; }
|
---|
266 | bool isOk() const { return SUCCEEDED(rc()); }
|
---|
267 |
|
---|
268 | const ComPtr<Progress>& GetProgressObject() const {return mProgress;}
|
---|
269 |
|
---|
270 | /**
|
---|
271 | * Runs Medium::Task::executeTask() on the current thread
|
---|
272 | * instead of creating a new one.
|
---|
273 | */
|
---|
274 | HRESULT runNow()
|
---|
275 | {
|
---|
276 | LogFlowFuncEnter();
|
---|
277 |
|
---|
278 | mRC = executeTask();
|
---|
279 |
|
---|
280 | LogFlowFunc(("rc=%Rhrc\n", mRC));
|
---|
281 | LogFlowFuncLeave();
|
---|
282 | return mRC;
|
---|
283 | }
|
---|
284 |
|
---|
285 | /**
|
---|
286 | * Implementation code for the "create base" task.
|
---|
287 | * Used as function for execution from a standalone thread.
|
---|
288 | */
|
---|
289 | void handler()
|
---|
290 | {
|
---|
291 | LogFlowFuncEnter();
|
---|
292 | try
|
---|
293 | {
|
---|
294 | mRC = executeTask(); /* (destructor picks up mRC, see above) */
|
---|
295 | LogFlowFunc(("rc=%Rhrc\n", mRC));
|
---|
296 | }
|
---|
297 | catch (...)
|
---|
298 | {
|
---|
299 | LogRel(("Some exception in the function Medium::Task:handler()\n"));
|
---|
300 | }
|
---|
301 |
|
---|
302 | LogFlowFuncLeave();
|
---|
303 | }
|
---|
304 |
|
---|
305 | PVDINTERFACE mVDOperationIfaces;
|
---|
306 |
|
---|
307 | const ComObjPtr<Medium> mMedium;
|
---|
308 | AutoCaller mMediumCaller;
|
---|
309 |
|
---|
310 | protected:
|
---|
311 | HRESULT mRC;
|
---|
312 |
|
---|
313 | private:
|
---|
314 | virtual HRESULT executeTask() = 0;
|
---|
315 |
|
---|
316 | const ComObjPtr<Progress> mProgress;
|
---|
317 |
|
---|
318 | static DECLCALLBACK(int) vdProgressCall(void *pvUser, unsigned uPercent);
|
---|
319 |
|
---|
320 | VDINTERFACEPROGRESS mVDIfProgress;
|
---|
321 |
|
---|
322 | /* Must have a strong VirtualBox reference during a task otherwise the
|
---|
323 | * reference count might drop to 0 while a task is still running. This
|
---|
324 | * would result in weird behavior, including deadlocks due to uninit and
|
---|
325 | * locking order issues. The deadlock often is not detectable because the
|
---|
326 | * uninit uses event semaphores which sabotages deadlock detection. */
|
---|
327 | ComObjPtr<VirtualBox> mVirtualBox;
|
---|
328 | AutoCaller mVirtualBoxCaller;
|
---|
329 | };
|
---|
330 |
|
---|
331 | HRESULT Medium::Task::executeTask()
|
---|
332 | {
|
---|
333 | return E_NOTIMPL;//ReturnComNotImplemented()
|
---|
334 | }
|
---|
335 |
|
---|
336 | class Medium::CreateBaseTask : public Medium::Task
|
---|
337 | {
|
---|
338 | public:
|
---|
339 | CreateBaseTask(Medium *aMedium,
|
---|
340 | Progress *aProgress,
|
---|
341 | uint64_t aSize,
|
---|
342 | MediumVariant_T aVariant)
|
---|
343 | : Medium::Task(aMedium, aProgress),
|
---|
344 | mSize(aSize),
|
---|
345 | mVariant(aVariant)
|
---|
346 | {
|
---|
347 | m_strTaskName = "createBase";
|
---|
348 | }
|
---|
349 |
|
---|
350 | uint64_t mSize;
|
---|
351 | MediumVariant_T mVariant;
|
---|
352 |
|
---|
353 | private:
|
---|
354 | HRESULT executeTask();
|
---|
355 | };
|
---|
356 |
|
---|
357 | class Medium::CreateDiffTask : public Medium::Task
|
---|
358 | {
|
---|
359 | public:
|
---|
360 | CreateDiffTask(Medium *aMedium,
|
---|
361 | Progress *aProgress,
|
---|
362 | Medium *aTarget,
|
---|
363 | MediumVariant_T aVariant,
|
---|
364 | MediumLockList *aMediumLockList,
|
---|
365 | bool fKeepMediumLockList = false)
|
---|
366 | : Medium::Task(aMedium, aProgress),
|
---|
367 | mpMediumLockList(aMediumLockList),
|
---|
368 | mTarget(aTarget),
|
---|
369 | mVariant(aVariant),
|
---|
370 | mTargetCaller(aTarget),
|
---|
371 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
372 | {
|
---|
373 | AssertReturnVoidStmt(aTarget != NULL, mRC = E_FAIL);
|
---|
374 | mRC = mTargetCaller.rc();
|
---|
375 | if (FAILED(mRC))
|
---|
376 | return;
|
---|
377 | m_strTaskName = "createDiff";
|
---|
378 | }
|
---|
379 |
|
---|
380 | ~CreateDiffTask()
|
---|
381 | {
|
---|
382 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
383 | delete mpMediumLockList;
|
---|
384 | }
|
---|
385 |
|
---|
386 | MediumLockList *mpMediumLockList;
|
---|
387 |
|
---|
388 | const ComObjPtr<Medium> mTarget;
|
---|
389 | MediumVariant_T mVariant;
|
---|
390 |
|
---|
391 | private:
|
---|
392 | HRESULT executeTask();
|
---|
393 | AutoCaller mTargetCaller;
|
---|
394 | bool mfKeepMediumLockList;
|
---|
395 | };
|
---|
396 |
|
---|
397 | class Medium::CloneTask : public Medium::Task
|
---|
398 | {
|
---|
399 | public:
|
---|
400 | CloneTask(Medium *aMedium,
|
---|
401 | Progress *aProgress,
|
---|
402 | Medium *aTarget,
|
---|
403 | MediumVariant_T aVariant,
|
---|
404 | Medium *aParent,
|
---|
405 | uint32_t idxSrcImageSame,
|
---|
406 | uint32_t idxDstImageSame,
|
---|
407 | MediumLockList *aSourceMediumLockList,
|
---|
408 | MediumLockList *aTargetMediumLockList,
|
---|
409 | bool fKeepSourceMediumLockList = false,
|
---|
410 | bool fKeepTargetMediumLockList = false)
|
---|
411 | : Medium::Task(aMedium, aProgress),
|
---|
412 | mTarget(aTarget),
|
---|
413 | mParent(aParent),
|
---|
414 | mpSourceMediumLockList(aSourceMediumLockList),
|
---|
415 | mpTargetMediumLockList(aTargetMediumLockList),
|
---|
416 | mVariant(aVariant),
|
---|
417 | midxSrcImageSame(idxSrcImageSame),
|
---|
418 | midxDstImageSame(idxDstImageSame),
|
---|
419 | mTargetCaller(aTarget),
|
---|
420 | mParentCaller(aParent),
|
---|
421 | mfKeepSourceMediumLockList(fKeepSourceMediumLockList),
|
---|
422 | mfKeepTargetMediumLockList(fKeepTargetMediumLockList)
|
---|
423 | {
|
---|
424 | AssertReturnVoidStmt(aTarget != NULL, mRC = E_FAIL);
|
---|
425 | mRC = mTargetCaller.rc();
|
---|
426 | if (FAILED(mRC))
|
---|
427 | return;
|
---|
428 | /* aParent may be NULL */
|
---|
429 | mRC = mParentCaller.rc();
|
---|
430 | if (FAILED(mRC))
|
---|
431 | return;
|
---|
432 | AssertReturnVoidStmt(aSourceMediumLockList != NULL, mRC = E_FAIL);
|
---|
433 | AssertReturnVoidStmt(aTargetMediumLockList != NULL, mRC = E_FAIL);
|
---|
434 | m_strTaskName = "createClone";
|
---|
435 | }
|
---|
436 |
|
---|
437 | ~CloneTask()
|
---|
438 | {
|
---|
439 | if (!mfKeepSourceMediumLockList && mpSourceMediumLockList)
|
---|
440 | delete mpSourceMediumLockList;
|
---|
441 | if (!mfKeepTargetMediumLockList && mpTargetMediumLockList)
|
---|
442 | delete mpTargetMediumLockList;
|
---|
443 | }
|
---|
444 |
|
---|
445 | const ComObjPtr<Medium> mTarget;
|
---|
446 | const ComObjPtr<Medium> mParent;
|
---|
447 | MediumLockList *mpSourceMediumLockList;
|
---|
448 | MediumLockList *mpTargetMediumLockList;
|
---|
449 | MediumVariant_T mVariant;
|
---|
450 | uint32_t midxSrcImageSame;
|
---|
451 | uint32_t midxDstImageSame;
|
---|
452 |
|
---|
453 | private:
|
---|
454 | HRESULT executeTask();
|
---|
455 | AutoCaller mTargetCaller;
|
---|
456 | AutoCaller mParentCaller;
|
---|
457 | bool mfKeepSourceMediumLockList;
|
---|
458 | bool mfKeepTargetMediumLockList;
|
---|
459 | };
|
---|
460 |
|
---|
461 | class Medium::MoveTask : public Medium::Task
|
---|
462 | {
|
---|
463 | public:
|
---|
464 | MoveTask(Medium *aMedium,
|
---|
465 | Progress *aProgress,
|
---|
466 | MediumVariant_T aVariant,
|
---|
467 | MediumLockList *aMediumLockList,
|
---|
468 | bool fKeepMediumLockList = false)
|
---|
469 | : Medium::Task(aMedium, aProgress),
|
---|
470 | mpMediumLockList(aMediumLockList),
|
---|
471 | mVariant(aVariant),
|
---|
472 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
473 | {
|
---|
474 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
475 | m_strTaskName = "createMove";
|
---|
476 | }
|
---|
477 |
|
---|
478 | ~MoveTask()
|
---|
479 | {
|
---|
480 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
481 | delete mpMediumLockList;
|
---|
482 | }
|
---|
483 |
|
---|
484 | MediumLockList *mpMediumLockList;
|
---|
485 | MediumVariant_T mVariant;
|
---|
486 |
|
---|
487 | private:
|
---|
488 | HRESULT executeTask();
|
---|
489 | bool mfKeepMediumLockList;
|
---|
490 | };
|
---|
491 |
|
---|
492 | class Medium::CompactTask : public Medium::Task
|
---|
493 | {
|
---|
494 | public:
|
---|
495 | CompactTask(Medium *aMedium,
|
---|
496 | Progress *aProgress,
|
---|
497 | MediumLockList *aMediumLockList,
|
---|
498 | bool fKeepMediumLockList = false)
|
---|
499 | : Medium::Task(aMedium, aProgress),
|
---|
500 | mpMediumLockList(aMediumLockList),
|
---|
501 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
502 | {
|
---|
503 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
504 | m_strTaskName = "createCompact";
|
---|
505 | }
|
---|
506 |
|
---|
507 | ~CompactTask()
|
---|
508 | {
|
---|
509 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
510 | delete mpMediumLockList;
|
---|
511 | }
|
---|
512 |
|
---|
513 | MediumLockList *mpMediumLockList;
|
---|
514 |
|
---|
515 | private:
|
---|
516 | HRESULT executeTask();
|
---|
517 | bool mfKeepMediumLockList;
|
---|
518 | };
|
---|
519 |
|
---|
520 | class Medium::ResizeTask : public Medium::Task
|
---|
521 | {
|
---|
522 | public:
|
---|
523 | ResizeTask(Medium *aMedium,
|
---|
524 | uint64_t aSize,
|
---|
525 | Progress *aProgress,
|
---|
526 | MediumLockList *aMediumLockList,
|
---|
527 | bool fKeepMediumLockList = false)
|
---|
528 | : Medium::Task(aMedium, aProgress),
|
---|
529 | mSize(aSize),
|
---|
530 | mpMediumLockList(aMediumLockList),
|
---|
531 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
532 | {
|
---|
533 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
534 | m_strTaskName = "createResize";
|
---|
535 | }
|
---|
536 |
|
---|
537 | ~ResizeTask()
|
---|
538 | {
|
---|
539 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
540 | delete mpMediumLockList;
|
---|
541 | }
|
---|
542 |
|
---|
543 | uint64_t mSize;
|
---|
544 | MediumLockList *mpMediumLockList;
|
---|
545 |
|
---|
546 | private:
|
---|
547 | HRESULT executeTask();
|
---|
548 | bool mfKeepMediumLockList;
|
---|
549 | };
|
---|
550 |
|
---|
551 | class Medium::ResetTask : public Medium::Task
|
---|
552 | {
|
---|
553 | public:
|
---|
554 | ResetTask(Medium *aMedium,
|
---|
555 | Progress *aProgress,
|
---|
556 | MediumLockList *aMediumLockList,
|
---|
557 | bool fKeepMediumLockList = false)
|
---|
558 | : Medium::Task(aMedium, aProgress),
|
---|
559 | mpMediumLockList(aMediumLockList),
|
---|
560 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
561 | {
|
---|
562 | m_strTaskName = "createReset";
|
---|
563 | }
|
---|
564 |
|
---|
565 | ~ResetTask()
|
---|
566 | {
|
---|
567 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
568 | delete mpMediumLockList;
|
---|
569 | }
|
---|
570 |
|
---|
571 | MediumLockList *mpMediumLockList;
|
---|
572 |
|
---|
573 | private:
|
---|
574 | HRESULT executeTask();
|
---|
575 | bool mfKeepMediumLockList;
|
---|
576 | };
|
---|
577 |
|
---|
578 | class Medium::DeleteTask : public Medium::Task
|
---|
579 | {
|
---|
580 | public:
|
---|
581 | DeleteTask(Medium *aMedium,
|
---|
582 | Progress *aProgress,
|
---|
583 | MediumLockList *aMediumLockList,
|
---|
584 | bool fKeepMediumLockList = false)
|
---|
585 | : Medium::Task(aMedium, aProgress),
|
---|
586 | mpMediumLockList(aMediumLockList),
|
---|
587 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
588 | {
|
---|
589 | m_strTaskName = "createDelete";
|
---|
590 | }
|
---|
591 |
|
---|
592 | ~DeleteTask()
|
---|
593 | {
|
---|
594 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
595 | delete mpMediumLockList;
|
---|
596 | }
|
---|
597 |
|
---|
598 | MediumLockList *mpMediumLockList;
|
---|
599 |
|
---|
600 | private:
|
---|
601 | HRESULT executeTask();
|
---|
602 | bool mfKeepMediumLockList;
|
---|
603 | };
|
---|
604 |
|
---|
605 | class Medium::MergeTask : public Medium::Task
|
---|
606 | {
|
---|
607 | public:
|
---|
608 | MergeTask(Medium *aMedium,
|
---|
609 | Medium *aTarget,
|
---|
610 | bool fMergeForward,
|
---|
611 | Medium *aParentForTarget,
|
---|
612 | MediumLockList *aChildrenToReparent,
|
---|
613 | Progress *aProgress,
|
---|
614 | MediumLockList *aMediumLockList,
|
---|
615 | bool fKeepMediumLockList = false)
|
---|
616 | : Medium::Task(aMedium, aProgress),
|
---|
617 | mTarget(aTarget),
|
---|
618 | mfMergeForward(fMergeForward),
|
---|
619 | mParentForTarget(aParentForTarget),
|
---|
620 | mpChildrenToReparent(aChildrenToReparent),
|
---|
621 | mpMediumLockList(aMediumLockList),
|
---|
622 | mTargetCaller(aTarget),
|
---|
623 | mParentForTargetCaller(aParentForTarget),
|
---|
624 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
625 | {
|
---|
626 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
627 | m_strTaskName = "createMerge";
|
---|
628 | }
|
---|
629 |
|
---|
630 | ~MergeTask()
|
---|
631 | {
|
---|
632 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
633 | delete mpMediumLockList;
|
---|
634 | if (mpChildrenToReparent)
|
---|
635 | delete mpChildrenToReparent;
|
---|
636 | }
|
---|
637 |
|
---|
638 | const ComObjPtr<Medium> mTarget;
|
---|
639 | bool mfMergeForward;
|
---|
640 | /* When mpChildrenToReparent is null then mParentForTarget is non-null and
|
---|
641 | * vice versa. In other words: they are used in different cases. */
|
---|
642 | const ComObjPtr<Medium> mParentForTarget;
|
---|
643 | MediumLockList *mpChildrenToReparent;
|
---|
644 | MediumLockList *mpMediumLockList;
|
---|
645 |
|
---|
646 | private:
|
---|
647 | HRESULT executeTask();
|
---|
648 | AutoCaller mTargetCaller;
|
---|
649 | AutoCaller mParentForTargetCaller;
|
---|
650 | bool mfKeepMediumLockList;
|
---|
651 | };
|
---|
652 |
|
---|
653 | class Medium::ImportTask : public Medium::Task
|
---|
654 | {
|
---|
655 | public:
|
---|
656 | ImportTask(Medium *aMedium,
|
---|
657 | Progress *aProgress,
|
---|
658 | const char *aFilename,
|
---|
659 | MediumFormat *aFormat,
|
---|
660 | MediumVariant_T aVariant,
|
---|
661 | RTVFSIOSTREAM aVfsIosSrc,
|
---|
662 | Medium *aParent,
|
---|
663 | MediumLockList *aTargetMediumLockList,
|
---|
664 | bool fKeepTargetMediumLockList = false)
|
---|
665 | : Medium::Task(aMedium, aProgress),
|
---|
666 | mFilename(aFilename),
|
---|
667 | mFormat(aFormat),
|
---|
668 | mVariant(aVariant),
|
---|
669 | mParent(aParent),
|
---|
670 | mpTargetMediumLockList(aTargetMediumLockList),
|
---|
671 | mpVfsIoIf(NULL),
|
---|
672 | mParentCaller(aParent),
|
---|
673 | mfKeepTargetMediumLockList(fKeepTargetMediumLockList)
|
---|
674 | {
|
---|
675 | AssertReturnVoidStmt(aTargetMediumLockList != NULL, mRC = E_FAIL);
|
---|
676 | /* aParent may be NULL */
|
---|
677 | mRC = mParentCaller.rc();
|
---|
678 | if (FAILED(mRC))
|
---|
679 | return;
|
---|
680 |
|
---|
681 | mVDImageIfaces = aMedium->m->vdImageIfaces;
|
---|
682 |
|
---|
683 | int vrc = VDIfCreateFromVfsStream(aVfsIosSrc, RTFILE_O_READ, &mpVfsIoIf);
|
---|
684 | AssertRCReturnVoidStmt(vrc, mRC = E_FAIL);
|
---|
685 |
|
---|
686 | vrc = VDInterfaceAdd(&mpVfsIoIf->Core, "Medium::ImportTaskVfsIos",
|
---|
687 | VDINTERFACETYPE_IO, mpVfsIoIf,
|
---|
688 | sizeof(VDINTERFACEIO), &mVDImageIfaces);
|
---|
689 | AssertRCReturnVoidStmt(vrc, mRC = E_FAIL);
|
---|
690 | m_strTaskName = "createImport";
|
---|
691 | }
|
---|
692 |
|
---|
693 | ~ImportTask()
|
---|
694 | {
|
---|
695 | if (!mfKeepTargetMediumLockList && mpTargetMediumLockList)
|
---|
696 | delete mpTargetMediumLockList;
|
---|
697 | if (mpVfsIoIf)
|
---|
698 | {
|
---|
699 | VDIfDestroyFromVfsStream(mpVfsIoIf);
|
---|
700 | mpVfsIoIf = NULL;
|
---|
701 | }
|
---|
702 | }
|
---|
703 |
|
---|
704 | Utf8Str mFilename;
|
---|
705 | ComObjPtr<MediumFormat> mFormat;
|
---|
706 | MediumVariant_T mVariant;
|
---|
707 | const ComObjPtr<Medium> mParent;
|
---|
708 | MediumLockList *mpTargetMediumLockList;
|
---|
709 | PVDINTERFACE mVDImageIfaces;
|
---|
710 | PVDINTERFACEIO mpVfsIoIf; /**< Pointer to the VFS I/O stream to VD I/O interface wrapper. */
|
---|
711 |
|
---|
712 | private:
|
---|
713 | HRESULT executeTask();
|
---|
714 | AutoCaller mParentCaller;
|
---|
715 | bool mfKeepTargetMediumLockList;
|
---|
716 | };
|
---|
717 |
|
---|
718 | class Medium::EncryptTask : public Medium::Task
|
---|
719 | {
|
---|
720 | public:
|
---|
721 | EncryptTask(Medium *aMedium,
|
---|
722 | const com::Utf8Str &strNewPassword,
|
---|
723 | const com::Utf8Str &strCurrentPassword,
|
---|
724 | const com::Utf8Str &strCipher,
|
---|
725 | const com::Utf8Str &strNewPasswordId,
|
---|
726 | Progress *aProgress,
|
---|
727 | MediumLockList *aMediumLockList)
|
---|
728 | : Medium::Task(aMedium, aProgress),
|
---|
729 | mstrNewPassword(strNewPassword),
|
---|
730 | mstrCurrentPassword(strCurrentPassword),
|
---|
731 | mstrCipher(strCipher),
|
---|
732 | mstrNewPasswordId(strNewPasswordId),
|
---|
733 | mpMediumLockList(aMediumLockList)
|
---|
734 | {
|
---|
735 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
736 | /* aParent may be NULL */
|
---|
737 | mRC = mParentCaller.rc();
|
---|
738 | if (FAILED(mRC))
|
---|
739 | return;
|
---|
740 |
|
---|
741 | mVDImageIfaces = aMedium->m->vdImageIfaces;
|
---|
742 | m_strTaskName = "createEncrypt";
|
---|
743 | }
|
---|
744 |
|
---|
745 | ~EncryptTask()
|
---|
746 | {
|
---|
747 | if (mstrNewPassword.length())
|
---|
748 | RTMemWipeThoroughly(mstrNewPassword.mutableRaw(), mstrNewPassword.length(), 10 /* cPasses */);
|
---|
749 | if (mstrCurrentPassword.length())
|
---|
750 | RTMemWipeThoroughly(mstrCurrentPassword.mutableRaw(), mstrCurrentPassword.length(), 10 /* cPasses */);
|
---|
751 |
|
---|
752 | /* Keep any errors which might be set when deleting the lock list. */
|
---|
753 | ErrorInfoKeeper eik;
|
---|
754 | delete mpMediumLockList;
|
---|
755 | }
|
---|
756 |
|
---|
757 | Utf8Str mstrNewPassword;
|
---|
758 | Utf8Str mstrCurrentPassword;
|
---|
759 | Utf8Str mstrCipher;
|
---|
760 | Utf8Str mstrNewPasswordId;
|
---|
761 | MediumLockList *mpMediumLockList;
|
---|
762 | PVDINTERFACE mVDImageIfaces;
|
---|
763 |
|
---|
764 | private:
|
---|
765 | HRESULT executeTask();
|
---|
766 | AutoCaller mParentCaller;
|
---|
767 | };
|
---|
768 |
|
---|
769 | /**
|
---|
770 | * Settings for a crypto filter instance.
|
---|
771 | */
|
---|
772 | struct Medium::CryptoFilterSettings
|
---|
773 | {
|
---|
774 | CryptoFilterSettings()
|
---|
775 | : fCreateKeyStore(false),
|
---|
776 | pszPassword(NULL),
|
---|
777 | pszKeyStore(NULL),
|
---|
778 | pszKeyStoreLoad(NULL),
|
---|
779 | pbDek(NULL),
|
---|
780 | cbDek(0),
|
---|
781 | pszCipher(NULL),
|
---|
782 | pszCipherReturned(NULL)
|
---|
783 | { }
|
---|
784 |
|
---|
785 | bool fCreateKeyStore;
|
---|
786 | const char *pszPassword;
|
---|
787 | char *pszKeyStore;
|
---|
788 | const char *pszKeyStoreLoad;
|
---|
789 |
|
---|
790 | const uint8_t *pbDek;
|
---|
791 | size_t cbDek;
|
---|
792 | const char *pszCipher;
|
---|
793 |
|
---|
794 | /** The cipher returned by the crypto filter. */
|
---|
795 | char *pszCipherReturned;
|
---|
796 |
|
---|
797 | PVDINTERFACE vdFilterIfaces;
|
---|
798 |
|
---|
799 | VDINTERFACECONFIG vdIfCfg;
|
---|
800 | VDINTERFACECRYPTO vdIfCrypto;
|
---|
801 | };
|
---|
802 |
|
---|
803 | /**
|
---|
804 | * PFNVDPROGRESS callback handler for Task operations.
|
---|
805 | *
|
---|
806 | * @param pvUser Pointer to the Progress instance.
|
---|
807 | * @param uPercent Completion percentage (0-100).
|
---|
808 | */
|
---|
809 | /*static*/
|
---|
810 | DECLCALLBACK(int) Medium::Task::vdProgressCall(void *pvUser, unsigned uPercent)
|
---|
811 | {
|
---|
812 | Progress *that = static_cast<Progress *>(pvUser);
|
---|
813 |
|
---|
814 | if (that != NULL)
|
---|
815 | {
|
---|
816 | /* update the progress object, capping it at 99% as the final percent
|
---|
817 | * is used for additional operations like setting the UUIDs and similar. */
|
---|
818 | HRESULT rc = that->SetCurrentOperationProgress(uPercent * 99 / 100);
|
---|
819 | if (FAILED(rc))
|
---|
820 | {
|
---|
821 | if (rc == E_FAIL)
|
---|
822 | return VERR_CANCELLED;
|
---|
823 | else
|
---|
824 | return VERR_INVALID_STATE;
|
---|
825 | }
|
---|
826 | }
|
---|
827 |
|
---|
828 | return VINF_SUCCESS;
|
---|
829 | }
|
---|
830 |
|
---|
831 | /**
|
---|
832 | * Implementation code for the "create base" task.
|
---|
833 | */
|
---|
834 | HRESULT Medium::CreateBaseTask::executeTask()
|
---|
835 | {
|
---|
836 | return mMedium->i_taskCreateBaseHandler(*this);
|
---|
837 | }
|
---|
838 |
|
---|
839 | /**
|
---|
840 | * Implementation code for the "create diff" task.
|
---|
841 | */
|
---|
842 | HRESULT Medium::CreateDiffTask::executeTask()
|
---|
843 | {
|
---|
844 | return mMedium->i_taskCreateDiffHandler(*this);
|
---|
845 | }
|
---|
846 |
|
---|
847 | /**
|
---|
848 | * Implementation code for the "clone" task.
|
---|
849 | */
|
---|
850 | HRESULT Medium::CloneTask::executeTask()
|
---|
851 | {
|
---|
852 | return mMedium->i_taskCloneHandler(*this);
|
---|
853 | }
|
---|
854 |
|
---|
855 | /**
|
---|
856 | * Implementation code for the "move" task.
|
---|
857 | */
|
---|
858 | HRESULT Medium::MoveTask::executeTask()
|
---|
859 | {
|
---|
860 | return mMedium->i_taskMoveHandler(*this);
|
---|
861 | }
|
---|
862 |
|
---|
863 | /**
|
---|
864 | * Implementation code for the "compact" task.
|
---|
865 | */
|
---|
866 | HRESULT Medium::CompactTask::executeTask()
|
---|
867 | {
|
---|
868 | return mMedium->i_taskCompactHandler(*this);
|
---|
869 | }
|
---|
870 |
|
---|
871 | /**
|
---|
872 | * Implementation code for the "resize" task.
|
---|
873 | */
|
---|
874 | HRESULT Medium::ResizeTask::executeTask()
|
---|
875 | {
|
---|
876 | return mMedium->i_taskResizeHandler(*this);
|
---|
877 | }
|
---|
878 |
|
---|
879 |
|
---|
880 | /**
|
---|
881 | * Implementation code for the "reset" task.
|
---|
882 | */
|
---|
883 | HRESULT Medium::ResetTask::executeTask()
|
---|
884 | {
|
---|
885 | return mMedium->i_taskResetHandler(*this);
|
---|
886 | }
|
---|
887 |
|
---|
888 | /**
|
---|
889 | * Implementation code for the "delete" task.
|
---|
890 | */
|
---|
891 | HRESULT Medium::DeleteTask::executeTask()
|
---|
892 | {
|
---|
893 | return mMedium->i_taskDeleteHandler(*this);
|
---|
894 | }
|
---|
895 |
|
---|
896 | /**
|
---|
897 | * Implementation code for the "merge" task.
|
---|
898 | */
|
---|
899 | HRESULT Medium::MergeTask::executeTask()
|
---|
900 | {
|
---|
901 | return mMedium->i_taskMergeHandler(*this);
|
---|
902 | }
|
---|
903 |
|
---|
904 | /**
|
---|
905 | * Implementation code for the "import" task.
|
---|
906 | */
|
---|
907 | HRESULT Medium::ImportTask::executeTask()
|
---|
908 | {
|
---|
909 | return mMedium->i_taskImportHandler(*this);
|
---|
910 | }
|
---|
911 |
|
---|
912 | /**
|
---|
913 | * Implementation code for the "encrypt" task.
|
---|
914 | */
|
---|
915 | HRESULT Medium::EncryptTask::executeTask()
|
---|
916 | {
|
---|
917 | return mMedium->i_taskEncryptHandler(*this);
|
---|
918 | }
|
---|
919 |
|
---|
920 | ////////////////////////////////////////////////////////////////////////////////
|
---|
921 | //
|
---|
922 | // Medium constructor / destructor
|
---|
923 | //
|
---|
924 | ////////////////////////////////////////////////////////////////////////////////
|
---|
925 |
|
---|
926 | DEFINE_EMPTY_CTOR_DTOR(Medium)
|
---|
927 |
|
---|
928 | HRESULT Medium::FinalConstruct()
|
---|
929 | {
|
---|
930 | m = new Data;
|
---|
931 |
|
---|
932 | /* Initialize the callbacks of the VD error interface */
|
---|
933 | m->vdIfError.pfnError = i_vdErrorCall;
|
---|
934 | m->vdIfError.pfnMessage = NULL;
|
---|
935 |
|
---|
936 | /* Initialize the callbacks of the VD config interface */
|
---|
937 | m->vdIfConfig.pfnAreKeysValid = i_vdConfigAreKeysValid;
|
---|
938 | m->vdIfConfig.pfnQuerySize = i_vdConfigQuerySize;
|
---|
939 | m->vdIfConfig.pfnQuery = i_vdConfigQuery;
|
---|
940 | m->vdIfConfig.pfnQueryBytes = NULL;
|
---|
941 |
|
---|
942 | /* Initialize the callbacks of the VD TCP interface (we always use the host
|
---|
943 | * IP stack for now) */
|
---|
944 | m->vdIfTcpNet.pfnSocketCreate = i_vdTcpSocketCreate;
|
---|
945 | m->vdIfTcpNet.pfnSocketDestroy = i_vdTcpSocketDestroy;
|
---|
946 | m->vdIfTcpNet.pfnClientConnect = i_vdTcpClientConnect;
|
---|
947 | m->vdIfTcpNet.pfnClientClose = i_vdTcpClientClose;
|
---|
948 | m->vdIfTcpNet.pfnIsClientConnected = i_vdTcpIsClientConnected;
|
---|
949 | m->vdIfTcpNet.pfnSelectOne = i_vdTcpSelectOne;
|
---|
950 | m->vdIfTcpNet.pfnRead = i_vdTcpRead;
|
---|
951 | m->vdIfTcpNet.pfnWrite = i_vdTcpWrite;
|
---|
952 | m->vdIfTcpNet.pfnSgWrite = i_vdTcpSgWrite;
|
---|
953 | m->vdIfTcpNet.pfnFlush = i_vdTcpFlush;
|
---|
954 | m->vdIfTcpNet.pfnSetSendCoalescing = i_vdTcpSetSendCoalescing;
|
---|
955 | m->vdIfTcpNet.pfnGetLocalAddress = i_vdTcpGetLocalAddress;
|
---|
956 | m->vdIfTcpNet.pfnGetPeerAddress = i_vdTcpGetPeerAddress;
|
---|
957 | m->vdIfTcpNet.pfnSelectOneEx = NULL;
|
---|
958 | m->vdIfTcpNet.pfnPoke = NULL;
|
---|
959 |
|
---|
960 | /* Initialize the per-disk interface chain (could be done more globally,
|
---|
961 | * but it's not wasting much time or space so it's not worth it). */
|
---|
962 | int vrc;
|
---|
963 | vrc = VDInterfaceAdd(&m->vdIfError.Core,
|
---|
964 | "Medium::vdInterfaceError",
|
---|
965 | VDINTERFACETYPE_ERROR, this,
|
---|
966 | sizeof(VDINTERFACEERROR), &m->vdDiskIfaces);
|
---|
967 | AssertRCReturn(vrc, E_FAIL);
|
---|
968 |
|
---|
969 | /* Initialize the per-image interface chain */
|
---|
970 | vrc = VDInterfaceAdd(&m->vdIfConfig.Core,
|
---|
971 | "Medium::vdInterfaceConfig",
|
---|
972 | VDINTERFACETYPE_CONFIG, this,
|
---|
973 | sizeof(VDINTERFACECONFIG), &m->vdImageIfaces);
|
---|
974 | AssertRCReturn(vrc, E_FAIL);
|
---|
975 |
|
---|
976 | vrc = VDInterfaceAdd(&m->vdIfTcpNet.Core,
|
---|
977 | "Medium::vdInterfaceTcpNet",
|
---|
978 | VDINTERFACETYPE_TCPNET, this,
|
---|
979 | sizeof(VDINTERFACETCPNET), &m->vdImageIfaces);
|
---|
980 | AssertRCReturn(vrc, E_FAIL);
|
---|
981 |
|
---|
982 | return BaseFinalConstruct();
|
---|
983 | }
|
---|
984 |
|
---|
985 | void Medium::FinalRelease()
|
---|
986 | {
|
---|
987 | uninit();
|
---|
988 |
|
---|
989 | delete m;
|
---|
990 |
|
---|
991 | BaseFinalRelease();
|
---|
992 | }
|
---|
993 |
|
---|
994 | /**
|
---|
995 | * Initializes an empty hard disk object without creating or opening an associated
|
---|
996 | * storage unit.
|
---|
997 | *
|
---|
998 | * This gets called by VirtualBox::CreateMedium() in which case uuidMachineRegistry
|
---|
999 | * is empty since starting with VirtualBox 4.0, we no longer add opened media to a
|
---|
1000 | * registry automatically (this is deferred until the medium is attached to a machine).
|
---|
1001 | *
|
---|
1002 | * This also gets called when VirtualBox creates diff images; in this case uuidMachineRegistry
|
---|
1003 | * is set to the registry of the parent image to make sure they all end up in the same
|
---|
1004 | * file.
|
---|
1005 | *
|
---|
1006 | * For hard disks that don't have the MediumFormatCapabilities_CreateFixed or
|
---|
1007 | * MediumFormatCapabilities_CreateDynamic capability (and therefore cannot be created or deleted
|
---|
1008 | * with the means of VirtualBox) the associated storage unit is assumed to be
|
---|
1009 | * ready for use so the state of the hard disk object will be set to Created.
|
---|
1010 | *
|
---|
1011 | * @param aVirtualBox VirtualBox object.
|
---|
1012 | * @param aFormat
|
---|
1013 | * @param aLocation Storage unit location.
|
---|
1014 | * @param uuidMachineRegistry The registry to which this medium should be added
|
---|
1015 | * (global registry UUID or machine UUID or empty if none).
|
---|
1016 | * @param aDeviceType Device Type.
|
---|
1017 | */
|
---|
1018 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1019 | const Utf8Str &aFormat,
|
---|
1020 | const Utf8Str &aLocation,
|
---|
1021 | const Guid &uuidMachineRegistry,
|
---|
1022 | const DeviceType_T aDeviceType)
|
---|
1023 | {
|
---|
1024 | AssertReturn(aVirtualBox != NULL, E_FAIL);
|
---|
1025 | AssertReturn(!aFormat.isEmpty(), E_FAIL);
|
---|
1026 |
|
---|
1027 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1028 | AutoInitSpan autoInitSpan(this);
|
---|
1029 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1030 |
|
---|
1031 | HRESULT rc = S_OK;
|
---|
1032 |
|
---|
1033 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1034 |
|
---|
1035 | if (uuidMachineRegistry.isValid() && !uuidMachineRegistry.isZero())
|
---|
1036 | m->llRegistryIDs.push_back(uuidMachineRegistry);
|
---|
1037 |
|
---|
1038 | /* no storage yet */
|
---|
1039 | m->state = MediumState_NotCreated;
|
---|
1040 |
|
---|
1041 | /* cannot be a host drive */
|
---|
1042 | m->hostDrive = false;
|
---|
1043 |
|
---|
1044 | m->devType = aDeviceType;
|
---|
1045 |
|
---|
1046 | /* No storage unit is created yet, no need to call Medium::i_queryInfo */
|
---|
1047 |
|
---|
1048 | rc = i_setFormat(aFormat);
|
---|
1049 | if (FAILED(rc)) return rc;
|
---|
1050 |
|
---|
1051 | rc = i_setLocation(aLocation);
|
---|
1052 | if (FAILED(rc)) return rc;
|
---|
1053 |
|
---|
1054 | if (!(m->formatObj->i_getCapabilities() & ( MediumFormatCapabilities_CreateFixed
|
---|
1055 | | MediumFormatCapabilities_CreateDynamic))
|
---|
1056 | )
|
---|
1057 | {
|
---|
1058 | /* Storage for mediums of this format can neither be explicitly
|
---|
1059 | * created by VirtualBox nor deleted, so we place the medium to
|
---|
1060 | * Inaccessible state here and also add it to the registry. The
|
---|
1061 | * state means that one has to use RefreshState() to update the
|
---|
1062 | * medium format specific fields. */
|
---|
1063 | m->state = MediumState_Inaccessible;
|
---|
1064 | // create new UUID
|
---|
1065 | unconst(m->id).create();
|
---|
1066 |
|
---|
1067 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1068 | ComObjPtr<Medium> pMedium;
|
---|
1069 |
|
---|
1070 | /*
|
---|
1071 | * Check whether the UUID is taken already and create a new one
|
---|
1072 | * if required.
|
---|
1073 | * Try this only a limited amount of times in case the PRNG is broken
|
---|
1074 | * in some way to prevent an endless loop.
|
---|
1075 | */
|
---|
1076 | for (unsigned i = 0; i < 5; i++)
|
---|
1077 | {
|
---|
1078 | bool fInUse;
|
---|
1079 |
|
---|
1080 | fInUse = m->pVirtualBox->i_isMediaUuidInUse(m->id, aDeviceType);
|
---|
1081 | if (fInUse)
|
---|
1082 | {
|
---|
1083 | // create new UUID
|
---|
1084 | unconst(m->id).create();
|
---|
1085 | }
|
---|
1086 | else
|
---|
1087 | break;
|
---|
1088 | }
|
---|
1089 |
|
---|
1090 | rc = m->pVirtualBox->i_registerMedium(this, &pMedium, treeLock);
|
---|
1091 | Assert(this == pMedium || FAILED(rc));
|
---|
1092 | }
|
---|
1093 |
|
---|
1094 | /* Confirm a successful initialization when it's the case */
|
---|
1095 | if (SUCCEEDED(rc))
|
---|
1096 | autoInitSpan.setSucceeded();
|
---|
1097 |
|
---|
1098 | return rc;
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 | /**
|
---|
1102 | * Initializes the medium object by opening the storage unit at the specified
|
---|
1103 | * location. The enOpenMode parameter defines whether the medium will be opened
|
---|
1104 | * read/write or read-only.
|
---|
1105 | *
|
---|
1106 | * This gets called by VirtualBox::OpenMedium() and also by
|
---|
1107 | * Machine::AttachDevice() and createImplicitDiffs() when new diff
|
---|
1108 | * images are created.
|
---|
1109 | *
|
---|
1110 | * There is no registry for this case since starting with VirtualBox 4.0, we
|
---|
1111 | * no longer add opened media to a registry automatically (this is deferred
|
---|
1112 | * until the medium is attached to a machine).
|
---|
1113 | *
|
---|
1114 | * For hard disks, the UUID, format and the parent of this medium will be
|
---|
1115 | * determined when reading the medium storage unit. For DVD and floppy images,
|
---|
1116 | * which have no UUIDs in their storage units, new UUIDs are created.
|
---|
1117 | * If the detected or set parent is not known to VirtualBox, then this method
|
---|
1118 | * will fail.
|
---|
1119 | *
|
---|
1120 | * @param aVirtualBox VirtualBox object.
|
---|
1121 | * @param aLocation Storage unit location.
|
---|
1122 | * @param enOpenMode Whether to open the medium read/write or read-only.
|
---|
1123 | * @param fForceNewUuid Whether a new UUID should be set to avoid duplicates.
|
---|
1124 | * @param aDeviceType Device type of medium.
|
---|
1125 | */
|
---|
1126 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1127 | const Utf8Str &aLocation,
|
---|
1128 | HDDOpenMode enOpenMode,
|
---|
1129 | bool fForceNewUuid,
|
---|
1130 | DeviceType_T aDeviceType)
|
---|
1131 | {
|
---|
1132 | AssertReturn(aVirtualBox, E_INVALIDARG);
|
---|
1133 | AssertReturn(!aLocation.isEmpty(), E_INVALIDARG);
|
---|
1134 |
|
---|
1135 | HRESULT rc = S_OK;
|
---|
1136 |
|
---|
1137 | {
|
---|
1138 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1139 | AutoInitSpan autoInitSpan(this);
|
---|
1140 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1141 |
|
---|
1142 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1143 |
|
---|
1144 | /* there must be a storage unit */
|
---|
1145 | m->state = MediumState_Created;
|
---|
1146 |
|
---|
1147 | /* remember device type for correct unregistering later */
|
---|
1148 | m->devType = aDeviceType;
|
---|
1149 |
|
---|
1150 | /* cannot be a host drive */
|
---|
1151 | m->hostDrive = false;
|
---|
1152 |
|
---|
1153 | /* remember the open mode (defaults to ReadWrite) */
|
---|
1154 | m->hddOpenMode = enOpenMode;
|
---|
1155 |
|
---|
1156 | if (aDeviceType == DeviceType_DVD)
|
---|
1157 | m->type = MediumType_Readonly;
|
---|
1158 | else if (aDeviceType == DeviceType_Floppy)
|
---|
1159 | m->type = MediumType_Writethrough;
|
---|
1160 |
|
---|
1161 | rc = i_setLocation(aLocation);
|
---|
1162 | if (FAILED(rc)) return rc;
|
---|
1163 |
|
---|
1164 | /* get all the information about the medium from the storage unit */
|
---|
1165 | if (fForceNewUuid)
|
---|
1166 | unconst(m->uuidImage).create();
|
---|
1167 |
|
---|
1168 | m->state = MediumState_Inaccessible;
|
---|
1169 | m->strLastAccessError = tr("Accessibility check was not yet performed");
|
---|
1170 |
|
---|
1171 | /* Confirm a successful initialization before the call to i_queryInfo.
|
---|
1172 | * Otherwise we can end up with a AutoCaller deadlock because the
|
---|
1173 | * medium becomes visible but is not marked as initialized. Causes
|
---|
1174 | * locking trouble (e.g. trying to save media registries) which is
|
---|
1175 | * hard to solve. */
|
---|
1176 | autoInitSpan.setSucceeded();
|
---|
1177 | }
|
---|
1178 |
|
---|
1179 | /* we're normal code from now on, no longer init */
|
---|
1180 | AutoCaller autoCaller(this);
|
---|
1181 | if (FAILED(autoCaller.rc()))
|
---|
1182 | return autoCaller.rc();
|
---|
1183 |
|
---|
1184 | /* need to call i_queryInfo immediately to correctly place the medium in
|
---|
1185 | * the respective media tree and update other information such as uuid */
|
---|
1186 | rc = i_queryInfo(fForceNewUuid /* fSetImageId */, false /* fSetParentId */,
|
---|
1187 | autoCaller);
|
---|
1188 | if (SUCCEEDED(rc))
|
---|
1189 | {
|
---|
1190 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1191 |
|
---|
1192 | /* if the storage unit is not accessible, it's not acceptable for the
|
---|
1193 | * newly opened media so convert this into an error */
|
---|
1194 | if (m->state == MediumState_Inaccessible)
|
---|
1195 | {
|
---|
1196 | Assert(!m->strLastAccessError.isEmpty());
|
---|
1197 | rc = setError(E_FAIL, "%s", m->strLastAccessError.c_str());
|
---|
1198 | alock.release();
|
---|
1199 | autoCaller.release();
|
---|
1200 | uninit();
|
---|
1201 | }
|
---|
1202 | else
|
---|
1203 | {
|
---|
1204 | AssertStmt(!m->id.isZero(),
|
---|
1205 | alock.release(); autoCaller.release(); uninit(); return E_FAIL);
|
---|
1206 |
|
---|
1207 | /* storage format must be detected by Medium::i_queryInfo if the
|
---|
1208 | * medium is accessible */
|
---|
1209 | AssertStmt(!m->strFormat.isEmpty(),
|
---|
1210 | alock.release(); autoCaller.release(); uninit(); return E_FAIL);
|
---|
1211 | }
|
---|
1212 | }
|
---|
1213 | else
|
---|
1214 | {
|
---|
1215 | /* opening this image failed, mark the object as dead */
|
---|
1216 | autoCaller.release();
|
---|
1217 | uninit();
|
---|
1218 | }
|
---|
1219 |
|
---|
1220 | return rc;
|
---|
1221 | }
|
---|
1222 |
|
---|
1223 | /**
|
---|
1224 | * Initializes the medium object by loading its data from the given settings
|
---|
1225 | * node. The medium will always be opened read/write.
|
---|
1226 | *
|
---|
1227 | * In this case, since we're loading from a registry, uuidMachineRegistry is
|
---|
1228 | * always set: it's either the global registry UUID or a machine UUID when
|
---|
1229 | * loading from a per-machine registry.
|
---|
1230 | *
|
---|
1231 | * @param aParent Parent medium disk or NULL for a root (base) medium.
|
---|
1232 | * @param aDeviceType Device type of the medium.
|
---|
1233 | * @param uuidMachineRegistry The registry to which this medium should be
|
---|
1234 | * added (global registry UUID or machine UUID).
|
---|
1235 | * @param data Configuration settings.
|
---|
1236 | * @param strMachineFolder The machine folder with which to resolve relative paths;
|
---|
1237 | * if empty, then we use the VirtualBox home directory
|
---|
1238 | *
|
---|
1239 | * @note Locks the medium tree for writing.
|
---|
1240 | */
|
---|
1241 | HRESULT Medium::initOne(Medium *aParent,
|
---|
1242 | DeviceType_T aDeviceType,
|
---|
1243 | const Guid &uuidMachineRegistry,
|
---|
1244 | const settings::Medium &data,
|
---|
1245 | const Utf8Str &strMachineFolder)
|
---|
1246 | {
|
---|
1247 | HRESULT rc;
|
---|
1248 |
|
---|
1249 | if (uuidMachineRegistry.isValid() && !uuidMachineRegistry.isZero())
|
---|
1250 | m->llRegistryIDs.push_back(uuidMachineRegistry);
|
---|
1251 |
|
---|
1252 | /* register with VirtualBox/parent early, since uninit() will
|
---|
1253 | * unconditionally unregister on failure */
|
---|
1254 | if (aParent)
|
---|
1255 | {
|
---|
1256 | // differencing medium: add to parent
|
---|
1257 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1258 | // no need to check maximum depth as settings reading did it
|
---|
1259 | i_setParent(aParent);
|
---|
1260 | }
|
---|
1261 |
|
---|
1262 | /* see below why we don't call Medium::i_queryInfo (and therefore treat
|
---|
1263 | * the medium as inaccessible for now */
|
---|
1264 | m->state = MediumState_Inaccessible;
|
---|
1265 | m->strLastAccessError = tr("Accessibility check was not yet performed");
|
---|
1266 |
|
---|
1267 | /* required */
|
---|
1268 | unconst(m->id) = data.uuid;
|
---|
1269 |
|
---|
1270 | /* assume not a host drive */
|
---|
1271 | m->hostDrive = false;
|
---|
1272 |
|
---|
1273 | /* optional */
|
---|
1274 | m->strDescription = data.strDescription;
|
---|
1275 |
|
---|
1276 | /* required */
|
---|
1277 | if (aDeviceType == DeviceType_HardDisk)
|
---|
1278 | {
|
---|
1279 | AssertReturn(!data.strFormat.isEmpty(), E_FAIL);
|
---|
1280 | rc = i_setFormat(data.strFormat);
|
---|
1281 | if (FAILED(rc)) return rc;
|
---|
1282 | }
|
---|
1283 | else
|
---|
1284 | {
|
---|
1285 | /// @todo handle host drive settings here as well?
|
---|
1286 | if (!data.strFormat.isEmpty())
|
---|
1287 | rc = i_setFormat(data.strFormat);
|
---|
1288 | else
|
---|
1289 | rc = i_setFormat("RAW");
|
---|
1290 | if (FAILED(rc)) return rc;
|
---|
1291 | }
|
---|
1292 |
|
---|
1293 | /* optional, only for diffs, default is false; we can only auto-reset
|
---|
1294 | * diff media so they must have a parent */
|
---|
1295 | if (aParent != NULL)
|
---|
1296 | m->autoReset = data.fAutoReset;
|
---|
1297 | else
|
---|
1298 | m->autoReset = false;
|
---|
1299 |
|
---|
1300 | /* properties (after setting the format as it populates the map). Note that
|
---|
1301 | * if some properties are not supported but present in the settings file,
|
---|
1302 | * they will still be read and accessible (for possible backward
|
---|
1303 | * compatibility; we can also clean them up from the XML upon next
|
---|
1304 | * XML format version change if we wish) */
|
---|
1305 | for (settings::StringsMap::const_iterator it = data.properties.begin();
|
---|
1306 | it != data.properties.end();
|
---|
1307 | ++it)
|
---|
1308 | {
|
---|
1309 | const Utf8Str &name = it->first;
|
---|
1310 | const Utf8Str &value = it->second;
|
---|
1311 | m->mapProperties[name] = value;
|
---|
1312 | }
|
---|
1313 |
|
---|
1314 | /* try to decrypt an optional iSCSI initiator secret */
|
---|
1315 | settings::StringsMap::const_iterator itCph = data.properties.find("InitiatorSecretEncrypted");
|
---|
1316 | if ( itCph != data.properties.end()
|
---|
1317 | && !itCph->second.isEmpty())
|
---|
1318 | {
|
---|
1319 | Utf8Str strPlaintext;
|
---|
1320 | int vrc = m->pVirtualBox->i_decryptSetting(&strPlaintext, itCph->second);
|
---|
1321 | if (RT_SUCCESS(vrc))
|
---|
1322 | m->mapProperties["InitiatorSecret"] = strPlaintext;
|
---|
1323 | }
|
---|
1324 |
|
---|
1325 | Utf8Str strFull;
|
---|
1326 | if (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
1327 | {
|
---|
1328 | // compose full path of the medium, if it's not fully qualified...
|
---|
1329 | // slightly convoluted logic here. If the caller has given us a
|
---|
1330 | // machine folder, then a relative path will be relative to that:
|
---|
1331 | if ( !strMachineFolder.isEmpty()
|
---|
1332 | && !RTPathStartsWithRoot(data.strLocation.c_str())
|
---|
1333 | )
|
---|
1334 | {
|
---|
1335 | strFull = strMachineFolder;
|
---|
1336 | strFull += RTPATH_SLASH;
|
---|
1337 | strFull += data.strLocation;
|
---|
1338 | }
|
---|
1339 | else
|
---|
1340 | {
|
---|
1341 | // Otherwise use the old VirtualBox "make absolute path" logic:
|
---|
1342 | rc = m->pVirtualBox->i_calculateFullPath(data.strLocation, strFull);
|
---|
1343 | if (FAILED(rc)) return rc;
|
---|
1344 | }
|
---|
1345 | }
|
---|
1346 | else
|
---|
1347 | strFull = data.strLocation;
|
---|
1348 |
|
---|
1349 | rc = i_setLocation(strFull);
|
---|
1350 | if (FAILED(rc)) return rc;
|
---|
1351 |
|
---|
1352 | if (aDeviceType == DeviceType_HardDisk)
|
---|
1353 | {
|
---|
1354 | /* type is only for base hard disks */
|
---|
1355 | if (m->pParent.isNull())
|
---|
1356 | m->type = data.hdType;
|
---|
1357 | }
|
---|
1358 | else if (aDeviceType == DeviceType_DVD)
|
---|
1359 | m->type = MediumType_Readonly;
|
---|
1360 | else
|
---|
1361 | m->type = MediumType_Writethrough;
|
---|
1362 |
|
---|
1363 | /* remember device type for correct unregistering later */
|
---|
1364 | m->devType = aDeviceType;
|
---|
1365 |
|
---|
1366 | LogFlowThisFunc(("m->strLocationFull='%s', m->strFormat=%s, m->id={%RTuuid}\n",
|
---|
1367 | m->strLocationFull.c_str(), m->strFormat.c_str(), m->id.raw()));
|
---|
1368 |
|
---|
1369 | return S_OK;
|
---|
1370 | }
|
---|
1371 |
|
---|
1372 | /**
|
---|
1373 | * Initializes the medium object and its children by loading its data from the
|
---|
1374 | * given settings node. The medium will always be opened read/write.
|
---|
1375 | *
|
---|
1376 | * In this case, since we're loading from a registry, uuidMachineRegistry is
|
---|
1377 | * always set: it's either the global registry UUID or a machine UUID when
|
---|
1378 | * loading from a per-machine registry.
|
---|
1379 | *
|
---|
1380 | * @param aVirtualBox VirtualBox object.
|
---|
1381 | * @param aParent Parent medium disk or NULL for a root (base) medium.
|
---|
1382 | * @param aDeviceType Device type of the medium.
|
---|
1383 | * @param uuidMachineRegistry The registry to which this medium should be added
|
---|
1384 | * (global registry UUID or machine UUID).
|
---|
1385 | * @param data Configuration settings.
|
---|
1386 | * @param strMachineFolder The machine folder with which to resolve relative
|
---|
1387 | * paths; if empty, then we use the VirtualBox home directory
|
---|
1388 | * @param mediaTreeLock Autolock.
|
---|
1389 | *
|
---|
1390 | * @note Locks the medium tree for writing.
|
---|
1391 | */
|
---|
1392 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1393 | Medium *aParent,
|
---|
1394 | DeviceType_T aDeviceType,
|
---|
1395 | const Guid &uuidMachineRegistry,
|
---|
1396 | const settings::Medium &data,
|
---|
1397 | const Utf8Str &strMachineFolder,
|
---|
1398 | AutoWriteLock &mediaTreeLock)
|
---|
1399 | {
|
---|
1400 | using namespace settings;
|
---|
1401 |
|
---|
1402 | Assert(aVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
1403 | AssertReturn(aVirtualBox, E_INVALIDARG);
|
---|
1404 |
|
---|
1405 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1406 | AutoInitSpan autoInitSpan(this);
|
---|
1407 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1408 |
|
---|
1409 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1410 |
|
---|
1411 | // Do not inline this method call, as the purpose of having this separate
|
---|
1412 | // is to save on stack size. Less local variables are the key for reaching
|
---|
1413 | // deep recursion levels with small stack (XPCOM/g++ without optimization).
|
---|
1414 | HRESULT rc = initOne(aParent, aDeviceType, uuidMachineRegistry, data, strMachineFolder);
|
---|
1415 |
|
---|
1416 |
|
---|
1417 | /* Don't call Medium::i_queryInfo for registered media to prevent the calling
|
---|
1418 | * thread (i.e. the VirtualBox server startup thread) from an unexpected
|
---|
1419 | * freeze but mark it as initially inaccessible instead. The vital UUID,
|
---|
1420 | * location and format properties are read from the registry file above; to
|
---|
1421 | * get the actual state and the rest of the data, the user will have to call
|
---|
1422 | * COMGETTER(State). */
|
---|
1423 |
|
---|
1424 | /* load all children */
|
---|
1425 | for (settings::MediaList::const_iterator it = data.llChildren.begin();
|
---|
1426 | it != data.llChildren.end();
|
---|
1427 | ++it)
|
---|
1428 | {
|
---|
1429 | const settings::Medium &med = *it;
|
---|
1430 |
|
---|
1431 | ComObjPtr<Medium> pMedium;
|
---|
1432 | pMedium.createObject();
|
---|
1433 | rc = pMedium->init(aVirtualBox,
|
---|
1434 | this, // parent
|
---|
1435 | aDeviceType,
|
---|
1436 | uuidMachineRegistry,
|
---|
1437 | med, // child data
|
---|
1438 | strMachineFolder,
|
---|
1439 | mediaTreeLock);
|
---|
1440 | if (FAILED(rc)) break;
|
---|
1441 |
|
---|
1442 | rc = m->pVirtualBox->i_registerMedium(pMedium, &pMedium, mediaTreeLock);
|
---|
1443 | if (FAILED(rc)) break;
|
---|
1444 | }
|
---|
1445 |
|
---|
1446 | /* Confirm a successful initialization when it's the case */
|
---|
1447 | if (SUCCEEDED(rc))
|
---|
1448 | autoInitSpan.setSucceeded();
|
---|
1449 |
|
---|
1450 | return rc;
|
---|
1451 | }
|
---|
1452 |
|
---|
1453 | /**
|
---|
1454 | * Initializes the medium object by providing the host drive information.
|
---|
1455 | * Not used for anything but the host floppy/host DVD case.
|
---|
1456 | *
|
---|
1457 | * There is no registry for this case.
|
---|
1458 | *
|
---|
1459 | * @param aVirtualBox VirtualBox object.
|
---|
1460 | * @param aDeviceType Device type of the medium.
|
---|
1461 | * @param aLocation Location of the host drive.
|
---|
1462 | * @param aDescription Comment for this host drive.
|
---|
1463 | *
|
---|
1464 | * @note Locks VirtualBox lock for writing.
|
---|
1465 | */
|
---|
1466 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1467 | DeviceType_T aDeviceType,
|
---|
1468 | const Utf8Str &aLocation,
|
---|
1469 | const Utf8Str &aDescription /* = Utf8Str::Empty */)
|
---|
1470 | {
|
---|
1471 | ComAssertRet(aDeviceType == DeviceType_DVD || aDeviceType == DeviceType_Floppy, E_INVALIDARG);
|
---|
1472 | ComAssertRet(!aLocation.isEmpty(), E_INVALIDARG);
|
---|
1473 |
|
---|
1474 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1475 | AutoInitSpan autoInitSpan(this);
|
---|
1476 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1477 |
|
---|
1478 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1479 |
|
---|
1480 | // We do not store host drives in VirtualBox.xml or anywhere else, so if we want
|
---|
1481 | // host drives to be identifiable by UUID and not give the drive a different UUID
|
---|
1482 | // every time VirtualBox starts, we need to fake a reproducible UUID here:
|
---|
1483 | RTUUID uuid;
|
---|
1484 | RTUuidClear(&uuid);
|
---|
1485 | if (aDeviceType == DeviceType_DVD)
|
---|
1486 | memcpy(&uuid.au8[0], "DVD", 3);
|
---|
1487 | else
|
---|
1488 | memcpy(&uuid.au8[0], "FD", 2);
|
---|
1489 | /* use device name, adjusted to the end of uuid, shortened if necessary */
|
---|
1490 | size_t lenLocation = aLocation.length();
|
---|
1491 | if (lenLocation > 12)
|
---|
1492 | memcpy(&uuid.au8[4], aLocation.c_str() + (lenLocation - 12), 12);
|
---|
1493 | else
|
---|
1494 | memcpy(&uuid.au8[4 + 12 - lenLocation], aLocation.c_str(), lenLocation);
|
---|
1495 | unconst(m->id) = uuid;
|
---|
1496 |
|
---|
1497 | if (aDeviceType == DeviceType_DVD)
|
---|
1498 | m->type = MediumType_Readonly;
|
---|
1499 | else
|
---|
1500 | m->type = MediumType_Writethrough;
|
---|
1501 | m->devType = aDeviceType;
|
---|
1502 | m->state = MediumState_Created;
|
---|
1503 | m->hostDrive = true;
|
---|
1504 | HRESULT rc = i_setFormat("RAW");
|
---|
1505 | if (FAILED(rc)) return rc;
|
---|
1506 | rc = i_setLocation(aLocation);
|
---|
1507 | if (FAILED(rc)) return rc;
|
---|
1508 | m->strDescription = aDescription;
|
---|
1509 |
|
---|
1510 | autoInitSpan.setSucceeded();
|
---|
1511 | return S_OK;
|
---|
1512 | }
|
---|
1513 |
|
---|
1514 | /**
|
---|
1515 | * Uninitializes the instance.
|
---|
1516 | *
|
---|
1517 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
1518 | *
|
---|
1519 | * @note All children of this medium get uninitialized by calling their
|
---|
1520 | * uninit() methods.
|
---|
1521 | */
|
---|
1522 | void Medium::uninit()
|
---|
1523 | {
|
---|
1524 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1525 | * the pVirtualBox reference, and in this case we don't need to continue.
|
---|
1526 | * Normally this would be handled through the AutoUninitSpan magic, however
|
---|
1527 | * this cannot be done at this point as the media tree must be locked
|
---|
1528 | * before reaching the AutoUninitSpan, otherwise deadlocks can happen.
|
---|
1529 | *
|
---|
1530 | * NOTE: The tree lock is higher priority than the medium caller and medium
|
---|
1531 | * object locks, i.e. the medium caller may have to be released and be
|
---|
1532 | * re-acquired in the right place later. See Medium::getParent() for sample
|
---|
1533 | * code how to do this safely. */
|
---|
1534 | VirtualBox *pVirtualBox = m->pVirtualBox;
|
---|
1535 | if (!pVirtualBox)
|
---|
1536 | return;
|
---|
1537 |
|
---|
1538 | /* Caller must not hold the object or media tree lock over uninit(). */
|
---|
1539 | Assert(!isWriteLockOnCurrentThread());
|
---|
1540 | Assert(!pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
1541 |
|
---|
1542 | AutoWriteLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1543 |
|
---|
1544 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
1545 | AutoUninitSpan autoUninitSpan(this);
|
---|
1546 | if (autoUninitSpan.uninitDone())
|
---|
1547 | return;
|
---|
1548 |
|
---|
1549 | if (!m->formatObj.isNull())
|
---|
1550 | m->formatObj.setNull();
|
---|
1551 |
|
---|
1552 | if (m->state == MediumState_Deleting)
|
---|
1553 | {
|
---|
1554 | /* This medium has been already deleted (directly or as part of a
|
---|
1555 | * merge). Reparenting has already been done. */
|
---|
1556 | Assert(m->pParent.isNull());
|
---|
1557 | }
|
---|
1558 | else
|
---|
1559 | {
|
---|
1560 | MediaList llChildren(m->llChildren);
|
---|
1561 | m->llChildren.clear();
|
---|
1562 | autoUninitSpan.setSucceeded();
|
---|
1563 |
|
---|
1564 | while (!llChildren.empty())
|
---|
1565 | {
|
---|
1566 | ComObjPtr<Medium> pChild = llChildren.front();
|
---|
1567 | llChildren.pop_front();
|
---|
1568 | pChild->m->pParent.setNull();
|
---|
1569 | treeLock.release();
|
---|
1570 | pChild->uninit();
|
---|
1571 | treeLock.acquire();
|
---|
1572 | }
|
---|
1573 |
|
---|
1574 | if (m->pParent)
|
---|
1575 | {
|
---|
1576 | // this is a differencing disk: then remove it from the parent's children list
|
---|
1577 | i_deparent();
|
---|
1578 | }
|
---|
1579 | }
|
---|
1580 |
|
---|
1581 | unconst(m->pVirtualBox) = NULL;
|
---|
1582 | }
|
---|
1583 |
|
---|
1584 | /**
|
---|
1585 | * Internal helper that removes "this" from the list of children of its
|
---|
1586 | * parent. Used in uninit() and other places when reparenting is necessary.
|
---|
1587 | *
|
---|
1588 | * The caller must hold the medium tree lock!
|
---|
1589 | */
|
---|
1590 | void Medium::i_deparent()
|
---|
1591 | {
|
---|
1592 | MediaList &llParent = m->pParent->m->llChildren;
|
---|
1593 | for (MediaList::iterator it = llParent.begin();
|
---|
1594 | it != llParent.end();
|
---|
1595 | ++it)
|
---|
1596 | {
|
---|
1597 | Medium *pParentsChild = *it;
|
---|
1598 | if (this == pParentsChild)
|
---|
1599 | {
|
---|
1600 | llParent.erase(it);
|
---|
1601 | break;
|
---|
1602 | }
|
---|
1603 | }
|
---|
1604 | m->pParent.setNull();
|
---|
1605 | }
|
---|
1606 |
|
---|
1607 | /**
|
---|
1608 | * Internal helper that removes "this" from the list of children of its
|
---|
1609 | * parent. Used in uninit() and other places when reparenting is necessary.
|
---|
1610 | *
|
---|
1611 | * The caller must hold the medium tree lock!
|
---|
1612 | */
|
---|
1613 | void Medium::i_setParent(const ComObjPtr<Medium> &pParent)
|
---|
1614 | {
|
---|
1615 | m->pParent = pParent;
|
---|
1616 | if (pParent)
|
---|
1617 | pParent->m->llChildren.push_back(this);
|
---|
1618 | }
|
---|
1619 |
|
---|
1620 |
|
---|
1621 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1622 | //
|
---|
1623 | // IMedium public methods
|
---|
1624 | //
|
---|
1625 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1626 |
|
---|
1627 | HRESULT Medium::getId(com::Guid &aId)
|
---|
1628 | {
|
---|
1629 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1630 |
|
---|
1631 | aId = m->id;
|
---|
1632 |
|
---|
1633 | return S_OK;
|
---|
1634 | }
|
---|
1635 |
|
---|
1636 | HRESULT Medium::getDescription(com::Utf8Str &aDescription)
|
---|
1637 | {
|
---|
1638 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1639 |
|
---|
1640 | aDescription = m->strDescription;
|
---|
1641 |
|
---|
1642 | return S_OK;
|
---|
1643 | }
|
---|
1644 |
|
---|
1645 | HRESULT Medium::setDescription(const com::Utf8Str &aDescription)
|
---|
1646 | {
|
---|
1647 | /// @todo update m->strDescription and save the global registry (and local
|
---|
1648 | /// registries of portable VMs referring to this medium), this will also
|
---|
1649 | /// require to add the mRegistered flag to data
|
---|
1650 |
|
---|
1651 | HRESULT rc = S_OK;
|
---|
1652 |
|
---|
1653 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
1654 |
|
---|
1655 | try
|
---|
1656 | {
|
---|
1657 | // locking: we need the tree lock first because we access parent pointers
|
---|
1658 | // and we need to write-lock the media involved
|
---|
1659 | uint32_t cHandles = 2;
|
---|
1660 | LockHandle* pHandles[2] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
1661 | this->lockHandle() };
|
---|
1662 |
|
---|
1663 | AutoWriteLock alock(cHandles,
|
---|
1664 | pHandles
|
---|
1665 | COMMA_LOCKVAL_SRC_POS);
|
---|
1666 |
|
---|
1667 | /* Build the lock list. */
|
---|
1668 | alock.release();
|
---|
1669 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
1670 | this /* pToLockWrite */,
|
---|
1671 | true /* fMediumLockWriteAll */,
|
---|
1672 | NULL,
|
---|
1673 | *pMediumLockList);
|
---|
1674 | alock.acquire();
|
---|
1675 |
|
---|
1676 | if (FAILED(rc))
|
---|
1677 | {
|
---|
1678 | throw setError(rc,
|
---|
1679 | tr("Failed to create medium lock list for '%s'"),
|
---|
1680 | i_getLocationFull().c_str());
|
---|
1681 | }
|
---|
1682 |
|
---|
1683 | alock.release();
|
---|
1684 | rc = pMediumLockList->Lock();
|
---|
1685 | alock.acquire();
|
---|
1686 |
|
---|
1687 | if (FAILED(rc))
|
---|
1688 | {
|
---|
1689 | throw setError(rc,
|
---|
1690 | tr("Failed to lock media '%s'"),
|
---|
1691 | i_getLocationFull().c_str());
|
---|
1692 | }
|
---|
1693 |
|
---|
1694 | /* Set a new description */
|
---|
1695 | if (SUCCEEDED(rc))
|
---|
1696 | {
|
---|
1697 | m->strDescription = aDescription;
|
---|
1698 | }
|
---|
1699 |
|
---|
1700 | // save the settings
|
---|
1701 | i_markRegistriesModified();
|
---|
1702 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
1703 | }
|
---|
1704 | catch (HRESULT aRC) { rc = aRC; }
|
---|
1705 |
|
---|
1706 | delete pMediumLockList;
|
---|
1707 |
|
---|
1708 | return rc;
|
---|
1709 | }
|
---|
1710 |
|
---|
1711 | HRESULT Medium::getState(MediumState_T *aState)
|
---|
1712 | {
|
---|
1713 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1714 | *aState = m->state;
|
---|
1715 |
|
---|
1716 | return S_OK;
|
---|
1717 | }
|
---|
1718 |
|
---|
1719 | HRESULT Medium::getVariant(std::vector<MediumVariant_T> &aVariant)
|
---|
1720 | {
|
---|
1721 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1722 |
|
---|
1723 | const size_t cBits = sizeof(MediumVariant_T) * 8;
|
---|
1724 | aVariant.resize(cBits);
|
---|
1725 | for (size_t i = 0; i < cBits; ++i)
|
---|
1726 | aVariant[i] = (MediumVariant_T)(m->variant & RT_BIT(i));
|
---|
1727 |
|
---|
1728 | return S_OK;
|
---|
1729 | }
|
---|
1730 |
|
---|
1731 | HRESULT Medium::getLocation(com::Utf8Str &aLocation)
|
---|
1732 | {
|
---|
1733 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1734 |
|
---|
1735 | aLocation = m->strLocationFull;
|
---|
1736 |
|
---|
1737 | return S_OK;
|
---|
1738 | }
|
---|
1739 |
|
---|
1740 | HRESULT Medium::getName(com::Utf8Str &aName)
|
---|
1741 | {
|
---|
1742 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1743 |
|
---|
1744 | aName = i_getName();
|
---|
1745 |
|
---|
1746 | return S_OK;
|
---|
1747 | }
|
---|
1748 |
|
---|
1749 | HRESULT Medium::getDeviceType(DeviceType_T *aDeviceType)
|
---|
1750 | {
|
---|
1751 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1752 |
|
---|
1753 | *aDeviceType = m->devType;
|
---|
1754 |
|
---|
1755 | return S_OK;
|
---|
1756 | }
|
---|
1757 |
|
---|
1758 | HRESULT Medium::getHostDrive(BOOL *aHostDrive)
|
---|
1759 | {
|
---|
1760 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1761 |
|
---|
1762 | *aHostDrive = m->hostDrive;
|
---|
1763 |
|
---|
1764 | return S_OK;
|
---|
1765 | }
|
---|
1766 |
|
---|
1767 | HRESULT Medium::getSize(LONG64 *aSize)
|
---|
1768 | {
|
---|
1769 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1770 |
|
---|
1771 | *aSize = m->size;
|
---|
1772 |
|
---|
1773 | return S_OK;
|
---|
1774 | }
|
---|
1775 |
|
---|
1776 | HRESULT Medium::getFormat(com::Utf8Str &aFormat)
|
---|
1777 | {
|
---|
1778 | /* no need to lock, m->strFormat is const */
|
---|
1779 |
|
---|
1780 | aFormat = m->strFormat;
|
---|
1781 | return S_OK;
|
---|
1782 | }
|
---|
1783 |
|
---|
1784 | HRESULT Medium::getMediumFormat(ComPtr<IMediumFormat> &aMediumFormat)
|
---|
1785 | {
|
---|
1786 | /* no need to lock, m->formatObj is const */
|
---|
1787 | m->formatObj.queryInterfaceTo(aMediumFormat.asOutParam());
|
---|
1788 |
|
---|
1789 | return S_OK;
|
---|
1790 | }
|
---|
1791 |
|
---|
1792 | HRESULT Medium::getType(AutoCaller &autoCaller, MediumType_T *aType)
|
---|
1793 | {
|
---|
1794 | NOREF(autoCaller);
|
---|
1795 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1796 |
|
---|
1797 | *aType = m->type;
|
---|
1798 |
|
---|
1799 | return S_OK;
|
---|
1800 | }
|
---|
1801 |
|
---|
1802 | HRESULT Medium::setType(AutoCaller &autoCaller, MediumType_T aType)
|
---|
1803 | {
|
---|
1804 | autoCaller.release();
|
---|
1805 |
|
---|
1806 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1807 | * the pVirtualBox reference, see #uninit(). */
|
---|
1808 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
1809 |
|
---|
1810 | // we access m->pParent
|
---|
1811 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
1812 |
|
---|
1813 | autoCaller.add();
|
---|
1814 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1815 |
|
---|
1816 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1817 |
|
---|
1818 | switch (m->state)
|
---|
1819 | {
|
---|
1820 | case MediumState_Created:
|
---|
1821 | case MediumState_Inaccessible:
|
---|
1822 | break;
|
---|
1823 | default:
|
---|
1824 | return i_setStateError();
|
---|
1825 | }
|
---|
1826 |
|
---|
1827 | if (m->type == aType)
|
---|
1828 | {
|
---|
1829 | /* Nothing to do */
|
---|
1830 | return S_OK;
|
---|
1831 | }
|
---|
1832 |
|
---|
1833 | DeviceType_T devType = i_getDeviceType();
|
---|
1834 | // DVD media can only be readonly.
|
---|
1835 | if (devType == DeviceType_DVD && aType != MediumType_Readonly)
|
---|
1836 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1837 | tr("Cannot change the type of DVD medium '%s'"),
|
---|
1838 | m->strLocationFull.c_str());
|
---|
1839 | // Floppy media can only be writethrough or readonly.
|
---|
1840 | if ( devType == DeviceType_Floppy
|
---|
1841 | && aType != MediumType_Writethrough
|
---|
1842 | && aType != MediumType_Readonly)
|
---|
1843 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1844 | tr("Cannot change the type of floppy medium '%s'"),
|
---|
1845 | m->strLocationFull.c_str());
|
---|
1846 |
|
---|
1847 | /* cannot change the type of a differencing medium */
|
---|
1848 | if (m->pParent)
|
---|
1849 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1850 | tr("Cannot change the type of medium '%s' because it is a differencing medium"),
|
---|
1851 | m->strLocationFull.c_str());
|
---|
1852 |
|
---|
1853 | /* Cannot change the type of a medium being in use by more than one VM.
|
---|
1854 | * If the change is to Immutable or MultiAttach then it must not be
|
---|
1855 | * directly attached to any VM, otherwise the assumptions about indirect
|
---|
1856 | * attachment elsewhere are violated and the VM becomes inaccessible.
|
---|
1857 | * Attaching an immutable medium triggers the diff creation, and this is
|
---|
1858 | * vital for the correct operation. */
|
---|
1859 | if ( m->backRefs.size() > 1
|
---|
1860 | || ( ( aType == MediumType_Immutable
|
---|
1861 | || aType == MediumType_MultiAttach)
|
---|
1862 | && m->backRefs.size() > 0))
|
---|
1863 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1864 | tr("Cannot change the type of medium '%s' because it is attached to %d virtual machines"),
|
---|
1865 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
1866 |
|
---|
1867 | switch (aType)
|
---|
1868 | {
|
---|
1869 | case MediumType_Normal:
|
---|
1870 | case MediumType_Immutable:
|
---|
1871 | case MediumType_MultiAttach:
|
---|
1872 | {
|
---|
1873 | /* normal can be easily converted to immutable and vice versa even
|
---|
1874 | * if they have children as long as they are not attached to any
|
---|
1875 | * machine themselves */
|
---|
1876 | break;
|
---|
1877 | }
|
---|
1878 | case MediumType_Writethrough:
|
---|
1879 | case MediumType_Shareable:
|
---|
1880 | case MediumType_Readonly:
|
---|
1881 | {
|
---|
1882 | /* cannot change to writethrough, shareable or readonly
|
---|
1883 | * if there are children */
|
---|
1884 | if (i_getChildren().size() != 0)
|
---|
1885 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
1886 | tr("Cannot change type for medium '%s' since it has %d child media"),
|
---|
1887 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
1888 | if (aType == MediumType_Shareable)
|
---|
1889 | {
|
---|
1890 | MediumVariant_T variant = i_getVariant();
|
---|
1891 | if (!(variant & MediumVariant_Fixed))
|
---|
1892 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1893 | tr("Cannot change type for medium '%s' to 'Shareable' since it is a dynamic medium storage unit"),
|
---|
1894 | m->strLocationFull.c_str());
|
---|
1895 | }
|
---|
1896 | else if (aType == MediumType_Readonly && devType == DeviceType_HardDisk)
|
---|
1897 | {
|
---|
1898 | // Readonly hard disks are not allowed, this medium type is reserved for
|
---|
1899 | // DVDs and floppy images at the moment. Later we might allow readonly hard
|
---|
1900 | // disks, but that's extremely unusual and many guest OSes will have trouble.
|
---|
1901 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1902 | tr("Cannot change type for medium '%s' to 'Readonly' since it is a hard disk"),
|
---|
1903 | m->strLocationFull.c_str());
|
---|
1904 | }
|
---|
1905 | break;
|
---|
1906 | }
|
---|
1907 | default:
|
---|
1908 | AssertFailedReturn(E_FAIL);
|
---|
1909 | }
|
---|
1910 |
|
---|
1911 | if (aType == MediumType_MultiAttach)
|
---|
1912 | {
|
---|
1913 | // This type is new with VirtualBox 4.0 and therefore requires settings
|
---|
1914 | // version 1.11 in the settings backend. Unfortunately it is not enough to do
|
---|
1915 | // the usual routine in MachineConfigFile::bumpSettingsVersionIfNeeded() for
|
---|
1916 | // two reasons: The medium type is a property of the media registry tree, which
|
---|
1917 | // can reside in the global config file (for pre-4.0 media); we would therefore
|
---|
1918 | // possibly need to bump the global config version. We don't want to do that though
|
---|
1919 | // because that might make downgrading to pre-4.0 impossible.
|
---|
1920 | // As a result, we can only use these two new types if the medium is NOT in the
|
---|
1921 | // global registry:
|
---|
1922 | const Guid &uuidGlobalRegistry = m->pVirtualBox->i_getGlobalRegistryId();
|
---|
1923 | if (i_isInRegistry(uuidGlobalRegistry))
|
---|
1924 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1925 | tr("Cannot change type for medium '%s': the media type 'MultiAttach' can only be used "
|
---|
1926 | "on media registered with a machine that was created with VirtualBox 4.0 or later"),
|
---|
1927 | m->strLocationFull.c_str());
|
---|
1928 | }
|
---|
1929 |
|
---|
1930 | m->type = aType;
|
---|
1931 |
|
---|
1932 | // save the settings
|
---|
1933 | mlock.release();
|
---|
1934 | treeLock.release();
|
---|
1935 | i_markRegistriesModified();
|
---|
1936 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
1937 |
|
---|
1938 | return S_OK;
|
---|
1939 | }
|
---|
1940 |
|
---|
1941 | HRESULT Medium::getAllowedTypes(std::vector<MediumType_T> &aAllowedTypes)
|
---|
1942 | {
|
---|
1943 | NOREF(aAllowedTypes);
|
---|
1944 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1945 |
|
---|
1946 | ReturnComNotImplemented();
|
---|
1947 | }
|
---|
1948 |
|
---|
1949 | HRESULT Medium::getParent(AutoCaller &autoCaller, ComPtr<IMedium> &aParent)
|
---|
1950 | {
|
---|
1951 | autoCaller.release();
|
---|
1952 |
|
---|
1953 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1954 | * the pVirtualBox reference, see #uninit(). */
|
---|
1955 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
1956 |
|
---|
1957 | /* we access m->pParent */
|
---|
1958 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
1959 |
|
---|
1960 | autoCaller.add();
|
---|
1961 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1962 |
|
---|
1963 | m->pParent.queryInterfaceTo(aParent.asOutParam());
|
---|
1964 |
|
---|
1965 | return S_OK;
|
---|
1966 | }
|
---|
1967 |
|
---|
1968 | HRESULT Medium::getChildren(AutoCaller &autoCaller, std::vector<ComPtr<IMedium> > &aChildren)
|
---|
1969 | {
|
---|
1970 | autoCaller.release();
|
---|
1971 |
|
---|
1972 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1973 | * the pVirtualBox reference, see #uninit(). */
|
---|
1974 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
1975 |
|
---|
1976 | /* we access children */
|
---|
1977 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
1978 |
|
---|
1979 | autoCaller.add();
|
---|
1980 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1981 |
|
---|
1982 | MediaList children(this->i_getChildren());
|
---|
1983 | aChildren.resize(children.size());
|
---|
1984 | size_t i = 0;
|
---|
1985 | for (MediaList::const_iterator it = children.begin(); it != children.end(); ++it, ++i)
|
---|
1986 | (*it).queryInterfaceTo(aChildren[i].asOutParam());
|
---|
1987 | return S_OK;
|
---|
1988 | }
|
---|
1989 |
|
---|
1990 | HRESULT Medium::getBase(AutoCaller &autoCaller, ComPtr<IMedium> &aBase)
|
---|
1991 | {
|
---|
1992 | autoCaller.release();
|
---|
1993 |
|
---|
1994 | /* i_getBase() will do callers/locking */
|
---|
1995 | i_getBase().queryInterfaceTo(aBase.asOutParam());
|
---|
1996 |
|
---|
1997 | return S_OK;
|
---|
1998 | }
|
---|
1999 |
|
---|
2000 | HRESULT Medium::getReadOnly(AutoCaller &autoCaller, BOOL *aReadOnly)
|
---|
2001 | {
|
---|
2002 | autoCaller.release();
|
---|
2003 |
|
---|
2004 | /* isReadOnly() will do locking */
|
---|
2005 | *aReadOnly = i_isReadOnly();
|
---|
2006 |
|
---|
2007 | return S_OK;
|
---|
2008 | }
|
---|
2009 |
|
---|
2010 | HRESULT Medium::getLogicalSize(LONG64 *aLogicalSize)
|
---|
2011 | {
|
---|
2012 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2013 |
|
---|
2014 | *aLogicalSize = m->logicalSize;
|
---|
2015 |
|
---|
2016 | return S_OK;
|
---|
2017 | }
|
---|
2018 |
|
---|
2019 | HRESULT Medium::getAutoReset(BOOL *aAutoReset)
|
---|
2020 | {
|
---|
2021 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2022 |
|
---|
2023 | if (m->pParent.isNull())
|
---|
2024 | *aAutoReset = FALSE;
|
---|
2025 | else
|
---|
2026 | *aAutoReset = m->autoReset;
|
---|
2027 |
|
---|
2028 | return S_OK;
|
---|
2029 | }
|
---|
2030 |
|
---|
2031 | HRESULT Medium::setAutoReset(BOOL aAutoReset)
|
---|
2032 | {
|
---|
2033 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2034 |
|
---|
2035 | if (m->pParent.isNull())
|
---|
2036 | return setError(VBOX_E_NOT_SUPPORTED,
|
---|
2037 | tr("Medium '%s' is not differencing"),
|
---|
2038 | m->strLocationFull.c_str());
|
---|
2039 |
|
---|
2040 | if (m->autoReset != !!aAutoReset)
|
---|
2041 | {
|
---|
2042 | m->autoReset = !!aAutoReset;
|
---|
2043 |
|
---|
2044 | // save the settings
|
---|
2045 | mlock.release();
|
---|
2046 | i_markRegistriesModified();
|
---|
2047 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2048 | }
|
---|
2049 |
|
---|
2050 | return S_OK;
|
---|
2051 | }
|
---|
2052 |
|
---|
2053 | HRESULT Medium::getLastAccessError(com::Utf8Str &aLastAccessError)
|
---|
2054 | {
|
---|
2055 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2056 |
|
---|
2057 | aLastAccessError = m->strLastAccessError;
|
---|
2058 |
|
---|
2059 | return S_OK;
|
---|
2060 | }
|
---|
2061 |
|
---|
2062 | HRESULT Medium::getMachineIds(std::vector<com::Guid> &aMachineIds)
|
---|
2063 | {
|
---|
2064 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2065 |
|
---|
2066 | if (m->backRefs.size() != 0)
|
---|
2067 | {
|
---|
2068 | BackRefList brlist(m->backRefs);
|
---|
2069 | aMachineIds.resize(brlist.size());
|
---|
2070 | size_t i = 0;
|
---|
2071 | for (BackRefList::const_iterator it = brlist.begin(); it != brlist.end(); ++it, ++i)
|
---|
2072 | aMachineIds[i] = it->machineId;
|
---|
2073 | }
|
---|
2074 |
|
---|
2075 | return S_OK;
|
---|
2076 | }
|
---|
2077 |
|
---|
2078 | HRESULT Medium::setIds(AutoCaller &autoCaller,
|
---|
2079 | BOOL aSetImageId,
|
---|
2080 | const com::Guid &aImageId,
|
---|
2081 | BOOL aSetParentId,
|
---|
2082 | const com::Guid &aParentId)
|
---|
2083 | {
|
---|
2084 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2085 |
|
---|
2086 | switch (m->state)
|
---|
2087 | {
|
---|
2088 | case MediumState_Created:
|
---|
2089 | break;
|
---|
2090 | default:
|
---|
2091 | return i_setStateError();
|
---|
2092 | }
|
---|
2093 |
|
---|
2094 | Guid imageId, parentId;
|
---|
2095 | if (aSetImageId)
|
---|
2096 | {
|
---|
2097 | if (aImageId.isZero())
|
---|
2098 | imageId.create();
|
---|
2099 | else
|
---|
2100 | {
|
---|
2101 | imageId = aImageId;
|
---|
2102 | if (!imageId.isValid())
|
---|
2103 | return setError(E_INVALIDARG, tr("Argument %s is invalid"), "aImageId");
|
---|
2104 | }
|
---|
2105 | }
|
---|
2106 | if (aSetParentId)
|
---|
2107 | {
|
---|
2108 | if (aParentId.isZero())
|
---|
2109 | parentId.create();
|
---|
2110 | else
|
---|
2111 | parentId = aParentId;
|
---|
2112 | }
|
---|
2113 |
|
---|
2114 | unconst(m->uuidImage) = imageId;
|
---|
2115 | unconst(m->uuidParentImage) = parentId;
|
---|
2116 |
|
---|
2117 | // must not hold any locks before calling Medium::i_queryInfo
|
---|
2118 | alock.release();
|
---|
2119 |
|
---|
2120 | HRESULT rc = i_queryInfo(!!aSetImageId /* fSetImageId */,
|
---|
2121 | !!aSetParentId /* fSetParentId */,
|
---|
2122 | autoCaller);
|
---|
2123 |
|
---|
2124 | return rc;
|
---|
2125 | }
|
---|
2126 |
|
---|
2127 | HRESULT Medium::refreshState(AutoCaller &autoCaller, MediumState_T *aState)
|
---|
2128 | {
|
---|
2129 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2130 |
|
---|
2131 | HRESULT rc = S_OK;
|
---|
2132 |
|
---|
2133 | switch (m->state)
|
---|
2134 | {
|
---|
2135 | case MediumState_Created:
|
---|
2136 | case MediumState_Inaccessible:
|
---|
2137 | case MediumState_LockedRead:
|
---|
2138 | {
|
---|
2139 | // must not hold any locks before calling Medium::i_queryInfo
|
---|
2140 | alock.release();
|
---|
2141 |
|
---|
2142 | rc = i_queryInfo(false /* fSetImageId */, false /* fSetParentId */,
|
---|
2143 | autoCaller);
|
---|
2144 |
|
---|
2145 | alock.acquire();
|
---|
2146 | break;
|
---|
2147 | }
|
---|
2148 | default:
|
---|
2149 | break;
|
---|
2150 | }
|
---|
2151 |
|
---|
2152 | *aState = m->state;
|
---|
2153 |
|
---|
2154 | return rc;
|
---|
2155 | }
|
---|
2156 |
|
---|
2157 | HRESULT Medium::getSnapshotIds(const com::Guid &aMachineId,
|
---|
2158 | std::vector<com::Guid> &aSnapshotIds)
|
---|
2159 | {
|
---|
2160 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2161 |
|
---|
2162 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
2163 | it != m->backRefs.end(); ++it)
|
---|
2164 | {
|
---|
2165 | if (it->machineId == aMachineId)
|
---|
2166 | {
|
---|
2167 | size_t size = it->llSnapshotIds.size();
|
---|
2168 |
|
---|
2169 | /* if the medium is attached to the machine in the current state, we
|
---|
2170 | * return its ID as the first element of the array */
|
---|
2171 | if (it->fInCurState)
|
---|
2172 | ++size;
|
---|
2173 |
|
---|
2174 | if (size > 0)
|
---|
2175 | {
|
---|
2176 | aSnapshotIds.resize(size);
|
---|
2177 |
|
---|
2178 | size_t j = 0;
|
---|
2179 | if (it->fInCurState)
|
---|
2180 | aSnapshotIds[j++] = it->machineId.toUtf16();
|
---|
2181 |
|
---|
2182 | for(GuidList::const_iterator jt = it->llSnapshotIds.begin(); jt != it->llSnapshotIds.end(); ++jt, ++j)
|
---|
2183 | aSnapshotIds[j] = (*jt);
|
---|
2184 | }
|
---|
2185 |
|
---|
2186 | break;
|
---|
2187 | }
|
---|
2188 | }
|
---|
2189 |
|
---|
2190 | return S_OK;
|
---|
2191 | }
|
---|
2192 |
|
---|
2193 | HRESULT Medium::lockRead(ComPtr<IToken> &aToken)
|
---|
2194 | {
|
---|
2195 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2196 |
|
---|
2197 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
2198 | if (m->queryInfoRunning)
|
---|
2199 | {
|
---|
2200 | /* Must not hold the media tree lock, as Medium::i_queryInfo needs this
|
---|
2201 | * lock and thus we would run into a deadlock here. */
|
---|
2202 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
2203 | while (m->queryInfoRunning)
|
---|
2204 | {
|
---|
2205 | alock.release();
|
---|
2206 | /* must not hold the object lock now */
|
---|
2207 | Assert(!isWriteLockOnCurrentThread());
|
---|
2208 | {
|
---|
2209 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
2210 | }
|
---|
2211 | alock.acquire();
|
---|
2212 | }
|
---|
2213 | }
|
---|
2214 |
|
---|
2215 | HRESULT rc = S_OK;
|
---|
2216 |
|
---|
2217 | switch (m->state)
|
---|
2218 | {
|
---|
2219 | case MediumState_Created:
|
---|
2220 | case MediumState_Inaccessible:
|
---|
2221 | case MediumState_LockedRead:
|
---|
2222 | {
|
---|
2223 | ++m->readers;
|
---|
2224 |
|
---|
2225 | ComAssertMsgBreak(m->readers != 0, ("Counter overflow"), rc = E_FAIL);
|
---|
2226 |
|
---|
2227 | /* Remember pre-lock state */
|
---|
2228 | if (m->state != MediumState_LockedRead)
|
---|
2229 | m->preLockState = m->state;
|
---|
2230 |
|
---|
2231 | LogFlowThisFunc(("Okay - prev state=%d readers=%d\n", m->state, m->readers));
|
---|
2232 | m->state = MediumState_LockedRead;
|
---|
2233 |
|
---|
2234 | ComObjPtr<MediumLockToken> pToken;
|
---|
2235 | rc = pToken.createObject();
|
---|
2236 | if (SUCCEEDED(rc))
|
---|
2237 | rc = pToken->init(this, false /* fWrite */);
|
---|
2238 | if (FAILED(rc))
|
---|
2239 | {
|
---|
2240 | --m->readers;
|
---|
2241 | if (m->readers == 0)
|
---|
2242 | m->state = m->preLockState;
|
---|
2243 | return rc;
|
---|
2244 | }
|
---|
2245 |
|
---|
2246 | pToken.queryInterfaceTo(aToken.asOutParam());
|
---|
2247 | break;
|
---|
2248 | }
|
---|
2249 | default:
|
---|
2250 | {
|
---|
2251 | LogFlowThisFunc(("Failing - state=%d\n", m->state));
|
---|
2252 | rc = i_setStateError();
|
---|
2253 | break;
|
---|
2254 | }
|
---|
2255 | }
|
---|
2256 |
|
---|
2257 | return rc;
|
---|
2258 | }
|
---|
2259 |
|
---|
2260 | /**
|
---|
2261 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
2262 | * in-process calls).
|
---|
2263 | */
|
---|
2264 | HRESULT Medium::i_unlockRead(MediumState_T *aState)
|
---|
2265 | {
|
---|
2266 | AutoCaller autoCaller(this);
|
---|
2267 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2268 |
|
---|
2269 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2270 |
|
---|
2271 | HRESULT rc = S_OK;
|
---|
2272 |
|
---|
2273 | switch (m->state)
|
---|
2274 | {
|
---|
2275 | case MediumState_LockedRead:
|
---|
2276 | {
|
---|
2277 | ComAssertMsgBreak(m->readers != 0, ("Counter underflow"), rc = E_FAIL);
|
---|
2278 | --m->readers;
|
---|
2279 |
|
---|
2280 | /* Reset the state after the last reader */
|
---|
2281 | if (m->readers == 0)
|
---|
2282 | {
|
---|
2283 | m->state = m->preLockState;
|
---|
2284 | /* There are cases where we inject the deleting state into
|
---|
2285 | * a medium locked for reading. Make sure #unmarkForDeletion()
|
---|
2286 | * gets the right state afterwards. */
|
---|
2287 | if (m->preLockState == MediumState_Deleting)
|
---|
2288 | m->preLockState = MediumState_Created;
|
---|
2289 | }
|
---|
2290 |
|
---|
2291 | LogFlowThisFunc(("new state=%d\n", m->state));
|
---|
2292 | break;
|
---|
2293 | }
|
---|
2294 | default:
|
---|
2295 | {
|
---|
2296 | LogFlowThisFunc(("Failing - state=%d\n", m->state));
|
---|
2297 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2298 | tr("Medium '%s' is not locked for reading"),
|
---|
2299 | m->strLocationFull.c_str());
|
---|
2300 | break;
|
---|
2301 | }
|
---|
2302 | }
|
---|
2303 |
|
---|
2304 | /* return the current state after */
|
---|
2305 | if (aState)
|
---|
2306 | *aState = m->state;
|
---|
2307 |
|
---|
2308 | return rc;
|
---|
2309 | }
|
---|
2310 | HRESULT Medium::lockWrite(ComPtr<IToken> &aToken)
|
---|
2311 | {
|
---|
2312 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2313 |
|
---|
2314 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
2315 | if (m->queryInfoRunning)
|
---|
2316 | {
|
---|
2317 | /* Must not hold the media tree lock, as Medium::i_queryInfo needs this
|
---|
2318 | * lock and thus we would run into a deadlock here. */
|
---|
2319 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
2320 | while (m->queryInfoRunning)
|
---|
2321 | {
|
---|
2322 | alock.release();
|
---|
2323 | /* must not hold the object lock now */
|
---|
2324 | Assert(!isWriteLockOnCurrentThread());
|
---|
2325 | {
|
---|
2326 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
2327 | }
|
---|
2328 | alock.acquire();
|
---|
2329 | }
|
---|
2330 | }
|
---|
2331 |
|
---|
2332 | HRESULT rc = S_OK;
|
---|
2333 |
|
---|
2334 | switch (m->state)
|
---|
2335 | {
|
---|
2336 | case MediumState_Created:
|
---|
2337 | case MediumState_Inaccessible:
|
---|
2338 | {
|
---|
2339 | m->preLockState = m->state;
|
---|
2340 |
|
---|
2341 | LogFlowThisFunc(("Okay - prev state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2342 | m->state = MediumState_LockedWrite;
|
---|
2343 |
|
---|
2344 | ComObjPtr<MediumLockToken> pToken;
|
---|
2345 | rc = pToken.createObject();
|
---|
2346 | if (SUCCEEDED(rc))
|
---|
2347 | rc = pToken->init(this, true /* fWrite */);
|
---|
2348 | if (FAILED(rc))
|
---|
2349 | {
|
---|
2350 | m->state = m->preLockState;
|
---|
2351 | return rc;
|
---|
2352 | }
|
---|
2353 |
|
---|
2354 | pToken.queryInterfaceTo(aToken.asOutParam());
|
---|
2355 | break;
|
---|
2356 | }
|
---|
2357 | default:
|
---|
2358 | {
|
---|
2359 | LogFlowThisFunc(("Failing - state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2360 | rc = i_setStateError();
|
---|
2361 | break;
|
---|
2362 | }
|
---|
2363 | }
|
---|
2364 |
|
---|
2365 | return rc;
|
---|
2366 | }
|
---|
2367 |
|
---|
2368 | /**
|
---|
2369 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
2370 | * in-process calls).
|
---|
2371 | */
|
---|
2372 | HRESULT Medium::i_unlockWrite(MediumState_T *aState)
|
---|
2373 | {
|
---|
2374 | AutoCaller autoCaller(this);
|
---|
2375 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2376 |
|
---|
2377 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2378 |
|
---|
2379 | HRESULT rc = S_OK;
|
---|
2380 |
|
---|
2381 | switch (m->state)
|
---|
2382 | {
|
---|
2383 | case MediumState_LockedWrite:
|
---|
2384 | {
|
---|
2385 | m->state = m->preLockState;
|
---|
2386 | /* There are cases where we inject the deleting state into
|
---|
2387 | * a medium locked for writing. Make sure #unmarkForDeletion()
|
---|
2388 | * gets the right state afterwards. */
|
---|
2389 | if (m->preLockState == MediumState_Deleting)
|
---|
2390 | m->preLockState = MediumState_Created;
|
---|
2391 | LogFlowThisFunc(("new state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2392 | break;
|
---|
2393 | }
|
---|
2394 | default:
|
---|
2395 | {
|
---|
2396 | LogFlowThisFunc(("Failing - state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2397 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2398 | tr("Medium '%s' is not locked for writing"),
|
---|
2399 | m->strLocationFull.c_str());
|
---|
2400 | break;
|
---|
2401 | }
|
---|
2402 | }
|
---|
2403 |
|
---|
2404 | /* return the current state after */
|
---|
2405 | if (aState)
|
---|
2406 | *aState = m->state;
|
---|
2407 |
|
---|
2408 | return rc;
|
---|
2409 | }
|
---|
2410 |
|
---|
2411 | HRESULT Medium::close(AutoCaller &aAutoCaller)
|
---|
2412 | {
|
---|
2413 | // make a copy of VirtualBox pointer which gets nulled by uninit()
|
---|
2414 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
2415 |
|
---|
2416 | MultiResult mrc = i_close(aAutoCaller);
|
---|
2417 |
|
---|
2418 | pVirtualBox->i_saveModifiedRegistries();
|
---|
2419 |
|
---|
2420 | return mrc;
|
---|
2421 | }
|
---|
2422 |
|
---|
2423 | HRESULT Medium::getProperty(const com::Utf8Str &aName,
|
---|
2424 | com::Utf8Str &aValue)
|
---|
2425 | {
|
---|
2426 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2427 |
|
---|
2428 | settings::StringsMap::const_iterator it = m->mapProperties.find(aName);
|
---|
2429 | if (it == m->mapProperties.end())
|
---|
2430 | {
|
---|
2431 | if (!aName.startsWith("Special/"))
|
---|
2432 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2433 | tr("Property '%s' does not exist"), aName.c_str());
|
---|
2434 | else
|
---|
2435 | /* be more silent here */
|
---|
2436 | return VBOX_E_OBJECT_NOT_FOUND;
|
---|
2437 | }
|
---|
2438 |
|
---|
2439 | aValue = it->second;
|
---|
2440 |
|
---|
2441 | return S_OK;
|
---|
2442 | }
|
---|
2443 |
|
---|
2444 | HRESULT Medium::setProperty(const com::Utf8Str &aName,
|
---|
2445 | const com::Utf8Str &aValue)
|
---|
2446 | {
|
---|
2447 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2448 |
|
---|
2449 | switch (m->state)
|
---|
2450 | {
|
---|
2451 | case MediumState_Created:
|
---|
2452 | case MediumState_Inaccessible:
|
---|
2453 | break;
|
---|
2454 | default:
|
---|
2455 | return i_setStateError();
|
---|
2456 | }
|
---|
2457 |
|
---|
2458 | settings::StringsMap::iterator it = m->mapProperties.find(aName);
|
---|
2459 | if ( !aName.startsWith("Special/")
|
---|
2460 | && !i_isPropertyForFilter(aName))
|
---|
2461 | {
|
---|
2462 | if (it == m->mapProperties.end())
|
---|
2463 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2464 | tr("Property '%s' does not exist"),
|
---|
2465 | aName.c_str());
|
---|
2466 | it->second = aValue;
|
---|
2467 | }
|
---|
2468 | else
|
---|
2469 | {
|
---|
2470 | if (it == m->mapProperties.end())
|
---|
2471 | {
|
---|
2472 | if (!aValue.isEmpty())
|
---|
2473 | m->mapProperties[aName] = aValue;
|
---|
2474 | }
|
---|
2475 | else
|
---|
2476 | {
|
---|
2477 | if (!aValue.isEmpty())
|
---|
2478 | it->second = aValue;
|
---|
2479 | else
|
---|
2480 | m->mapProperties.erase(it);
|
---|
2481 | }
|
---|
2482 | }
|
---|
2483 |
|
---|
2484 | // save the settings
|
---|
2485 | mlock.release();
|
---|
2486 | i_markRegistriesModified();
|
---|
2487 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2488 |
|
---|
2489 | return S_OK;
|
---|
2490 | }
|
---|
2491 |
|
---|
2492 | HRESULT Medium::getProperties(const com::Utf8Str &aNames,
|
---|
2493 | std::vector<com::Utf8Str> &aReturnNames,
|
---|
2494 | std::vector<com::Utf8Str> &aReturnValues)
|
---|
2495 | {
|
---|
2496 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2497 |
|
---|
2498 | /// @todo make use of aNames according to the documentation
|
---|
2499 | NOREF(aNames);
|
---|
2500 |
|
---|
2501 | aReturnNames.resize(m->mapProperties.size());
|
---|
2502 | aReturnValues.resize(m->mapProperties.size());
|
---|
2503 | size_t i = 0;
|
---|
2504 | for (settings::StringsMap::const_iterator it = m->mapProperties.begin();
|
---|
2505 | it != m->mapProperties.end();
|
---|
2506 | ++it, ++i)
|
---|
2507 | {
|
---|
2508 | aReturnNames[i] = it->first;
|
---|
2509 | aReturnValues[i] = it->second;
|
---|
2510 | }
|
---|
2511 | return S_OK;
|
---|
2512 | }
|
---|
2513 |
|
---|
2514 | HRESULT Medium::setProperties(const std::vector<com::Utf8Str> &aNames,
|
---|
2515 | const std::vector<com::Utf8Str> &aValues)
|
---|
2516 | {
|
---|
2517 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2518 |
|
---|
2519 | /* first pass: validate names */
|
---|
2520 | for (size_t i = 0;
|
---|
2521 | i < aNames.size();
|
---|
2522 | ++i)
|
---|
2523 | {
|
---|
2524 | Utf8Str strName(aNames[i]);
|
---|
2525 | if ( !strName.startsWith("Special/")
|
---|
2526 | && !i_isPropertyForFilter(strName)
|
---|
2527 | && m->mapProperties.find(strName) == m->mapProperties.end())
|
---|
2528 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2529 | tr("Property '%s' does not exist"), strName.c_str());
|
---|
2530 | }
|
---|
2531 |
|
---|
2532 | /* second pass: assign */
|
---|
2533 | for (size_t i = 0;
|
---|
2534 | i < aNames.size();
|
---|
2535 | ++i)
|
---|
2536 | {
|
---|
2537 | Utf8Str strName(aNames[i]);
|
---|
2538 | Utf8Str strValue(aValues[i]);
|
---|
2539 | settings::StringsMap::iterator it = m->mapProperties.find(strName);
|
---|
2540 | if ( !strName.startsWith("Special/")
|
---|
2541 | && !i_isPropertyForFilter(strName))
|
---|
2542 | {
|
---|
2543 | AssertReturn(it != m->mapProperties.end(), E_FAIL);
|
---|
2544 | it->second = strValue;
|
---|
2545 | }
|
---|
2546 | else
|
---|
2547 | {
|
---|
2548 | if (it == m->mapProperties.end())
|
---|
2549 | {
|
---|
2550 | if (!strValue.isEmpty())
|
---|
2551 | m->mapProperties[strName] = strValue;
|
---|
2552 | }
|
---|
2553 | else
|
---|
2554 | {
|
---|
2555 | if (!strValue.isEmpty())
|
---|
2556 | it->second = strValue;
|
---|
2557 | else
|
---|
2558 | m->mapProperties.erase(it);
|
---|
2559 | }
|
---|
2560 | }
|
---|
2561 | }
|
---|
2562 |
|
---|
2563 | // save the settings
|
---|
2564 | mlock.release();
|
---|
2565 | i_markRegistriesModified();
|
---|
2566 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2567 |
|
---|
2568 | return S_OK;
|
---|
2569 | }
|
---|
2570 | HRESULT Medium::createBaseStorage(LONG64 aLogicalSize,
|
---|
2571 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2572 | ComPtr<IProgress> &aProgress)
|
---|
2573 | {
|
---|
2574 | if (aLogicalSize < 0)
|
---|
2575 | return setError(E_INVALIDARG, tr("The medium size argument (%lld) is negative"), aLogicalSize);
|
---|
2576 |
|
---|
2577 | HRESULT rc = S_OK;
|
---|
2578 | ComObjPtr<Progress> pProgress;
|
---|
2579 | Medium::Task *pTask = NULL;
|
---|
2580 |
|
---|
2581 | try
|
---|
2582 | {
|
---|
2583 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2584 |
|
---|
2585 | ULONG mediumVariantFlags = 0;
|
---|
2586 |
|
---|
2587 | if (aVariant.size())
|
---|
2588 | {
|
---|
2589 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
2590 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
2591 | }
|
---|
2592 |
|
---|
2593 | mediumVariantFlags &= ((unsigned)~MediumVariant_Diff);
|
---|
2594 |
|
---|
2595 | if ( !(mediumVariantFlags & MediumVariant_Fixed)
|
---|
2596 | && !(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_CreateDynamic))
|
---|
2597 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2598 | tr("Medium format '%s' does not support dynamic storage creation"),
|
---|
2599 | m->strFormat.c_str());
|
---|
2600 |
|
---|
2601 | if ( (mediumVariantFlags & MediumVariant_Fixed)
|
---|
2602 | && !(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_CreateFixed))
|
---|
2603 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2604 | tr("Medium format '%s' does not support fixed storage creation"),
|
---|
2605 | m->strFormat.c_str());
|
---|
2606 |
|
---|
2607 | if (m->state != MediumState_NotCreated)
|
---|
2608 | throw i_setStateError();
|
---|
2609 |
|
---|
2610 | pProgress.createObject();
|
---|
2611 | rc = pProgress->init(m->pVirtualBox,
|
---|
2612 | static_cast<IMedium*>(this),
|
---|
2613 | (mediumVariantFlags & MediumVariant_Fixed)
|
---|
2614 | ? BstrFmt(tr("Creating fixed medium storage unit '%s'"), m->strLocationFull.c_str()).raw()
|
---|
2615 | : BstrFmt(tr("Creating dynamic medium storage unit '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
2616 | TRUE /* aCancelable */);
|
---|
2617 | if (FAILED(rc))
|
---|
2618 | throw rc;
|
---|
2619 |
|
---|
2620 | /* setup task object to carry out the operation asynchronously */
|
---|
2621 | pTask = new Medium::CreateBaseTask(this, pProgress, aLogicalSize,
|
---|
2622 | (MediumVariant_T)mediumVariantFlags);
|
---|
2623 | //(MediumVariant_T)aVariant);
|
---|
2624 | rc = pTask->rc();
|
---|
2625 | AssertComRC(rc);
|
---|
2626 | if (FAILED(rc))
|
---|
2627 | throw rc;
|
---|
2628 |
|
---|
2629 | m->state = MediumState_Creating;
|
---|
2630 | }
|
---|
2631 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2632 |
|
---|
2633 | if (SUCCEEDED(rc))
|
---|
2634 | {
|
---|
2635 | rc = pTask->createThread();
|
---|
2636 |
|
---|
2637 | if (SUCCEEDED(rc))
|
---|
2638 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2639 | }
|
---|
2640 | else if (pTask != NULL)
|
---|
2641 | delete pTask;
|
---|
2642 |
|
---|
2643 | return rc;
|
---|
2644 | }
|
---|
2645 |
|
---|
2646 | HRESULT Medium::deleteStorage(ComPtr<IProgress> &aProgress)
|
---|
2647 | {
|
---|
2648 | ComObjPtr<Progress> pProgress;
|
---|
2649 |
|
---|
2650 | MultiResult mrc = i_deleteStorage(&pProgress,
|
---|
2651 | false /* aWait */);
|
---|
2652 | /* Must save the registries in any case, since an entry was removed. */
|
---|
2653 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2654 |
|
---|
2655 | if (SUCCEEDED(mrc))
|
---|
2656 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2657 |
|
---|
2658 | return mrc;
|
---|
2659 | }
|
---|
2660 |
|
---|
2661 | HRESULT Medium::createDiffStorage(AutoCaller &autoCaller,
|
---|
2662 | const ComPtr<IMedium> &aTarget,
|
---|
2663 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2664 | ComPtr<IProgress> &aProgress)
|
---|
2665 | {
|
---|
2666 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
2667 | * to lock order violations, it probably causes lock order issues related
|
---|
2668 | * to the AutoCaller usage. */
|
---|
2669 | IMedium *aT = aTarget;
|
---|
2670 | ComObjPtr<Medium> diff = static_cast<Medium*>(aT);
|
---|
2671 |
|
---|
2672 | autoCaller.release();
|
---|
2673 |
|
---|
2674 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
2675 | * the pVirtualBox reference, see #uninit(). */
|
---|
2676 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
2677 |
|
---|
2678 | // we access m->pParent
|
---|
2679 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
2680 |
|
---|
2681 | autoCaller.add();
|
---|
2682 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2683 |
|
---|
2684 | AutoMultiWriteLock2 alock(this->lockHandle(), diff->lockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
2685 |
|
---|
2686 | if (m->type == MediumType_Writethrough)
|
---|
2687 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2688 | tr("Medium type of '%s' is Writethrough"),
|
---|
2689 | m->strLocationFull.c_str());
|
---|
2690 | else if (m->type == MediumType_Shareable)
|
---|
2691 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2692 | tr("Medium type of '%s' is Shareable"),
|
---|
2693 | m->strLocationFull.c_str());
|
---|
2694 | else if (m->type == MediumType_Readonly)
|
---|
2695 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2696 | tr("Medium type of '%s' is Readonly"),
|
---|
2697 | m->strLocationFull.c_str());
|
---|
2698 |
|
---|
2699 | /* Apply the normal locking logic to the entire chain. */
|
---|
2700 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
2701 | alock.release();
|
---|
2702 | treeLock.release();
|
---|
2703 | HRESULT rc = diff->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2704 | diff /* pToLockWrite */,
|
---|
2705 | false /* fMediumLockWriteAll */,
|
---|
2706 | this,
|
---|
2707 | *pMediumLockList);
|
---|
2708 | treeLock.acquire();
|
---|
2709 | alock.acquire();
|
---|
2710 | if (FAILED(rc))
|
---|
2711 | {
|
---|
2712 | delete pMediumLockList;
|
---|
2713 | return rc;
|
---|
2714 | }
|
---|
2715 |
|
---|
2716 | alock.release();
|
---|
2717 | treeLock.release();
|
---|
2718 | rc = pMediumLockList->Lock();
|
---|
2719 | treeLock.acquire();
|
---|
2720 | alock.acquire();
|
---|
2721 | if (FAILED(rc))
|
---|
2722 | {
|
---|
2723 | delete pMediumLockList;
|
---|
2724 |
|
---|
2725 | return setError(rc, tr("Could not lock medium when creating diff '%s'"),
|
---|
2726 | diff->i_getLocationFull().c_str());
|
---|
2727 | }
|
---|
2728 |
|
---|
2729 | Guid parentMachineRegistry;
|
---|
2730 | if (i_getFirstRegistryMachineId(parentMachineRegistry))
|
---|
2731 | {
|
---|
2732 | /* since this medium has been just created it isn't associated yet */
|
---|
2733 | diff->m->llRegistryIDs.push_back(parentMachineRegistry);
|
---|
2734 | alock.release();
|
---|
2735 | treeLock.release();
|
---|
2736 | diff->i_markRegistriesModified();
|
---|
2737 | treeLock.acquire();
|
---|
2738 | alock.acquire();
|
---|
2739 | }
|
---|
2740 |
|
---|
2741 | alock.release();
|
---|
2742 | treeLock.release();
|
---|
2743 |
|
---|
2744 | ComObjPtr<Progress> pProgress;
|
---|
2745 |
|
---|
2746 | ULONG mediumVariantFlags = 0;
|
---|
2747 |
|
---|
2748 | if (aVariant.size())
|
---|
2749 | {
|
---|
2750 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
2751 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
2752 | }
|
---|
2753 |
|
---|
2754 | rc = i_createDiffStorage(diff, (MediumVariant_T)mediumVariantFlags, pMediumLockList,
|
---|
2755 | &pProgress, false /* aWait */);
|
---|
2756 | if (FAILED(rc))
|
---|
2757 | delete pMediumLockList;
|
---|
2758 | else
|
---|
2759 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2760 |
|
---|
2761 | return rc;
|
---|
2762 | }
|
---|
2763 |
|
---|
2764 | HRESULT Medium::mergeTo(const ComPtr<IMedium> &aTarget,
|
---|
2765 | ComPtr<IProgress> &aProgress)
|
---|
2766 | {
|
---|
2767 |
|
---|
2768 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
2769 | * to lock order violations, it probably causes lock order issues related
|
---|
2770 | * to the AutoCaller usage. */
|
---|
2771 | IMedium *aT = aTarget;
|
---|
2772 |
|
---|
2773 | ComAssertRet(aT != this, E_INVALIDARG);
|
---|
2774 |
|
---|
2775 | ComObjPtr<Medium> pTarget = static_cast<Medium*>(aT);
|
---|
2776 |
|
---|
2777 | bool fMergeForward = false;
|
---|
2778 | ComObjPtr<Medium> pParentForTarget;
|
---|
2779 | MediumLockList *pChildrenToReparent = NULL;
|
---|
2780 | MediumLockList *pMediumLockList = NULL;
|
---|
2781 |
|
---|
2782 | HRESULT rc = S_OK;
|
---|
2783 |
|
---|
2784 | rc = i_prepareMergeTo(pTarget, NULL, NULL, true, fMergeForward,
|
---|
2785 | pParentForTarget, pChildrenToReparent, pMediumLockList);
|
---|
2786 | if (FAILED(rc)) return rc;
|
---|
2787 |
|
---|
2788 | ComObjPtr<Progress> pProgress;
|
---|
2789 |
|
---|
2790 | rc = i_mergeTo(pTarget, fMergeForward, pParentForTarget, pChildrenToReparent,
|
---|
2791 | pMediumLockList, &pProgress, false /* aWait */);
|
---|
2792 | if (FAILED(rc))
|
---|
2793 | i_cancelMergeTo(pChildrenToReparent, pMediumLockList);
|
---|
2794 | else
|
---|
2795 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2796 |
|
---|
2797 | return rc;
|
---|
2798 | }
|
---|
2799 |
|
---|
2800 | HRESULT Medium::cloneToBase(const ComPtr<IMedium> &aTarget,
|
---|
2801 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2802 | ComPtr<IProgress> &aProgress)
|
---|
2803 | {
|
---|
2804 | int rc = S_OK;
|
---|
2805 |
|
---|
2806 | rc = cloneTo(aTarget, aVariant, NULL, aProgress);
|
---|
2807 | return rc;
|
---|
2808 | }
|
---|
2809 |
|
---|
2810 | HRESULT Medium::cloneTo(const ComPtr<IMedium> &aTarget,
|
---|
2811 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2812 | const ComPtr<IMedium> &aParent,
|
---|
2813 | ComPtr<IProgress> &aProgress)
|
---|
2814 | {
|
---|
2815 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
2816 | * to lock order violations, it probably causes lock order issues related
|
---|
2817 | * to the AutoCaller usage. */
|
---|
2818 | ComAssertRet(aTarget != this, E_INVALIDARG);
|
---|
2819 |
|
---|
2820 | IMedium *aT = aTarget;
|
---|
2821 | ComObjPtr<Medium> pTarget = static_cast<Medium*>(aT);
|
---|
2822 | ComObjPtr<Medium> pParent;
|
---|
2823 | if (aParent)
|
---|
2824 | {
|
---|
2825 | IMedium *aP = aParent;
|
---|
2826 | pParent = static_cast<Medium*>(aP);
|
---|
2827 | }
|
---|
2828 |
|
---|
2829 | HRESULT rc = S_OK;
|
---|
2830 | ComObjPtr<Progress> pProgress;
|
---|
2831 | Medium::Task *pTask = NULL;
|
---|
2832 |
|
---|
2833 | try
|
---|
2834 | {
|
---|
2835 | // locking: we need the tree lock first because we access parent pointers
|
---|
2836 | // and we need to write-lock the media involved
|
---|
2837 | uint32_t cHandles = 3;
|
---|
2838 | LockHandle* pHandles[4] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
2839 | this->lockHandle(),
|
---|
2840 | pTarget->lockHandle() };
|
---|
2841 | /* Only add parent to the lock if it is not null */
|
---|
2842 | if (!pParent.isNull())
|
---|
2843 | pHandles[cHandles++] = pParent->lockHandle();
|
---|
2844 | AutoWriteLock alock(cHandles,
|
---|
2845 | pHandles
|
---|
2846 | COMMA_LOCKVAL_SRC_POS);
|
---|
2847 |
|
---|
2848 | if ( pTarget->m->state != MediumState_NotCreated
|
---|
2849 | && pTarget->m->state != MediumState_Created)
|
---|
2850 | throw pTarget->i_setStateError();
|
---|
2851 |
|
---|
2852 | /* Build the source lock list. */
|
---|
2853 | MediumLockList *pSourceMediumLockList(new MediumLockList());
|
---|
2854 | alock.release();
|
---|
2855 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2856 | NULL /* pToLockWrite */,
|
---|
2857 | false /* fMediumLockWriteAll */,
|
---|
2858 | NULL,
|
---|
2859 | *pSourceMediumLockList);
|
---|
2860 | alock.acquire();
|
---|
2861 | if (FAILED(rc))
|
---|
2862 | {
|
---|
2863 | delete pSourceMediumLockList;
|
---|
2864 | throw rc;
|
---|
2865 | }
|
---|
2866 |
|
---|
2867 | /* Build the target lock list (including the to-be parent chain). */
|
---|
2868 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
2869 | alock.release();
|
---|
2870 | rc = pTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2871 | pTarget /* pToLockWrite */,
|
---|
2872 | false /* fMediumLockWriteAll */,
|
---|
2873 | pParent,
|
---|
2874 | *pTargetMediumLockList);
|
---|
2875 | alock.acquire();
|
---|
2876 | if (FAILED(rc))
|
---|
2877 | {
|
---|
2878 | delete pSourceMediumLockList;
|
---|
2879 | delete pTargetMediumLockList;
|
---|
2880 | throw rc;
|
---|
2881 | }
|
---|
2882 |
|
---|
2883 | alock.release();
|
---|
2884 | rc = pSourceMediumLockList->Lock();
|
---|
2885 | alock.acquire();
|
---|
2886 | if (FAILED(rc))
|
---|
2887 | {
|
---|
2888 | delete pSourceMediumLockList;
|
---|
2889 | delete pTargetMediumLockList;
|
---|
2890 | throw setError(rc,
|
---|
2891 | tr("Failed to lock source media '%s'"),
|
---|
2892 | i_getLocationFull().c_str());
|
---|
2893 | }
|
---|
2894 | alock.release();
|
---|
2895 | rc = pTargetMediumLockList->Lock();
|
---|
2896 | alock.acquire();
|
---|
2897 | if (FAILED(rc))
|
---|
2898 | {
|
---|
2899 | delete pSourceMediumLockList;
|
---|
2900 | delete pTargetMediumLockList;
|
---|
2901 | throw setError(rc,
|
---|
2902 | tr("Failed to lock target media '%s'"),
|
---|
2903 | pTarget->i_getLocationFull().c_str());
|
---|
2904 | }
|
---|
2905 |
|
---|
2906 | pProgress.createObject();
|
---|
2907 | rc = pProgress->init(m->pVirtualBox,
|
---|
2908 | static_cast <IMedium *>(this),
|
---|
2909 | BstrFmt(tr("Creating clone medium '%s'"), pTarget->m->strLocationFull.c_str()).raw(),
|
---|
2910 | TRUE /* aCancelable */);
|
---|
2911 | if (FAILED(rc))
|
---|
2912 | {
|
---|
2913 | delete pSourceMediumLockList;
|
---|
2914 | delete pTargetMediumLockList;
|
---|
2915 | throw rc;
|
---|
2916 | }
|
---|
2917 |
|
---|
2918 | ULONG mediumVariantFlags = 0;
|
---|
2919 |
|
---|
2920 | if (aVariant.size())
|
---|
2921 | {
|
---|
2922 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
2923 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
2924 | }
|
---|
2925 |
|
---|
2926 | /* setup task object to carry out the operation asynchronously */
|
---|
2927 | pTask = new Medium::CloneTask(this, pProgress, pTarget,
|
---|
2928 | (MediumVariant_T)mediumVariantFlags,
|
---|
2929 | pParent, UINT32_MAX, UINT32_MAX,
|
---|
2930 | pSourceMediumLockList, pTargetMediumLockList);
|
---|
2931 | rc = pTask->rc();
|
---|
2932 | AssertComRC(rc);
|
---|
2933 | if (FAILED(rc))
|
---|
2934 | throw rc;
|
---|
2935 |
|
---|
2936 | if (pTarget->m->state == MediumState_NotCreated)
|
---|
2937 | pTarget->m->state = MediumState_Creating;
|
---|
2938 | }
|
---|
2939 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2940 |
|
---|
2941 | if (SUCCEEDED(rc))
|
---|
2942 | {
|
---|
2943 | rc = pTask->createThread();
|
---|
2944 |
|
---|
2945 | if (SUCCEEDED(rc))
|
---|
2946 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2947 | }
|
---|
2948 | else if (pTask != NULL)
|
---|
2949 | delete pTask;
|
---|
2950 |
|
---|
2951 | return rc;
|
---|
2952 | }
|
---|
2953 |
|
---|
2954 | HRESULT Medium::setLocation(const com::Utf8Str &aLocation, ComPtr<IProgress> &aProgress)
|
---|
2955 | {
|
---|
2956 |
|
---|
2957 | ComObjPtr<Medium> pParent;
|
---|
2958 | ComObjPtr<Progress> pProgress;
|
---|
2959 | HRESULT rc = S_OK;
|
---|
2960 | Medium::Task *pTask = NULL;
|
---|
2961 |
|
---|
2962 | try
|
---|
2963 | {
|
---|
2964 | /// @todo NEWMEDIA for file names, add the default extension if no extension
|
---|
2965 | /// is present (using the information from the VD backend which also implies
|
---|
2966 | /// that one more parameter should be passed to setLocation() requesting
|
---|
2967 | /// that functionality since it is only allowed when called from this method
|
---|
2968 |
|
---|
2969 | /// @todo NEWMEDIA rename the file and set m->location on success, then save
|
---|
2970 | /// the global registry (and local registries of portable VMs referring to
|
---|
2971 | /// this medium), this will also require to add the mRegistered flag to data
|
---|
2972 |
|
---|
2973 | // locking: we need the tree lock first because we access parent pointers
|
---|
2974 | // and we need to write-lock the media involved
|
---|
2975 | uint32_t cHandles = 2;
|
---|
2976 | LockHandle* pHandles[2] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
2977 | this->lockHandle() };
|
---|
2978 |
|
---|
2979 | AutoWriteLock alock(cHandles,
|
---|
2980 | pHandles
|
---|
2981 | COMMA_LOCKVAL_SRC_POS);
|
---|
2982 |
|
---|
2983 | /* play with locations */
|
---|
2984 | {
|
---|
2985 | /* get source path and filename */
|
---|
2986 | Utf8Str sourcePath = i_getLocationFull();
|
---|
2987 | Utf8Str sourceFName = i_getName();
|
---|
2988 |
|
---|
2989 | if (aLocation.isEmpty())
|
---|
2990 | {
|
---|
2991 | rc = setError(VERR_PATH_ZERO_LENGTH,
|
---|
2992 | tr("Medium '%s' can't be moved. Destination path is empty."),
|
---|
2993 | i_getLocationFull().c_str());
|
---|
2994 | throw rc;
|
---|
2995 | }
|
---|
2996 |
|
---|
2997 | /* extract destination path and filename */
|
---|
2998 | Utf8Str destPath(aLocation);
|
---|
2999 | Utf8Str destFName(destPath);
|
---|
3000 | destFName.stripPath();
|
---|
3001 |
|
---|
3002 | Utf8Str suffix(destFName);
|
---|
3003 | suffix.stripSuffix();
|
---|
3004 |
|
---|
3005 | if (suffix.equals(destFName) && !destFName.isEmpty())
|
---|
3006 | {
|
---|
3007 | /*
|
---|
3008 | * The target path has no filename: Either "/path/to/new/location" or
|
---|
3009 | * just "newname" (no trailing backslash or there is no filename with
|
---|
3010 | * extension(suffix)).
|
---|
3011 | */
|
---|
3012 | if (destPath.equals(destFName))
|
---|
3013 | {
|
---|
3014 | /* new path contains only "newname", no path, no extension */
|
---|
3015 | destFName.append(RTPathSuffix(sourceFName.c_str()));
|
---|
3016 | destPath = destFName;
|
---|
3017 | }
|
---|
3018 | else
|
---|
3019 | {
|
---|
3020 | /* new path looks like "/path/to/new/location" */
|
---|
3021 | destFName.setNull();
|
---|
3022 | destPath.append(RTPATH_SLASH);
|
---|
3023 | }
|
---|
3024 | }
|
---|
3025 |
|
---|
3026 | if (destFName.isEmpty())
|
---|
3027 | {
|
---|
3028 | /* No target name */
|
---|
3029 | destPath.append(sourceFName);
|
---|
3030 | }
|
---|
3031 | else
|
---|
3032 | {
|
---|
3033 | if (destPath.equals(destFName))
|
---|
3034 | {
|
---|
3035 | /*
|
---|
3036 | * The target path contains of only a filename without a directory.
|
---|
3037 | * Move the medium within the source directory to the new name
|
---|
3038 | * (actually rename operation).
|
---|
3039 | * Scratches sourcePath!
|
---|
3040 | */
|
---|
3041 | destPath = sourcePath.stripFilename().append(RTPATH_SLASH).append(destFName);
|
---|
3042 | }
|
---|
3043 | suffix = i_getFormat();
|
---|
3044 | if (suffix.compare("RAW", Utf8Str::CaseInsensitive) == 0)
|
---|
3045 | {
|
---|
3046 | if (i_getDeviceType() == DeviceType_DVD)
|
---|
3047 | suffix = "iso";
|
---|
3048 | else
|
---|
3049 | {
|
---|
3050 | rc = setError(VERR_NOT_A_FILE,
|
---|
3051 | tr("Medium '%s' has RAW type. \"Move\" operation isn't supported for this type."),
|
---|
3052 | i_getLocationFull().c_str());
|
---|
3053 | throw rc;
|
---|
3054 | }
|
---|
3055 | }
|
---|
3056 | /* Set the target extension like on the source. Any conversions are prohibited */
|
---|
3057 | suffix.toLower();
|
---|
3058 | destPath.stripSuffix().append('.').append(suffix);
|
---|
3059 | }
|
---|
3060 |
|
---|
3061 | if (!i_isMediumFormatFile())
|
---|
3062 | {
|
---|
3063 | rc = setError(VERR_NOT_A_FILE,
|
---|
3064 | tr("Medium '%s' isn't a file object. \"Move\" operation isn't supported."),
|
---|
3065 | i_getLocationFull().c_str());
|
---|
3066 | throw rc;
|
---|
3067 | }
|
---|
3068 | /* Path must be absolute */
|
---|
3069 | if (!RTPathStartsWithRoot(destPath.c_str()))
|
---|
3070 | {
|
---|
3071 | rc = setError(VBOX_E_FILE_ERROR,
|
---|
3072 | tr("The given path '%s' is not fully qualified"),
|
---|
3073 | destPath.c_str());
|
---|
3074 | throw rc;
|
---|
3075 | }
|
---|
3076 | /* Check path for a new file object */
|
---|
3077 | rc = VirtualBox::i_ensureFilePathExists(destPath, true);
|
---|
3078 | if (FAILED(rc))
|
---|
3079 | throw rc;
|
---|
3080 |
|
---|
3081 | /* Set needed variables for "moving" procedure. It'll be used later in separate thread task */
|
---|
3082 | rc = i_preparationForMoving(destPath);
|
---|
3083 | if (FAILED(rc))
|
---|
3084 | {
|
---|
3085 | rc = setError(VERR_NO_CHANGE,
|
---|
3086 | tr("Medium '%s' is already in the correct location"),
|
---|
3087 | i_getLocationFull().c_str());
|
---|
3088 | throw rc;
|
---|
3089 | }
|
---|
3090 | }
|
---|
3091 |
|
---|
3092 | /* Check VMs which have this medium attached to*/
|
---|
3093 | std::vector<com::Guid> aMachineIds;
|
---|
3094 | rc = getMachineIds(aMachineIds);
|
---|
3095 | std::vector<com::Guid>::const_iterator currMachineID = aMachineIds.begin();
|
---|
3096 | std::vector<com::Guid>::const_iterator lastMachineID = aMachineIds.end();
|
---|
3097 |
|
---|
3098 | while (currMachineID != lastMachineID)
|
---|
3099 | {
|
---|
3100 | Guid id(*currMachineID);
|
---|
3101 | ComObjPtr<Machine> aMachine;
|
---|
3102 |
|
---|
3103 | alock.release();
|
---|
3104 | rc = m->pVirtualBox->i_findMachine(id, false, true, &aMachine);
|
---|
3105 | alock.acquire();
|
---|
3106 |
|
---|
3107 | if (SUCCEEDED(rc))
|
---|
3108 | {
|
---|
3109 | ComObjPtr<SessionMachine> sm;
|
---|
3110 | ComPtr<IInternalSessionControl> ctl;
|
---|
3111 |
|
---|
3112 | alock.release();
|
---|
3113 | bool ses = aMachine->i_isSessionOpenVM(sm, &ctl);
|
---|
3114 | alock.acquire();
|
---|
3115 |
|
---|
3116 | if (ses)
|
---|
3117 | {
|
---|
3118 | rc = setError(VERR_VM_UNEXPECTED_VM_STATE,
|
---|
3119 | tr("At least the VM '%s' to whom this medium '%s' attached has currently an opened session. Stop all VMs before relocating this medium"),
|
---|
3120 | id.toString().c_str(),
|
---|
3121 | i_getLocationFull().c_str());
|
---|
3122 | throw rc;
|
---|
3123 | }
|
---|
3124 | }
|
---|
3125 | ++currMachineID;
|
---|
3126 | }
|
---|
3127 |
|
---|
3128 | /* Build the source lock list. */
|
---|
3129 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3130 | alock.release();
|
---|
3131 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
3132 | this /* pToLockWrite */,
|
---|
3133 | true /* fMediumLockWriteAll */,
|
---|
3134 | NULL,
|
---|
3135 | *pMediumLockList);
|
---|
3136 | alock.acquire();
|
---|
3137 | if (FAILED(rc))
|
---|
3138 | {
|
---|
3139 | delete pMediumLockList;
|
---|
3140 | throw setError(rc,
|
---|
3141 | tr("Failed to create medium lock list for '%s'"),
|
---|
3142 | i_getLocationFull().c_str());
|
---|
3143 | }
|
---|
3144 | alock.release();
|
---|
3145 | rc = pMediumLockList->Lock();
|
---|
3146 | alock.acquire();
|
---|
3147 | if (FAILED(rc))
|
---|
3148 | {
|
---|
3149 | delete pMediumLockList;
|
---|
3150 | throw setError(rc,
|
---|
3151 | tr("Failed to lock media '%s'"),
|
---|
3152 | i_getLocationFull().c_str());
|
---|
3153 | }
|
---|
3154 |
|
---|
3155 | pProgress.createObject();
|
---|
3156 | rc = pProgress->init(m->pVirtualBox,
|
---|
3157 | static_cast <IMedium *>(this),
|
---|
3158 | BstrFmt(tr("Moving medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3159 | TRUE /* aCancelable */);
|
---|
3160 |
|
---|
3161 | /* Do the disk moving. */
|
---|
3162 | if (SUCCEEDED(rc))
|
---|
3163 | {
|
---|
3164 | ULONG mediumVariantFlags = i_getVariant();
|
---|
3165 |
|
---|
3166 | /* setup task object to carry out the operation asynchronously */
|
---|
3167 | pTask = new Medium::MoveTask(this, pProgress,
|
---|
3168 | (MediumVariant_T)mediumVariantFlags,
|
---|
3169 | pMediumLockList);
|
---|
3170 | rc = pTask->rc();
|
---|
3171 | AssertComRC(rc);
|
---|
3172 | if (FAILED(rc))
|
---|
3173 | throw rc;
|
---|
3174 | }
|
---|
3175 |
|
---|
3176 | }
|
---|
3177 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3178 |
|
---|
3179 | if (SUCCEEDED(rc))
|
---|
3180 | {
|
---|
3181 | rc = pTask->createThread();
|
---|
3182 |
|
---|
3183 | if (SUCCEEDED(rc))
|
---|
3184 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3185 | }
|
---|
3186 | else
|
---|
3187 | {
|
---|
3188 | if (pTask)
|
---|
3189 | delete pTask;
|
---|
3190 | }
|
---|
3191 |
|
---|
3192 | return rc;
|
---|
3193 | }
|
---|
3194 |
|
---|
3195 | HRESULT Medium::compact(ComPtr<IProgress> &aProgress)
|
---|
3196 | {
|
---|
3197 | HRESULT rc = S_OK;
|
---|
3198 | ComObjPtr<Progress> pProgress;
|
---|
3199 | Medium::Task *pTask = NULL;
|
---|
3200 |
|
---|
3201 | try
|
---|
3202 | {
|
---|
3203 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3204 |
|
---|
3205 | /* Build the medium lock list. */
|
---|
3206 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3207 | alock.release();
|
---|
3208 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
3209 | this /* pToLockWrite */,
|
---|
3210 | false /* fMediumLockWriteAll */,
|
---|
3211 | NULL,
|
---|
3212 | *pMediumLockList);
|
---|
3213 | alock.acquire();
|
---|
3214 | if (FAILED(rc))
|
---|
3215 | {
|
---|
3216 | delete pMediumLockList;
|
---|
3217 | throw rc;
|
---|
3218 | }
|
---|
3219 |
|
---|
3220 | alock.release();
|
---|
3221 | rc = pMediumLockList->Lock();
|
---|
3222 | alock.acquire();
|
---|
3223 | if (FAILED(rc))
|
---|
3224 | {
|
---|
3225 | delete pMediumLockList;
|
---|
3226 | throw setError(rc,
|
---|
3227 | tr("Failed to lock media when compacting '%s'"),
|
---|
3228 | i_getLocationFull().c_str());
|
---|
3229 | }
|
---|
3230 |
|
---|
3231 | pProgress.createObject();
|
---|
3232 | rc = pProgress->init(m->pVirtualBox,
|
---|
3233 | static_cast <IMedium *>(this),
|
---|
3234 | BstrFmt(tr("Compacting medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3235 | TRUE /* aCancelable */);
|
---|
3236 | if (FAILED(rc))
|
---|
3237 | {
|
---|
3238 | delete pMediumLockList;
|
---|
3239 | throw rc;
|
---|
3240 | }
|
---|
3241 |
|
---|
3242 | /* setup task object to carry out the operation asynchronously */
|
---|
3243 | pTask = new Medium::CompactTask(this, pProgress, pMediumLockList);
|
---|
3244 | rc = pTask->rc();
|
---|
3245 | AssertComRC(rc);
|
---|
3246 | if (FAILED(rc))
|
---|
3247 | throw rc;
|
---|
3248 | }
|
---|
3249 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3250 |
|
---|
3251 | if (SUCCEEDED(rc))
|
---|
3252 | {
|
---|
3253 | rc = pTask->createThread();
|
---|
3254 |
|
---|
3255 | if (SUCCEEDED(rc))
|
---|
3256 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3257 | }
|
---|
3258 | else if (pTask != NULL)
|
---|
3259 | delete pTask;
|
---|
3260 |
|
---|
3261 | return rc;
|
---|
3262 | }
|
---|
3263 |
|
---|
3264 | HRESULT Medium::resize(LONG64 aLogicalSize,
|
---|
3265 | ComPtr<IProgress> &aProgress)
|
---|
3266 | {
|
---|
3267 | HRESULT rc = S_OK;
|
---|
3268 | ComObjPtr<Progress> pProgress;
|
---|
3269 | Medium::Task *pTask = NULL;
|
---|
3270 |
|
---|
3271 | try
|
---|
3272 | {
|
---|
3273 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3274 |
|
---|
3275 | /* Build the medium lock list. */
|
---|
3276 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3277 | alock.release();
|
---|
3278 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
3279 | this /* pToLockWrite */,
|
---|
3280 | false /* fMediumLockWriteAll */,
|
---|
3281 | NULL,
|
---|
3282 | *pMediumLockList);
|
---|
3283 | alock.acquire();
|
---|
3284 | if (FAILED(rc))
|
---|
3285 | {
|
---|
3286 | delete pMediumLockList;
|
---|
3287 | throw rc;
|
---|
3288 | }
|
---|
3289 |
|
---|
3290 | alock.release();
|
---|
3291 | rc = pMediumLockList->Lock();
|
---|
3292 | alock.acquire();
|
---|
3293 | if (FAILED(rc))
|
---|
3294 | {
|
---|
3295 | delete pMediumLockList;
|
---|
3296 | throw setError(rc,
|
---|
3297 | tr("Failed to lock media when compacting '%s'"),
|
---|
3298 | i_getLocationFull().c_str());
|
---|
3299 | }
|
---|
3300 |
|
---|
3301 | pProgress.createObject();
|
---|
3302 | rc = pProgress->init(m->pVirtualBox,
|
---|
3303 | static_cast <IMedium *>(this),
|
---|
3304 | BstrFmt(tr("Compacting medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3305 | TRUE /* aCancelable */);
|
---|
3306 | if (FAILED(rc))
|
---|
3307 | {
|
---|
3308 | delete pMediumLockList;
|
---|
3309 | throw rc;
|
---|
3310 | }
|
---|
3311 |
|
---|
3312 | /* setup task object to carry out the operation asynchronously */
|
---|
3313 | pTask = new Medium::ResizeTask(this, aLogicalSize, pProgress, pMediumLockList);
|
---|
3314 | rc = pTask->rc();
|
---|
3315 | AssertComRC(rc);
|
---|
3316 | if (FAILED(rc))
|
---|
3317 | throw rc;
|
---|
3318 | }
|
---|
3319 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3320 |
|
---|
3321 | if (SUCCEEDED(rc))
|
---|
3322 | {
|
---|
3323 | rc = pTask->createThread();
|
---|
3324 |
|
---|
3325 | if (SUCCEEDED(rc))
|
---|
3326 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3327 | }
|
---|
3328 | else if (pTask != NULL)
|
---|
3329 | delete pTask;
|
---|
3330 |
|
---|
3331 | return rc;
|
---|
3332 | }
|
---|
3333 |
|
---|
3334 | HRESULT Medium::reset(AutoCaller &autoCaller, ComPtr<IProgress> &aProgress)
|
---|
3335 | {
|
---|
3336 | HRESULT rc = S_OK;
|
---|
3337 | ComObjPtr<Progress> pProgress;
|
---|
3338 | Medium::Task *pTask = NULL;
|
---|
3339 |
|
---|
3340 | try
|
---|
3341 | {
|
---|
3342 | autoCaller.release();
|
---|
3343 |
|
---|
3344 | /* It is possible that some previous/concurrent uninit has already
|
---|
3345 | * cleared the pVirtualBox reference, see #uninit(). */
|
---|
3346 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
3347 |
|
---|
3348 | /* canClose() needs the tree lock */
|
---|
3349 | AutoMultiWriteLock2 multilock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL,
|
---|
3350 | this->lockHandle()
|
---|
3351 | COMMA_LOCKVAL_SRC_POS);
|
---|
3352 |
|
---|
3353 | autoCaller.add();
|
---|
3354 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
3355 |
|
---|
3356 | LogFlowThisFunc(("ENTER for medium %s\n", m->strLocationFull.c_str()));
|
---|
3357 |
|
---|
3358 | if (m->pParent.isNull())
|
---|
3359 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3360 | tr("Medium type of '%s' is not differencing"),
|
---|
3361 | m->strLocationFull.c_str());
|
---|
3362 |
|
---|
3363 | rc = i_canClose();
|
---|
3364 | if (FAILED(rc))
|
---|
3365 | throw rc;
|
---|
3366 |
|
---|
3367 | /* Build the medium lock list. */
|
---|
3368 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3369 | multilock.release();
|
---|
3370 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
3371 | this /* pToLockWrite */,
|
---|
3372 | false /* fMediumLockWriteAll */,
|
---|
3373 | NULL,
|
---|
3374 | *pMediumLockList);
|
---|
3375 | multilock.acquire();
|
---|
3376 | if (FAILED(rc))
|
---|
3377 | {
|
---|
3378 | delete pMediumLockList;
|
---|
3379 | throw rc;
|
---|
3380 | }
|
---|
3381 |
|
---|
3382 | multilock.release();
|
---|
3383 | rc = pMediumLockList->Lock();
|
---|
3384 | multilock.acquire();
|
---|
3385 | if (FAILED(rc))
|
---|
3386 | {
|
---|
3387 | delete pMediumLockList;
|
---|
3388 | throw setError(rc,
|
---|
3389 | tr("Failed to lock media when resetting '%s'"),
|
---|
3390 | i_getLocationFull().c_str());
|
---|
3391 | }
|
---|
3392 |
|
---|
3393 | pProgress.createObject();
|
---|
3394 | rc = pProgress->init(m->pVirtualBox,
|
---|
3395 | static_cast<IMedium*>(this),
|
---|
3396 | BstrFmt(tr("Resetting differencing medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3397 | FALSE /* aCancelable */);
|
---|
3398 | if (FAILED(rc))
|
---|
3399 | throw rc;
|
---|
3400 |
|
---|
3401 | /* setup task object to carry out the operation asynchronously */
|
---|
3402 | pTask = new Medium::ResetTask(this, pProgress, pMediumLockList);
|
---|
3403 | rc = pTask->rc();
|
---|
3404 | AssertComRC(rc);
|
---|
3405 | if (FAILED(rc))
|
---|
3406 | throw rc;
|
---|
3407 | }
|
---|
3408 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3409 |
|
---|
3410 | if (SUCCEEDED(rc))
|
---|
3411 | {
|
---|
3412 | rc = pTask->createThread();
|
---|
3413 |
|
---|
3414 | if (SUCCEEDED(rc))
|
---|
3415 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3416 | }
|
---|
3417 | else if (pTask != NULL)
|
---|
3418 | delete pTask;
|
---|
3419 |
|
---|
3420 | LogFlowThisFunc(("LEAVE, rc=%Rhrc\n", rc));
|
---|
3421 |
|
---|
3422 | return rc;
|
---|
3423 | }
|
---|
3424 |
|
---|
3425 | HRESULT Medium::changeEncryption(const com::Utf8Str &aCurrentPassword, const com::Utf8Str &aCipher,
|
---|
3426 | const com::Utf8Str &aNewPassword, const com::Utf8Str &aNewPasswordId,
|
---|
3427 | ComPtr<IProgress> &aProgress)
|
---|
3428 | {
|
---|
3429 | HRESULT rc = S_OK;
|
---|
3430 | ComObjPtr<Progress> pProgress;
|
---|
3431 | Medium::Task *pTask = NULL;
|
---|
3432 |
|
---|
3433 | try
|
---|
3434 | {
|
---|
3435 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3436 |
|
---|
3437 | DeviceType_T devType = i_getDeviceType();
|
---|
3438 | /* Cannot encrypt DVD or floppy images so far. */
|
---|
3439 | if ( devType == DeviceType_DVD
|
---|
3440 | || devType == DeviceType_Floppy)
|
---|
3441 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3442 | tr("Cannot encrypt DVD or Floppy medium '%s'"),
|
---|
3443 | m->strLocationFull.c_str());
|
---|
3444 |
|
---|
3445 | /* Cannot encrypt media which are attached to more than one virtual machine. */
|
---|
3446 | if (m->backRefs.size() > 1)
|
---|
3447 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3448 | tr("Cannot encrypt medium '%s' because it is attached to %d virtual machines"),
|
---|
3449 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
3450 |
|
---|
3451 | if (i_getChildren().size() != 0)
|
---|
3452 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3453 | tr("Cannot encrypt medium '%s' because it has %d children"),
|
---|
3454 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
3455 |
|
---|
3456 | /* Build the medium lock list. */
|
---|
3457 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3458 | alock.release();
|
---|
3459 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
3460 | this /* pToLockWrite */,
|
---|
3461 | true /* fMediumLockAllWrite */,
|
---|
3462 | NULL,
|
---|
3463 | *pMediumLockList);
|
---|
3464 | alock.acquire();
|
---|
3465 | if (FAILED(rc))
|
---|
3466 | {
|
---|
3467 | delete pMediumLockList;
|
---|
3468 | throw rc;
|
---|
3469 | }
|
---|
3470 |
|
---|
3471 | alock.release();
|
---|
3472 | rc = pMediumLockList->Lock();
|
---|
3473 | alock.acquire();
|
---|
3474 | if (FAILED(rc))
|
---|
3475 | {
|
---|
3476 | delete pMediumLockList;
|
---|
3477 | throw setError(rc,
|
---|
3478 | tr("Failed to lock media for encryption '%s'"),
|
---|
3479 | i_getLocationFull().c_str());
|
---|
3480 | }
|
---|
3481 |
|
---|
3482 | /*
|
---|
3483 | * Check all media in the chain to not contain any branches or references to
|
---|
3484 | * other virtual machines, we support encrypting only a list of differencing media at the moment.
|
---|
3485 | */
|
---|
3486 | MediumLockList::Base::const_iterator mediumListBegin = pMediumLockList->GetBegin();
|
---|
3487 | MediumLockList::Base::const_iterator mediumListEnd = pMediumLockList->GetEnd();
|
---|
3488 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
3489 | it != mediumListEnd;
|
---|
3490 | ++it)
|
---|
3491 | {
|
---|
3492 | const MediumLock &mediumLock = *it;
|
---|
3493 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
3494 | AutoReadLock mediumReadLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
3495 |
|
---|
3496 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
3497 |
|
---|
3498 | if (pMedium->m->backRefs.size() > 1)
|
---|
3499 | {
|
---|
3500 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3501 | tr("Cannot encrypt medium '%s' because it is attached to %d virtual machines"),
|
---|
3502 | pMedium->m->strLocationFull.c_str(), pMedium->m->backRefs.size());
|
---|
3503 | break;
|
---|
3504 | }
|
---|
3505 | else if (pMedium->i_getChildren().size() > 1)
|
---|
3506 | {
|
---|
3507 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3508 | tr("Cannot encrypt medium '%s' because it has %d children"),
|
---|
3509 | pMedium->m->strLocationFull.c_str(), pMedium->i_getChildren().size());
|
---|
3510 | break;
|
---|
3511 | }
|
---|
3512 | }
|
---|
3513 |
|
---|
3514 | if (FAILED(rc))
|
---|
3515 | {
|
---|
3516 | delete pMediumLockList;
|
---|
3517 | throw rc;
|
---|
3518 | }
|
---|
3519 |
|
---|
3520 | const char *pszAction = "Encrypting";
|
---|
3521 | if ( aCurrentPassword.isNotEmpty()
|
---|
3522 | && aCipher.isEmpty())
|
---|
3523 | pszAction = "Decrypting";
|
---|
3524 |
|
---|
3525 | pProgress.createObject();
|
---|
3526 | rc = pProgress->init(m->pVirtualBox,
|
---|
3527 | static_cast <IMedium *>(this),
|
---|
3528 | BstrFmt(tr("%s medium '%s'"), pszAction, m->strLocationFull.c_str()).raw(),
|
---|
3529 | TRUE /* aCancelable */);
|
---|
3530 | if (FAILED(rc))
|
---|
3531 | {
|
---|
3532 | delete pMediumLockList;
|
---|
3533 | throw rc;
|
---|
3534 | }
|
---|
3535 |
|
---|
3536 | /* setup task object to carry out the operation asynchronously */
|
---|
3537 | pTask = new Medium::EncryptTask(this, aNewPassword, aCurrentPassword,
|
---|
3538 | aCipher, aNewPasswordId, pProgress, pMediumLockList);
|
---|
3539 | rc = pTask->rc();
|
---|
3540 | AssertComRC(rc);
|
---|
3541 | if (FAILED(rc))
|
---|
3542 | throw rc;
|
---|
3543 | }
|
---|
3544 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3545 |
|
---|
3546 | if (SUCCEEDED(rc))
|
---|
3547 | {
|
---|
3548 | rc = pTask->createThread();
|
---|
3549 |
|
---|
3550 | if (SUCCEEDED(rc))
|
---|
3551 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3552 | }
|
---|
3553 | else if (pTask != NULL)
|
---|
3554 | delete pTask;
|
---|
3555 |
|
---|
3556 | return rc;
|
---|
3557 | }
|
---|
3558 |
|
---|
3559 | HRESULT Medium::getEncryptionSettings(com::Utf8Str &aCipher, com::Utf8Str &aPasswordId)
|
---|
3560 | {
|
---|
3561 | #ifndef VBOX_WITH_EXTPACK
|
---|
3562 | RT_NOREF(aCipher, aPasswordId);
|
---|
3563 | #endif
|
---|
3564 | HRESULT rc = S_OK;
|
---|
3565 |
|
---|
3566 | try
|
---|
3567 | {
|
---|
3568 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
3569 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3570 |
|
---|
3571 | /* Check whether encryption is configured for this medium. */
|
---|
3572 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
3573 | if (it == pBase->m->mapProperties.end())
|
---|
3574 | throw VBOX_E_NOT_SUPPORTED;
|
---|
3575 |
|
---|
3576 | # ifdef VBOX_WITH_EXTPACK
|
---|
3577 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
3578 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
3579 | {
|
---|
3580 | /* Load the plugin */
|
---|
3581 | Utf8Str strPlugin;
|
---|
3582 | rc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
3583 | if (SUCCEEDED(rc))
|
---|
3584 | {
|
---|
3585 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
3586 | if (RT_FAILURE(vrc))
|
---|
3587 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3588 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
3589 | i_vdError(vrc).c_str());
|
---|
3590 | }
|
---|
3591 | else
|
---|
3592 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3593 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
3594 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
3595 | }
|
---|
3596 | else
|
---|
3597 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3598 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
3599 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
3600 |
|
---|
3601 | PVDISK pDisk = NULL;
|
---|
3602 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
3603 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
3604 |
|
---|
3605 | Medium::CryptoFilterSettings CryptoSettings;
|
---|
3606 |
|
---|
3607 | i_taskEncryptSettingsSetup(&CryptoSettings, NULL, it->second.c_str(), NULL, false /* fCreateKeyStore */);
|
---|
3608 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ | VD_FILTER_FLAGS_INFO, CryptoSettings.vdFilterIfaces);
|
---|
3609 | if (RT_FAILURE(vrc))
|
---|
3610 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3611 | tr("Failed to load the encryption filter: %s"),
|
---|
3612 | i_vdError(vrc).c_str());
|
---|
3613 |
|
---|
3614 | it = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
3615 | if (it == pBase->m->mapProperties.end())
|
---|
3616 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3617 | tr("Image is configured for encryption but doesn't has a KeyId set"));
|
---|
3618 |
|
---|
3619 | aPasswordId = it->second.c_str();
|
---|
3620 | aCipher = CryptoSettings.pszCipherReturned;
|
---|
3621 | RTStrFree(CryptoSettings.pszCipherReturned);
|
---|
3622 |
|
---|
3623 | VDDestroy(pDisk);
|
---|
3624 | # else
|
---|
3625 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3626 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
3627 | # endif
|
---|
3628 | }
|
---|
3629 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3630 |
|
---|
3631 | return rc;
|
---|
3632 | }
|
---|
3633 |
|
---|
3634 | HRESULT Medium::checkEncryptionPassword(const com::Utf8Str &aPassword)
|
---|
3635 | {
|
---|
3636 | HRESULT rc = S_OK;
|
---|
3637 |
|
---|
3638 | try
|
---|
3639 | {
|
---|
3640 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
3641 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3642 |
|
---|
3643 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
3644 | if (it == pBase->m->mapProperties.end())
|
---|
3645 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3646 | tr("The image is not configured for encryption"));
|
---|
3647 |
|
---|
3648 | if (aPassword.isEmpty())
|
---|
3649 | throw setError(E_INVALIDARG,
|
---|
3650 | tr("The given password must not be empty"));
|
---|
3651 |
|
---|
3652 | # ifdef VBOX_WITH_EXTPACK
|
---|
3653 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
3654 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
3655 | {
|
---|
3656 | /* Load the plugin */
|
---|
3657 | Utf8Str strPlugin;
|
---|
3658 | rc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
3659 | if (SUCCEEDED(rc))
|
---|
3660 | {
|
---|
3661 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
3662 | if (RT_FAILURE(vrc))
|
---|
3663 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3664 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
3665 | i_vdError(vrc).c_str());
|
---|
3666 | }
|
---|
3667 | else
|
---|
3668 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3669 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
3670 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
3671 | }
|
---|
3672 | else
|
---|
3673 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3674 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
3675 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
3676 |
|
---|
3677 | PVDISK pDisk = NULL;
|
---|
3678 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
3679 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
3680 |
|
---|
3681 | Medium::CryptoFilterSettings CryptoSettings;
|
---|
3682 |
|
---|
3683 | i_taskEncryptSettingsSetup(&CryptoSettings, NULL, it->second.c_str(), aPassword.c_str(),
|
---|
3684 | false /* fCreateKeyStore */);
|
---|
3685 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ, CryptoSettings.vdFilterIfaces);
|
---|
3686 | if (vrc == VERR_VD_PASSWORD_INCORRECT)
|
---|
3687 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
3688 | tr("The given password is incorrect"));
|
---|
3689 | else if (RT_FAILURE(vrc))
|
---|
3690 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3691 | tr("Failed to load the encryption filter: %s"),
|
---|
3692 | i_vdError(vrc).c_str());
|
---|
3693 |
|
---|
3694 | VDDestroy(pDisk);
|
---|
3695 | # else
|
---|
3696 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3697 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
3698 | # endif
|
---|
3699 | }
|
---|
3700 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3701 |
|
---|
3702 | return rc;
|
---|
3703 | }
|
---|
3704 |
|
---|
3705 | ////////////////////////////////////////////////////////////////////////////////
|
---|
3706 | //
|
---|
3707 | // Medium public internal methods
|
---|
3708 | //
|
---|
3709 | ////////////////////////////////////////////////////////////////////////////////
|
---|
3710 |
|
---|
3711 | /**
|
---|
3712 | * Internal method to return the medium's parent medium. Must have caller + locking!
|
---|
3713 | * @return
|
---|
3714 | */
|
---|
3715 | const ComObjPtr<Medium>& Medium::i_getParent() const
|
---|
3716 | {
|
---|
3717 | return m->pParent;
|
---|
3718 | }
|
---|
3719 |
|
---|
3720 | /**
|
---|
3721 | * Internal method to return the medium's list of child media. Must have caller + locking!
|
---|
3722 | * @return
|
---|
3723 | */
|
---|
3724 | const MediaList& Medium::i_getChildren() const
|
---|
3725 | {
|
---|
3726 | return m->llChildren;
|
---|
3727 | }
|
---|
3728 |
|
---|
3729 | /**
|
---|
3730 | * Internal method to return the medium's GUID. Must have caller + locking!
|
---|
3731 | * @return
|
---|
3732 | */
|
---|
3733 | const Guid& Medium::i_getId() const
|
---|
3734 | {
|
---|
3735 | return m->id;
|
---|
3736 | }
|
---|
3737 |
|
---|
3738 | /**
|
---|
3739 | * Internal method to return the medium's state. Must have caller + locking!
|
---|
3740 | * @return
|
---|
3741 | */
|
---|
3742 | MediumState_T Medium::i_getState() const
|
---|
3743 | {
|
---|
3744 | return m->state;
|
---|
3745 | }
|
---|
3746 |
|
---|
3747 | /**
|
---|
3748 | * Internal method to return the medium's variant. Must have caller + locking!
|
---|
3749 | * @return
|
---|
3750 | */
|
---|
3751 | MediumVariant_T Medium::i_getVariant() const
|
---|
3752 | {
|
---|
3753 | return m->variant;
|
---|
3754 | }
|
---|
3755 |
|
---|
3756 | /**
|
---|
3757 | * Internal method which returns true if this medium represents a host drive.
|
---|
3758 | * @return
|
---|
3759 | */
|
---|
3760 | bool Medium::i_isHostDrive() const
|
---|
3761 | {
|
---|
3762 | return m->hostDrive;
|
---|
3763 | }
|
---|
3764 |
|
---|
3765 | /**
|
---|
3766 | * Internal method to return the medium's full location. Must have caller + locking!
|
---|
3767 | * @return
|
---|
3768 | */
|
---|
3769 | const Utf8Str& Medium::i_getLocationFull() const
|
---|
3770 | {
|
---|
3771 | return m->strLocationFull;
|
---|
3772 | }
|
---|
3773 |
|
---|
3774 | /**
|
---|
3775 | * Internal method to return the medium's format string. Must have caller + locking!
|
---|
3776 | * @return
|
---|
3777 | */
|
---|
3778 | const Utf8Str& Medium::i_getFormat() const
|
---|
3779 | {
|
---|
3780 | return m->strFormat;
|
---|
3781 | }
|
---|
3782 |
|
---|
3783 | /**
|
---|
3784 | * Internal method to return the medium's format object. Must have caller + locking!
|
---|
3785 | * @return
|
---|
3786 | */
|
---|
3787 | const ComObjPtr<MediumFormat>& Medium::i_getMediumFormat() const
|
---|
3788 | {
|
---|
3789 | return m->formatObj;
|
---|
3790 | }
|
---|
3791 |
|
---|
3792 | /**
|
---|
3793 | * Internal method that returns true if the medium is represented by a file on the host disk
|
---|
3794 | * (and not iSCSI or something).
|
---|
3795 | * @return
|
---|
3796 | */
|
---|
3797 | bool Medium::i_isMediumFormatFile() const
|
---|
3798 | {
|
---|
3799 | if ( m->formatObj
|
---|
3800 | && (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
3801 | )
|
---|
3802 | return true;
|
---|
3803 | return false;
|
---|
3804 | }
|
---|
3805 |
|
---|
3806 | /**
|
---|
3807 | * Internal method to return the medium's size. Must have caller + locking!
|
---|
3808 | * @return
|
---|
3809 | */
|
---|
3810 | uint64_t Medium::i_getSize() const
|
---|
3811 | {
|
---|
3812 | return m->size;
|
---|
3813 | }
|
---|
3814 |
|
---|
3815 | /**
|
---|
3816 | * Returns the medium device type. Must have caller + locking!
|
---|
3817 | * @return
|
---|
3818 | */
|
---|
3819 | DeviceType_T Medium::i_getDeviceType() const
|
---|
3820 | {
|
---|
3821 | return m->devType;
|
---|
3822 | }
|
---|
3823 |
|
---|
3824 | /**
|
---|
3825 | * Returns the medium type. Must have caller + locking!
|
---|
3826 | * @return
|
---|
3827 | */
|
---|
3828 | MediumType_T Medium::i_getType() const
|
---|
3829 | {
|
---|
3830 | return m->type;
|
---|
3831 | }
|
---|
3832 |
|
---|
3833 | /**
|
---|
3834 | * Returns a short version of the location attribute.
|
---|
3835 | *
|
---|
3836 | * @note Must be called from under this object's read or write lock.
|
---|
3837 | */
|
---|
3838 | Utf8Str Medium::i_getName()
|
---|
3839 | {
|
---|
3840 | Utf8Str name = RTPathFilename(m->strLocationFull.c_str());
|
---|
3841 | return name;
|
---|
3842 | }
|
---|
3843 |
|
---|
3844 | /**
|
---|
3845 | * This adds the given UUID to the list of media registries in which this
|
---|
3846 | * medium should be registered. The UUID can either be a machine UUID,
|
---|
3847 | * to add a machine registry, or the global registry UUID as returned by
|
---|
3848 | * VirtualBox::getGlobalRegistryId().
|
---|
3849 | *
|
---|
3850 | * Note that for hard disks, this method does nothing if the medium is
|
---|
3851 | * already in another registry to avoid having hard disks in more than
|
---|
3852 | * one registry, which causes trouble with keeping diff images in sync.
|
---|
3853 | * See getFirstRegistryMachineId() for details.
|
---|
3854 | *
|
---|
3855 | * @param id
|
---|
3856 | * @return true if the registry was added; false if the given id was already on the list.
|
---|
3857 | */
|
---|
3858 | bool Medium::i_addRegistry(const Guid& id)
|
---|
3859 | {
|
---|
3860 | AutoCaller autoCaller(this);
|
---|
3861 | if (FAILED(autoCaller.rc()))
|
---|
3862 | return false;
|
---|
3863 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3864 |
|
---|
3865 | bool fAdd = true;
|
---|
3866 |
|
---|
3867 | // hard disks cannot be in more than one registry
|
---|
3868 | if ( m->devType == DeviceType_HardDisk
|
---|
3869 | && m->llRegistryIDs.size() > 0)
|
---|
3870 | fAdd = false;
|
---|
3871 |
|
---|
3872 | // no need to add the UUID twice
|
---|
3873 | if (fAdd)
|
---|
3874 | {
|
---|
3875 | for (GuidList::const_iterator it = m->llRegistryIDs.begin();
|
---|
3876 | it != m->llRegistryIDs.end();
|
---|
3877 | ++it)
|
---|
3878 | {
|
---|
3879 | if ((*it) == id)
|
---|
3880 | {
|
---|
3881 | fAdd = false;
|
---|
3882 | break;
|
---|
3883 | }
|
---|
3884 | }
|
---|
3885 | }
|
---|
3886 |
|
---|
3887 | if (fAdd)
|
---|
3888 | m->llRegistryIDs.push_back(id);
|
---|
3889 |
|
---|
3890 | return fAdd;
|
---|
3891 | }
|
---|
3892 |
|
---|
3893 | /**
|
---|
3894 | * This adds the given UUID to the list of media registries in which this
|
---|
3895 | * medium should be registered. The UUID can either be a machine UUID,
|
---|
3896 | * to add a machine registry, or the global registry UUID as returned by
|
---|
3897 | * VirtualBox::getGlobalRegistryId(). This recurses over all children.
|
---|
3898 | *
|
---|
3899 | * Note that for hard disks, this method does nothing if the medium is
|
---|
3900 | * already in another registry to avoid having hard disks in more than
|
---|
3901 | * one registry, which causes trouble with keeping diff images in sync.
|
---|
3902 | * See getFirstRegistryMachineId() for details.
|
---|
3903 | *
|
---|
3904 | * @note the caller must hold the media tree lock for reading.
|
---|
3905 | *
|
---|
3906 | * @param id
|
---|
3907 | * @return true if the registry was added; false if the given id was already on the list.
|
---|
3908 | */
|
---|
3909 | bool Medium::i_addRegistryRecursive(const Guid &id)
|
---|
3910 | {
|
---|
3911 | AutoCaller autoCaller(this);
|
---|
3912 | if (FAILED(autoCaller.rc()))
|
---|
3913 | return false;
|
---|
3914 |
|
---|
3915 | bool fAdd = i_addRegistry(id);
|
---|
3916 |
|
---|
3917 | // protected by the medium tree lock held by our original caller
|
---|
3918 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
3919 | it != i_getChildren().end();
|
---|
3920 | ++it)
|
---|
3921 | {
|
---|
3922 | Medium *pChild = *it;
|
---|
3923 | fAdd |= pChild->i_addRegistryRecursive(id);
|
---|
3924 | }
|
---|
3925 |
|
---|
3926 | return fAdd;
|
---|
3927 | }
|
---|
3928 |
|
---|
3929 | /**
|
---|
3930 | * Removes the given UUID from the list of media registry UUIDs of this medium.
|
---|
3931 | *
|
---|
3932 | * @param id
|
---|
3933 | * @return true if the UUID was found or false if not.
|
---|
3934 | */
|
---|
3935 | bool Medium::i_removeRegistry(const Guid &id)
|
---|
3936 | {
|
---|
3937 | AutoCaller autoCaller(this);
|
---|
3938 | if (FAILED(autoCaller.rc()))
|
---|
3939 | return false;
|
---|
3940 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3941 |
|
---|
3942 | bool fRemove = false;
|
---|
3943 |
|
---|
3944 | /// @todo r=klaus eliminate this code, replace it by using find.
|
---|
3945 | for (GuidList::iterator it = m->llRegistryIDs.begin();
|
---|
3946 | it != m->llRegistryIDs.end();
|
---|
3947 | ++it)
|
---|
3948 | {
|
---|
3949 | if ((*it) == id)
|
---|
3950 | {
|
---|
3951 | // getting away with this as the iterator isn't used after
|
---|
3952 | m->llRegistryIDs.erase(it);
|
---|
3953 | fRemove = true;
|
---|
3954 | break;
|
---|
3955 | }
|
---|
3956 | }
|
---|
3957 |
|
---|
3958 | return fRemove;
|
---|
3959 | }
|
---|
3960 |
|
---|
3961 | /**
|
---|
3962 | * Removes the given UUID from the list of media registry UUIDs, for this
|
---|
3963 | * medium and all its children recursively.
|
---|
3964 | *
|
---|
3965 | * @note the caller must hold the media tree lock for reading.
|
---|
3966 | *
|
---|
3967 | * @param id
|
---|
3968 | * @return true if the UUID was found or false if not.
|
---|
3969 | */
|
---|
3970 | bool Medium::i_removeRegistryRecursive(const Guid &id)
|
---|
3971 | {
|
---|
3972 | AutoCaller autoCaller(this);
|
---|
3973 | if (FAILED(autoCaller.rc()))
|
---|
3974 | return false;
|
---|
3975 |
|
---|
3976 | bool fRemove = i_removeRegistry(id);
|
---|
3977 |
|
---|
3978 | // protected by the medium tree lock held by our original caller
|
---|
3979 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
3980 | it != i_getChildren().end();
|
---|
3981 | ++it)
|
---|
3982 | {
|
---|
3983 | Medium *pChild = *it;
|
---|
3984 | fRemove |= pChild->i_removeRegistryRecursive(id);
|
---|
3985 | }
|
---|
3986 |
|
---|
3987 | return fRemove;
|
---|
3988 | }
|
---|
3989 |
|
---|
3990 | /**
|
---|
3991 | * Returns true if id is in the list of media registries for this medium.
|
---|
3992 | *
|
---|
3993 | * Must have caller + read locking!
|
---|
3994 | *
|
---|
3995 | * @param id
|
---|
3996 | * @return
|
---|
3997 | */
|
---|
3998 | bool Medium::i_isInRegistry(const Guid &id)
|
---|
3999 | {
|
---|
4000 | /// @todo r=klaus eliminate this code, replace it by using find.
|
---|
4001 | for (GuidList::const_iterator it = m->llRegistryIDs.begin();
|
---|
4002 | it != m->llRegistryIDs.end();
|
---|
4003 | ++it)
|
---|
4004 | {
|
---|
4005 | if (*it == id)
|
---|
4006 | return true;
|
---|
4007 | }
|
---|
4008 |
|
---|
4009 | return false;
|
---|
4010 | }
|
---|
4011 |
|
---|
4012 | /**
|
---|
4013 | * Internal method to return the medium's first registry machine (i.e. the machine in whose
|
---|
4014 | * machine XML this medium is listed).
|
---|
4015 | *
|
---|
4016 | * Every attached medium must now (4.0) reside in at least one media registry, which is identified
|
---|
4017 | * by a UUID. This is either a machine UUID if the machine is from 4.0 or newer, in which case
|
---|
4018 | * machines have their own media registries, or it is the pseudo-UUID of the VirtualBox
|
---|
4019 | * object if the machine is old and still needs the global registry in VirtualBox.xml.
|
---|
4020 | *
|
---|
4021 | * By definition, hard disks may only be in one media registry, in which all its children
|
---|
4022 | * will be stored as well. Otherwise we run into problems with having keep multiple registries
|
---|
4023 | * in sync. (This is the "cloned VM" case in which VM1 may link to the disks of VM2; in this
|
---|
4024 | * case, only VM2's registry is used for the disk in question.)
|
---|
4025 | *
|
---|
4026 | * If there is no medium registry, particularly if the medium has not been attached yet, this
|
---|
4027 | * does not modify uuid and returns false.
|
---|
4028 | *
|
---|
4029 | * ISOs and RAWs, by contrast, can be in more than one repository to make things easier for
|
---|
4030 | * the user.
|
---|
4031 | *
|
---|
4032 | * Must have caller + locking!
|
---|
4033 | *
|
---|
4034 | * @param uuid Receives first registry machine UUID, if available.
|
---|
4035 | * @return true if uuid was set.
|
---|
4036 | */
|
---|
4037 | bool Medium::i_getFirstRegistryMachineId(Guid &uuid) const
|
---|
4038 | {
|
---|
4039 | if (m->llRegistryIDs.size())
|
---|
4040 | {
|
---|
4041 | uuid = m->llRegistryIDs.front();
|
---|
4042 | return true;
|
---|
4043 | }
|
---|
4044 | return false;
|
---|
4045 | }
|
---|
4046 |
|
---|
4047 | /**
|
---|
4048 | * Marks all the registries in which this medium is registered as modified.
|
---|
4049 | */
|
---|
4050 | void Medium::i_markRegistriesModified()
|
---|
4051 | {
|
---|
4052 | AutoCaller autoCaller(this);
|
---|
4053 | if (FAILED(autoCaller.rc())) return;
|
---|
4054 |
|
---|
4055 | // Get local copy, as keeping the lock over VirtualBox::markRegistryModified
|
---|
4056 | // causes trouble with the lock order
|
---|
4057 | GuidList llRegistryIDs;
|
---|
4058 | {
|
---|
4059 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4060 | llRegistryIDs = m->llRegistryIDs;
|
---|
4061 | }
|
---|
4062 |
|
---|
4063 | autoCaller.release();
|
---|
4064 |
|
---|
4065 | /* Save the error information now, the implicit restore when this goes
|
---|
4066 | * out of scope will throw away spurious additional errors created below. */
|
---|
4067 | ErrorInfoKeeper eik;
|
---|
4068 | for (GuidList::const_iterator it = llRegistryIDs.begin();
|
---|
4069 | it != llRegistryIDs.end();
|
---|
4070 | ++it)
|
---|
4071 | {
|
---|
4072 | m->pVirtualBox->i_markRegistryModified(*it);
|
---|
4073 | }
|
---|
4074 | }
|
---|
4075 |
|
---|
4076 | /**
|
---|
4077 | * Adds the given machine and optionally the snapshot to the list of the objects
|
---|
4078 | * this medium is attached to.
|
---|
4079 | *
|
---|
4080 | * @param aMachineId Machine ID.
|
---|
4081 | * @param aSnapshotId Snapshot ID; when non-empty, adds a snapshot attachment.
|
---|
4082 | */
|
---|
4083 | HRESULT Medium::i_addBackReference(const Guid &aMachineId,
|
---|
4084 | const Guid &aSnapshotId /*= Guid::Empty*/)
|
---|
4085 | {
|
---|
4086 | AssertReturn(aMachineId.isValid(), E_FAIL);
|
---|
4087 |
|
---|
4088 | LogFlowThisFunc(("ENTER, aMachineId: {%RTuuid}, aSnapshotId: {%RTuuid}\n", aMachineId.raw(), aSnapshotId.raw()));
|
---|
4089 |
|
---|
4090 | AutoCaller autoCaller(this);
|
---|
4091 | AssertComRCReturnRC(autoCaller.rc());
|
---|
4092 |
|
---|
4093 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4094 |
|
---|
4095 | switch (m->state)
|
---|
4096 | {
|
---|
4097 | case MediumState_Created:
|
---|
4098 | case MediumState_Inaccessible:
|
---|
4099 | case MediumState_LockedRead:
|
---|
4100 | case MediumState_LockedWrite:
|
---|
4101 | break;
|
---|
4102 |
|
---|
4103 | default:
|
---|
4104 | return i_setStateError();
|
---|
4105 | }
|
---|
4106 |
|
---|
4107 | if (m->numCreateDiffTasks > 0)
|
---|
4108 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4109 | tr("Cannot attach medium '%s' {%RTuuid}: %u differencing child media are being created"),
|
---|
4110 | m->strLocationFull.c_str(),
|
---|
4111 | m->id.raw(),
|
---|
4112 | m->numCreateDiffTasks);
|
---|
4113 |
|
---|
4114 | BackRefList::iterator it = std::find_if(m->backRefs.begin(),
|
---|
4115 | m->backRefs.end(),
|
---|
4116 | BackRef::EqualsTo(aMachineId));
|
---|
4117 | if (it == m->backRefs.end())
|
---|
4118 | {
|
---|
4119 | BackRef ref(aMachineId, aSnapshotId);
|
---|
4120 | m->backRefs.push_back(ref);
|
---|
4121 |
|
---|
4122 | return S_OK;
|
---|
4123 | }
|
---|
4124 |
|
---|
4125 | // if the caller has not supplied a snapshot ID, then we're attaching
|
---|
4126 | // to a machine a medium which represents the machine's current state,
|
---|
4127 | // so set the flag
|
---|
4128 |
|
---|
4129 | if (aSnapshotId.isZero())
|
---|
4130 | {
|
---|
4131 | /* sanity: no duplicate attachments */
|
---|
4132 | if (it->fInCurState)
|
---|
4133 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4134 | tr("Cannot attach medium '%s' {%RTuuid}: medium is already associated with the current state of machine uuid {%RTuuid}!"),
|
---|
4135 | m->strLocationFull.c_str(),
|
---|
4136 | m->id.raw(),
|
---|
4137 | aMachineId.raw());
|
---|
4138 | it->fInCurState = true;
|
---|
4139 |
|
---|
4140 | return S_OK;
|
---|
4141 | }
|
---|
4142 |
|
---|
4143 | // otherwise: a snapshot medium is being attached
|
---|
4144 |
|
---|
4145 | /* sanity: no duplicate attachments */
|
---|
4146 | for (GuidList::const_iterator jt = it->llSnapshotIds.begin();
|
---|
4147 | jt != it->llSnapshotIds.end();
|
---|
4148 | ++jt)
|
---|
4149 | {
|
---|
4150 | const Guid &idOldSnapshot = *jt;
|
---|
4151 |
|
---|
4152 | if (idOldSnapshot == aSnapshotId)
|
---|
4153 | {
|
---|
4154 | #ifdef DEBUG
|
---|
4155 | i_dumpBackRefs();
|
---|
4156 | #endif
|
---|
4157 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4158 | tr("Cannot attach medium '%s' {%RTuuid} from snapshot '%RTuuid': medium is already in use by this snapshot!"),
|
---|
4159 | m->strLocationFull.c_str(),
|
---|
4160 | m->id.raw(),
|
---|
4161 | aSnapshotId.raw());
|
---|
4162 | }
|
---|
4163 | }
|
---|
4164 |
|
---|
4165 | it->llSnapshotIds.push_back(aSnapshotId);
|
---|
4166 | // Do not touch fInCurState, as the image may be attached to the current
|
---|
4167 | // state *and* a snapshot, otherwise we lose the current state association!
|
---|
4168 |
|
---|
4169 | LogFlowThisFuncLeave();
|
---|
4170 |
|
---|
4171 | return S_OK;
|
---|
4172 | }
|
---|
4173 |
|
---|
4174 | /**
|
---|
4175 | * Removes the given machine and optionally the snapshot from the list of the
|
---|
4176 | * objects this medium is attached to.
|
---|
4177 | *
|
---|
4178 | * @param aMachineId Machine ID.
|
---|
4179 | * @param aSnapshotId Snapshot ID; when non-empty, removes the snapshot
|
---|
4180 | * attachment.
|
---|
4181 | */
|
---|
4182 | HRESULT Medium::i_removeBackReference(const Guid &aMachineId,
|
---|
4183 | const Guid &aSnapshotId /*= Guid::Empty*/)
|
---|
4184 | {
|
---|
4185 | AssertReturn(aMachineId.isValid(), E_FAIL);
|
---|
4186 |
|
---|
4187 | AutoCaller autoCaller(this);
|
---|
4188 | AssertComRCReturnRC(autoCaller.rc());
|
---|
4189 |
|
---|
4190 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4191 |
|
---|
4192 | BackRefList::iterator it =
|
---|
4193 | std::find_if(m->backRefs.begin(), m->backRefs.end(),
|
---|
4194 | BackRef::EqualsTo(aMachineId));
|
---|
4195 | AssertReturn(it != m->backRefs.end(), E_FAIL);
|
---|
4196 |
|
---|
4197 | if (aSnapshotId.isZero())
|
---|
4198 | {
|
---|
4199 | /* remove the current state attachment */
|
---|
4200 | it->fInCurState = false;
|
---|
4201 | }
|
---|
4202 | else
|
---|
4203 | {
|
---|
4204 | /* remove the snapshot attachment */
|
---|
4205 | GuidList::iterator jt = std::find(it->llSnapshotIds.begin(),
|
---|
4206 | it->llSnapshotIds.end(),
|
---|
4207 | aSnapshotId);
|
---|
4208 |
|
---|
4209 | AssertReturn(jt != it->llSnapshotIds.end(), E_FAIL);
|
---|
4210 | it->llSnapshotIds.erase(jt);
|
---|
4211 | }
|
---|
4212 |
|
---|
4213 | /* if the backref becomes empty, remove it */
|
---|
4214 | if (it->fInCurState == false && it->llSnapshotIds.size() == 0)
|
---|
4215 | m->backRefs.erase(it);
|
---|
4216 |
|
---|
4217 | return S_OK;
|
---|
4218 | }
|
---|
4219 |
|
---|
4220 | /**
|
---|
4221 | * Internal method to return the medium's list of backrefs. Must have caller + locking!
|
---|
4222 | * @return
|
---|
4223 | */
|
---|
4224 | const Guid* Medium::i_getFirstMachineBackrefId() const
|
---|
4225 | {
|
---|
4226 | if (!m->backRefs.size())
|
---|
4227 | return NULL;
|
---|
4228 |
|
---|
4229 | return &m->backRefs.front().machineId;
|
---|
4230 | }
|
---|
4231 |
|
---|
4232 | /**
|
---|
4233 | * Internal method which returns a machine that either this medium or one of its children
|
---|
4234 | * is attached to. This is used for finding a replacement media registry when an existing
|
---|
4235 | * media registry is about to be deleted in VirtualBox::unregisterMachine().
|
---|
4236 | *
|
---|
4237 | * Must have caller + locking, *and* caller must hold the media tree lock!
|
---|
4238 | * @return
|
---|
4239 | */
|
---|
4240 | const Guid* Medium::i_getAnyMachineBackref() const
|
---|
4241 | {
|
---|
4242 | if (m->backRefs.size())
|
---|
4243 | return &m->backRefs.front().machineId;
|
---|
4244 |
|
---|
4245 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
4246 | it != i_getChildren().end();
|
---|
4247 | ++it)
|
---|
4248 | {
|
---|
4249 | Medium *pChild = *it;
|
---|
4250 | // recurse for this child
|
---|
4251 | const Guid* puuid;
|
---|
4252 | if ((puuid = pChild->i_getAnyMachineBackref()))
|
---|
4253 | return puuid;
|
---|
4254 | }
|
---|
4255 |
|
---|
4256 | return NULL;
|
---|
4257 | }
|
---|
4258 |
|
---|
4259 | const Guid* Medium::i_getFirstMachineBackrefSnapshotId() const
|
---|
4260 | {
|
---|
4261 | if (!m->backRefs.size())
|
---|
4262 | return NULL;
|
---|
4263 |
|
---|
4264 | const BackRef &ref = m->backRefs.front();
|
---|
4265 | if (ref.llSnapshotIds.empty())
|
---|
4266 | return NULL;
|
---|
4267 |
|
---|
4268 | return &ref.llSnapshotIds.front();
|
---|
4269 | }
|
---|
4270 |
|
---|
4271 | size_t Medium::i_getMachineBackRefCount() const
|
---|
4272 | {
|
---|
4273 | return m->backRefs.size();
|
---|
4274 | }
|
---|
4275 |
|
---|
4276 | #ifdef DEBUG
|
---|
4277 | /**
|
---|
4278 | * Debugging helper that gets called after VirtualBox initialization that writes all
|
---|
4279 | * machine backreferences to the debug log.
|
---|
4280 | */
|
---|
4281 | void Medium::i_dumpBackRefs()
|
---|
4282 | {
|
---|
4283 | AutoCaller autoCaller(this);
|
---|
4284 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4285 |
|
---|
4286 | LogFlowThisFunc(("Dumping backrefs for medium '%s':\n", m->strLocationFull.c_str()));
|
---|
4287 |
|
---|
4288 | for (BackRefList::iterator it2 = m->backRefs.begin();
|
---|
4289 | it2 != m->backRefs.end();
|
---|
4290 | ++it2)
|
---|
4291 | {
|
---|
4292 | const BackRef &ref = *it2;
|
---|
4293 | LogFlowThisFunc((" Backref from machine {%RTuuid} (fInCurState: %d)\n", ref.machineId.raw(), ref.fInCurState));
|
---|
4294 |
|
---|
4295 | for (GuidList::const_iterator jt2 = it2->llSnapshotIds.begin();
|
---|
4296 | jt2 != it2->llSnapshotIds.end();
|
---|
4297 | ++jt2)
|
---|
4298 | {
|
---|
4299 | const Guid &id = *jt2;
|
---|
4300 | LogFlowThisFunc((" Backref from snapshot {%RTuuid}\n", id.raw()));
|
---|
4301 | }
|
---|
4302 | }
|
---|
4303 | }
|
---|
4304 | #endif
|
---|
4305 |
|
---|
4306 | /**
|
---|
4307 | * Checks if the given change of \a aOldPath to \a aNewPath affects the location
|
---|
4308 | * of this media and updates it if necessary to reflect the new location.
|
---|
4309 | *
|
---|
4310 | * @param strOldPath Old path (full).
|
---|
4311 | * @param strNewPath New path (full).
|
---|
4312 | *
|
---|
4313 | * @note Locks this object for writing.
|
---|
4314 | */
|
---|
4315 | HRESULT Medium::i_updatePath(const Utf8Str &strOldPath, const Utf8Str &strNewPath)
|
---|
4316 | {
|
---|
4317 | AssertReturn(!strOldPath.isEmpty(), E_FAIL);
|
---|
4318 | AssertReturn(!strNewPath.isEmpty(), E_FAIL);
|
---|
4319 |
|
---|
4320 | AutoCaller autoCaller(this);
|
---|
4321 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4322 |
|
---|
4323 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4324 |
|
---|
4325 | LogFlowThisFunc(("locationFull.before='%s'\n", m->strLocationFull.c_str()));
|
---|
4326 |
|
---|
4327 | const char *pcszMediumPath = m->strLocationFull.c_str();
|
---|
4328 |
|
---|
4329 | if (RTPathStartsWith(pcszMediumPath, strOldPath.c_str()))
|
---|
4330 | {
|
---|
4331 | Utf8Str newPath(strNewPath);
|
---|
4332 | newPath.append(pcszMediumPath + strOldPath.length());
|
---|
4333 | unconst(m->strLocationFull) = newPath;
|
---|
4334 |
|
---|
4335 | LogFlowThisFunc(("locationFull.after='%s'\n", m->strLocationFull.c_str()));
|
---|
4336 | // we changed something
|
---|
4337 | return S_OK;
|
---|
4338 | }
|
---|
4339 |
|
---|
4340 | // no change was necessary, signal error which the caller needs to interpret
|
---|
4341 | return VBOX_E_FILE_ERROR;
|
---|
4342 | }
|
---|
4343 |
|
---|
4344 | /**
|
---|
4345 | * Returns the base medium of the media chain this medium is part of.
|
---|
4346 | *
|
---|
4347 | * The base medium is found by walking up the parent-child relationship axis.
|
---|
4348 | * If the medium doesn't have a parent (i.e. it's a base medium), it
|
---|
4349 | * returns itself in response to this method.
|
---|
4350 | *
|
---|
4351 | * @param aLevel Where to store the number of ancestors of this medium
|
---|
4352 | * (zero for the base), may be @c NULL.
|
---|
4353 | *
|
---|
4354 | * @note Locks medium tree for reading.
|
---|
4355 | */
|
---|
4356 | ComObjPtr<Medium> Medium::i_getBase(uint32_t *aLevel /*= NULL*/)
|
---|
4357 | {
|
---|
4358 | ComObjPtr<Medium> pBase;
|
---|
4359 |
|
---|
4360 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
4361 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
4362 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
4363 | if (!pVirtualBox)
|
---|
4364 | return pBase;
|
---|
4365 |
|
---|
4366 | /* we access m->pParent */
|
---|
4367 | AutoReadLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4368 |
|
---|
4369 | AutoCaller autoCaller(this);
|
---|
4370 | AssertReturn(autoCaller.isOk(), pBase);
|
---|
4371 |
|
---|
4372 | pBase = this;
|
---|
4373 | uint32_t level = 0;
|
---|
4374 |
|
---|
4375 | if (m->pParent)
|
---|
4376 | {
|
---|
4377 | for (;;)
|
---|
4378 | {
|
---|
4379 | AutoCaller baseCaller(pBase);
|
---|
4380 | AssertReturn(baseCaller.isOk(), pBase);
|
---|
4381 |
|
---|
4382 | if (pBase->m->pParent.isNull())
|
---|
4383 | break;
|
---|
4384 |
|
---|
4385 | pBase = pBase->m->pParent;
|
---|
4386 | ++level;
|
---|
4387 | }
|
---|
4388 | }
|
---|
4389 |
|
---|
4390 | if (aLevel != NULL)
|
---|
4391 | *aLevel = level;
|
---|
4392 |
|
---|
4393 | return pBase;
|
---|
4394 | }
|
---|
4395 |
|
---|
4396 | /**
|
---|
4397 | * Returns the depth of this medium in the media chain.
|
---|
4398 | *
|
---|
4399 | * @note Locks medium tree for reading.
|
---|
4400 | */
|
---|
4401 | uint32_t Medium::i_getDepth()
|
---|
4402 | {
|
---|
4403 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
4404 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
4405 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
4406 | if (!pVirtualBox)
|
---|
4407 | return 1;
|
---|
4408 |
|
---|
4409 | /* we access m->pParent */
|
---|
4410 | AutoReadLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4411 |
|
---|
4412 | uint32_t cDepth = 0;
|
---|
4413 | ComObjPtr<Medium> pMedium(this);
|
---|
4414 | while (!pMedium.isNull())
|
---|
4415 | {
|
---|
4416 | AutoCaller autoCaller(this);
|
---|
4417 | AssertReturn(autoCaller.isOk(), cDepth + 1);
|
---|
4418 |
|
---|
4419 | pMedium = pMedium->m->pParent;
|
---|
4420 | cDepth++;
|
---|
4421 | }
|
---|
4422 |
|
---|
4423 | return cDepth;
|
---|
4424 | }
|
---|
4425 |
|
---|
4426 | /**
|
---|
4427 | * Returns @c true if this medium cannot be modified because it has
|
---|
4428 | * dependents (children) or is part of the snapshot. Related to the medium
|
---|
4429 | * type and posterity, not to the current media state.
|
---|
4430 | *
|
---|
4431 | * @note Locks this object and medium tree for reading.
|
---|
4432 | */
|
---|
4433 | bool Medium::i_isReadOnly()
|
---|
4434 | {
|
---|
4435 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
4436 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
4437 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
4438 | if (!pVirtualBox)
|
---|
4439 | return false;
|
---|
4440 |
|
---|
4441 | /* we access children */
|
---|
4442 | AutoReadLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4443 |
|
---|
4444 | AutoCaller autoCaller(this);
|
---|
4445 | AssertComRCReturn(autoCaller.rc(), false);
|
---|
4446 |
|
---|
4447 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4448 |
|
---|
4449 | switch (m->type)
|
---|
4450 | {
|
---|
4451 | case MediumType_Normal:
|
---|
4452 | {
|
---|
4453 | if (i_getChildren().size() != 0)
|
---|
4454 | return true;
|
---|
4455 |
|
---|
4456 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
4457 | it != m->backRefs.end(); ++it)
|
---|
4458 | if (it->llSnapshotIds.size() != 0)
|
---|
4459 | return true;
|
---|
4460 |
|
---|
4461 | if (m->variant & MediumVariant_VmdkStreamOptimized)
|
---|
4462 | return true;
|
---|
4463 |
|
---|
4464 | return false;
|
---|
4465 | }
|
---|
4466 | case MediumType_Immutable:
|
---|
4467 | case MediumType_MultiAttach:
|
---|
4468 | return true;
|
---|
4469 | case MediumType_Writethrough:
|
---|
4470 | case MediumType_Shareable:
|
---|
4471 | case MediumType_Readonly: /* explicit readonly media has no diffs */
|
---|
4472 | return false;
|
---|
4473 | default:
|
---|
4474 | break;
|
---|
4475 | }
|
---|
4476 |
|
---|
4477 | AssertFailedReturn(false);
|
---|
4478 | }
|
---|
4479 |
|
---|
4480 | /**
|
---|
4481 | * Internal method to return the medium's size. Must have caller + locking!
|
---|
4482 | * @return
|
---|
4483 | */
|
---|
4484 | void Medium::i_updateId(const Guid &id)
|
---|
4485 | {
|
---|
4486 | unconst(m->id) = id;
|
---|
4487 | }
|
---|
4488 |
|
---|
4489 | /**
|
---|
4490 | * Saves the settings of one medium.
|
---|
4491 | *
|
---|
4492 | * @note Caller MUST take care of the medium tree lock and caller.
|
---|
4493 | *
|
---|
4494 | * @param data Settings struct to be updated.
|
---|
4495 | * @param strHardDiskFolder Folder for which paths should be relative.
|
---|
4496 | */
|
---|
4497 | void Medium::i_saveSettingsOne(settings::Medium &data, const Utf8Str &strHardDiskFolder)
|
---|
4498 | {
|
---|
4499 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4500 |
|
---|
4501 | data.uuid = m->id;
|
---|
4502 |
|
---|
4503 | // make path relative if needed
|
---|
4504 | if ( !strHardDiskFolder.isEmpty()
|
---|
4505 | && RTPathStartsWith(m->strLocationFull.c_str(), strHardDiskFolder.c_str())
|
---|
4506 | )
|
---|
4507 | data.strLocation = m->strLocationFull.substr(strHardDiskFolder.length() + 1);
|
---|
4508 | else
|
---|
4509 | data.strLocation = m->strLocationFull;
|
---|
4510 | data.strFormat = m->strFormat;
|
---|
4511 |
|
---|
4512 | /* optional, only for diffs, default is false */
|
---|
4513 | if (m->pParent)
|
---|
4514 | data.fAutoReset = m->autoReset;
|
---|
4515 | else
|
---|
4516 | data.fAutoReset = false;
|
---|
4517 |
|
---|
4518 | /* optional */
|
---|
4519 | data.strDescription = m->strDescription;
|
---|
4520 |
|
---|
4521 | /* optional properties */
|
---|
4522 | data.properties.clear();
|
---|
4523 |
|
---|
4524 | /* handle iSCSI initiator secrets transparently */
|
---|
4525 | bool fHaveInitiatorSecretEncrypted = false;
|
---|
4526 | Utf8Str strCiphertext;
|
---|
4527 | settings::StringsMap::const_iterator itPln = m->mapProperties.find("InitiatorSecret");
|
---|
4528 | if ( itPln != m->mapProperties.end()
|
---|
4529 | && !itPln->second.isEmpty())
|
---|
4530 | {
|
---|
4531 | /* Encrypt the plain secret. If that does not work (i.e. no or wrong settings key
|
---|
4532 | * specified), just use the encrypted secret (if there is any). */
|
---|
4533 | int rc = m->pVirtualBox->i_encryptSetting(itPln->second, &strCiphertext);
|
---|
4534 | if (RT_SUCCESS(rc))
|
---|
4535 | fHaveInitiatorSecretEncrypted = true;
|
---|
4536 | }
|
---|
4537 | for (settings::StringsMap::const_iterator it = m->mapProperties.begin();
|
---|
4538 | it != m->mapProperties.end();
|
---|
4539 | ++it)
|
---|
4540 | {
|
---|
4541 | /* only save properties that have non-default values */
|
---|
4542 | if (!it->second.isEmpty())
|
---|
4543 | {
|
---|
4544 | const Utf8Str &name = it->first;
|
---|
4545 | const Utf8Str &value = it->second;
|
---|
4546 | /* do NOT store the plain InitiatorSecret */
|
---|
4547 | if ( !fHaveInitiatorSecretEncrypted
|
---|
4548 | || !name.equals("InitiatorSecret"))
|
---|
4549 | data.properties[name] = value;
|
---|
4550 | }
|
---|
4551 | }
|
---|
4552 | if (fHaveInitiatorSecretEncrypted)
|
---|
4553 | data.properties["InitiatorSecretEncrypted"] = strCiphertext;
|
---|
4554 |
|
---|
4555 | /* only for base media */
|
---|
4556 | if (m->pParent.isNull())
|
---|
4557 | data.hdType = m->type;
|
---|
4558 | }
|
---|
4559 |
|
---|
4560 | /**
|
---|
4561 | * Saves medium data by putting it into the provided data structure.
|
---|
4562 | * Recurses over all children to save their settings, too.
|
---|
4563 | *
|
---|
4564 | * @param data Settings struct to be updated.
|
---|
4565 | * @param strHardDiskFolder Folder for which paths should be relative.
|
---|
4566 | *
|
---|
4567 | * @note Locks this object, medium tree and children for reading.
|
---|
4568 | */
|
---|
4569 | HRESULT Medium::i_saveSettings(settings::Medium &data,
|
---|
4570 | const Utf8Str &strHardDiskFolder)
|
---|
4571 | {
|
---|
4572 | /* we access m->pParent */
|
---|
4573 | AutoReadLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4574 |
|
---|
4575 | AutoCaller autoCaller(this);
|
---|
4576 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4577 |
|
---|
4578 | i_saveSettingsOne(data, strHardDiskFolder);
|
---|
4579 |
|
---|
4580 | /* save all children */
|
---|
4581 | settings::MediaList &llSettingsChildren = data.llChildren;
|
---|
4582 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
4583 | it != i_getChildren().end();
|
---|
4584 | ++it)
|
---|
4585 | {
|
---|
4586 | // Use the element straight in the list to reduce both unnecessary
|
---|
4587 | // deep copying (when unwinding the recursion the entire medium
|
---|
4588 | // settings sub-tree is copied) and the stack footprint (the settings
|
---|
4589 | // need almost 1K, and there can be VMs with long image chains.
|
---|
4590 | llSettingsChildren.push_back(settings::Medium::Empty);
|
---|
4591 | HRESULT rc = (*it)->i_saveSettings(llSettingsChildren.back(), strHardDiskFolder);
|
---|
4592 | if (FAILED(rc))
|
---|
4593 | {
|
---|
4594 | llSettingsChildren.pop_back();
|
---|
4595 | return rc;
|
---|
4596 | }
|
---|
4597 | }
|
---|
4598 |
|
---|
4599 | return S_OK;
|
---|
4600 | }
|
---|
4601 |
|
---|
4602 | /**
|
---|
4603 | * Constructs a medium lock list for this medium. The lock is not taken.
|
---|
4604 | *
|
---|
4605 | * @note Caller MUST NOT hold the media tree or medium lock.
|
---|
4606 | *
|
---|
4607 | * @param fFailIfInaccessible If true, this fails with an error if a medium is inaccessible. If false,
|
---|
4608 | * inaccessible media are silently skipped and not locked (i.e. their state remains "Inaccessible");
|
---|
4609 | * this is necessary for a VM's removable media VM startup for which we do not want to fail.
|
---|
4610 | * @param pToLockWrite If not NULL, associate a write lock with this medium object.
|
---|
4611 | * @param fMediumLockWriteAll Whether to associate a write lock to all other media too.
|
---|
4612 | * @param pToBeParent Medium which will become the parent of this medium.
|
---|
4613 | * @param mediumLockList Where to store the resulting list.
|
---|
4614 | */
|
---|
4615 | HRESULT Medium::i_createMediumLockList(bool fFailIfInaccessible,
|
---|
4616 | Medium *pToLockWrite,
|
---|
4617 | bool fMediumLockWriteAll,
|
---|
4618 | Medium *pToBeParent,
|
---|
4619 | MediumLockList &mediumLockList)
|
---|
4620 | {
|
---|
4621 | /** @todo r=klaus this needs to be reworked, as the code below uses
|
---|
4622 | * i_getParent without holding the tree lock, and changing this is
|
---|
4623 | * a significant amount of effort. */
|
---|
4624 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
4625 | Assert(!isWriteLockOnCurrentThread());
|
---|
4626 |
|
---|
4627 | AutoCaller autoCaller(this);
|
---|
4628 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4629 |
|
---|
4630 | HRESULT rc = S_OK;
|
---|
4631 |
|
---|
4632 | /* paranoid sanity checking if the medium has a to-be parent medium */
|
---|
4633 | if (pToBeParent)
|
---|
4634 | {
|
---|
4635 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4636 | ComAssertRet(i_getParent().isNull(), E_FAIL);
|
---|
4637 | ComAssertRet(i_getChildren().size() == 0, E_FAIL);
|
---|
4638 | }
|
---|
4639 |
|
---|
4640 | ErrorInfoKeeper eik;
|
---|
4641 | MultiResult mrc(S_OK);
|
---|
4642 |
|
---|
4643 | ComObjPtr<Medium> pMedium = this;
|
---|
4644 | while (!pMedium.isNull())
|
---|
4645 | {
|
---|
4646 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
4647 |
|
---|
4648 | /* Accessibility check must be first, otherwise locking interferes
|
---|
4649 | * with getting the medium state. Lock lists are not created for
|
---|
4650 | * fun, and thus getting the medium status is no luxury. */
|
---|
4651 | MediumState_T mediumState = pMedium->i_getState();
|
---|
4652 | if (mediumState == MediumState_Inaccessible)
|
---|
4653 | {
|
---|
4654 | alock.release();
|
---|
4655 | rc = pMedium->i_queryInfo(false /* fSetImageId */, false /* fSetParentId */,
|
---|
4656 | autoCaller);
|
---|
4657 | alock.acquire();
|
---|
4658 | if (FAILED(rc)) return rc;
|
---|
4659 |
|
---|
4660 | mediumState = pMedium->i_getState();
|
---|
4661 | if (mediumState == MediumState_Inaccessible)
|
---|
4662 | {
|
---|
4663 | // ignore inaccessible ISO media and silently return S_OK,
|
---|
4664 | // otherwise VM startup (esp. restore) may fail without good reason
|
---|
4665 | if (!fFailIfInaccessible)
|
---|
4666 | return S_OK;
|
---|
4667 |
|
---|
4668 | // otherwise report an error
|
---|
4669 | Bstr error;
|
---|
4670 | rc = pMedium->COMGETTER(LastAccessError)(error.asOutParam());
|
---|
4671 | if (FAILED(rc)) return rc;
|
---|
4672 |
|
---|
4673 | /* collect multiple errors */
|
---|
4674 | eik.restore();
|
---|
4675 | Assert(!error.isEmpty());
|
---|
4676 | mrc = setError(E_FAIL,
|
---|
4677 | "%ls",
|
---|
4678 | error.raw());
|
---|
4679 | // error message will be something like
|
---|
4680 | // "Could not open the medium ... VD: error VERR_FILE_NOT_FOUND opening image file ... (VERR_FILE_NOT_FOUND).
|
---|
4681 | eik.fetch();
|
---|
4682 | }
|
---|
4683 | }
|
---|
4684 |
|
---|
4685 | if (pMedium == pToLockWrite)
|
---|
4686 | mediumLockList.Prepend(pMedium, true);
|
---|
4687 | else
|
---|
4688 | mediumLockList.Prepend(pMedium, fMediumLockWriteAll);
|
---|
4689 |
|
---|
4690 | pMedium = pMedium->i_getParent();
|
---|
4691 | if (pMedium.isNull() && pToBeParent)
|
---|
4692 | {
|
---|
4693 | pMedium = pToBeParent;
|
---|
4694 | pToBeParent = NULL;
|
---|
4695 | }
|
---|
4696 | }
|
---|
4697 |
|
---|
4698 | return mrc;
|
---|
4699 | }
|
---|
4700 |
|
---|
4701 | /**
|
---|
4702 | * Creates a new differencing storage unit using the format of the given target
|
---|
4703 | * medium and the location. Note that @c aTarget must be NotCreated.
|
---|
4704 | *
|
---|
4705 | * The @a aMediumLockList parameter contains the associated medium lock list,
|
---|
4706 | * which must be in locked state. If @a aWait is @c true then the caller is
|
---|
4707 | * responsible for unlocking.
|
---|
4708 | *
|
---|
4709 | * If @a aProgress is not NULL but the object it points to is @c null then a
|
---|
4710 | * new progress object will be created and assigned to @a *aProgress on
|
---|
4711 | * success, otherwise the existing progress object is used. If @a aProgress is
|
---|
4712 | * NULL, then no progress object is created/used at all.
|
---|
4713 | *
|
---|
4714 | * When @a aWait is @c false, this method will create a thread to perform the
|
---|
4715 | * create operation asynchronously and will return immediately. Otherwise, it
|
---|
4716 | * will perform the operation on the calling thread and will not return to the
|
---|
4717 | * caller until the operation is completed. Note that @a aProgress cannot be
|
---|
4718 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
4719 | *
|
---|
4720 | * @param aTarget Target medium.
|
---|
4721 | * @param aVariant Precise medium variant to create.
|
---|
4722 | * @param aMediumLockList List of media which should be locked.
|
---|
4723 | * @param aProgress Where to find/store a Progress object to track
|
---|
4724 | * operation completion.
|
---|
4725 | * @param aWait @c true if this method should block instead of
|
---|
4726 | * creating an asynchronous thread.
|
---|
4727 | *
|
---|
4728 | * @note Locks this object and @a aTarget for writing.
|
---|
4729 | */
|
---|
4730 | HRESULT Medium::i_createDiffStorage(ComObjPtr<Medium> &aTarget,
|
---|
4731 | MediumVariant_T aVariant,
|
---|
4732 | MediumLockList *aMediumLockList,
|
---|
4733 | ComObjPtr<Progress> *aProgress,
|
---|
4734 | bool aWait)
|
---|
4735 | {
|
---|
4736 | AssertReturn(!aTarget.isNull(), E_FAIL);
|
---|
4737 | AssertReturn(aMediumLockList, E_FAIL);
|
---|
4738 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
4739 |
|
---|
4740 | AutoCaller autoCaller(this);
|
---|
4741 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4742 |
|
---|
4743 | AutoCaller targetCaller(aTarget);
|
---|
4744 | if (FAILED(targetCaller.rc())) return targetCaller.rc();
|
---|
4745 |
|
---|
4746 | HRESULT rc = S_OK;
|
---|
4747 | ComObjPtr<Progress> pProgress;
|
---|
4748 | Medium::Task *pTask = NULL;
|
---|
4749 |
|
---|
4750 | try
|
---|
4751 | {
|
---|
4752 | AutoMultiWriteLock2 alock(this, aTarget COMMA_LOCKVAL_SRC_POS);
|
---|
4753 |
|
---|
4754 | ComAssertThrow( m->type != MediumType_Writethrough
|
---|
4755 | && m->type != MediumType_Shareable
|
---|
4756 | && m->type != MediumType_Readonly, E_FAIL);
|
---|
4757 | ComAssertThrow(m->state == MediumState_LockedRead, E_FAIL);
|
---|
4758 |
|
---|
4759 | if (aTarget->m->state != MediumState_NotCreated)
|
---|
4760 | throw aTarget->i_setStateError();
|
---|
4761 |
|
---|
4762 | /* Check that the medium is not attached to the current state of
|
---|
4763 | * any VM referring to it. */
|
---|
4764 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
4765 | it != m->backRefs.end();
|
---|
4766 | ++it)
|
---|
4767 | {
|
---|
4768 | if (it->fInCurState)
|
---|
4769 | {
|
---|
4770 | /* Note: when a VM snapshot is being taken, all normal media
|
---|
4771 | * attached to the VM in the current state will be, as an
|
---|
4772 | * exception, also associated with the snapshot which is about
|
---|
4773 | * to create (see SnapshotMachine::init()) before deassociating
|
---|
4774 | * them from the current state (which takes place only on
|
---|
4775 | * success in Machine::fixupHardDisks()), so that the size of
|
---|
4776 | * snapshotIds will be 1 in this case. The extra condition is
|
---|
4777 | * used to filter out this legal situation. */
|
---|
4778 | if (it->llSnapshotIds.size() == 0)
|
---|
4779 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4780 | 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"),
|
---|
4781 | m->strLocationFull.c_str(), it->machineId.raw());
|
---|
4782 |
|
---|
4783 | Assert(it->llSnapshotIds.size() == 1);
|
---|
4784 | }
|
---|
4785 | }
|
---|
4786 |
|
---|
4787 | if (aProgress != NULL)
|
---|
4788 | {
|
---|
4789 | /* use the existing progress object... */
|
---|
4790 | pProgress = *aProgress;
|
---|
4791 |
|
---|
4792 | /* ...but create a new one if it is null */
|
---|
4793 | if (pProgress.isNull())
|
---|
4794 | {
|
---|
4795 | pProgress.createObject();
|
---|
4796 | rc = pProgress->init(m->pVirtualBox,
|
---|
4797 | static_cast<IMedium*>(this),
|
---|
4798 | BstrFmt(tr("Creating differencing medium storage unit '%s'"),
|
---|
4799 | aTarget->m->strLocationFull.c_str()).raw(),
|
---|
4800 | TRUE /* aCancelable */);
|
---|
4801 | if (FAILED(rc))
|
---|
4802 | throw rc;
|
---|
4803 | }
|
---|
4804 | }
|
---|
4805 |
|
---|
4806 | /* setup task object to carry out the operation sync/async */
|
---|
4807 | pTask = new Medium::CreateDiffTask(this, pProgress, aTarget, aVariant,
|
---|
4808 | aMediumLockList,
|
---|
4809 | aWait /* fKeepMediumLockList */);
|
---|
4810 | rc = pTask->rc();
|
---|
4811 | AssertComRC(rc);
|
---|
4812 | if (FAILED(rc))
|
---|
4813 | throw rc;
|
---|
4814 |
|
---|
4815 | /* register a task (it will deregister itself when done) */
|
---|
4816 | ++m->numCreateDiffTasks;
|
---|
4817 | Assert(m->numCreateDiffTasks != 0); /* overflow? */
|
---|
4818 |
|
---|
4819 | aTarget->m->state = MediumState_Creating;
|
---|
4820 | }
|
---|
4821 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4822 |
|
---|
4823 | if (SUCCEEDED(rc))
|
---|
4824 | {
|
---|
4825 | if (aWait)
|
---|
4826 | {
|
---|
4827 | rc = pTask->runNow();
|
---|
4828 |
|
---|
4829 | delete pTask;
|
---|
4830 | }
|
---|
4831 | else
|
---|
4832 | rc = pTask->createThread();
|
---|
4833 |
|
---|
4834 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
4835 | *aProgress = pProgress;
|
---|
4836 | }
|
---|
4837 | else if (pTask != NULL)
|
---|
4838 | delete pTask;
|
---|
4839 |
|
---|
4840 | return rc;
|
---|
4841 | }
|
---|
4842 |
|
---|
4843 | /**
|
---|
4844 | * Returns a preferred format for differencing media.
|
---|
4845 | */
|
---|
4846 | Utf8Str Medium::i_getPreferredDiffFormat()
|
---|
4847 | {
|
---|
4848 | AutoCaller autoCaller(this);
|
---|
4849 | AssertComRCReturn(autoCaller.rc(), Utf8Str::Empty);
|
---|
4850 |
|
---|
4851 | /* check that our own format supports diffs */
|
---|
4852 | if (!(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_Differencing))
|
---|
4853 | {
|
---|
4854 | /* use the default format if not */
|
---|
4855 | Utf8Str tmp;
|
---|
4856 | m->pVirtualBox->i_getDefaultHardDiskFormat(tmp);
|
---|
4857 | return tmp;
|
---|
4858 | }
|
---|
4859 |
|
---|
4860 | /* m->strFormat is const, no need to lock */
|
---|
4861 | return m->strFormat;
|
---|
4862 | }
|
---|
4863 |
|
---|
4864 | /**
|
---|
4865 | * Returns a preferred variant for differencing media.
|
---|
4866 | */
|
---|
4867 | MediumVariant_T Medium::i_getPreferredDiffVariant()
|
---|
4868 | {
|
---|
4869 | AutoCaller autoCaller(this);
|
---|
4870 | AssertComRCReturn(autoCaller.rc(), MediumVariant_Standard);
|
---|
4871 |
|
---|
4872 | /* check that our own format supports diffs */
|
---|
4873 | if (!(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_Differencing))
|
---|
4874 | return MediumVariant_Standard;
|
---|
4875 |
|
---|
4876 | /* m->variant is const, no need to lock */
|
---|
4877 | ULONG mediumVariantFlags = (ULONG)m->variant;
|
---|
4878 | mediumVariantFlags &= ~(MediumVariant_Fixed | MediumVariant_VmdkStreamOptimized);
|
---|
4879 | mediumVariantFlags |= MediumVariant_Diff;
|
---|
4880 | return (MediumVariant_T)mediumVariantFlags;
|
---|
4881 | }
|
---|
4882 |
|
---|
4883 | /**
|
---|
4884 | * Implementation for the public Medium::Close() with the exception of calling
|
---|
4885 | * VirtualBox::saveRegistries(), in case someone wants to call this for several
|
---|
4886 | * media.
|
---|
4887 | *
|
---|
4888 | * After this returns with success, uninit() has been called on the medium, and
|
---|
4889 | * the object is no longer usable ("not ready" state).
|
---|
4890 | *
|
---|
4891 | * @param autoCaller AutoCaller instance which must have been created on the caller's
|
---|
4892 | * stack for this medium. This gets released hereupon
|
---|
4893 | * which the Medium instance gets uninitialized.
|
---|
4894 | * @return
|
---|
4895 | */
|
---|
4896 | HRESULT Medium::i_close(AutoCaller &autoCaller)
|
---|
4897 | {
|
---|
4898 | // must temporarily drop the caller, need the tree lock first
|
---|
4899 | autoCaller.release();
|
---|
4900 |
|
---|
4901 | // we're accessing parent/child and backrefs, so lock the tree first, then ourselves
|
---|
4902 | AutoMultiWriteLock2 multilock(&m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
4903 | this->lockHandle()
|
---|
4904 | COMMA_LOCKVAL_SRC_POS);
|
---|
4905 |
|
---|
4906 | autoCaller.add();
|
---|
4907 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4908 |
|
---|
4909 | LogFlowFunc(("ENTER for %s\n", i_getLocationFull().c_str()));
|
---|
4910 |
|
---|
4911 | bool wasCreated = true;
|
---|
4912 |
|
---|
4913 | switch (m->state)
|
---|
4914 | {
|
---|
4915 | case MediumState_NotCreated:
|
---|
4916 | wasCreated = false;
|
---|
4917 | break;
|
---|
4918 | case MediumState_Created:
|
---|
4919 | case MediumState_Inaccessible:
|
---|
4920 | break;
|
---|
4921 | default:
|
---|
4922 | return i_setStateError();
|
---|
4923 | }
|
---|
4924 |
|
---|
4925 | if (m->backRefs.size() != 0)
|
---|
4926 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4927 | tr("Medium '%s' cannot be closed because it is still attached to %d virtual machines"),
|
---|
4928 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
4929 |
|
---|
4930 | // perform extra media-dependent close checks
|
---|
4931 | HRESULT rc = i_canClose();
|
---|
4932 | if (FAILED(rc)) return rc;
|
---|
4933 |
|
---|
4934 | m->fClosing = true;
|
---|
4935 |
|
---|
4936 | if (wasCreated)
|
---|
4937 | {
|
---|
4938 | // remove from the list of known media before performing actual
|
---|
4939 | // uninitialization (to keep the media registry consistent on
|
---|
4940 | // failure to do so)
|
---|
4941 | rc = i_unregisterWithVirtualBox();
|
---|
4942 | if (FAILED(rc)) return rc;
|
---|
4943 |
|
---|
4944 | multilock.release();
|
---|
4945 | // Release the AutoCaller now, as otherwise uninit() will simply hang.
|
---|
4946 | // Needs to be done before mark the registries as modified and saving
|
---|
4947 | // the registry, as otherwise there may be a deadlock with someone else
|
---|
4948 | // closing this object while we're in i_saveModifiedRegistries(), which
|
---|
4949 | // needs the media tree lock, which the other thread holds until after
|
---|
4950 | // uninit() below.
|
---|
4951 | autoCaller.release();
|
---|
4952 | i_markRegistriesModified();
|
---|
4953 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
4954 | }
|
---|
4955 | else
|
---|
4956 | {
|
---|
4957 | multilock.release();
|
---|
4958 | // release the AutoCaller, as otherwise uninit() will simply hang
|
---|
4959 | autoCaller.release();
|
---|
4960 | }
|
---|
4961 |
|
---|
4962 | // Keep the locks held until after uninit, as otherwise the consistency
|
---|
4963 | // of the medium tree cannot be guaranteed.
|
---|
4964 | uninit();
|
---|
4965 |
|
---|
4966 | LogFlowFuncLeave();
|
---|
4967 |
|
---|
4968 | return rc;
|
---|
4969 | }
|
---|
4970 |
|
---|
4971 | /**
|
---|
4972 | * Deletes the medium storage unit.
|
---|
4973 | *
|
---|
4974 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
4975 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
4976 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
4977 | * progress object is created/used at all.
|
---|
4978 | *
|
---|
4979 | * When @a aWait is @c false, this method will create a thread to perform the
|
---|
4980 | * delete operation asynchronously and will return immediately. Otherwise, it
|
---|
4981 | * will perform the operation on the calling thread and will not return to the
|
---|
4982 | * caller until the operation is completed. Note that @a aProgress cannot be
|
---|
4983 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
4984 | *
|
---|
4985 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
4986 | * completion.
|
---|
4987 | * @param aWait @c true if this method should block instead of creating
|
---|
4988 | * an asynchronous thread.
|
---|
4989 | *
|
---|
4990 | * @note Locks mVirtualBox and this object for writing. Locks medium tree for
|
---|
4991 | * writing.
|
---|
4992 | */
|
---|
4993 | HRESULT Medium::i_deleteStorage(ComObjPtr<Progress> *aProgress,
|
---|
4994 | bool aWait)
|
---|
4995 | {
|
---|
4996 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
4997 | * to lock order violations, it probably causes lock order issues related
|
---|
4998 | * to the AutoCaller usage. */
|
---|
4999 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
5000 |
|
---|
5001 | AutoCaller autoCaller(this);
|
---|
5002 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
5003 |
|
---|
5004 | HRESULT rc = S_OK;
|
---|
5005 | ComObjPtr<Progress> pProgress;
|
---|
5006 | Medium::Task *pTask = NULL;
|
---|
5007 |
|
---|
5008 | try
|
---|
5009 | {
|
---|
5010 | /* we're accessing the media tree, and canClose() needs it too */
|
---|
5011 | AutoMultiWriteLock2 multilock(&m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
5012 | this->lockHandle()
|
---|
5013 | COMMA_LOCKVAL_SRC_POS);
|
---|
5014 | LogFlowThisFunc(("aWait=%RTbool locationFull=%s\n", aWait, i_getLocationFull().c_str() ));
|
---|
5015 |
|
---|
5016 | if ( !(m->formatObj->i_getCapabilities() & ( MediumFormatCapabilities_CreateDynamic
|
---|
5017 | | MediumFormatCapabilities_CreateFixed)))
|
---|
5018 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
5019 | tr("Medium format '%s' does not support storage deletion"),
|
---|
5020 | m->strFormat.c_str());
|
---|
5021 |
|
---|
5022 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
5023 | /** @todo r=klaus would be great if this could be moved to the async
|
---|
5024 | * part of the operation as it can take quite a while */
|
---|
5025 | if (m->queryInfoRunning)
|
---|
5026 | {
|
---|
5027 | while (m->queryInfoRunning)
|
---|
5028 | {
|
---|
5029 | multilock.release();
|
---|
5030 | /* Must not hold the media tree lock or the object lock, as
|
---|
5031 | * Medium::i_queryInfo needs this lock and thus we would run
|
---|
5032 | * into a deadlock here. */
|
---|
5033 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
5034 | Assert(!isWriteLockOnCurrentThread());
|
---|
5035 | {
|
---|
5036 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
5037 | }
|
---|
5038 | multilock.acquire();
|
---|
5039 | }
|
---|
5040 | }
|
---|
5041 |
|
---|
5042 | /* Note that we are fine with Inaccessible state too: a) for symmetry
|
---|
5043 | * with create calls and b) because it doesn't really harm to try, if
|
---|
5044 | * it is really inaccessible, the delete operation will fail anyway.
|
---|
5045 | * Accepting Inaccessible state is especially important because all
|
---|
5046 | * registered media are initially Inaccessible upon VBoxSVC startup
|
---|
5047 | * until COMGETTER(RefreshState) is called. Accept Deleting state
|
---|
5048 | * because some callers need to put the medium in this state early
|
---|
5049 | * to prevent races. */
|
---|
5050 | switch (m->state)
|
---|
5051 | {
|
---|
5052 | case MediumState_Created:
|
---|
5053 | case MediumState_Deleting:
|
---|
5054 | case MediumState_Inaccessible:
|
---|
5055 | break;
|
---|
5056 | default:
|
---|
5057 | throw i_setStateError();
|
---|
5058 | }
|
---|
5059 |
|
---|
5060 | if (m->backRefs.size() != 0)
|
---|
5061 | {
|
---|
5062 | Utf8Str strMachines;
|
---|
5063 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
5064 | it != m->backRefs.end();
|
---|
5065 | ++it)
|
---|
5066 | {
|
---|
5067 | const BackRef &b = *it;
|
---|
5068 | if (strMachines.length())
|
---|
5069 | strMachines.append(", ");
|
---|
5070 | strMachines.append(b.machineId.toString().c_str());
|
---|
5071 | }
|
---|
5072 | #ifdef DEBUG
|
---|
5073 | i_dumpBackRefs();
|
---|
5074 | #endif
|
---|
5075 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5076 | tr("Cannot delete storage: medium '%s' is still attached to the following %d virtual machine(s): %s"),
|
---|
5077 | m->strLocationFull.c_str(),
|
---|
5078 | m->backRefs.size(),
|
---|
5079 | strMachines.c_str());
|
---|
5080 | }
|
---|
5081 |
|
---|
5082 | rc = i_canClose();
|
---|
5083 | if (FAILED(rc))
|
---|
5084 | throw rc;
|
---|
5085 |
|
---|
5086 | /* go to Deleting state, so that the medium is not actually locked */
|
---|
5087 | if (m->state != MediumState_Deleting)
|
---|
5088 | {
|
---|
5089 | rc = i_markForDeletion();
|
---|
5090 | if (FAILED(rc))
|
---|
5091 | throw rc;
|
---|
5092 | }
|
---|
5093 |
|
---|
5094 | /* Build the medium lock list. */
|
---|
5095 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
5096 | multilock.release();
|
---|
5097 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5098 | this /* pToLockWrite */,
|
---|
5099 | false /* fMediumLockWriteAll */,
|
---|
5100 | NULL,
|
---|
5101 | *pMediumLockList);
|
---|
5102 | multilock.acquire();
|
---|
5103 | if (FAILED(rc))
|
---|
5104 | {
|
---|
5105 | delete pMediumLockList;
|
---|
5106 | throw rc;
|
---|
5107 | }
|
---|
5108 |
|
---|
5109 | multilock.release();
|
---|
5110 | rc = pMediumLockList->Lock();
|
---|
5111 | multilock.acquire();
|
---|
5112 | if (FAILED(rc))
|
---|
5113 | {
|
---|
5114 | delete pMediumLockList;
|
---|
5115 | throw setError(rc,
|
---|
5116 | tr("Failed to lock media when deleting '%s'"),
|
---|
5117 | i_getLocationFull().c_str());
|
---|
5118 | }
|
---|
5119 |
|
---|
5120 | /* try to remove from the list of known media before performing
|
---|
5121 | * actual deletion (we favor the consistency of the media registry
|
---|
5122 | * which would have been broken if unregisterWithVirtualBox() failed
|
---|
5123 | * after we successfully deleted the storage) */
|
---|
5124 | rc = i_unregisterWithVirtualBox();
|
---|
5125 | if (FAILED(rc))
|
---|
5126 | throw rc;
|
---|
5127 | // no longer need lock
|
---|
5128 | multilock.release();
|
---|
5129 | i_markRegistriesModified();
|
---|
5130 |
|
---|
5131 | if (aProgress != NULL)
|
---|
5132 | {
|
---|
5133 | /* use the existing progress object... */
|
---|
5134 | pProgress = *aProgress;
|
---|
5135 |
|
---|
5136 | /* ...but create a new one if it is null */
|
---|
5137 | if (pProgress.isNull())
|
---|
5138 | {
|
---|
5139 | pProgress.createObject();
|
---|
5140 | rc = pProgress->init(m->pVirtualBox,
|
---|
5141 | static_cast<IMedium*>(this),
|
---|
5142 | BstrFmt(tr("Deleting medium storage unit '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
5143 | FALSE /* aCancelable */);
|
---|
5144 | if (FAILED(rc))
|
---|
5145 | throw rc;
|
---|
5146 | }
|
---|
5147 | }
|
---|
5148 |
|
---|
5149 | /* setup task object to carry out the operation sync/async */
|
---|
5150 | pTask = new Medium::DeleteTask(this, pProgress, pMediumLockList);
|
---|
5151 | rc = pTask->rc();
|
---|
5152 | AssertComRC(rc);
|
---|
5153 | if (FAILED(rc))
|
---|
5154 | throw rc;
|
---|
5155 | }
|
---|
5156 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5157 |
|
---|
5158 | if (SUCCEEDED(rc))
|
---|
5159 | {
|
---|
5160 | if (aWait)
|
---|
5161 | {
|
---|
5162 | rc = pTask->runNow();
|
---|
5163 |
|
---|
5164 | delete pTask;
|
---|
5165 | }
|
---|
5166 | else
|
---|
5167 | rc = pTask->createThread();
|
---|
5168 |
|
---|
5169 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
5170 | *aProgress = pProgress;
|
---|
5171 |
|
---|
5172 | }
|
---|
5173 | else
|
---|
5174 | {
|
---|
5175 | if (pTask)
|
---|
5176 | delete pTask;
|
---|
5177 |
|
---|
5178 | /* Undo deleting state if necessary. */
|
---|
5179 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5180 | /* Make sure that any error signalled by unmarkForDeletion() is not
|
---|
5181 | * ending up in the error list (if the caller uses MultiResult). It
|
---|
5182 | * usually is spurious, as in most cases the medium hasn't been marked
|
---|
5183 | * for deletion when the error was thrown above. */
|
---|
5184 | ErrorInfoKeeper eik;
|
---|
5185 | i_unmarkForDeletion();
|
---|
5186 | }
|
---|
5187 |
|
---|
5188 | return rc;
|
---|
5189 | }
|
---|
5190 |
|
---|
5191 | /**
|
---|
5192 | * Mark a medium for deletion.
|
---|
5193 | *
|
---|
5194 | * @note Caller must hold the write lock on this medium!
|
---|
5195 | */
|
---|
5196 | HRESULT Medium::i_markForDeletion()
|
---|
5197 | {
|
---|
5198 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5199 | switch (m->state)
|
---|
5200 | {
|
---|
5201 | case MediumState_Created:
|
---|
5202 | case MediumState_Inaccessible:
|
---|
5203 | m->preLockState = m->state;
|
---|
5204 | m->state = MediumState_Deleting;
|
---|
5205 | return S_OK;
|
---|
5206 | default:
|
---|
5207 | return i_setStateError();
|
---|
5208 | }
|
---|
5209 | }
|
---|
5210 |
|
---|
5211 | /**
|
---|
5212 | * Removes the "mark for deletion".
|
---|
5213 | *
|
---|
5214 | * @note Caller must hold the write lock on this medium!
|
---|
5215 | */
|
---|
5216 | HRESULT Medium::i_unmarkForDeletion()
|
---|
5217 | {
|
---|
5218 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5219 | switch (m->state)
|
---|
5220 | {
|
---|
5221 | case MediumState_Deleting:
|
---|
5222 | m->state = m->preLockState;
|
---|
5223 | return S_OK;
|
---|
5224 | default:
|
---|
5225 | return i_setStateError();
|
---|
5226 | }
|
---|
5227 | }
|
---|
5228 |
|
---|
5229 | /**
|
---|
5230 | * Mark a medium for deletion which is in locked state.
|
---|
5231 | *
|
---|
5232 | * @note Caller must hold the write lock on this medium!
|
---|
5233 | */
|
---|
5234 | HRESULT Medium::i_markLockedForDeletion()
|
---|
5235 | {
|
---|
5236 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5237 | if ( ( m->state == MediumState_LockedRead
|
---|
5238 | || m->state == MediumState_LockedWrite)
|
---|
5239 | && m->preLockState == MediumState_Created)
|
---|
5240 | {
|
---|
5241 | m->preLockState = MediumState_Deleting;
|
---|
5242 | return S_OK;
|
---|
5243 | }
|
---|
5244 | else
|
---|
5245 | return i_setStateError();
|
---|
5246 | }
|
---|
5247 |
|
---|
5248 | /**
|
---|
5249 | * Removes the "mark for deletion" for a medium in locked state.
|
---|
5250 | *
|
---|
5251 | * @note Caller must hold the write lock on this medium!
|
---|
5252 | */
|
---|
5253 | HRESULT Medium::i_unmarkLockedForDeletion()
|
---|
5254 | {
|
---|
5255 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5256 | if ( ( m->state == MediumState_LockedRead
|
---|
5257 | || m->state == MediumState_LockedWrite)
|
---|
5258 | && m->preLockState == MediumState_Deleting)
|
---|
5259 | {
|
---|
5260 | m->preLockState = MediumState_Created;
|
---|
5261 | return S_OK;
|
---|
5262 | }
|
---|
5263 | else
|
---|
5264 | return i_setStateError();
|
---|
5265 | }
|
---|
5266 |
|
---|
5267 | /**
|
---|
5268 | * Queries the preferred merge direction from this to the other medium, i.e.
|
---|
5269 | * the one which requires the least amount of I/O and therefore time and
|
---|
5270 | * disk consumption.
|
---|
5271 | *
|
---|
5272 | * @returns Status code.
|
---|
5273 | * @retval E_FAIL in case determining the merge direction fails for some reason,
|
---|
5274 | * for example if getting the size of the media fails. There is no
|
---|
5275 | * error set though and the caller is free to continue to find out
|
---|
5276 | * what was going wrong later. Leaves fMergeForward unset.
|
---|
5277 | * @retval VBOX_E_INVALID_OBJECT_STATE if both media are not related to each other
|
---|
5278 | * An error is set.
|
---|
5279 | * @param pOther The other medium to merge with.
|
---|
5280 | * @param fMergeForward Resulting preferred merge direction (out).
|
---|
5281 | */
|
---|
5282 | HRESULT Medium::i_queryPreferredMergeDirection(const ComObjPtr<Medium> &pOther,
|
---|
5283 | bool &fMergeForward)
|
---|
5284 | {
|
---|
5285 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
5286 | * to lock order violations, it probably causes lock order issues related
|
---|
5287 | * to the AutoCaller usage. Likewise the code using this method seems
|
---|
5288 | * problematic. */
|
---|
5289 | AssertReturn(pOther != NULL, E_FAIL);
|
---|
5290 | AssertReturn(pOther != this, E_FAIL);
|
---|
5291 |
|
---|
5292 | AutoCaller autoCaller(this);
|
---|
5293 | AssertComRCReturnRC(autoCaller.rc());
|
---|
5294 |
|
---|
5295 | AutoCaller otherCaller(pOther);
|
---|
5296 | AssertComRCReturnRC(otherCaller.rc());
|
---|
5297 |
|
---|
5298 | HRESULT rc = S_OK;
|
---|
5299 | bool fThisParent = false; /**<< Flag whether this medium is the parent of pOther. */
|
---|
5300 |
|
---|
5301 | try
|
---|
5302 | {
|
---|
5303 | // locking: we need the tree lock first because we access parent pointers
|
---|
5304 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5305 |
|
---|
5306 | /* more sanity checking and figuring out the current merge direction */
|
---|
5307 | ComObjPtr<Medium> pMedium = i_getParent();
|
---|
5308 | while (!pMedium.isNull() && pMedium != pOther)
|
---|
5309 | pMedium = pMedium->i_getParent();
|
---|
5310 | if (pMedium == pOther)
|
---|
5311 | fThisParent = false;
|
---|
5312 | else
|
---|
5313 | {
|
---|
5314 | pMedium = pOther->i_getParent();
|
---|
5315 | while (!pMedium.isNull() && pMedium != this)
|
---|
5316 | pMedium = pMedium->i_getParent();
|
---|
5317 | if (pMedium == this)
|
---|
5318 | fThisParent = true;
|
---|
5319 | else
|
---|
5320 | {
|
---|
5321 | Utf8Str tgtLoc;
|
---|
5322 | {
|
---|
5323 | AutoReadLock alock(pOther COMMA_LOCKVAL_SRC_POS);
|
---|
5324 | tgtLoc = pOther->i_getLocationFull();
|
---|
5325 | }
|
---|
5326 |
|
---|
5327 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5328 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5329 | tr("Media '%s' and '%s' are unrelated"),
|
---|
5330 | m->strLocationFull.c_str(), tgtLoc.c_str());
|
---|
5331 | }
|
---|
5332 | }
|
---|
5333 |
|
---|
5334 | /*
|
---|
5335 | * Figure out the preferred merge direction. The current way is to
|
---|
5336 | * get the current sizes of file based images and select the merge
|
---|
5337 | * direction depending on the size.
|
---|
5338 | *
|
---|
5339 | * Can't use the VD API to get current size here as the media might
|
---|
5340 | * be write locked by a running VM. Resort to RTFileQuerySize().
|
---|
5341 | */
|
---|
5342 | int vrc = VINF_SUCCESS;
|
---|
5343 | uint64_t cbMediumThis = 0;
|
---|
5344 | uint64_t cbMediumOther = 0;
|
---|
5345 |
|
---|
5346 | if (i_isMediumFormatFile() && pOther->i_isMediumFormatFile())
|
---|
5347 | {
|
---|
5348 | vrc = RTFileQuerySize(this->i_getLocationFull().c_str(), &cbMediumThis);
|
---|
5349 | if (RT_SUCCESS(vrc))
|
---|
5350 | {
|
---|
5351 | vrc = RTFileQuerySize(pOther->i_getLocationFull().c_str(),
|
---|
5352 | &cbMediumOther);
|
---|
5353 | }
|
---|
5354 |
|
---|
5355 | if (RT_FAILURE(vrc))
|
---|
5356 | rc = E_FAIL;
|
---|
5357 | else
|
---|
5358 | {
|
---|
5359 | /*
|
---|
5360 | * Check which merge direction might be more optimal.
|
---|
5361 | * This method is not bullet proof of course as there might
|
---|
5362 | * be overlapping blocks in the images so the file size is
|
---|
5363 | * not the best indicator but it is good enough for our purpose
|
---|
5364 | * and everything else is too complicated, especially when the
|
---|
5365 | * media are used by a running VM.
|
---|
5366 | */
|
---|
5367 | bool fMergeIntoThis = cbMediumThis > cbMediumOther;
|
---|
5368 | fMergeForward = fMergeIntoThis != fThisParent;
|
---|
5369 | }
|
---|
5370 | }
|
---|
5371 | }
|
---|
5372 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5373 |
|
---|
5374 | return rc;
|
---|
5375 | }
|
---|
5376 |
|
---|
5377 | /**
|
---|
5378 | * Prepares this (source) medium, target medium and all intermediate media
|
---|
5379 | * for the merge operation.
|
---|
5380 | *
|
---|
5381 | * This method is to be called prior to calling the #mergeTo() to perform
|
---|
5382 | * necessary consistency checks and place involved media to appropriate
|
---|
5383 | * states. If #mergeTo() is not called or fails, the state modifications
|
---|
5384 | * performed by this method must be undone by #i_cancelMergeTo().
|
---|
5385 | *
|
---|
5386 | * See #mergeTo() for more information about merging.
|
---|
5387 | *
|
---|
5388 | * @param pTarget Target medium.
|
---|
5389 | * @param aMachineId Allowed machine attachment. NULL means do not check.
|
---|
5390 | * @param aSnapshotId Allowed snapshot attachment. NULL or empty UUID means
|
---|
5391 | * do not check.
|
---|
5392 | * @param fLockMedia Flag whether to lock the medium lock list or not.
|
---|
5393 | * If set to false and the medium lock list locking fails
|
---|
5394 | * later you must call #i_cancelMergeTo().
|
---|
5395 | * @param fMergeForward Resulting merge direction (out).
|
---|
5396 | * @param pParentForTarget New parent for target medium after merge (out).
|
---|
5397 | * @param aChildrenToReparent Medium lock list containing all children of the
|
---|
5398 | * source which will have to be reparented to the target
|
---|
5399 | * after merge (out).
|
---|
5400 | * @param aMediumLockList Medium locking information (out).
|
---|
5401 | *
|
---|
5402 | * @note Locks medium tree for reading. Locks this object, aTarget and all
|
---|
5403 | * intermediate media for writing.
|
---|
5404 | */
|
---|
5405 | HRESULT Medium::i_prepareMergeTo(const ComObjPtr<Medium> &pTarget,
|
---|
5406 | const Guid *aMachineId,
|
---|
5407 | const Guid *aSnapshotId,
|
---|
5408 | bool fLockMedia,
|
---|
5409 | bool &fMergeForward,
|
---|
5410 | ComObjPtr<Medium> &pParentForTarget,
|
---|
5411 | MediumLockList * &aChildrenToReparent,
|
---|
5412 | MediumLockList * &aMediumLockList)
|
---|
5413 | {
|
---|
5414 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
5415 | * to lock order violations, it probably causes lock order issues related
|
---|
5416 | * to the AutoCaller usage. Likewise the code using this method seems
|
---|
5417 | * problematic. */
|
---|
5418 | AssertReturn(pTarget != NULL, E_FAIL);
|
---|
5419 | AssertReturn(pTarget != this, E_FAIL);
|
---|
5420 |
|
---|
5421 | AutoCaller autoCaller(this);
|
---|
5422 | AssertComRCReturnRC(autoCaller.rc());
|
---|
5423 |
|
---|
5424 | AutoCaller targetCaller(pTarget);
|
---|
5425 | AssertComRCReturnRC(targetCaller.rc());
|
---|
5426 |
|
---|
5427 | HRESULT rc = S_OK;
|
---|
5428 | fMergeForward = false;
|
---|
5429 | pParentForTarget.setNull();
|
---|
5430 | Assert(aChildrenToReparent == NULL);
|
---|
5431 | aChildrenToReparent = NULL;
|
---|
5432 | Assert(aMediumLockList == NULL);
|
---|
5433 | aMediumLockList = NULL;
|
---|
5434 |
|
---|
5435 | try
|
---|
5436 | {
|
---|
5437 | // locking: we need the tree lock first because we access parent pointers
|
---|
5438 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5439 |
|
---|
5440 | /* more sanity checking and figuring out the merge direction */
|
---|
5441 | ComObjPtr<Medium> pMedium = i_getParent();
|
---|
5442 | while (!pMedium.isNull() && pMedium != pTarget)
|
---|
5443 | pMedium = pMedium->i_getParent();
|
---|
5444 | if (pMedium == pTarget)
|
---|
5445 | fMergeForward = false;
|
---|
5446 | else
|
---|
5447 | {
|
---|
5448 | pMedium = pTarget->i_getParent();
|
---|
5449 | while (!pMedium.isNull() && pMedium != this)
|
---|
5450 | pMedium = pMedium->i_getParent();
|
---|
5451 | if (pMedium == this)
|
---|
5452 | fMergeForward = true;
|
---|
5453 | else
|
---|
5454 | {
|
---|
5455 | Utf8Str tgtLoc;
|
---|
5456 | {
|
---|
5457 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5458 | tgtLoc = pTarget->i_getLocationFull();
|
---|
5459 | }
|
---|
5460 |
|
---|
5461 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5462 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5463 | tr("Media '%s' and '%s' are unrelated"),
|
---|
5464 | m->strLocationFull.c_str(), tgtLoc.c_str());
|
---|
5465 | }
|
---|
5466 | }
|
---|
5467 |
|
---|
5468 | /* Build the lock list. */
|
---|
5469 | aMediumLockList = new MediumLockList();
|
---|
5470 | treeLock.release();
|
---|
5471 | if (fMergeForward)
|
---|
5472 | rc = pTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5473 | pTarget /* pToLockWrite */,
|
---|
5474 | false /* fMediumLockWriteAll */,
|
---|
5475 | NULL,
|
---|
5476 | *aMediumLockList);
|
---|
5477 | else
|
---|
5478 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5479 | pTarget /* pToLockWrite */,
|
---|
5480 | false /* fMediumLockWriteAll */,
|
---|
5481 | NULL,
|
---|
5482 | *aMediumLockList);
|
---|
5483 | treeLock.acquire();
|
---|
5484 | if (FAILED(rc))
|
---|
5485 | throw rc;
|
---|
5486 |
|
---|
5487 | /* Sanity checking, must be after lock list creation as it depends on
|
---|
5488 | * valid medium states. The medium objects must be accessible. Only
|
---|
5489 | * do this if immediate locking is requested, otherwise it fails when
|
---|
5490 | * we construct a medium lock list for an already running VM. Snapshot
|
---|
5491 | * deletion uses this to simplify its life. */
|
---|
5492 | if (fLockMedia)
|
---|
5493 | {
|
---|
5494 | {
|
---|
5495 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5496 | if (m->state != MediumState_Created)
|
---|
5497 | throw i_setStateError();
|
---|
5498 | }
|
---|
5499 | {
|
---|
5500 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5501 | if (pTarget->m->state != MediumState_Created)
|
---|
5502 | throw pTarget->i_setStateError();
|
---|
5503 | }
|
---|
5504 | }
|
---|
5505 |
|
---|
5506 | /* check medium attachment and other sanity conditions */
|
---|
5507 | if (fMergeForward)
|
---|
5508 | {
|
---|
5509 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5510 | if (i_getChildren().size() > 1)
|
---|
5511 | {
|
---|
5512 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5513 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
5514 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
5515 | }
|
---|
5516 | /* One backreference is only allowed if the machine ID is not empty
|
---|
5517 | * and it matches the machine the medium is attached to (including
|
---|
5518 | * the snapshot ID if not empty). */
|
---|
5519 | if ( m->backRefs.size() != 0
|
---|
5520 | && ( !aMachineId
|
---|
5521 | || m->backRefs.size() != 1
|
---|
5522 | || aMachineId->isZero()
|
---|
5523 | || *i_getFirstMachineBackrefId() != *aMachineId
|
---|
5524 | || ( (!aSnapshotId || !aSnapshotId->isZero())
|
---|
5525 | && *i_getFirstMachineBackrefSnapshotId() != *aSnapshotId)))
|
---|
5526 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5527 | tr("Medium '%s' is attached to %d virtual machines"),
|
---|
5528 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
5529 | if (m->type == MediumType_Immutable)
|
---|
5530 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5531 | tr("Medium '%s' is immutable"),
|
---|
5532 | m->strLocationFull.c_str());
|
---|
5533 | if (m->type == MediumType_MultiAttach)
|
---|
5534 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5535 | tr("Medium '%s' is multi-attach"),
|
---|
5536 | m->strLocationFull.c_str());
|
---|
5537 | }
|
---|
5538 | else
|
---|
5539 | {
|
---|
5540 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5541 | if (pTarget->i_getChildren().size() > 1)
|
---|
5542 | {
|
---|
5543 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5544 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
5545 | pTarget->m->strLocationFull.c_str(),
|
---|
5546 | pTarget->i_getChildren().size());
|
---|
5547 | }
|
---|
5548 | if (pTarget->m->type == MediumType_Immutable)
|
---|
5549 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5550 | tr("Medium '%s' is immutable"),
|
---|
5551 | pTarget->m->strLocationFull.c_str());
|
---|
5552 | if (pTarget->m->type == MediumType_MultiAttach)
|
---|
5553 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5554 | tr("Medium '%s' is multi-attach"),
|
---|
5555 | pTarget->m->strLocationFull.c_str());
|
---|
5556 | }
|
---|
5557 | ComObjPtr<Medium> pLast(fMergeForward ? (Medium *)pTarget : this);
|
---|
5558 | ComObjPtr<Medium> pLastIntermediate = pLast->i_getParent();
|
---|
5559 | for (pLast = pLastIntermediate;
|
---|
5560 | !pLast.isNull() && pLast != pTarget && pLast != this;
|
---|
5561 | pLast = pLast->i_getParent())
|
---|
5562 | {
|
---|
5563 | AutoReadLock alock(pLast COMMA_LOCKVAL_SRC_POS);
|
---|
5564 | if (pLast->i_getChildren().size() > 1)
|
---|
5565 | {
|
---|
5566 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5567 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
5568 | pLast->m->strLocationFull.c_str(),
|
---|
5569 | pLast->i_getChildren().size());
|
---|
5570 | }
|
---|
5571 | if (pLast->m->backRefs.size() != 0)
|
---|
5572 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5573 | tr("Medium '%s' is attached to %d virtual machines"),
|
---|
5574 | pLast->m->strLocationFull.c_str(),
|
---|
5575 | pLast->m->backRefs.size());
|
---|
5576 |
|
---|
5577 | }
|
---|
5578 |
|
---|
5579 | /* Update medium states appropriately */
|
---|
5580 | {
|
---|
5581 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5582 |
|
---|
5583 | if (m->state == MediumState_Created)
|
---|
5584 | {
|
---|
5585 | rc = i_markForDeletion();
|
---|
5586 | if (FAILED(rc))
|
---|
5587 | throw rc;
|
---|
5588 | }
|
---|
5589 | else
|
---|
5590 | {
|
---|
5591 | if (fLockMedia)
|
---|
5592 | throw i_setStateError();
|
---|
5593 | else if ( m->state == MediumState_LockedWrite
|
---|
5594 | || m->state == MediumState_LockedRead)
|
---|
5595 | {
|
---|
5596 | /* Either mark it for deletion in locked state or allow
|
---|
5597 | * others to have done so. */
|
---|
5598 | if (m->preLockState == MediumState_Created)
|
---|
5599 | i_markLockedForDeletion();
|
---|
5600 | else if (m->preLockState != MediumState_Deleting)
|
---|
5601 | throw i_setStateError();
|
---|
5602 | }
|
---|
5603 | else
|
---|
5604 | throw i_setStateError();
|
---|
5605 | }
|
---|
5606 | }
|
---|
5607 |
|
---|
5608 | if (fMergeForward)
|
---|
5609 | {
|
---|
5610 | /* we will need parent to reparent target */
|
---|
5611 | pParentForTarget = i_getParent();
|
---|
5612 | }
|
---|
5613 | else
|
---|
5614 | {
|
---|
5615 | /* we will need to reparent children of the source */
|
---|
5616 | aChildrenToReparent = new MediumLockList();
|
---|
5617 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
5618 | it != i_getChildren().end();
|
---|
5619 | ++it)
|
---|
5620 | {
|
---|
5621 | pMedium = *it;
|
---|
5622 | aChildrenToReparent->Append(pMedium, true /* fLockWrite */);
|
---|
5623 | }
|
---|
5624 | if (fLockMedia && aChildrenToReparent)
|
---|
5625 | {
|
---|
5626 | treeLock.release();
|
---|
5627 | rc = aChildrenToReparent->Lock();
|
---|
5628 | treeLock.acquire();
|
---|
5629 | if (FAILED(rc))
|
---|
5630 | throw rc;
|
---|
5631 | }
|
---|
5632 | }
|
---|
5633 | for (pLast = pLastIntermediate;
|
---|
5634 | !pLast.isNull() && pLast != pTarget && pLast != this;
|
---|
5635 | pLast = pLast->i_getParent())
|
---|
5636 | {
|
---|
5637 | AutoWriteLock alock(pLast COMMA_LOCKVAL_SRC_POS);
|
---|
5638 | if (pLast->m->state == MediumState_Created)
|
---|
5639 | {
|
---|
5640 | rc = pLast->i_markForDeletion();
|
---|
5641 | if (FAILED(rc))
|
---|
5642 | throw rc;
|
---|
5643 | }
|
---|
5644 | else
|
---|
5645 | throw pLast->i_setStateError();
|
---|
5646 | }
|
---|
5647 |
|
---|
5648 | /* Tweak the lock list in the backward merge case, as the target
|
---|
5649 | * isn't marked to be locked for writing yet. */
|
---|
5650 | if (!fMergeForward)
|
---|
5651 | {
|
---|
5652 | MediumLockList::Base::iterator lockListBegin =
|
---|
5653 | aMediumLockList->GetBegin();
|
---|
5654 | MediumLockList::Base::iterator lockListEnd =
|
---|
5655 | aMediumLockList->GetEnd();
|
---|
5656 | ++lockListEnd;
|
---|
5657 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
5658 | it != lockListEnd;
|
---|
5659 | ++it)
|
---|
5660 | {
|
---|
5661 | MediumLock &mediumLock = *it;
|
---|
5662 | if (mediumLock.GetMedium() == pTarget)
|
---|
5663 | {
|
---|
5664 | HRESULT rc2 = mediumLock.UpdateLock(true);
|
---|
5665 | AssertComRC(rc2);
|
---|
5666 | break;
|
---|
5667 | }
|
---|
5668 | }
|
---|
5669 | }
|
---|
5670 |
|
---|
5671 | if (fLockMedia)
|
---|
5672 | {
|
---|
5673 | treeLock.release();
|
---|
5674 | rc = aMediumLockList->Lock();
|
---|
5675 | treeLock.acquire();
|
---|
5676 | if (FAILED(rc))
|
---|
5677 | {
|
---|
5678 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5679 | throw setError(rc,
|
---|
5680 | tr("Failed to lock media when merging to '%s'"),
|
---|
5681 | pTarget->i_getLocationFull().c_str());
|
---|
5682 | }
|
---|
5683 | }
|
---|
5684 | }
|
---|
5685 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5686 |
|
---|
5687 | if (FAILED(rc))
|
---|
5688 | {
|
---|
5689 | if (aMediumLockList)
|
---|
5690 | {
|
---|
5691 | delete aMediumLockList;
|
---|
5692 | aMediumLockList = NULL;
|
---|
5693 | }
|
---|
5694 | if (aChildrenToReparent)
|
---|
5695 | {
|
---|
5696 | delete aChildrenToReparent;
|
---|
5697 | aChildrenToReparent = NULL;
|
---|
5698 | }
|
---|
5699 | }
|
---|
5700 |
|
---|
5701 | return rc;
|
---|
5702 | }
|
---|
5703 |
|
---|
5704 | /**
|
---|
5705 | * Merges this medium to the specified medium which must be either its
|
---|
5706 | * direct ancestor or descendant.
|
---|
5707 | *
|
---|
5708 | * Given this medium is SOURCE and the specified medium is TARGET, we will
|
---|
5709 | * get two variants of the merge operation:
|
---|
5710 | *
|
---|
5711 | * forward merge
|
---|
5712 | * ------------------------->
|
---|
5713 | * [Extra] <- SOURCE <- Intermediate <- TARGET
|
---|
5714 | * Any Del Del LockWr
|
---|
5715 | *
|
---|
5716 | *
|
---|
5717 | * backward merge
|
---|
5718 | * <-------------------------
|
---|
5719 | * TARGET <- Intermediate <- SOURCE <- [Extra]
|
---|
5720 | * LockWr Del Del LockWr
|
---|
5721 | *
|
---|
5722 | * Each diagram shows the involved media on the media chain where
|
---|
5723 | * SOURCE and TARGET belong. Under each medium there is a state value which
|
---|
5724 | * the medium must have at a time of the mergeTo() call.
|
---|
5725 | *
|
---|
5726 | * The media in the square braces may be absent (e.g. when the forward
|
---|
5727 | * operation takes place and SOURCE is the base medium, or when the backward
|
---|
5728 | * merge operation takes place and TARGET is the last child in the chain) but if
|
---|
5729 | * they present they are involved too as shown.
|
---|
5730 | *
|
---|
5731 | * Neither the source medium nor intermediate media may be attached to
|
---|
5732 | * any VM directly or in the snapshot, otherwise this method will assert.
|
---|
5733 | *
|
---|
5734 | * The #i_prepareMergeTo() method must be called prior to this method to place
|
---|
5735 | * all involved to necessary states and perform other consistency checks.
|
---|
5736 | *
|
---|
5737 | * If @a aWait is @c true then this method will perform the operation on the
|
---|
5738 | * calling thread and will not return to the caller until the operation is
|
---|
5739 | * completed. When this method succeeds, all intermediate medium objects in
|
---|
5740 | * the chain will be uninitialized, the state of the target medium (and all
|
---|
5741 | * involved extra media) will be restored. @a aMediumLockList will not be
|
---|
5742 | * deleted, whether the operation is successful or not. The caller has to do
|
---|
5743 | * this if appropriate. Note that this (source) medium is not uninitialized
|
---|
5744 | * because of possible AutoCaller instances held by the caller of this method
|
---|
5745 | * on the current thread. It's therefore the responsibility of the caller to
|
---|
5746 | * call Medium::uninit() after releasing all callers.
|
---|
5747 | *
|
---|
5748 | * If @a aWait is @c false then this method will create a thread to perform the
|
---|
5749 | * operation asynchronously and will return immediately. If the operation
|
---|
5750 | * succeeds, the thread will uninitialize the source medium object and all
|
---|
5751 | * intermediate medium objects in the chain, reset the state of the target
|
---|
5752 | * medium (and all involved extra media) and delete @a aMediumLockList.
|
---|
5753 | * If the operation fails, the thread will only reset the states of all
|
---|
5754 | * involved media and delete @a aMediumLockList.
|
---|
5755 | *
|
---|
5756 | * When this method fails (regardless of the @a aWait mode), it is a caller's
|
---|
5757 | * responsibility to undo state changes and delete @a aMediumLockList using
|
---|
5758 | * #i_cancelMergeTo().
|
---|
5759 | *
|
---|
5760 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
5761 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
5762 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
5763 | * progress object is created/used at all. Note that @a aProgress cannot be
|
---|
5764 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
5765 | *
|
---|
5766 | * @param pTarget Target medium.
|
---|
5767 | * @param fMergeForward Merge direction.
|
---|
5768 | * @param pParentForTarget New parent for target medium after merge.
|
---|
5769 | * @param aChildrenToReparent List of children of the source which will have
|
---|
5770 | * to be reparented to the target after merge.
|
---|
5771 | * @param aMediumLockList Medium locking information.
|
---|
5772 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
5773 | * completion.
|
---|
5774 | * @param aWait @c true if this method should block instead of creating
|
---|
5775 | * an asynchronous thread.
|
---|
5776 | *
|
---|
5777 | * @note Locks the tree lock for writing. Locks the media from the chain
|
---|
5778 | * for writing.
|
---|
5779 | */
|
---|
5780 | HRESULT Medium::i_mergeTo(const ComObjPtr<Medium> &pTarget,
|
---|
5781 | bool fMergeForward,
|
---|
5782 | const ComObjPtr<Medium> &pParentForTarget,
|
---|
5783 | MediumLockList *aChildrenToReparent,
|
---|
5784 | MediumLockList *aMediumLockList,
|
---|
5785 | ComObjPtr<Progress> *aProgress,
|
---|
5786 | bool aWait)
|
---|
5787 | {
|
---|
5788 | AssertReturn(pTarget != NULL, E_FAIL);
|
---|
5789 | AssertReturn(pTarget != this, E_FAIL);
|
---|
5790 | AssertReturn(aMediumLockList != NULL, E_FAIL);
|
---|
5791 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
5792 |
|
---|
5793 | AutoCaller autoCaller(this);
|
---|
5794 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
5795 |
|
---|
5796 | AutoCaller targetCaller(pTarget);
|
---|
5797 | AssertComRCReturnRC(targetCaller.rc());
|
---|
5798 |
|
---|
5799 | HRESULT rc = S_OK;
|
---|
5800 | ComObjPtr<Progress> pProgress;
|
---|
5801 | Medium::Task *pTask = NULL;
|
---|
5802 |
|
---|
5803 | try
|
---|
5804 | {
|
---|
5805 | if (aProgress != NULL)
|
---|
5806 | {
|
---|
5807 | /* use the existing progress object... */
|
---|
5808 | pProgress = *aProgress;
|
---|
5809 |
|
---|
5810 | /* ...but create a new one if it is null */
|
---|
5811 | if (pProgress.isNull())
|
---|
5812 | {
|
---|
5813 | Utf8Str tgtName;
|
---|
5814 | {
|
---|
5815 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5816 | tgtName = pTarget->i_getName();
|
---|
5817 | }
|
---|
5818 |
|
---|
5819 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5820 |
|
---|
5821 | pProgress.createObject();
|
---|
5822 | rc = pProgress->init(m->pVirtualBox,
|
---|
5823 | static_cast<IMedium*>(this),
|
---|
5824 | BstrFmt(tr("Merging medium '%s' to '%s'"),
|
---|
5825 | i_getName().c_str(),
|
---|
5826 | tgtName.c_str()).raw(),
|
---|
5827 | TRUE /* aCancelable */);
|
---|
5828 | if (FAILED(rc))
|
---|
5829 | throw rc;
|
---|
5830 | }
|
---|
5831 | }
|
---|
5832 |
|
---|
5833 | /* setup task object to carry out the operation sync/async */
|
---|
5834 | pTask = new Medium::MergeTask(this, pTarget, fMergeForward,
|
---|
5835 | pParentForTarget, aChildrenToReparent,
|
---|
5836 | pProgress, aMediumLockList,
|
---|
5837 | aWait /* fKeepMediumLockList */);
|
---|
5838 | rc = pTask->rc();
|
---|
5839 | AssertComRC(rc);
|
---|
5840 | if (FAILED(rc))
|
---|
5841 | throw rc;
|
---|
5842 | }
|
---|
5843 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5844 |
|
---|
5845 | if (SUCCEEDED(rc))
|
---|
5846 | {
|
---|
5847 | if (aWait)
|
---|
5848 | {
|
---|
5849 | rc = pTask->runNow();
|
---|
5850 |
|
---|
5851 | delete pTask;
|
---|
5852 | }
|
---|
5853 | else
|
---|
5854 | rc = pTask->createThread();
|
---|
5855 |
|
---|
5856 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
5857 | *aProgress = pProgress;
|
---|
5858 | }
|
---|
5859 | else if (pTask != NULL)
|
---|
5860 | delete pTask;
|
---|
5861 |
|
---|
5862 | return rc;
|
---|
5863 | }
|
---|
5864 |
|
---|
5865 | /**
|
---|
5866 | * Undoes what #i_prepareMergeTo() did. Must be called if #mergeTo() is not
|
---|
5867 | * called or fails. Frees memory occupied by @a aMediumLockList and unlocks
|
---|
5868 | * the medium objects in @a aChildrenToReparent.
|
---|
5869 | *
|
---|
5870 | * @param aChildrenToReparent List of children of the source which will have
|
---|
5871 | * to be reparented to the target after merge.
|
---|
5872 | * @param aMediumLockList Medium locking information.
|
---|
5873 | *
|
---|
5874 | * @note Locks the media from the chain for writing.
|
---|
5875 | */
|
---|
5876 | void Medium::i_cancelMergeTo(MediumLockList *aChildrenToReparent,
|
---|
5877 | MediumLockList *aMediumLockList)
|
---|
5878 | {
|
---|
5879 | AutoCaller autoCaller(this);
|
---|
5880 | AssertComRCReturnVoid(autoCaller.rc());
|
---|
5881 |
|
---|
5882 | AssertReturnVoid(aMediumLockList != NULL);
|
---|
5883 |
|
---|
5884 | /* Revert media marked for deletion to previous state. */
|
---|
5885 | HRESULT rc;
|
---|
5886 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
5887 | aMediumLockList->GetBegin();
|
---|
5888 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
5889 | aMediumLockList->GetEnd();
|
---|
5890 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
5891 | it != mediumListEnd;
|
---|
5892 | ++it)
|
---|
5893 | {
|
---|
5894 | const MediumLock &mediumLock = *it;
|
---|
5895 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
5896 | AutoWriteLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
5897 |
|
---|
5898 | if (pMedium->m->state == MediumState_Deleting)
|
---|
5899 | {
|
---|
5900 | rc = pMedium->i_unmarkForDeletion();
|
---|
5901 | AssertComRC(rc);
|
---|
5902 | }
|
---|
5903 | else if ( ( pMedium->m->state == MediumState_LockedWrite
|
---|
5904 | || pMedium->m->state == MediumState_LockedRead)
|
---|
5905 | && pMedium->m->preLockState == MediumState_Deleting)
|
---|
5906 | {
|
---|
5907 | rc = pMedium->i_unmarkLockedForDeletion();
|
---|
5908 | AssertComRC(rc);
|
---|
5909 | }
|
---|
5910 | }
|
---|
5911 |
|
---|
5912 | /* the destructor will do the work */
|
---|
5913 | delete aMediumLockList;
|
---|
5914 |
|
---|
5915 | /* unlock the children which had to be reparented, the destructor will do
|
---|
5916 | * the work */
|
---|
5917 | if (aChildrenToReparent)
|
---|
5918 | delete aChildrenToReparent;
|
---|
5919 | }
|
---|
5920 |
|
---|
5921 | /**
|
---|
5922 | * Fix the parent UUID of all children to point to this medium as their
|
---|
5923 | * parent.
|
---|
5924 | */
|
---|
5925 | HRESULT Medium::i_fixParentUuidOfChildren(MediumLockList *pChildrenToReparent)
|
---|
5926 | {
|
---|
5927 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
5928 | * to lock order violations, it probably causes lock order issues related
|
---|
5929 | * to the AutoCaller usage. Likewise the code using this method seems
|
---|
5930 | * problematic. */
|
---|
5931 | Assert(!isWriteLockOnCurrentThread());
|
---|
5932 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
5933 | MediumLockList mediumLockList;
|
---|
5934 | HRESULT rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5935 | NULL /* pToLockWrite */,
|
---|
5936 | false /* fMediumLockWriteAll */,
|
---|
5937 | this,
|
---|
5938 | mediumLockList);
|
---|
5939 | AssertComRCReturnRC(rc);
|
---|
5940 |
|
---|
5941 | try
|
---|
5942 | {
|
---|
5943 | PVDISK hdd;
|
---|
5944 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
5945 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
5946 |
|
---|
5947 | try
|
---|
5948 | {
|
---|
5949 | MediumLockList::Base::iterator lockListBegin =
|
---|
5950 | mediumLockList.GetBegin();
|
---|
5951 | MediumLockList::Base::iterator lockListEnd =
|
---|
5952 | mediumLockList.GetEnd();
|
---|
5953 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
5954 | it != lockListEnd;
|
---|
5955 | ++it)
|
---|
5956 | {
|
---|
5957 | MediumLock &mediumLock = *it;
|
---|
5958 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
5959 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
5960 |
|
---|
5961 | // open the medium
|
---|
5962 | vrc = VDOpen(hdd,
|
---|
5963 | pMedium->m->strFormat.c_str(),
|
---|
5964 | pMedium->m->strLocationFull.c_str(),
|
---|
5965 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
5966 | pMedium->m->vdImageIfaces);
|
---|
5967 | if (RT_FAILURE(vrc))
|
---|
5968 | throw vrc;
|
---|
5969 | }
|
---|
5970 |
|
---|
5971 | MediumLockList::Base::iterator childrenBegin = pChildrenToReparent->GetBegin();
|
---|
5972 | MediumLockList::Base::iterator childrenEnd = pChildrenToReparent->GetEnd();
|
---|
5973 | for (MediumLockList::Base::iterator it = childrenBegin;
|
---|
5974 | it != childrenEnd;
|
---|
5975 | ++it)
|
---|
5976 | {
|
---|
5977 | Medium *pMedium = it->GetMedium();
|
---|
5978 | /* VD_OPEN_FLAGS_INFO since UUID is wrong yet */
|
---|
5979 | vrc = VDOpen(hdd,
|
---|
5980 | pMedium->m->strFormat.c_str(),
|
---|
5981 | pMedium->m->strLocationFull.c_str(),
|
---|
5982 | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
5983 | pMedium->m->vdImageIfaces);
|
---|
5984 | if (RT_FAILURE(vrc))
|
---|
5985 | throw vrc;
|
---|
5986 |
|
---|
5987 | vrc = VDSetParentUuid(hdd, VD_LAST_IMAGE, m->id.raw());
|
---|
5988 | if (RT_FAILURE(vrc))
|
---|
5989 | throw vrc;
|
---|
5990 |
|
---|
5991 | vrc = VDClose(hdd, false /* fDelete */);
|
---|
5992 | if (RT_FAILURE(vrc))
|
---|
5993 | throw vrc;
|
---|
5994 | }
|
---|
5995 | }
|
---|
5996 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5997 | catch (int aVRC)
|
---|
5998 | {
|
---|
5999 | rc = setError(E_FAIL,
|
---|
6000 | tr("Could not update medium UUID references to parent '%s' (%s)"),
|
---|
6001 | m->strLocationFull.c_str(),
|
---|
6002 | i_vdError(aVRC).c_str());
|
---|
6003 | }
|
---|
6004 |
|
---|
6005 | VDDestroy(hdd);
|
---|
6006 | }
|
---|
6007 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6008 |
|
---|
6009 | return rc;
|
---|
6010 | }
|
---|
6011 |
|
---|
6012 | /**
|
---|
6013 | *
|
---|
6014 | * @note Similar code exists in i_taskExportHandler.
|
---|
6015 | */
|
---|
6016 | HRESULT Medium::i_addRawToFss(const char *aFilename, SecretKeyStore *pKeyStore, RTVFSFSSTREAM hVfsFssDst,
|
---|
6017 | const ComObjPtr<Progress> &aProgress, bool fSparse)
|
---|
6018 | {
|
---|
6019 | AutoCaller autoCaller(this);
|
---|
6020 | HRESULT hrc = autoCaller.rc();
|
---|
6021 | if (SUCCEEDED(hrc))
|
---|
6022 | {
|
---|
6023 | /*
|
---|
6024 | * Get a readonly hdd for this medium.
|
---|
6025 | */
|
---|
6026 | Medium::CryptoFilterSettings CryptoSettingsRead;
|
---|
6027 | MediumLockList SourceMediumLockList;
|
---|
6028 | PVDISK pHdd;
|
---|
6029 | hrc = i_openHddForReading(pKeyStore, &pHdd, &SourceMediumLockList, &CryptoSettingsRead);
|
---|
6030 | if (SUCCEEDED(hrc))
|
---|
6031 | {
|
---|
6032 | /*
|
---|
6033 | * Create a VFS file interface to the HDD and attach a progress wrapper
|
---|
6034 | * that monitors the progress reading of the raw image. The image will
|
---|
6035 | * be read twice if hVfsFssDst does sparse processing.
|
---|
6036 | */
|
---|
6037 | RTVFSFILE hVfsFileDisk = NIL_RTVFSFILE;
|
---|
6038 | int vrc = VDCreateVfsFileFromDisk(pHdd, 0 /*fFlags*/, &hVfsFileDisk);
|
---|
6039 | if (RT_SUCCESS(vrc))
|
---|
6040 | {
|
---|
6041 | RTVFSFILE hVfsFileProgress = NIL_RTVFSFILE;
|
---|
6042 | vrc = RTVfsCreateProgressForFile(hVfsFileDisk, aProgress->i_iprtProgressCallback, &*aProgress,
|
---|
6043 | RTVFSPROGRESS_F_CANCELABLE | RTVFSPROGRESS_F_FORWARD_SEEK_AS_READ,
|
---|
6044 | VDGetSize(pHdd, VD_LAST_IMAGE) * (fSparse ? 2 : 1) /*cbExpectedRead*/,
|
---|
6045 | 0 /*cbExpectedWritten*/, &hVfsFileProgress);
|
---|
6046 | RTVfsFileRelease(hVfsFileDisk);
|
---|
6047 | if (RT_SUCCESS(vrc))
|
---|
6048 | {
|
---|
6049 | RTVFSOBJ hVfsObj = RTVfsObjFromFile(hVfsFileProgress);
|
---|
6050 | RTVfsFileRelease(hVfsFileProgress);
|
---|
6051 |
|
---|
6052 | vrc = RTVfsFsStrmAdd(hVfsFssDst, aFilename, hVfsObj, 0 /*fFlags*/);
|
---|
6053 | RTVfsObjRelease(hVfsObj);
|
---|
6054 | if (RT_FAILURE(vrc))
|
---|
6055 | hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Failed to add '%s' to output (%Rrc)"), aFilename, vrc);
|
---|
6056 | }
|
---|
6057 | else
|
---|
6058 | hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
6059 | tr("RTVfsCreateProgressForFile failed when processing '%s' (%Rrc)"), aFilename, vrc);
|
---|
6060 | }
|
---|
6061 | else
|
---|
6062 | hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("VDCreateVfsFileFromDisk failed for '%s' (%Rrc)"), aFilename, vrc);
|
---|
6063 | VDDestroy(pHdd);
|
---|
6064 | }
|
---|
6065 | }
|
---|
6066 | return hrc;
|
---|
6067 | }
|
---|
6068 |
|
---|
6069 | /**
|
---|
6070 | * Used by IAppliance to export disk images.
|
---|
6071 | *
|
---|
6072 | * @param aFilename Filename to create (UTF8).
|
---|
6073 | * @param aFormat Medium format for creating @a aFilename.
|
---|
6074 | * @param aVariant Which exact image format variant to use for the
|
---|
6075 | * destination image.
|
---|
6076 | * @param pKeyStore The optional key store for decrypting the data for
|
---|
6077 | * encrypted media during the export.
|
---|
6078 | * @param hVfsIosDst The destination I/O stream object.
|
---|
6079 | * @param aProgress Progress object to use.
|
---|
6080 | * @return
|
---|
6081 | *
|
---|
6082 | * @note The source format is defined by the Medium instance.
|
---|
6083 | */
|
---|
6084 | HRESULT Medium::i_exportFile(const char *aFilename,
|
---|
6085 | const ComObjPtr<MediumFormat> &aFormat,
|
---|
6086 | MediumVariant_T aVariant,
|
---|
6087 | SecretKeyStore *pKeyStore,
|
---|
6088 | RTVFSIOSTREAM hVfsIosDst,
|
---|
6089 | const ComObjPtr<Progress> &aProgress)
|
---|
6090 | {
|
---|
6091 | AssertPtrReturn(aFilename, E_INVALIDARG);
|
---|
6092 | AssertReturn(aFormat.isNotNull(), E_INVALIDARG);
|
---|
6093 | AssertReturn(aProgress.isNotNull(), E_INVALIDARG);
|
---|
6094 |
|
---|
6095 | AutoCaller autoCaller(this);
|
---|
6096 | HRESULT hrc = autoCaller.rc();
|
---|
6097 | if (SUCCEEDED(hrc))
|
---|
6098 | {
|
---|
6099 | /*
|
---|
6100 | * Setup VD interfaces.
|
---|
6101 | */
|
---|
6102 | PVDINTERFACE pVDImageIfaces = m->vdImageIfaces;
|
---|
6103 | PVDINTERFACEIO pVfsIoIf;
|
---|
6104 | int vrc = VDIfCreateFromVfsStream(hVfsIosDst, RTFILE_O_WRITE, &pVfsIoIf);
|
---|
6105 | if (RT_SUCCESS(vrc))
|
---|
6106 | {
|
---|
6107 | vrc = VDInterfaceAdd(&pVfsIoIf->Core, "Medium::ExportTaskVfsIos", VDINTERFACETYPE_IO,
|
---|
6108 | pVfsIoIf, sizeof(VDINTERFACEIO), &pVDImageIfaces);
|
---|
6109 | if (RT_SUCCESS(vrc))
|
---|
6110 | {
|
---|
6111 | /*
|
---|
6112 | * Get a readonly hdd for this medium (source).
|
---|
6113 | */
|
---|
6114 | Medium::CryptoFilterSettings CryptoSettingsRead;
|
---|
6115 | MediumLockList SourceMediumLockList;
|
---|
6116 | PVDISK pSrcHdd;
|
---|
6117 | hrc = i_openHddForReading(pKeyStore, &pSrcHdd, &SourceMediumLockList, &CryptoSettingsRead);
|
---|
6118 | if (SUCCEEDED(hrc))
|
---|
6119 | {
|
---|
6120 | /*
|
---|
6121 | * Create the target medium.
|
---|
6122 | */
|
---|
6123 | Utf8Str strDstFormat(aFormat->i_getId());
|
---|
6124 |
|
---|
6125 | /* ensure the target directory exists */
|
---|
6126 | uint64_t fDstCapabilities = aFormat->i_getCapabilities();
|
---|
6127 | if (fDstCapabilities & MediumFormatCapabilities_File)
|
---|
6128 | {
|
---|
6129 | Utf8Str strDstLocation(aFilename);
|
---|
6130 | hrc = VirtualBox::i_ensureFilePathExists(strDstLocation.c_str(),
|
---|
6131 | !(aVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
6132 | }
|
---|
6133 | if (SUCCEEDED(hrc))
|
---|
6134 | {
|
---|
6135 | PVDISK pDstHdd;
|
---|
6136 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDstHdd);
|
---|
6137 | if (RT_SUCCESS(vrc))
|
---|
6138 | {
|
---|
6139 | /*
|
---|
6140 | * Create an interface for getting progress callbacks.
|
---|
6141 | */
|
---|
6142 | VDINTERFACEPROGRESS ProgressIf = VDINTERFACEPROGRESS_INITALIZER(aProgress->i_vdProgressCallback);
|
---|
6143 | PVDINTERFACE pProgress = NULL;
|
---|
6144 | vrc = VDInterfaceAdd(&ProgressIf.Core, "export-progress", VDINTERFACETYPE_PROGRESS,
|
---|
6145 | &*aProgress, sizeof(ProgressIf), &pProgress);
|
---|
6146 | AssertRC(vrc);
|
---|
6147 |
|
---|
6148 | /*
|
---|
6149 | * Do the exporting.
|
---|
6150 | */
|
---|
6151 | vrc = VDCopy(pSrcHdd,
|
---|
6152 | VD_LAST_IMAGE,
|
---|
6153 | pDstHdd,
|
---|
6154 | strDstFormat.c_str(),
|
---|
6155 | aFilename,
|
---|
6156 | false /* fMoveByRename */,
|
---|
6157 | 0 /* cbSize */,
|
---|
6158 | aVariant & ~MediumVariant_NoCreateDir,
|
---|
6159 | NULL /* pDstUuid */,
|
---|
6160 | VD_OPEN_FLAGS_NORMAL | VD_OPEN_FLAGS_SEQUENTIAL,
|
---|
6161 | pProgress,
|
---|
6162 | pVDImageIfaces,
|
---|
6163 | NULL);
|
---|
6164 | if (RT_SUCCESS(vrc))
|
---|
6165 | hrc = S_OK;
|
---|
6166 | else
|
---|
6167 | hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Could not create the exported medium '%s'%s"),
|
---|
6168 | aFilename, i_vdError(vrc).c_str());
|
---|
6169 | VDDestroy(pDstHdd);
|
---|
6170 | }
|
---|
6171 | else
|
---|
6172 | hrc = setErrorVrc(vrc);
|
---|
6173 | }
|
---|
6174 | }
|
---|
6175 | VDDestroy(pSrcHdd);
|
---|
6176 | }
|
---|
6177 | else
|
---|
6178 | hrc = setErrorVrc(vrc, "VDInterfaceAdd -> %Rrc", vrc);
|
---|
6179 | VDIfDestroyFromVfsStream(pVfsIoIf);
|
---|
6180 | }
|
---|
6181 | else
|
---|
6182 | hrc = setErrorVrc(vrc, "VDIfCreateFromVfsStream -> %Rrc", vrc);
|
---|
6183 | }
|
---|
6184 | return hrc;
|
---|
6185 | }
|
---|
6186 |
|
---|
6187 | /**
|
---|
6188 | * Used by IAppliance to import disk images.
|
---|
6189 | *
|
---|
6190 | * @param aFilename Filename to read (UTF8).
|
---|
6191 | * @param aFormat Medium format for reading @a aFilename.
|
---|
6192 | * @param aVariant Which exact image format variant to use
|
---|
6193 | * for the destination image.
|
---|
6194 | * @param aVfsIosSrc Handle to the source I/O stream.
|
---|
6195 | * @param aParent Parent medium. May be NULL.
|
---|
6196 | * @param aProgress Progress object to use.
|
---|
6197 | * @return
|
---|
6198 | * @note The destination format is defined by the Medium instance.
|
---|
6199 | *
|
---|
6200 | * @todo The only consumer of this method (Appliance::i_importOneDiskImage) is
|
---|
6201 | * already on a worker thread, so perhaps consider bypassing the thread
|
---|
6202 | * here and run in the task synchronously? VBoxSVC has enough threads as
|
---|
6203 | * it is...
|
---|
6204 | */
|
---|
6205 | HRESULT Medium::i_importFile(const char *aFilename,
|
---|
6206 | const ComObjPtr<MediumFormat> &aFormat,
|
---|
6207 | MediumVariant_T aVariant,
|
---|
6208 | RTVFSIOSTREAM aVfsIosSrc,
|
---|
6209 | const ComObjPtr<Medium> &aParent,
|
---|
6210 | const ComObjPtr<Progress> &aProgress)
|
---|
6211 | {
|
---|
6212 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
6213 | * to lock order violations, it probably causes lock order issues related
|
---|
6214 | * to the AutoCaller usage. */
|
---|
6215 | AssertPtrReturn(aFilename, E_INVALIDARG);
|
---|
6216 | AssertReturn(!aFormat.isNull(), E_INVALIDARG);
|
---|
6217 | AssertReturn(!aProgress.isNull(), E_INVALIDARG);
|
---|
6218 |
|
---|
6219 | AutoCaller autoCaller(this);
|
---|
6220 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
6221 |
|
---|
6222 | HRESULT rc = S_OK;
|
---|
6223 | Medium::Task *pTask = NULL;
|
---|
6224 |
|
---|
6225 | try
|
---|
6226 | {
|
---|
6227 | // locking: we need the tree lock first because we access parent pointers
|
---|
6228 | // and we need to write-lock the media involved
|
---|
6229 | uint32_t cHandles = 2;
|
---|
6230 | LockHandle* pHandles[3] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
6231 | this->lockHandle() };
|
---|
6232 | /* Only add parent to the lock if it is not null */
|
---|
6233 | if (!aParent.isNull())
|
---|
6234 | pHandles[cHandles++] = aParent->lockHandle();
|
---|
6235 | AutoWriteLock alock(cHandles,
|
---|
6236 | pHandles
|
---|
6237 | COMMA_LOCKVAL_SRC_POS);
|
---|
6238 |
|
---|
6239 | if ( m->state != MediumState_NotCreated
|
---|
6240 | && m->state != MediumState_Created)
|
---|
6241 | throw i_setStateError();
|
---|
6242 |
|
---|
6243 | /* Build the target lock list. */
|
---|
6244 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
6245 | alock.release();
|
---|
6246 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
6247 | this /* pToLockWrite */,
|
---|
6248 | false /* fMediumLockWriteAll */,
|
---|
6249 | aParent,
|
---|
6250 | *pTargetMediumLockList);
|
---|
6251 | alock.acquire();
|
---|
6252 | if (FAILED(rc))
|
---|
6253 | {
|
---|
6254 | delete pTargetMediumLockList;
|
---|
6255 | throw rc;
|
---|
6256 | }
|
---|
6257 |
|
---|
6258 | alock.release();
|
---|
6259 | rc = pTargetMediumLockList->Lock();
|
---|
6260 | alock.acquire();
|
---|
6261 | if (FAILED(rc))
|
---|
6262 | {
|
---|
6263 | delete pTargetMediumLockList;
|
---|
6264 | throw setError(rc,
|
---|
6265 | tr("Failed to lock target media '%s'"),
|
---|
6266 | i_getLocationFull().c_str());
|
---|
6267 | }
|
---|
6268 |
|
---|
6269 | /* setup task object to carry out the operation asynchronously */
|
---|
6270 | pTask = new Medium::ImportTask(this, aProgress, aFilename, aFormat, aVariant,
|
---|
6271 | aVfsIosSrc, aParent, pTargetMediumLockList);
|
---|
6272 | rc = pTask->rc();
|
---|
6273 | AssertComRC(rc);
|
---|
6274 | if (FAILED(rc))
|
---|
6275 | throw rc;
|
---|
6276 |
|
---|
6277 | if (m->state == MediumState_NotCreated)
|
---|
6278 | m->state = MediumState_Creating;
|
---|
6279 | }
|
---|
6280 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6281 |
|
---|
6282 | if (SUCCEEDED(rc))
|
---|
6283 | rc = pTask->createThread();
|
---|
6284 | else if (pTask != NULL)
|
---|
6285 | delete pTask;
|
---|
6286 |
|
---|
6287 | return rc;
|
---|
6288 | }
|
---|
6289 |
|
---|
6290 | /**
|
---|
6291 | * Internal version of the public CloneTo API which allows to enable certain
|
---|
6292 | * optimizations to improve speed during VM cloning.
|
---|
6293 | *
|
---|
6294 | * @param aTarget Target medium
|
---|
6295 | * @param aVariant Which exact image format variant to use
|
---|
6296 | * for the destination image.
|
---|
6297 | * @param aParent Parent medium. May be NULL.
|
---|
6298 | * @param aProgress Progress object to use.
|
---|
6299 | * @param idxSrcImageSame The last image in the source chain which has the
|
---|
6300 | * same content as the given image in the destination
|
---|
6301 | * chain. Use UINT32_MAX to disable this optimization.
|
---|
6302 | * @param idxDstImageSame The last image in the destination chain which has the
|
---|
6303 | * same content as the given image in the source chain.
|
---|
6304 | * Use UINT32_MAX to disable this optimization.
|
---|
6305 | * @return
|
---|
6306 | */
|
---|
6307 | HRESULT Medium::i_cloneToEx(const ComObjPtr<Medium> &aTarget, ULONG aVariant,
|
---|
6308 | const ComObjPtr<Medium> &aParent, IProgress **aProgress,
|
---|
6309 | uint32_t idxSrcImageSame, uint32_t idxDstImageSame)
|
---|
6310 | {
|
---|
6311 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
6312 | * to lock order violations, it probably causes lock order issues related
|
---|
6313 | * to the AutoCaller usage. */
|
---|
6314 | CheckComArgNotNull(aTarget);
|
---|
6315 | CheckComArgOutPointerValid(aProgress);
|
---|
6316 | ComAssertRet(aTarget != this, E_INVALIDARG);
|
---|
6317 |
|
---|
6318 | AutoCaller autoCaller(this);
|
---|
6319 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
6320 |
|
---|
6321 | HRESULT rc = S_OK;
|
---|
6322 | ComObjPtr<Progress> pProgress;
|
---|
6323 | Medium::Task *pTask = NULL;
|
---|
6324 |
|
---|
6325 | try
|
---|
6326 | {
|
---|
6327 | // locking: we need the tree lock first because we access parent pointers
|
---|
6328 | // and we need to write-lock the media involved
|
---|
6329 | uint32_t cHandles = 3;
|
---|
6330 | LockHandle* pHandles[4] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
6331 | this->lockHandle(),
|
---|
6332 | aTarget->lockHandle() };
|
---|
6333 | /* Only add parent to the lock if it is not null */
|
---|
6334 | if (!aParent.isNull())
|
---|
6335 | pHandles[cHandles++] = aParent->lockHandle();
|
---|
6336 | AutoWriteLock alock(cHandles,
|
---|
6337 | pHandles
|
---|
6338 | COMMA_LOCKVAL_SRC_POS);
|
---|
6339 |
|
---|
6340 | if ( aTarget->m->state != MediumState_NotCreated
|
---|
6341 | && aTarget->m->state != MediumState_Created)
|
---|
6342 | throw aTarget->i_setStateError();
|
---|
6343 |
|
---|
6344 | /* Build the source lock list. */
|
---|
6345 | MediumLockList *pSourceMediumLockList(new MediumLockList());
|
---|
6346 | alock.release();
|
---|
6347 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
6348 | NULL /* pToLockWrite */,
|
---|
6349 | false /* fMediumLockWriteAll */,
|
---|
6350 | NULL,
|
---|
6351 | *pSourceMediumLockList);
|
---|
6352 | alock.acquire();
|
---|
6353 | if (FAILED(rc))
|
---|
6354 | {
|
---|
6355 | delete pSourceMediumLockList;
|
---|
6356 | throw rc;
|
---|
6357 | }
|
---|
6358 |
|
---|
6359 | /* Build the target lock list (including the to-be parent chain). */
|
---|
6360 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
6361 | alock.release();
|
---|
6362 | rc = aTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
6363 | aTarget /* pToLockWrite */,
|
---|
6364 | false /* fMediumLockWriteAll */,
|
---|
6365 | aParent,
|
---|
6366 | *pTargetMediumLockList);
|
---|
6367 | alock.acquire();
|
---|
6368 | if (FAILED(rc))
|
---|
6369 | {
|
---|
6370 | delete pSourceMediumLockList;
|
---|
6371 | delete pTargetMediumLockList;
|
---|
6372 | throw rc;
|
---|
6373 | }
|
---|
6374 |
|
---|
6375 | alock.release();
|
---|
6376 | rc = pSourceMediumLockList->Lock();
|
---|
6377 | alock.acquire();
|
---|
6378 | if (FAILED(rc))
|
---|
6379 | {
|
---|
6380 | delete pSourceMediumLockList;
|
---|
6381 | delete pTargetMediumLockList;
|
---|
6382 | throw setError(rc,
|
---|
6383 | tr("Failed to lock source media '%s'"),
|
---|
6384 | i_getLocationFull().c_str());
|
---|
6385 | }
|
---|
6386 | alock.release();
|
---|
6387 | rc = pTargetMediumLockList->Lock();
|
---|
6388 | alock.acquire();
|
---|
6389 | if (FAILED(rc))
|
---|
6390 | {
|
---|
6391 | delete pSourceMediumLockList;
|
---|
6392 | delete pTargetMediumLockList;
|
---|
6393 | throw setError(rc,
|
---|
6394 | tr("Failed to lock target media '%s'"),
|
---|
6395 | aTarget->i_getLocationFull().c_str());
|
---|
6396 | }
|
---|
6397 |
|
---|
6398 | pProgress.createObject();
|
---|
6399 | rc = pProgress->init(m->pVirtualBox,
|
---|
6400 | static_cast <IMedium *>(this),
|
---|
6401 | BstrFmt(tr("Creating clone medium '%s'"), aTarget->m->strLocationFull.c_str()).raw(),
|
---|
6402 | TRUE /* aCancelable */);
|
---|
6403 | if (FAILED(rc))
|
---|
6404 | {
|
---|
6405 | delete pSourceMediumLockList;
|
---|
6406 | delete pTargetMediumLockList;
|
---|
6407 | throw rc;
|
---|
6408 | }
|
---|
6409 |
|
---|
6410 | /* setup task object to carry out the operation asynchronously */
|
---|
6411 | pTask = new Medium::CloneTask(this, pProgress, aTarget,
|
---|
6412 | (MediumVariant_T)aVariant,
|
---|
6413 | aParent, idxSrcImageSame,
|
---|
6414 | idxDstImageSame, pSourceMediumLockList,
|
---|
6415 | pTargetMediumLockList);
|
---|
6416 | rc = pTask->rc();
|
---|
6417 | AssertComRC(rc);
|
---|
6418 | if (FAILED(rc))
|
---|
6419 | throw rc;
|
---|
6420 |
|
---|
6421 | if (aTarget->m->state == MediumState_NotCreated)
|
---|
6422 | aTarget->m->state = MediumState_Creating;
|
---|
6423 | }
|
---|
6424 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6425 |
|
---|
6426 | if (SUCCEEDED(rc))
|
---|
6427 | {
|
---|
6428 | rc = pTask->createThread();
|
---|
6429 |
|
---|
6430 | if (SUCCEEDED(rc))
|
---|
6431 | pProgress.queryInterfaceTo(aProgress);
|
---|
6432 | }
|
---|
6433 | else if (pTask != NULL)
|
---|
6434 | delete pTask;
|
---|
6435 |
|
---|
6436 | return rc;
|
---|
6437 | }
|
---|
6438 |
|
---|
6439 | /**
|
---|
6440 | * Returns the key identifier for this medium if encryption is configured.
|
---|
6441 | *
|
---|
6442 | * @returns Key identifier or empty string if no encryption is configured.
|
---|
6443 | */
|
---|
6444 | const Utf8Str& Medium::i_getKeyId()
|
---|
6445 | {
|
---|
6446 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
6447 |
|
---|
6448 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6449 |
|
---|
6450 | settings::StringsMap::const_iterator it = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
6451 | if (it == pBase->m->mapProperties.end())
|
---|
6452 | return Utf8Str::Empty;
|
---|
6453 |
|
---|
6454 | return it->second;
|
---|
6455 | }
|
---|
6456 |
|
---|
6457 | /**
|
---|
6458 | * Returns all filter related properties.
|
---|
6459 | *
|
---|
6460 | * @returns COM status code.
|
---|
6461 | * @param aReturnNames Where to store the properties names on success.
|
---|
6462 | * @param aReturnValues Where to store the properties values on success.
|
---|
6463 | */
|
---|
6464 | HRESULT Medium::i_getFilterProperties(std::vector<com::Utf8Str> &aReturnNames,
|
---|
6465 | std::vector<com::Utf8Str> &aReturnValues)
|
---|
6466 | {
|
---|
6467 | std::vector<com::Utf8Str> aPropNames;
|
---|
6468 | std::vector<com::Utf8Str> aPropValues;
|
---|
6469 | HRESULT hrc = getProperties(Utf8Str(""), aPropNames, aPropValues);
|
---|
6470 |
|
---|
6471 | if (SUCCEEDED(hrc))
|
---|
6472 | {
|
---|
6473 | unsigned cReturnSize = 0;
|
---|
6474 | aReturnNames.resize(0);
|
---|
6475 | aReturnValues.resize(0);
|
---|
6476 | for (unsigned idx = 0; idx < aPropNames.size(); idx++)
|
---|
6477 | {
|
---|
6478 | if (i_isPropertyForFilter(aPropNames[idx]))
|
---|
6479 | {
|
---|
6480 | aReturnNames.resize(cReturnSize + 1);
|
---|
6481 | aReturnValues.resize(cReturnSize + 1);
|
---|
6482 | aReturnNames[cReturnSize] = aPropNames[idx];
|
---|
6483 | aReturnValues[cReturnSize] = aPropValues[idx];
|
---|
6484 | cReturnSize++;
|
---|
6485 | }
|
---|
6486 | }
|
---|
6487 | }
|
---|
6488 |
|
---|
6489 | return hrc;
|
---|
6490 | }
|
---|
6491 |
|
---|
6492 | /**
|
---|
6493 | * Preparation to move this medium to a new location
|
---|
6494 | *
|
---|
6495 | * @param aLocation Location of the storage unit. If the location is a FS-path,
|
---|
6496 | * then it can be relative to the VirtualBox home directory.
|
---|
6497 | *
|
---|
6498 | * @note Must be called from under this object's write lock.
|
---|
6499 | */
|
---|
6500 | HRESULT Medium::i_preparationForMoving(const Utf8Str &aLocation)
|
---|
6501 | {
|
---|
6502 | HRESULT rc = E_FAIL;
|
---|
6503 |
|
---|
6504 | if (i_getLocationFull() != aLocation)
|
---|
6505 | {
|
---|
6506 | m->strNewLocationFull = aLocation;
|
---|
6507 | m->fMoveThisMedium = true;
|
---|
6508 | rc = S_OK;
|
---|
6509 | }
|
---|
6510 |
|
---|
6511 | return rc;
|
---|
6512 | }
|
---|
6513 |
|
---|
6514 | /**
|
---|
6515 | * Checking whether current operation "moving" or not
|
---|
6516 | */
|
---|
6517 | bool Medium::i_isMoveOperation(const ComObjPtr<Medium> &aTarget) const
|
---|
6518 | {
|
---|
6519 | RT_NOREF(aTarget);
|
---|
6520 | return (m->fMoveThisMedium == true) ? true:false;
|
---|
6521 | }
|
---|
6522 |
|
---|
6523 | bool Medium::i_resetMoveOperationData()
|
---|
6524 | {
|
---|
6525 | m->strNewLocationFull.setNull();
|
---|
6526 | m->fMoveThisMedium = false;
|
---|
6527 | return true;
|
---|
6528 | }
|
---|
6529 |
|
---|
6530 | Utf8Str Medium::i_getNewLocationForMoving() const
|
---|
6531 | {
|
---|
6532 | if (m->fMoveThisMedium == true)
|
---|
6533 | return m->strNewLocationFull;
|
---|
6534 | else
|
---|
6535 | return Utf8Str();
|
---|
6536 | }
|
---|
6537 | ////////////////////////////////////////////////////////////////////////////////
|
---|
6538 | //
|
---|
6539 | // Private methods
|
---|
6540 | //
|
---|
6541 | ////////////////////////////////////////////////////////////////////////////////
|
---|
6542 |
|
---|
6543 | /**
|
---|
6544 | * Queries information from the medium.
|
---|
6545 | *
|
---|
6546 | * As a result of this call, the accessibility state and data members such as
|
---|
6547 | * size and description will be updated with the current information.
|
---|
6548 | *
|
---|
6549 | * @note This method may block during a system I/O call that checks storage
|
---|
6550 | * accessibility.
|
---|
6551 | *
|
---|
6552 | * @note Caller MUST NOT hold the media tree or medium lock.
|
---|
6553 | *
|
---|
6554 | * @note Locks m->pParent for reading. Locks this object for writing.
|
---|
6555 | *
|
---|
6556 | * @param fSetImageId Whether to reset the UUID contained in the image file
|
---|
6557 | * to the UUID in the medium instance data (see SetIDs())
|
---|
6558 | * @param fSetParentId Whether to reset the parent UUID contained in the image
|
---|
6559 | * file to the parent UUID in the medium instance data (see
|
---|
6560 | * SetIDs())
|
---|
6561 | * @param autoCaller
|
---|
6562 | * @return
|
---|
6563 | */
|
---|
6564 | HRESULT Medium::i_queryInfo(bool fSetImageId, bool fSetParentId, AutoCaller &autoCaller)
|
---|
6565 | {
|
---|
6566 | Assert(!isWriteLockOnCurrentThread());
|
---|
6567 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6568 |
|
---|
6569 | if ( ( m->state != MediumState_Created
|
---|
6570 | && m->state != MediumState_Inaccessible
|
---|
6571 | && m->state != MediumState_LockedRead)
|
---|
6572 | || m->fClosing)
|
---|
6573 | return E_FAIL;
|
---|
6574 |
|
---|
6575 | HRESULT rc = S_OK;
|
---|
6576 |
|
---|
6577 | int vrc = VINF_SUCCESS;
|
---|
6578 |
|
---|
6579 | /* check if a blocking i_queryInfo() call is in progress on some other thread,
|
---|
6580 | * and wait for it to finish if so instead of querying data ourselves */
|
---|
6581 | if (m->queryInfoRunning)
|
---|
6582 | {
|
---|
6583 | Assert( m->state == MediumState_LockedRead
|
---|
6584 | || m->state == MediumState_LockedWrite);
|
---|
6585 |
|
---|
6586 | while (m->queryInfoRunning)
|
---|
6587 | {
|
---|
6588 | alock.release();
|
---|
6589 | /* must not hold the object lock now */
|
---|
6590 | Assert(!isWriteLockOnCurrentThread());
|
---|
6591 | {
|
---|
6592 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
6593 | }
|
---|
6594 | alock.acquire();
|
---|
6595 | }
|
---|
6596 |
|
---|
6597 | return S_OK;
|
---|
6598 | }
|
---|
6599 |
|
---|
6600 | bool success = false;
|
---|
6601 | Utf8Str lastAccessError;
|
---|
6602 |
|
---|
6603 | /* are we dealing with a new medium constructed using the existing
|
---|
6604 | * location? */
|
---|
6605 | bool isImport = m->id.isZero();
|
---|
6606 | unsigned uOpenFlags = VD_OPEN_FLAGS_INFO;
|
---|
6607 |
|
---|
6608 | /* Note that we don't use VD_OPEN_FLAGS_READONLY when opening new
|
---|
6609 | * media because that would prevent necessary modifications
|
---|
6610 | * when opening media of some third-party formats for the first
|
---|
6611 | * time in VirtualBox (such as VMDK for which VDOpen() needs to
|
---|
6612 | * generate an UUID if it is missing) */
|
---|
6613 | if ( m->hddOpenMode == OpenReadOnly
|
---|
6614 | || m->type == MediumType_Readonly
|
---|
6615 | || (!isImport && !fSetImageId && !fSetParentId)
|
---|
6616 | )
|
---|
6617 | uOpenFlags |= VD_OPEN_FLAGS_READONLY;
|
---|
6618 |
|
---|
6619 | /* Open shareable medium with the appropriate flags */
|
---|
6620 | if (m->type == MediumType_Shareable)
|
---|
6621 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
6622 |
|
---|
6623 | /* Lock the medium, which makes the behavior much more consistent, must be
|
---|
6624 | * done before dropping the object lock and setting queryInfoRunning. */
|
---|
6625 | ComPtr<IToken> pToken;
|
---|
6626 | if (uOpenFlags & (VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SHAREABLE))
|
---|
6627 | rc = LockRead(pToken.asOutParam());
|
---|
6628 | else
|
---|
6629 | rc = LockWrite(pToken.asOutParam());
|
---|
6630 | if (FAILED(rc)) return rc;
|
---|
6631 |
|
---|
6632 | /* Copies of the input state fields which are not read-only,
|
---|
6633 | * as we're dropping the lock. CAUTION: be extremely careful what
|
---|
6634 | * you do with the contents of this medium object, as you will
|
---|
6635 | * create races if there are concurrent changes. */
|
---|
6636 | Utf8Str format(m->strFormat);
|
---|
6637 | Utf8Str location(m->strLocationFull);
|
---|
6638 | ComObjPtr<MediumFormat> formatObj = m->formatObj;
|
---|
6639 |
|
---|
6640 | /* "Output" values which can't be set because the lock isn't held
|
---|
6641 | * at the time the values are determined. */
|
---|
6642 | Guid mediumId = m->id;
|
---|
6643 | uint64_t mediumSize = 0;
|
---|
6644 | uint64_t mediumLogicalSize = 0;
|
---|
6645 |
|
---|
6646 | /* Flag whether a base image has a non-zero parent UUID and thus
|
---|
6647 | * need repairing after it was closed again. */
|
---|
6648 | bool fRepairImageZeroParentUuid = false;
|
---|
6649 |
|
---|
6650 | ComObjPtr<VirtualBox> pVirtualBox = m->pVirtualBox;
|
---|
6651 |
|
---|
6652 | /* must be set before leaving the object lock the first time */
|
---|
6653 | m->queryInfoRunning = true;
|
---|
6654 |
|
---|
6655 | /* must leave object lock now, because a lock from a higher lock class
|
---|
6656 | * is needed and also a lengthy operation is coming */
|
---|
6657 | alock.release();
|
---|
6658 | autoCaller.release();
|
---|
6659 |
|
---|
6660 | /* Note that taking the queryInfoSem after leaving the object lock above
|
---|
6661 | * can lead to short spinning of the loops waiting for i_queryInfo() to
|
---|
6662 | * complete. This is unavoidable since the other order causes a lock order
|
---|
6663 | * violation: here it would be requesting the object lock (at the beginning
|
---|
6664 | * of the method), then queryInfoSem, and below the other way round. */
|
---|
6665 | AutoWriteLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
6666 |
|
---|
6667 | /* take the opportunity to have a media tree lock, released initially */
|
---|
6668 | Assert(!isWriteLockOnCurrentThread());
|
---|
6669 | Assert(!pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
6670 | AutoWriteLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
6671 | treeLock.release();
|
---|
6672 |
|
---|
6673 | /* re-take the caller, but not the object lock, to keep uninit away */
|
---|
6674 | autoCaller.add();
|
---|
6675 | if (FAILED(autoCaller.rc()))
|
---|
6676 | {
|
---|
6677 | m->queryInfoRunning = false;
|
---|
6678 | return autoCaller.rc();
|
---|
6679 | }
|
---|
6680 |
|
---|
6681 | try
|
---|
6682 | {
|
---|
6683 | /* skip accessibility checks for host drives */
|
---|
6684 | if (m->hostDrive)
|
---|
6685 | {
|
---|
6686 | success = true;
|
---|
6687 | throw S_OK;
|
---|
6688 | }
|
---|
6689 |
|
---|
6690 | PVDISK hdd;
|
---|
6691 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
6692 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6693 |
|
---|
6694 | try
|
---|
6695 | {
|
---|
6696 | /** @todo This kind of opening of media is assuming that diff
|
---|
6697 | * media can be opened as base media. Should be documented that
|
---|
6698 | * it must work for all medium format backends. */
|
---|
6699 | vrc = VDOpen(hdd,
|
---|
6700 | format.c_str(),
|
---|
6701 | location.c_str(),
|
---|
6702 | uOpenFlags | m->uOpenFlagsDef,
|
---|
6703 | m->vdImageIfaces);
|
---|
6704 | if (RT_FAILURE(vrc))
|
---|
6705 | {
|
---|
6706 | lastAccessError = Utf8StrFmt(tr("Could not open the medium '%s'%s"),
|
---|
6707 | location.c_str(), i_vdError(vrc).c_str());
|
---|
6708 | throw S_OK;
|
---|
6709 | }
|
---|
6710 |
|
---|
6711 | if (formatObj->i_getCapabilities() & MediumFormatCapabilities_Uuid)
|
---|
6712 | {
|
---|
6713 | /* Modify the UUIDs if necessary. The associated fields are
|
---|
6714 | * not modified by other code, so no need to copy. */
|
---|
6715 | if (fSetImageId)
|
---|
6716 | {
|
---|
6717 | alock.acquire();
|
---|
6718 | vrc = VDSetUuid(hdd, 0, m->uuidImage.raw());
|
---|
6719 | alock.release();
|
---|
6720 | if (RT_FAILURE(vrc))
|
---|
6721 | {
|
---|
6722 | lastAccessError = Utf8StrFmt(tr("Could not update the UUID of medium '%s'%s"),
|
---|
6723 | location.c_str(), i_vdError(vrc).c_str());
|
---|
6724 | throw S_OK;
|
---|
6725 | }
|
---|
6726 | mediumId = m->uuidImage;
|
---|
6727 | }
|
---|
6728 | if (fSetParentId)
|
---|
6729 | {
|
---|
6730 | alock.acquire();
|
---|
6731 | vrc = VDSetParentUuid(hdd, 0, m->uuidParentImage.raw());
|
---|
6732 | alock.release();
|
---|
6733 | if (RT_FAILURE(vrc))
|
---|
6734 | {
|
---|
6735 | lastAccessError = Utf8StrFmt(tr("Could not update the parent UUID of medium '%s'%s"),
|
---|
6736 | location.c_str(), i_vdError(vrc).c_str());
|
---|
6737 | throw S_OK;
|
---|
6738 | }
|
---|
6739 | }
|
---|
6740 | /* zap the information, these are no long-term members */
|
---|
6741 | alock.acquire();
|
---|
6742 | unconst(m->uuidImage).clear();
|
---|
6743 | unconst(m->uuidParentImage).clear();
|
---|
6744 | alock.release();
|
---|
6745 |
|
---|
6746 | /* check the UUID */
|
---|
6747 | RTUUID uuid;
|
---|
6748 | vrc = VDGetUuid(hdd, 0, &uuid);
|
---|
6749 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6750 |
|
---|
6751 | if (isImport)
|
---|
6752 | {
|
---|
6753 | mediumId = uuid;
|
---|
6754 |
|
---|
6755 | if (mediumId.isZero() && (m->hddOpenMode == OpenReadOnly))
|
---|
6756 | // only when importing a VDMK that has no UUID, create one in memory
|
---|
6757 | mediumId.create();
|
---|
6758 | }
|
---|
6759 | else
|
---|
6760 | {
|
---|
6761 | Assert(!mediumId.isZero());
|
---|
6762 |
|
---|
6763 | if (mediumId != uuid)
|
---|
6764 | {
|
---|
6765 | /** @todo r=klaus this always refers to VirtualBox.xml as the medium registry, even for new VMs */
|
---|
6766 | lastAccessError = Utf8StrFmt(
|
---|
6767 | tr("UUID {%RTuuid} of the medium '%s' does not match the value {%RTuuid} stored in the media registry ('%s')"),
|
---|
6768 | &uuid,
|
---|
6769 | location.c_str(),
|
---|
6770 | mediumId.raw(),
|
---|
6771 | pVirtualBox->i_settingsFilePath().c_str());
|
---|
6772 | throw S_OK;
|
---|
6773 | }
|
---|
6774 | }
|
---|
6775 | }
|
---|
6776 | else
|
---|
6777 | {
|
---|
6778 | /* the backend does not support storing UUIDs within the
|
---|
6779 | * underlying storage so use what we store in XML */
|
---|
6780 |
|
---|
6781 | if (fSetImageId)
|
---|
6782 | {
|
---|
6783 | /* set the UUID if an API client wants to change it */
|
---|
6784 | alock.acquire();
|
---|
6785 | mediumId = m->uuidImage;
|
---|
6786 | alock.release();
|
---|
6787 | }
|
---|
6788 | else if (isImport)
|
---|
6789 | {
|
---|
6790 | /* generate an UUID for an imported UUID-less medium */
|
---|
6791 | mediumId.create();
|
---|
6792 | }
|
---|
6793 | }
|
---|
6794 |
|
---|
6795 | /* set the image uuid before the below parent uuid handling code
|
---|
6796 | * might place it somewhere in the media tree, so that the medium
|
---|
6797 | * UUID is valid at this point */
|
---|
6798 | alock.acquire();
|
---|
6799 | if (isImport || fSetImageId)
|
---|
6800 | unconst(m->id) = mediumId;
|
---|
6801 | alock.release();
|
---|
6802 |
|
---|
6803 | /* get the medium variant */
|
---|
6804 | unsigned uImageFlags;
|
---|
6805 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
6806 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6807 | alock.acquire();
|
---|
6808 | m->variant = (MediumVariant_T)uImageFlags;
|
---|
6809 | alock.release();
|
---|
6810 |
|
---|
6811 | /* check/get the parent uuid and update corresponding state */
|
---|
6812 | if (uImageFlags & VD_IMAGE_FLAGS_DIFF)
|
---|
6813 | {
|
---|
6814 | RTUUID parentId;
|
---|
6815 | vrc = VDGetParentUuid(hdd, 0, &parentId);
|
---|
6816 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6817 |
|
---|
6818 | /* streamOptimized VMDK images are only accepted as base
|
---|
6819 | * images, as this allows automatic repair of OVF appliances.
|
---|
6820 | * Since such images don't support random writes they will not
|
---|
6821 | * be created for diff images. Only an overly smart user might
|
---|
6822 | * manually create this case. Too bad for him. */
|
---|
6823 | if ( (isImport || fSetParentId)
|
---|
6824 | && !(uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
|
---|
6825 | {
|
---|
6826 | /* the parent must be known to us. Note that we freely
|
---|
6827 | * call locking methods of mVirtualBox and parent, as all
|
---|
6828 | * relevant locks must be already held. There may be no
|
---|
6829 | * concurrent access to the just opened medium on other
|
---|
6830 | * threads yet (and init() will fail if this method reports
|
---|
6831 | * MediumState_Inaccessible) */
|
---|
6832 |
|
---|
6833 | ComObjPtr<Medium> pParent;
|
---|
6834 | if (RTUuidIsNull(&parentId))
|
---|
6835 | rc = VBOX_E_OBJECT_NOT_FOUND;
|
---|
6836 | else
|
---|
6837 | rc = pVirtualBox->i_findHardDiskById(Guid(parentId), false /* aSetError */, &pParent);
|
---|
6838 | if (FAILED(rc))
|
---|
6839 | {
|
---|
6840 | if (fSetImageId && !fSetParentId)
|
---|
6841 | {
|
---|
6842 | /* If the image UUID gets changed for an existing
|
---|
6843 | * image then the parent UUID can be stale. In such
|
---|
6844 | * cases clear the parent information. The parent
|
---|
6845 | * information may/will be re-set later if the
|
---|
6846 | * API client wants to adjust a complete medium
|
---|
6847 | * hierarchy one by one. */
|
---|
6848 | rc = S_OK;
|
---|
6849 | alock.acquire();
|
---|
6850 | RTUuidClear(&parentId);
|
---|
6851 | vrc = VDSetParentUuid(hdd, 0, &parentId);
|
---|
6852 | alock.release();
|
---|
6853 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6854 | }
|
---|
6855 | else
|
---|
6856 | {
|
---|
6857 | lastAccessError = Utf8StrFmt(tr("Parent medium with UUID {%RTuuid} of the medium '%s' is not found in the media registry ('%s')"),
|
---|
6858 | &parentId, location.c_str(),
|
---|
6859 | pVirtualBox->i_settingsFilePath().c_str());
|
---|
6860 | throw S_OK;
|
---|
6861 | }
|
---|
6862 | }
|
---|
6863 |
|
---|
6864 | /* must drop the caller before taking the tree lock */
|
---|
6865 | autoCaller.release();
|
---|
6866 | /* we set m->pParent & children() */
|
---|
6867 | treeLock.acquire();
|
---|
6868 | autoCaller.add();
|
---|
6869 | if (FAILED(autoCaller.rc()))
|
---|
6870 | throw autoCaller.rc();
|
---|
6871 |
|
---|
6872 | if (m->pParent)
|
---|
6873 | i_deparent();
|
---|
6874 |
|
---|
6875 | if (!pParent.isNull())
|
---|
6876 | if (pParent->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
6877 | {
|
---|
6878 | AutoReadLock plock(pParent COMMA_LOCKVAL_SRC_POS);
|
---|
6879 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
6880 | 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"),
|
---|
6881 | pParent->m->strLocationFull.c_str());
|
---|
6882 | }
|
---|
6883 | i_setParent(pParent);
|
---|
6884 |
|
---|
6885 | treeLock.release();
|
---|
6886 | }
|
---|
6887 | else
|
---|
6888 | {
|
---|
6889 | /* must drop the caller before taking the tree lock */
|
---|
6890 | autoCaller.release();
|
---|
6891 | /* we access m->pParent */
|
---|
6892 | treeLock.acquire();
|
---|
6893 | autoCaller.add();
|
---|
6894 | if (FAILED(autoCaller.rc()))
|
---|
6895 | throw autoCaller.rc();
|
---|
6896 |
|
---|
6897 | /* check that parent UUIDs match. Note that there's no need
|
---|
6898 | * for the parent's AutoCaller (our lifetime is bound to
|
---|
6899 | * it) */
|
---|
6900 |
|
---|
6901 | if (m->pParent.isNull())
|
---|
6902 | {
|
---|
6903 | /* Due to a bug in VDCopy() in VirtualBox 3.0.0-3.0.14
|
---|
6904 | * and 3.1.0-3.1.8 there are base images out there
|
---|
6905 | * which have a non-zero parent UUID. No point in
|
---|
6906 | * complaining about them, instead automatically
|
---|
6907 | * repair the problem. Later we can bring back the
|
---|
6908 | * error message, but we should wait until really
|
---|
6909 | * most users have repaired their images, either with
|
---|
6910 | * VBoxFixHdd or this way. */
|
---|
6911 | #if 1
|
---|
6912 | fRepairImageZeroParentUuid = true;
|
---|
6913 | #else /* 0 */
|
---|
6914 | lastAccessError = Utf8StrFmt(
|
---|
6915 | tr("Medium type of '%s' is differencing but it is not associated with any parent medium in the media registry ('%s')"),
|
---|
6916 | location.c_str(),
|
---|
6917 | pVirtualBox->settingsFilePath().c_str());
|
---|
6918 | treeLock.release();
|
---|
6919 | throw S_OK;
|
---|
6920 | #endif /* 0 */
|
---|
6921 | }
|
---|
6922 |
|
---|
6923 | {
|
---|
6924 | autoCaller.release();
|
---|
6925 | AutoReadLock parentLock(m->pParent COMMA_LOCKVAL_SRC_POS);
|
---|
6926 | autoCaller.add();
|
---|
6927 | if (FAILED(autoCaller.rc()))
|
---|
6928 | throw autoCaller.rc();
|
---|
6929 |
|
---|
6930 | if ( !fRepairImageZeroParentUuid
|
---|
6931 | && m->pParent->i_getState() != MediumState_Inaccessible
|
---|
6932 | && m->pParent->i_getId() != parentId)
|
---|
6933 | {
|
---|
6934 | /** @todo r=klaus this always refers to VirtualBox.xml as the medium registry, even for new VMs */
|
---|
6935 | lastAccessError = Utf8StrFmt(
|
---|
6936 | tr("Parent UUID {%RTuuid} of the medium '%s' does not match UUID {%RTuuid} of its parent medium stored in the media registry ('%s')"),
|
---|
6937 | &parentId, location.c_str(),
|
---|
6938 | m->pParent->i_getId().raw(),
|
---|
6939 | pVirtualBox->i_settingsFilePath().c_str());
|
---|
6940 | parentLock.release();
|
---|
6941 | treeLock.release();
|
---|
6942 | throw S_OK;
|
---|
6943 | }
|
---|
6944 | }
|
---|
6945 |
|
---|
6946 | /// @todo NEWMEDIA what to do if the parent is not
|
---|
6947 | /// accessible while the diff is? Probably nothing. The
|
---|
6948 | /// real code will detect the mismatch anyway.
|
---|
6949 |
|
---|
6950 | treeLock.release();
|
---|
6951 | }
|
---|
6952 | }
|
---|
6953 |
|
---|
6954 | mediumSize = VDGetFileSize(hdd, 0);
|
---|
6955 | mediumLogicalSize = VDGetSize(hdd, 0);
|
---|
6956 |
|
---|
6957 | success = true;
|
---|
6958 | }
|
---|
6959 | catch (HRESULT aRC)
|
---|
6960 | {
|
---|
6961 | rc = aRC;
|
---|
6962 | }
|
---|
6963 |
|
---|
6964 | vrc = VDDestroy(hdd);
|
---|
6965 | if (RT_FAILURE(vrc))
|
---|
6966 | {
|
---|
6967 | lastAccessError = Utf8StrFmt(tr("Could not update and close the medium '%s'%s"),
|
---|
6968 | location.c_str(), i_vdError(vrc).c_str());
|
---|
6969 | success = false;
|
---|
6970 | throw S_OK;
|
---|
6971 | }
|
---|
6972 | }
|
---|
6973 | catch (HRESULT aRC)
|
---|
6974 | {
|
---|
6975 | rc = aRC;
|
---|
6976 | }
|
---|
6977 |
|
---|
6978 | autoCaller.release();
|
---|
6979 | treeLock.acquire();
|
---|
6980 | autoCaller.add();
|
---|
6981 | if (FAILED(autoCaller.rc()))
|
---|
6982 | {
|
---|
6983 | m->queryInfoRunning = false;
|
---|
6984 | return autoCaller.rc();
|
---|
6985 | }
|
---|
6986 | alock.acquire();
|
---|
6987 |
|
---|
6988 | if (success)
|
---|
6989 | {
|
---|
6990 | m->size = mediumSize;
|
---|
6991 | m->logicalSize = mediumLogicalSize;
|
---|
6992 | m->strLastAccessError.setNull();
|
---|
6993 | }
|
---|
6994 | else
|
---|
6995 | {
|
---|
6996 | m->strLastAccessError = lastAccessError;
|
---|
6997 | Log1WarningFunc(("'%s' is not accessible (error='%s', rc=%Rhrc, vrc=%Rrc)\n",
|
---|
6998 | location.c_str(), m->strLastAccessError.c_str(), rc, vrc));
|
---|
6999 | }
|
---|
7000 |
|
---|
7001 | /* Set the proper state according to the result of the check */
|
---|
7002 | if (success)
|
---|
7003 | m->preLockState = MediumState_Created;
|
---|
7004 | else
|
---|
7005 | m->preLockState = MediumState_Inaccessible;
|
---|
7006 |
|
---|
7007 | /* unblock anyone waiting for the i_queryInfo results */
|
---|
7008 | qlock.release();
|
---|
7009 | m->queryInfoRunning = false;
|
---|
7010 |
|
---|
7011 | pToken->Abandon();
|
---|
7012 | pToken.setNull();
|
---|
7013 |
|
---|
7014 | if (FAILED(rc)) return rc;
|
---|
7015 |
|
---|
7016 | /* If this is a base image which incorrectly has a parent UUID set,
|
---|
7017 | * repair the image now by zeroing the parent UUID. This is only done
|
---|
7018 | * when we have structural information from a config file, on import
|
---|
7019 | * this is not possible. If someone would accidentally call openMedium
|
---|
7020 | * with a diff image before the base is registered this would destroy
|
---|
7021 | * the diff. Not acceptable. */
|
---|
7022 | if (fRepairImageZeroParentUuid)
|
---|
7023 | {
|
---|
7024 | rc = LockWrite(pToken.asOutParam());
|
---|
7025 | if (FAILED(rc)) return rc;
|
---|
7026 |
|
---|
7027 | alock.release();
|
---|
7028 |
|
---|
7029 | try
|
---|
7030 | {
|
---|
7031 | PVDISK hdd;
|
---|
7032 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
7033 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7034 |
|
---|
7035 | try
|
---|
7036 | {
|
---|
7037 | vrc = VDOpen(hdd,
|
---|
7038 | format.c_str(),
|
---|
7039 | location.c_str(),
|
---|
7040 | (uOpenFlags & ~VD_OPEN_FLAGS_READONLY) | m->uOpenFlagsDef,
|
---|
7041 | m->vdImageIfaces);
|
---|
7042 | if (RT_FAILURE(vrc))
|
---|
7043 | throw S_OK;
|
---|
7044 |
|
---|
7045 | RTUUID zeroParentUuid;
|
---|
7046 | RTUuidClear(&zeroParentUuid);
|
---|
7047 | vrc = VDSetParentUuid(hdd, 0, &zeroParentUuid);
|
---|
7048 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7049 | }
|
---|
7050 | catch (HRESULT aRC)
|
---|
7051 | {
|
---|
7052 | rc = aRC;
|
---|
7053 | }
|
---|
7054 |
|
---|
7055 | VDDestroy(hdd);
|
---|
7056 | }
|
---|
7057 | catch (HRESULT aRC)
|
---|
7058 | {
|
---|
7059 | rc = aRC;
|
---|
7060 | }
|
---|
7061 |
|
---|
7062 | pToken->Abandon();
|
---|
7063 | pToken.setNull();
|
---|
7064 | if (FAILED(rc)) return rc;
|
---|
7065 | }
|
---|
7066 |
|
---|
7067 | return rc;
|
---|
7068 | }
|
---|
7069 |
|
---|
7070 | /**
|
---|
7071 | * Performs extra checks if the medium can be closed and returns S_OK in
|
---|
7072 | * this case. Otherwise, returns a respective error message. Called by
|
---|
7073 | * Close() under the medium tree lock and the medium lock.
|
---|
7074 | *
|
---|
7075 | * @note Also reused by Medium::Reset().
|
---|
7076 | *
|
---|
7077 | * @note Caller must hold the media tree write lock!
|
---|
7078 | */
|
---|
7079 | HRESULT Medium::i_canClose()
|
---|
7080 | {
|
---|
7081 | Assert(m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
7082 |
|
---|
7083 | if (i_getChildren().size() != 0)
|
---|
7084 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
7085 | tr("Cannot close medium '%s' because it has %d child media"),
|
---|
7086 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
7087 |
|
---|
7088 | return S_OK;
|
---|
7089 | }
|
---|
7090 |
|
---|
7091 | /**
|
---|
7092 | * Unregisters this medium with mVirtualBox. Called by close() under the medium tree lock.
|
---|
7093 | *
|
---|
7094 | * @note Caller must have locked the media tree lock for writing!
|
---|
7095 | */
|
---|
7096 | HRESULT Medium::i_unregisterWithVirtualBox()
|
---|
7097 | {
|
---|
7098 | /* Note that we need to de-associate ourselves from the parent to let
|
---|
7099 | * VirtualBox::i_unregisterMedium() properly save the registry */
|
---|
7100 |
|
---|
7101 | /* we modify m->pParent and access children */
|
---|
7102 | Assert(m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
7103 |
|
---|
7104 | Medium *pParentBackup = m->pParent;
|
---|
7105 | AssertReturn(i_getChildren().size() == 0, E_FAIL);
|
---|
7106 | if (m->pParent)
|
---|
7107 | i_deparent();
|
---|
7108 |
|
---|
7109 | HRESULT rc = m->pVirtualBox->i_unregisterMedium(this);
|
---|
7110 | if (FAILED(rc))
|
---|
7111 | {
|
---|
7112 | if (pParentBackup)
|
---|
7113 | {
|
---|
7114 | // re-associate with the parent as we are still relatives in the registry
|
---|
7115 | i_setParent(pParentBackup);
|
---|
7116 | }
|
---|
7117 | }
|
---|
7118 |
|
---|
7119 | return rc;
|
---|
7120 | }
|
---|
7121 |
|
---|
7122 | /**
|
---|
7123 | * Like SetProperty but do not trigger a settings store. Only for internal use!
|
---|
7124 | */
|
---|
7125 | HRESULT Medium::i_setPropertyDirect(const Utf8Str &aName, const Utf8Str &aValue)
|
---|
7126 | {
|
---|
7127 | AutoCaller autoCaller(this);
|
---|
7128 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
7129 |
|
---|
7130 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7131 |
|
---|
7132 | switch (m->state)
|
---|
7133 | {
|
---|
7134 | case MediumState_Created:
|
---|
7135 | case MediumState_Inaccessible:
|
---|
7136 | break;
|
---|
7137 | default:
|
---|
7138 | return i_setStateError();
|
---|
7139 | }
|
---|
7140 |
|
---|
7141 | m->mapProperties[aName] = aValue;
|
---|
7142 |
|
---|
7143 | return S_OK;
|
---|
7144 | }
|
---|
7145 |
|
---|
7146 | /**
|
---|
7147 | * Sets the extended error info according to the current media state.
|
---|
7148 | *
|
---|
7149 | * @note Must be called from under this object's write or read lock.
|
---|
7150 | */
|
---|
7151 | HRESULT Medium::i_setStateError()
|
---|
7152 | {
|
---|
7153 | HRESULT rc = E_FAIL;
|
---|
7154 |
|
---|
7155 | switch (m->state)
|
---|
7156 | {
|
---|
7157 | case MediumState_NotCreated:
|
---|
7158 | {
|
---|
7159 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7160 | tr("Storage for the medium '%s' is not created"),
|
---|
7161 | m->strLocationFull.c_str());
|
---|
7162 | break;
|
---|
7163 | }
|
---|
7164 | case MediumState_Created:
|
---|
7165 | {
|
---|
7166 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7167 | tr("Storage for the medium '%s' is already created"),
|
---|
7168 | m->strLocationFull.c_str());
|
---|
7169 | break;
|
---|
7170 | }
|
---|
7171 | case MediumState_LockedRead:
|
---|
7172 | {
|
---|
7173 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7174 | tr("Medium '%s' is locked for reading by another task"),
|
---|
7175 | m->strLocationFull.c_str());
|
---|
7176 | break;
|
---|
7177 | }
|
---|
7178 | case MediumState_LockedWrite:
|
---|
7179 | {
|
---|
7180 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7181 | tr("Medium '%s' is locked for writing by another task"),
|
---|
7182 | m->strLocationFull.c_str());
|
---|
7183 | break;
|
---|
7184 | }
|
---|
7185 | case MediumState_Inaccessible:
|
---|
7186 | {
|
---|
7187 | /* be in sync with Console::powerUpThread() */
|
---|
7188 | if (!m->strLastAccessError.isEmpty())
|
---|
7189 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7190 | tr("Medium '%s' is not accessible. %s"),
|
---|
7191 | m->strLocationFull.c_str(), m->strLastAccessError.c_str());
|
---|
7192 | else
|
---|
7193 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7194 | tr("Medium '%s' is not accessible"),
|
---|
7195 | m->strLocationFull.c_str());
|
---|
7196 | break;
|
---|
7197 | }
|
---|
7198 | case MediumState_Creating:
|
---|
7199 | {
|
---|
7200 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7201 | tr("Storage for the medium '%s' is being created"),
|
---|
7202 | m->strLocationFull.c_str());
|
---|
7203 | break;
|
---|
7204 | }
|
---|
7205 | case MediumState_Deleting:
|
---|
7206 | {
|
---|
7207 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7208 | tr("Storage for the medium '%s' is being deleted"),
|
---|
7209 | m->strLocationFull.c_str());
|
---|
7210 | break;
|
---|
7211 | }
|
---|
7212 | default:
|
---|
7213 | {
|
---|
7214 | AssertFailed();
|
---|
7215 | break;
|
---|
7216 | }
|
---|
7217 | }
|
---|
7218 |
|
---|
7219 | return rc;
|
---|
7220 | }
|
---|
7221 |
|
---|
7222 | /**
|
---|
7223 | * Sets the value of m->strLocationFull. The given location must be a fully
|
---|
7224 | * qualified path; relative paths are not supported here.
|
---|
7225 | *
|
---|
7226 | * As a special exception, if the specified location is a file path that ends with '/'
|
---|
7227 | * then the file name part will be generated by this method automatically in the format
|
---|
7228 | * '{\<uuid\>}.\<ext\>' where \<uuid\> is a fresh UUID that this method will generate
|
---|
7229 | * and assign to this medium, and \<ext\> is the default extension for this
|
---|
7230 | * medium's storage format. Note that this procedure requires the media state to
|
---|
7231 | * be NotCreated and will return a failure otherwise.
|
---|
7232 | *
|
---|
7233 | * @param aLocation Location of the storage unit. If the location is a FS-path,
|
---|
7234 | * then it can be relative to the VirtualBox home directory.
|
---|
7235 | * @param aFormat Optional fallback format if it is an import and the format
|
---|
7236 | * cannot be determined.
|
---|
7237 | *
|
---|
7238 | * @note Must be called from under this object's write lock.
|
---|
7239 | */
|
---|
7240 | HRESULT Medium::i_setLocation(const Utf8Str &aLocation,
|
---|
7241 | const Utf8Str &aFormat /* = Utf8Str::Empty */)
|
---|
7242 | {
|
---|
7243 | AssertReturn(!aLocation.isEmpty(), E_FAIL);
|
---|
7244 |
|
---|
7245 | AutoCaller autoCaller(this);
|
---|
7246 | AssertComRCReturnRC(autoCaller.rc());
|
---|
7247 |
|
---|
7248 | /* formatObj may be null only when initializing from an existing path and
|
---|
7249 | * no format is known yet */
|
---|
7250 | AssertReturn( (!m->strFormat.isEmpty() && !m->formatObj.isNull())
|
---|
7251 | || ( getObjectState().getState() == ObjectState::InInit
|
---|
7252 | && m->state != MediumState_NotCreated
|
---|
7253 | && m->id.isZero()
|
---|
7254 | && m->strFormat.isEmpty()
|
---|
7255 | && m->formatObj.isNull()),
|
---|
7256 | E_FAIL);
|
---|
7257 |
|
---|
7258 | /* are we dealing with a new medium constructed using the existing
|
---|
7259 | * location? */
|
---|
7260 | bool isImport = m->strFormat.isEmpty();
|
---|
7261 |
|
---|
7262 | if ( isImport
|
---|
7263 | || ( (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
7264 | && !m->hostDrive))
|
---|
7265 | {
|
---|
7266 | Guid id;
|
---|
7267 |
|
---|
7268 | Utf8Str locationFull(aLocation);
|
---|
7269 |
|
---|
7270 | if (m->state == MediumState_NotCreated)
|
---|
7271 | {
|
---|
7272 | /* must be a file (formatObj must be already known) */
|
---|
7273 | Assert(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File);
|
---|
7274 |
|
---|
7275 | if (RTPathFilename(aLocation.c_str()) == NULL)
|
---|
7276 | {
|
---|
7277 | /* no file name is given (either an empty string or ends with a
|
---|
7278 | * slash), generate a new UUID + file name if the state allows
|
---|
7279 | * this */
|
---|
7280 |
|
---|
7281 | ComAssertMsgRet(!m->formatObj->i_getFileExtensions().empty(),
|
---|
7282 | ("Must be at least one extension if it is MediumFormatCapabilities_File\n"),
|
---|
7283 | E_FAIL);
|
---|
7284 |
|
---|
7285 | Utf8Str strExt = m->formatObj->i_getFileExtensions().front();
|
---|
7286 | ComAssertMsgRet(!strExt.isEmpty(),
|
---|
7287 | ("Default extension must not be empty\n"),
|
---|
7288 | E_FAIL);
|
---|
7289 |
|
---|
7290 | id.create();
|
---|
7291 |
|
---|
7292 | locationFull = Utf8StrFmt("%s{%RTuuid}.%s",
|
---|
7293 | aLocation.c_str(), id.raw(), strExt.c_str());
|
---|
7294 | }
|
---|
7295 | }
|
---|
7296 |
|
---|
7297 | // we must always have full paths now (if it refers to a file)
|
---|
7298 | if ( ( m->formatObj.isNull()
|
---|
7299 | || m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
7300 | && !RTPathStartsWithRoot(locationFull.c_str()))
|
---|
7301 | return setError(VBOX_E_FILE_ERROR,
|
---|
7302 | tr("The given path '%s' is not fully qualified"),
|
---|
7303 | locationFull.c_str());
|
---|
7304 |
|
---|
7305 | /* detect the backend from the storage unit if importing */
|
---|
7306 | if (isImport)
|
---|
7307 | {
|
---|
7308 | VDTYPE enmType = VDTYPE_INVALID;
|
---|
7309 | char *backendName = NULL;
|
---|
7310 |
|
---|
7311 | int vrc = VINF_SUCCESS;
|
---|
7312 |
|
---|
7313 | /* is it a file? */
|
---|
7314 | {
|
---|
7315 | RTFILE file;
|
---|
7316 | vrc = RTFileOpen(&file, locationFull.c_str(), RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
7317 | if (RT_SUCCESS(vrc))
|
---|
7318 | RTFileClose(file);
|
---|
7319 | }
|
---|
7320 | if (RT_SUCCESS(vrc))
|
---|
7321 | {
|
---|
7322 | vrc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
|
---|
7323 | locationFull.c_str(), &backendName, &enmType);
|
---|
7324 | }
|
---|
7325 | else if ( vrc != VERR_FILE_NOT_FOUND
|
---|
7326 | && vrc != VERR_PATH_NOT_FOUND
|
---|
7327 | && vrc != VERR_ACCESS_DENIED
|
---|
7328 | && locationFull != aLocation)
|
---|
7329 | {
|
---|
7330 | /* assume it's not a file, restore the original location */
|
---|
7331 | locationFull = aLocation;
|
---|
7332 | vrc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
|
---|
7333 | locationFull.c_str(), &backendName, &enmType);
|
---|
7334 | }
|
---|
7335 |
|
---|
7336 | if (RT_FAILURE(vrc))
|
---|
7337 | {
|
---|
7338 | if (vrc == VERR_ACCESS_DENIED)
|
---|
7339 | return setError(VBOX_E_FILE_ERROR,
|
---|
7340 | tr("Permission problem accessing the file for the medium '%s' (%Rrc)"),
|
---|
7341 | locationFull.c_str(), vrc);
|
---|
7342 | else if (vrc == VERR_FILE_NOT_FOUND || vrc == VERR_PATH_NOT_FOUND)
|
---|
7343 | return setError(VBOX_E_FILE_ERROR,
|
---|
7344 | tr("Could not find file for the medium '%s' (%Rrc)"),
|
---|
7345 | locationFull.c_str(), vrc);
|
---|
7346 | else if (aFormat.isEmpty())
|
---|
7347 | return setError(VBOX_E_IPRT_ERROR,
|
---|
7348 | tr("Could not get the storage format of the medium '%s' (%Rrc)"),
|
---|
7349 | locationFull.c_str(), vrc);
|
---|
7350 | else
|
---|
7351 | {
|
---|
7352 | HRESULT rc = i_setFormat(aFormat);
|
---|
7353 | /* setFormat() must not fail since we've just used the backend so
|
---|
7354 | * the format object must be there */
|
---|
7355 | AssertComRCReturnRC(rc);
|
---|
7356 | }
|
---|
7357 | }
|
---|
7358 | else if ( enmType == VDTYPE_INVALID
|
---|
7359 | || m->devType != i_convertToDeviceType(enmType))
|
---|
7360 | {
|
---|
7361 | /*
|
---|
7362 | * The user tried to use a image as a device which is not supported
|
---|
7363 | * by the backend.
|
---|
7364 | */
|
---|
7365 | return setError(E_FAIL,
|
---|
7366 | tr("The medium '%s' can't be used as the requested device type"),
|
---|
7367 | locationFull.c_str());
|
---|
7368 | }
|
---|
7369 | else
|
---|
7370 | {
|
---|
7371 | ComAssertRet(backendName != NULL && *backendName != '\0', E_FAIL);
|
---|
7372 |
|
---|
7373 | HRESULT rc = i_setFormat(backendName);
|
---|
7374 | RTStrFree(backendName);
|
---|
7375 |
|
---|
7376 | /* setFormat() must not fail since we've just used the backend so
|
---|
7377 | * the format object must be there */
|
---|
7378 | AssertComRCReturnRC(rc);
|
---|
7379 | }
|
---|
7380 | }
|
---|
7381 |
|
---|
7382 | m->strLocationFull = locationFull;
|
---|
7383 |
|
---|
7384 | /* is it still a file? */
|
---|
7385 | if ( (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
7386 | && (m->state == MediumState_NotCreated)
|
---|
7387 | )
|
---|
7388 | /* assign a new UUID (this UUID will be used when calling
|
---|
7389 | * VDCreateBase/VDCreateDiff as a wanted UUID). Note that we
|
---|
7390 | * also do that if we didn't generate it to make sure it is
|
---|
7391 | * either generated by us or reset to null */
|
---|
7392 | unconst(m->id) = id;
|
---|
7393 | }
|
---|
7394 | else
|
---|
7395 | m->strLocationFull = aLocation;
|
---|
7396 |
|
---|
7397 | return S_OK;
|
---|
7398 | }
|
---|
7399 |
|
---|
7400 | /**
|
---|
7401 | * Checks that the format ID is valid and sets it on success.
|
---|
7402 | *
|
---|
7403 | * Note that this method will caller-reference the format object on success!
|
---|
7404 | * This reference must be released somewhere to let the MediumFormat object be
|
---|
7405 | * uninitialized.
|
---|
7406 | *
|
---|
7407 | * @note Must be called from under this object's write lock.
|
---|
7408 | */
|
---|
7409 | HRESULT Medium::i_setFormat(const Utf8Str &aFormat)
|
---|
7410 | {
|
---|
7411 | /* get the format object first */
|
---|
7412 | {
|
---|
7413 | SystemProperties *pSysProps = m->pVirtualBox->i_getSystemProperties();
|
---|
7414 | AutoReadLock propsLock(pSysProps COMMA_LOCKVAL_SRC_POS);
|
---|
7415 |
|
---|
7416 | unconst(m->formatObj) = pSysProps->i_mediumFormat(aFormat);
|
---|
7417 | if (m->formatObj.isNull())
|
---|
7418 | return setError(E_INVALIDARG,
|
---|
7419 | tr("Invalid medium storage format '%s'"),
|
---|
7420 | aFormat.c_str());
|
---|
7421 |
|
---|
7422 | /* get properties (preinsert them as keys in the map). Note that the
|
---|
7423 | * map doesn't grow over the object life time since the set of
|
---|
7424 | * properties is meant to be constant. */
|
---|
7425 |
|
---|
7426 | Assert(m->mapProperties.empty());
|
---|
7427 |
|
---|
7428 | for (MediumFormat::PropertyArray::const_iterator it = m->formatObj->i_getProperties().begin();
|
---|
7429 | it != m->formatObj->i_getProperties().end();
|
---|
7430 | ++it)
|
---|
7431 | {
|
---|
7432 | m->mapProperties.insert(std::make_pair(it->strName, Utf8Str::Empty));
|
---|
7433 | }
|
---|
7434 | }
|
---|
7435 |
|
---|
7436 | unconst(m->strFormat) = aFormat;
|
---|
7437 |
|
---|
7438 | return S_OK;
|
---|
7439 | }
|
---|
7440 |
|
---|
7441 | /**
|
---|
7442 | * Converts the Medium device type to the VD type.
|
---|
7443 | */
|
---|
7444 | VDTYPE Medium::i_convertDeviceType()
|
---|
7445 | {
|
---|
7446 | VDTYPE enmType;
|
---|
7447 |
|
---|
7448 | switch (m->devType)
|
---|
7449 | {
|
---|
7450 | case DeviceType_HardDisk:
|
---|
7451 | enmType = VDTYPE_HDD;
|
---|
7452 | break;
|
---|
7453 | case DeviceType_DVD:
|
---|
7454 | enmType = VDTYPE_OPTICAL_DISC;
|
---|
7455 | break;
|
---|
7456 | case DeviceType_Floppy:
|
---|
7457 | enmType = VDTYPE_FLOPPY;
|
---|
7458 | break;
|
---|
7459 | default:
|
---|
7460 | ComAssertFailedRet(VDTYPE_INVALID);
|
---|
7461 | }
|
---|
7462 |
|
---|
7463 | return enmType;
|
---|
7464 | }
|
---|
7465 |
|
---|
7466 | /**
|
---|
7467 | * Converts from the VD type to the medium type.
|
---|
7468 | */
|
---|
7469 | DeviceType_T Medium::i_convertToDeviceType(VDTYPE enmType)
|
---|
7470 | {
|
---|
7471 | DeviceType_T devType;
|
---|
7472 |
|
---|
7473 | switch (enmType)
|
---|
7474 | {
|
---|
7475 | case VDTYPE_HDD:
|
---|
7476 | devType = DeviceType_HardDisk;
|
---|
7477 | break;
|
---|
7478 | case VDTYPE_OPTICAL_DISC:
|
---|
7479 | devType = DeviceType_DVD;
|
---|
7480 | break;
|
---|
7481 | case VDTYPE_FLOPPY:
|
---|
7482 | devType = DeviceType_Floppy;
|
---|
7483 | break;
|
---|
7484 | default:
|
---|
7485 | ComAssertFailedRet(DeviceType_Null);
|
---|
7486 | }
|
---|
7487 |
|
---|
7488 | return devType;
|
---|
7489 | }
|
---|
7490 |
|
---|
7491 | /**
|
---|
7492 | * Internal method which checks whether a property name is for a filter plugin.
|
---|
7493 | */
|
---|
7494 | bool Medium::i_isPropertyForFilter(const com::Utf8Str &aName)
|
---|
7495 | {
|
---|
7496 | /* If the name contains "/" use the part before as a filter name and lookup the filter. */
|
---|
7497 | size_t offSlash;
|
---|
7498 | if ((offSlash = aName.find("/", 0)) != aName.npos)
|
---|
7499 | {
|
---|
7500 | com::Utf8Str strFilter;
|
---|
7501 | com::Utf8Str strKey;
|
---|
7502 |
|
---|
7503 | HRESULT rc = strFilter.assignEx(aName, 0, offSlash);
|
---|
7504 | if (FAILED(rc))
|
---|
7505 | return false;
|
---|
7506 |
|
---|
7507 | rc = strKey.assignEx(aName, offSlash + 1, aName.length() - offSlash - 1); /* Skip slash */
|
---|
7508 | if (FAILED(rc))
|
---|
7509 | return false;
|
---|
7510 |
|
---|
7511 | VDFILTERINFO FilterInfo;
|
---|
7512 | int vrc = VDFilterInfoOne(strFilter.c_str(), &FilterInfo);
|
---|
7513 | if (RT_SUCCESS(vrc))
|
---|
7514 | {
|
---|
7515 | /* Check that the property exists. */
|
---|
7516 | PCVDCONFIGINFO paConfig = FilterInfo.paConfigInfo;
|
---|
7517 | while (paConfig->pszKey)
|
---|
7518 | {
|
---|
7519 | if (strKey.equals(paConfig->pszKey))
|
---|
7520 | return true;
|
---|
7521 | paConfig++;
|
---|
7522 | }
|
---|
7523 | }
|
---|
7524 | }
|
---|
7525 |
|
---|
7526 | return false;
|
---|
7527 | }
|
---|
7528 |
|
---|
7529 | /**
|
---|
7530 | * Returns the last error message collected by the i_vdErrorCall callback and
|
---|
7531 | * resets it.
|
---|
7532 | *
|
---|
7533 | * The error message is returned prepended with a dot and a space, like this:
|
---|
7534 | * <code>
|
---|
7535 | * ". <error_text> (%Rrc)"
|
---|
7536 | * </code>
|
---|
7537 | * to make it easily appendable to a more general error message. The @c %Rrc
|
---|
7538 | * format string is given @a aVRC as an argument.
|
---|
7539 | *
|
---|
7540 | * If there is no last error message collected by i_vdErrorCall or if it is a
|
---|
7541 | * null or empty string, then this function returns the following text:
|
---|
7542 | * <code>
|
---|
7543 | * " (%Rrc)"
|
---|
7544 | * </code>
|
---|
7545 | *
|
---|
7546 | * @note Doesn't do any object locking; it is assumed that the caller makes sure
|
---|
7547 | * the callback isn't called by more than one thread at a time.
|
---|
7548 | *
|
---|
7549 | * @param aVRC VBox error code to use when no error message is provided.
|
---|
7550 | */
|
---|
7551 | Utf8Str Medium::i_vdError(int aVRC)
|
---|
7552 | {
|
---|
7553 | Utf8Str error;
|
---|
7554 |
|
---|
7555 | if (m->vdError.isEmpty())
|
---|
7556 | error = Utf8StrFmt(" (%Rrc)", aVRC);
|
---|
7557 | else
|
---|
7558 | error = Utf8StrFmt(".\n%s", m->vdError.c_str());
|
---|
7559 |
|
---|
7560 | m->vdError.setNull();
|
---|
7561 |
|
---|
7562 | return error;
|
---|
7563 | }
|
---|
7564 |
|
---|
7565 | /**
|
---|
7566 | * Error message callback.
|
---|
7567 | *
|
---|
7568 | * Puts the reported error message to the m->vdError field.
|
---|
7569 | *
|
---|
7570 | * @note Doesn't do any object locking; it is assumed that the caller makes sure
|
---|
7571 | * the callback isn't called by more than one thread at a time.
|
---|
7572 | *
|
---|
7573 | * @param pvUser The opaque data passed on container creation.
|
---|
7574 | * @param rc The VBox error code.
|
---|
7575 | * @param SRC_POS Use RT_SRC_POS.
|
---|
7576 | * @param pszFormat Error message format string.
|
---|
7577 | * @param va Error message arguments.
|
---|
7578 | */
|
---|
7579 | /*static*/
|
---|
7580 | DECLCALLBACK(void) Medium::i_vdErrorCall(void *pvUser, int rc, RT_SRC_POS_DECL,
|
---|
7581 | const char *pszFormat, va_list va)
|
---|
7582 | {
|
---|
7583 | NOREF(pszFile); NOREF(iLine); NOREF(pszFunction); /* RT_SRC_POS_DECL */
|
---|
7584 |
|
---|
7585 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
7586 | AssertReturnVoid(that != NULL);
|
---|
7587 |
|
---|
7588 | if (that->m->vdError.isEmpty())
|
---|
7589 | that->m->vdError =
|
---|
7590 | Utf8StrFmt("%s (%Rrc)", Utf8Str(pszFormat, va).c_str(), rc);
|
---|
7591 | else
|
---|
7592 | that->m->vdError =
|
---|
7593 | Utf8StrFmt("%s.\n%s (%Rrc)", that->m->vdError.c_str(),
|
---|
7594 | Utf8Str(pszFormat, va).c_str(), rc);
|
---|
7595 | }
|
---|
7596 |
|
---|
7597 | /* static */
|
---|
7598 | DECLCALLBACK(bool) Medium::i_vdConfigAreKeysValid(void *pvUser,
|
---|
7599 | const char * /* pszzValid */)
|
---|
7600 | {
|
---|
7601 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
7602 | AssertReturn(that != NULL, false);
|
---|
7603 |
|
---|
7604 | /* we always return true since the only keys we have are those found in
|
---|
7605 | * VDBACKENDINFO */
|
---|
7606 | return true;
|
---|
7607 | }
|
---|
7608 |
|
---|
7609 | /* static */
|
---|
7610 | DECLCALLBACK(int) Medium::i_vdConfigQuerySize(void *pvUser,
|
---|
7611 | const char *pszName,
|
---|
7612 | size_t *pcbValue)
|
---|
7613 | {
|
---|
7614 | AssertReturn(VALID_PTR(pcbValue), VERR_INVALID_POINTER);
|
---|
7615 |
|
---|
7616 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
7617 | AssertReturn(that != NULL, VERR_GENERAL_FAILURE);
|
---|
7618 |
|
---|
7619 | settings::StringsMap::const_iterator it = that->m->mapProperties.find(Utf8Str(pszName));
|
---|
7620 | if (it == that->m->mapProperties.end())
|
---|
7621 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7622 |
|
---|
7623 | /* we interpret null values as "no value" in Medium */
|
---|
7624 | if (it->second.isEmpty())
|
---|
7625 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7626 |
|
---|
7627 | *pcbValue = it->second.length() + 1 /* include terminator */;
|
---|
7628 |
|
---|
7629 | return VINF_SUCCESS;
|
---|
7630 | }
|
---|
7631 |
|
---|
7632 | /* static */
|
---|
7633 | DECLCALLBACK(int) Medium::i_vdConfigQuery(void *pvUser,
|
---|
7634 | const char *pszName,
|
---|
7635 | char *pszValue,
|
---|
7636 | size_t cchValue)
|
---|
7637 | {
|
---|
7638 | AssertReturn(VALID_PTR(pszValue), VERR_INVALID_POINTER);
|
---|
7639 |
|
---|
7640 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
7641 | AssertReturn(that != NULL, VERR_GENERAL_FAILURE);
|
---|
7642 |
|
---|
7643 | settings::StringsMap::const_iterator it = that->m->mapProperties.find(Utf8Str(pszName));
|
---|
7644 | if (it == that->m->mapProperties.end())
|
---|
7645 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7646 |
|
---|
7647 | /* we interpret null values as "no value" in Medium */
|
---|
7648 | if (it->second.isEmpty())
|
---|
7649 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7650 |
|
---|
7651 | const Utf8Str &value = it->second;
|
---|
7652 | if (value.length() >= cchValue)
|
---|
7653 | return VERR_CFGM_NOT_ENOUGH_SPACE;
|
---|
7654 |
|
---|
7655 | memcpy(pszValue, value.c_str(), value.length() + 1);
|
---|
7656 |
|
---|
7657 | return VINF_SUCCESS;
|
---|
7658 | }
|
---|
7659 |
|
---|
7660 | DECLCALLBACK(int) Medium::i_vdTcpSocketCreate(uint32_t fFlags, PVDSOCKET pSock)
|
---|
7661 | {
|
---|
7662 | PVDSOCKETINT pSocketInt = NULL;
|
---|
7663 |
|
---|
7664 | if ((fFlags & VD_INTERFACETCPNET_CONNECT_EXTENDED_SELECT) != 0)
|
---|
7665 | return VERR_NOT_SUPPORTED;
|
---|
7666 |
|
---|
7667 | pSocketInt = (PVDSOCKETINT)RTMemAllocZ(sizeof(VDSOCKETINT));
|
---|
7668 | if (!pSocketInt)
|
---|
7669 | return VERR_NO_MEMORY;
|
---|
7670 |
|
---|
7671 | pSocketInt->hSocket = NIL_RTSOCKET;
|
---|
7672 | *pSock = pSocketInt;
|
---|
7673 | return VINF_SUCCESS;
|
---|
7674 | }
|
---|
7675 |
|
---|
7676 | DECLCALLBACK(int) Medium::i_vdTcpSocketDestroy(VDSOCKET Sock)
|
---|
7677 | {
|
---|
7678 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7679 |
|
---|
7680 | if (pSocketInt->hSocket != NIL_RTSOCKET)
|
---|
7681 | RTTcpClientCloseEx(pSocketInt->hSocket, false /*fGracefulShutdown*/);
|
---|
7682 |
|
---|
7683 | RTMemFree(pSocketInt);
|
---|
7684 |
|
---|
7685 | return VINF_SUCCESS;
|
---|
7686 | }
|
---|
7687 |
|
---|
7688 | DECLCALLBACK(int) Medium::i_vdTcpClientConnect(VDSOCKET Sock, const char *pszAddress, uint32_t uPort,
|
---|
7689 | RTMSINTERVAL cMillies)
|
---|
7690 | {
|
---|
7691 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7692 |
|
---|
7693 | return RTTcpClientConnectEx(pszAddress, uPort, &pSocketInt->hSocket, cMillies, NULL);
|
---|
7694 | }
|
---|
7695 |
|
---|
7696 | DECLCALLBACK(int) Medium::i_vdTcpClientClose(VDSOCKET Sock)
|
---|
7697 | {
|
---|
7698 | int rc = VINF_SUCCESS;
|
---|
7699 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7700 |
|
---|
7701 | rc = RTTcpClientCloseEx(pSocketInt->hSocket, false /*fGracefulShutdown*/);
|
---|
7702 | pSocketInt->hSocket = NIL_RTSOCKET;
|
---|
7703 | return rc;
|
---|
7704 | }
|
---|
7705 |
|
---|
7706 | DECLCALLBACK(bool) Medium::i_vdTcpIsClientConnected(VDSOCKET Sock)
|
---|
7707 | {
|
---|
7708 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7709 | return pSocketInt->hSocket != NIL_RTSOCKET;
|
---|
7710 | }
|
---|
7711 |
|
---|
7712 | DECLCALLBACK(int) Medium::i_vdTcpSelectOne(VDSOCKET Sock, RTMSINTERVAL cMillies)
|
---|
7713 | {
|
---|
7714 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7715 | return RTTcpSelectOne(pSocketInt->hSocket, cMillies);
|
---|
7716 | }
|
---|
7717 |
|
---|
7718 | DECLCALLBACK(int) Medium::i_vdTcpRead(VDSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead)
|
---|
7719 | {
|
---|
7720 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7721 | return RTTcpRead(pSocketInt->hSocket, pvBuffer, cbBuffer, pcbRead);
|
---|
7722 | }
|
---|
7723 |
|
---|
7724 | DECLCALLBACK(int) Medium::i_vdTcpWrite(VDSOCKET Sock, const void *pvBuffer, size_t cbBuffer)
|
---|
7725 | {
|
---|
7726 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7727 | return RTTcpWrite(pSocketInt->hSocket, pvBuffer, cbBuffer);
|
---|
7728 | }
|
---|
7729 |
|
---|
7730 | DECLCALLBACK(int) Medium::i_vdTcpSgWrite(VDSOCKET Sock, PCRTSGBUF pSgBuf)
|
---|
7731 | {
|
---|
7732 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7733 | return RTTcpSgWrite(pSocketInt->hSocket, pSgBuf);
|
---|
7734 | }
|
---|
7735 |
|
---|
7736 | DECLCALLBACK(int) Medium::i_vdTcpFlush(VDSOCKET Sock)
|
---|
7737 | {
|
---|
7738 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7739 | return RTTcpFlush(pSocketInt->hSocket);
|
---|
7740 | }
|
---|
7741 |
|
---|
7742 | DECLCALLBACK(int) Medium::i_vdTcpSetSendCoalescing(VDSOCKET Sock, bool fEnable)
|
---|
7743 | {
|
---|
7744 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7745 | return RTTcpSetSendCoalescing(pSocketInt->hSocket, fEnable);
|
---|
7746 | }
|
---|
7747 |
|
---|
7748 | DECLCALLBACK(int) Medium::i_vdTcpGetLocalAddress(VDSOCKET Sock, PRTNETADDR pAddr)
|
---|
7749 | {
|
---|
7750 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7751 | return RTTcpGetLocalAddress(pSocketInt->hSocket, pAddr);
|
---|
7752 | }
|
---|
7753 |
|
---|
7754 | DECLCALLBACK(int) Medium::i_vdTcpGetPeerAddress(VDSOCKET Sock, PRTNETADDR pAddr)
|
---|
7755 | {
|
---|
7756 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7757 | return RTTcpGetPeerAddress(pSocketInt->hSocket, pAddr);
|
---|
7758 | }
|
---|
7759 |
|
---|
7760 | DECLCALLBACK(bool) Medium::i_vdCryptoConfigAreKeysValid(void *pvUser, const char *pszzValid)
|
---|
7761 | {
|
---|
7762 | /* Just return always true here. */
|
---|
7763 | NOREF(pvUser);
|
---|
7764 | NOREF(pszzValid);
|
---|
7765 | return true;
|
---|
7766 | }
|
---|
7767 |
|
---|
7768 | DECLCALLBACK(int) Medium::i_vdCryptoConfigQuerySize(void *pvUser, const char *pszName, size_t *pcbValue)
|
---|
7769 | {
|
---|
7770 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7771 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7772 | AssertReturn(VALID_PTR(pcbValue), VERR_INVALID_POINTER);
|
---|
7773 |
|
---|
7774 | size_t cbValue = 0;
|
---|
7775 | if (!strcmp(pszName, "Algorithm"))
|
---|
7776 | cbValue = strlen(pSettings->pszCipher) + 1;
|
---|
7777 | else if (!strcmp(pszName, "KeyId"))
|
---|
7778 | cbValue = sizeof("irrelevant");
|
---|
7779 | else if (!strcmp(pszName, "KeyStore"))
|
---|
7780 | {
|
---|
7781 | if (!pSettings->pszKeyStoreLoad)
|
---|
7782 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7783 | cbValue = strlen(pSettings->pszKeyStoreLoad) + 1;
|
---|
7784 | }
|
---|
7785 | else if (!strcmp(pszName, "CreateKeyStore"))
|
---|
7786 | cbValue = 2; /* Single digit + terminator. */
|
---|
7787 | else
|
---|
7788 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7789 |
|
---|
7790 | *pcbValue = cbValue + 1 /* include terminator */;
|
---|
7791 |
|
---|
7792 | return VINF_SUCCESS;
|
---|
7793 | }
|
---|
7794 |
|
---|
7795 | DECLCALLBACK(int) Medium::i_vdCryptoConfigQuery(void *pvUser, const char *pszName,
|
---|
7796 | char *pszValue, size_t cchValue)
|
---|
7797 | {
|
---|
7798 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7799 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7800 | AssertReturn(VALID_PTR(pszValue), VERR_INVALID_POINTER);
|
---|
7801 |
|
---|
7802 | const char *psz = NULL;
|
---|
7803 | if (!strcmp(pszName, "Algorithm"))
|
---|
7804 | psz = pSettings->pszCipher;
|
---|
7805 | else if (!strcmp(pszName, "KeyId"))
|
---|
7806 | psz = "irrelevant";
|
---|
7807 | else if (!strcmp(pszName, "KeyStore"))
|
---|
7808 | psz = pSettings->pszKeyStoreLoad;
|
---|
7809 | else if (!strcmp(pszName, "CreateKeyStore"))
|
---|
7810 | {
|
---|
7811 | if (pSettings->fCreateKeyStore)
|
---|
7812 | psz = "1";
|
---|
7813 | else
|
---|
7814 | psz = "0";
|
---|
7815 | }
|
---|
7816 | else
|
---|
7817 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7818 |
|
---|
7819 | size_t cch = strlen(psz);
|
---|
7820 | if (cch >= cchValue)
|
---|
7821 | return VERR_CFGM_NOT_ENOUGH_SPACE;
|
---|
7822 |
|
---|
7823 | memcpy(pszValue, psz, cch + 1);
|
---|
7824 | return VINF_SUCCESS;
|
---|
7825 | }
|
---|
7826 |
|
---|
7827 | DECLCALLBACK(int) Medium::i_vdCryptoKeyRetain(void *pvUser, const char *pszId,
|
---|
7828 | const uint8_t **ppbKey, size_t *pcbKey)
|
---|
7829 | {
|
---|
7830 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7831 | NOREF(pszId);
|
---|
7832 | NOREF(ppbKey);
|
---|
7833 | NOREF(pcbKey);
|
---|
7834 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7835 | AssertMsgFailedReturn(("This method should not be called here!\n"), VERR_INVALID_STATE);
|
---|
7836 | }
|
---|
7837 |
|
---|
7838 | DECLCALLBACK(int) Medium::i_vdCryptoKeyRelease(void *pvUser, const char *pszId)
|
---|
7839 | {
|
---|
7840 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7841 | NOREF(pszId);
|
---|
7842 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7843 | AssertMsgFailedReturn(("This method should not be called here!\n"), VERR_INVALID_STATE);
|
---|
7844 | }
|
---|
7845 |
|
---|
7846 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStorePasswordRetain(void *pvUser, const char *pszId, const char **ppszPassword)
|
---|
7847 | {
|
---|
7848 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7849 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7850 |
|
---|
7851 | NOREF(pszId);
|
---|
7852 | *ppszPassword = pSettings->pszPassword;
|
---|
7853 | return VINF_SUCCESS;
|
---|
7854 | }
|
---|
7855 |
|
---|
7856 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStorePasswordRelease(void *pvUser, const char *pszId)
|
---|
7857 | {
|
---|
7858 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7859 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7860 | NOREF(pszId);
|
---|
7861 | return VINF_SUCCESS;
|
---|
7862 | }
|
---|
7863 |
|
---|
7864 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStoreSave(void *pvUser, const void *pvKeyStore, size_t cbKeyStore)
|
---|
7865 | {
|
---|
7866 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7867 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7868 |
|
---|
7869 | pSettings->pszKeyStore = (char *)RTMemAllocZ(cbKeyStore);
|
---|
7870 | if (!pSettings->pszKeyStore)
|
---|
7871 | return VERR_NO_MEMORY;
|
---|
7872 |
|
---|
7873 | memcpy(pSettings->pszKeyStore, pvKeyStore, cbKeyStore);
|
---|
7874 | return VINF_SUCCESS;
|
---|
7875 | }
|
---|
7876 |
|
---|
7877 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStoreReturnParameters(void *pvUser, const char *pszCipher,
|
---|
7878 | const uint8_t *pbDek, size_t cbDek)
|
---|
7879 | {
|
---|
7880 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7881 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7882 |
|
---|
7883 | pSettings->pszCipherReturned = RTStrDup(pszCipher);
|
---|
7884 | pSettings->pbDek = pbDek;
|
---|
7885 | pSettings->cbDek = cbDek;
|
---|
7886 |
|
---|
7887 | return pSettings->pszCipherReturned ? VINF_SUCCESS : VERR_NO_MEMORY;
|
---|
7888 | }
|
---|
7889 |
|
---|
7890 | /**
|
---|
7891 | * Creates a read-only VDISK instance for this medium.
|
---|
7892 | *
|
---|
7893 | * @note Caller should not hold any medium related locks as this method will
|
---|
7894 | * acquire the medium lock for writing and others (VirtualBox).
|
---|
7895 | *
|
---|
7896 | * @returns COM status code.
|
---|
7897 | * @param pKeyStore The key store.
|
---|
7898 | * @param ppHdd Where to return the pointer to the VDISK on
|
---|
7899 | * success.
|
---|
7900 | * @param pMediumLockList The lock list to populate and lock. Caller
|
---|
7901 | * is responsible for calling the destructor or
|
---|
7902 | * MediumLockList::Clear() after destroying
|
---|
7903 | * @a *ppHdd
|
---|
7904 | * @param pCryptoSettingsRead The crypto read settings to use for setting
|
---|
7905 | * up decryption of the VDISK. This object
|
---|
7906 | * must be alive until the VDISK is destroyed!
|
---|
7907 | */
|
---|
7908 | HRESULT Medium::i_openHddForReading(SecretKeyStore *pKeyStore, PVDISK *ppHdd, MediumLockList *pMediumLockList,
|
---|
7909 | Medium::CryptoFilterSettings *pCryptoSettingsRead)
|
---|
7910 | {
|
---|
7911 | /*
|
---|
7912 | * Create the media lock list and lock the media.
|
---|
7913 | */
|
---|
7914 | HRESULT hrc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
7915 | NULL /* pToLockWrite */,
|
---|
7916 | false /* fMediumLockWriteAll */,
|
---|
7917 | NULL,
|
---|
7918 | *pMediumLockList);
|
---|
7919 | if (SUCCEEDED(hrc))
|
---|
7920 | hrc = pMediumLockList->Lock();
|
---|
7921 | if (FAILED(hrc))
|
---|
7922 | return hrc;
|
---|
7923 |
|
---|
7924 | /*
|
---|
7925 | * Get the base medium before write locking this medium.
|
---|
7926 | */
|
---|
7927 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
7928 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7929 |
|
---|
7930 | /*
|
---|
7931 | * Create the VDISK instance.
|
---|
7932 | */
|
---|
7933 | PVDISK pHdd;
|
---|
7934 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pHdd);
|
---|
7935 | AssertRCReturn(vrc, E_FAIL);
|
---|
7936 |
|
---|
7937 | /*
|
---|
7938 | * Goto avoidance using try/catch/throw(HRESULT).
|
---|
7939 | */
|
---|
7940 | try
|
---|
7941 | {
|
---|
7942 | settings::StringsMap::iterator itKeyStore = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
7943 | if (itKeyStore != pBase->m->mapProperties.end())
|
---|
7944 | {
|
---|
7945 | settings::StringsMap::iterator itKeyId = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
7946 |
|
---|
7947 | #ifdef VBOX_WITH_EXTPACK
|
---|
7948 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
7949 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
7950 | {
|
---|
7951 | /* Load the plugin */
|
---|
7952 | Utf8Str strPlugin;
|
---|
7953 | hrc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
7954 | if (SUCCEEDED(hrc))
|
---|
7955 | {
|
---|
7956 | vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
7957 | if (RT_FAILURE(vrc))
|
---|
7958 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
7959 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
7960 | i_vdError(vrc).c_str());
|
---|
7961 | }
|
---|
7962 | else
|
---|
7963 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
7964 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
7965 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
7966 | }
|
---|
7967 | else
|
---|
7968 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
7969 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
7970 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
7971 | #else
|
---|
7972 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
7973 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
7974 | #endif
|
---|
7975 |
|
---|
7976 | if (itKeyId == pBase->m->mapProperties.end())
|
---|
7977 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7978 | tr("Image '%s' is configured for encryption but doesn't has a key identifier set"),
|
---|
7979 | pBase->m->strLocationFull.c_str());
|
---|
7980 |
|
---|
7981 | /* Find the proper secret key in the key store. */
|
---|
7982 | if (!pKeyStore)
|
---|
7983 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7984 | tr("Image '%s' is configured for encryption but there is no key store to retrieve the password from"),
|
---|
7985 | pBase->m->strLocationFull.c_str());
|
---|
7986 |
|
---|
7987 | SecretKey *pKey = NULL;
|
---|
7988 | vrc = pKeyStore->retainSecretKey(itKeyId->second, &pKey);
|
---|
7989 | if (RT_FAILURE(vrc))
|
---|
7990 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7991 | tr("Failed to retrieve the secret key with ID \"%s\" from the store (%Rrc)"),
|
---|
7992 | itKeyId->second.c_str(), vrc);
|
---|
7993 |
|
---|
7994 | i_taskEncryptSettingsSetup(pCryptoSettingsRead, NULL, itKeyStore->second.c_str(), (const char *)pKey->getKeyBuffer(),
|
---|
7995 | false /* fCreateKeyStore */);
|
---|
7996 | vrc = VDFilterAdd(pHdd, "CRYPT", VD_FILTER_FLAGS_READ, pCryptoSettingsRead->vdFilterIfaces);
|
---|
7997 | pKeyStore->releaseSecretKey(itKeyId->second);
|
---|
7998 | if (vrc == VERR_VD_PASSWORD_INCORRECT)
|
---|
7999 | throw setError(VBOX_E_PASSWORD_INCORRECT, tr("The password to decrypt the image is incorrect"));
|
---|
8000 | if (RT_FAILURE(vrc))
|
---|
8001 | throw setError(VBOX_E_INVALID_OBJECT_STATE, tr("Failed to load the decryption filter: %s"),
|
---|
8002 | i_vdError(vrc).c_str());
|
---|
8003 | }
|
---|
8004 |
|
---|
8005 | /*
|
---|
8006 | * Open all media in the source chain.
|
---|
8007 | */
|
---|
8008 | MediumLockList::Base::const_iterator sourceListBegin = pMediumLockList->GetBegin();
|
---|
8009 | MediumLockList::Base::const_iterator sourceListEnd = pMediumLockList->GetEnd();
|
---|
8010 | for (MediumLockList::Base::const_iterator it = sourceListBegin; it != sourceListEnd; ++it)
|
---|
8011 | {
|
---|
8012 | const MediumLock &mediumLock = *it;
|
---|
8013 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8014 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8015 |
|
---|
8016 | /* sanity check */
|
---|
8017 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
8018 |
|
---|
8019 | /* Open all media in read-only mode. */
|
---|
8020 | vrc = VDOpen(pHdd,
|
---|
8021 | pMedium->m->strFormat.c_str(),
|
---|
8022 | pMedium->m->strLocationFull.c_str(),
|
---|
8023 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
8024 | pMedium->m->vdImageIfaces);
|
---|
8025 | if (RT_FAILURE(vrc))
|
---|
8026 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8027 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8028 | pMedium->m->strLocationFull.c_str(),
|
---|
8029 | i_vdError(vrc).c_str());
|
---|
8030 | }
|
---|
8031 |
|
---|
8032 | Assert(m->state == MediumState_LockedRead);
|
---|
8033 |
|
---|
8034 | /*
|
---|
8035 | * Done!
|
---|
8036 | */
|
---|
8037 | *ppHdd = pHdd;
|
---|
8038 | return S_OK;
|
---|
8039 | }
|
---|
8040 | catch (HRESULT hrc2)
|
---|
8041 | {
|
---|
8042 | hrc = hrc2;
|
---|
8043 | }
|
---|
8044 |
|
---|
8045 | VDDestroy(pHdd);
|
---|
8046 | return hrc;
|
---|
8047 |
|
---|
8048 | }
|
---|
8049 |
|
---|
8050 | /**
|
---|
8051 | * Implementation code for the "create base" task.
|
---|
8052 | *
|
---|
8053 | * This only gets started from Medium::CreateBaseStorage() and always runs
|
---|
8054 | * asynchronously. As a result, we always save the VirtualBox.xml file when
|
---|
8055 | * we're done here.
|
---|
8056 | *
|
---|
8057 | * @param task
|
---|
8058 | * @return
|
---|
8059 | */
|
---|
8060 | HRESULT Medium::i_taskCreateBaseHandler(Medium::CreateBaseTask &task)
|
---|
8061 | {
|
---|
8062 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
8063 | * to lock order violations, it probably causes lock order issues related
|
---|
8064 | * to the AutoCaller usage. */
|
---|
8065 | HRESULT rc = S_OK;
|
---|
8066 |
|
---|
8067 | /* these parameters we need after creation */
|
---|
8068 | uint64_t size = 0, logicalSize = 0;
|
---|
8069 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
8070 | bool fGenerateUuid = false;
|
---|
8071 |
|
---|
8072 | try
|
---|
8073 | {
|
---|
8074 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8075 |
|
---|
8076 | /* The object may request a specific UUID (through a special form of
|
---|
8077 | * the setLocation() argument). Otherwise we have to generate it */
|
---|
8078 | Guid id = m->id;
|
---|
8079 |
|
---|
8080 | fGenerateUuid = id.isZero();
|
---|
8081 | if (fGenerateUuid)
|
---|
8082 | {
|
---|
8083 | id.create();
|
---|
8084 | /* VirtualBox::i_registerMedium() will need UUID */
|
---|
8085 | unconst(m->id) = id;
|
---|
8086 | }
|
---|
8087 |
|
---|
8088 | Utf8Str format(m->strFormat);
|
---|
8089 | Utf8Str location(m->strLocationFull);
|
---|
8090 | uint64_t capabilities = m->formatObj->i_getCapabilities();
|
---|
8091 | ComAssertThrow(capabilities & ( MediumFormatCapabilities_CreateFixed
|
---|
8092 | | MediumFormatCapabilities_CreateDynamic), E_FAIL);
|
---|
8093 | Assert(m->state == MediumState_Creating);
|
---|
8094 |
|
---|
8095 | PVDISK hdd;
|
---|
8096 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8097 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8098 |
|
---|
8099 | /* unlock before the potentially lengthy operation */
|
---|
8100 | thisLock.release();
|
---|
8101 |
|
---|
8102 | try
|
---|
8103 | {
|
---|
8104 | /* ensure the directory exists */
|
---|
8105 | if (capabilities & MediumFormatCapabilities_File)
|
---|
8106 | {
|
---|
8107 | rc = VirtualBox::i_ensureFilePathExists(location, !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
8108 | if (FAILED(rc))
|
---|
8109 | throw rc;
|
---|
8110 | }
|
---|
8111 |
|
---|
8112 | VDGEOMETRY geo = { 0, 0, 0 }; /* auto-detect */
|
---|
8113 |
|
---|
8114 | vrc = VDCreateBase(hdd,
|
---|
8115 | format.c_str(),
|
---|
8116 | location.c_str(),
|
---|
8117 | task.mSize,
|
---|
8118 | task.mVariant & ~MediumVariant_NoCreateDir,
|
---|
8119 | NULL,
|
---|
8120 | &geo,
|
---|
8121 | &geo,
|
---|
8122 | id.raw(),
|
---|
8123 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
8124 | m->vdImageIfaces,
|
---|
8125 | task.mVDOperationIfaces);
|
---|
8126 | if (RT_FAILURE(vrc))
|
---|
8127 | {
|
---|
8128 | if (vrc == VERR_VD_INVALID_TYPE)
|
---|
8129 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8130 | tr("Parameters for creating the medium storage unit '%s' are invalid%s"),
|
---|
8131 | location.c_str(), i_vdError(vrc).c_str());
|
---|
8132 | else
|
---|
8133 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8134 | tr("Could not create the medium storage unit '%s'%s"),
|
---|
8135 | location.c_str(), i_vdError(vrc).c_str());
|
---|
8136 | }
|
---|
8137 |
|
---|
8138 | size = VDGetFileSize(hdd, 0);
|
---|
8139 | logicalSize = VDGetSize(hdd, 0);
|
---|
8140 | unsigned uImageFlags;
|
---|
8141 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
8142 | if (RT_SUCCESS(vrc))
|
---|
8143 | variant = (MediumVariant_T)uImageFlags;
|
---|
8144 | }
|
---|
8145 | catch (HRESULT aRC) { rc = aRC; }
|
---|
8146 |
|
---|
8147 | VDDestroy(hdd);
|
---|
8148 | }
|
---|
8149 | catch (HRESULT aRC) { rc = aRC; }
|
---|
8150 |
|
---|
8151 | if (SUCCEEDED(rc))
|
---|
8152 | {
|
---|
8153 | /* register with mVirtualBox as the last step and move to
|
---|
8154 | * Created state only on success (leaving an orphan file is
|
---|
8155 | * better than breaking media registry consistency) */
|
---|
8156 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
8157 | ComObjPtr<Medium> pMedium;
|
---|
8158 | rc = m->pVirtualBox->i_registerMedium(this, &pMedium, treeLock);
|
---|
8159 | Assert(pMedium == NULL || this == pMedium);
|
---|
8160 | }
|
---|
8161 |
|
---|
8162 | // re-acquire the lock before changing state
|
---|
8163 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8164 |
|
---|
8165 | if (SUCCEEDED(rc))
|
---|
8166 | {
|
---|
8167 | m->state = MediumState_Created;
|
---|
8168 |
|
---|
8169 | m->size = size;
|
---|
8170 | m->logicalSize = logicalSize;
|
---|
8171 | m->variant = variant;
|
---|
8172 |
|
---|
8173 | thisLock.release();
|
---|
8174 | i_markRegistriesModified();
|
---|
8175 | if (task.isAsync())
|
---|
8176 | {
|
---|
8177 | // in asynchronous mode, save settings now
|
---|
8178 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
8179 | }
|
---|
8180 | }
|
---|
8181 | else
|
---|
8182 | {
|
---|
8183 | /* back to NotCreated on failure */
|
---|
8184 | m->state = MediumState_NotCreated;
|
---|
8185 |
|
---|
8186 | /* reset UUID to prevent it from being reused next time */
|
---|
8187 | if (fGenerateUuid)
|
---|
8188 | unconst(m->id).clear();
|
---|
8189 | }
|
---|
8190 |
|
---|
8191 | return rc;
|
---|
8192 | }
|
---|
8193 |
|
---|
8194 | /**
|
---|
8195 | * Implementation code for the "create diff" task.
|
---|
8196 | *
|
---|
8197 | * This task always gets started from Medium::createDiffStorage() and can run
|
---|
8198 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
8199 | * that function. If we run synchronously, the caller expects the medium
|
---|
8200 | * registry modification to be set before returning; otherwise (in asynchronous
|
---|
8201 | * mode), we save the settings ourselves.
|
---|
8202 | *
|
---|
8203 | * @param task
|
---|
8204 | * @return
|
---|
8205 | */
|
---|
8206 | HRESULT Medium::i_taskCreateDiffHandler(Medium::CreateDiffTask &task)
|
---|
8207 | {
|
---|
8208 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
8209 | * to lock order violations, it probably causes lock order issues related
|
---|
8210 | * to the AutoCaller usage. */
|
---|
8211 | HRESULT rcTmp = S_OK;
|
---|
8212 |
|
---|
8213 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
8214 |
|
---|
8215 | uint64_t size = 0, logicalSize = 0;
|
---|
8216 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
8217 | bool fGenerateUuid = false;
|
---|
8218 |
|
---|
8219 | try
|
---|
8220 | {
|
---|
8221 | if (i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
8222 | {
|
---|
8223 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8224 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8225 | 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"),
|
---|
8226 | m->strLocationFull.c_str());
|
---|
8227 | }
|
---|
8228 |
|
---|
8229 | /* Lock both in {parent,child} order. */
|
---|
8230 | AutoMultiWriteLock2 mediaLock(this, pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8231 |
|
---|
8232 | /* The object may request a specific UUID (through a special form of
|
---|
8233 | * the setLocation() argument). Otherwise we have to generate it */
|
---|
8234 | Guid targetId = pTarget->m->id;
|
---|
8235 |
|
---|
8236 | fGenerateUuid = targetId.isZero();
|
---|
8237 | if (fGenerateUuid)
|
---|
8238 | {
|
---|
8239 | targetId.create();
|
---|
8240 | /* VirtualBox::i_registerMedium() will need UUID */
|
---|
8241 | unconst(pTarget->m->id) = targetId;
|
---|
8242 | }
|
---|
8243 |
|
---|
8244 | Guid id = m->id;
|
---|
8245 |
|
---|
8246 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
8247 | Utf8Str targetLocation(pTarget->m->strLocationFull);
|
---|
8248 | uint64_t capabilities = pTarget->m->formatObj->i_getCapabilities();
|
---|
8249 | ComAssertThrow(capabilities & MediumFormatCapabilities_CreateDynamic, E_FAIL);
|
---|
8250 |
|
---|
8251 | Assert(pTarget->m->state == MediumState_Creating);
|
---|
8252 | Assert(m->state == MediumState_LockedRead);
|
---|
8253 |
|
---|
8254 | PVDISK hdd;
|
---|
8255 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8256 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8257 |
|
---|
8258 | /* the two media are now protected by their non-default states;
|
---|
8259 | * unlock the media before the potentially lengthy operation */
|
---|
8260 | mediaLock.release();
|
---|
8261 |
|
---|
8262 | try
|
---|
8263 | {
|
---|
8264 | /* Open all media in the target chain but the last. */
|
---|
8265 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
8266 | task.mpMediumLockList->GetBegin();
|
---|
8267 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
8268 | task.mpMediumLockList->GetEnd();
|
---|
8269 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
8270 | it != targetListEnd;
|
---|
8271 | ++it)
|
---|
8272 | {
|
---|
8273 | const MediumLock &mediumLock = *it;
|
---|
8274 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8275 |
|
---|
8276 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8277 |
|
---|
8278 | /* Skip over the target diff medium */
|
---|
8279 | if (pMedium->m->state == MediumState_Creating)
|
---|
8280 | continue;
|
---|
8281 |
|
---|
8282 | /* sanity check */
|
---|
8283 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
8284 |
|
---|
8285 | /* Open all media in appropriate mode. */
|
---|
8286 | vrc = VDOpen(hdd,
|
---|
8287 | pMedium->m->strFormat.c_str(),
|
---|
8288 | pMedium->m->strLocationFull.c_str(),
|
---|
8289 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
8290 | pMedium->m->vdImageIfaces);
|
---|
8291 | if (RT_FAILURE(vrc))
|
---|
8292 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8293 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8294 | pMedium->m->strLocationFull.c_str(),
|
---|
8295 | i_vdError(vrc).c_str());
|
---|
8296 | }
|
---|
8297 |
|
---|
8298 | /* ensure the target directory exists */
|
---|
8299 | if (capabilities & MediumFormatCapabilities_File)
|
---|
8300 | {
|
---|
8301 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
8302 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
8303 | if (FAILED(rc))
|
---|
8304 | throw rc;
|
---|
8305 | }
|
---|
8306 |
|
---|
8307 | vrc = VDCreateDiff(hdd,
|
---|
8308 | targetFormat.c_str(),
|
---|
8309 | targetLocation.c_str(),
|
---|
8310 | (task.mVariant & ~(MediumVariant_NoCreateDir | MediumVariant_VmdkESX)) | VD_IMAGE_FLAGS_DIFF,
|
---|
8311 | NULL,
|
---|
8312 | targetId.raw(),
|
---|
8313 | id.raw(),
|
---|
8314 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
8315 | pTarget->m->vdImageIfaces,
|
---|
8316 | task.mVDOperationIfaces);
|
---|
8317 | if (RT_FAILURE(vrc))
|
---|
8318 | {
|
---|
8319 | if (vrc == VERR_VD_INVALID_TYPE)
|
---|
8320 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8321 | tr("Parameters for creating the differencing medium storage unit '%s' are invalid%s"),
|
---|
8322 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
8323 | else
|
---|
8324 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8325 | tr("Could not create the differencing medium storage unit '%s'%s"),
|
---|
8326 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
8327 | }
|
---|
8328 |
|
---|
8329 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
8330 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
8331 | unsigned uImageFlags;
|
---|
8332 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
8333 | if (RT_SUCCESS(vrc))
|
---|
8334 | variant = (MediumVariant_T)uImageFlags;
|
---|
8335 | }
|
---|
8336 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8337 |
|
---|
8338 | VDDestroy(hdd);
|
---|
8339 | }
|
---|
8340 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8341 |
|
---|
8342 | MultiResult mrc(rcTmp);
|
---|
8343 |
|
---|
8344 | if (SUCCEEDED(mrc))
|
---|
8345 | {
|
---|
8346 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
8347 |
|
---|
8348 | Assert(pTarget->m->pParent.isNull());
|
---|
8349 |
|
---|
8350 | /* associate child with the parent, maximum depth was checked above */
|
---|
8351 | pTarget->i_setParent(this);
|
---|
8352 |
|
---|
8353 | /* diffs for immutable media are auto-reset by default */
|
---|
8354 | bool fAutoReset;
|
---|
8355 | {
|
---|
8356 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
8357 | AutoReadLock block(pBase COMMA_LOCKVAL_SRC_POS);
|
---|
8358 | fAutoReset = (pBase->m->type == MediumType_Immutable);
|
---|
8359 | }
|
---|
8360 | {
|
---|
8361 | AutoWriteLock tlock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8362 | pTarget->m->autoReset = fAutoReset;
|
---|
8363 | }
|
---|
8364 |
|
---|
8365 | /* register with mVirtualBox as the last step and move to
|
---|
8366 | * Created state only on success (leaving an orphan file is
|
---|
8367 | * better than breaking media registry consistency) */
|
---|
8368 | ComObjPtr<Medium> pMedium;
|
---|
8369 | mrc = m->pVirtualBox->i_registerMedium(pTarget, &pMedium, treeLock);
|
---|
8370 | Assert(pTarget == pMedium);
|
---|
8371 |
|
---|
8372 | if (FAILED(mrc))
|
---|
8373 | /* break the parent association on failure to register */
|
---|
8374 | i_deparent();
|
---|
8375 | }
|
---|
8376 |
|
---|
8377 | AutoMultiWriteLock2 mediaLock(this, pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8378 |
|
---|
8379 | if (SUCCEEDED(mrc))
|
---|
8380 | {
|
---|
8381 | pTarget->m->state = MediumState_Created;
|
---|
8382 |
|
---|
8383 | pTarget->m->size = size;
|
---|
8384 | pTarget->m->logicalSize = logicalSize;
|
---|
8385 | pTarget->m->variant = variant;
|
---|
8386 | }
|
---|
8387 | else
|
---|
8388 | {
|
---|
8389 | /* back to NotCreated on failure */
|
---|
8390 | pTarget->m->state = MediumState_NotCreated;
|
---|
8391 |
|
---|
8392 | pTarget->m->autoReset = false;
|
---|
8393 |
|
---|
8394 | /* reset UUID to prevent it from being reused next time */
|
---|
8395 | if (fGenerateUuid)
|
---|
8396 | unconst(pTarget->m->id).clear();
|
---|
8397 | }
|
---|
8398 |
|
---|
8399 | // deregister the task registered in createDiffStorage()
|
---|
8400 | Assert(m->numCreateDiffTasks != 0);
|
---|
8401 | --m->numCreateDiffTasks;
|
---|
8402 |
|
---|
8403 | mediaLock.release();
|
---|
8404 | i_markRegistriesModified();
|
---|
8405 | if (task.isAsync())
|
---|
8406 | {
|
---|
8407 | // in asynchronous mode, save settings now
|
---|
8408 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
8409 | }
|
---|
8410 |
|
---|
8411 | /* Note that in sync mode, it's the caller's responsibility to
|
---|
8412 | * unlock the medium. */
|
---|
8413 |
|
---|
8414 | return mrc;
|
---|
8415 | }
|
---|
8416 |
|
---|
8417 | /**
|
---|
8418 | * Implementation code for the "merge" task.
|
---|
8419 | *
|
---|
8420 | * This task always gets started from Medium::mergeTo() and can run
|
---|
8421 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
8422 | * that function. If we run synchronously, the caller expects the medium
|
---|
8423 | * registry modification to be set before returning; otherwise (in asynchronous
|
---|
8424 | * mode), we save the settings ourselves.
|
---|
8425 | *
|
---|
8426 | * @param task
|
---|
8427 | * @return
|
---|
8428 | */
|
---|
8429 | HRESULT Medium::i_taskMergeHandler(Medium::MergeTask &task)
|
---|
8430 | {
|
---|
8431 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
8432 | * to lock order violations, it probably causes lock order issues related
|
---|
8433 | * to the AutoCaller usage. */
|
---|
8434 | HRESULT rcTmp = S_OK;
|
---|
8435 |
|
---|
8436 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
8437 |
|
---|
8438 | try
|
---|
8439 | {
|
---|
8440 | if (!task.mParentForTarget.isNull())
|
---|
8441 | if (task.mParentForTarget->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
8442 | {
|
---|
8443 | AutoReadLock plock(task.mParentForTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8444 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8445 | tr("Cannot merge image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
8446 | task.mParentForTarget->m->strLocationFull.c_str());
|
---|
8447 | }
|
---|
8448 |
|
---|
8449 | PVDISK hdd;
|
---|
8450 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8451 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8452 |
|
---|
8453 | try
|
---|
8454 | {
|
---|
8455 | // Similar code appears in SessionMachine::onlineMergeMedium, so
|
---|
8456 | // if you make any changes below check whether they are applicable
|
---|
8457 | // in that context as well.
|
---|
8458 |
|
---|
8459 | unsigned uTargetIdx = VD_LAST_IMAGE;
|
---|
8460 | unsigned uSourceIdx = VD_LAST_IMAGE;
|
---|
8461 | /* Open all media in the chain. */
|
---|
8462 | MediumLockList::Base::iterator lockListBegin =
|
---|
8463 | task.mpMediumLockList->GetBegin();
|
---|
8464 | MediumLockList::Base::iterator lockListEnd =
|
---|
8465 | task.mpMediumLockList->GetEnd();
|
---|
8466 | unsigned i = 0;
|
---|
8467 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
8468 | it != lockListEnd;
|
---|
8469 | ++it)
|
---|
8470 | {
|
---|
8471 | MediumLock &mediumLock = *it;
|
---|
8472 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8473 |
|
---|
8474 | if (pMedium == this)
|
---|
8475 | uSourceIdx = i;
|
---|
8476 | else if (pMedium == pTarget)
|
---|
8477 | uTargetIdx = i;
|
---|
8478 |
|
---|
8479 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8480 |
|
---|
8481 | /*
|
---|
8482 | * complex sanity (sane complexity)
|
---|
8483 | *
|
---|
8484 | * The current medium must be in the Deleting (medium is merged)
|
---|
8485 | * or LockedRead (parent medium) state if it is not the target.
|
---|
8486 | * If it is the target it must be in the LockedWrite state.
|
---|
8487 | */
|
---|
8488 | Assert( ( pMedium != pTarget
|
---|
8489 | && ( pMedium->m->state == MediumState_Deleting
|
---|
8490 | || pMedium->m->state == MediumState_LockedRead))
|
---|
8491 | || ( pMedium == pTarget
|
---|
8492 | && pMedium->m->state == MediumState_LockedWrite));
|
---|
8493 | /*
|
---|
8494 | * Medium must be the target, in the LockedRead state
|
---|
8495 | * or Deleting state where it is not allowed to be attached
|
---|
8496 | * to a virtual machine.
|
---|
8497 | */
|
---|
8498 | Assert( pMedium == pTarget
|
---|
8499 | || pMedium->m->state == MediumState_LockedRead
|
---|
8500 | || ( pMedium->m->backRefs.size() == 0
|
---|
8501 | && pMedium->m->state == MediumState_Deleting));
|
---|
8502 | /* The source medium must be in Deleting state. */
|
---|
8503 | Assert( pMedium != this
|
---|
8504 | || pMedium->m->state == MediumState_Deleting);
|
---|
8505 |
|
---|
8506 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
8507 |
|
---|
8508 | if ( pMedium->m->state == MediumState_LockedRead
|
---|
8509 | || pMedium->m->state == MediumState_Deleting)
|
---|
8510 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
8511 | if (pMedium->m->type == MediumType_Shareable)
|
---|
8512 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
8513 |
|
---|
8514 | /* Open the medium */
|
---|
8515 | vrc = VDOpen(hdd,
|
---|
8516 | pMedium->m->strFormat.c_str(),
|
---|
8517 | pMedium->m->strLocationFull.c_str(),
|
---|
8518 | uOpenFlags | m->uOpenFlagsDef,
|
---|
8519 | pMedium->m->vdImageIfaces);
|
---|
8520 | if (RT_FAILURE(vrc))
|
---|
8521 | throw vrc;
|
---|
8522 |
|
---|
8523 | i++;
|
---|
8524 | }
|
---|
8525 |
|
---|
8526 | ComAssertThrow( uSourceIdx != VD_LAST_IMAGE
|
---|
8527 | && uTargetIdx != VD_LAST_IMAGE, E_FAIL);
|
---|
8528 |
|
---|
8529 | vrc = VDMerge(hdd, uSourceIdx, uTargetIdx,
|
---|
8530 | task.mVDOperationIfaces);
|
---|
8531 | if (RT_FAILURE(vrc))
|
---|
8532 | throw vrc;
|
---|
8533 |
|
---|
8534 | /* update parent UUIDs */
|
---|
8535 | if (!task.mfMergeForward)
|
---|
8536 | {
|
---|
8537 | /* we need to update UUIDs of all source's children
|
---|
8538 | * which cannot be part of the container at once so
|
---|
8539 | * add each one in there individually */
|
---|
8540 | if (task.mpChildrenToReparent)
|
---|
8541 | {
|
---|
8542 | MediumLockList::Base::iterator childrenBegin = task.mpChildrenToReparent->GetBegin();
|
---|
8543 | MediumLockList::Base::iterator childrenEnd = task.mpChildrenToReparent->GetEnd();
|
---|
8544 | for (MediumLockList::Base::iterator it = childrenBegin;
|
---|
8545 | it != childrenEnd;
|
---|
8546 | ++it)
|
---|
8547 | {
|
---|
8548 | Medium *pMedium = it->GetMedium();
|
---|
8549 | /* VD_OPEN_FLAGS_INFO since UUID is wrong yet */
|
---|
8550 | vrc = VDOpen(hdd,
|
---|
8551 | pMedium->m->strFormat.c_str(),
|
---|
8552 | pMedium->m->strLocationFull.c_str(),
|
---|
8553 | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
8554 | pMedium->m->vdImageIfaces);
|
---|
8555 | if (RT_FAILURE(vrc))
|
---|
8556 | throw vrc;
|
---|
8557 |
|
---|
8558 | vrc = VDSetParentUuid(hdd, VD_LAST_IMAGE,
|
---|
8559 | pTarget->m->id.raw());
|
---|
8560 | if (RT_FAILURE(vrc))
|
---|
8561 | throw vrc;
|
---|
8562 |
|
---|
8563 | vrc = VDClose(hdd, false /* fDelete */);
|
---|
8564 | if (RT_FAILURE(vrc))
|
---|
8565 | throw vrc;
|
---|
8566 | }
|
---|
8567 | }
|
---|
8568 | }
|
---|
8569 | }
|
---|
8570 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8571 | catch (int aVRC)
|
---|
8572 | {
|
---|
8573 | rcTmp = setError(VBOX_E_FILE_ERROR,
|
---|
8574 | tr("Could not merge the medium '%s' to '%s'%s"),
|
---|
8575 | m->strLocationFull.c_str(),
|
---|
8576 | pTarget->m->strLocationFull.c_str(),
|
---|
8577 | i_vdError(aVRC).c_str());
|
---|
8578 | }
|
---|
8579 |
|
---|
8580 | VDDestroy(hdd);
|
---|
8581 | }
|
---|
8582 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8583 |
|
---|
8584 | ErrorInfoKeeper eik;
|
---|
8585 | MultiResult mrc(rcTmp);
|
---|
8586 | HRESULT rc2;
|
---|
8587 |
|
---|
8588 | if (SUCCEEDED(mrc))
|
---|
8589 | {
|
---|
8590 | /* all media but the target were successfully deleted by
|
---|
8591 | * VDMerge; reparent the last one and uninitialize deleted media. */
|
---|
8592 |
|
---|
8593 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
8594 |
|
---|
8595 | if (task.mfMergeForward)
|
---|
8596 | {
|
---|
8597 | /* first, unregister the target since it may become a base
|
---|
8598 | * medium which needs re-registration */
|
---|
8599 | rc2 = m->pVirtualBox->i_unregisterMedium(pTarget);
|
---|
8600 | AssertComRC(rc2);
|
---|
8601 |
|
---|
8602 | /* then, reparent it and disconnect the deleted branch at both ends
|
---|
8603 | * (chain->parent() is source's parent). Depth check above. */
|
---|
8604 | pTarget->i_deparent();
|
---|
8605 | pTarget->i_setParent(task.mParentForTarget);
|
---|
8606 | if (task.mParentForTarget)
|
---|
8607 | i_deparent();
|
---|
8608 |
|
---|
8609 | /* then, register again */
|
---|
8610 | ComObjPtr<Medium> pMedium;
|
---|
8611 | rc2 = m->pVirtualBox->i_registerMedium(pTarget, &pMedium,
|
---|
8612 | treeLock);
|
---|
8613 | AssertComRC(rc2);
|
---|
8614 | }
|
---|
8615 | else
|
---|
8616 | {
|
---|
8617 | Assert(pTarget->i_getChildren().size() == 1);
|
---|
8618 | Medium *targetChild = pTarget->i_getChildren().front();
|
---|
8619 |
|
---|
8620 | /* disconnect the deleted branch at the elder end */
|
---|
8621 | targetChild->i_deparent();
|
---|
8622 |
|
---|
8623 | /* reparent source's children and disconnect the deleted
|
---|
8624 | * branch at the younger end */
|
---|
8625 | if (task.mpChildrenToReparent)
|
---|
8626 | {
|
---|
8627 | /* obey {parent,child} lock order */
|
---|
8628 | AutoWriteLock sourceLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8629 |
|
---|
8630 | MediumLockList::Base::iterator childrenBegin = task.mpChildrenToReparent->GetBegin();
|
---|
8631 | MediumLockList::Base::iterator childrenEnd = task.mpChildrenToReparent->GetEnd();
|
---|
8632 | for (MediumLockList::Base::iterator it = childrenBegin;
|
---|
8633 | it != childrenEnd;
|
---|
8634 | ++it)
|
---|
8635 | {
|
---|
8636 | Medium *pMedium = it->GetMedium();
|
---|
8637 | AutoWriteLock childLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8638 |
|
---|
8639 | pMedium->i_deparent(); // removes pMedium from source
|
---|
8640 | // no depth check, reduces depth
|
---|
8641 | pMedium->i_setParent(pTarget);
|
---|
8642 | }
|
---|
8643 | }
|
---|
8644 | }
|
---|
8645 |
|
---|
8646 | /* unregister and uninitialize all media removed by the merge */
|
---|
8647 | MediumLockList::Base::iterator lockListBegin =
|
---|
8648 | task.mpMediumLockList->GetBegin();
|
---|
8649 | MediumLockList::Base::iterator lockListEnd =
|
---|
8650 | task.mpMediumLockList->GetEnd();
|
---|
8651 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
8652 | it != lockListEnd;
|
---|
8653 | )
|
---|
8654 | {
|
---|
8655 | MediumLock &mediumLock = *it;
|
---|
8656 | /* Create a real copy of the medium pointer, as the medium
|
---|
8657 | * lock deletion below would invalidate the referenced object. */
|
---|
8658 | const ComObjPtr<Medium> pMedium = mediumLock.GetMedium();
|
---|
8659 |
|
---|
8660 | /* The target and all media not merged (readonly) are skipped */
|
---|
8661 | if ( pMedium == pTarget
|
---|
8662 | || pMedium->m->state == MediumState_LockedRead)
|
---|
8663 | {
|
---|
8664 | ++it;
|
---|
8665 | continue;
|
---|
8666 | }
|
---|
8667 |
|
---|
8668 | rc2 = pMedium->m->pVirtualBox->i_unregisterMedium(pMedium);
|
---|
8669 | AssertComRC(rc2);
|
---|
8670 |
|
---|
8671 | /* now, uninitialize the deleted medium (note that
|
---|
8672 | * due to the Deleting state, uninit() will not touch
|
---|
8673 | * the parent-child relationship so we need to
|
---|
8674 | * uninitialize each disk individually) */
|
---|
8675 |
|
---|
8676 | /* note that the operation initiator medium (which is
|
---|
8677 | * normally also the source medium) is a special case
|
---|
8678 | * -- there is one more caller added by Task to it which
|
---|
8679 | * we must release. Also, if we are in sync mode, the
|
---|
8680 | * caller may still hold an AutoCaller instance for it
|
---|
8681 | * and therefore we cannot uninit() it (it's therefore
|
---|
8682 | * the caller's responsibility) */
|
---|
8683 | if (pMedium == this)
|
---|
8684 | {
|
---|
8685 | Assert(i_getChildren().size() == 0);
|
---|
8686 | Assert(m->backRefs.size() == 0);
|
---|
8687 | task.mMediumCaller.release();
|
---|
8688 | }
|
---|
8689 |
|
---|
8690 | /* Delete the medium lock list entry, which also releases the
|
---|
8691 | * caller added by MergeChain before uninit() and updates the
|
---|
8692 | * iterator to point to the right place. */
|
---|
8693 | rc2 = task.mpMediumLockList->RemoveByIterator(it);
|
---|
8694 | AssertComRC(rc2);
|
---|
8695 |
|
---|
8696 | if (task.isAsync() || pMedium != this)
|
---|
8697 | {
|
---|
8698 | treeLock.release();
|
---|
8699 | pMedium->uninit();
|
---|
8700 | treeLock.acquire();
|
---|
8701 | }
|
---|
8702 | }
|
---|
8703 | }
|
---|
8704 |
|
---|
8705 | i_markRegistriesModified();
|
---|
8706 | if (task.isAsync())
|
---|
8707 | {
|
---|
8708 | // in asynchronous mode, save settings now
|
---|
8709 | eik.restore();
|
---|
8710 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
8711 | eik.fetch();
|
---|
8712 | }
|
---|
8713 |
|
---|
8714 | if (FAILED(mrc))
|
---|
8715 | {
|
---|
8716 | /* Here we come if either VDMerge() failed (in which case we
|
---|
8717 | * assume that it tried to do everything to make a further
|
---|
8718 | * retry possible -- e.g. not deleted intermediate media
|
---|
8719 | * and so on) or VirtualBox::saveRegistries() failed (where we
|
---|
8720 | * should have the original tree but with intermediate storage
|
---|
8721 | * units deleted by VDMerge()). We have to only restore states
|
---|
8722 | * (through the MergeChain dtor) unless we are run synchronously
|
---|
8723 | * in which case it's the responsibility of the caller as stated
|
---|
8724 | * in the mergeTo() docs. The latter also implies that we
|
---|
8725 | * don't own the merge chain, so release it in this case. */
|
---|
8726 | if (task.isAsync())
|
---|
8727 | i_cancelMergeTo(task.mpChildrenToReparent, task.mpMediumLockList);
|
---|
8728 | }
|
---|
8729 |
|
---|
8730 | return mrc;
|
---|
8731 | }
|
---|
8732 |
|
---|
8733 | /**
|
---|
8734 | * Implementation code for the "clone" task.
|
---|
8735 | *
|
---|
8736 | * This only gets started from Medium::CloneTo() and always runs asynchronously.
|
---|
8737 | * As a result, we always save the VirtualBox.xml file when we're done here.
|
---|
8738 | *
|
---|
8739 | * @param task
|
---|
8740 | * @return
|
---|
8741 | */
|
---|
8742 | HRESULT Medium::i_taskCloneHandler(Medium::CloneTask &task)
|
---|
8743 | {
|
---|
8744 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
8745 | * to lock order violations, it probably causes lock order issues related
|
---|
8746 | * to the AutoCaller usage. */
|
---|
8747 | HRESULT rcTmp = S_OK;
|
---|
8748 |
|
---|
8749 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
8750 | const ComObjPtr<Medium> &pParent = task.mParent;
|
---|
8751 |
|
---|
8752 | bool fCreatingTarget = false;
|
---|
8753 |
|
---|
8754 | uint64_t size = 0, logicalSize = 0;
|
---|
8755 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
8756 | bool fGenerateUuid = false;
|
---|
8757 |
|
---|
8758 | try
|
---|
8759 | {
|
---|
8760 | if (!pParent.isNull())
|
---|
8761 | {
|
---|
8762 |
|
---|
8763 | if (pParent->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
8764 | {
|
---|
8765 | AutoReadLock plock(pParent COMMA_LOCKVAL_SRC_POS);
|
---|
8766 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8767 | tr("Cannot clone image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
8768 | pParent->m->strLocationFull.c_str());
|
---|
8769 | }
|
---|
8770 | }
|
---|
8771 |
|
---|
8772 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
8773 | * signal from the task initiator (which releases it only after
|
---|
8774 | * RTThreadCreate()) that we can start the job. */
|
---|
8775 | AutoMultiWriteLock3 thisLock(this, pTarget, pParent COMMA_LOCKVAL_SRC_POS);
|
---|
8776 |
|
---|
8777 | fCreatingTarget = pTarget->m->state == MediumState_Creating;
|
---|
8778 |
|
---|
8779 | /* The object may request a specific UUID (through a special form of
|
---|
8780 | * the setLocation() argument). Otherwise we have to generate it */
|
---|
8781 | Guid targetId = pTarget->m->id;
|
---|
8782 |
|
---|
8783 | fGenerateUuid = targetId.isZero();
|
---|
8784 | if (fGenerateUuid)
|
---|
8785 | {
|
---|
8786 | targetId.create();
|
---|
8787 | /* VirtualBox::registerMedium() will need UUID */
|
---|
8788 | unconst(pTarget->m->id) = targetId;
|
---|
8789 | }
|
---|
8790 |
|
---|
8791 | PVDISK hdd;
|
---|
8792 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8793 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8794 |
|
---|
8795 | try
|
---|
8796 | {
|
---|
8797 | /* Open all media in the source chain. */
|
---|
8798 | MediumLockList::Base::const_iterator sourceListBegin =
|
---|
8799 | task.mpSourceMediumLockList->GetBegin();
|
---|
8800 | MediumLockList::Base::const_iterator sourceListEnd =
|
---|
8801 | task.mpSourceMediumLockList->GetEnd();
|
---|
8802 | for (MediumLockList::Base::const_iterator it = sourceListBegin;
|
---|
8803 | it != sourceListEnd;
|
---|
8804 | ++it)
|
---|
8805 | {
|
---|
8806 | const MediumLock &mediumLock = *it;
|
---|
8807 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8808 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8809 |
|
---|
8810 | /* sanity check */
|
---|
8811 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
8812 |
|
---|
8813 | /** Open all media in read-only mode. */
|
---|
8814 | vrc = VDOpen(hdd,
|
---|
8815 | pMedium->m->strFormat.c_str(),
|
---|
8816 | pMedium->m->strLocationFull.c_str(),
|
---|
8817 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
8818 | pMedium->m->vdImageIfaces);
|
---|
8819 | if (RT_FAILURE(vrc))
|
---|
8820 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8821 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8822 | pMedium->m->strLocationFull.c_str(),
|
---|
8823 | i_vdError(vrc).c_str());
|
---|
8824 | }
|
---|
8825 |
|
---|
8826 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
8827 | Utf8Str targetLocation(pTarget->m->strLocationFull);
|
---|
8828 | uint64_t capabilities = pTarget->m->formatObj->i_getCapabilities();
|
---|
8829 |
|
---|
8830 | Assert( pTarget->m->state == MediumState_Creating
|
---|
8831 | || pTarget->m->state == MediumState_LockedWrite);
|
---|
8832 | Assert(m->state == MediumState_LockedRead);
|
---|
8833 | Assert( pParent.isNull()
|
---|
8834 | || pParent->m->state == MediumState_LockedRead);
|
---|
8835 |
|
---|
8836 | /* unlock before the potentially lengthy operation */
|
---|
8837 | thisLock.release();
|
---|
8838 |
|
---|
8839 | /* ensure the target directory exists */
|
---|
8840 | if (capabilities & MediumFormatCapabilities_File)
|
---|
8841 | {
|
---|
8842 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
8843 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
8844 | if (FAILED(rc))
|
---|
8845 | throw rc;
|
---|
8846 | }
|
---|
8847 |
|
---|
8848 | PVDISK targetHdd;
|
---|
8849 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &targetHdd);
|
---|
8850 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8851 |
|
---|
8852 | try
|
---|
8853 | {
|
---|
8854 | /* Open all media in the target chain. */
|
---|
8855 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
8856 | task.mpTargetMediumLockList->GetBegin();
|
---|
8857 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
8858 | task.mpTargetMediumLockList->GetEnd();
|
---|
8859 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
8860 | it != targetListEnd;
|
---|
8861 | ++it)
|
---|
8862 | {
|
---|
8863 | const MediumLock &mediumLock = *it;
|
---|
8864 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8865 |
|
---|
8866 | /* If the target medium is not created yet there's no
|
---|
8867 | * reason to open it. */
|
---|
8868 | if (pMedium == pTarget && fCreatingTarget)
|
---|
8869 | continue;
|
---|
8870 |
|
---|
8871 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8872 |
|
---|
8873 | /* sanity check */
|
---|
8874 | Assert( pMedium->m->state == MediumState_LockedRead
|
---|
8875 | || pMedium->m->state == MediumState_LockedWrite);
|
---|
8876 |
|
---|
8877 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
8878 | if (pMedium->m->state != MediumState_LockedWrite)
|
---|
8879 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
8880 | if (pMedium->m->type == MediumType_Shareable)
|
---|
8881 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
8882 |
|
---|
8883 | /* Open all media in appropriate mode. */
|
---|
8884 | vrc = VDOpen(targetHdd,
|
---|
8885 | pMedium->m->strFormat.c_str(),
|
---|
8886 | pMedium->m->strLocationFull.c_str(),
|
---|
8887 | uOpenFlags | m->uOpenFlagsDef,
|
---|
8888 | pMedium->m->vdImageIfaces);
|
---|
8889 | if (RT_FAILURE(vrc))
|
---|
8890 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8891 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8892 | pMedium->m->strLocationFull.c_str(),
|
---|
8893 | i_vdError(vrc).c_str());
|
---|
8894 | }
|
---|
8895 |
|
---|
8896 | /* target isn't locked, but no changing data is accessed */
|
---|
8897 | if (task.midxSrcImageSame == UINT32_MAX)
|
---|
8898 | {
|
---|
8899 | vrc = VDCopy(hdd,
|
---|
8900 | VD_LAST_IMAGE,
|
---|
8901 | targetHdd,
|
---|
8902 | targetFormat.c_str(),
|
---|
8903 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
8904 | false /* fMoveByRename */,
|
---|
8905 | 0 /* cbSize */,
|
---|
8906 | task.mVariant & ~MediumVariant_NoCreateDir,
|
---|
8907 | targetId.raw(),
|
---|
8908 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
8909 | NULL /* pVDIfsOperation */,
|
---|
8910 | pTarget->m->vdImageIfaces,
|
---|
8911 | task.mVDOperationIfaces);
|
---|
8912 | }
|
---|
8913 | else
|
---|
8914 | {
|
---|
8915 | vrc = VDCopyEx(hdd,
|
---|
8916 | VD_LAST_IMAGE,
|
---|
8917 | targetHdd,
|
---|
8918 | targetFormat.c_str(),
|
---|
8919 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
8920 | false /* fMoveByRename */,
|
---|
8921 | 0 /* cbSize */,
|
---|
8922 | task.midxSrcImageSame,
|
---|
8923 | task.midxDstImageSame,
|
---|
8924 | task.mVariant & ~MediumVariant_NoCreateDir,
|
---|
8925 | targetId.raw(),
|
---|
8926 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
8927 | NULL /* pVDIfsOperation */,
|
---|
8928 | pTarget->m->vdImageIfaces,
|
---|
8929 | task.mVDOperationIfaces);
|
---|
8930 | }
|
---|
8931 | if (RT_FAILURE(vrc))
|
---|
8932 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8933 | tr("Could not create the clone medium '%s'%s"),
|
---|
8934 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
8935 |
|
---|
8936 | size = VDGetFileSize(targetHdd, VD_LAST_IMAGE);
|
---|
8937 | logicalSize = VDGetSize(targetHdd, VD_LAST_IMAGE);
|
---|
8938 | unsigned uImageFlags;
|
---|
8939 | vrc = VDGetImageFlags(targetHdd, 0, &uImageFlags);
|
---|
8940 | if (RT_SUCCESS(vrc))
|
---|
8941 | variant = (MediumVariant_T)uImageFlags;
|
---|
8942 | }
|
---|
8943 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8944 |
|
---|
8945 | VDDestroy(targetHdd);
|
---|
8946 | }
|
---|
8947 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8948 |
|
---|
8949 | VDDestroy(hdd);
|
---|
8950 | }
|
---|
8951 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8952 |
|
---|
8953 | ErrorInfoKeeper eik;
|
---|
8954 | MultiResult mrc(rcTmp);
|
---|
8955 |
|
---|
8956 | /* Only do the parent changes for newly created media. */
|
---|
8957 | if (SUCCEEDED(mrc) && fCreatingTarget)
|
---|
8958 | {
|
---|
8959 | /* we set m->pParent & children() */
|
---|
8960 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
8961 |
|
---|
8962 | Assert(pTarget->m->pParent.isNull());
|
---|
8963 |
|
---|
8964 | if (pParent)
|
---|
8965 | {
|
---|
8966 | /* Associate the clone with the parent and deassociate
|
---|
8967 | * from VirtualBox. Depth check above. */
|
---|
8968 | pTarget->i_setParent(pParent);
|
---|
8969 |
|
---|
8970 | /* register with mVirtualBox as the last step and move to
|
---|
8971 | * Created state only on success (leaving an orphan file is
|
---|
8972 | * better than breaking media registry consistency) */
|
---|
8973 | eik.restore();
|
---|
8974 | ComObjPtr<Medium> pMedium;
|
---|
8975 | mrc = pParent->m->pVirtualBox->i_registerMedium(pTarget, &pMedium,
|
---|
8976 | treeLock);
|
---|
8977 | Assert( FAILED(mrc)
|
---|
8978 | || pTarget == pMedium);
|
---|
8979 | eik.fetch();
|
---|
8980 |
|
---|
8981 | if (FAILED(mrc))
|
---|
8982 | /* break parent association on failure to register */
|
---|
8983 | pTarget->i_deparent(); // removes target from parent
|
---|
8984 | }
|
---|
8985 | else
|
---|
8986 | {
|
---|
8987 | /* just register */
|
---|
8988 | eik.restore();
|
---|
8989 | ComObjPtr<Medium> pMedium;
|
---|
8990 | mrc = m->pVirtualBox->i_registerMedium(pTarget, &pMedium,
|
---|
8991 | treeLock);
|
---|
8992 | Assert( FAILED(mrc)
|
---|
8993 | || pTarget == pMedium);
|
---|
8994 | eik.fetch();
|
---|
8995 | }
|
---|
8996 | }
|
---|
8997 |
|
---|
8998 | if (fCreatingTarget)
|
---|
8999 | {
|
---|
9000 | AutoWriteLock mLock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
9001 |
|
---|
9002 | if (SUCCEEDED(mrc))
|
---|
9003 | {
|
---|
9004 | pTarget->m->state = MediumState_Created;
|
---|
9005 |
|
---|
9006 | pTarget->m->size = size;
|
---|
9007 | pTarget->m->logicalSize = logicalSize;
|
---|
9008 | pTarget->m->variant = variant;
|
---|
9009 | }
|
---|
9010 | else
|
---|
9011 | {
|
---|
9012 | /* back to NotCreated on failure */
|
---|
9013 | pTarget->m->state = MediumState_NotCreated;
|
---|
9014 |
|
---|
9015 | /* reset UUID to prevent it from being reused next time */
|
---|
9016 | if (fGenerateUuid)
|
---|
9017 | unconst(pTarget->m->id).clear();
|
---|
9018 | }
|
---|
9019 | }
|
---|
9020 |
|
---|
9021 | /* Copy any filter related settings over to the target. */
|
---|
9022 | if (SUCCEEDED(mrc))
|
---|
9023 | {
|
---|
9024 | /* Copy any filter related settings over. */
|
---|
9025 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
9026 | ComObjPtr<Medium> pTargetBase = pTarget->i_getBase();
|
---|
9027 | std::vector<com::Utf8Str> aFilterPropNames;
|
---|
9028 | std::vector<com::Utf8Str> aFilterPropValues;
|
---|
9029 | mrc = pBase->i_getFilterProperties(aFilterPropNames, aFilterPropValues);
|
---|
9030 | if (SUCCEEDED(mrc))
|
---|
9031 | {
|
---|
9032 | /* Go through the properties and add them to the target medium. */
|
---|
9033 | for (unsigned idx = 0; idx < aFilterPropNames.size(); idx++)
|
---|
9034 | {
|
---|
9035 | mrc = pTargetBase->i_setPropertyDirect(aFilterPropNames[idx], aFilterPropValues[idx]);
|
---|
9036 | if (FAILED(mrc)) break;
|
---|
9037 | }
|
---|
9038 |
|
---|
9039 | // now, at the end of this task (always asynchronous), save the settings
|
---|
9040 | if (SUCCEEDED(mrc))
|
---|
9041 | {
|
---|
9042 | // save the settings
|
---|
9043 | i_markRegistriesModified();
|
---|
9044 | /* collect multiple errors */
|
---|
9045 | eik.restore();
|
---|
9046 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
9047 | eik.fetch();
|
---|
9048 | }
|
---|
9049 | }
|
---|
9050 | }
|
---|
9051 |
|
---|
9052 | /* Everything is explicitly unlocked when the task exits,
|
---|
9053 | * as the task destruction also destroys the source chain. */
|
---|
9054 |
|
---|
9055 | /* Make sure the source chain is released early. It could happen
|
---|
9056 | * that we get a deadlock in Appliance::Import when Medium::Close
|
---|
9057 | * is called & the source chain is released at the same time. */
|
---|
9058 | task.mpSourceMediumLockList->Clear();
|
---|
9059 |
|
---|
9060 | return mrc;
|
---|
9061 | }
|
---|
9062 |
|
---|
9063 | /**
|
---|
9064 | * Implementation code for the "move" task.
|
---|
9065 | *
|
---|
9066 | * This only gets started from Medium::SetLocation() and always
|
---|
9067 | * runs asynchronously.
|
---|
9068 | *
|
---|
9069 | * @param task
|
---|
9070 | * @return
|
---|
9071 | */
|
---|
9072 | HRESULT Medium::i_taskMoveHandler(Medium::MoveTask &task)
|
---|
9073 | {
|
---|
9074 |
|
---|
9075 | HRESULT rcOut = S_OK;
|
---|
9076 |
|
---|
9077 | /* pTarget is equal "this" in our case */
|
---|
9078 | const ComObjPtr<Medium> &pTarget = task.mMedium;
|
---|
9079 |
|
---|
9080 | uint64_t size = 0; NOREF(size);
|
---|
9081 | uint64_t logicalSize = 0; NOREF(logicalSize);
|
---|
9082 | MediumVariant_T variant = MediumVariant_Standard; NOREF(variant);
|
---|
9083 |
|
---|
9084 | /*
|
---|
9085 | * it's exactly moving, not cloning
|
---|
9086 | */
|
---|
9087 | if (!i_isMoveOperation(pTarget))
|
---|
9088 | {
|
---|
9089 | HRESULT rc = setError(VBOX_E_FILE_ERROR,
|
---|
9090 | tr("Wrong preconditions for moving the medium %s"),
|
---|
9091 | pTarget->m->strLocationFull.c_str());
|
---|
9092 | return rc;
|
---|
9093 | }
|
---|
9094 |
|
---|
9095 | try
|
---|
9096 | {
|
---|
9097 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
9098 | * signal from the task initiator (which releases it only after
|
---|
9099 | * RTThreadCreate()) that we can start the job. */
|
---|
9100 |
|
---|
9101 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9102 |
|
---|
9103 | PVDISK hdd;
|
---|
9104 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9105 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9106 |
|
---|
9107 | try
|
---|
9108 | {
|
---|
9109 | /* Open all media in the source chain. */
|
---|
9110 | MediumLockList::Base::const_iterator sourceListBegin =
|
---|
9111 | task.mpMediumLockList->GetBegin();
|
---|
9112 | MediumLockList::Base::const_iterator sourceListEnd =
|
---|
9113 | task.mpMediumLockList->GetEnd();
|
---|
9114 | for (MediumLockList::Base::const_iterator it = sourceListBegin;
|
---|
9115 | it != sourceListEnd;
|
---|
9116 | ++it)
|
---|
9117 | {
|
---|
9118 | const MediumLock &mediumLock = *it;
|
---|
9119 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9120 | AutoWriteLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9121 |
|
---|
9122 | /* sanity check */
|
---|
9123 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
9124 |
|
---|
9125 | vrc = VDOpen(hdd,
|
---|
9126 | pMedium->m->strFormat.c_str(),
|
---|
9127 | pMedium->m->strLocationFull.c_str(),
|
---|
9128 | VD_OPEN_FLAGS_NORMAL,
|
---|
9129 | pMedium->m->vdImageIfaces);
|
---|
9130 | if (RT_FAILURE(vrc))
|
---|
9131 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9132 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9133 | pMedium->m->strLocationFull.c_str(),
|
---|
9134 | i_vdError(vrc).c_str());
|
---|
9135 | }
|
---|
9136 |
|
---|
9137 | /* we can directly use pTarget->m->"variables" but for better reading we use local copies */
|
---|
9138 | Guid targetId = pTarget->m->id;
|
---|
9139 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
9140 | uint64_t targetCapabilities = pTarget->m->formatObj->i_getCapabilities();
|
---|
9141 |
|
---|
9142 | /*
|
---|
9143 | * change target location
|
---|
9144 | * m->strNewLocationFull has been set already together with m->fMoveThisMedium in
|
---|
9145 | * i_preparationForMoving()
|
---|
9146 | */
|
---|
9147 | Utf8Str targetLocation = i_getNewLocationForMoving();
|
---|
9148 |
|
---|
9149 | /* unlock before the potentially lengthy operation */
|
---|
9150 | thisLock.release();
|
---|
9151 |
|
---|
9152 | /* ensure the target directory exists */
|
---|
9153 | if (targetCapabilities & MediumFormatCapabilities_File)
|
---|
9154 | {
|
---|
9155 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
9156 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
9157 | if (FAILED(rc))
|
---|
9158 | throw rc;
|
---|
9159 | }
|
---|
9160 |
|
---|
9161 | try
|
---|
9162 | {
|
---|
9163 | vrc = VDCopy(hdd,
|
---|
9164 | VD_LAST_IMAGE,
|
---|
9165 | hdd,
|
---|
9166 | targetFormat.c_str(),
|
---|
9167 | targetLocation.c_str(),
|
---|
9168 | true /* fMoveByRename */,
|
---|
9169 | 0 /* cbSize */,
|
---|
9170 | VD_IMAGE_FLAGS_NONE,
|
---|
9171 | targetId.raw(),
|
---|
9172 | VD_OPEN_FLAGS_NORMAL,
|
---|
9173 | NULL /* pVDIfsOperation */,
|
---|
9174 | NULL,
|
---|
9175 | NULL);
|
---|
9176 | if (RT_FAILURE(vrc))
|
---|
9177 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9178 | tr("Could not move medium '%s'%s"),
|
---|
9179 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
9180 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
9181 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
9182 | unsigned uImageFlags;
|
---|
9183 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
9184 | if (RT_SUCCESS(vrc))
|
---|
9185 | variant = (MediumVariant_T)uImageFlags;
|
---|
9186 |
|
---|
9187 | /*
|
---|
9188 | * set current location, because VDCopy\VDCopyEx doesn't do it.
|
---|
9189 | * also reset moving flag
|
---|
9190 | */
|
---|
9191 | i_resetMoveOperationData();
|
---|
9192 | m->strLocationFull = targetLocation;
|
---|
9193 |
|
---|
9194 | }
|
---|
9195 | catch (HRESULT aRC) { rcOut = aRC; }
|
---|
9196 |
|
---|
9197 | }
|
---|
9198 | catch (HRESULT aRC) { rcOut = aRC; }
|
---|
9199 |
|
---|
9200 | VDDestroy(hdd);
|
---|
9201 | }
|
---|
9202 | catch (HRESULT aRC) { rcOut = aRC; }
|
---|
9203 |
|
---|
9204 | ErrorInfoKeeper eik;
|
---|
9205 | MultiResult mrc(rcOut);
|
---|
9206 |
|
---|
9207 | // now, at the end of this task (always asynchronous), save the settings
|
---|
9208 | if (SUCCEEDED(mrc))
|
---|
9209 | {
|
---|
9210 | // save the settings
|
---|
9211 | i_markRegistriesModified();
|
---|
9212 | /* collect multiple errors */
|
---|
9213 | eik.restore();
|
---|
9214 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
9215 | eik.fetch();
|
---|
9216 | }
|
---|
9217 |
|
---|
9218 | /* Everything is explicitly unlocked when the task exits,
|
---|
9219 | * as the task destruction also destroys the source chain. */
|
---|
9220 |
|
---|
9221 | task.mpMediumLockList->Clear();
|
---|
9222 |
|
---|
9223 | return mrc;
|
---|
9224 | }
|
---|
9225 |
|
---|
9226 | /**
|
---|
9227 | * Implementation code for the "delete" task.
|
---|
9228 | *
|
---|
9229 | * This task always gets started from Medium::deleteStorage() and can run
|
---|
9230 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
9231 | * that function.
|
---|
9232 | *
|
---|
9233 | * @param task
|
---|
9234 | * @return
|
---|
9235 | */
|
---|
9236 | HRESULT Medium::i_taskDeleteHandler(Medium::DeleteTask &task)
|
---|
9237 | {
|
---|
9238 | NOREF(task);
|
---|
9239 | HRESULT rc = S_OK;
|
---|
9240 |
|
---|
9241 | try
|
---|
9242 | {
|
---|
9243 | /* The lock is also used as a signal from the task initiator (which
|
---|
9244 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
9245 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9246 |
|
---|
9247 | PVDISK hdd;
|
---|
9248 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9249 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9250 |
|
---|
9251 | Utf8Str format(m->strFormat);
|
---|
9252 | Utf8Str location(m->strLocationFull);
|
---|
9253 |
|
---|
9254 | /* unlock before the potentially lengthy operation */
|
---|
9255 | Assert(m->state == MediumState_Deleting);
|
---|
9256 | thisLock.release();
|
---|
9257 |
|
---|
9258 | try
|
---|
9259 | {
|
---|
9260 | vrc = VDOpen(hdd,
|
---|
9261 | format.c_str(),
|
---|
9262 | location.c_str(),
|
---|
9263 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
9264 | m->vdImageIfaces);
|
---|
9265 | if (RT_SUCCESS(vrc))
|
---|
9266 | vrc = VDClose(hdd, true /* fDelete */);
|
---|
9267 |
|
---|
9268 | if (RT_FAILURE(vrc))
|
---|
9269 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9270 | tr("Could not delete the medium storage unit '%s'%s"),
|
---|
9271 | location.c_str(), i_vdError(vrc).c_str());
|
---|
9272 |
|
---|
9273 | }
|
---|
9274 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9275 |
|
---|
9276 | VDDestroy(hdd);
|
---|
9277 | }
|
---|
9278 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9279 |
|
---|
9280 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9281 |
|
---|
9282 | /* go to the NotCreated state even on failure since the storage
|
---|
9283 | * may have been already partially deleted and cannot be used any
|
---|
9284 | * more. One will be able to manually re-open the storage if really
|
---|
9285 | * needed to re-register it. */
|
---|
9286 | m->state = MediumState_NotCreated;
|
---|
9287 |
|
---|
9288 | /* Reset UUID to prevent Create* from reusing it again */
|
---|
9289 | unconst(m->id).clear();
|
---|
9290 |
|
---|
9291 | return rc;
|
---|
9292 | }
|
---|
9293 |
|
---|
9294 | /**
|
---|
9295 | * Implementation code for the "reset" task.
|
---|
9296 | *
|
---|
9297 | * This always gets started asynchronously from Medium::Reset().
|
---|
9298 | *
|
---|
9299 | * @param task
|
---|
9300 | * @return
|
---|
9301 | */
|
---|
9302 | HRESULT Medium::i_taskResetHandler(Medium::ResetTask &task)
|
---|
9303 | {
|
---|
9304 | HRESULT rc = S_OK;
|
---|
9305 |
|
---|
9306 | uint64_t size = 0, logicalSize = 0;
|
---|
9307 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
9308 |
|
---|
9309 | try
|
---|
9310 | {
|
---|
9311 | /* The lock is also used as a signal from the task initiator (which
|
---|
9312 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
9313 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9314 |
|
---|
9315 | /// @todo Below we use a pair of delete/create operations to reset
|
---|
9316 | /// the diff contents but the most efficient way will of course be
|
---|
9317 | /// to add a VDResetDiff() API call
|
---|
9318 |
|
---|
9319 | PVDISK hdd;
|
---|
9320 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9321 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9322 |
|
---|
9323 | Guid id = m->id;
|
---|
9324 | Utf8Str format(m->strFormat);
|
---|
9325 | Utf8Str location(m->strLocationFull);
|
---|
9326 |
|
---|
9327 | Medium *pParent = m->pParent;
|
---|
9328 | Guid parentId = pParent->m->id;
|
---|
9329 | Utf8Str parentFormat(pParent->m->strFormat);
|
---|
9330 | Utf8Str parentLocation(pParent->m->strLocationFull);
|
---|
9331 |
|
---|
9332 | Assert(m->state == MediumState_LockedWrite);
|
---|
9333 |
|
---|
9334 | /* unlock before the potentially lengthy operation */
|
---|
9335 | thisLock.release();
|
---|
9336 |
|
---|
9337 | try
|
---|
9338 | {
|
---|
9339 | /* Open all media in the target chain but the last. */
|
---|
9340 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
9341 | task.mpMediumLockList->GetBegin();
|
---|
9342 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
9343 | task.mpMediumLockList->GetEnd();
|
---|
9344 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
9345 | it != targetListEnd;
|
---|
9346 | ++it)
|
---|
9347 | {
|
---|
9348 | const MediumLock &mediumLock = *it;
|
---|
9349 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9350 |
|
---|
9351 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9352 |
|
---|
9353 | /* sanity check, "this" is checked above */
|
---|
9354 | Assert( pMedium == this
|
---|
9355 | || pMedium->m->state == MediumState_LockedRead);
|
---|
9356 |
|
---|
9357 | /* Open all media in appropriate mode. */
|
---|
9358 | vrc = VDOpen(hdd,
|
---|
9359 | pMedium->m->strFormat.c_str(),
|
---|
9360 | pMedium->m->strLocationFull.c_str(),
|
---|
9361 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
9362 | pMedium->m->vdImageIfaces);
|
---|
9363 | if (RT_FAILURE(vrc))
|
---|
9364 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9365 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9366 | pMedium->m->strLocationFull.c_str(),
|
---|
9367 | i_vdError(vrc).c_str());
|
---|
9368 |
|
---|
9369 | /* Done when we hit the media which should be reset */
|
---|
9370 | if (pMedium == this)
|
---|
9371 | break;
|
---|
9372 | }
|
---|
9373 |
|
---|
9374 | /* first, delete the storage unit */
|
---|
9375 | vrc = VDClose(hdd, true /* fDelete */);
|
---|
9376 | if (RT_FAILURE(vrc))
|
---|
9377 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9378 | tr("Could not delete the medium storage unit '%s'%s"),
|
---|
9379 | location.c_str(), i_vdError(vrc).c_str());
|
---|
9380 |
|
---|
9381 | /* next, create it again */
|
---|
9382 | vrc = VDOpen(hdd,
|
---|
9383 | parentFormat.c_str(),
|
---|
9384 | parentLocation.c_str(),
|
---|
9385 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
9386 | m->vdImageIfaces);
|
---|
9387 | if (RT_FAILURE(vrc))
|
---|
9388 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9389 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9390 | parentLocation.c_str(), i_vdError(vrc).c_str());
|
---|
9391 |
|
---|
9392 | vrc = VDCreateDiff(hdd,
|
---|
9393 | format.c_str(),
|
---|
9394 | location.c_str(),
|
---|
9395 | /// @todo use the same medium variant as before
|
---|
9396 | VD_IMAGE_FLAGS_NONE,
|
---|
9397 | NULL,
|
---|
9398 | id.raw(),
|
---|
9399 | parentId.raw(),
|
---|
9400 | VD_OPEN_FLAGS_NORMAL,
|
---|
9401 | m->vdImageIfaces,
|
---|
9402 | task.mVDOperationIfaces);
|
---|
9403 | if (RT_FAILURE(vrc))
|
---|
9404 | {
|
---|
9405 | if (vrc == VERR_VD_INVALID_TYPE)
|
---|
9406 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9407 | tr("Parameters for creating the differencing medium storage unit '%s' are invalid%s"),
|
---|
9408 | location.c_str(), i_vdError(vrc).c_str());
|
---|
9409 | else
|
---|
9410 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9411 | tr("Could not create the differencing medium storage unit '%s'%s"),
|
---|
9412 | location.c_str(), i_vdError(vrc).c_str());
|
---|
9413 | }
|
---|
9414 |
|
---|
9415 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
9416 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
9417 | unsigned uImageFlags;
|
---|
9418 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
9419 | if (RT_SUCCESS(vrc))
|
---|
9420 | variant = (MediumVariant_T)uImageFlags;
|
---|
9421 | }
|
---|
9422 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9423 |
|
---|
9424 | VDDestroy(hdd);
|
---|
9425 | }
|
---|
9426 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9427 |
|
---|
9428 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9429 |
|
---|
9430 | m->size = size;
|
---|
9431 | m->logicalSize = logicalSize;
|
---|
9432 | m->variant = variant;
|
---|
9433 |
|
---|
9434 | /* Everything is explicitly unlocked when the task exits,
|
---|
9435 | * as the task destruction also destroys the media chain. */
|
---|
9436 |
|
---|
9437 | return rc;
|
---|
9438 | }
|
---|
9439 |
|
---|
9440 | /**
|
---|
9441 | * Implementation code for the "compact" task.
|
---|
9442 | *
|
---|
9443 | * @param task
|
---|
9444 | * @return
|
---|
9445 | */
|
---|
9446 | HRESULT Medium::i_taskCompactHandler(Medium::CompactTask &task)
|
---|
9447 | {
|
---|
9448 | HRESULT rc = S_OK;
|
---|
9449 |
|
---|
9450 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
9451 | * signal from the task initiator (which releases it only after
|
---|
9452 | * RTThreadCreate()) that we can start the job. */
|
---|
9453 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9454 |
|
---|
9455 | try
|
---|
9456 | {
|
---|
9457 | PVDISK hdd;
|
---|
9458 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9459 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9460 |
|
---|
9461 | try
|
---|
9462 | {
|
---|
9463 | /* Open all media in the chain. */
|
---|
9464 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
9465 | task.mpMediumLockList->GetBegin();
|
---|
9466 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
9467 | task.mpMediumLockList->GetEnd();
|
---|
9468 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
9469 | mediumListEnd;
|
---|
9470 | --mediumListLast;
|
---|
9471 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
9472 | it != mediumListEnd;
|
---|
9473 | ++it)
|
---|
9474 | {
|
---|
9475 | const MediumLock &mediumLock = *it;
|
---|
9476 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9477 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9478 |
|
---|
9479 | /* sanity check */
|
---|
9480 | if (it == mediumListLast)
|
---|
9481 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
9482 | else
|
---|
9483 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
9484 |
|
---|
9485 | /* Open all media but last in read-only mode. Do not handle
|
---|
9486 | * shareable media, as compaction and sharing are mutually
|
---|
9487 | * exclusive. */
|
---|
9488 | vrc = VDOpen(hdd,
|
---|
9489 | pMedium->m->strFormat.c_str(),
|
---|
9490 | pMedium->m->strLocationFull.c_str(),
|
---|
9491 | m->uOpenFlagsDef | (it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
9492 | pMedium->m->vdImageIfaces);
|
---|
9493 | if (RT_FAILURE(vrc))
|
---|
9494 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9495 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9496 | pMedium->m->strLocationFull.c_str(),
|
---|
9497 | i_vdError(vrc).c_str());
|
---|
9498 | }
|
---|
9499 |
|
---|
9500 | Assert(m->state == MediumState_LockedWrite);
|
---|
9501 |
|
---|
9502 | Utf8Str location(m->strLocationFull);
|
---|
9503 |
|
---|
9504 | /* unlock before the potentially lengthy operation */
|
---|
9505 | thisLock.release();
|
---|
9506 |
|
---|
9507 | vrc = VDCompact(hdd, VD_LAST_IMAGE, task.mVDOperationIfaces);
|
---|
9508 | if (RT_FAILURE(vrc))
|
---|
9509 | {
|
---|
9510 | if (vrc == VERR_NOT_SUPPORTED)
|
---|
9511 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
9512 | tr("Compacting is not yet supported for medium '%s'"),
|
---|
9513 | location.c_str());
|
---|
9514 | else if (vrc == VERR_NOT_IMPLEMENTED)
|
---|
9515 | throw setError(E_NOTIMPL,
|
---|
9516 | tr("Compacting is not implemented, medium '%s'"),
|
---|
9517 | location.c_str());
|
---|
9518 | else
|
---|
9519 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9520 | tr("Could not compact medium '%s'%s"),
|
---|
9521 | location.c_str(),
|
---|
9522 | i_vdError(vrc).c_str());
|
---|
9523 | }
|
---|
9524 | }
|
---|
9525 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9526 |
|
---|
9527 | VDDestroy(hdd);
|
---|
9528 | }
|
---|
9529 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9530 |
|
---|
9531 | /* Everything is explicitly unlocked when the task exits,
|
---|
9532 | * as the task destruction also destroys the media chain. */
|
---|
9533 |
|
---|
9534 | return rc;
|
---|
9535 | }
|
---|
9536 |
|
---|
9537 | /**
|
---|
9538 | * Implementation code for the "resize" task.
|
---|
9539 | *
|
---|
9540 | * @param task
|
---|
9541 | * @return
|
---|
9542 | */
|
---|
9543 | HRESULT Medium::i_taskResizeHandler(Medium::ResizeTask &task)
|
---|
9544 | {
|
---|
9545 | HRESULT rc = S_OK;
|
---|
9546 |
|
---|
9547 | uint64_t size = 0, logicalSize = 0;
|
---|
9548 |
|
---|
9549 | try
|
---|
9550 | {
|
---|
9551 | /* The lock is also used as a signal from the task initiator (which
|
---|
9552 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
9553 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9554 |
|
---|
9555 | PVDISK hdd;
|
---|
9556 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9557 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9558 |
|
---|
9559 | try
|
---|
9560 | {
|
---|
9561 | /* Open all media in the chain. */
|
---|
9562 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
9563 | task.mpMediumLockList->GetBegin();
|
---|
9564 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
9565 | task.mpMediumLockList->GetEnd();
|
---|
9566 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
9567 | mediumListEnd;
|
---|
9568 | --mediumListLast;
|
---|
9569 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
9570 | it != mediumListEnd;
|
---|
9571 | ++it)
|
---|
9572 | {
|
---|
9573 | const MediumLock &mediumLock = *it;
|
---|
9574 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9575 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9576 |
|
---|
9577 | /* sanity check */
|
---|
9578 | if (it == mediumListLast)
|
---|
9579 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
9580 | else
|
---|
9581 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
9582 |
|
---|
9583 | /* Open all media but last in read-only mode. Do not handle
|
---|
9584 | * shareable media, as compaction and sharing are mutually
|
---|
9585 | * exclusive. */
|
---|
9586 | vrc = VDOpen(hdd,
|
---|
9587 | pMedium->m->strFormat.c_str(),
|
---|
9588 | pMedium->m->strLocationFull.c_str(),
|
---|
9589 | m->uOpenFlagsDef | (it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
9590 | pMedium->m->vdImageIfaces);
|
---|
9591 | if (RT_FAILURE(vrc))
|
---|
9592 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9593 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9594 | pMedium->m->strLocationFull.c_str(),
|
---|
9595 | i_vdError(vrc).c_str());
|
---|
9596 | }
|
---|
9597 |
|
---|
9598 | Assert(m->state == MediumState_LockedWrite);
|
---|
9599 |
|
---|
9600 | Utf8Str location(m->strLocationFull);
|
---|
9601 |
|
---|
9602 | /* unlock before the potentially lengthy operation */
|
---|
9603 | thisLock.release();
|
---|
9604 |
|
---|
9605 | VDGEOMETRY geo = {0, 0, 0}; /* auto */
|
---|
9606 | vrc = VDResize(hdd, task.mSize, &geo, &geo, task.mVDOperationIfaces);
|
---|
9607 | if (RT_FAILURE(vrc))
|
---|
9608 | {
|
---|
9609 | if (vrc == VERR_NOT_SUPPORTED)
|
---|
9610 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
9611 | tr("Resizing to new size %llu is not yet supported for medium '%s'"),
|
---|
9612 | task.mSize, location.c_str());
|
---|
9613 | else if (vrc == VERR_NOT_IMPLEMENTED)
|
---|
9614 | throw setError(E_NOTIMPL,
|
---|
9615 | tr("Resiting is not implemented, medium '%s'"),
|
---|
9616 | location.c_str());
|
---|
9617 | else
|
---|
9618 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9619 | tr("Could not resize medium '%s'%s"),
|
---|
9620 | location.c_str(),
|
---|
9621 | i_vdError(vrc).c_str());
|
---|
9622 | }
|
---|
9623 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
9624 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
9625 | }
|
---|
9626 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9627 |
|
---|
9628 | VDDestroy(hdd);
|
---|
9629 | }
|
---|
9630 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9631 |
|
---|
9632 | if (SUCCEEDED(rc))
|
---|
9633 | {
|
---|
9634 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9635 | m->size = size;
|
---|
9636 | m->logicalSize = logicalSize;
|
---|
9637 | }
|
---|
9638 |
|
---|
9639 | /* Everything is explicitly unlocked when the task exits,
|
---|
9640 | * as the task destruction also destroys the media chain. */
|
---|
9641 |
|
---|
9642 | return rc;
|
---|
9643 | }
|
---|
9644 |
|
---|
9645 | /**
|
---|
9646 | * Implementation code for the "import" task.
|
---|
9647 | *
|
---|
9648 | * This only gets started from Medium::importFile() and always runs
|
---|
9649 | * asynchronously. It potentially touches the media registry, so we
|
---|
9650 | * always save the VirtualBox.xml file when we're done here.
|
---|
9651 | *
|
---|
9652 | * @param task
|
---|
9653 | * @return
|
---|
9654 | */
|
---|
9655 | HRESULT Medium::i_taskImportHandler(Medium::ImportTask &task)
|
---|
9656 | {
|
---|
9657 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
9658 | * to lock order violations, it probably causes lock order issues related
|
---|
9659 | * to the AutoCaller usage. */
|
---|
9660 | HRESULT rcTmp = S_OK;
|
---|
9661 |
|
---|
9662 | const ComObjPtr<Medium> &pParent = task.mParent;
|
---|
9663 |
|
---|
9664 | bool fCreatingTarget = false;
|
---|
9665 |
|
---|
9666 | uint64_t size = 0, logicalSize = 0;
|
---|
9667 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
9668 | bool fGenerateUuid = false;
|
---|
9669 |
|
---|
9670 | try
|
---|
9671 | {
|
---|
9672 | if (!pParent.isNull())
|
---|
9673 | if (pParent->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
9674 | {
|
---|
9675 | AutoReadLock plock(pParent COMMA_LOCKVAL_SRC_POS);
|
---|
9676 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
9677 | tr("Cannot import image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
9678 | pParent->m->strLocationFull.c_str());
|
---|
9679 | }
|
---|
9680 |
|
---|
9681 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
9682 | * signal from the task initiator (which releases it only after
|
---|
9683 | * RTThreadCreate()) that we can start the job. */
|
---|
9684 | AutoMultiWriteLock2 thisLock(this, pParent COMMA_LOCKVAL_SRC_POS);
|
---|
9685 |
|
---|
9686 | fCreatingTarget = m->state == MediumState_Creating;
|
---|
9687 |
|
---|
9688 | /* The object may request a specific UUID (through a special form of
|
---|
9689 | * the setLocation() argument). Otherwise we have to generate it */
|
---|
9690 | Guid targetId = m->id;
|
---|
9691 |
|
---|
9692 | fGenerateUuid = targetId.isZero();
|
---|
9693 | if (fGenerateUuid)
|
---|
9694 | {
|
---|
9695 | targetId.create();
|
---|
9696 | /* VirtualBox::i_registerMedium() will need UUID */
|
---|
9697 | unconst(m->id) = targetId;
|
---|
9698 | }
|
---|
9699 |
|
---|
9700 |
|
---|
9701 | PVDISK hdd;
|
---|
9702 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9703 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9704 |
|
---|
9705 | try
|
---|
9706 | {
|
---|
9707 | /* Open source medium. */
|
---|
9708 | vrc = VDOpen(hdd,
|
---|
9709 | task.mFormat->i_getId().c_str(),
|
---|
9710 | task.mFilename.c_str(),
|
---|
9711 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SEQUENTIAL | m->uOpenFlagsDef,
|
---|
9712 | task.mVDImageIfaces);
|
---|
9713 | if (RT_FAILURE(vrc))
|
---|
9714 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9715 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9716 | task.mFilename.c_str(),
|
---|
9717 | i_vdError(vrc).c_str());
|
---|
9718 |
|
---|
9719 | Utf8Str targetFormat(m->strFormat);
|
---|
9720 | Utf8Str targetLocation(m->strLocationFull);
|
---|
9721 | uint64_t capabilities = task.mFormat->i_getCapabilities();
|
---|
9722 |
|
---|
9723 | Assert( m->state == MediumState_Creating
|
---|
9724 | || m->state == MediumState_LockedWrite);
|
---|
9725 | Assert( pParent.isNull()
|
---|
9726 | || pParent->m->state == MediumState_LockedRead);
|
---|
9727 |
|
---|
9728 | /* unlock before the potentially lengthy operation */
|
---|
9729 | thisLock.release();
|
---|
9730 |
|
---|
9731 | /* ensure the target directory exists */
|
---|
9732 | if (capabilities & MediumFormatCapabilities_File)
|
---|
9733 | {
|
---|
9734 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
9735 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
9736 | if (FAILED(rc))
|
---|
9737 | throw rc;
|
---|
9738 | }
|
---|
9739 |
|
---|
9740 | PVDISK targetHdd;
|
---|
9741 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &targetHdd);
|
---|
9742 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9743 |
|
---|
9744 | try
|
---|
9745 | {
|
---|
9746 | /* Open all media in the target chain. */
|
---|
9747 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
9748 | task.mpTargetMediumLockList->GetBegin();
|
---|
9749 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
9750 | task.mpTargetMediumLockList->GetEnd();
|
---|
9751 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
9752 | it != targetListEnd;
|
---|
9753 | ++it)
|
---|
9754 | {
|
---|
9755 | const MediumLock &mediumLock = *it;
|
---|
9756 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9757 |
|
---|
9758 | /* If the target medium is not created yet there's no
|
---|
9759 | * reason to open it. */
|
---|
9760 | if (pMedium == this && fCreatingTarget)
|
---|
9761 | continue;
|
---|
9762 |
|
---|
9763 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9764 |
|
---|
9765 | /* sanity check */
|
---|
9766 | Assert( pMedium->m->state == MediumState_LockedRead
|
---|
9767 | || pMedium->m->state == MediumState_LockedWrite);
|
---|
9768 |
|
---|
9769 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
9770 | if (pMedium->m->state != MediumState_LockedWrite)
|
---|
9771 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
9772 | if (pMedium->m->type == MediumType_Shareable)
|
---|
9773 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
9774 |
|
---|
9775 | /* Open all media in appropriate mode. */
|
---|
9776 | vrc = VDOpen(targetHdd,
|
---|
9777 | pMedium->m->strFormat.c_str(),
|
---|
9778 | pMedium->m->strLocationFull.c_str(),
|
---|
9779 | uOpenFlags | m->uOpenFlagsDef,
|
---|
9780 | pMedium->m->vdImageIfaces);
|
---|
9781 | if (RT_FAILURE(vrc))
|
---|
9782 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9783 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9784 | pMedium->m->strLocationFull.c_str(),
|
---|
9785 | i_vdError(vrc).c_str());
|
---|
9786 | }
|
---|
9787 |
|
---|
9788 | vrc = VDCopy(hdd,
|
---|
9789 | VD_LAST_IMAGE,
|
---|
9790 | targetHdd,
|
---|
9791 | targetFormat.c_str(),
|
---|
9792 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
9793 | false /* fMoveByRename */,
|
---|
9794 | 0 /* cbSize */,
|
---|
9795 | task.mVariant & ~MediumVariant_NoCreateDir,
|
---|
9796 | targetId.raw(),
|
---|
9797 | VD_OPEN_FLAGS_NORMAL,
|
---|
9798 | NULL /* pVDIfsOperation */,
|
---|
9799 | m->vdImageIfaces,
|
---|
9800 | task.mVDOperationIfaces);
|
---|
9801 | if (RT_FAILURE(vrc))
|
---|
9802 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9803 | tr("Could not create the imported medium '%s'%s"),
|
---|
9804 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
9805 |
|
---|
9806 | size = VDGetFileSize(targetHdd, VD_LAST_IMAGE);
|
---|
9807 | logicalSize = VDGetSize(targetHdd, VD_LAST_IMAGE);
|
---|
9808 | unsigned uImageFlags;
|
---|
9809 | vrc = VDGetImageFlags(targetHdd, 0, &uImageFlags);
|
---|
9810 | if (RT_SUCCESS(vrc))
|
---|
9811 | variant = (MediumVariant_T)uImageFlags;
|
---|
9812 | }
|
---|
9813 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9814 |
|
---|
9815 | VDDestroy(targetHdd);
|
---|
9816 | }
|
---|
9817 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9818 |
|
---|
9819 | VDDestroy(hdd);
|
---|
9820 | }
|
---|
9821 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9822 |
|
---|
9823 | ErrorInfoKeeper eik;
|
---|
9824 | MultiResult mrc(rcTmp);
|
---|
9825 |
|
---|
9826 | /* Only do the parent changes for newly created media. */
|
---|
9827 | if (SUCCEEDED(mrc) && fCreatingTarget)
|
---|
9828 | {
|
---|
9829 | /* we set m->pParent & children() */
|
---|
9830 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
9831 |
|
---|
9832 | Assert(m->pParent.isNull());
|
---|
9833 |
|
---|
9834 | if (pParent)
|
---|
9835 | {
|
---|
9836 | /* Associate the imported medium with the parent and deassociate
|
---|
9837 | * from VirtualBox. Depth check above. */
|
---|
9838 | i_setParent(pParent);
|
---|
9839 |
|
---|
9840 | /* register with mVirtualBox as the last step and move to
|
---|
9841 | * Created state only on success (leaving an orphan file is
|
---|
9842 | * better than breaking media registry consistency) */
|
---|
9843 | eik.restore();
|
---|
9844 | ComObjPtr<Medium> pMedium;
|
---|
9845 | mrc = pParent->m->pVirtualBox->i_registerMedium(this, &pMedium,
|
---|
9846 | treeLock);
|
---|
9847 | Assert(this == pMedium);
|
---|
9848 | eik.fetch();
|
---|
9849 |
|
---|
9850 | if (FAILED(mrc))
|
---|
9851 | /* break parent association on failure to register */
|
---|
9852 | this->i_deparent(); // removes target from parent
|
---|
9853 | }
|
---|
9854 | else
|
---|
9855 | {
|
---|
9856 | /* just register */
|
---|
9857 | eik.restore();
|
---|
9858 | ComObjPtr<Medium> pMedium;
|
---|
9859 | mrc = m->pVirtualBox->i_registerMedium(this, &pMedium, treeLock);
|
---|
9860 | Assert(this == pMedium);
|
---|
9861 | eik.fetch();
|
---|
9862 | }
|
---|
9863 | }
|
---|
9864 |
|
---|
9865 | if (fCreatingTarget)
|
---|
9866 | {
|
---|
9867 | AutoWriteLock mLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9868 |
|
---|
9869 | if (SUCCEEDED(mrc))
|
---|
9870 | {
|
---|
9871 | m->state = MediumState_Created;
|
---|
9872 |
|
---|
9873 | m->size = size;
|
---|
9874 | m->logicalSize = logicalSize;
|
---|
9875 | m->variant = variant;
|
---|
9876 | }
|
---|
9877 | else
|
---|
9878 | {
|
---|
9879 | /* back to NotCreated on failure */
|
---|
9880 | m->state = MediumState_NotCreated;
|
---|
9881 |
|
---|
9882 | /* reset UUID to prevent it from being reused next time */
|
---|
9883 | if (fGenerateUuid)
|
---|
9884 | unconst(m->id).clear();
|
---|
9885 | }
|
---|
9886 | }
|
---|
9887 |
|
---|
9888 | // now, at the end of this task (always asynchronous), save the settings
|
---|
9889 | {
|
---|
9890 | // save the settings
|
---|
9891 | i_markRegistriesModified();
|
---|
9892 | /* collect multiple errors */
|
---|
9893 | eik.restore();
|
---|
9894 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
9895 | eik.fetch();
|
---|
9896 | }
|
---|
9897 |
|
---|
9898 | /* Everything is explicitly unlocked when the task exits,
|
---|
9899 | * as the task destruction also destroys the target chain. */
|
---|
9900 |
|
---|
9901 | /* Make sure the target chain is released early, otherwise it can
|
---|
9902 | * lead to deadlocks with concurrent IAppliance activities. */
|
---|
9903 | task.mpTargetMediumLockList->Clear();
|
---|
9904 |
|
---|
9905 | return mrc;
|
---|
9906 | }
|
---|
9907 |
|
---|
9908 | /**
|
---|
9909 | * Sets up the encryption settings for a filter.
|
---|
9910 | */
|
---|
9911 | void Medium::i_taskEncryptSettingsSetup(CryptoFilterSettings *pSettings, const char *pszCipher,
|
---|
9912 | const char *pszKeyStore, const char *pszPassword,
|
---|
9913 | bool fCreateKeyStore)
|
---|
9914 | {
|
---|
9915 | pSettings->pszCipher = pszCipher;
|
---|
9916 | pSettings->pszPassword = pszPassword;
|
---|
9917 | pSettings->pszKeyStoreLoad = pszKeyStore;
|
---|
9918 | pSettings->fCreateKeyStore = fCreateKeyStore;
|
---|
9919 | pSettings->pbDek = NULL;
|
---|
9920 | pSettings->cbDek = 0;
|
---|
9921 | pSettings->vdFilterIfaces = NULL;
|
---|
9922 |
|
---|
9923 | pSettings->vdIfCfg.pfnAreKeysValid = i_vdCryptoConfigAreKeysValid;
|
---|
9924 | pSettings->vdIfCfg.pfnQuerySize = i_vdCryptoConfigQuerySize;
|
---|
9925 | pSettings->vdIfCfg.pfnQuery = i_vdCryptoConfigQuery;
|
---|
9926 | pSettings->vdIfCfg.pfnQueryBytes = NULL;
|
---|
9927 |
|
---|
9928 | pSettings->vdIfCrypto.pfnKeyRetain = i_vdCryptoKeyRetain;
|
---|
9929 | pSettings->vdIfCrypto.pfnKeyRelease = i_vdCryptoKeyRelease;
|
---|
9930 | pSettings->vdIfCrypto.pfnKeyStorePasswordRetain = i_vdCryptoKeyStorePasswordRetain;
|
---|
9931 | pSettings->vdIfCrypto.pfnKeyStorePasswordRelease = i_vdCryptoKeyStorePasswordRelease;
|
---|
9932 | pSettings->vdIfCrypto.pfnKeyStoreSave = i_vdCryptoKeyStoreSave;
|
---|
9933 | pSettings->vdIfCrypto.pfnKeyStoreReturnParameters = i_vdCryptoKeyStoreReturnParameters;
|
---|
9934 |
|
---|
9935 | int vrc = VDInterfaceAdd(&pSettings->vdIfCfg.Core,
|
---|
9936 | "Medium::vdInterfaceCfgCrypto",
|
---|
9937 | VDINTERFACETYPE_CONFIG, pSettings,
|
---|
9938 | sizeof(VDINTERFACECONFIG), &pSettings->vdFilterIfaces);
|
---|
9939 | AssertRC(vrc);
|
---|
9940 |
|
---|
9941 | vrc = VDInterfaceAdd(&pSettings->vdIfCrypto.Core,
|
---|
9942 | "Medium::vdInterfaceCrypto",
|
---|
9943 | VDINTERFACETYPE_CRYPTO, pSettings,
|
---|
9944 | sizeof(VDINTERFACECRYPTO), &pSettings->vdFilterIfaces);
|
---|
9945 | AssertRC(vrc);
|
---|
9946 | }
|
---|
9947 |
|
---|
9948 | /**
|
---|
9949 | * Implementation code for the "encrypt" task.
|
---|
9950 | *
|
---|
9951 | * @param task
|
---|
9952 | * @return
|
---|
9953 | */
|
---|
9954 | HRESULT Medium::i_taskEncryptHandler(Medium::EncryptTask &task)
|
---|
9955 | {
|
---|
9956 | # ifndef VBOX_WITH_EXTPACK
|
---|
9957 | RT_NOREF(task);
|
---|
9958 | # endif
|
---|
9959 | HRESULT rc = S_OK;
|
---|
9960 |
|
---|
9961 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
9962 | * signal from the task initiator (which releases it only after
|
---|
9963 | * RTThreadCreate()) that we can start the job. */
|
---|
9964 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
9965 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9966 |
|
---|
9967 | try
|
---|
9968 | {
|
---|
9969 | # ifdef VBOX_WITH_EXTPACK
|
---|
9970 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
9971 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
9972 | {
|
---|
9973 | /* Load the plugin */
|
---|
9974 | Utf8Str strPlugin;
|
---|
9975 | rc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
9976 | if (SUCCEEDED(rc))
|
---|
9977 | {
|
---|
9978 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
9979 | if (RT_FAILURE(vrc))
|
---|
9980 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
9981 | tr("Encrypting the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
9982 | i_vdError(vrc).c_str());
|
---|
9983 | }
|
---|
9984 | else
|
---|
9985 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
9986 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
9987 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
9988 | }
|
---|
9989 | else
|
---|
9990 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
9991 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
9992 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
9993 |
|
---|
9994 | PVDISK pDisk = NULL;
|
---|
9995 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
9996 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9997 |
|
---|
9998 | Medium::CryptoFilterSettings CryptoSettingsRead;
|
---|
9999 | Medium::CryptoFilterSettings CryptoSettingsWrite;
|
---|
10000 |
|
---|
10001 | void *pvBuf = NULL;
|
---|
10002 | const char *pszPasswordNew = NULL;
|
---|
10003 | try
|
---|
10004 | {
|
---|
10005 | /* Set up disk encryption filters. */
|
---|
10006 | if (task.mstrCurrentPassword.isEmpty())
|
---|
10007 | {
|
---|
10008 | /*
|
---|
10009 | * Query whether the medium property indicating that encryption is
|
---|
10010 | * configured is existing.
|
---|
10011 | */
|
---|
10012 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
10013 | if (it != pBase->m->mapProperties.end())
|
---|
10014 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
10015 | tr("The password given for the encrypted image is incorrect"));
|
---|
10016 | }
|
---|
10017 | else
|
---|
10018 | {
|
---|
10019 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
10020 | if (it == pBase->m->mapProperties.end())
|
---|
10021 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10022 | tr("The image is not configured for encryption"));
|
---|
10023 |
|
---|
10024 | i_taskEncryptSettingsSetup(&CryptoSettingsRead, NULL, it->second.c_str(), task.mstrCurrentPassword.c_str(),
|
---|
10025 | false /* fCreateKeyStore */);
|
---|
10026 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ, CryptoSettingsRead.vdFilterIfaces);
|
---|
10027 | if (vrc == VERR_VD_PASSWORD_INCORRECT)
|
---|
10028 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
10029 | tr("The password to decrypt the image is incorrect"));
|
---|
10030 | else if (RT_FAILURE(vrc))
|
---|
10031 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10032 | tr("Failed to load the decryption filter: %s"),
|
---|
10033 | i_vdError(vrc).c_str());
|
---|
10034 | }
|
---|
10035 |
|
---|
10036 | if (task.mstrCipher.isNotEmpty())
|
---|
10037 | {
|
---|
10038 | if ( task.mstrNewPassword.isEmpty()
|
---|
10039 | && task.mstrNewPasswordId.isEmpty()
|
---|
10040 | && task.mstrCurrentPassword.isNotEmpty())
|
---|
10041 | {
|
---|
10042 | /* An empty password and password ID will default to the current password. */
|
---|
10043 | pszPasswordNew = task.mstrCurrentPassword.c_str();
|
---|
10044 | }
|
---|
10045 | else if (task.mstrNewPassword.isEmpty())
|
---|
10046 | throw setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
10047 | tr("A password must be given for the image encryption"));
|
---|
10048 | else if (task.mstrNewPasswordId.isEmpty())
|
---|
10049 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10050 | tr("A valid identifier for the password must be given"));
|
---|
10051 | else
|
---|
10052 | pszPasswordNew = task.mstrNewPassword.c_str();
|
---|
10053 |
|
---|
10054 | i_taskEncryptSettingsSetup(&CryptoSettingsWrite, task.mstrCipher.c_str(), NULL,
|
---|
10055 | pszPasswordNew, true /* fCreateKeyStore */);
|
---|
10056 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_WRITE, CryptoSettingsWrite.vdFilterIfaces);
|
---|
10057 | if (RT_FAILURE(vrc))
|
---|
10058 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10059 | tr("Failed to load the encryption filter: %s"),
|
---|
10060 | i_vdError(vrc).c_str());
|
---|
10061 | }
|
---|
10062 | else if (task.mstrNewPasswordId.isNotEmpty() || task.mstrNewPassword.isNotEmpty())
|
---|
10063 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10064 | tr("The password and password identifier must be empty if the output should be unencrypted"));
|
---|
10065 |
|
---|
10066 | /* Open all media in the chain. */
|
---|
10067 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
10068 | task.mpMediumLockList->GetBegin();
|
---|
10069 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
10070 | task.mpMediumLockList->GetEnd();
|
---|
10071 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
10072 | mediumListEnd;
|
---|
10073 | --mediumListLast;
|
---|
10074 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
10075 | it != mediumListEnd;
|
---|
10076 | ++it)
|
---|
10077 | {
|
---|
10078 | const MediumLock &mediumLock = *it;
|
---|
10079 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
10080 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
10081 |
|
---|
10082 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
10083 |
|
---|
10084 | /* Open all media but last in read-only mode. Do not handle
|
---|
10085 | * shareable media, as compaction and sharing are mutually
|
---|
10086 | * exclusive. */
|
---|
10087 | vrc = VDOpen(pDisk,
|
---|
10088 | pMedium->m->strFormat.c_str(),
|
---|
10089 | pMedium->m->strLocationFull.c_str(),
|
---|
10090 | m->uOpenFlagsDef | (it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
10091 | pMedium->m->vdImageIfaces);
|
---|
10092 | if (RT_FAILURE(vrc))
|
---|
10093 | throw setError(VBOX_E_FILE_ERROR,
|
---|
10094 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
10095 | pMedium->m->strLocationFull.c_str(),
|
---|
10096 | i_vdError(vrc).c_str());
|
---|
10097 | }
|
---|
10098 |
|
---|
10099 | Assert(m->state == MediumState_LockedWrite);
|
---|
10100 |
|
---|
10101 | Utf8Str location(m->strLocationFull);
|
---|
10102 |
|
---|
10103 | /* unlock before the potentially lengthy operation */
|
---|
10104 | thisLock.release();
|
---|
10105 |
|
---|
10106 | vrc = VDPrepareWithFilters(pDisk, task.mVDOperationIfaces);
|
---|
10107 | if (RT_FAILURE(vrc))
|
---|
10108 | throw setError(VBOX_E_FILE_ERROR,
|
---|
10109 | tr("Could not prepare disk images for encryption (%Rrc): %s"),
|
---|
10110 | vrc, i_vdError(vrc).c_str());
|
---|
10111 |
|
---|
10112 | thisLock.acquire();
|
---|
10113 | /* If everything went well set the new key store. */
|
---|
10114 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
10115 | if (it != pBase->m->mapProperties.end())
|
---|
10116 | pBase->m->mapProperties.erase(it);
|
---|
10117 |
|
---|
10118 | /* Delete KeyId if encryption is removed or the password did change. */
|
---|
10119 | if ( task.mstrNewPasswordId.isNotEmpty()
|
---|
10120 | || task.mstrCipher.isEmpty())
|
---|
10121 | {
|
---|
10122 | it = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
10123 | if (it != pBase->m->mapProperties.end())
|
---|
10124 | pBase->m->mapProperties.erase(it);
|
---|
10125 | }
|
---|
10126 |
|
---|
10127 | if (CryptoSettingsWrite.pszKeyStore)
|
---|
10128 | {
|
---|
10129 | pBase->m->mapProperties["CRYPT/KeyStore"] = Utf8Str(CryptoSettingsWrite.pszKeyStore);
|
---|
10130 | if (task.mstrNewPasswordId.isNotEmpty())
|
---|
10131 | pBase->m->mapProperties["CRYPT/KeyId"] = task.mstrNewPasswordId;
|
---|
10132 | }
|
---|
10133 |
|
---|
10134 | if (CryptoSettingsRead.pszCipherReturned)
|
---|
10135 | RTStrFree(CryptoSettingsRead.pszCipherReturned);
|
---|
10136 |
|
---|
10137 | if (CryptoSettingsWrite.pszCipherReturned)
|
---|
10138 | RTStrFree(CryptoSettingsWrite.pszCipherReturned);
|
---|
10139 |
|
---|
10140 | thisLock.release();
|
---|
10141 | pBase->i_markRegistriesModified();
|
---|
10142 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
10143 | }
|
---|
10144 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10145 |
|
---|
10146 | if (pvBuf)
|
---|
10147 | RTMemFree(pvBuf);
|
---|
10148 |
|
---|
10149 | VDDestroy(pDisk);
|
---|
10150 | # else
|
---|
10151 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
10152 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
10153 | # endif
|
---|
10154 | }
|
---|
10155 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10156 |
|
---|
10157 | /* Everything is explicitly unlocked when the task exits,
|
---|
10158 | * as the task destruction also destroys the media chain. */
|
---|
10159 |
|
---|
10160 | return rc;
|
---|
10161 | }
|
---|
10162 |
|
---|
10163 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|