1 | /* $Id: MediumImpl.cpp 64199 2016-10-11 09:08:45Z 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::ExportTask : public Medium::Task
|
---|
654 | {
|
---|
655 | public:
|
---|
656 | ExportTask(Medium *aMedium,
|
---|
657 | Progress *aProgress,
|
---|
658 | const char *aFilename,
|
---|
659 | MediumFormat *aFormat,
|
---|
660 | MediumVariant_T aVariant,
|
---|
661 | SecretKeyStore *pSecretKeyStore,
|
---|
662 | VDINTERFACEIO *aVDImageIOIf,
|
---|
663 | void *aVDImageIOUser,
|
---|
664 | MediumLockList *aSourceMediumLockList,
|
---|
665 | bool fKeepSourceMediumLockList = false)
|
---|
666 | : Medium::Task(aMedium, aProgress),
|
---|
667 | mpSourceMediumLockList(aSourceMediumLockList),
|
---|
668 | mFilename(aFilename),
|
---|
669 | mFormat(aFormat),
|
---|
670 | mVariant(aVariant),
|
---|
671 | m_pSecretKeyStore(pSecretKeyStore),
|
---|
672 | mfKeepSourceMediumLockList(fKeepSourceMediumLockList)
|
---|
673 | {
|
---|
674 | AssertReturnVoidStmt(aSourceMediumLockList != NULL, mRC = E_FAIL);
|
---|
675 |
|
---|
676 | mVDImageIfaces = aMedium->m->vdImageIfaces;
|
---|
677 | if (aVDImageIOIf)
|
---|
678 | {
|
---|
679 | int vrc = VDInterfaceAdd(&aVDImageIOIf->Core, "Medium::vdInterfaceIO",
|
---|
680 | VDINTERFACETYPE_IO, aVDImageIOUser,
|
---|
681 | sizeof(VDINTERFACEIO), &mVDImageIfaces);
|
---|
682 | AssertRCReturnVoidStmt(vrc, mRC = E_FAIL);
|
---|
683 | }
|
---|
684 | m_strTaskName = "createExport";
|
---|
685 | }
|
---|
686 |
|
---|
687 | ~ExportTask()
|
---|
688 | {
|
---|
689 | if (!mfKeepSourceMediumLockList && mpSourceMediumLockList)
|
---|
690 | delete mpSourceMediumLockList;
|
---|
691 | }
|
---|
692 |
|
---|
693 | MediumLockList *mpSourceMediumLockList;
|
---|
694 | Utf8Str mFilename;
|
---|
695 | ComObjPtr<MediumFormat> mFormat;
|
---|
696 | MediumVariant_T mVariant;
|
---|
697 | PVDINTERFACE mVDImageIfaces;
|
---|
698 | SecretKeyStore *m_pSecretKeyStore;
|
---|
699 |
|
---|
700 | private:
|
---|
701 | HRESULT executeTask();
|
---|
702 | bool mfKeepSourceMediumLockList;
|
---|
703 | };
|
---|
704 |
|
---|
705 | class Medium::ImportTask : public Medium::Task
|
---|
706 | {
|
---|
707 | public:
|
---|
708 | ImportTask(Medium *aMedium,
|
---|
709 | Progress *aProgress,
|
---|
710 | const char *aFilename,
|
---|
711 | MediumFormat *aFormat,
|
---|
712 | MediumVariant_T aVariant,
|
---|
713 | RTVFSIOSTREAM aVfsIosSrc,
|
---|
714 | Medium *aParent,
|
---|
715 | MediumLockList *aTargetMediumLockList,
|
---|
716 | bool fKeepTargetMediumLockList = false)
|
---|
717 | : Medium::Task(aMedium, aProgress),
|
---|
718 | mFilename(aFilename),
|
---|
719 | mFormat(aFormat),
|
---|
720 | mVariant(aVariant),
|
---|
721 | mParent(aParent),
|
---|
722 | mpTargetMediumLockList(aTargetMediumLockList),
|
---|
723 | mpVfsIoIf(NULL),
|
---|
724 | mParentCaller(aParent),
|
---|
725 | mfKeepTargetMediumLockList(fKeepTargetMediumLockList)
|
---|
726 | {
|
---|
727 | AssertReturnVoidStmt(aTargetMediumLockList != NULL, mRC = E_FAIL);
|
---|
728 | /* aParent may be NULL */
|
---|
729 | mRC = mParentCaller.rc();
|
---|
730 | if (FAILED(mRC))
|
---|
731 | return;
|
---|
732 |
|
---|
733 | mVDImageIfaces = aMedium->m->vdImageIfaces;
|
---|
734 |
|
---|
735 | int vrc = VDIfCreateFromVfsStream(aVfsIosSrc, RTFILE_O_READ, &mpVfsIoIf);
|
---|
736 | AssertRCReturnVoidStmt(vrc, mRC = E_FAIL);
|
---|
737 |
|
---|
738 | vrc = VDInterfaceAdd(&mpVfsIoIf->Core, "Medium::ImportTaskVfsIos",
|
---|
739 | VDINTERFACETYPE_IO, mpVfsIoIf,
|
---|
740 | sizeof(VDINTERFACEIO), &mVDImageIfaces);
|
---|
741 | AssertRCReturnVoidStmt(vrc, mRC = E_FAIL);
|
---|
742 | m_strTaskName = "createImport";
|
---|
743 | }
|
---|
744 |
|
---|
745 | ~ImportTask()
|
---|
746 | {
|
---|
747 | if (!mfKeepTargetMediumLockList && mpTargetMediumLockList)
|
---|
748 | delete mpTargetMediumLockList;
|
---|
749 | if (mpVfsIoIf)
|
---|
750 | {
|
---|
751 | VDIfDestroyFromVfsStream(mpVfsIoIf);
|
---|
752 | mpVfsIoIf = NULL;
|
---|
753 | }
|
---|
754 | }
|
---|
755 |
|
---|
756 | Utf8Str mFilename;
|
---|
757 | ComObjPtr<MediumFormat> mFormat;
|
---|
758 | MediumVariant_T mVariant;
|
---|
759 | const ComObjPtr<Medium> mParent;
|
---|
760 | MediumLockList *mpTargetMediumLockList;
|
---|
761 | PVDINTERFACE mVDImageIfaces;
|
---|
762 | PVDINTERFACEIO mpVfsIoIf; /**< Pointer to the VFS I/O stream to VD I/O interface wrapper. */
|
---|
763 |
|
---|
764 | private:
|
---|
765 | HRESULT executeTask();
|
---|
766 | AutoCaller mParentCaller;
|
---|
767 | bool mfKeepTargetMediumLockList;
|
---|
768 | };
|
---|
769 |
|
---|
770 | class Medium::EncryptTask : public Medium::Task
|
---|
771 | {
|
---|
772 | public:
|
---|
773 | EncryptTask(Medium *aMedium,
|
---|
774 | const com::Utf8Str &strNewPassword,
|
---|
775 | const com::Utf8Str &strCurrentPassword,
|
---|
776 | const com::Utf8Str &strCipher,
|
---|
777 | const com::Utf8Str &strNewPasswordId,
|
---|
778 | Progress *aProgress,
|
---|
779 | MediumLockList *aMediumLockList)
|
---|
780 | : Medium::Task(aMedium, aProgress),
|
---|
781 | mstrNewPassword(strNewPassword),
|
---|
782 | mstrCurrentPassword(strCurrentPassword),
|
---|
783 | mstrCipher(strCipher),
|
---|
784 | mstrNewPasswordId(strNewPasswordId),
|
---|
785 | mpMediumLockList(aMediumLockList)
|
---|
786 | {
|
---|
787 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
788 | /* aParent may be NULL */
|
---|
789 | mRC = mParentCaller.rc();
|
---|
790 | if (FAILED(mRC))
|
---|
791 | return;
|
---|
792 |
|
---|
793 | mVDImageIfaces = aMedium->m->vdImageIfaces;
|
---|
794 | m_strTaskName = "createEncrypt";
|
---|
795 | }
|
---|
796 |
|
---|
797 | ~EncryptTask()
|
---|
798 | {
|
---|
799 | if (mstrNewPassword.length())
|
---|
800 | RTMemWipeThoroughly(mstrNewPassword.mutableRaw(), mstrNewPassword.length(), 10 /* cPasses */);
|
---|
801 | if (mstrCurrentPassword.length())
|
---|
802 | RTMemWipeThoroughly(mstrCurrentPassword.mutableRaw(), mstrCurrentPassword.length(), 10 /* cPasses */);
|
---|
803 |
|
---|
804 | /* Keep any errors which might be set when deleting the lock list. */
|
---|
805 | ErrorInfoKeeper eik;
|
---|
806 | delete mpMediumLockList;
|
---|
807 | }
|
---|
808 |
|
---|
809 | Utf8Str mstrNewPassword;
|
---|
810 | Utf8Str mstrCurrentPassword;
|
---|
811 | Utf8Str mstrCipher;
|
---|
812 | Utf8Str mstrNewPasswordId;
|
---|
813 | MediumLockList *mpMediumLockList;
|
---|
814 | PVDINTERFACE mVDImageIfaces;
|
---|
815 |
|
---|
816 | private:
|
---|
817 | HRESULT executeTask();
|
---|
818 | AutoCaller mParentCaller;
|
---|
819 | };
|
---|
820 |
|
---|
821 | /**
|
---|
822 | * Settings for a crypto filter instance.
|
---|
823 | */
|
---|
824 | struct Medium::CryptoFilterSettings
|
---|
825 | {
|
---|
826 | CryptoFilterSettings()
|
---|
827 | : fCreateKeyStore(false),
|
---|
828 | pszPassword(NULL),
|
---|
829 | pszKeyStore(NULL),
|
---|
830 | pszKeyStoreLoad(NULL),
|
---|
831 | pbDek(NULL),
|
---|
832 | cbDek(0),
|
---|
833 | pszCipher(NULL),
|
---|
834 | pszCipherReturned(NULL)
|
---|
835 | { }
|
---|
836 |
|
---|
837 | bool fCreateKeyStore;
|
---|
838 | const char *pszPassword;
|
---|
839 | char *pszKeyStore;
|
---|
840 | const char *pszKeyStoreLoad;
|
---|
841 |
|
---|
842 | const uint8_t *pbDek;
|
---|
843 | size_t cbDek;
|
---|
844 | const char *pszCipher;
|
---|
845 |
|
---|
846 | /** The cipher returned by the crypto filter. */
|
---|
847 | char *pszCipherReturned;
|
---|
848 |
|
---|
849 | PVDINTERFACE vdFilterIfaces;
|
---|
850 |
|
---|
851 | VDINTERFACECONFIG vdIfCfg;
|
---|
852 | VDINTERFACECRYPTO vdIfCrypto;
|
---|
853 | };
|
---|
854 |
|
---|
855 | /**
|
---|
856 | * PFNVDPROGRESS callback handler for Task operations.
|
---|
857 | *
|
---|
858 | * @param pvUser Pointer to the Progress instance.
|
---|
859 | * @param uPercent Completion percentage (0-100).
|
---|
860 | */
|
---|
861 | /*static*/
|
---|
862 | DECLCALLBACK(int) Medium::Task::vdProgressCall(void *pvUser, unsigned uPercent)
|
---|
863 | {
|
---|
864 | Progress *that = static_cast<Progress *>(pvUser);
|
---|
865 |
|
---|
866 | if (that != NULL)
|
---|
867 | {
|
---|
868 | /* update the progress object, capping it at 99% as the final percent
|
---|
869 | * is used for additional operations like setting the UUIDs and similar. */
|
---|
870 | HRESULT rc = that->SetCurrentOperationProgress(uPercent * 99 / 100);
|
---|
871 | if (FAILED(rc))
|
---|
872 | {
|
---|
873 | if (rc == E_FAIL)
|
---|
874 | return VERR_CANCELLED;
|
---|
875 | else
|
---|
876 | return VERR_INVALID_STATE;
|
---|
877 | }
|
---|
878 | }
|
---|
879 |
|
---|
880 | return VINF_SUCCESS;
|
---|
881 | }
|
---|
882 |
|
---|
883 | /**
|
---|
884 | * Implementation code for the "create base" task.
|
---|
885 | */
|
---|
886 | HRESULT Medium::CreateBaseTask::executeTask()
|
---|
887 | {
|
---|
888 | return mMedium->i_taskCreateBaseHandler(*this);
|
---|
889 | }
|
---|
890 |
|
---|
891 | /**
|
---|
892 | * Implementation code for the "create diff" task.
|
---|
893 | */
|
---|
894 | HRESULT Medium::CreateDiffTask::executeTask()
|
---|
895 | {
|
---|
896 | return mMedium->i_taskCreateDiffHandler(*this);
|
---|
897 | }
|
---|
898 |
|
---|
899 | /**
|
---|
900 | * Implementation code for the "clone" task.
|
---|
901 | */
|
---|
902 | HRESULT Medium::CloneTask::executeTask()
|
---|
903 | {
|
---|
904 | return mMedium->i_taskCloneHandler(*this);
|
---|
905 | }
|
---|
906 |
|
---|
907 | /**
|
---|
908 | * Implementation code for the "move" task.
|
---|
909 | */
|
---|
910 | HRESULT Medium::MoveTask::executeTask()
|
---|
911 | {
|
---|
912 | return mMedium->i_taskMoveHandler(*this);
|
---|
913 | }
|
---|
914 |
|
---|
915 | /**
|
---|
916 | * Implementation code for the "compact" task.
|
---|
917 | */
|
---|
918 | HRESULT Medium::CompactTask::executeTask()
|
---|
919 | {
|
---|
920 | return mMedium->i_taskCompactHandler(*this);
|
---|
921 | }
|
---|
922 |
|
---|
923 | /**
|
---|
924 | * Implementation code for the "resize" task.
|
---|
925 | */
|
---|
926 | HRESULT Medium::ResizeTask::executeTask()
|
---|
927 | {
|
---|
928 | return mMedium->i_taskResizeHandler(*this);
|
---|
929 | }
|
---|
930 |
|
---|
931 |
|
---|
932 | /**
|
---|
933 | * Implementation code for the "reset" task.
|
---|
934 | */
|
---|
935 | HRESULT Medium::ResetTask::executeTask()
|
---|
936 | {
|
---|
937 | return mMedium->i_taskResetHandler(*this);
|
---|
938 | }
|
---|
939 |
|
---|
940 | /**
|
---|
941 | * Implementation code for the "delete" task.
|
---|
942 | */
|
---|
943 | HRESULT Medium::DeleteTask::executeTask()
|
---|
944 | {
|
---|
945 | return mMedium->i_taskDeleteHandler(*this);
|
---|
946 | }
|
---|
947 |
|
---|
948 | /**
|
---|
949 | * Implementation code for the "merge" task.
|
---|
950 | */
|
---|
951 | HRESULT Medium::MergeTask::executeTask()
|
---|
952 | {
|
---|
953 | return mMedium->i_taskMergeHandler(*this);
|
---|
954 | }
|
---|
955 |
|
---|
956 | /**
|
---|
957 | * Implementation code for the "export" task.
|
---|
958 | */
|
---|
959 | HRESULT Medium::ExportTask::executeTask()
|
---|
960 | {
|
---|
961 | return mMedium->i_taskExportHandler(*this);
|
---|
962 | }
|
---|
963 |
|
---|
964 | /**
|
---|
965 | * Implementation code for the "import" task.
|
---|
966 | */
|
---|
967 | HRESULT Medium::ImportTask::executeTask()
|
---|
968 | {
|
---|
969 | return mMedium->i_taskImportHandler(*this);
|
---|
970 | }
|
---|
971 |
|
---|
972 | /**
|
---|
973 | * Implementation code for the "encrypt" task.
|
---|
974 | */
|
---|
975 | HRESULT Medium::EncryptTask::executeTask()
|
---|
976 | {
|
---|
977 | return mMedium->i_taskEncryptHandler(*this);
|
---|
978 | }
|
---|
979 |
|
---|
980 | ////////////////////////////////////////////////////////////////////////////////
|
---|
981 | //
|
---|
982 | // Medium constructor / destructor
|
---|
983 | //
|
---|
984 | ////////////////////////////////////////////////////////////////////////////////
|
---|
985 |
|
---|
986 | DEFINE_EMPTY_CTOR_DTOR(Medium)
|
---|
987 |
|
---|
988 | HRESULT Medium::FinalConstruct()
|
---|
989 | {
|
---|
990 | m = new Data;
|
---|
991 |
|
---|
992 | /* Initialize the callbacks of the VD error interface */
|
---|
993 | m->vdIfError.pfnError = i_vdErrorCall;
|
---|
994 | m->vdIfError.pfnMessage = NULL;
|
---|
995 |
|
---|
996 | /* Initialize the callbacks of the VD config interface */
|
---|
997 | m->vdIfConfig.pfnAreKeysValid = i_vdConfigAreKeysValid;
|
---|
998 | m->vdIfConfig.pfnQuerySize = i_vdConfigQuerySize;
|
---|
999 | m->vdIfConfig.pfnQuery = i_vdConfigQuery;
|
---|
1000 | m->vdIfConfig.pfnQueryBytes = NULL;
|
---|
1001 |
|
---|
1002 | /* Initialize the callbacks of the VD TCP interface (we always use the host
|
---|
1003 | * IP stack for now) */
|
---|
1004 | m->vdIfTcpNet.pfnSocketCreate = i_vdTcpSocketCreate;
|
---|
1005 | m->vdIfTcpNet.pfnSocketDestroy = i_vdTcpSocketDestroy;
|
---|
1006 | m->vdIfTcpNet.pfnClientConnect = i_vdTcpClientConnect;
|
---|
1007 | m->vdIfTcpNet.pfnClientClose = i_vdTcpClientClose;
|
---|
1008 | m->vdIfTcpNet.pfnIsClientConnected = i_vdTcpIsClientConnected;
|
---|
1009 | m->vdIfTcpNet.pfnSelectOne = i_vdTcpSelectOne;
|
---|
1010 | m->vdIfTcpNet.pfnRead = i_vdTcpRead;
|
---|
1011 | m->vdIfTcpNet.pfnWrite = i_vdTcpWrite;
|
---|
1012 | m->vdIfTcpNet.pfnSgWrite = i_vdTcpSgWrite;
|
---|
1013 | m->vdIfTcpNet.pfnFlush = i_vdTcpFlush;
|
---|
1014 | m->vdIfTcpNet.pfnSetSendCoalescing = i_vdTcpSetSendCoalescing;
|
---|
1015 | m->vdIfTcpNet.pfnGetLocalAddress = i_vdTcpGetLocalAddress;
|
---|
1016 | m->vdIfTcpNet.pfnGetPeerAddress = i_vdTcpGetPeerAddress;
|
---|
1017 | m->vdIfTcpNet.pfnSelectOneEx = NULL;
|
---|
1018 | m->vdIfTcpNet.pfnPoke = NULL;
|
---|
1019 |
|
---|
1020 | /* Initialize the per-disk interface chain (could be done more globally,
|
---|
1021 | * but it's not wasting much time or space so it's not worth it). */
|
---|
1022 | int vrc;
|
---|
1023 | vrc = VDInterfaceAdd(&m->vdIfError.Core,
|
---|
1024 | "Medium::vdInterfaceError",
|
---|
1025 | VDINTERFACETYPE_ERROR, this,
|
---|
1026 | sizeof(VDINTERFACEERROR), &m->vdDiskIfaces);
|
---|
1027 | AssertRCReturn(vrc, E_FAIL);
|
---|
1028 |
|
---|
1029 | /* Initialize the per-image interface chain */
|
---|
1030 | vrc = VDInterfaceAdd(&m->vdIfConfig.Core,
|
---|
1031 | "Medium::vdInterfaceConfig",
|
---|
1032 | VDINTERFACETYPE_CONFIG, this,
|
---|
1033 | sizeof(VDINTERFACECONFIG), &m->vdImageIfaces);
|
---|
1034 | AssertRCReturn(vrc, E_FAIL);
|
---|
1035 |
|
---|
1036 | vrc = VDInterfaceAdd(&m->vdIfTcpNet.Core,
|
---|
1037 | "Medium::vdInterfaceTcpNet",
|
---|
1038 | VDINTERFACETYPE_TCPNET, this,
|
---|
1039 | sizeof(VDINTERFACETCPNET), &m->vdImageIfaces);
|
---|
1040 | AssertRCReturn(vrc, E_FAIL);
|
---|
1041 |
|
---|
1042 | return BaseFinalConstruct();
|
---|
1043 | }
|
---|
1044 |
|
---|
1045 | void Medium::FinalRelease()
|
---|
1046 | {
|
---|
1047 | uninit();
|
---|
1048 |
|
---|
1049 | delete m;
|
---|
1050 |
|
---|
1051 | BaseFinalRelease();
|
---|
1052 | }
|
---|
1053 |
|
---|
1054 | /**
|
---|
1055 | * Initializes an empty hard disk object without creating or opening an associated
|
---|
1056 | * storage unit.
|
---|
1057 | *
|
---|
1058 | * This gets called by VirtualBox::CreateMedium() in which case uuidMachineRegistry
|
---|
1059 | * is empty since starting with VirtualBox 4.0, we no longer add opened media to a
|
---|
1060 | * registry automatically (this is deferred until the medium is attached to a machine).
|
---|
1061 | *
|
---|
1062 | * This also gets called when VirtualBox creates diff images; in this case uuidMachineRegistry
|
---|
1063 | * is set to the registry of the parent image to make sure they all end up in the same
|
---|
1064 | * file.
|
---|
1065 | *
|
---|
1066 | * For hard disks that don't have the MediumFormatCapabilities_CreateFixed or
|
---|
1067 | * MediumFormatCapabilities_CreateDynamic capability (and therefore cannot be created or deleted
|
---|
1068 | * with the means of VirtualBox) the associated storage unit is assumed to be
|
---|
1069 | * ready for use so the state of the hard disk object will be set to Created.
|
---|
1070 | *
|
---|
1071 | * @param aVirtualBox VirtualBox object.
|
---|
1072 | * @param aFormat
|
---|
1073 | * @param aLocation Storage unit location.
|
---|
1074 | * @param uuidMachineRegistry The registry to which this medium should be added
|
---|
1075 | * (global registry UUID or machine UUID or empty if none).
|
---|
1076 | * @param deviceType Device Type.
|
---|
1077 | */
|
---|
1078 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1079 | const Utf8Str &aFormat,
|
---|
1080 | const Utf8Str &aLocation,
|
---|
1081 | const Guid &uuidMachineRegistry,
|
---|
1082 | const DeviceType_T aDeviceType)
|
---|
1083 | {
|
---|
1084 | AssertReturn(aVirtualBox != NULL, E_FAIL);
|
---|
1085 | AssertReturn(!aFormat.isEmpty(), E_FAIL);
|
---|
1086 |
|
---|
1087 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1088 | AutoInitSpan autoInitSpan(this);
|
---|
1089 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1090 |
|
---|
1091 | HRESULT rc = S_OK;
|
---|
1092 |
|
---|
1093 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1094 |
|
---|
1095 | if (uuidMachineRegistry.isValid() && !uuidMachineRegistry.isZero())
|
---|
1096 | m->llRegistryIDs.push_back(uuidMachineRegistry);
|
---|
1097 |
|
---|
1098 | /* no storage yet */
|
---|
1099 | m->state = MediumState_NotCreated;
|
---|
1100 |
|
---|
1101 | /* cannot be a host drive */
|
---|
1102 | m->hostDrive = false;
|
---|
1103 |
|
---|
1104 | m->devType = aDeviceType;
|
---|
1105 |
|
---|
1106 | /* No storage unit is created yet, no need to call Medium::i_queryInfo */
|
---|
1107 |
|
---|
1108 | rc = i_setFormat(aFormat);
|
---|
1109 | if (FAILED(rc)) return rc;
|
---|
1110 |
|
---|
1111 | rc = i_setLocation(aLocation);
|
---|
1112 | if (FAILED(rc)) return rc;
|
---|
1113 |
|
---|
1114 | if (!(m->formatObj->i_getCapabilities() & ( MediumFormatCapabilities_CreateFixed
|
---|
1115 | | MediumFormatCapabilities_CreateDynamic))
|
---|
1116 | )
|
---|
1117 | {
|
---|
1118 | /* Storage for mediums of this format can neither be explicitly
|
---|
1119 | * created by VirtualBox nor deleted, so we place the medium to
|
---|
1120 | * Inaccessible state here and also add it to the registry. The
|
---|
1121 | * state means that one has to use RefreshState() to update the
|
---|
1122 | * medium format specific fields. */
|
---|
1123 | m->state = MediumState_Inaccessible;
|
---|
1124 | // create new UUID
|
---|
1125 | unconst(m->id).create();
|
---|
1126 |
|
---|
1127 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1128 | ComObjPtr<Medium> pMedium;
|
---|
1129 |
|
---|
1130 | /*
|
---|
1131 | * Check whether the UUID is taken already and create a new one
|
---|
1132 | * if required.
|
---|
1133 | * Try this only a limited amount of times in case the PRNG is broken
|
---|
1134 | * in some way to prevent an endless loop.
|
---|
1135 | */
|
---|
1136 | for (unsigned i = 0; i < 5; i++)
|
---|
1137 | {
|
---|
1138 | bool fInUse;
|
---|
1139 |
|
---|
1140 | fInUse = m->pVirtualBox->i_isMediaUuidInUse(m->id, aDeviceType);
|
---|
1141 | if (fInUse)
|
---|
1142 | {
|
---|
1143 | // create new UUID
|
---|
1144 | unconst(m->id).create();
|
---|
1145 | }
|
---|
1146 | else
|
---|
1147 | break;
|
---|
1148 | }
|
---|
1149 |
|
---|
1150 | rc = m->pVirtualBox->i_registerMedium(this, &pMedium, treeLock);
|
---|
1151 | Assert(this == pMedium || FAILED(rc));
|
---|
1152 | }
|
---|
1153 |
|
---|
1154 | /* Confirm a successful initialization when it's the case */
|
---|
1155 | if (SUCCEEDED(rc))
|
---|
1156 | autoInitSpan.setSucceeded();
|
---|
1157 |
|
---|
1158 | return rc;
|
---|
1159 | }
|
---|
1160 |
|
---|
1161 | /**
|
---|
1162 | * Initializes the medium object by opening the storage unit at the specified
|
---|
1163 | * location. The enOpenMode parameter defines whether the medium will be opened
|
---|
1164 | * read/write or read-only.
|
---|
1165 | *
|
---|
1166 | * This gets called by VirtualBox::OpenMedium() and also by
|
---|
1167 | * Machine::AttachDevice() and createImplicitDiffs() when new diff
|
---|
1168 | * images are created.
|
---|
1169 | *
|
---|
1170 | * There is no registry for this case since starting with VirtualBox 4.0, we
|
---|
1171 | * no longer add opened media to a registry automatically (this is deferred
|
---|
1172 | * until the medium is attached to a machine).
|
---|
1173 | *
|
---|
1174 | * For hard disks, the UUID, format and the parent of this medium will be
|
---|
1175 | * determined when reading the medium storage unit. For DVD and floppy images,
|
---|
1176 | * which have no UUIDs in their storage units, new UUIDs are created.
|
---|
1177 | * If the detected or set parent is not known to VirtualBox, then this method
|
---|
1178 | * will fail.
|
---|
1179 | *
|
---|
1180 | * @param aVirtualBox VirtualBox object.
|
---|
1181 | * @param aLocation Storage unit location.
|
---|
1182 | * @param enOpenMode Whether to open the medium read/write or read-only.
|
---|
1183 | * @param fForceNewUuid Whether a new UUID should be set to avoid duplicates.
|
---|
1184 | * @param aDeviceType Device type of medium.
|
---|
1185 | */
|
---|
1186 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1187 | const Utf8Str &aLocation,
|
---|
1188 | HDDOpenMode enOpenMode,
|
---|
1189 | bool fForceNewUuid,
|
---|
1190 | DeviceType_T aDeviceType)
|
---|
1191 | {
|
---|
1192 | AssertReturn(aVirtualBox, E_INVALIDARG);
|
---|
1193 | AssertReturn(!aLocation.isEmpty(), E_INVALIDARG);
|
---|
1194 |
|
---|
1195 | HRESULT rc = S_OK;
|
---|
1196 |
|
---|
1197 | {
|
---|
1198 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1199 | AutoInitSpan autoInitSpan(this);
|
---|
1200 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1201 |
|
---|
1202 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1203 |
|
---|
1204 | /* there must be a storage unit */
|
---|
1205 | m->state = MediumState_Created;
|
---|
1206 |
|
---|
1207 | /* remember device type for correct unregistering later */
|
---|
1208 | m->devType = aDeviceType;
|
---|
1209 |
|
---|
1210 | /* cannot be a host drive */
|
---|
1211 | m->hostDrive = false;
|
---|
1212 |
|
---|
1213 | /* remember the open mode (defaults to ReadWrite) */
|
---|
1214 | m->hddOpenMode = enOpenMode;
|
---|
1215 |
|
---|
1216 | if (aDeviceType == DeviceType_DVD)
|
---|
1217 | m->type = MediumType_Readonly;
|
---|
1218 | else if (aDeviceType == DeviceType_Floppy)
|
---|
1219 | m->type = MediumType_Writethrough;
|
---|
1220 |
|
---|
1221 | rc = i_setLocation(aLocation);
|
---|
1222 | if (FAILED(rc)) return rc;
|
---|
1223 |
|
---|
1224 | /* get all the information about the medium from the storage unit */
|
---|
1225 | if (fForceNewUuid)
|
---|
1226 | unconst(m->uuidImage).create();
|
---|
1227 |
|
---|
1228 | m->state = MediumState_Inaccessible;
|
---|
1229 | m->strLastAccessError = tr("Accessibility check was not yet performed");
|
---|
1230 |
|
---|
1231 | /* Confirm a successful initialization before the call to i_queryInfo.
|
---|
1232 | * Otherwise we can end up with a AutoCaller deadlock because the
|
---|
1233 | * medium becomes visible but is not marked as initialized. Causes
|
---|
1234 | * locking trouble (e.g. trying to save media registries) which is
|
---|
1235 | * hard to solve. */
|
---|
1236 | autoInitSpan.setSucceeded();
|
---|
1237 | }
|
---|
1238 |
|
---|
1239 | /* we're normal code from now on, no longer init */
|
---|
1240 | AutoCaller autoCaller(this);
|
---|
1241 | if (FAILED(autoCaller.rc()))
|
---|
1242 | return autoCaller.rc();
|
---|
1243 |
|
---|
1244 | /* need to call i_queryInfo immediately to correctly place the medium in
|
---|
1245 | * the respective media tree and update other information such as uuid */
|
---|
1246 | rc = i_queryInfo(fForceNewUuid /* fSetImageId */, false /* fSetParentId */,
|
---|
1247 | autoCaller);
|
---|
1248 | if (SUCCEEDED(rc))
|
---|
1249 | {
|
---|
1250 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1251 |
|
---|
1252 | /* if the storage unit is not accessible, it's not acceptable for the
|
---|
1253 | * newly opened media so convert this into an error */
|
---|
1254 | if (m->state == MediumState_Inaccessible)
|
---|
1255 | {
|
---|
1256 | Assert(!m->strLastAccessError.isEmpty());
|
---|
1257 | rc = setError(E_FAIL, "%s", m->strLastAccessError.c_str());
|
---|
1258 | alock.release();
|
---|
1259 | autoCaller.release();
|
---|
1260 | uninit();
|
---|
1261 | }
|
---|
1262 | else
|
---|
1263 | {
|
---|
1264 | AssertStmt(!m->id.isZero(),
|
---|
1265 | alock.release(); autoCaller.release(); uninit(); return E_FAIL);
|
---|
1266 |
|
---|
1267 | /* storage format must be detected by Medium::i_queryInfo if the
|
---|
1268 | * medium is accessible */
|
---|
1269 | AssertStmt(!m->strFormat.isEmpty(),
|
---|
1270 | alock.release(); autoCaller.release(); uninit(); return E_FAIL);
|
---|
1271 | }
|
---|
1272 | }
|
---|
1273 | else
|
---|
1274 | {
|
---|
1275 | /* opening this image failed, mark the object as dead */
|
---|
1276 | autoCaller.release();
|
---|
1277 | uninit();
|
---|
1278 | }
|
---|
1279 |
|
---|
1280 | return rc;
|
---|
1281 | }
|
---|
1282 |
|
---|
1283 | /**
|
---|
1284 | * Initializes the medium object by loading its data from the given settings
|
---|
1285 | * node. The medium will always be opened read/write.
|
---|
1286 | *
|
---|
1287 | * In this case, since we're loading from a registry, uuidMachineRegistry is
|
---|
1288 | * always set: it's either the global registry UUID or a machine UUID when
|
---|
1289 | * loading from a per-machine registry.
|
---|
1290 | *
|
---|
1291 | * @param aParent Parent medium disk or NULL for a root (base) medium.
|
---|
1292 | * @param aDeviceType Device type of the medium.
|
---|
1293 | * @param uuidMachineRegistry The registry to which this medium should be
|
---|
1294 | * added (global registry UUID or machine UUID).
|
---|
1295 | * @param data Configuration settings.
|
---|
1296 | * @param strMachineFolder The machine folder with which to resolve relative paths;
|
---|
1297 | * if empty, then we use the VirtualBox home directory
|
---|
1298 | *
|
---|
1299 | * @note Locks the medium tree for writing.
|
---|
1300 | */
|
---|
1301 | HRESULT Medium::initOne(Medium *aParent,
|
---|
1302 | DeviceType_T aDeviceType,
|
---|
1303 | const Guid &uuidMachineRegistry,
|
---|
1304 | const settings::Medium &data,
|
---|
1305 | const Utf8Str &strMachineFolder)
|
---|
1306 | {
|
---|
1307 | HRESULT rc;
|
---|
1308 |
|
---|
1309 | if (uuidMachineRegistry.isValid() && !uuidMachineRegistry.isZero())
|
---|
1310 | m->llRegistryIDs.push_back(uuidMachineRegistry);
|
---|
1311 |
|
---|
1312 | /* register with VirtualBox/parent early, since uninit() will
|
---|
1313 | * unconditionally unregister on failure */
|
---|
1314 | if (aParent)
|
---|
1315 | {
|
---|
1316 | // differencing medium: add to parent
|
---|
1317 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1318 | // no need to check maximum depth as settings reading did it
|
---|
1319 | i_setParent(aParent);
|
---|
1320 | }
|
---|
1321 |
|
---|
1322 | /* see below why we don't call Medium::i_queryInfo (and therefore treat
|
---|
1323 | * the medium as inaccessible for now */
|
---|
1324 | m->state = MediumState_Inaccessible;
|
---|
1325 | m->strLastAccessError = tr("Accessibility check was not yet performed");
|
---|
1326 |
|
---|
1327 | /* required */
|
---|
1328 | unconst(m->id) = data.uuid;
|
---|
1329 |
|
---|
1330 | /* assume not a host drive */
|
---|
1331 | m->hostDrive = false;
|
---|
1332 |
|
---|
1333 | /* optional */
|
---|
1334 | m->strDescription = data.strDescription;
|
---|
1335 |
|
---|
1336 | /* required */
|
---|
1337 | if (aDeviceType == DeviceType_HardDisk)
|
---|
1338 | {
|
---|
1339 | AssertReturn(!data.strFormat.isEmpty(), E_FAIL);
|
---|
1340 | rc = i_setFormat(data.strFormat);
|
---|
1341 | if (FAILED(rc)) return rc;
|
---|
1342 | }
|
---|
1343 | else
|
---|
1344 | {
|
---|
1345 | /// @todo handle host drive settings here as well?
|
---|
1346 | if (!data.strFormat.isEmpty())
|
---|
1347 | rc = i_setFormat(data.strFormat);
|
---|
1348 | else
|
---|
1349 | rc = i_setFormat("RAW");
|
---|
1350 | if (FAILED(rc)) return rc;
|
---|
1351 | }
|
---|
1352 |
|
---|
1353 | /* optional, only for diffs, default is false; we can only auto-reset
|
---|
1354 | * diff media so they must have a parent */
|
---|
1355 | if (aParent != NULL)
|
---|
1356 | m->autoReset = data.fAutoReset;
|
---|
1357 | else
|
---|
1358 | m->autoReset = false;
|
---|
1359 |
|
---|
1360 | /* properties (after setting the format as it populates the map). Note that
|
---|
1361 | * if some properties are not supported but present in the settings file,
|
---|
1362 | * they will still be read and accessible (for possible backward
|
---|
1363 | * compatibility; we can also clean them up from the XML upon next
|
---|
1364 | * XML format version change if we wish) */
|
---|
1365 | for (settings::StringsMap::const_iterator it = data.properties.begin();
|
---|
1366 | it != data.properties.end();
|
---|
1367 | ++it)
|
---|
1368 | {
|
---|
1369 | const Utf8Str &name = it->first;
|
---|
1370 | const Utf8Str &value = it->second;
|
---|
1371 | m->mapProperties[name] = value;
|
---|
1372 | }
|
---|
1373 |
|
---|
1374 | /* try to decrypt an optional iSCSI initiator secret */
|
---|
1375 | settings::StringsMap::const_iterator itCph = data.properties.find("InitiatorSecretEncrypted");
|
---|
1376 | if ( itCph != data.properties.end()
|
---|
1377 | && !itCph->second.isEmpty())
|
---|
1378 | {
|
---|
1379 | Utf8Str strPlaintext;
|
---|
1380 | int vrc = m->pVirtualBox->i_decryptSetting(&strPlaintext, itCph->second);
|
---|
1381 | if (RT_SUCCESS(vrc))
|
---|
1382 | m->mapProperties["InitiatorSecret"] = strPlaintext;
|
---|
1383 | }
|
---|
1384 |
|
---|
1385 | Utf8Str strFull;
|
---|
1386 | if (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
1387 | {
|
---|
1388 | // compose full path of the medium, if it's not fully qualified...
|
---|
1389 | // slightly convoluted logic here. If the caller has given us a
|
---|
1390 | // machine folder, then a relative path will be relative to that:
|
---|
1391 | if ( !strMachineFolder.isEmpty()
|
---|
1392 | && !RTPathStartsWithRoot(data.strLocation.c_str())
|
---|
1393 | )
|
---|
1394 | {
|
---|
1395 | strFull = strMachineFolder;
|
---|
1396 | strFull += RTPATH_SLASH;
|
---|
1397 | strFull += data.strLocation;
|
---|
1398 | }
|
---|
1399 | else
|
---|
1400 | {
|
---|
1401 | // Otherwise use the old VirtualBox "make absolute path" logic:
|
---|
1402 | rc = m->pVirtualBox->i_calculateFullPath(data.strLocation, strFull);
|
---|
1403 | if (FAILED(rc)) return rc;
|
---|
1404 | }
|
---|
1405 | }
|
---|
1406 | else
|
---|
1407 | strFull = data.strLocation;
|
---|
1408 |
|
---|
1409 | rc = i_setLocation(strFull);
|
---|
1410 | if (FAILED(rc)) return rc;
|
---|
1411 |
|
---|
1412 | if (aDeviceType == DeviceType_HardDisk)
|
---|
1413 | {
|
---|
1414 | /* type is only for base hard disks */
|
---|
1415 | if (m->pParent.isNull())
|
---|
1416 | m->type = data.hdType;
|
---|
1417 | }
|
---|
1418 | else if (aDeviceType == DeviceType_DVD)
|
---|
1419 | m->type = MediumType_Readonly;
|
---|
1420 | else
|
---|
1421 | m->type = MediumType_Writethrough;
|
---|
1422 |
|
---|
1423 | /* remember device type for correct unregistering later */
|
---|
1424 | m->devType = aDeviceType;
|
---|
1425 |
|
---|
1426 | LogFlowThisFunc(("m->strLocationFull='%s', m->strFormat=%s, m->id={%RTuuid}\n",
|
---|
1427 | m->strLocationFull.c_str(), m->strFormat.c_str(), m->id.raw()));
|
---|
1428 |
|
---|
1429 | return S_OK;
|
---|
1430 | }
|
---|
1431 |
|
---|
1432 | /**
|
---|
1433 | * Initializes the medium object and its children by loading its data from the
|
---|
1434 | * given settings node. The medium will always be opened read/write.
|
---|
1435 | *
|
---|
1436 | * In this case, since we're loading from a registry, uuidMachineRegistry is
|
---|
1437 | * always set: it's either the global registry UUID or a machine UUID when
|
---|
1438 | * loading from a per-machine registry.
|
---|
1439 | *
|
---|
1440 | * @param aVirtualBox VirtualBox object.
|
---|
1441 | * @param aParent Parent medium disk or NULL for a root (base) medium.
|
---|
1442 | * @param aDeviceType Device type of the medium.
|
---|
1443 | * @param uuidMachineRegistry The registry to which this medium should be added (global registry UUID or machine UUID).
|
---|
1444 | * @param data Configuration settings.
|
---|
1445 | * @param strMachineFolder The machine folder with which to resolve relative paths; if empty, then we use the VirtualBox home directory
|
---|
1446 | *
|
---|
1447 | * @note Locks the medium tree for writing.
|
---|
1448 | */
|
---|
1449 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1450 | Medium *aParent,
|
---|
1451 | DeviceType_T aDeviceType,
|
---|
1452 | const Guid &uuidMachineRegistry,
|
---|
1453 | const settings::Medium &data,
|
---|
1454 | const Utf8Str &strMachineFolder,
|
---|
1455 | AutoWriteLock &mediaTreeLock)
|
---|
1456 | {
|
---|
1457 | using namespace settings;
|
---|
1458 |
|
---|
1459 | Assert(aVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
1460 | AssertReturn(aVirtualBox, E_INVALIDARG);
|
---|
1461 |
|
---|
1462 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1463 | AutoInitSpan autoInitSpan(this);
|
---|
1464 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1465 |
|
---|
1466 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1467 |
|
---|
1468 | // Do not inline this method call, as the purpose of having this separate
|
---|
1469 | // is to save on stack size. Less local variables are the key for reaching
|
---|
1470 | // deep recursion levels with small stack (XPCOM/g++ without optimization).
|
---|
1471 | HRESULT rc = initOne(aParent, aDeviceType, uuidMachineRegistry, data, strMachineFolder);
|
---|
1472 |
|
---|
1473 |
|
---|
1474 | /* Don't call Medium::i_queryInfo for registered media to prevent the calling
|
---|
1475 | * thread (i.e. the VirtualBox server startup thread) from an unexpected
|
---|
1476 | * freeze but mark it as initially inaccessible instead. The vital UUID,
|
---|
1477 | * location and format properties are read from the registry file above; to
|
---|
1478 | * get the actual state and the rest of the data, the user will have to call
|
---|
1479 | * COMGETTER(State). */
|
---|
1480 |
|
---|
1481 | /* load all children */
|
---|
1482 | for (settings::MediaList::const_iterator it = data.llChildren.begin();
|
---|
1483 | it != data.llChildren.end();
|
---|
1484 | ++it)
|
---|
1485 | {
|
---|
1486 | const settings::Medium &med = *it;
|
---|
1487 |
|
---|
1488 | ComObjPtr<Medium> pMedium;
|
---|
1489 | pMedium.createObject();
|
---|
1490 | rc = pMedium->init(aVirtualBox,
|
---|
1491 | this, // parent
|
---|
1492 | aDeviceType,
|
---|
1493 | uuidMachineRegistry,
|
---|
1494 | med, // child data
|
---|
1495 | strMachineFolder,
|
---|
1496 | mediaTreeLock);
|
---|
1497 | if (FAILED(rc)) break;
|
---|
1498 |
|
---|
1499 | rc = m->pVirtualBox->i_registerMedium(pMedium, &pMedium, mediaTreeLock);
|
---|
1500 | if (FAILED(rc)) break;
|
---|
1501 | }
|
---|
1502 |
|
---|
1503 | /* Confirm a successful initialization when it's the case */
|
---|
1504 | if (SUCCEEDED(rc))
|
---|
1505 | autoInitSpan.setSucceeded();
|
---|
1506 |
|
---|
1507 | return rc;
|
---|
1508 | }
|
---|
1509 |
|
---|
1510 | /**
|
---|
1511 | * Initializes the medium object by providing the host drive information.
|
---|
1512 | * Not used for anything but the host floppy/host DVD case.
|
---|
1513 | *
|
---|
1514 | * There is no registry for this case.
|
---|
1515 | *
|
---|
1516 | * @param aVirtualBox VirtualBox object.
|
---|
1517 | * @param aDeviceType Device type of the medium.
|
---|
1518 | * @param aLocation Location of the host drive.
|
---|
1519 | * @param aDescription Comment for this host drive.
|
---|
1520 | *
|
---|
1521 | * @note Locks VirtualBox lock for writing.
|
---|
1522 | */
|
---|
1523 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1524 | DeviceType_T aDeviceType,
|
---|
1525 | const Utf8Str &aLocation,
|
---|
1526 | const Utf8Str &aDescription /* = Utf8Str::Empty */)
|
---|
1527 | {
|
---|
1528 | ComAssertRet(aDeviceType == DeviceType_DVD || aDeviceType == DeviceType_Floppy, E_INVALIDARG);
|
---|
1529 | ComAssertRet(!aLocation.isEmpty(), E_INVALIDARG);
|
---|
1530 |
|
---|
1531 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1532 | AutoInitSpan autoInitSpan(this);
|
---|
1533 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1534 |
|
---|
1535 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1536 |
|
---|
1537 | // We do not store host drives in VirtualBox.xml or anywhere else, so if we want
|
---|
1538 | // host drives to be identifiable by UUID and not give the drive a different UUID
|
---|
1539 | // every time VirtualBox starts, we need to fake a reproducible UUID here:
|
---|
1540 | RTUUID uuid;
|
---|
1541 | RTUuidClear(&uuid);
|
---|
1542 | if (aDeviceType == DeviceType_DVD)
|
---|
1543 | memcpy(&uuid.au8[0], "DVD", 3);
|
---|
1544 | else
|
---|
1545 | memcpy(&uuid.au8[0], "FD", 2);
|
---|
1546 | /* use device name, adjusted to the end of uuid, shortened if necessary */
|
---|
1547 | size_t lenLocation = aLocation.length();
|
---|
1548 | if (lenLocation > 12)
|
---|
1549 | memcpy(&uuid.au8[4], aLocation.c_str() + (lenLocation - 12), 12);
|
---|
1550 | else
|
---|
1551 | memcpy(&uuid.au8[4 + 12 - lenLocation], aLocation.c_str(), lenLocation);
|
---|
1552 | unconst(m->id) = uuid;
|
---|
1553 |
|
---|
1554 | if (aDeviceType == DeviceType_DVD)
|
---|
1555 | m->type = MediumType_Readonly;
|
---|
1556 | else
|
---|
1557 | m->type = MediumType_Writethrough;
|
---|
1558 | m->devType = aDeviceType;
|
---|
1559 | m->state = MediumState_Created;
|
---|
1560 | m->hostDrive = true;
|
---|
1561 | HRESULT rc = i_setFormat("RAW");
|
---|
1562 | if (FAILED(rc)) return rc;
|
---|
1563 | rc = i_setLocation(aLocation);
|
---|
1564 | if (FAILED(rc)) return rc;
|
---|
1565 | m->strDescription = aDescription;
|
---|
1566 |
|
---|
1567 | autoInitSpan.setSucceeded();
|
---|
1568 | return S_OK;
|
---|
1569 | }
|
---|
1570 |
|
---|
1571 | /**
|
---|
1572 | * Uninitializes the instance.
|
---|
1573 | *
|
---|
1574 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
1575 | *
|
---|
1576 | * @note All children of this medium get uninitialized by calling their
|
---|
1577 | * uninit() methods.
|
---|
1578 | */
|
---|
1579 | void Medium::uninit()
|
---|
1580 | {
|
---|
1581 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1582 | * the pVirtualBox reference, and in this case we don't need to continue.
|
---|
1583 | * Normally this would be handled through the AutoUninitSpan magic, however
|
---|
1584 | * this cannot be done at this point as the media tree must be locked
|
---|
1585 | * before reaching the AutoUninitSpan, otherwise deadlocks can happen.
|
---|
1586 | *
|
---|
1587 | * NOTE: The tree lock is higher priority than the medium caller and medium
|
---|
1588 | * object locks, i.e. the medium caller may have to be released and be
|
---|
1589 | * re-acquired in the right place later. See Medium::getParent() for sample
|
---|
1590 | * code how to do this safely. */
|
---|
1591 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
1592 | if (!pVirtualBox)
|
---|
1593 | return;
|
---|
1594 |
|
---|
1595 | /* Caller must not hold the object or media tree lock over uninit(). */
|
---|
1596 | Assert(!isWriteLockOnCurrentThread());
|
---|
1597 | Assert(!pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
1598 |
|
---|
1599 | AutoWriteLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1600 |
|
---|
1601 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
1602 | AutoUninitSpan autoUninitSpan(this);
|
---|
1603 | if (autoUninitSpan.uninitDone())
|
---|
1604 | return;
|
---|
1605 |
|
---|
1606 | if (!m->formatObj.isNull())
|
---|
1607 | m->formatObj.setNull();
|
---|
1608 |
|
---|
1609 | if (m->state == MediumState_Deleting)
|
---|
1610 | {
|
---|
1611 | /* This medium has been already deleted (directly or as part of a
|
---|
1612 | * merge). Reparenting has already been done. */
|
---|
1613 | Assert(m->pParent.isNull());
|
---|
1614 | }
|
---|
1615 | else
|
---|
1616 | {
|
---|
1617 | MediaList llChildren(m->llChildren);
|
---|
1618 | m->llChildren.clear();
|
---|
1619 | autoUninitSpan.setSucceeded();
|
---|
1620 |
|
---|
1621 | while (!llChildren.empty())
|
---|
1622 | {
|
---|
1623 | ComObjPtr<Medium> pChild = llChildren.front();
|
---|
1624 | llChildren.pop_front();
|
---|
1625 | pChild->m->pParent.setNull();
|
---|
1626 | treeLock.release();
|
---|
1627 | pChild->uninit();
|
---|
1628 | treeLock.acquire();
|
---|
1629 | }
|
---|
1630 |
|
---|
1631 | if (m->pParent)
|
---|
1632 | {
|
---|
1633 | // this is a differencing disk: then remove it from the parent's children list
|
---|
1634 | i_deparent();
|
---|
1635 | }
|
---|
1636 | }
|
---|
1637 |
|
---|
1638 | unconst(m->pVirtualBox) = NULL;
|
---|
1639 | }
|
---|
1640 |
|
---|
1641 | /**
|
---|
1642 | * Internal helper that removes "this" from the list of children of its
|
---|
1643 | * parent. Used in uninit() and other places when reparenting is necessary.
|
---|
1644 | *
|
---|
1645 | * The caller must hold the medium tree lock!
|
---|
1646 | */
|
---|
1647 | void Medium::i_deparent()
|
---|
1648 | {
|
---|
1649 | MediaList &llParent = m->pParent->m->llChildren;
|
---|
1650 | for (MediaList::iterator it = llParent.begin();
|
---|
1651 | it != llParent.end();
|
---|
1652 | ++it)
|
---|
1653 | {
|
---|
1654 | Medium *pParentsChild = *it;
|
---|
1655 | if (this == pParentsChild)
|
---|
1656 | {
|
---|
1657 | llParent.erase(it);
|
---|
1658 | break;
|
---|
1659 | }
|
---|
1660 | }
|
---|
1661 | m->pParent.setNull();
|
---|
1662 | }
|
---|
1663 |
|
---|
1664 | /**
|
---|
1665 | * Internal helper that removes "this" from the list of children of its
|
---|
1666 | * parent. Used in uninit() and other places when reparenting is necessary.
|
---|
1667 | *
|
---|
1668 | * The caller must hold the medium tree lock!
|
---|
1669 | */
|
---|
1670 | void Medium::i_setParent(const ComObjPtr<Medium> &pParent)
|
---|
1671 | {
|
---|
1672 | m->pParent = pParent;
|
---|
1673 | if (pParent)
|
---|
1674 | pParent->m->llChildren.push_back(this);
|
---|
1675 | }
|
---|
1676 |
|
---|
1677 |
|
---|
1678 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1679 | //
|
---|
1680 | // IMedium public methods
|
---|
1681 | //
|
---|
1682 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1683 |
|
---|
1684 | HRESULT Medium::getId(com::Guid &aId)
|
---|
1685 | {
|
---|
1686 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1687 |
|
---|
1688 | aId = m->id;
|
---|
1689 |
|
---|
1690 | return S_OK;
|
---|
1691 | }
|
---|
1692 |
|
---|
1693 | HRESULT Medium::getDescription(com::Utf8Str &aDescription)
|
---|
1694 | {
|
---|
1695 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1696 |
|
---|
1697 | aDescription = m->strDescription;
|
---|
1698 |
|
---|
1699 | return S_OK;
|
---|
1700 | }
|
---|
1701 |
|
---|
1702 | HRESULT Medium::setDescription(const com::Utf8Str &aDescription)
|
---|
1703 | {
|
---|
1704 | /// @todo update m->strDescription and save the global registry (and local
|
---|
1705 | /// registries of portable VMs referring to this medium), this will also
|
---|
1706 | /// require to add the mRegistered flag to data
|
---|
1707 |
|
---|
1708 | HRESULT rc = S_OK;
|
---|
1709 |
|
---|
1710 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
1711 |
|
---|
1712 | try
|
---|
1713 | {
|
---|
1714 | // locking: we need the tree lock first because we access parent pointers
|
---|
1715 | // and we need to write-lock the media involved
|
---|
1716 | uint32_t cHandles = 2;
|
---|
1717 | LockHandle* pHandles[2] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
1718 | this->lockHandle() };
|
---|
1719 |
|
---|
1720 | AutoWriteLock alock(cHandles,
|
---|
1721 | pHandles
|
---|
1722 | COMMA_LOCKVAL_SRC_POS);
|
---|
1723 |
|
---|
1724 | /* Build the lock list. */
|
---|
1725 | alock.release();
|
---|
1726 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
1727 | this /* pToLockWrite */,
|
---|
1728 | true /* fMediumLockWriteAll */,
|
---|
1729 | NULL,
|
---|
1730 | *pMediumLockList);
|
---|
1731 | alock.acquire();
|
---|
1732 |
|
---|
1733 | if (FAILED(rc))
|
---|
1734 | {
|
---|
1735 | throw setError(rc,
|
---|
1736 | tr("Failed to create medium lock list for '%s'"),
|
---|
1737 | i_getLocationFull().c_str());
|
---|
1738 | }
|
---|
1739 |
|
---|
1740 | alock.release();
|
---|
1741 | rc = pMediumLockList->Lock();
|
---|
1742 | alock.acquire();
|
---|
1743 |
|
---|
1744 | if (FAILED(rc))
|
---|
1745 | {
|
---|
1746 | throw setError(rc,
|
---|
1747 | tr("Failed to lock media '%s'"),
|
---|
1748 | i_getLocationFull().c_str());
|
---|
1749 | }
|
---|
1750 |
|
---|
1751 | /* Set a new description */
|
---|
1752 | if (SUCCEEDED(rc))
|
---|
1753 | {
|
---|
1754 | m->strDescription = aDescription;
|
---|
1755 | }
|
---|
1756 |
|
---|
1757 | // save the settings
|
---|
1758 | i_markRegistriesModified();
|
---|
1759 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
1760 | }
|
---|
1761 | catch (HRESULT aRC) { rc = aRC; }
|
---|
1762 |
|
---|
1763 | delete pMediumLockList;
|
---|
1764 |
|
---|
1765 | return rc;
|
---|
1766 | }
|
---|
1767 |
|
---|
1768 | HRESULT Medium::getState(MediumState_T *aState)
|
---|
1769 | {
|
---|
1770 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1771 | *aState = m->state;
|
---|
1772 |
|
---|
1773 | return S_OK;
|
---|
1774 | }
|
---|
1775 |
|
---|
1776 | HRESULT Medium::getVariant(std::vector<MediumVariant_T> &aVariant)
|
---|
1777 | {
|
---|
1778 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1779 |
|
---|
1780 | const size_t cBits = sizeof(MediumVariant_T) * 8;
|
---|
1781 | aVariant.resize(cBits);
|
---|
1782 | for (size_t i = 0; i < cBits; ++i)
|
---|
1783 | aVariant[i] = (MediumVariant_T)(m->variant & RT_BIT(i));
|
---|
1784 |
|
---|
1785 | return S_OK;
|
---|
1786 | }
|
---|
1787 |
|
---|
1788 | HRESULT Medium::getLocation(com::Utf8Str &aLocation)
|
---|
1789 | {
|
---|
1790 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1791 |
|
---|
1792 | aLocation = m->strLocationFull;
|
---|
1793 |
|
---|
1794 | return S_OK;
|
---|
1795 | }
|
---|
1796 |
|
---|
1797 | HRESULT Medium::getName(com::Utf8Str &aName)
|
---|
1798 | {
|
---|
1799 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1800 |
|
---|
1801 | aName = i_getName();
|
---|
1802 |
|
---|
1803 | return S_OK;
|
---|
1804 | }
|
---|
1805 |
|
---|
1806 | HRESULT Medium::getDeviceType(DeviceType_T *aDeviceType)
|
---|
1807 | {
|
---|
1808 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1809 |
|
---|
1810 | *aDeviceType = m->devType;
|
---|
1811 |
|
---|
1812 | return S_OK;
|
---|
1813 | }
|
---|
1814 |
|
---|
1815 | HRESULT Medium::getHostDrive(BOOL *aHostDrive)
|
---|
1816 | {
|
---|
1817 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1818 |
|
---|
1819 | *aHostDrive = m->hostDrive;
|
---|
1820 |
|
---|
1821 | return S_OK;
|
---|
1822 | }
|
---|
1823 |
|
---|
1824 | HRESULT Medium::getSize(LONG64 *aSize)
|
---|
1825 | {
|
---|
1826 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1827 |
|
---|
1828 | *aSize = m->size;
|
---|
1829 |
|
---|
1830 | return S_OK;
|
---|
1831 | }
|
---|
1832 |
|
---|
1833 | HRESULT Medium::getFormat(com::Utf8Str &aFormat)
|
---|
1834 | {
|
---|
1835 | /* no need to lock, m->strFormat is const */
|
---|
1836 |
|
---|
1837 | aFormat = m->strFormat;
|
---|
1838 | return S_OK;
|
---|
1839 | }
|
---|
1840 |
|
---|
1841 | HRESULT Medium::getMediumFormat(ComPtr<IMediumFormat> &aMediumFormat)
|
---|
1842 | {
|
---|
1843 | /* no need to lock, m->formatObj is const */
|
---|
1844 | m->formatObj.queryInterfaceTo(aMediumFormat.asOutParam());
|
---|
1845 |
|
---|
1846 | return S_OK;
|
---|
1847 | }
|
---|
1848 |
|
---|
1849 | HRESULT Medium::getType(AutoCaller &autoCaller, MediumType_T *aType)
|
---|
1850 | {
|
---|
1851 | NOREF(autoCaller);
|
---|
1852 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1853 |
|
---|
1854 | *aType = m->type;
|
---|
1855 |
|
---|
1856 | return S_OK;
|
---|
1857 | }
|
---|
1858 |
|
---|
1859 | HRESULT Medium::setType(AutoCaller &autoCaller, MediumType_T aType)
|
---|
1860 | {
|
---|
1861 | autoCaller.release();
|
---|
1862 |
|
---|
1863 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1864 | * the pVirtualBox reference, see #uninit(). */
|
---|
1865 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
1866 |
|
---|
1867 | // we access m->pParent
|
---|
1868 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
1869 |
|
---|
1870 | autoCaller.add();
|
---|
1871 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1872 |
|
---|
1873 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1874 |
|
---|
1875 | switch (m->state)
|
---|
1876 | {
|
---|
1877 | case MediumState_Created:
|
---|
1878 | case MediumState_Inaccessible:
|
---|
1879 | break;
|
---|
1880 | default:
|
---|
1881 | return i_setStateError();
|
---|
1882 | }
|
---|
1883 |
|
---|
1884 | if (m->type == aType)
|
---|
1885 | {
|
---|
1886 | /* Nothing to do */
|
---|
1887 | return S_OK;
|
---|
1888 | }
|
---|
1889 |
|
---|
1890 | DeviceType_T devType = i_getDeviceType();
|
---|
1891 | // DVD media can only be readonly.
|
---|
1892 | if (devType == DeviceType_DVD && aType != MediumType_Readonly)
|
---|
1893 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1894 | tr("Cannot change the type of DVD medium '%s'"),
|
---|
1895 | m->strLocationFull.c_str());
|
---|
1896 | // Floppy media can only be writethrough or readonly.
|
---|
1897 | if ( devType == DeviceType_Floppy
|
---|
1898 | && aType != MediumType_Writethrough
|
---|
1899 | && aType != MediumType_Readonly)
|
---|
1900 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1901 | tr("Cannot change the type of floppy medium '%s'"),
|
---|
1902 | m->strLocationFull.c_str());
|
---|
1903 |
|
---|
1904 | /* cannot change the type of a differencing medium */
|
---|
1905 | if (m->pParent)
|
---|
1906 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1907 | tr("Cannot change the type of medium '%s' because it is a differencing medium"),
|
---|
1908 | m->strLocationFull.c_str());
|
---|
1909 |
|
---|
1910 | /* Cannot change the type of a medium being in use by more than one VM.
|
---|
1911 | * If the change is to Immutable or MultiAttach then it must not be
|
---|
1912 | * directly attached to any VM, otherwise the assumptions about indirect
|
---|
1913 | * attachment elsewhere are violated and the VM becomes inaccessible.
|
---|
1914 | * Attaching an immutable medium triggers the diff creation, and this is
|
---|
1915 | * vital for the correct operation. */
|
---|
1916 | if ( m->backRefs.size() > 1
|
---|
1917 | || ( ( aType == MediumType_Immutable
|
---|
1918 | || aType == MediumType_MultiAttach)
|
---|
1919 | && m->backRefs.size() > 0))
|
---|
1920 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1921 | tr("Cannot change the type of medium '%s' because it is attached to %d virtual machines"),
|
---|
1922 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
1923 |
|
---|
1924 | switch (aType)
|
---|
1925 | {
|
---|
1926 | case MediumType_Normal:
|
---|
1927 | case MediumType_Immutable:
|
---|
1928 | case MediumType_MultiAttach:
|
---|
1929 | {
|
---|
1930 | /* normal can be easily converted to immutable and vice versa even
|
---|
1931 | * if they have children as long as they are not attached to any
|
---|
1932 | * machine themselves */
|
---|
1933 | break;
|
---|
1934 | }
|
---|
1935 | case MediumType_Writethrough:
|
---|
1936 | case MediumType_Shareable:
|
---|
1937 | case MediumType_Readonly:
|
---|
1938 | {
|
---|
1939 | /* cannot change to writethrough, shareable or readonly
|
---|
1940 | * if there are children */
|
---|
1941 | if (i_getChildren().size() != 0)
|
---|
1942 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
1943 | tr("Cannot change type for medium '%s' since it has %d child media"),
|
---|
1944 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
1945 | if (aType == MediumType_Shareable)
|
---|
1946 | {
|
---|
1947 | MediumVariant_T variant = i_getVariant();
|
---|
1948 | if (!(variant & MediumVariant_Fixed))
|
---|
1949 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1950 | tr("Cannot change type for medium '%s' to 'Shareable' since it is a dynamic medium storage unit"),
|
---|
1951 | m->strLocationFull.c_str());
|
---|
1952 | }
|
---|
1953 | else if (aType == MediumType_Readonly && devType == DeviceType_HardDisk)
|
---|
1954 | {
|
---|
1955 | // Readonly hard disks are not allowed, this medium type is reserved for
|
---|
1956 | // DVDs and floppy images at the moment. Later we might allow readonly hard
|
---|
1957 | // disks, but that's extremely unusual and many guest OSes will have trouble.
|
---|
1958 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1959 | tr("Cannot change type for medium '%s' to 'Readonly' since it is a hard disk"),
|
---|
1960 | m->strLocationFull.c_str());
|
---|
1961 | }
|
---|
1962 | break;
|
---|
1963 | }
|
---|
1964 | default:
|
---|
1965 | AssertFailedReturn(E_FAIL);
|
---|
1966 | }
|
---|
1967 |
|
---|
1968 | if (aType == MediumType_MultiAttach)
|
---|
1969 | {
|
---|
1970 | // This type is new with VirtualBox 4.0 and therefore requires settings
|
---|
1971 | // version 1.11 in the settings backend. Unfortunately it is not enough to do
|
---|
1972 | // the usual routine in MachineConfigFile::bumpSettingsVersionIfNeeded() for
|
---|
1973 | // two reasons: The medium type is a property of the media registry tree, which
|
---|
1974 | // can reside in the global config file (for pre-4.0 media); we would therefore
|
---|
1975 | // possibly need to bump the global config version. We don't want to do that though
|
---|
1976 | // because that might make downgrading to pre-4.0 impossible.
|
---|
1977 | // As a result, we can only use these two new types if the medium is NOT in the
|
---|
1978 | // global registry:
|
---|
1979 | const Guid &uuidGlobalRegistry = m->pVirtualBox->i_getGlobalRegistryId();
|
---|
1980 | if (i_isInRegistry(uuidGlobalRegistry))
|
---|
1981 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1982 | tr("Cannot change type for medium '%s': the media type 'MultiAttach' can only be used "
|
---|
1983 | "on media registered with a machine that was created with VirtualBox 4.0 or later"),
|
---|
1984 | m->strLocationFull.c_str());
|
---|
1985 | }
|
---|
1986 |
|
---|
1987 | m->type = aType;
|
---|
1988 |
|
---|
1989 | // save the settings
|
---|
1990 | mlock.release();
|
---|
1991 | treeLock.release();
|
---|
1992 | i_markRegistriesModified();
|
---|
1993 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
1994 |
|
---|
1995 | return S_OK;
|
---|
1996 | }
|
---|
1997 |
|
---|
1998 | HRESULT Medium::getAllowedTypes(std::vector<MediumType_T> &aAllowedTypes)
|
---|
1999 | {
|
---|
2000 | NOREF(aAllowedTypes);
|
---|
2001 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2002 |
|
---|
2003 | ReturnComNotImplemented();
|
---|
2004 | }
|
---|
2005 |
|
---|
2006 | HRESULT Medium::getParent(AutoCaller &autoCaller, ComPtr<IMedium> &aParent)
|
---|
2007 | {
|
---|
2008 | autoCaller.release();
|
---|
2009 |
|
---|
2010 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
2011 | * the pVirtualBox reference, see #uninit(). */
|
---|
2012 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
2013 |
|
---|
2014 | /* we access m->pParent */
|
---|
2015 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
2016 |
|
---|
2017 | autoCaller.add();
|
---|
2018 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2019 |
|
---|
2020 | m->pParent.queryInterfaceTo(aParent.asOutParam());
|
---|
2021 |
|
---|
2022 | return S_OK;
|
---|
2023 | }
|
---|
2024 |
|
---|
2025 | HRESULT Medium::getChildren(AutoCaller &autoCaller, std::vector<ComPtr<IMedium> > &aChildren)
|
---|
2026 | {
|
---|
2027 | autoCaller.release();
|
---|
2028 |
|
---|
2029 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
2030 | * the pVirtualBox reference, see #uninit(). */
|
---|
2031 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
2032 |
|
---|
2033 | /* we access children */
|
---|
2034 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
2035 |
|
---|
2036 | autoCaller.add();
|
---|
2037 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2038 |
|
---|
2039 | MediaList children(this->i_getChildren());
|
---|
2040 | aChildren.resize(children.size());
|
---|
2041 | size_t i = 0;
|
---|
2042 | for (MediaList::const_iterator it = children.begin(); it != children.end(); ++it, ++i)
|
---|
2043 | (*it).queryInterfaceTo(aChildren[i].asOutParam());
|
---|
2044 | return S_OK;
|
---|
2045 | }
|
---|
2046 |
|
---|
2047 | HRESULT Medium::getBase(AutoCaller &autoCaller, ComPtr<IMedium> &aBase)
|
---|
2048 | {
|
---|
2049 | autoCaller.release();
|
---|
2050 |
|
---|
2051 | /* i_getBase() will do callers/locking */
|
---|
2052 | i_getBase().queryInterfaceTo(aBase.asOutParam());
|
---|
2053 |
|
---|
2054 | return S_OK;
|
---|
2055 | }
|
---|
2056 |
|
---|
2057 | HRESULT Medium::getReadOnly(AutoCaller &autoCaller, BOOL *aReadOnly)
|
---|
2058 | {
|
---|
2059 | autoCaller.release();
|
---|
2060 |
|
---|
2061 | /* isReadOnly() will do locking */
|
---|
2062 | *aReadOnly = i_isReadOnly();
|
---|
2063 |
|
---|
2064 | return S_OK;
|
---|
2065 | }
|
---|
2066 |
|
---|
2067 | HRESULT Medium::getLogicalSize(LONG64 *aLogicalSize)
|
---|
2068 | {
|
---|
2069 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2070 |
|
---|
2071 | *aLogicalSize = m->logicalSize;
|
---|
2072 |
|
---|
2073 | return S_OK;
|
---|
2074 | }
|
---|
2075 |
|
---|
2076 | HRESULT Medium::getAutoReset(BOOL *aAutoReset)
|
---|
2077 | {
|
---|
2078 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2079 |
|
---|
2080 | if (m->pParent.isNull())
|
---|
2081 | *aAutoReset = FALSE;
|
---|
2082 | else
|
---|
2083 | *aAutoReset = m->autoReset;
|
---|
2084 |
|
---|
2085 | return S_OK;
|
---|
2086 | }
|
---|
2087 |
|
---|
2088 | HRESULT Medium::setAutoReset(BOOL aAutoReset)
|
---|
2089 | {
|
---|
2090 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2091 |
|
---|
2092 | if (m->pParent.isNull())
|
---|
2093 | return setError(VBOX_E_NOT_SUPPORTED,
|
---|
2094 | tr("Medium '%s' is not differencing"),
|
---|
2095 | m->strLocationFull.c_str());
|
---|
2096 |
|
---|
2097 | if (m->autoReset != !!aAutoReset)
|
---|
2098 | {
|
---|
2099 | m->autoReset = !!aAutoReset;
|
---|
2100 |
|
---|
2101 | // save the settings
|
---|
2102 | mlock.release();
|
---|
2103 | i_markRegistriesModified();
|
---|
2104 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2105 | }
|
---|
2106 |
|
---|
2107 | return S_OK;
|
---|
2108 | }
|
---|
2109 |
|
---|
2110 | HRESULT Medium::getLastAccessError(com::Utf8Str &aLastAccessError)
|
---|
2111 | {
|
---|
2112 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2113 |
|
---|
2114 | aLastAccessError = m->strLastAccessError;
|
---|
2115 |
|
---|
2116 | return S_OK;
|
---|
2117 | }
|
---|
2118 |
|
---|
2119 | HRESULT Medium::getMachineIds(std::vector<com::Guid> &aMachineIds)
|
---|
2120 | {
|
---|
2121 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2122 |
|
---|
2123 | if (m->backRefs.size() != 0)
|
---|
2124 | {
|
---|
2125 | BackRefList brlist(m->backRefs);
|
---|
2126 | aMachineIds.resize(brlist.size());
|
---|
2127 | size_t i = 0;
|
---|
2128 | for (BackRefList::const_iterator it = brlist.begin(); it != brlist.end(); ++it, ++i)
|
---|
2129 | aMachineIds[i] = it->machineId;
|
---|
2130 | }
|
---|
2131 |
|
---|
2132 | return S_OK;
|
---|
2133 | }
|
---|
2134 |
|
---|
2135 | HRESULT Medium::setIds(AutoCaller &autoCaller,
|
---|
2136 | BOOL aSetImageId,
|
---|
2137 | const com::Guid &aImageId,
|
---|
2138 | BOOL aSetParentId,
|
---|
2139 | const com::Guid &aParentId)
|
---|
2140 | {
|
---|
2141 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2142 |
|
---|
2143 | switch (m->state)
|
---|
2144 | {
|
---|
2145 | case MediumState_Created:
|
---|
2146 | break;
|
---|
2147 | default:
|
---|
2148 | return i_setStateError();
|
---|
2149 | }
|
---|
2150 |
|
---|
2151 | Guid imageId, parentId;
|
---|
2152 | if (aSetImageId)
|
---|
2153 | {
|
---|
2154 | if (aImageId.isZero())
|
---|
2155 | imageId.create();
|
---|
2156 | else
|
---|
2157 | {
|
---|
2158 | imageId = aImageId;
|
---|
2159 | if (!imageId.isValid())
|
---|
2160 | return setError(E_INVALIDARG, tr("Argument %s is invalid"), "aImageId");
|
---|
2161 | }
|
---|
2162 | }
|
---|
2163 | if (aSetParentId)
|
---|
2164 | {
|
---|
2165 | if (aParentId.isZero())
|
---|
2166 | parentId.create();
|
---|
2167 | else
|
---|
2168 | parentId = aParentId;
|
---|
2169 | }
|
---|
2170 |
|
---|
2171 | unconst(m->uuidImage) = imageId;
|
---|
2172 | unconst(m->uuidParentImage) = parentId;
|
---|
2173 |
|
---|
2174 | // must not hold any locks before calling Medium::i_queryInfo
|
---|
2175 | alock.release();
|
---|
2176 |
|
---|
2177 | HRESULT rc = i_queryInfo(!!aSetImageId /* fSetImageId */,
|
---|
2178 | !!aSetParentId /* fSetParentId */,
|
---|
2179 | autoCaller);
|
---|
2180 |
|
---|
2181 | return rc;
|
---|
2182 | }
|
---|
2183 |
|
---|
2184 | HRESULT Medium::refreshState(AutoCaller &autoCaller, MediumState_T *aState)
|
---|
2185 | {
|
---|
2186 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2187 |
|
---|
2188 | HRESULT rc = S_OK;
|
---|
2189 |
|
---|
2190 | switch (m->state)
|
---|
2191 | {
|
---|
2192 | case MediumState_Created:
|
---|
2193 | case MediumState_Inaccessible:
|
---|
2194 | case MediumState_LockedRead:
|
---|
2195 | {
|
---|
2196 | // must not hold any locks before calling Medium::i_queryInfo
|
---|
2197 | alock.release();
|
---|
2198 |
|
---|
2199 | rc = i_queryInfo(false /* fSetImageId */, false /* fSetParentId */,
|
---|
2200 | autoCaller);
|
---|
2201 |
|
---|
2202 | alock.acquire();
|
---|
2203 | break;
|
---|
2204 | }
|
---|
2205 | default:
|
---|
2206 | break;
|
---|
2207 | }
|
---|
2208 |
|
---|
2209 | *aState = m->state;
|
---|
2210 |
|
---|
2211 | return rc;
|
---|
2212 | }
|
---|
2213 |
|
---|
2214 | HRESULT Medium::getSnapshotIds(const com::Guid &aMachineId,
|
---|
2215 | std::vector<com::Guid> &aSnapshotIds)
|
---|
2216 | {
|
---|
2217 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2218 |
|
---|
2219 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
2220 | it != m->backRefs.end(); ++it)
|
---|
2221 | {
|
---|
2222 | if (it->machineId == aMachineId)
|
---|
2223 | {
|
---|
2224 | size_t size = it->llSnapshotIds.size();
|
---|
2225 |
|
---|
2226 | /* if the medium is attached to the machine in the current state, we
|
---|
2227 | * return its ID as the first element of the array */
|
---|
2228 | if (it->fInCurState)
|
---|
2229 | ++size;
|
---|
2230 |
|
---|
2231 | if (size > 0)
|
---|
2232 | {
|
---|
2233 | aSnapshotIds.resize(size);
|
---|
2234 |
|
---|
2235 | size_t j = 0;
|
---|
2236 | if (it->fInCurState)
|
---|
2237 | aSnapshotIds[j++] = it->machineId.toUtf16();
|
---|
2238 |
|
---|
2239 | for(GuidList::const_iterator jt = it->llSnapshotIds.begin(); jt != it->llSnapshotIds.end(); ++jt, ++j)
|
---|
2240 | aSnapshotIds[j] = (*jt);
|
---|
2241 | }
|
---|
2242 |
|
---|
2243 | break;
|
---|
2244 | }
|
---|
2245 | }
|
---|
2246 |
|
---|
2247 | return S_OK;
|
---|
2248 | }
|
---|
2249 |
|
---|
2250 | HRESULT Medium::lockRead(ComPtr<IToken> &aToken)
|
---|
2251 | {
|
---|
2252 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2253 |
|
---|
2254 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
2255 | if (m->queryInfoRunning)
|
---|
2256 | {
|
---|
2257 | /* Must not hold the media tree lock, as Medium::i_queryInfo needs this
|
---|
2258 | * lock and thus we would run into a deadlock here. */
|
---|
2259 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
2260 | while (m->queryInfoRunning)
|
---|
2261 | {
|
---|
2262 | alock.release();
|
---|
2263 | /* must not hold the object lock now */
|
---|
2264 | Assert(!isWriteLockOnCurrentThread());
|
---|
2265 | {
|
---|
2266 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
2267 | }
|
---|
2268 | alock.acquire();
|
---|
2269 | }
|
---|
2270 | }
|
---|
2271 |
|
---|
2272 | HRESULT rc = S_OK;
|
---|
2273 |
|
---|
2274 | switch (m->state)
|
---|
2275 | {
|
---|
2276 | case MediumState_Created:
|
---|
2277 | case MediumState_Inaccessible:
|
---|
2278 | case MediumState_LockedRead:
|
---|
2279 | {
|
---|
2280 | ++m->readers;
|
---|
2281 |
|
---|
2282 | ComAssertMsgBreak(m->readers != 0, ("Counter overflow"), rc = E_FAIL);
|
---|
2283 |
|
---|
2284 | /* Remember pre-lock state */
|
---|
2285 | if (m->state != MediumState_LockedRead)
|
---|
2286 | m->preLockState = m->state;
|
---|
2287 |
|
---|
2288 | LogFlowThisFunc(("Okay - prev state=%d readers=%d\n", m->state, m->readers));
|
---|
2289 | m->state = MediumState_LockedRead;
|
---|
2290 |
|
---|
2291 | ComObjPtr<MediumLockToken> pToken;
|
---|
2292 | rc = pToken.createObject();
|
---|
2293 | if (SUCCEEDED(rc))
|
---|
2294 | rc = pToken->init(this, false /* fWrite */);
|
---|
2295 | if (FAILED(rc))
|
---|
2296 | {
|
---|
2297 | --m->readers;
|
---|
2298 | if (m->readers == 0)
|
---|
2299 | m->state = m->preLockState;
|
---|
2300 | return rc;
|
---|
2301 | }
|
---|
2302 |
|
---|
2303 | pToken.queryInterfaceTo(aToken.asOutParam());
|
---|
2304 | break;
|
---|
2305 | }
|
---|
2306 | default:
|
---|
2307 | {
|
---|
2308 | LogFlowThisFunc(("Failing - state=%d\n", m->state));
|
---|
2309 | rc = i_setStateError();
|
---|
2310 | break;
|
---|
2311 | }
|
---|
2312 | }
|
---|
2313 |
|
---|
2314 | return rc;
|
---|
2315 | }
|
---|
2316 |
|
---|
2317 | /**
|
---|
2318 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
2319 | * in-process calls).
|
---|
2320 | */
|
---|
2321 | HRESULT Medium::i_unlockRead(MediumState_T *aState)
|
---|
2322 | {
|
---|
2323 | AutoCaller autoCaller(this);
|
---|
2324 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2325 |
|
---|
2326 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2327 |
|
---|
2328 | HRESULT rc = S_OK;
|
---|
2329 |
|
---|
2330 | switch (m->state)
|
---|
2331 | {
|
---|
2332 | case MediumState_LockedRead:
|
---|
2333 | {
|
---|
2334 | ComAssertMsgBreak(m->readers != 0, ("Counter underflow"), rc = E_FAIL);
|
---|
2335 | --m->readers;
|
---|
2336 |
|
---|
2337 | /* Reset the state after the last reader */
|
---|
2338 | if (m->readers == 0)
|
---|
2339 | {
|
---|
2340 | m->state = m->preLockState;
|
---|
2341 | /* There are cases where we inject the deleting state into
|
---|
2342 | * a medium locked for reading. Make sure #unmarkForDeletion()
|
---|
2343 | * gets the right state afterwards. */
|
---|
2344 | if (m->preLockState == MediumState_Deleting)
|
---|
2345 | m->preLockState = MediumState_Created;
|
---|
2346 | }
|
---|
2347 |
|
---|
2348 | LogFlowThisFunc(("new state=%d\n", m->state));
|
---|
2349 | break;
|
---|
2350 | }
|
---|
2351 | default:
|
---|
2352 | {
|
---|
2353 | LogFlowThisFunc(("Failing - state=%d\n", m->state));
|
---|
2354 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2355 | tr("Medium '%s' is not locked for reading"),
|
---|
2356 | m->strLocationFull.c_str());
|
---|
2357 | break;
|
---|
2358 | }
|
---|
2359 | }
|
---|
2360 |
|
---|
2361 | /* return the current state after */
|
---|
2362 | if (aState)
|
---|
2363 | *aState = m->state;
|
---|
2364 |
|
---|
2365 | return rc;
|
---|
2366 | }
|
---|
2367 | HRESULT Medium::lockWrite(ComPtr<IToken> &aToken)
|
---|
2368 | {
|
---|
2369 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2370 |
|
---|
2371 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
2372 | if (m->queryInfoRunning)
|
---|
2373 | {
|
---|
2374 | /* Must not hold the media tree lock, as Medium::i_queryInfo needs this
|
---|
2375 | * lock and thus we would run into a deadlock here. */
|
---|
2376 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
2377 | while (m->queryInfoRunning)
|
---|
2378 | {
|
---|
2379 | alock.release();
|
---|
2380 | /* must not hold the object lock now */
|
---|
2381 | Assert(!isWriteLockOnCurrentThread());
|
---|
2382 | {
|
---|
2383 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
2384 | }
|
---|
2385 | alock.acquire();
|
---|
2386 | }
|
---|
2387 | }
|
---|
2388 |
|
---|
2389 | HRESULT rc = S_OK;
|
---|
2390 |
|
---|
2391 | switch (m->state)
|
---|
2392 | {
|
---|
2393 | case MediumState_Created:
|
---|
2394 | case MediumState_Inaccessible:
|
---|
2395 | {
|
---|
2396 | m->preLockState = m->state;
|
---|
2397 |
|
---|
2398 | LogFlowThisFunc(("Okay - prev state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2399 | m->state = MediumState_LockedWrite;
|
---|
2400 |
|
---|
2401 | ComObjPtr<MediumLockToken> pToken;
|
---|
2402 | rc = pToken.createObject();
|
---|
2403 | if (SUCCEEDED(rc))
|
---|
2404 | rc = pToken->init(this, true /* fWrite */);
|
---|
2405 | if (FAILED(rc))
|
---|
2406 | {
|
---|
2407 | m->state = m->preLockState;
|
---|
2408 | return rc;
|
---|
2409 | }
|
---|
2410 |
|
---|
2411 | pToken.queryInterfaceTo(aToken.asOutParam());
|
---|
2412 | break;
|
---|
2413 | }
|
---|
2414 | default:
|
---|
2415 | {
|
---|
2416 | LogFlowThisFunc(("Failing - state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2417 | rc = i_setStateError();
|
---|
2418 | break;
|
---|
2419 | }
|
---|
2420 | }
|
---|
2421 |
|
---|
2422 | return rc;
|
---|
2423 | }
|
---|
2424 |
|
---|
2425 | /**
|
---|
2426 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
2427 | * in-process calls).
|
---|
2428 | */
|
---|
2429 | HRESULT Medium::i_unlockWrite(MediumState_T *aState)
|
---|
2430 | {
|
---|
2431 | AutoCaller autoCaller(this);
|
---|
2432 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2433 |
|
---|
2434 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2435 |
|
---|
2436 | HRESULT rc = S_OK;
|
---|
2437 |
|
---|
2438 | switch (m->state)
|
---|
2439 | {
|
---|
2440 | case MediumState_LockedWrite:
|
---|
2441 | {
|
---|
2442 | m->state = m->preLockState;
|
---|
2443 | /* There are cases where we inject the deleting state into
|
---|
2444 | * a medium locked for writing. Make sure #unmarkForDeletion()
|
---|
2445 | * gets the right state afterwards. */
|
---|
2446 | if (m->preLockState == MediumState_Deleting)
|
---|
2447 | m->preLockState = MediumState_Created;
|
---|
2448 | LogFlowThisFunc(("new state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2449 | break;
|
---|
2450 | }
|
---|
2451 | default:
|
---|
2452 | {
|
---|
2453 | LogFlowThisFunc(("Failing - state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2454 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2455 | tr("Medium '%s' is not locked for writing"),
|
---|
2456 | m->strLocationFull.c_str());
|
---|
2457 | break;
|
---|
2458 | }
|
---|
2459 | }
|
---|
2460 |
|
---|
2461 | /* return the current state after */
|
---|
2462 | if (aState)
|
---|
2463 | *aState = m->state;
|
---|
2464 |
|
---|
2465 | return rc;
|
---|
2466 | }
|
---|
2467 |
|
---|
2468 | HRESULT Medium::close(AutoCaller &aAutoCaller)
|
---|
2469 | {
|
---|
2470 | // make a copy of VirtualBox pointer which gets nulled by uninit()
|
---|
2471 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
2472 |
|
---|
2473 | MultiResult mrc = i_close(aAutoCaller);
|
---|
2474 |
|
---|
2475 | pVirtualBox->i_saveModifiedRegistries();
|
---|
2476 |
|
---|
2477 | return mrc;
|
---|
2478 | }
|
---|
2479 |
|
---|
2480 | HRESULT Medium::getProperty(const com::Utf8Str &aName,
|
---|
2481 | com::Utf8Str &aValue)
|
---|
2482 | {
|
---|
2483 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2484 |
|
---|
2485 | settings::StringsMap::const_iterator it = m->mapProperties.find(aName);
|
---|
2486 | if (it == m->mapProperties.end())
|
---|
2487 | {
|
---|
2488 | if (!aName.startsWith("Special/"))
|
---|
2489 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2490 | tr("Property '%s' does not exist"), aName.c_str());
|
---|
2491 | else
|
---|
2492 | /* be more silent here */
|
---|
2493 | return VBOX_E_OBJECT_NOT_FOUND;
|
---|
2494 | }
|
---|
2495 |
|
---|
2496 | aValue = it->second;
|
---|
2497 |
|
---|
2498 | return S_OK;
|
---|
2499 | }
|
---|
2500 |
|
---|
2501 | HRESULT Medium::setProperty(const com::Utf8Str &aName,
|
---|
2502 | const com::Utf8Str &aValue)
|
---|
2503 | {
|
---|
2504 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2505 |
|
---|
2506 | switch (m->state)
|
---|
2507 | {
|
---|
2508 | case MediumState_Created:
|
---|
2509 | case MediumState_Inaccessible:
|
---|
2510 | break;
|
---|
2511 | default:
|
---|
2512 | return i_setStateError();
|
---|
2513 | }
|
---|
2514 |
|
---|
2515 | settings::StringsMap::iterator it = m->mapProperties.find(aName);
|
---|
2516 | if ( !aName.startsWith("Special/")
|
---|
2517 | && !i_isPropertyForFilter(aName))
|
---|
2518 | {
|
---|
2519 | if (it == m->mapProperties.end())
|
---|
2520 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2521 | tr("Property '%s' does not exist"),
|
---|
2522 | aName.c_str());
|
---|
2523 | it->second = aValue;
|
---|
2524 | }
|
---|
2525 | else
|
---|
2526 | {
|
---|
2527 | if (it == m->mapProperties.end())
|
---|
2528 | {
|
---|
2529 | if (!aValue.isEmpty())
|
---|
2530 | m->mapProperties[aName] = aValue;
|
---|
2531 | }
|
---|
2532 | else
|
---|
2533 | {
|
---|
2534 | if (!aValue.isEmpty())
|
---|
2535 | it->second = aValue;
|
---|
2536 | else
|
---|
2537 | m->mapProperties.erase(it);
|
---|
2538 | }
|
---|
2539 | }
|
---|
2540 |
|
---|
2541 | // save the settings
|
---|
2542 | mlock.release();
|
---|
2543 | i_markRegistriesModified();
|
---|
2544 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2545 |
|
---|
2546 | return S_OK;
|
---|
2547 | }
|
---|
2548 |
|
---|
2549 | HRESULT Medium::getProperties(const com::Utf8Str &aNames,
|
---|
2550 | std::vector<com::Utf8Str> &aReturnNames,
|
---|
2551 | std::vector<com::Utf8Str> &aReturnValues)
|
---|
2552 | {
|
---|
2553 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2554 |
|
---|
2555 | /// @todo make use of aNames according to the documentation
|
---|
2556 | NOREF(aNames);
|
---|
2557 |
|
---|
2558 | aReturnNames.resize(m->mapProperties.size());
|
---|
2559 | aReturnValues.resize(m->mapProperties.size());
|
---|
2560 | size_t i = 0;
|
---|
2561 | for (settings::StringsMap::const_iterator it = m->mapProperties.begin();
|
---|
2562 | it != m->mapProperties.end();
|
---|
2563 | ++it, ++i)
|
---|
2564 | {
|
---|
2565 | aReturnNames[i] = it->first;
|
---|
2566 | aReturnValues[i] = it->second;
|
---|
2567 | }
|
---|
2568 | return S_OK;
|
---|
2569 | }
|
---|
2570 |
|
---|
2571 | HRESULT Medium::setProperties(const std::vector<com::Utf8Str> &aNames,
|
---|
2572 | const std::vector<com::Utf8Str> &aValues)
|
---|
2573 | {
|
---|
2574 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2575 |
|
---|
2576 | /* first pass: validate names */
|
---|
2577 | for (size_t i = 0;
|
---|
2578 | i < aNames.size();
|
---|
2579 | ++i)
|
---|
2580 | {
|
---|
2581 | Utf8Str strName(aNames[i]);
|
---|
2582 | if ( !strName.startsWith("Special/")
|
---|
2583 | && !i_isPropertyForFilter(strName)
|
---|
2584 | && m->mapProperties.find(strName) == m->mapProperties.end())
|
---|
2585 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2586 | tr("Property '%s' does not exist"), strName.c_str());
|
---|
2587 | }
|
---|
2588 |
|
---|
2589 | /* second pass: assign */
|
---|
2590 | for (size_t i = 0;
|
---|
2591 | i < aNames.size();
|
---|
2592 | ++i)
|
---|
2593 | {
|
---|
2594 | Utf8Str strName(aNames[i]);
|
---|
2595 | Utf8Str strValue(aValues[i]);
|
---|
2596 | settings::StringsMap::iterator it = m->mapProperties.find(strName);
|
---|
2597 | if ( !strName.startsWith("Special/")
|
---|
2598 | && !i_isPropertyForFilter(strName))
|
---|
2599 | {
|
---|
2600 | AssertReturn(it != m->mapProperties.end(), E_FAIL);
|
---|
2601 | it->second = strValue;
|
---|
2602 | }
|
---|
2603 | else
|
---|
2604 | {
|
---|
2605 | if (it == m->mapProperties.end())
|
---|
2606 | {
|
---|
2607 | if (!strValue.isEmpty())
|
---|
2608 | m->mapProperties[strName] = strValue;
|
---|
2609 | }
|
---|
2610 | else
|
---|
2611 | {
|
---|
2612 | if (!strValue.isEmpty())
|
---|
2613 | it->second = strValue;
|
---|
2614 | else
|
---|
2615 | m->mapProperties.erase(it);
|
---|
2616 | }
|
---|
2617 | }
|
---|
2618 | }
|
---|
2619 |
|
---|
2620 | // save the settings
|
---|
2621 | mlock.release();
|
---|
2622 | i_markRegistriesModified();
|
---|
2623 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2624 |
|
---|
2625 | return S_OK;
|
---|
2626 | }
|
---|
2627 | HRESULT Medium::createBaseStorage(LONG64 aLogicalSize,
|
---|
2628 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2629 | ComPtr<IProgress> &aProgress)
|
---|
2630 | {
|
---|
2631 | if (aLogicalSize < 0)
|
---|
2632 | return setError(E_INVALIDARG, tr("The medium size argument (%lld) is negative"), aLogicalSize);
|
---|
2633 |
|
---|
2634 | HRESULT rc = S_OK;
|
---|
2635 | ComObjPtr<Progress> pProgress;
|
---|
2636 | Medium::Task *pTask = NULL;
|
---|
2637 |
|
---|
2638 | try
|
---|
2639 | {
|
---|
2640 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2641 |
|
---|
2642 | ULONG mediumVariantFlags = 0;
|
---|
2643 |
|
---|
2644 | if (aVariant.size())
|
---|
2645 | {
|
---|
2646 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
2647 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
2648 | }
|
---|
2649 |
|
---|
2650 | mediumVariantFlags &= ((unsigned)~MediumVariant_Diff);
|
---|
2651 |
|
---|
2652 | if ( !(mediumVariantFlags & MediumVariant_Fixed)
|
---|
2653 | && !(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_CreateDynamic))
|
---|
2654 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2655 | tr("Medium format '%s' does not support dynamic storage creation"),
|
---|
2656 | m->strFormat.c_str());
|
---|
2657 |
|
---|
2658 | if ( (mediumVariantFlags & MediumVariant_Fixed)
|
---|
2659 | && !(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_CreateFixed))
|
---|
2660 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2661 | tr("Medium format '%s' does not support fixed storage creation"),
|
---|
2662 | m->strFormat.c_str());
|
---|
2663 |
|
---|
2664 | if (m->state != MediumState_NotCreated)
|
---|
2665 | throw i_setStateError();
|
---|
2666 |
|
---|
2667 | pProgress.createObject();
|
---|
2668 | rc = pProgress->init(m->pVirtualBox,
|
---|
2669 | static_cast<IMedium*>(this),
|
---|
2670 | (mediumVariantFlags & MediumVariant_Fixed)
|
---|
2671 | ? BstrFmt(tr("Creating fixed medium storage unit '%s'"), m->strLocationFull.c_str()).raw()
|
---|
2672 | : BstrFmt(tr("Creating dynamic medium storage unit '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
2673 | TRUE /* aCancelable */);
|
---|
2674 | if (FAILED(rc))
|
---|
2675 | throw rc;
|
---|
2676 |
|
---|
2677 | /* setup task object to carry out the operation asynchronously */
|
---|
2678 | pTask = new Medium::CreateBaseTask(this, pProgress, aLogicalSize,
|
---|
2679 | (MediumVariant_T)mediumVariantFlags);
|
---|
2680 | //(MediumVariant_T)aVariant);
|
---|
2681 | rc = pTask->rc();
|
---|
2682 | AssertComRC(rc);
|
---|
2683 | if (FAILED(rc))
|
---|
2684 | throw rc;
|
---|
2685 |
|
---|
2686 | m->state = MediumState_Creating;
|
---|
2687 | }
|
---|
2688 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2689 |
|
---|
2690 | if (SUCCEEDED(rc))
|
---|
2691 | {
|
---|
2692 | rc = pTask->createThread();
|
---|
2693 |
|
---|
2694 | if (SUCCEEDED(rc))
|
---|
2695 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2696 | }
|
---|
2697 | else if (pTask != NULL)
|
---|
2698 | delete pTask;
|
---|
2699 |
|
---|
2700 | return rc;
|
---|
2701 | }
|
---|
2702 |
|
---|
2703 | HRESULT Medium::deleteStorage(ComPtr<IProgress> &aProgress)
|
---|
2704 | {
|
---|
2705 | ComObjPtr<Progress> pProgress;
|
---|
2706 |
|
---|
2707 | MultiResult mrc = i_deleteStorage(&pProgress,
|
---|
2708 | false /* aWait */);
|
---|
2709 | /* Must save the registries in any case, since an entry was removed. */
|
---|
2710 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2711 |
|
---|
2712 | if (SUCCEEDED(mrc))
|
---|
2713 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2714 |
|
---|
2715 | return mrc;
|
---|
2716 | }
|
---|
2717 |
|
---|
2718 | HRESULT Medium::createDiffStorage(AutoCaller &autoCaller,
|
---|
2719 | const ComPtr<IMedium> &aTarget,
|
---|
2720 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2721 | ComPtr<IProgress> &aProgress)
|
---|
2722 | {
|
---|
2723 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
2724 | * to lock order violations, it probably causes lock order issues related
|
---|
2725 | * to the AutoCaller usage. */
|
---|
2726 | IMedium *aT = aTarget;
|
---|
2727 | ComObjPtr<Medium> diff = static_cast<Medium*>(aT);
|
---|
2728 |
|
---|
2729 | autoCaller.release();
|
---|
2730 |
|
---|
2731 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
2732 | * the pVirtualBox reference, see #uninit(). */
|
---|
2733 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
2734 |
|
---|
2735 | // we access m->pParent
|
---|
2736 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
2737 |
|
---|
2738 | autoCaller.add();
|
---|
2739 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2740 |
|
---|
2741 | AutoMultiWriteLock2 alock(this->lockHandle(), diff->lockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
2742 |
|
---|
2743 | if (m->type == MediumType_Writethrough)
|
---|
2744 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2745 | tr("Medium type of '%s' is Writethrough"),
|
---|
2746 | m->strLocationFull.c_str());
|
---|
2747 | else if (m->type == MediumType_Shareable)
|
---|
2748 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2749 | tr("Medium type of '%s' is Shareable"),
|
---|
2750 | m->strLocationFull.c_str());
|
---|
2751 | else if (m->type == MediumType_Readonly)
|
---|
2752 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2753 | tr("Medium type of '%s' is Readonly"),
|
---|
2754 | m->strLocationFull.c_str());
|
---|
2755 |
|
---|
2756 | /* Apply the normal locking logic to the entire chain. */
|
---|
2757 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
2758 | alock.release();
|
---|
2759 | treeLock.release();
|
---|
2760 | HRESULT rc = diff->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2761 | diff /* pToLockWrite */,
|
---|
2762 | false /* fMediumLockWriteAll */,
|
---|
2763 | this,
|
---|
2764 | *pMediumLockList);
|
---|
2765 | treeLock.acquire();
|
---|
2766 | alock.acquire();
|
---|
2767 | if (FAILED(rc))
|
---|
2768 | {
|
---|
2769 | delete pMediumLockList;
|
---|
2770 | return rc;
|
---|
2771 | }
|
---|
2772 |
|
---|
2773 | alock.release();
|
---|
2774 | treeLock.release();
|
---|
2775 | rc = pMediumLockList->Lock();
|
---|
2776 | treeLock.acquire();
|
---|
2777 | alock.acquire();
|
---|
2778 | if (FAILED(rc))
|
---|
2779 | {
|
---|
2780 | delete pMediumLockList;
|
---|
2781 |
|
---|
2782 | return setError(rc, tr("Could not lock medium when creating diff '%s'"),
|
---|
2783 | diff->i_getLocationFull().c_str());
|
---|
2784 | }
|
---|
2785 |
|
---|
2786 | Guid parentMachineRegistry;
|
---|
2787 | if (i_getFirstRegistryMachineId(parentMachineRegistry))
|
---|
2788 | {
|
---|
2789 | /* since this medium has been just created it isn't associated yet */
|
---|
2790 | diff->m->llRegistryIDs.push_back(parentMachineRegistry);
|
---|
2791 | alock.release();
|
---|
2792 | treeLock.release();
|
---|
2793 | diff->i_markRegistriesModified();
|
---|
2794 | treeLock.acquire();
|
---|
2795 | alock.acquire();
|
---|
2796 | }
|
---|
2797 |
|
---|
2798 | alock.release();
|
---|
2799 | treeLock.release();
|
---|
2800 |
|
---|
2801 | ComObjPtr<Progress> pProgress;
|
---|
2802 |
|
---|
2803 | ULONG mediumVariantFlags = 0;
|
---|
2804 |
|
---|
2805 | if (aVariant.size())
|
---|
2806 | {
|
---|
2807 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
2808 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
2809 | }
|
---|
2810 |
|
---|
2811 | rc = i_createDiffStorage(diff, (MediumVariant_T)mediumVariantFlags, pMediumLockList,
|
---|
2812 | &pProgress, false /* aWait */);
|
---|
2813 | if (FAILED(rc))
|
---|
2814 | delete pMediumLockList;
|
---|
2815 | else
|
---|
2816 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2817 |
|
---|
2818 | return rc;
|
---|
2819 | }
|
---|
2820 |
|
---|
2821 | HRESULT Medium::mergeTo(const ComPtr<IMedium> &aTarget,
|
---|
2822 | ComPtr<IProgress> &aProgress)
|
---|
2823 | {
|
---|
2824 |
|
---|
2825 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
2826 | * to lock order violations, it probably causes lock order issues related
|
---|
2827 | * to the AutoCaller usage. */
|
---|
2828 | IMedium *aT = aTarget;
|
---|
2829 |
|
---|
2830 | ComAssertRet(aT != this, E_INVALIDARG);
|
---|
2831 |
|
---|
2832 | ComObjPtr<Medium> pTarget = static_cast<Medium*>(aT);
|
---|
2833 |
|
---|
2834 | bool fMergeForward = false;
|
---|
2835 | ComObjPtr<Medium> pParentForTarget;
|
---|
2836 | MediumLockList *pChildrenToReparent = NULL;
|
---|
2837 | MediumLockList *pMediumLockList = NULL;
|
---|
2838 |
|
---|
2839 | HRESULT rc = S_OK;
|
---|
2840 |
|
---|
2841 | rc = i_prepareMergeTo(pTarget, NULL, NULL, true, fMergeForward,
|
---|
2842 | pParentForTarget, pChildrenToReparent, pMediumLockList);
|
---|
2843 | if (FAILED(rc)) return rc;
|
---|
2844 |
|
---|
2845 | ComObjPtr<Progress> pProgress;
|
---|
2846 |
|
---|
2847 | rc = i_mergeTo(pTarget, fMergeForward, pParentForTarget, pChildrenToReparent,
|
---|
2848 | pMediumLockList, &pProgress, false /* aWait */);
|
---|
2849 | if (FAILED(rc))
|
---|
2850 | i_cancelMergeTo(pChildrenToReparent, pMediumLockList);
|
---|
2851 | else
|
---|
2852 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2853 |
|
---|
2854 | return rc;
|
---|
2855 | }
|
---|
2856 |
|
---|
2857 | HRESULT Medium::cloneToBase(const ComPtr<IMedium> &aTarget,
|
---|
2858 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2859 | ComPtr<IProgress> &aProgress)
|
---|
2860 | {
|
---|
2861 | int rc = S_OK;
|
---|
2862 |
|
---|
2863 | rc = cloneTo(aTarget, aVariant, NULL, aProgress);
|
---|
2864 | return rc;
|
---|
2865 | }
|
---|
2866 |
|
---|
2867 | HRESULT Medium::cloneTo(const ComPtr<IMedium> &aTarget,
|
---|
2868 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2869 | const ComPtr<IMedium> &aParent,
|
---|
2870 | ComPtr<IProgress> &aProgress)
|
---|
2871 | {
|
---|
2872 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
2873 | * to lock order violations, it probably causes lock order issues related
|
---|
2874 | * to the AutoCaller usage. */
|
---|
2875 | ComAssertRet(aTarget != this, E_INVALIDARG);
|
---|
2876 |
|
---|
2877 | IMedium *aT = aTarget;
|
---|
2878 | ComObjPtr<Medium> pTarget = static_cast<Medium*>(aT);
|
---|
2879 | ComObjPtr<Medium> pParent;
|
---|
2880 | if (aParent)
|
---|
2881 | {
|
---|
2882 | IMedium *aP = aParent;
|
---|
2883 | pParent = static_cast<Medium*>(aP);
|
---|
2884 | }
|
---|
2885 |
|
---|
2886 | HRESULT rc = S_OK;
|
---|
2887 | ComObjPtr<Progress> pProgress;
|
---|
2888 | Medium::Task *pTask = NULL;
|
---|
2889 |
|
---|
2890 | try
|
---|
2891 | {
|
---|
2892 | // locking: we need the tree lock first because we access parent pointers
|
---|
2893 | // and we need to write-lock the media involved
|
---|
2894 | uint32_t cHandles = 3;
|
---|
2895 | LockHandle* pHandles[4] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
2896 | this->lockHandle(),
|
---|
2897 | pTarget->lockHandle() };
|
---|
2898 | /* Only add parent to the lock if it is not null */
|
---|
2899 | if (!pParent.isNull())
|
---|
2900 | pHandles[cHandles++] = pParent->lockHandle();
|
---|
2901 | AutoWriteLock alock(cHandles,
|
---|
2902 | pHandles
|
---|
2903 | COMMA_LOCKVAL_SRC_POS);
|
---|
2904 |
|
---|
2905 | if ( pTarget->m->state != MediumState_NotCreated
|
---|
2906 | && pTarget->m->state != MediumState_Created)
|
---|
2907 | throw pTarget->i_setStateError();
|
---|
2908 |
|
---|
2909 | /* Build the source lock list. */
|
---|
2910 | MediumLockList *pSourceMediumLockList(new MediumLockList());
|
---|
2911 | alock.release();
|
---|
2912 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2913 | NULL /* pToLockWrite */,
|
---|
2914 | false /* fMediumLockWriteAll */,
|
---|
2915 | NULL,
|
---|
2916 | *pSourceMediumLockList);
|
---|
2917 | alock.acquire();
|
---|
2918 | if (FAILED(rc))
|
---|
2919 | {
|
---|
2920 | delete pSourceMediumLockList;
|
---|
2921 | throw rc;
|
---|
2922 | }
|
---|
2923 |
|
---|
2924 | /* Build the target lock list (including the to-be parent chain). */
|
---|
2925 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
2926 | alock.release();
|
---|
2927 | rc = pTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2928 | pTarget /* pToLockWrite */,
|
---|
2929 | false /* fMediumLockWriteAll */,
|
---|
2930 | pParent,
|
---|
2931 | *pTargetMediumLockList);
|
---|
2932 | alock.acquire();
|
---|
2933 | if (FAILED(rc))
|
---|
2934 | {
|
---|
2935 | delete pSourceMediumLockList;
|
---|
2936 | delete pTargetMediumLockList;
|
---|
2937 | throw rc;
|
---|
2938 | }
|
---|
2939 |
|
---|
2940 | alock.release();
|
---|
2941 | rc = pSourceMediumLockList->Lock();
|
---|
2942 | alock.acquire();
|
---|
2943 | if (FAILED(rc))
|
---|
2944 | {
|
---|
2945 | delete pSourceMediumLockList;
|
---|
2946 | delete pTargetMediumLockList;
|
---|
2947 | throw setError(rc,
|
---|
2948 | tr("Failed to lock source media '%s'"),
|
---|
2949 | i_getLocationFull().c_str());
|
---|
2950 | }
|
---|
2951 | alock.release();
|
---|
2952 | rc = pTargetMediumLockList->Lock();
|
---|
2953 | alock.acquire();
|
---|
2954 | if (FAILED(rc))
|
---|
2955 | {
|
---|
2956 | delete pSourceMediumLockList;
|
---|
2957 | delete pTargetMediumLockList;
|
---|
2958 | throw setError(rc,
|
---|
2959 | tr("Failed to lock target media '%s'"),
|
---|
2960 | pTarget->i_getLocationFull().c_str());
|
---|
2961 | }
|
---|
2962 |
|
---|
2963 | pProgress.createObject();
|
---|
2964 | rc = pProgress->init(m->pVirtualBox,
|
---|
2965 | static_cast <IMedium *>(this),
|
---|
2966 | BstrFmt(tr("Creating clone medium '%s'"), pTarget->m->strLocationFull.c_str()).raw(),
|
---|
2967 | TRUE /* aCancelable */);
|
---|
2968 | if (FAILED(rc))
|
---|
2969 | {
|
---|
2970 | delete pSourceMediumLockList;
|
---|
2971 | delete pTargetMediumLockList;
|
---|
2972 | throw rc;
|
---|
2973 | }
|
---|
2974 |
|
---|
2975 | ULONG mediumVariantFlags = 0;
|
---|
2976 |
|
---|
2977 | if (aVariant.size())
|
---|
2978 | {
|
---|
2979 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
2980 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
2981 | }
|
---|
2982 |
|
---|
2983 | /* setup task object to carry out the operation asynchronously */
|
---|
2984 | pTask = new Medium::CloneTask(this, pProgress, pTarget,
|
---|
2985 | (MediumVariant_T)mediumVariantFlags,
|
---|
2986 | pParent, UINT32_MAX, UINT32_MAX,
|
---|
2987 | pSourceMediumLockList, pTargetMediumLockList);
|
---|
2988 | rc = pTask->rc();
|
---|
2989 | AssertComRC(rc);
|
---|
2990 | if (FAILED(rc))
|
---|
2991 | throw rc;
|
---|
2992 |
|
---|
2993 | if (pTarget->m->state == MediumState_NotCreated)
|
---|
2994 | pTarget->m->state = MediumState_Creating;
|
---|
2995 | }
|
---|
2996 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2997 |
|
---|
2998 | if (SUCCEEDED(rc))
|
---|
2999 | {
|
---|
3000 | rc = pTask->createThread();
|
---|
3001 |
|
---|
3002 | if (SUCCEEDED(rc))
|
---|
3003 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3004 | }
|
---|
3005 | else if (pTask != NULL)
|
---|
3006 | delete pTask;
|
---|
3007 |
|
---|
3008 | return rc;
|
---|
3009 | }
|
---|
3010 |
|
---|
3011 | HRESULT Medium::setLocation(const com::Utf8Str &aLocation, ComPtr<IProgress> &aProgress)
|
---|
3012 | {
|
---|
3013 |
|
---|
3014 | ComObjPtr<Medium> pParent;
|
---|
3015 | ComObjPtr<Progress> pProgress;
|
---|
3016 | HRESULT rc = S_OK;
|
---|
3017 | Medium::Task *pTask = NULL;
|
---|
3018 |
|
---|
3019 | try
|
---|
3020 | {
|
---|
3021 | /// @todo NEWMEDIA for file names, add the default extension if no extension
|
---|
3022 | /// is present (using the information from the VD backend which also implies
|
---|
3023 | /// that one more parameter should be passed to setLocation() requesting
|
---|
3024 | /// that functionality since it is only allowed when called from this method
|
---|
3025 |
|
---|
3026 | /// @todo NEWMEDIA rename the file and set m->location on success, then save
|
---|
3027 | /// the global registry (and local registries of portable VMs referring to
|
---|
3028 | /// this medium), this will also require to add the mRegistered flag to data
|
---|
3029 |
|
---|
3030 | // locking: we need the tree lock first because we access parent pointers
|
---|
3031 | // and we need to write-lock the media involved
|
---|
3032 | uint32_t cHandles = 2;
|
---|
3033 | LockHandle* pHandles[2] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
3034 | this->lockHandle() };
|
---|
3035 |
|
---|
3036 | AutoWriteLock alock(cHandles,
|
---|
3037 | pHandles
|
---|
3038 | COMMA_LOCKVAL_SRC_POS);
|
---|
3039 |
|
---|
3040 | /* play with locations */
|
---|
3041 | {
|
---|
3042 | /*get source path and filename */
|
---|
3043 | Utf8Str sourceMediumPath = i_getLocationFull();
|
---|
3044 | Utf8Str sourceMediumFileName = i_getName();
|
---|
3045 |
|
---|
3046 | if (aLocation.isEmpty())
|
---|
3047 | {
|
---|
3048 | rc = setError(VERR_PATH_ZERO_LENGTH,
|
---|
3049 | tr("Medium '%s' can't be moved. Destination path is empty."),
|
---|
3050 | i_getLocationFull().c_str());
|
---|
3051 | throw rc;
|
---|
3052 | }
|
---|
3053 |
|
---|
3054 | /*extract destination path and filename */
|
---|
3055 | Utf8Str destMediumPath(aLocation);
|
---|
3056 | Utf8Str destMediumFileName(destMediumPath);
|
---|
3057 | destMediumFileName.stripPath();
|
---|
3058 |
|
---|
3059 | Utf8Str suffix(destMediumFileName);
|
---|
3060 | suffix.stripSuffix();//for small trick, see next condition
|
---|
3061 |
|
---|
3062 | if(suffix.equals(destMediumFileName) && !destMediumFileName.isEmpty())
|
---|
3063 | {
|
---|
3064 | /*
|
---|
3065 | * small trick. This case means target path has no filename at the end.
|
---|
3066 | * it will look like "/path/to/new/location" or just "newname"
|
---|
3067 | * there is no backslash in the end
|
---|
3068 | * or there is no filename with extension(suffix) in the end
|
---|
3069 | */
|
---|
3070 |
|
---|
3071 | /* case when new path contains only "newname", no path, no extension */
|
---|
3072 | if (destMediumPath.equals(destMediumFileName))
|
---|
3073 | {
|
---|
3074 | Utf8Str localSuffix = RTPathSuffix(sourceMediumFileName.c_str());
|
---|
3075 | destMediumFileName.append(localSuffix);
|
---|
3076 | destMediumPath = destMediumFileName;
|
---|
3077 | }
|
---|
3078 | /* case when new path looks like "/path/to/new/location"
|
---|
3079 | * In this case just set destMediumFileName to NULL and
|
---|
3080 | * and add '/' in the end of path.destMediumPath
|
---|
3081 | */
|
---|
3082 | else
|
---|
3083 | {
|
---|
3084 | destMediumFileName.setNull();
|
---|
3085 | destMediumPath.append(RTPATH_SLASH);
|
---|
3086 | }
|
---|
3087 | }
|
---|
3088 |
|
---|
3089 | if (destMediumFileName.isEmpty())
|
---|
3090 | {
|
---|
3091 | /* case when a target name is absent */
|
---|
3092 | destMediumPath.append(sourceMediumFileName);
|
---|
3093 | }
|
---|
3094 | else
|
---|
3095 | {
|
---|
3096 | if (destMediumPath.equals(destMediumFileName))
|
---|
3097 | {
|
---|
3098 | /* the passed target path consist of only a filename without directory
|
---|
3099 | * next move medium within the source directory with the passed new name
|
---|
3100 | */
|
---|
3101 | destMediumPath = sourceMediumPath.stripFilename().append(RTPATH_SLASH).append(destMediumFileName);
|
---|
3102 | }
|
---|
3103 | suffix = i_getFormat();
|
---|
3104 |
|
---|
3105 | if (suffix.compare("RAW", Utf8Str::CaseInsensitive) == 0)
|
---|
3106 | {
|
---|
3107 | if(i_getDeviceType() == DeviceType_DVD)
|
---|
3108 | {
|
---|
3109 | suffix = "iso";
|
---|
3110 | }
|
---|
3111 | else
|
---|
3112 | {
|
---|
3113 | rc = setError(VERR_NOT_A_FILE,
|
---|
3114 | tr("Medium '%s' has RAW type. \"Move\" operation isn't supported for this type."),
|
---|
3115 | i_getLocationFull().c_str());
|
---|
3116 | throw rc;
|
---|
3117 | }
|
---|
3118 | }
|
---|
3119 | /* set the target extension like on the source. Any conversions are prohibited */
|
---|
3120 | suffix.toLower();
|
---|
3121 | destMediumPath.stripSuffix().append('.').append(suffix);
|
---|
3122 | }
|
---|
3123 |
|
---|
3124 | if (i_isMediumFormatFile())
|
---|
3125 | {
|
---|
3126 | /* Check path for a new file object */
|
---|
3127 | rc = VirtualBox::i_ensureFilePathExists(destMediumPath, true);
|
---|
3128 | if (FAILED(rc))
|
---|
3129 | throw rc;
|
---|
3130 | }
|
---|
3131 | else
|
---|
3132 | {
|
---|
3133 | rc = setError(VERR_NOT_A_FILE,
|
---|
3134 | tr("Medium '%s' isn't a file object. \"Move\" operation isn't supported."),
|
---|
3135 | i_getLocationFull().c_str());
|
---|
3136 | throw rc;
|
---|
3137 | }
|
---|
3138 |
|
---|
3139 | /* Set needed variables for "moving" procedure. It'll be used later in separate thread task */
|
---|
3140 | rc = i_preparationForMoving(destMediumPath);
|
---|
3141 | if (FAILED(rc))
|
---|
3142 | {
|
---|
3143 | rc = setError(VERR_NO_CHANGE,
|
---|
3144 | tr("Medium '%s' is already in the correct location"),
|
---|
3145 | i_getLocationFull().c_str());
|
---|
3146 | throw rc;
|
---|
3147 | }
|
---|
3148 | }
|
---|
3149 |
|
---|
3150 | /* Check VMs which have this medium attached to*/
|
---|
3151 | std::vector<com::Guid> aMachineIds;
|
---|
3152 | rc = getMachineIds(aMachineIds);
|
---|
3153 | std::vector<com::Guid>::const_iterator currMachineID = aMachineIds.begin();
|
---|
3154 | std::vector<com::Guid>::const_iterator lastMachineID = aMachineIds.end();
|
---|
3155 |
|
---|
3156 | while (currMachineID != lastMachineID)
|
---|
3157 | {
|
---|
3158 | Guid id(*currMachineID);
|
---|
3159 | ComObjPtr<Machine> aMachine;
|
---|
3160 |
|
---|
3161 | alock.release();
|
---|
3162 | rc = m->pVirtualBox->i_findMachine(id, false, true, &aMachine);
|
---|
3163 | alock.acquire();
|
---|
3164 |
|
---|
3165 | if (SUCCEEDED(rc))
|
---|
3166 | {
|
---|
3167 | ComObjPtr<SessionMachine> sm;
|
---|
3168 | ComPtr<IInternalSessionControl> ctl;
|
---|
3169 |
|
---|
3170 | alock.release();
|
---|
3171 | bool ses = aMachine->i_isSessionOpenVM(sm, &ctl);
|
---|
3172 | alock.acquire();
|
---|
3173 |
|
---|
3174 | if (ses)
|
---|
3175 | {
|
---|
3176 | rc = setError(VERR_VM_UNEXPECTED_VM_STATE,
|
---|
3177 | tr("At least VM '%s' to whom this medium '%s' attached has the opened session now. "
|
---|
3178 | "Stop all needed VM before set a new location."),
|
---|
3179 | id.toString().c_str(),
|
---|
3180 | i_getLocationFull().c_str());
|
---|
3181 | throw rc;
|
---|
3182 | }
|
---|
3183 | }
|
---|
3184 | ++currMachineID;
|
---|
3185 | }
|
---|
3186 |
|
---|
3187 | /* Build the source lock list. */
|
---|
3188 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3189 | alock.release();
|
---|
3190 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
3191 | this /* pToLockWrite */,
|
---|
3192 | true /* fMediumLockWriteAll */,
|
---|
3193 | NULL,
|
---|
3194 | *pMediumLockList);
|
---|
3195 | alock.acquire();
|
---|
3196 | if (FAILED(rc))
|
---|
3197 | {
|
---|
3198 | delete pMediumLockList;
|
---|
3199 | throw setError(rc,
|
---|
3200 | tr("Failed to create medium lock list for '%s'"),
|
---|
3201 | i_getLocationFull().c_str());
|
---|
3202 | }
|
---|
3203 | alock.release();
|
---|
3204 | rc = pMediumLockList->Lock();
|
---|
3205 | alock.acquire();
|
---|
3206 | if (FAILED(rc))
|
---|
3207 | {
|
---|
3208 | delete pMediumLockList;
|
---|
3209 | throw setError(rc,
|
---|
3210 | tr("Failed to lock media '%s'"),
|
---|
3211 | i_getLocationFull().c_str());
|
---|
3212 | }
|
---|
3213 |
|
---|
3214 | pProgress.createObject();
|
---|
3215 | rc = pProgress->init(m->pVirtualBox,
|
---|
3216 | static_cast <IMedium *>(this),
|
---|
3217 | BstrFmt(tr("Moving medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3218 | TRUE /* aCancelable */);
|
---|
3219 |
|
---|
3220 | /* Do the disk moving. */
|
---|
3221 | if (SUCCEEDED(rc))
|
---|
3222 | {
|
---|
3223 | ULONG mediumVariantFlags = i_getVariant();
|
---|
3224 |
|
---|
3225 | /* setup task object to carry out the operation asynchronously */
|
---|
3226 | pTask = new Medium::MoveTask(this, pProgress,
|
---|
3227 | (MediumVariant_T)mediumVariantFlags,
|
---|
3228 | pMediumLockList);
|
---|
3229 | rc = pTask->rc();
|
---|
3230 | AssertComRC(rc);
|
---|
3231 | if (FAILED(rc))
|
---|
3232 | throw rc;
|
---|
3233 | }
|
---|
3234 |
|
---|
3235 | }
|
---|
3236 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3237 |
|
---|
3238 | if (SUCCEEDED(rc))
|
---|
3239 | {
|
---|
3240 | rc = pTask->createThread();
|
---|
3241 |
|
---|
3242 | if (SUCCEEDED(rc))
|
---|
3243 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3244 | }
|
---|
3245 | else
|
---|
3246 | {
|
---|
3247 | if (pTask != NULL)
|
---|
3248 | delete pTask;
|
---|
3249 | }
|
---|
3250 |
|
---|
3251 | return rc;
|
---|
3252 | }
|
---|
3253 |
|
---|
3254 | HRESULT Medium::compact(ComPtr<IProgress> &aProgress)
|
---|
3255 | {
|
---|
3256 | HRESULT rc = S_OK;
|
---|
3257 | ComObjPtr<Progress> pProgress;
|
---|
3258 | Medium::Task *pTask = NULL;
|
---|
3259 |
|
---|
3260 | try
|
---|
3261 | {
|
---|
3262 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3263 |
|
---|
3264 | /* Build the medium lock list. */
|
---|
3265 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3266 | alock.release();
|
---|
3267 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
3268 | this /* pToLockWrite */,
|
---|
3269 | false /* fMediumLockWriteAll */,
|
---|
3270 | NULL,
|
---|
3271 | *pMediumLockList);
|
---|
3272 | alock.acquire();
|
---|
3273 | if (FAILED(rc))
|
---|
3274 | {
|
---|
3275 | delete pMediumLockList;
|
---|
3276 | throw rc;
|
---|
3277 | }
|
---|
3278 |
|
---|
3279 | alock.release();
|
---|
3280 | rc = pMediumLockList->Lock();
|
---|
3281 | alock.acquire();
|
---|
3282 | if (FAILED(rc))
|
---|
3283 | {
|
---|
3284 | delete pMediumLockList;
|
---|
3285 | throw setError(rc,
|
---|
3286 | tr("Failed to lock media when compacting '%s'"),
|
---|
3287 | i_getLocationFull().c_str());
|
---|
3288 | }
|
---|
3289 |
|
---|
3290 | pProgress.createObject();
|
---|
3291 | rc = pProgress->init(m->pVirtualBox,
|
---|
3292 | static_cast <IMedium *>(this),
|
---|
3293 | BstrFmt(tr("Compacting medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3294 | TRUE /* aCancelable */);
|
---|
3295 | if (FAILED(rc))
|
---|
3296 | {
|
---|
3297 | delete pMediumLockList;
|
---|
3298 | throw rc;
|
---|
3299 | }
|
---|
3300 |
|
---|
3301 | /* setup task object to carry out the operation asynchronously */
|
---|
3302 | pTask = new Medium::CompactTask(this, pProgress, pMediumLockList);
|
---|
3303 | rc = pTask->rc();
|
---|
3304 | AssertComRC(rc);
|
---|
3305 | if (FAILED(rc))
|
---|
3306 | throw rc;
|
---|
3307 | }
|
---|
3308 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3309 |
|
---|
3310 | if (SUCCEEDED(rc))
|
---|
3311 | {
|
---|
3312 | rc = pTask->createThread();
|
---|
3313 |
|
---|
3314 | if (SUCCEEDED(rc))
|
---|
3315 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3316 | }
|
---|
3317 | else if (pTask != NULL)
|
---|
3318 | delete pTask;
|
---|
3319 |
|
---|
3320 | return rc;
|
---|
3321 | }
|
---|
3322 |
|
---|
3323 | HRESULT Medium::resize(LONG64 aLogicalSize,
|
---|
3324 | ComPtr<IProgress> &aProgress)
|
---|
3325 | {
|
---|
3326 | HRESULT rc = S_OK;
|
---|
3327 | ComObjPtr<Progress> pProgress;
|
---|
3328 | Medium::Task *pTask = NULL;
|
---|
3329 |
|
---|
3330 | try
|
---|
3331 | {
|
---|
3332 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3333 |
|
---|
3334 | /* Build the medium lock list. */
|
---|
3335 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3336 | alock.release();
|
---|
3337 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
3338 | this /* pToLockWrite */,
|
---|
3339 | false /* fMediumLockWriteAll */,
|
---|
3340 | NULL,
|
---|
3341 | *pMediumLockList);
|
---|
3342 | alock.acquire();
|
---|
3343 | if (FAILED(rc))
|
---|
3344 | {
|
---|
3345 | delete pMediumLockList;
|
---|
3346 | throw rc;
|
---|
3347 | }
|
---|
3348 |
|
---|
3349 | alock.release();
|
---|
3350 | rc = pMediumLockList->Lock();
|
---|
3351 | alock.acquire();
|
---|
3352 | if (FAILED(rc))
|
---|
3353 | {
|
---|
3354 | delete pMediumLockList;
|
---|
3355 | throw setError(rc,
|
---|
3356 | tr("Failed to lock media when compacting '%s'"),
|
---|
3357 | i_getLocationFull().c_str());
|
---|
3358 | }
|
---|
3359 |
|
---|
3360 | pProgress.createObject();
|
---|
3361 | rc = pProgress->init(m->pVirtualBox,
|
---|
3362 | static_cast <IMedium *>(this),
|
---|
3363 | BstrFmt(tr("Compacting medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3364 | TRUE /* aCancelable */);
|
---|
3365 | if (FAILED(rc))
|
---|
3366 | {
|
---|
3367 | delete pMediumLockList;
|
---|
3368 | throw rc;
|
---|
3369 | }
|
---|
3370 |
|
---|
3371 | /* setup task object to carry out the operation asynchronously */
|
---|
3372 | pTask = new Medium::ResizeTask(this, aLogicalSize, pProgress, pMediumLockList);
|
---|
3373 | rc = pTask->rc();
|
---|
3374 | AssertComRC(rc);
|
---|
3375 | if (FAILED(rc))
|
---|
3376 | throw rc;
|
---|
3377 | }
|
---|
3378 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3379 |
|
---|
3380 | if (SUCCEEDED(rc))
|
---|
3381 | {
|
---|
3382 | rc = pTask->createThread();
|
---|
3383 |
|
---|
3384 | if (SUCCEEDED(rc))
|
---|
3385 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3386 | }
|
---|
3387 | else if (pTask != NULL)
|
---|
3388 | delete pTask;
|
---|
3389 |
|
---|
3390 | return rc;
|
---|
3391 | }
|
---|
3392 |
|
---|
3393 | HRESULT Medium::reset(AutoCaller &autoCaller, ComPtr<IProgress> &aProgress)
|
---|
3394 | {
|
---|
3395 | HRESULT rc = S_OK;
|
---|
3396 | ComObjPtr<Progress> pProgress;
|
---|
3397 | Medium::Task *pTask = NULL;
|
---|
3398 |
|
---|
3399 | try
|
---|
3400 | {
|
---|
3401 | autoCaller.release();
|
---|
3402 |
|
---|
3403 | /* It is possible that some previous/concurrent uninit has already
|
---|
3404 | * cleared the pVirtualBox reference, see #uninit(). */
|
---|
3405 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
3406 |
|
---|
3407 | /* canClose() needs the tree lock */
|
---|
3408 | AutoMultiWriteLock2 multilock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL,
|
---|
3409 | this->lockHandle()
|
---|
3410 | COMMA_LOCKVAL_SRC_POS);
|
---|
3411 |
|
---|
3412 | autoCaller.add();
|
---|
3413 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
3414 |
|
---|
3415 | LogFlowThisFunc(("ENTER for medium %s\n", m->strLocationFull.c_str()));
|
---|
3416 |
|
---|
3417 | if (m->pParent.isNull())
|
---|
3418 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3419 | tr("Medium type of '%s' is not differencing"),
|
---|
3420 | m->strLocationFull.c_str());
|
---|
3421 |
|
---|
3422 | rc = i_canClose();
|
---|
3423 | if (FAILED(rc))
|
---|
3424 | throw rc;
|
---|
3425 |
|
---|
3426 | /* Build the medium lock list. */
|
---|
3427 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3428 | multilock.release();
|
---|
3429 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
3430 | this /* pToLockWrite */,
|
---|
3431 | false /* fMediumLockWriteAll */,
|
---|
3432 | NULL,
|
---|
3433 | *pMediumLockList);
|
---|
3434 | multilock.acquire();
|
---|
3435 | if (FAILED(rc))
|
---|
3436 | {
|
---|
3437 | delete pMediumLockList;
|
---|
3438 | throw rc;
|
---|
3439 | }
|
---|
3440 |
|
---|
3441 | multilock.release();
|
---|
3442 | rc = pMediumLockList->Lock();
|
---|
3443 | multilock.acquire();
|
---|
3444 | if (FAILED(rc))
|
---|
3445 | {
|
---|
3446 | delete pMediumLockList;
|
---|
3447 | throw setError(rc,
|
---|
3448 | tr("Failed to lock media when resetting '%s'"),
|
---|
3449 | i_getLocationFull().c_str());
|
---|
3450 | }
|
---|
3451 |
|
---|
3452 | pProgress.createObject();
|
---|
3453 | rc = pProgress->init(m->pVirtualBox,
|
---|
3454 | static_cast<IMedium*>(this),
|
---|
3455 | BstrFmt(tr("Resetting differencing medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3456 | FALSE /* aCancelable */);
|
---|
3457 | if (FAILED(rc))
|
---|
3458 | throw rc;
|
---|
3459 |
|
---|
3460 | /* setup task object to carry out the operation asynchronously */
|
---|
3461 | pTask = new Medium::ResetTask(this, pProgress, pMediumLockList);
|
---|
3462 | rc = pTask->rc();
|
---|
3463 | AssertComRC(rc);
|
---|
3464 | if (FAILED(rc))
|
---|
3465 | throw rc;
|
---|
3466 | }
|
---|
3467 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3468 |
|
---|
3469 | if (SUCCEEDED(rc))
|
---|
3470 | {
|
---|
3471 | rc = pTask->createThread();
|
---|
3472 |
|
---|
3473 | if (SUCCEEDED(rc))
|
---|
3474 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3475 | }
|
---|
3476 | else if (pTask != NULL)
|
---|
3477 | delete pTask;
|
---|
3478 |
|
---|
3479 | LogFlowThisFunc(("LEAVE, rc=%Rhrc\n", rc));
|
---|
3480 |
|
---|
3481 | return rc;
|
---|
3482 | }
|
---|
3483 |
|
---|
3484 | HRESULT Medium::changeEncryption(const com::Utf8Str &aCurrentPassword, const com::Utf8Str &aCipher,
|
---|
3485 | const com::Utf8Str &aNewPassword, const com::Utf8Str &aNewPasswordId,
|
---|
3486 | ComPtr<IProgress> &aProgress)
|
---|
3487 | {
|
---|
3488 | HRESULT rc = S_OK;
|
---|
3489 | ComObjPtr<Progress> pProgress;
|
---|
3490 | Medium::Task *pTask = NULL;
|
---|
3491 |
|
---|
3492 | try
|
---|
3493 | {
|
---|
3494 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3495 |
|
---|
3496 | DeviceType_T devType = i_getDeviceType();
|
---|
3497 | /* Cannot encrypt DVD or floppy images so far. */
|
---|
3498 | if ( devType == DeviceType_DVD
|
---|
3499 | || devType == DeviceType_Floppy)
|
---|
3500 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3501 | tr("Cannot encrypt DVD or Floppy medium '%s'"),
|
---|
3502 | m->strLocationFull.c_str());
|
---|
3503 |
|
---|
3504 | /* Cannot encrypt media which are attached to more than one virtual machine. */
|
---|
3505 | if (m->backRefs.size() > 1)
|
---|
3506 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3507 | tr("Cannot encrypt medium '%s' because it is attached to %d virtual machines"),
|
---|
3508 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
3509 |
|
---|
3510 | if (i_getChildren().size() != 0)
|
---|
3511 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3512 | tr("Cannot encrypt medium '%s' because it has %d children"),
|
---|
3513 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
3514 |
|
---|
3515 | /* Build the medium lock list. */
|
---|
3516 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3517 | alock.release();
|
---|
3518 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
3519 | this /* pToLockWrite */,
|
---|
3520 | true /* fMediumLockAllWrite */,
|
---|
3521 | NULL,
|
---|
3522 | *pMediumLockList);
|
---|
3523 | alock.acquire();
|
---|
3524 | if (FAILED(rc))
|
---|
3525 | {
|
---|
3526 | delete pMediumLockList;
|
---|
3527 | throw rc;
|
---|
3528 | }
|
---|
3529 |
|
---|
3530 | alock.release();
|
---|
3531 | rc = pMediumLockList->Lock();
|
---|
3532 | alock.acquire();
|
---|
3533 | if (FAILED(rc))
|
---|
3534 | {
|
---|
3535 | delete pMediumLockList;
|
---|
3536 | throw setError(rc,
|
---|
3537 | tr("Failed to lock media for encryption '%s'"),
|
---|
3538 | i_getLocationFull().c_str());
|
---|
3539 | }
|
---|
3540 |
|
---|
3541 | /*
|
---|
3542 | * Check all media in the chain to not contain any branches or references to
|
---|
3543 | * other virtual machines, we support encrypting only a list of differencing media at the moment.
|
---|
3544 | */
|
---|
3545 | MediumLockList::Base::const_iterator mediumListBegin = pMediumLockList->GetBegin();
|
---|
3546 | MediumLockList::Base::const_iterator mediumListEnd = pMediumLockList->GetEnd();
|
---|
3547 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
3548 | it != mediumListEnd;
|
---|
3549 | ++it)
|
---|
3550 | {
|
---|
3551 | const MediumLock &mediumLock = *it;
|
---|
3552 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
3553 | AutoReadLock mediumReadLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
3554 |
|
---|
3555 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
3556 |
|
---|
3557 | if (pMedium->m->backRefs.size() > 1)
|
---|
3558 | {
|
---|
3559 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3560 | tr("Cannot encrypt medium '%s' because it is attached to %d virtual machines"),
|
---|
3561 | pMedium->m->strLocationFull.c_str(), pMedium->m->backRefs.size());
|
---|
3562 | break;
|
---|
3563 | }
|
---|
3564 | else if (pMedium->i_getChildren().size() > 1)
|
---|
3565 | {
|
---|
3566 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3567 | tr("Cannot encrypt medium '%s' because it has %d children"),
|
---|
3568 | pMedium->m->strLocationFull.c_str(), pMedium->i_getChildren().size());
|
---|
3569 | break;
|
---|
3570 | }
|
---|
3571 | }
|
---|
3572 |
|
---|
3573 | if (FAILED(rc))
|
---|
3574 | {
|
---|
3575 | delete pMediumLockList;
|
---|
3576 | throw rc;
|
---|
3577 | }
|
---|
3578 |
|
---|
3579 | const char *pszAction = "Encrypting";
|
---|
3580 | if ( aCurrentPassword.isNotEmpty()
|
---|
3581 | && aCipher.isEmpty())
|
---|
3582 | pszAction = "Decrypting";
|
---|
3583 |
|
---|
3584 | pProgress.createObject();
|
---|
3585 | rc = pProgress->init(m->pVirtualBox,
|
---|
3586 | static_cast <IMedium *>(this),
|
---|
3587 | BstrFmt(tr("%s medium '%s'"), pszAction, m->strLocationFull.c_str()).raw(),
|
---|
3588 | TRUE /* aCancelable */);
|
---|
3589 | if (FAILED(rc))
|
---|
3590 | {
|
---|
3591 | delete pMediumLockList;
|
---|
3592 | throw rc;
|
---|
3593 | }
|
---|
3594 |
|
---|
3595 | /* setup task object to carry out the operation asynchronously */
|
---|
3596 | pTask = new Medium::EncryptTask(this, aNewPassword, aCurrentPassword,
|
---|
3597 | aCipher, aNewPasswordId, pProgress, pMediumLockList);
|
---|
3598 | rc = pTask->rc();
|
---|
3599 | AssertComRC(rc);
|
---|
3600 | if (FAILED(rc))
|
---|
3601 | throw rc;
|
---|
3602 | }
|
---|
3603 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3604 |
|
---|
3605 | if (SUCCEEDED(rc))
|
---|
3606 | {
|
---|
3607 | rc = pTask->createThread();
|
---|
3608 |
|
---|
3609 | if (SUCCEEDED(rc))
|
---|
3610 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3611 | }
|
---|
3612 | else if (pTask != NULL)
|
---|
3613 | delete pTask;
|
---|
3614 |
|
---|
3615 | return rc;
|
---|
3616 | }
|
---|
3617 |
|
---|
3618 | HRESULT Medium::getEncryptionSettings(com::Utf8Str &aCipher, com::Utf8Str &aPasswordId)
|
---|
3619 | {
|
---|
3620 | #ifndef VBOX_WITH_EXTPACK
|
---|
3621 | RT_NOREF(aCipher, aPasswordId);
|
---|
3622 | #endif
|
---|
3623 | HRESULT rc = S_OK;
|
---|
3624 |
|
---|
3625 | try
|
---|
3626 | {
|
---|
3627 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
3628 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3629 |
|
---|
3630 | /* Check whether encryption is configured for this medium. */
|
---|
3631 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
3632 | if (it == pBase->m->mapProperties.end())
|
---|
3633 | throw VBOX_E_NOT_SUPPORTED;
|
---|
3634 |
|
---|
3635 | # ifdef VBOX_WITH_EXTPACK
|
---|
3636 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
3637 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
3638 | {
|
---|
3639 | /* Load the plugin */
|
---|
3640 | Utf8Str strPlugin;
|
---|
3641 | rc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
3642 | if (SUCCEEDED(rc))
|
---|
3643 | {
|
---|
3644 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
3645 | if (RT_FAILURE(vrc))
|
---|
3646 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3647 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
3648 | i_vdError(vrc).c_str());
|
---|
3649 | }
|
---|
3650 | else
|
---|
3651 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3652 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
3653 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
3654 | }
|
---|
3655 | else
|
---|
3656 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3657 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
3658 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
3659 |
|
---|
3660 | PVBOXHDD pDisk = NULL;
|
---|
3661 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
3662 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
3663 |
|
---|
3664 | Medium::CryptoFilterSettings CryptoSettings;
|
---|
3665 |
|
---|
3666 | i_taskEncryptSettingsSetup(&CryptoSettings, NULL, it->second.c_str(), NULL, false /* fCreateKeyStore */);
|
---|
3667 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ | VD_FILTER_FLAGS_INFO, CryptoSettings.vdFilterIfaces);
|
---|
3668 | if (RT_FAILURE(vrc))
|
---|
3669 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3670 | tr("Failed to load the encryption filter: %s"),
|
---|
3671 | i_vdError(vrc).c_str());
|
---|
3672 |
|
---|
3673 | it = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
3674 | if (it == pBase->m->mapProperties.end())
|
---|
3675 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3676 | tr("Image is configured for encryption but doesn't has a KeyId set"));
|
---|
3677 |
|
---|
3678 | aPasswordId = it->second.c_str();
|
---|
3679 | aCipher = CryptoSettings.pszCipherReturned;
|
---|
3680 | RTStrFree(CryptoSettings.pszCipherReturned);
|
---|
3681 |
|
---|
3682 | VDDestroy(pDisk);
|
---|
3683 | # else
|
---|
3684 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3685 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
3686 | # endif
|
---|
3687 | }
|
---|
3688 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3689 |
|
---|
3690 | return rc;
|
---|
3691 | }
|
---|
3692 |
|
---|
3693 | HRESULT Medium::checkEncryptionPassword(const com::Utf8Str &aPassword)
|
---|
3694 | {
|
---|
3695 | HRESULT rc = S_OK;
|
---|
3696 |
|
---|
3697 | try
|
---|
3698 | {
|
---|
3699 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
3700 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3701 |
|
---|
3702 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
3703 | if (it == pBase->m->mapProperties.end())
|
---|
3704 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3705 | tr("The image is not configured for encryption"));
|
---|
3706 |
|
---|
3707 | if (aPassword.isEmpty())
|
---|
3708 | throw setError(E_INVALIDARG,
|
---|
3709 | tr("The given password must not be empty"));
|
---|
3710 |
|
---|
3711 | # ifdef VBOX_WITH_EXTPACK
|
---|
3712 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
3713 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
3714 | {
|
---|
3715 | /* Load the plugin */
|
---|
3716 | Utf8Str strPlugin;
|
---|
3717 | rc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
3718 | if (SUCCEEDED(rc))
|
---|
3719 | {
|
---|
3720 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
3721 | if (RT_FAILURE(vrc))
|
---|
3722 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3723 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
3724 | i_vdError(vrc).c_str());
|
---|
3725 | }
|
---|
3726 | else
|
---|
3727 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3728 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
3729 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
3730 | }
|
---|
3731 | else
|
---|
3732 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3733 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
3734 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
3735 |
|
---|
3736 | PVBOXHDD pDisk = NULL;
|
---|
3737 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
3738 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
3739 |
|
---|
3740 | Medium::CryptoFilterSettings CryptoSettings;
|
---|
3741 |
|
---|
3742 | i_taskEncryptSettingsSetup(&CryptoSettings, NULL, it->second.c_str(), aPassword.c_str(),
|
---|
3743 | false /* fCreateKeyStore */);
|
---|
3744 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ, CryptoSettings.vdFilterIfaces);
|
---|
3745 | if (vrc == VERR_VD_PASSWORD_INCORRECT)
|
---|
3746 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
3747 | tr("The given password is incorrect"));
|
---|
3748 | else if (RT_FAILURE(vrc))
|
---|
3749 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3750 | tr("Failed to load the encryption filter: %s"),
|
---|
3751 | i_vdError(vrc).c_str());
|
---|
3752 |
|
---|
3753 | VDDestroy(pDisk);
|
---|
3754 | # else
|
---|
3755 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3756 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
3757 | # endif
|
---|
3758 | }
|
---|
3759 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3760 |
|
---|
3761 | return rc;
|
---|
3762 | }
|
---|
3763 |
|
---|
3764 | ////////////////////////////////////////////////////////////////////////////////
|
---|
3765 | //
|
---|
3766 | // Medium public internal methods
|
---|
3767 | //
|
---|
3768 | ////////////////////////////////////////////////////////////////////////////////
|
---|
3769 |
|
---|
3770 | /**
|
---|
3771 | * Internal method to return the medium's parent medium. Must have caller + locking!
|
---|
3772 | * @return
|
---|
3773 | */
|
---|
3774 | const ComObjPtr<Medium>& Medium::i_getParent() const
|
---|
3775 | {
|
---|
3776 | return m->pParent;
|
---|
3777 | }
|
---|
3778 |
|
---|
3779 | /**
|
---|
3780 | * Internal method to return the medium's list of child media. Must have caller + locking!
|
---|
3781 | * @return
|
---|
3782 | */
|
---|
3783 | const MediaList& Medium::i_getChildren() const
|
---|
3784 | {
|
---|
3785 | return m->llChildren;
|
---|
3786 | }
|
---|
3787 |
|
---|
3788 | /**
|
---|
3789 | * Internal method to return the medium's GUID. Must have caller + locking!
|
---|
3790 | * @return
|
---|
3791 | */
|
---|
3792 | const Guid& Medium::i_getId() const
|
---|
3793 | {
|
---|
3794 | return m->id;
|
---|
3795 | }
|
---|
3796 |
|
---|
3797 | /**
|
---|
3798 | * Internal method to return the medium's state. Must have caller + locking!
|
---|
3799 | * @return
|
---|
3800 | */
|
---|
3801 | MediumState_T Medium::i_getState() const
|
---|
3802 | {
|
---|
3803 | return m->state;
|
---|
3804 | }
|
---|
3805 |
|
---|
3806 | /**
|
---|
3807 | * Internal method to return the medium's variant. Must have caller + locking!
|
---|
3808 | * @return
|
---|
3809 | */
|
---|
3810 | MediumVariant_T Medium::i_getVariant() const
|
---|
3811 | {
|
---|
3812 | return m->variant;
|
---|
3813 | }
|
---|
3814 |
|
---|
3815 | /**
|
---|
3816 | * Internal method which returns true if this medium represents a host drive.
|
---|
3817 | * @return
|
---|
3818 | */
|
---|
3819 | bool Medium::i_isHostDrive() const
|
---|
3820 | {
|
---|
3821 | return m->hostDrive;
|
---|
3822 | }
|
---|
3823 |
|
---|
3824 | /**
|
---|
3825 | * Internal method to return the medium's full location. Must have caller + locking!
|
---|
3826 | * @return
|
---|
3827 | */
|
---|
3828 | const Utf8Str& Medium::i_getLocationFull() const
|
---|
3829 | {
|
---|
3830 | return m->strLocationFull;
|
---|
3831 | }
|
---|
3832 |
|
---|
3833 | /**
|
---|
3834 | * Internal method to return the medium's format string. Must have caller + locking!
|
---|
3835 | * @return
|
---|
3836 | */
|
---|
3837 | const Utf8Str& Medium::i_getFormat() const
|
---|
3838 | {
|
---|
3839 | return m->strFormat;
|
---|
3840 | }
|
---|
3841 |
|
---|
3842 | /**
|
---|
3843 | * Internal method to return the medium's format object. Must have caller + locking!
|
---|
3844 | * @return
|
---|
3845 | */
|
---|
3846 | const ComObjPtr<MediumFormat>& Medium::i_getMediumFormat() const
|
---|
3847 | {
|
---|
3848 | return m->formatObj;
|
---|
3849 | }
|
---|
3850 |
|
---|
3851 | /**
|
---|
3852 | * Internal method that returns true if the medium is represented by a file on the host disk
|
---|
3853 | * (and not iSCSI or something).
|
---|
3854 | * @return
|
---|
3855 | */
|
---|
3856 | bool Medium::i_isMediumFormatFile() const
|
---|
3857 | {
|
---|
3858 | if ( m->formatObj
|
---|
3859 | && (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
3860 | )
|
---|
3861 | return true;
|
---|
3862 | return false;
|
---|
3863 | }
|
---|
3864 |
|
---|
3865 | /**
|
---|
3866 | * Internal method to return the medium's size. Must have caller + locking!
|
---|
3867 | * @return
|
---|
3868 | */
|
---|
3869 | uint64_t Medium::i_getSize() const
|
---|
3870 | {
|
---|
3871 | return m->size;
|
---|
3872 | }
|
---|
3873 |
|
---|
3874 | /**
|
---|
3875 | * Returns the medium device type. Must have caller + locking!
|
---|
3876 | * @return
|
---|
3877 | */
|
---|
3878 | DeviceType_T Medium::i_getDeviceType() const
|
---|
3879 | {
|
---|
3880 | return m->devType;
|
---|
3881 | }
|
---|
3882 |
|
---|
3883 | /**
|
---|
3884 | * Returns the medium type. Must have caller + locking!
|
---|
3885 | * @return
|
---|
3886 | */
|
---|
3887 | MediumType_T Medium::i_getType() const
|
---|
3888 | {
|
---|
3889 | return m->type;
|
---|
3890 | }
|
---|
3891 |
|
---|
3892 | /**
|
---|
3893 | * Returns a short version of the location attribute.
|
---|
3894 | *
|
---|
3895 | * @note Must be called from under this object's read or write lock.
|
---|
3896 | */
|
---|
3897 | Utf8Str Medium::i_getName()
|
---|
3898 | {
|
---|
3899 | Utf8Str name = RTPathFilename(m->strLocationFull.c_str());
|
---|
3900 | return name;
|
---|
3901 | }
|
---|
3902 |
|
---|
3903 | /**
|
---|
3904 | * This adds the given UUID to the list of media registries in which this
|
---|
3905 | * medium should be registered. The UUID can either be a machine UUID,
|
---|
3906 | * to add a machine registry, or the global registry UUID as returned by
|
---|
3907 | * VirtualBox::getGlobalRegistryId().
|
---|
3908 | *
|
---|
3909 | * Note that for hard disks, this method does nothing if the medium is
|
---|
3910 | * already in another registry to avoid having hard disks in more than
|
---|
3911 | * one registry, which causes trouble with keeping diff images in sync.
|
---|
3912 | * See getFirstRegistryMachineId() for details.
|
---|
3913 | *
|
---|
3914 | * @param id
|
---|
3915 | * @return true if the registry was added; false if the given id was already on the list.
|
---|
3916 | */
|
---|
3917 | bool Medium::i_addRegistry(const Guid& id)
|
---|
3918 | {
|
---|
3919 | AutoCaller autoCaller(this);
|
---|
3920 | if (FAILED(autoCaller.rc()))
|
---|
3921 | return false;
|
---|
3922 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3923 |
|
---|
3924 | bool fAdd = true;
|
---|
3925 |
|
---|
3926 | // hard disks cannot be in more than one registry
|
---|
3927 | if ( m->devType == DeviceType_HardDisk
|
---|
3928 | && m->llRegistryIDs.size() > 0)
|
---|
3929 | fAdd = false;
|
---|
3930 |
|
---|
3931 | // no need to add the UUID twice
|
---|
3932 | if (fAdd)
|
---|
3933 | {
|
---|
3934 | for (GuidList::const_iterator it = m->llRegistryIDs.begin();
|
---|
3935 | it != m->llRegistryIDs.end();
|
---|
3936 | ++it)
|
---|
3937 | {
|
---|
3938 | if ((*it) == id)
|
---|
3939 | {
|
---|
3940 | fAdd = false;
|
---|
3941 | break;
|
---|
3942 | }
|
---|
3943 | }
|
---|
3944 | }
|
---|
3945 |
|
---|
3946 | if (fAdd)
|
---|
3947 | m->llRegistryIDs.push_back(id);
|
---|
3948 |
|
---|
3949 | return fAdd;
|
---|
3950 | }
|
---|
3951 |
|
---|
3952 | /**
|
---|
3953 | * This adds the given UUID to the list of media registries in which this
|
---|
3954 | * medium should be registered. The UUID can either be a machine UUID,
|
---|
3955 | * to add a machine registry, or the global registry UUID as returned by
|
---|
3956 | * VirtualBox::getGlobalRegistryId(). This recurses over all children.
|
---|
3957 | *
|
---|
3958 | * Note that for hard disks, this method does nothing if the medium is
|
---|
3959 | * already in another registry to avoid having hard disks in more than
|
---|
3960 | * one registry, which causes trouble with keeping diff images in sync.
|
---|
3961 | * See getFirstRegistryMachineId() for details.
|
---|
3962 | *
|
---|
3963 | * @note the caller must hold the media tree lock for reading.
|
---|
3964 | *
|
---|
3965 | * @param id
|
---|
3966 | * @return true if the registry was added; false if the given id was already on the list.
|
---|
3967 | */
|
---|
3968 | bool Medium::i_addRegistryRecursive(const Guid &id)
|
---|
3969 | {
|
---|
3970 | AutoCaller autoCaller(this);
|
---|
3971 | if (FAILED(autoCaller.rc()))
|
---|
3972 | return false;
|
---|
3973 |
|
---|
3974 | bool fAdd = i_addRegistry(id);
|
---|
3975 |
|
---|
3976 | // protected by the medium tree lock held by our original caller
|
---|
3977 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
3978 | it != i_getChildren().end();
|
---|
3979 | ++it)
|
---|
3980 | {
|
---|
3981 | Medium *pChild = *it;
|
---|
3982 | fAdd |= pChild->i_addRegistryRecursive(id);
|
---|
3983 | }
|
---|
3984 |
|
---|
3985 | return fAdd;
|
---|
3986 | }
|
---|
3987 |
|
---|
3988 | /**
|
---|
3989 | * Removes the given UUID from the list of media registry UUIDs of this medium.
|
---|
3990 | *
|
---|
3991 | * @param id
|
---|
3992 | * @return true if the UUID was found or false if not.
|
---|
3993 | */
|
---|
3994 | bool Medium::i_removeRegistry(const Guid &id)
|
---|
3995 | {
|
---|
3996 | AutoCaller autoCaller(this);
|
---|
3997 | if (FAILED(autoCaller.rc()))
|
---|
3998 | return false;
|
---|
3999 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4000 |
|
---|
4001 | bool fRemove = false;
|
---|
4002 |
|
---|
4003 | /// @todo r=klaus eliminate this code, replace it by using find.
|
---|
4004 | for (GuidList::iterator it = m->llRegistryIDs.begin();
|
---|
4005 | it != m->llRegistryIDs.end();
|
---|
4006 | ++it)
|
---|
4007 | {
|
---|
4008 | if ((*it) == id)
|
---|
4009 | {
|
---|
4010 | // getting away with this as the iterator isn't used after
|
---|
4011 | m->llRegistryIDs.erase(it);
|
---|
4012 | fRemove = true;
|
---|
4013 | break;
|
---|
4014 | }
|
---|
4015 | }
|
---|
4016 |
|
---|
4017 | return fRemove;
|
---|
4018 | }
|
---|
4019 |
|
---|
4020 | /**
|
---|
4021 | * Removes the given UUID from the list of media registry UUIDs, for this
|
---|
4022 | * medium and all its children recursively.
|
---|
4023 | *
|
---|
4024 | * @note the caller must hold the media tree lock for reading.
|
---|
4025 | *
|
---|
4026 | * @param id
|
---|
4027 | * @return true if the UUID was found or false if not.
|
---|
4028 | */
|
---|
4029 | bool Medium::i_removeRegistryRecursive(const Guid &id)
|
---|
4030 | {
|
---|
4031 | AutoCaller autoCaller(this);
|
---|
4032 | if (FAILED(autoCaller.rc()))
|
---|
4033 | return false;
|
---|
4034 |
|
---|
4035 | bool fRemove = i_removeRegistry(id);
|
---|
4036 |
|
---|
4037 | // protected by the medium tree lock held by our original caller
|
---|
4038 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
4039 | it != i_getChildren().end();
|
---|
4040 | ++it)
|
---|
4041 | {
|
---|
4042 | Medium *pChild = *it;
|
---|
4043 | fRemove |= pChild->i_removeRegistryRecursive(id);
|
---|
4044 | }
|
---|
4045 |
|
---|
4046 | return fRemove;
|
---|
4047 | }
|
---|
4048 |
|
---|
4049 | /**
|
---|
4050 | * Returns true if id is in the list of media registries for this medium.
|
---|
4051 | *
|
---|
4052 | * Must have caller + read locking!
|
---|
4053 | *
|
---|
4054 | * @param id
|
---|
4055 | * @return
|
---|
4056 | */
|
---|
4057 | bool Medium::i_isInRegistry(const Guid &id)
|
---|
4058 | {
|
---|
4059 | /// @todo r=klaus eliminate this code, replace it by using find.
|
---|
4060 | for (GuidList::const_iterator it = m->llRegistryIDs.begin();
|
---|
4061 | it != m->llRegistryIDs.end();
|
---|
4062 | ++it)
|
---|
4063 | {
|
---|
4064 | if (*it == id)
|
---|
4065 | return true;
|
---|
4066 | }
|
---|
4067 |
|
---|
4068 | return false;
|
---|
4069 | }
|
---|
4070 |
|
---|
4071 | /**
|
---|
4072 | * Internal method to return the medium's first registry machine (i.e. the machine in whose
|
---|
4073 | * machine XML this medium is listed).
|
---|
4074 | *
|
---|
4075 | * Every attached medium must now (4.0) reside in at least one media registry, which is identified
|
---|
4076 | * by a UUID. This is either a machine UUID if the machine is from 4.0 or newer, in which case
|
---|
4077 | * machines have their own media registries, or it is the pseudo-UUID of the VirtualBox
|
---|
4078 | * object if the machine is old and still needs the global registry in VirtualBox.xml.
|
---|
4079 | *
|
---|
4080 | * By definition, hard disks may only be in one media registry, in which all its children
|
---|
4081 | * will be stored as well. Otherwise we run into problems with having keep multiple registries
|
---|
4082 | * in sync. (This is the "cloned VM" case in which VM1 may link to the disks of VM2; in this
|
---|
4083 | * case, only VM2's registry is used for the disk in question.)
|
---|
4084 | *
|
---|
4085 | * If there is no medium registry, particularly if the medium has not been attached yet, this
|
---|
4086 | * does not modify uuid and returns false.
|
---|
4087 | *
|
---|
4088 | * ISOs and RAWs, by contrast, can be in more than one repository to make things easier for
|
---|
4089 | * the user.
|
---|
4090 | *
|
---|
4091 | * Must have caller + locking!
|
---|
4092 | *
|
---|
4093 | * @param uuid Receives first registry machine UUID, if available.
|
---|
4094 | * @return true if uuid was set.
|
---|
4095 | */
|
---|
4096 | bool Medium::i_getFirstRegistryMachineId(Guid &uuid) const
|
---|
4097 | {
|
---|
4098 | if (m->llRegistryIDs.size())
|
---|
4099 | {
|
---|
4100 | uuid = m->llRegistryIDs.front();
|
---|
4101 | return true;
|
---|
4102 | }
|
---|
4103 | return false;
|
---|
4104 | }
|
---|
4105 |
|
---|
4106 | /**
|
---|
4107 | * Marks all the registries in which this medium is registered as modified.
|
---|
4108 | */
|
---|
4109 | void Medium::i_markRegistriesModified()
|
---|
4110 | {
|
---|
4111 | AutoCaller autoCaller(this);
|
---|
4112 | if (FAILED(autoCaller.rc())) return;
|
---|
4113 |
|
---|
4114 | // Get local copy, as keeping the lock over VirtualBox::markRegistryModified
|
---|
4115 | // causes trouble with the lock order
|
---|
4116 | GuidList llRegistryIDs;
|
---|
4117 | {
|
---|
4118 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4119 | llRegistryIDs = m->llRegistryIDs;
|
---|
4120 | }
|
---|
4121 |
|
---|
4122 | autoCaller.release();
|
---|
4123 |
|
---|
4124 | /* Save the error information now, the implicit restore when this goes
|
---|
4125 | * out of scope will throw away spurious additional errors created below. */
|
---|
4126 | ErrorInfoKeeper eik;
|
---|
4127 | for (GuidList::const_iterator it = llRegistryIDs.begin();
|
---|
4128 | it != llRegistryIDs.end();
|
---|
4129 | ++it)
|
---|
4130 | {
|
---|
4131 | m->pVirtualBox->i_markRegistryModified(*it);
|
---|
4132 | }
|
---|
4133 | }
|
---|
4134 |
|
---|
4135 | /**
|
---|
4136 | * Adds the given machine and optionally the snapshot to the list of the objects
|
---|
4137 | * this medium is attached to.
|
---|
4138 | *
|
---|
4139 | * @param aMachineId Machine ID.
|
---|
4140 | * @param aSnapshotId Snapshot ID; when non-empty, adds a snapshot attachment.
|
---|
4141 | */
|
---|
4142 | HRESULT Medium::i_addBackReference(const Guid &aMachineId,
|
---|
4143 | const Guid &aSnapshotId /*= Guid::Empty*/)
|
---|
4144 | {
|
---|
4145 | AssertReturn(aMachineId.isValid(), E_FAIL);
|
---|
4146 |
|
---|
4147 | LogFlowThisFunc(("ENTER, aMachineId: {%RTuuid}, aSnapshotId: {%RTuuid}\n", aMachineId.raw(), aSnapshotId.raw()));
|
---|
4148 |
|
---|
4149 | AutoCaller autoCaller(this);
|
---|
4150 | AssertComRCReturnRC(autoCaller.rc());
|
---|
4151 |
|
---|
4152 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4153 |
|
---|
4154 | switch (m->state)
|
---|
4155 | {
|
---|
4156 | case MediumState_Created:
|
---|
4157 | case MediumState_Inaccessible:
|
---|
4158 | case MediumState_LockedRead:
|
---|
4159 | case MediumState_LockedWrite:
|
---|
4160 | break;
|
---|
4161 |
|
---|
4162 | default:
|
---|
4163 | return i_setStateError();
|
---|
4164 | }
|
---|
4165 |
|
---|
4166 | if (m->numCreateDiffTasks > 0)
|
---|
4167 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4168 | tr("Cannot attach medium '%s' {%RTuuid}: %u differencing child media are being created"),
|
---|
4169 | m->strLocationFull.c_str(),
|
---|
4170 | m->id.raw(),
|
---|
4171 | m->numCreateDiffTasks);
|
---|
4172 |
|
---|
4173 | BackRefList::iterator it = std::find_if(m->backRefs.begin(),
|
---|
4174 | m->backRefs.end(),
|
---|
4175 | BackRef::EqualsTo(aMachineId));
|
---|
4176 | if (it == m->backRefs.end())
|
---|
4177 | {
|
---|
4178 | BackRef ref(aMachineId, aSnapshotId);
|
---|
4179 | m->backRefs.push_back(ref);
|
---|
4180 |
|
---|
4181 | return S_OK;
|
---|
4182 | }
|
---|
4183 |
|
---|
4184 | // if the caller has not supplied a snapshot ID, then we're attaching
|
---|
4185 | // to a machine a medium which represents the machine's current state,
|
---|
4186 | // so set the flag
|
---|
4187 |
|
---|
4188 | if (aSnapshotId.isZero())
|
---|
4189 | {
|
---|
4190 | /* sanity: no duplicate attachments */
|
---|
4191 | if (it->fInCurState)
|
---|
4192 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4193 | tr("Cannot attach medium '%s' {%RTuuid}: medium is already associated with the current state of machine uuid {%RTuuid}!"),
|
---|
4194 | m->strLocationFull.c_str(),
|
---|
4195 | m->id.raw(),
|
---|
4196 | aMachineId.raw());
|
---|
4197 | it->fInCurState = true;
|
---|
4198 |
|
---|
4199 | return S_OK;
|
---|
4200 | }
|
---|
4201 |
|
---|
4202 | // otherwise: a snapshot medium is being attached
|
---|
4203 |
|
---|
4204 | /* sanity: no duplicate attachments */
|
---|
4205 | for (GuidList::const_iterator jt = it->llSnapshotIds.begin();
|
---|
4206 | jt != it->llSnapshotIds.end();
|
---|
4207 | ++jt)
|
---|
4208 | {
|
---|
4209 | const Guid &idOldSnapshot = *jt;
|
---|
4210 |
|
---|
4211 | if (idOldSnapshot == aSnapshotId)
|
---|
4212 | {
|
---|
4213 | #ifdef DEBUG
|
---|
4214 | i_dumpBackRefs();
|
---|
4215 | #endif
|
---|
4216 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4217 | tr("Cannot attach medium '%s' {%RTuuid} from snapshot '%RTuuid': medium is already in use by this snapshot!"),
|
---|
4218 | m->strLocationFull.c_str(),
|
---|
4219 | m->id.raw(),
|
---|
4220 | aSnapshotId.raw());
|
---|
4221 | }
|
---|
4222 | }
|
---|
4223 |
|
---|
4224 | it->llSnapshotIds.push_back(aSnapshotId);
|
---|
4225 | // Do not touch fInCurState, as the image may be attached to the current
|
---|
4226 | // state *and* a snapshot, otherwise we lose the current state association!
|
---|
4227 |
|
---|
4228 | LogFlowThisFuncLeave();
|
---|
4229 |
|
---|
4230 | return S_OK;
|
---|
4231 | }
|
---|
4232 |
|
---|
4233 | /**
|
---|
4234 | * Removes the given machine and optionally the snapshot from the list of the
|
---|
4235 | * objects this medium is attached to.
|
---|
4236 | *
|
---|
4237 | * @param aMachineId Machine ID.
|
---|
4238 | * @param aSnapshotId Snapshot ID; when non-empty, removes the snapshot
|
---|
4239 | * attachment.
|
---|
4240 | */
|
---|
4241 | HRESULT Medium::i_removeBackReference(const Guid &aMachineId,
|
---|
4242 | const Guid &aSnapshotId /*= Guid::Empty*/)
|
---|
4243 | {
|
---|
4244 | AssertReturn(aMachineId.isValid(), E_FAIL);
|
---|
4245 |
|
---|
4246 | AutoCaller autoCaller(this);
|
---|
4247 | AssertComRCReturnRC(autoCaller.rc());
|
---|
4248 |
|
---|
4249 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4250 |
|
---|
4251 | BackRefList::iterator it =
|
---|
4252 | std::find_if(m->backRefs.begin(), m->backRefs.end(),
|
---|
4253 | BackRef::EqualsTo(aMachineId));
|
---|
4254 | AssertReturn(it != m->backRefs.end(), E_FAIL);
|
---|
4255 |
|
---|
4256 | if (aSnapshotId.isZero())
|
---|
4257 | {
|
---|
4258 | /* remove the current state attachment */
|
---|
4259 | it->fInCurState = false;
|
---|
4260 | }
|
---|
4261 | else
|
---|
4262 | {
|
---|
4263 | /* remove the snapshot attachment */
|
---|
4264 | GuidList::iterator jt = std::find(it->llSnapshotIds.begin(),
|
---|
4265 | it->llSnapshotIds.end(),
|
---|
4266 | aSnapshotId);
|
---|
4267 |
|
---|
4268 | AssertReturn(jt != it->llSnapshotIds.end(), E_FAIL);
|
---|
4269 | it->llSnapshotIds.erase(jt);
|
---|
4270 | }
|
---|
4271 |
|
---|
4272 | /* if the backref becomes empty, remove it */
|
---|
4273 | if (it->fInCurState == false && it->llSnapshotIds.size() == 0)
|
---|
4274 | m->backRefs.erase(it);
|
---|
4275 |
|
---|
4276 | return S_OK;
|
---|
4277 | }
|
---|
4278 |
|
---|
4279 | /**
|
---|
4280 | * Internal method to return the medium's list of backrefs. Must have caller + locking!
|
---|
4281 | * @return
|
---|
4282 | */
|
---|
4283 | const Guid* Medium::i_getFirstMachineBackrefId() const
|
---|
4284 | {
|
---|
4285 | if (!m->backRefs.size())
|
---|
4286 | return NULL;
|
---|
4287 |
|
---|
4288 | return &m->backRefs.front().machineId;
|
---|
4289 | }
|
---|
4290 |
|
---|
4291 | /**
|
---|
4292 | * Internal method which returns a machine that either this medium or one of its children
|
---|
4293 | * is attached to. This is used for finding a replacement media registry when an existing
|
---|
4294 | * media registry is about to be deleted in VirtualBox::unregisterMachine().
|
---|
4295 | *
|
---|
4296 | * Must have caller + locking, *and* caller must hold the media tree lock!
|
---|
4297 | * @return
|
---|
4298 | */
|
---|
4299 | const Guid* Medium::i_getAnyMachineBackref() const
|
---|
4300 | {
|
---|
4301 | if (m->backRefs.size())
|
---|
4302 | return &m->backRefs.front().machineId;
|
---|
4303 |
|
---|
4304 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
4305 | it != i_getChildren().end();
|
---|
4306 | ++it)
|
---|
4307 | {
|
---|
4308 | Medium *pChild = *it;
|
---|
4309 | // recurse for this child
|
---|
4310 | const Guid* puuid;
|
---|
4311 | if ((puuid = pChild->i_getAnyMachineBackref()))
|
---|
4312 | return puuid;
|
---|
4313 | }
|
---|
4314 |
|
---|
4315 | return NULL;
|
---|
4316 | }
|
---|
4317 |
|
---|
4318 | const Guid* Medium::i_getFirstMachineBackrefSnapshotId() const
|
---|
4319 | {
|
---|
4320 | if (!m->backRefs.size())
|
---|
4321 | return NULL;
|
---|
4322 |
|
---|
4323 | const BackRef &ref = m->backRefs.front();
|
---|
4324 | if (ref.llSnapshotIds.empty())
|
---|
4325 | return NULL;
|
---|
4326 |
|
---|
4327 | return &ref.llSnapshotIds.front();
|
---|
4328 | }
|
---|
4329 |
|
---|
4330 | size_t Medium::i_getMachineBackRefCount() const
|
---|
4331 | {
|
---|
4332 | return m->backRefs.size();
|
---|
4333 | }
|
---|
4334 |
|
---|
4335 | #ifdef DEBUG
|
---|
4336 | /**
|
---|
4337 | * Debugging helper that gets called after VirtualBox initialization that writes all
|
---|
4338 | * machine backreferences to the debug log.
|
---|
4339 | */
|
---|
4340 | void Medium::i_dumpBackRefs()
|
---|
4341 | {
|
---|
4342 | AutoCaller autoCaller(this);
|
---|
4343 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4344 |
|
---|
4345 | LogFlowThisFunc(("Dumping backrefs for medium '%s':\n", m->strLocationFull.c_str()));
|
---|
4346 |
|
---|
4347 | for (BackRefList::iterator it2 = m->backRefs.begin();
|
---|
4348 | it2 != m->backRefs.end();
|
---|
4349 | ++it2)
|
---|
4350 | {
|
---|
4351 | const BackRef &ref = *it2;
|
---|
4352 | LogFlowThisFunc((" Backref from machine {%RTuuid} (fInCurState: %d)\n", ref.machineId.raw(), ref.fInCurState));
|
---|
4353 |
|
---|
4354 | for (GuidList::const_iterator jt2 = it2->llSnapshotIds.begin();
|
---|
4355 | jt2 != it2->llSnapshotIds.end();
|
---|
4356 | ++jt2)
|
---|
4357 | {
|
---|
4358 | const Guid &id = *jt2;
|
---|
4359 | LogFlowThisFunc((" Backref from snapshot {%RTuuid}\n", id.raw()));
|
---|
4360 | }
|
---|
4361 | }
|
---|
4362 | }
|
---|
4363 | #endif
|
---|
4364 |
|
---|
4365 | /**
|
---|
4366 | * Checks if the given change of \a aOldPath to \a aNewPath affects the location
|
---|
4367 | * of this media and updates it if necessary to reflect the new location.
|
---|
4368 | *
|
---|
4369 | * @param aOldPath Old path (full).
|
---|
4370 | * @param aNewPath New path (full).
|
---|
4371 | *
|
---|
4372 | * @note Locks this object for writing.
|
---|
4373 | */
|
---|
4374 | HRESULT Medium::i_updatePath(const Utf8Str &strOldPath, const Utf8Str &strNewPath)
|
---|
4375 | {
|
---|
4376 | AssertReturn(!strOldPath.isEmpty(), E_FAIL);
|
---|
4377 | AssertReturn(!strNewPath.isEmpty(), E_FAIL);
|
---|
4378 |
|
---|
4379 | AutoCaller autoCaller(this);
|
---|
4380 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4381 |
|
---|
4382 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4383 |
|
---|
4384 | LogFlowThisFunc(("locationFull.before='%s'\n", m->strLocationFull.c_str()));
|
---|
4385 |
|
---|
4386 | const char *pcszMediumPath = m->strLocationFull.c_str();
|
---|
4387 |
|
---|
4388 | if (RTPathStartsWith(pcszMediumPath, strOldPath.c_str()))
|
---|
4389 | {
|
---|
4390 | Utf8Str newPath(strNewPath);
|
---|
4391 | newPath.append(pcszMediumPath + strOldPath.length());
|
---|
4392 | unconst(m->strLocationFull) = newPath;
|
---|
4393 |
|
---|
4394 | LogFlowThisFunc(("locationFull.after='%s'\n", m->strLocationFull.c_str()));
|
---|
4395 | // we changed something
|
---|
4396 | return S_OK;
|
---|
4397 | }
|
---|
4398 |
|
---|
4399 | // no change was necessary, signal error which the caller needs to interpret
|
---|
4400 | return VBOX_E_FILE_ERROR;
|
---|
4401 | }
|
---|
4402 |
|
---|
4403 | /**
|
---|
4404 | * Returns the base medium of the media chain this medium is part of.
|
---|
4405 | *
|
---|
4406 | * The base medium is found by walking up the parent-child relationship axis.
|
---|
4407 | * If the medium doesn't have a parent (i.e. it's a base medium), it
|
---|
4408 | * returns itself in response to this method.
|
---|
4409 | *
|
---|
4410 | * @param aLevel Where to store the number of ancestors of this medium
|
---|
4411 | * (zero for the base), may be @c NULL.
|
---|
4412 | *
|
---|
4413 | * @note Locks medium tree for reading.
|
---|
4414 | */
|
---|
4415 | ComObjPtr<Medium> Medium::i_getBase(uint32_t *aLevel /*= NULL*/)
|
---|
4416 | {
|
---|
4417 | ComObjPtr<Medium> pBase;
|
---|
4418 |
|
---|
4419 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
4420 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
4421 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
4422 | if (!pVirtualBox)
|
---|
4423 | return pBase;
|
---|
4424 |
|
---|
4425 | /* we access m->pParent */
|
---|
4426 | AutoReadLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4427 |
|
---|
4428 | AutoCaller autoCaller(this);
|
---|
4429 | AssertReturn(autoCaller.isOk(), pBase);
|
---|
4430 |
|
---|
4431 | pBase = this;
|
---|
4432 | uint32_t level = 0;
|
---|
4433 |
|
---|
4434 | if (m->pParent)
|
---|
4435 | {
|
---|
4436 | for (;;)
|
---|
4437 | {
|
---|
4438 | AutoCaller baseCaller(pBase);
|
---|
4439 | AssertReturn(baseCaller.isOk(), pBase);
|
---|
4440 |
|
---|
4441 | if (pBase->m->pParent.isNull())
|
---|
4442 | break;
|
---|
4443 |
|
---|
4444 | pBase = pBase->m->pParent;
|
---|
4445 | ++level;
|
---|
4446 | }
|
---|
4447 | }
|
---|
4448 |
|
---|
4449 | if (aLevel != NULL)
|
---|
4450 | *aLevel = level;
|
---|
4451 |
|
---|
4452 | return pBase;
|
---|
4453 | }
|
---|
4454 |
|
---|
4455 | /**
|
---|
4456 | * Returns the depth of this medium in the media chain.
|
---|
4457 | *
|
---|
4458 | * @note Locks medium tree for reading.
|
---|
4459 | */
|
---|
4460 | uint32_t Medium::i_getDepth()
|
---|
4461 | {
|
---|
4462 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
4463 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
4464 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
4465 | if (!pVirtualBox)
|
---|
4466 | return 1;
|
---|
4467 |
|
---|
4468 | /* we access m->pParent */
|
---|
4469 | AutoReadLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4470 |
|
---|
4471 | uint32_t cDepth = 0;
|
---|
4472 | ComObjPtr<Medium> pMedium(this);
|
---|
4473 | while (!pMedium.isNull())
|
---|
4474 | {
|
---|
4475 | AutoCaller autoCaller(this);
|
---|
4476 | AssertReturn(autoCaller.isOk(), cDepth + 1);
|
---|
4477 |
|
---|
4478 | pMedium = pMedium->m->pParent;
|
---|
4479 | cDepth++;
|
---|
4480 | }
|
---|
4481 |
|
---|
4482 | return cDepth;
|
---|
4483 | }
|
---|
4484 |
|
---|
4485 | /**
|
---|
4486 | * Returns @c true if this medium cannot be modified because it has
|
---|
4487 | * dependents (children) or is part of the snapshot. Related to the medium
|
---|
4488 | * type and posterity, not to the current media state.
|
---|
4489 | *
|
---|
4490 | * @note Locks this object and medium tree for reading.
|
---|
4491 | */
|
---|
4492 | bool Medium::i_isReadOnly()
|
---|
4493 | {
|
---|
4494 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
4495 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
4496 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
4497 | if (!pVirtualBox)
|
---|
4498 | return false;
|
---|
4499 |
|
---|
4500 | /* we access children */
|
---|
4501 | AutoReadLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4502 |
|
---|
4503 | AutoCaller autoCaller(this);
|
---|
4504 | AssertComRCReturn(autoCaller.rc(), false);
|
---|
4505 |
|
---|
4506 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4507 |
|
---|
4508 | switch (m->type)
|
---|
4509 | {
|
---|
4510 | case MediumType_Normal:
|
---|
4511 | {
|
---|
4512 | if (i_getChildren().size() != 0)
|
---|
4513 | return true;
|
---|
4514 |
|
---|
4515 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
4516 | it != m->backRefs.end(); ++it)
|
---|
4517 | if (it->llSnapshotIds.size() != 0)
|
---|
4518 | return true;
|
---|
4519 |
|
---|
4520 | if (m->variant & MediumVariant_VmdkStreamOptimized)
|
---|
4521 | return true;
|
---|
4522 |
|
---|
4523 | return false;
|
---|
4524 | }
|
---|
4525 | case MediumType_Immutable:
|
---|
4526 | case MediumType_MultiAttach:
|
---|
4527 | return true;
|
---|
4528 | case MediumType_Writethrough:
|
---|
4529 | case MediumType_Shareable:
|
---|
4530 | case MediumType_Readonly: /* explicit readonly media has no diffs */
|
---|
4531 | return false;
|
---|
4532 | default:
|
---|
4533 | break;
|
---|
4534 | }
|
---|
4535 |
|
---|
4536 | AssertFailedReturn(false);
|
---|
4537 | }
|
---|
4538 |
|
---|
4539 | /**
|
---|
4540 | * Internal method to return the medium's size. Must have caller + locking!
|
---|
4541 | * @return
|
---|
4542 | */
|
---|
4543 | void Medium::i_updateId(const Guid &id)
|
---|
4544 | {
|
---|
4545 | unconst(m->id) = id;
|
---|
4546 | }
|
---|
4547 |
|
---|
4548 | /**
|
---|
4549 | * Saves the settings of one medium.
|
---|
4550 | *
|
---|
4551 | * @note Caller MUST take care of the medium tree lock and caller.
|
---|
4552 | *
|
---|
4553 | * @param data Settings struct to be updated.
|
---|
4554 | * @param strHardDiskFolder Folder for which paths should be relative.
|
---|
4555 | */
|
---|
4556 | void Medium::i_saveSettingsOne(settings::Medium &data, const Utf8Str &strHardDiskFolder)
|
---|
4557 | {
|
---|
4558 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4559 |
|
---|
4560 | data.uuid = m->id;
|
---|
4561 |
|
---|
4562 | // make path relative if needed
|
---|
4563 | if ( !strHardDiskFolder.isEmpty()
|
---|
4564 | && RTPathStartsWith(m->strLocationFull.c_str(), strHardDiskFolder.c_str())
|
---|
4565 | )
|
---|
4566 | data.strLocation = m->strLocationFull.substr(strHardDiskFolder.length() + 1);
|
---|
4567 | else
|
---|
4568 | data.strLocation = m->strLocationFull;
|
---|
4569 | data.strFormat = m->strFormat;
|
---|
4570 |
|
---|
4571 | /* optional, only for diffs, default is false */
|
---|
4572 | if (m->pParent)
|
---|
4573 | data.fAutoReset = m->autoReset;
|
---|
4574 | else
|
---|
4575 | data.fAutoReset = false;
|
---|
4576 |
|
---|
4577 | /* optional */
|
---|
4578 | data.strDescription = m->strDescription;
|
---|
4579 |
|
---|
4580 | /* optional properties */
|
---|
4581 | data.properties.clear();
|
---|
4582 |
|
---|
4583 | /* handle iSCSI initiator secrets transparently */
|
---|
4584 | bool fHaveInitiatorSecretEncrypted = false;
|
---|
4585 | Utf8Str strCiphertext;
|
---|
4586 | settings::StringsMap::const_iterator itPln = m->mapProperties.find("InitiatorSecret");
|
---|
4587 | if ( itPln != m->mapProperties.end()
|
---|
4588 | && !itPln->second.isEmpty())
|
---|
4589 | {
|
---|
4590 | /* Encrypt the plain secret. If that does not work (i.e. no or wrong settings key
|
---|
4591 | * specified), just use the encrypted secret (if there is any). */
|
---|
4592 | int rc = m->pVirtualBox->i_encryptSetting(itPln->second, &strCiphertext);
|
---|
4593 | if (RT_SUCCESS(rc))
|
---|
4594 | fHaveInitiatorSecretEncrypted = true;
|
---|
4595 | }
|
---|
4596 | for (settings::StringsMap::const_iterator it = m->mapProperties.begin();
|
---|
4597 | it != m->mapProperties.end();
|
---|
4598 | ++it)
|
---|
4599 | {
|
---|
4600 | /* only save properties that have non-default values */
|
---|
4601 | if (!it->second.isEmpty())
|
---|
4602 | {
|
---|
4603 | const Utf8Str &name = it->first;
|
---|
4604 | const Utf8Str &value = it->second;
|
---|
4605 | /* do NOT store the plain InitiatorSecret */
|
---|
4606 | if ( !fHaveInitiatorSecretEncrypted
|
---|
4607 | || !name.equals("InitiatorSecret"))
|
---|
4608 | data.properties[name] = value;
|
---|
4609 | }
|
---|
4610 | }
|
---|
4611 | if (fHaveInitiatorSecretEncrypted)
|
---|
4612 | data.properties["InitiatorSecretEncrypted"] = strCiphertext;
|
---|
4613 |
|
---|
4614 | /* only for base media */
|
---|
4615 | if (m->pParent.isNull())
|
---|
4616 | data.hdType = m->type;
|
---|
4617 | }
|
---|
4618 |
|
---|
4619 | /**
|
---|
4620 | * Saves medium data by putting it into the provided data structure.
|
---|
4621 | * Recurses over all children to save their settings, too.
|
---|
4622 | *
|
---|
4623 | * @param data Settings struct to be updated.
|
---|
4624 | * @param strHardDiskFolder Folder for which paths should be relative.
|
---|
4625 | *
|
---|
4626 | * @note Locks this object, medium tree and children for reading.
|
---|
4627 | */
|
---|
4628 | HRESULT Medium::i_saveSettings(settings::Medium &data,
|
---|
4629 | const Utf8Str &strHardDiskFolder)
|
---|
4630 | {
|
---|
4631 | /* we access m->pParent */
|
---|
4632 | AutoReadLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4633 |
|
---|
4634 | AutoCaller autoCaller(this);
|
---|
4635 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4636 |
|
---|
4637 | i_saveSettingsOne(data, strHardDiskFolder);
|
---|
4638 |
|
---|
4639 | /* save all children */
|
---|
4640 | settings::MediaList &llSettingsChildren = data.llChildren;
|
---|
4641 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
4642 | it != i_getChildren().end();
|
---|
4643 | ++it)
|
---|
4644 | {
|
---|
4645 | // Use the element straight in the list to reduce both unnecessary
|
---|
4646 | // deep copying (when unwinding the recursion the entire medium
|
---|
4647 | // settings sub-tree is copied) and the stack footprint (the settings
|
---|
4648 | // need almost 1K, and there can be VMs with long image chains.
|
---|
4649 | llSettingsChildren.push_back(settings::Medium::Empty);
|
---|
4650 | HRESULT rc = (*it)->i_saveSettings(llSettingsChildren.back(), strHardDiskFolder);
|
---|
4651 | if (FAILED(rc))
|
---|
4652 | {
|
---|
4653 | llSettingsChildren.pop_back();
|
---|
4654 | return rc;
|
---|
4655 | }
|
---|
4656 | }
|
---|
4657 |
|
---|
4658 | return S_OK;
|
---|
4659 | }
|
---|
4660 |
|
---|
4661 | /**
|
---|
4662 | * Constructs a medium lock list for this medium. The lock is not taken.
|
---|
4663 | *
|
---|
4664 | * @note Caller MUST NOT hold the media tree or medium lock.
|
---|
4665 | *
|
---|
4666 | * @param fFailIfInaccessible If true, this fails with an error if a medium is inaccessible. If false,
|
---|
4667 | * inaccessible media are silently skipped and not locked (i.e. their state remains "Inaccessible");
|
---|
4668 | * this is necessary for a VM's removable media VM startup for which we do not want to fail.
|
---|
4669 | * @param pToLockWrite If not NULL, associate a write lock with this medium object.
|
---|
4670 | * @param fMediumLockWriteAll Whether to associate a write lock to all other media too.
|
---|
4671 | * @param pToBeParent Medium which will become the parent of this medium.
|
---|
4672 | * @param mediumLockList Where to store the resulting list.
|
---|
4673 | */
|
---|
4674 | HRESULT Medium::i_createMediumLockList(bool fFailIfInaccessible,
|
---|
4675 | Medium *pToLockWrite,
|
---|
4676 | bool fMediumLockWriteAll,
|
---|
4677 | Medium *pToBeParent,
|
---|
4678 | MediumLockList &mediumLockList)
|
---|
4679 | {
|
---|
4680 | /** @todo r=klaus this needs to be reworked, as the code below uses
|
---|
4681 | * i_getParent without holding the tree lock, and changing this is
|
---|
4682 | * a significant amount of effort. */
|
---|
4683 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
4684 | Assert(!isWriteLockOnCurrentThread());
|
---|
4685 |
|
---|
4686 | AutoCaller autoCaller(this);
|
---|
4687 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4688 |
|
---|
4689 | HRESULT rc = S_OK;
|
---|
4690 |
|
---|
4691 | /* paranoid sanity checking if the medium has a to-be parent medium */
|
---|
4692 | if (pToBeParent)
|
---|
4693 | {
|
---|
4694 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4695 | ComAssertRet(i_getParent().isNull(), E_FAIL);
|
---|
4696 | ComAssertRet(i_getChildren().size() == 0, E_FAIL);
|
---|
4697 | }
|
---|
4698 |
|
---|
4699 | ErrorInfoKeeper eik;
|
---|
4700 | MultiResult mrc(S_OK);
|
---|
4701 |
|
---|
4702 | ComObjPtr<Medium> pMedium = this;
|
---|
4703 | while (!pMedium.isNull())
|
---|
4704 | {
|
---|
4705 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
4706 |
|
---|
4707 | /* Accessibility check must be first, otherwise locking interferes
|
---|
4708 | * with getting the medium state. Lock lists are not created for
|
---|
4709 | * fun, and thus getting the medium status is no luxury. */
|
---|
4710 | MediumState_T mediumState = pMedium->i_getState();
|
---|
4711 | if (mediumState == MediumState_Inaccessible)
|
---|
4712 | {
|
---|
4713 | alock.release();
|
---|
4714 | rc = pMedium->i_queryInfo(false /* fSetImageId */, false /* fSetParentId */,
|
---|
4715 | autoCaller);
|
---|
4716 | alock.acquire();
|
---|
4717 | if (FAILED(rc)) return rc;
|
---|
4718 |
|
---|
4719 | mediumState = pMedium->i_getState();
|
---|
4720 | if (mediumState == MediumState_Inaccessible)
|
---|
4721 | {
|
---|
4722 | // ignore inaccessible ISO media and silently return S_OK,
|
---|
4723 | // otherwise VM startup (esp. restore) may fail without good reason
|
---|
4724 | if (!fFailIfInaccessible)
|
---|
4725 | return S_OK;
|
---|
4726 |
|
---|
4727 | // otherwise report an error
|
---|
4728 | Bstr error;
|
---|
4729 | rc = pMedium->COMGETTER(LastAccessError)(error.asOutParam());
|
---|
4730 | if (FAILED(rc)) return rc;
|
---|
4731 |
|
---|
4732 | /* collect multiple errors */
|
---|
4733 | eik.restore();
|
---|
4734 | Assert(!error.isEmpty());
|
---|
4735 | mrc = setError(E_FAIL,
|
---|
4736 | "%ls",
|
---|
4737 | error.raw());
|
---|
4738 | // error message will be something like
|
---|
4739 | // "Could not open the medium ... VD: error VERR_FILE_NOT_FOUND opening image file ... (VERR_FILE_NOT_FOUND).
|
---|
4740 | eik.fetch();
|
---|
4741 | }
|
---|
4742 | }
|
---|
4743 |
|
---|
4744 | if (pMedium == pToLockWrite)
|
---|
4745 | mediumLockList.Prepend(pMedium, true);
|
---|
4746 | else
|
---|
4747 | mediumLockList.Prepend(pMedium, fMediumLockWriteAll);
|
---|
4748 |
|
---|
4749 | pMedium = pMedium->i_getParent();
|
---|
4750 | if (pMedium.isNull() && pToBeParent)
|
---|
4751 | {
|
---|
4752 | pMedium = pToBeParent;
|
---|
4753 | pToBeParent = NULL;
|
---|
4754 | }
|
---|
4755 | }
|
---|
4756 |
|
---|
4757 | return mrc;
|
---|
4758 | }
|
---|
4759 |
|
---|
4760 | /**
|
---|
4761 | * Creates a new differencing storage unit using the format of the given target
|
---|
4762 | * medium and the location. Note that @c aTarget must be NotCreated.
|
---|
4763 | *
|
---|
4764 | * The @a aMediumLockList parameter contains the associated medium lock list,
|
---|
4765 | * which must be in locked state. If @a aWait is @c true then the caller is
|
---|
4766 | * responsible for unlocking.
|
---|
4767 | *
|
---|
4768 | * If @a aProgress is not NULL but the object it points to is @c null then a
|
---|
4769 | * new progress object will be created and assigned to @a *aProgress on
|
---|
4770 | * success, otherwise the existing progress object is used. If @a aProgress is
|
---|
4771 | * NULL, then no progress object is created/used at all.
|
---|
4772 | *
|
---|
4773 | * When @a aWait is @c false, this method will create a thread to perform the
|
---|
4774 | * create operation asynchronously and will return immediately. Otherwise, it
|
---|
4775 | * will perform the operation on the calling thread and will not return to the
|
---|
4776 | * caller until the operation is completed. Note that @a aProgress cannot be
|
---|
4777 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
4778 | *
|
---|
4779 | * @param aTarget Target medium.
|
---|
4780 | * @param aVariant Precise medium variant to create.
|
---|
4781 | * @param aMediumLockList List of media which should be locked.
|
---|
4782 | * @param aProgress Where to find/store a Progress object to track
|
---|
4783 | * operation completion.
|
---|
4784 | * @param aWait @c true if this method should block instead of
|
---|
4785 | * creating an asynchronous thread.
|
---|
4786 | *
|
---|
4787 | * @note Locks this object and @a aTarget for writing.
|
---|
4788 | */
|
---|
4789 | HRESULT Medium::i_createDiffStorage(ComObjPtr<Medium> &aTarget,
|
---|
4790 | MediumVariant_T aVariant,
|
---|
4791 | MediumLockList *aMediumLockList,
|
---|
4792 | ComObjPtr<Progress> *aProgress,
|
---|
4793 | bool aWait)
|
---|
4794 | {
|
---|
4795 | AssertReturn(!aTarget.isNull(), E_FAIL);
|
---|
4796 | AssertReturn(aMediumLockList, E_FAIL);
|
---|
4797 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
4798 |
|
---|
4799 | AutoCaller autoCaller(this);
|
---|
4800 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4801 |
|
---|
4802 | AutoCaller targetCaller(aTarget);
|
---|
4803 | if (FAILED(targetCaller.rc())) return targetCaller.rc();
|
---|
4804 |
|
---|
4805 | HRESULT rc = S_OK;
|
---|
4806 | ComObjPtr<Progress> pProgress;
|
---|
4807 | Medium::Task *pTask = NULL;
|
---|
4808 |
|
---|
4809 | try
|
---|
4810 | {
|
---|
4811 | AutoMultiWriteLock2 alock(this, aTarget COMMA_LOCKVAL_SRC_POS);
|
---|
4812 |
|
---|
4813 | ComAssertThrow( m->type != MediumType_Writethrough
|
---|
4814 | && m->type != MediumType_Shareable
|
---|
4815 | && m->type != MediumType_Readonly, E_FAIL);
|
---|
4816 | ComAssertThrow(m->state == MediumState_LockedRead, E_FAIL);
|
---|
4817 |
|
---|
4818 | if (aTarget->m->state != MediumState_NotCreated)
|
---|
4819 | throw aTarget->i_setStateError();
|
---|
4820 |
|
---|
4821 | /* Check that the medium is not attached to the current state of
|
---|
4822 | * any VM referring to it. */
|
---|
4823 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
4824 | it != m->backRefs.end();
|
---|
4825 | ++it)
|
---|
4826 | {
|
---|
4827 | if (it->fInCurState)
|
---|
4828 | {
|
---|
4829 | /* Note: when a VM snapshot is being taken, all normal media
|
---|
4830 | * attached to the VM in the current state will be, as an
|
---|
4831 | * exception, also associated with the snapshot which is about
|
---|
4832 | * to create (see SnapshotMachine::init()) before deassociating
|
---|
4833 | * them from the current state (which takes place only on
|
---|
4834 | * success in Machine::fixupHardDisks()), so that the size of
|
---|
4835 | * snapshotIds will be 1 in this case. The extra condition is
|
---|
4836 | * used to filter out this legal situation. */
|
---|
4837 | if (it->llSnapshotIds.size() == 0)
|
---|
4838 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4839 | 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"),
|
---|
4840 | m->strLocationFull.c_str(), it->machineId.raw());
|
---|
4841 |
|
---|
4842 | Assert(it->llSnapshotIds.size() == 1);
|
---|
4843 | }
|
---|
4844 | }
|
---|
4845 |
|
---|
4846 | if (aProgress != NULL)
|
---|
4847 | {
|
---|
4848 | /* use the existing progress object... */
|
---|
4849 | pProgress = *aProgress;
|
---|
4850 |
|
---|
4851 | /* ...but create a new one if it is null */
|
---|
4852 | if (pProgress.isNull())
|
---|
4853 | {
|
---|
4854 | pProgress.createObject();
|
---|
4855 | rc = pProgress->init(m->pVirtualBox,
|
---|
4856 | static_cast<IMedium*>(this),
|
---|
4857 | BstrFmt(tr("Creating differencing medium storage unit '%s'"),
|
---|
4858 | aTarget->m->strLocationFull.c_str()).raw(),
|
---|
4859 | TRUE /* aCancelable */);
|
---|
4860 | if (FAILED(rc))
|
---|
4861 | throw rc;
|
---|
4862 | }
|
---|
4863 | }
|
---|
4864 |
|
---|
4865 | /* setup task object to carry out the operation sync/async */
|
---|
4866 | pTask = new Medium::CreateDiffTask(this, pProgress, aTarget, aVariant,
|
---|
4867 | aMediumLockList,
|
---|
4868 | aWait /* fKeepMediumLockList */);
|
---|
4869 | rc = pTask->rc();
|
---|
4870 | AssertComRC(rc);
|
---|
4871 | if (FAILED(rc))
|
---|
4872 | throw rc;
|
---|
4873 |
|
---|
4874 | /* register a task (it will deregister itself when done) */
|
---|
4875 | ++m->numCreateDiffTasks;
|
---|
4876 | Assert(m->numCreateDiffTasks != 0); /* overflow? */
|
---|
4877 |
|
---|
4878 | aTarget->m->state = MediumState_Creating;
|
---|
4879 | }
|
---|
4880 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4881 |
|
---|
4882 | if (SUCCEEDED(rc))
|
---|
4883 | {
|
---|
4884 | if (aWait)
|
---|
4885 | {
|
---|
4886 | rc = pTask->runNow();
|
---|
4887 |
|
---|
4888 | delete pTask;
|
---|
4889 | }
|
---|
4890 | else
|
---|
4891 | rc = pTask->createThread();
|
---|
4892 |
|
---|
4893 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
4894 | *aProgress = pProgress;
|
---|
4895 | }
|
---|
4896 | else if (pTask != NULL)
|
---|
4897 | delete pTask;
|
---|
4898 |
|
---|
4899 | return rc;
|
---|
4900 | }
|
---|
4901 |
|
---|
4902 | /**
|
---|
4903 | * Returns a preferred format for differencing media.
|
---|
4904 | */
|
---|
4905 | Utf8Str Medium::i_getPreferredDiffFormat()
|
---|
4906 | {
|
---|
4907 | AutoCaller autoCaller(this);
|
---|
4908 | AssertComRCReturn(autoCaller.rc(), Utf8Str::Empty);
|
---|
4909 |
|
---|
4910 | /* check that our own format supports diffs */
|
---|
4911 | if (!(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_Differencing))
|
---|
4912 | {
|
---|
4913 | /* use the default format if not */
|
---|
4914 | Utf8Str tmp;
|
---|
4915 | m->pVirtualBox->i_getDefaultHardDiskFormat(tmp);
|
---|
4916 | return tmp;
|
---|
4917 | }
|
---|
4918 |
|
---|
4919 | /* m->strFormat is const, no need to lock */
|
---|
4920 | return m->strFormat;
|
---|
4921 | }
|
---|
4922 |
|
---|
4923 | /**
|
---|
4924 | * Returns a preferred variant for differencing media.
|
---|
4925 | */
|
---|
4926 | MediumVariant_T Medium::i_getPreferredDiffVariant()
|
---|
4927 | {
|
---|
4928 | AutoCaller autoCaller(this);
|
---|
4929 | AssertComRCReturn(autoCaller.rc(), MediumVariant_Standard);
|
---|
4930 |
|
---|
4931 | /* check that our own format supports diffs */
|
---|
4932 | if (!(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_Differencing))
|
---|
4933 | return MediumVariant_Standard;
|
---|
4934 |
|
---|
4935 | /* m->variant is const, no need to lock */
|
---|
4936 | ULONG mediumVariantFlags = (ULONG)m->variant;
|
---|
4937 | mediumVariantFlags &= ~(MediumVariant_Fixed | MediumVariant_VmdkStreamOptimized);
|
---|
4938 | mediumVariantFlags |= MediumVariant_Diff;
|
---|
4939 | return (MediumVariant_T)mediumVariantFlags;
|
---|
4940 | }
|
---|
4941 |
|
---|
4942 | /**
|
---|
4943 | * Implementation for the public Medium::Close() with the exception of calling
|
---|
4944 | * VirtualBox::saveRegistries(), in case someone wants to call this for several
|
---|
4945 | * media.
|
---|
4946 | *
|
---|
4947 | * After this returns with success, uninit() has been called on the medium, and
|
---|
4948 | * the object is no longer usable ("not ready" state).
|
---|
4949 | *
|
---|
4950 | * @param autoCaller AutoCaller instance which must have been created on the caller's
|
---|
4951 | * stack for this medium. This gets released hereupon
|
---|
4952 | * which the Medium instance gets uninitialized.
|
---|
4953 | * @return
|
---|
4954 | */
|
---|
4955 | HRESULT Medium::i_close(AutoCaller &autoCaller)
|
---|
4956 | {
|
---|
4957 | // must temporarily drop the caller, need the tree lock first
|
---|
4958 | autoCaller.release();
|
---|
4959 |
|
---|
4960 | // we're accessing parent/child and backrefs, so lock the tree first, then ourselves
|
---|
4961 | AutoMultiWriteLock2 multilock(&m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
4962 | this->lockHandle()
|
---|
4963 | COMMA_LOCKVAL_SRC_POS);
|
---|
4964 |
|
---|
4965 | autoCaller.add();
|
---|
4966 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4967 |
|
---|
4968 | LogFlowFunc(("ENTER for %s\n", i_getLocationFull().c_str()));
|
---|
4969 |
|
---|
4970 | bool wasCreated = true;
|
---|
4971 |
|
---|
4972 | switch (m->state)
|
---|
4973 | {
|
---|
4974 | case MediumState_NotCreated:
|
---|
4975 | wasCreated = false;
|
---|
4976 | break;
|
---|
4977 | case MediumState_Created:
|
---|
4978 | case MediumState_Inaccessible:
|
---|
4979 | break;
|
---|
4980 | default:
|
---|
4981 | return i_setStateError();
|
---|
4982 | }
|
---|
4983 |
|
---|
4984 | if (m->backRefs.size() != 0)
|
---|
4985 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4986 | tr("Medium '%s' cannot be closed because it is still attached to %d virtual machines"),
|
---|
4987 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
4988 |
|
---|
4989 | // perform extra media-dependent close checks
|
---|
4990 | HRESULT rc = i_canClose();
|
---|
4991 | if (FAILED(rc)) return rc;
|
---|
4992 |
|
---|
4993 | m->fClosing = true;
|
---|
4994 |
|
---|
4995 | if (wasCreated)
|
---|
4996 | {
|
---|
4997 | // remove from the list of known media before performing actual
|
---|
4998 | // uninitialization (to keep the media registry consistent on
|
---|
4999 | // failure to do so)
|
---|
5000 | rc = i_unregisterWithVirtualBox();
|
---|
5001 | if (FAILED(rc)) return rc;
|
---|
5002 |
|
---|
5003 | multilock.release();
|
---|
5004 | // Release the AutoCaller now, as otherwise uninit() will simply hang.
|
---|
5005 | // Needs to be done before mark the registries as modified and saving
|
---|
5006 | // the registry, as otherwise there may be a deadlock with someone else
|
---|
5007 | // closing this object while we're in i_saveModifiedRegistries(), which
|
---|
5008 | // needs the media tree lock, which the other thread holds until after
|
---|
5009 | // uninit() below.
|
---|
5010 | autoCaller.release();
|
---|
5011 | i_markRegistriesModified();
|
---|
5012 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
5013 | }
|
---|
5014 | else
|
---|
5015 | {
|
---|
5016 | multilock.release();
|
---|
5017 | // release the AutoCaller, as otherwise uninit() will simply hang
|
---|
5018 | autoCaller.release();
|
---|
5019 | }
|
---|
5020 |
|
---|
5021 | // Keep the locks held until after uninit, as otherwise the consistency
|
---|
5022 | // of the medium tree cannot be guaranteed.
|
---|
5023 | uninit();
|
---|
5024 |
|
---|
5025 | LogFlowFuncLeave();
|
---|
5026 |
|
---|
5027 | return rc;
|
---|
5028 | }
|
---|
5029 |
|
---|
5030 | /**
|
---|
5031 | * Deletes the medium storage unit.
|
---|
5032 | *
|
---|
5033 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
5034 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
5035 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
5036 | * progress object is created/used at all.
|
---|
5037 | *
|
---|
5038 | * When @a aWait is @c false, this method will create a thread to perform the
|
---|
5039 | * delete operation asynchronously and will return immediately. Otherwise, it
|
---|
5040 | * will perform the operation on the calling thread and will not return to the
|
---|
5041 | * caller until the operation is completed. Note that @a aProgress cannot be
|
---|
5042 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
5043 | *
|
---|
5044 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
5045 | * completion.
|
---|
5046 | * @param aWait @c true if this method should block instead of creating
|
---|
5047 | * an asynchronous thread.
|
---|
5048 | *
|
---|
5049 | * @note Locks mVirtualBox and this object for writing. Locks medium tree for
|
---|
5050 | * writing.
|
---|
5051 | */
|
---|
5052 | HRESULT Medium::i_deleteStorage(ComObjPtr<Progress> *aProgress,
|
---|
5053 | bool aWait)
|
---|
5054 | {
|
---|
5055 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
5056 | * to lock order violations, it probably causes lock order issues related
|
---|
5057 | * to the AutoCaller usage. */
|
---|
5058 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
5059 |
|
---|
5060 | AutoCaller autoCaller(this);
|
---|
5061 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
5062 |
|
---|
5063 | HRESULT rc = S_OK;
|
---|
5064 | ComObjPtr<Progress> pProgress;
|
---|
5065 | Medium::Task *pTask = NULL;
|
---|
5066 |
|
---|
5067 | try
|
---|
5068 | {
|
---|
5069 | /* we're accessing the media tree, and canClose() needs it too */
|
---|
5070 | AutoMultiWriteLock2 multilock(&m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
5071 | this->lockHandle()
|
---|
5072 | COMMA_LOCKVAL_SRC_POS);
|
---|
5073 | LogFlowThisFunc(("aWait=%RTbool locationFull=%s\n", aWait, i_getLocationFull().c_str() ));
|
---|
5074 |
|
---|
5075 | if ( !(m->formatObj->i_getCapabilities() & ( MediumFormatCapabilities_CreateDynamic
|
---|
5076 | | MediumFormatCapabilities_CreateFixed)))
|
---|
5077 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
5078 | tr("Medium format '%s' does not support storage deletion"),
|
---|
5079 | m->strFormat.c_str());
|
---|
5080 |
|
---|
5081 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
5082 | /** @todo r=klaus would be great if this could be moved to the async
|
---|
5083 | * part of the operation as it can take quite a while */
|
---|
5084 | if (m->queryInfoRunning)
|
---|
5085 | {
|
---|
5086 | while (m->queryInfoRunning)
|
---|
5087 | {
|
---|
5088 | multilock.release();
|
---|
5089 | /* Must not hold the media tree lock or the object lock, as
|
---|
5090 | * Medium::i_queryInfo needs this lock and thus we would run
|
---|
5091 | * into a deadlock here. */
|
---|
5092 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
5093 | Assert(!isWriteLockOnCurrentThread());
|
---|
5094 | {
|
---|
5095 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
5096 | }
|
---|
5097 | multilock.acquire();
|
---|
5098 | }
|
---|
5099 | }
|
---|
5100 |
|
---|
5101 | /* Note that we are fine with Inaccessible state too: a) for symmetry
|
---|
5102 | * with create calls and b) because it doesn't really harm to try, if
|
---|
5103 | * it is really inaccessible, the delete operation will fail anyway.
|
---|
5104 | * Accepting Inaccessible state is especially important because all
|
---|
5105 | * registered media are initially Inaccessible upon VBoxSVC startup
|
---|
5106 | * until COMGETTER(RefreshState) is called. Accept Deleting state
|
---|
5107 | * because some callers need to put the medium in this state early
|
---|
5108 | * to prevent races. */
|
---|
5109 | switch (m->state)
|
---|
5110 | {
|
---|
5111 | case MediumState_Created:
|
---|
5112 | case MediumState_Deleting:
|
---|
5113 | case MediumState_Inaccessible:
|
---|
5114 | break;
|
---|
5115 | default:
|
---|
5116 | throw i_setStateError();
|
---|
5117 | }
|
---|
5118 |
|
---|
5119 | if (m->backRefs.size() != 0)
|
---|
5120 | {
|
---|
5121 | Utf8Str strMachines;
|
---|
5122 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
5123 | it != m->backRefs.end();
|
---|
5124 | ++it)
|
---|
5125 | {
|
---|
5126 | const BackRef &b = *it;
|
---|
5127 | if (strMachines.length())
|
---|
5128 | strMachines.append(", ");
|
---|
5129 | strMachines.append(b.machineId.toString().c_str());
|
---|
5130 | }
|
---|
5131 | #ifdef DEBUG
|
---|
5132 | i_dumpBackRefs();
|
---|
5133 | #endif
|
---|
5134 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5135 | tr("Cannot delete storage: medium '%s' is still attached to the following %d virtual machine(s): %s"),
|
---|
5136 | m->strLocationFull.c_str(),
|
---|
5137 | m->backRefs.size(),
|
---|
5138 | strMachines.c_str());
|
---|
5139 | }
|
---|
5140 |
|
---|
5141 | rc = i_canClose();
|
---|
5142 | if (FAILED(rc))
|
---|
5143 | throw rc;
|
---|
5144 |
|
---|
5145 | /* go to Deleting state, so that the medium is not actually locked */
|
---|
5146 | if (m->state != MediumState_Deleting)
|
---|
5147 | {
|
---|
5148 | rc = i_markForDeletion();
|
---|
5149 | if (FAILED(rc))
|
---|
5150 | throw rc;
|
---|
5151 | }
|
---|
5152 |
|
---|
5153 | /* Build the medium lock list. */
|
---|
5154 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
5155 | multilock.release();
|
---|
5156 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5157 | this /* pToLockWrite */,
|
---|
5158 | false /* fMediumLockWriteAll */,
|
---|
5159 | NULL,
|
---|
5160 | *pMediumLockList);
|
---|
5161 | multilock.acquire();
|
---|
5162 | if (FAILED(rc))
|
---|
5163 | {
|
---|
5164 | delete pMediumLockList;
|
---|
5165 | throw rc;
|
---|
5166 | }
|
---|
5167 |
|
---|
5168 | multilock.release();
|
---|
5169 | rc = pMediumLockList->Lock();
|
---|
5170 | multilock.acquire();
|
---|
5171 | if (FAILED(rc))
|
---|
5172 | {
|
---|
5173 | delete pMediumLockList;
|
---|
5174 | throw setError(rc,
|
---|
5175 | tr("Failed to lock media when deleting '%s'"),
|
---|
5176 | i_getLocationFull().c_str());
|
---|
5177 | }
|
---|
5178 |
|
---|
5179 | /* try to remove from the list of known media before performing
|
---|
5180 | * actual deletion (we favor the consistency of the media registry
|
---|
5181 | * which would have been broken if unregisterWithVirtualBox() failed
|
---|
5182 | * after we successfully deleted the storage) */
|
---|
5183 | rc = i_unregisterWithVirtualBox();
|
---|
5184 | if (FAILED(rc))
|
---|
5185 | throw rc;
|
---|
5186 | // no longer need lock
|
---|
5187 | multilock.release();
|
---|
5188 | i_markRegistriesModified();
|
---|
5189 |
|
---|
5190 | if (aProgress != NULL)
|
---|
5191 | {
|
---|
5192 | /* use the existing progress object... */
|
---|
5193 | pProgress = *aProgress;
|
---|
5194 |
|
---|
5195 | /* ...but create a new one if it is null */
|
---|
5196 | if (pProgress.isNull())
|
---|
5197 | {
|
---|
5198 | pProgress.createObject();
|
---|
5199 | rc = pProgress->init(m->pVirtualBox,
|
---|
5200 | static_cast<IMedium*>(this),
|
---|
5201 | BstrFmt(tr("Deleting medium storage unit '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
5202 | FALSE /* aCancelable */);
|
---|
5203 | if (FAILED(rc))
|
---|
5204 | throw rc;
|
---|
5205 | }
|
---|
5206 | }
|
---|
5207 |
|
---|
5208 | /* setup task object to carry out the operation sync/async */
|
---|
5209 | pTask = new Medium::DeleteTask(this, pProgress, pMediumLockList);
|
---|
5210 | rc = pTask->rc();
|
---|
5211 | AssertComRC(rc);
|
---|
5212 | if (FAILED(rc))
|
---|
5213 | throw rc;
|
---|
5214 | }
|
---|
5215 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5216 |
|
---|
5217 | if (SUCCEEDED(rc))
|
---|
5218 | {
|
---|
5219 | if (aWait)
|
---|
5220 | {
|
---|
5221 | rc = pTask->runNow();
|
---|
5222 |
|
---|
5223 | delete pTask;
|
---|
5224 | }
|
---|
5225 | else
|
---|
5226 | rc = pTask->createThread();
|
---|
5227 |
|
---|
5228 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
5229 | *aProgress = pProgress;
|
---|
5230 |
|
---|
5231 | }
|
---|
5232 | else
|
---|
5233 | {
|
---|
5234 | if (pTask)
|
---|
5235 | delete pTask;
|
---|
5236 |
|
---|
5237 | /* Undo deleting state if necessary. */
|
---|
5238 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5239 | /* Make sure that any error signalled by unmarkForDeletion() is not
|
---|
5240 | * ending up in the error list (if the caller uses MultiResult). It
|
---|
5241 | * usually is spurious, as in most cases the medium hasn't been marked
|
---|
5242 | * for deletion when the error was thrown above. */
|
---|
5243 | ErrorInfoKeeper eik;
|
---|
5244 | i_unmarkForDeletion();
|
---|
5245 | }
|
---|
5246 |
|
---|
5247 | return rc;
|
---|
5248 | }
|
---|
5249 |
|
---|
5250 | /**
|
---|
5251 | * Mark a medium for deletion.
|
---|
5252 | *
|
---|
5253 | * @note Caller must hold the write lock on this medium!
|
---|
5254 | */
|
---|
5255 | HRESULT Medium::i_markForDeletion()
|
---|
5256 | {
|
---|
5257 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5258 | switch (m->state)
|
---|
5259 | {
|
---|
5260 | case MediumState_Created:
|
---|
5261 | case MediumState_Inaccessible:
|
---|
5262 | m->preLockState = m->state;
|
---|
5263 | m->state = MediumState_Deleting;
|
---|
5264 | return S_OK;
|
---|
5265 | default:
|
---|
5266 | return i_setStateError();
|
---|
5267 | }
|
---|
5268 | }
|
---|
5269 |
|
---|
5270 | /**
|
---|
5271 | * Removes the "mark for deletion".
|
---|
5272 | *
|
---|
5273 | * @note Caller must hold the write lock on this medium!
|
---|
5274 | */
|
---|
5275 | HRESULT Medium::i_unmarkForDeletion()
|
---|
5276 | {
|
---|
5277 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5278 | switch (m->state)
|
---|
5279 | {
|
---|
5280 | case MediumState_Deleting:
|
---|
5281 | m->state = m->preLockState;
|
---|
5282 | return S_OK;
|
---|
5283 | default:
|
---|
5284 | return i_setStateError();
|
---|
5285 | }
|
---|
5286 | }
|
---|
5287 |
|
---|
5288 | /**
|
---|
5289 | * Mark a medium for deletion which is in locked state.
|
---|
5290 | *
|
---|
5291 | * @note Caller must hold the write lock on this medium!
|
---|
5292 | */
|
---|
5293 | HRESULT Medium::i_markLockedForDeletion()
|
---|
5294 | {
|
---|
5295 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5296 | if ( ( m->state == MediumState_LockedRead
|
---|
5297 | || m->state == MediumState_LockedWrite)
|
---|
5298 | && m->preLockState == MediumState_Created)
|
---|
5299 | {
|
---|
5300 | m->preLockState = MediumState_Deleting;
|
---|
5301 | return S_OK;
|
---|
5302 | }
|
---|
5303 | else
|
---|
5304 | return i_setStateError();
|
---|
5305 | }
|
---|
5306 |
|
---|
5307 | /**
|
---|
5308 | * Removes the "mark for deletion" for a medium in locked state.
|
---|
5309 | *
|
---|
5310 | * @note Caller must hold the write lock on this medium!
|
---|
5311 | */
|
---|
5312 | HRESULT Medium::i_unmarkLockedForDeletion()
|
---|
5313 | {
|
---|
5314 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5315 | if ( ( m->state == MediumState_LockedRead
|
---|
5316 | || m->state == MediumState_LockedWrite)
|
---|
5317 | && m->preLockState == MediumState_Deleting)
|
---|
5318 | {
|
---|
5319 | m->preLockState = MediumState_Created;
|
---|
5320 | return S_OK;
|
---|
5321 | }
|
---|
5322 | else
|
---|
5323 | return i_setStateError();
|
---|
5324 | }
|
---|
5325 |
|
---|
5326 | /**
|
---|
5327 | * Queries the preferred merge direction from this to the other medium, i.e.
|
---|
5328 | * the one which requires the least amount of I/O and therefore time and
|
---|
5329 | * disk consumption.
|
---|
5330 | *
|
---|
5331 | * @returns Status code.
|
---|
5332 | * @retval E_FAIL in case determining the merge direction fails for some reason,
|
---|
5333 | * for example if getting the size of the media fails. There is no
|
---|
5334 | * error set though and the caller is free to continue to find out
|
---|
5335 | * what was going wrong later. Leaves fMergeForward unset.
|
---|
5336 | * @retval VBOX_E_INVALID_OBJECT_STATE if both media are not related to each other
|
---|
5337 | * An error is set.
|
---|
5338 | * @param pOther The other medium to merge with.
|
---|
5339 | * @param fMergeForward Resulting preferred merge direction (out).
|
---|
5340 | */
|
---|
5341 | HRESULT Medium::i_queryPreferredMergeDirection(const ComObjPtr<Medium> &pOther,
|
---|
5342 | bool &fMergeForward)
|
---|
5343 | {
|
---|
5344 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
5345 | * to lock order violations, it probably causes lock order issues related
|
---|
5346 | * to the AutoCaller usage. Likewise the code using this method seems
|
---|
5347 | * problematic. */
|
---|
5348 | AssertReturn(pOther != NULL, E_FAIL);
|
---|
5349 | AssertReturn(pOther != this, E_FAIL);
|
---|
5350 |
|
---|
5351 | AutoCaller autoCaller(this);
|
---|
5352 | AssertComRCReturnRC(autoCaller.rc());
|
---|
5353 |
|
---|
5354 | AutoCaller otherCaller(pOther);
|
---|
5355 | AssertComRCReturnRC(otherCaller.rc());
|
---|
5356 |
|
---|
5357 | HRESULT rc = S_OK;
|
---|
5358 | bool fThisParent = false; /**<< Flag whether this medium is the parent of pOther. */
|
---|
5359 |
|
---|
5360 | try
|
---|
5361 | {
|
---|
5362 | // locking: we need the tree lock first because we access parent pointers
|
---|
5363 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5364 |
|
---|
5365 | /* more sanity checking and figuring out the current merge direction */
|
---|
5366 | ComObjPtr<Medium> pMedium = i_getParent();
|
---|
5367 | while (!pMedium.isNull() && pMedium != pOther)
|
---|
5368 | pMedium = pMedium->i_getParent();
|
---|
5369 | if (pMedium == pOther)
|
---|
5370 | fThisParent = false;
|
---|
5371 | else
|
---|
5372 | {
|
---|
5373 | pMedium = pOther->i_getParent();
|
---|
5374 | while (!pMedium.isNull() && pMedium != this)
|
---|
5375 | pMedium = pMedium->i_getParent();
|
---|
5376 | if (pMedium == this)
|
---|
5377 | fThisParent = true;
|
---|
5378 | else
|
---|
5379 | {
|
---|
5380 | Utf8Str tgtLoc;
|
---|
5381 | {
|
---|
5382 | AutoReadLock alock(pOther COMMA_LOCKVAL_SRC_POS);
|
---|
5383 | tgtLoc = pOther->i_getLocationFull();
|
---|
5384 | }
|
---|
5385 |
|
---|
5386 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5387 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5388 | tr("Media '%s' and '%s' are unrelated"),
|
---|
5389 | m->strLocationFull.c_str(), tgtLoc.c_str());
|
---|
5390 | }
|
---|
5391 | }
|
---|
5392 |
|
---|
5393 | /*
|
---|
5394 | * Figure out the preferred merge direction. The current way is to
|
---|
5395 | * get the current sizes of file based images and select the merge
|
---|
5396 | * direction depending on the size.
|
---|
5397 | *
|
---|
5398 | * Can't use the VD API to get current size here as the media might
|
---|
5399 | * be write locked by a running VM. Resort to RTFileQuerySize().
|
---|
5400 | */
|
---|
5401 | int vrc = VINF_SUCCESS;
|
---|
5402 | uint64_t cbMediumThis = 0;
|
---|
5403 | uint64_t cbMediumOther = 0;
|
---|
5404 |
|
---|
5405 | if (i_isMediumFormatFile() && pOther->i_isMediumFormatFile())
|
---|
5406 | {
|
---|
5407 | vrc = RTFileQuerySize(this->i_getLocationFull().c_str(), &cbMediumThis);
|
---|
5408 | if (RT_SUCCESS(vrc))
|
---|
5409 | {
|
---|
5410 | vrc = RTFileQuerySize(pOther->i_getLocationFull().c_str(),
|
---|
5411 | &cbMediumOther);
|
---|
5412 | }
|
---|
5413 |
|
---|
5414 | if (RT_FAILURE(vrc))
|
---|
5415 | rc = E_FAIL;
|
---|
5416 | else
|
---|
5417 | {
|
---|
5418 | /*
|
---|
5419 | * Check which merge direction might be more optimal.
|
---|
5420 | * This method is not bullet proof of course as there might
|
---|
5421 | * be overlapping blocks in the images so the file size is
|
---|
5422 | * not the best indicator but it is good enough for our purpose
|
---|
5423 | * and everything else is too complicated, especially when the
|
---|
5424 | * media are used by a running VM.
|
---|
5425 | */
|
---|
5426 | bool fMergeIntoThis = cbMediumThis > cbMediumOther;
|
---|
5427 | fMergeForward = fMergeIntoThis != fThisParent;
|
---|
5428 | }
|
---|
5429 | }
|
---|
5430 | }
|
---|
5431 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5432 |
|
---|
5433 | return rc;
|
---|
5434 | }
|
---|
5435 |
|
---|
5436 | /**
|
---|
5437 | * Prepares this (source) medium, target medium and all intermediate media
|
---|
5438 | * for the merge operation.
|
---|
5439 | *
|
---|
5440 | * This method is to be called prior to calling the #mergeTo() to perform
|
---|
5441 | * necessary consistency checks and place involved media to appropriate
|
---|
5442 | * states. If #mergeTo() is not called or fails, the state modifications
|
---|
5443 | * performed by this method must be undone by #cancelMergeTo().
|
---|
5444 | *
|
---|
5445 | * See #mergeTo() for more information about merging.
|
---|
5446 | *
|
---|
5447 | * @param pTarget Target medium.
|
---|
5448 | * @param aMachineId Allowed machine attachment. NULL means do not check.
|
---|
5449 | * @param aSnapshotId Allowed snapshot attachment. NULL or empty UUID means
|
---|
5450 | * do not check.
|
---|
5451 | * @param fLockMedia Flag whether to lock the medium lock list or not.
|
---|
5452 | * If set to false and the medium lock list locking fails
|
---|
5453 | * later you must call #cancelMergeTo().
|
---|
5454 | * @param fMergeForward Resulting merge direction (out).
|
---|
5455 | * @param pParentForTarget New parent for target medium after merge (out).
|
---|
5456 | * @param aChildrenToReparent Medium lock list containing all children of the
|
---|
5457 | * source which will have to be reparented to the target
|
---|
5458 | * after merge (out).
|
---|
5459 | * @param aMediumLockList Medium locking information (out).
|
---|
5460 | *
|
---|
5461 | * @note Locks medium tree for reading. Locks this object, aTarget and all
|
---|
5462 | * intermediate media for writing.
|
---|
5463 | */
|
---|
5464 | HRESULT Medium::i_prepareMergeTo(const ComObjPtr<Medium> &pTarget,
|
---|
5465 | const Guid *aMachineId,
|
---|
5466 | const Guid *aSnapshotId,
|
---|
5467 | bool fLockMedia,
|
---|
5468 | bool &fMergeForward,
|
---|
5469 | ComObjPtr<Medium> &pParentForTarget,
|
---|
5470 | MediumLockList * &aChildrenToReparent,
|
---|
5471 | MediumLockList * &aMediumLockList)
|
---|
5472 | {
|
---|
5473 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
5474 | * to lock order violations, it probably causes lock order issues related
|
---|
5475 | * to the AutoCaller usage. Likewise the code using this method seems
|
---|
5476 | * problematic. */
|
---|
5477 | AssertReturn(pTarget != NULL, E_FAIL);
|
---|
5478 | AssertReturn(pTarget != this, E_FAIL);
|
---|
5479 |
|
---|
5480 | AutoCaller autoCaller(this);
|
---|
5481 | AssertComRCReturnRC(autoCaller.rc());
|
---|
5482 |
|
---|
5483 | AutoCaller targetCaller(pTarget);
|
---|
5484 | AssertComRCReturnRC(targetCaller.rc());
|
---|
5485 |
|
---|
5486 | HRESULT rc = S_OK;
|
---|
5487 | fMergeForward = false;
|
---|
5488 | pParentForTarget.setNull();
|
---|
5489 | Assert(aChildrenToReparent == NULL);
|
---|
5490 | aChildrenToReparent = NULL;
|
---|
5491 | Assert(aMediumLockList == NULL);
|
---|
5492 | aMediumLockList = NULL;
|
---|
5493 |
|
---|
5494 | try
|
---|
5495 | {
|
---|
5496 | // locking: we need the tree lock first because we access parent pointers
|
---|
5497 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5498 |
|
---|
5499 | /* more sanity checking and figuring out the merge direction */
|
---|
5500 | ComObjPtr<Medium> pMedium = i_getParent();
|
---|
5501 | while (!pMedium.isNull() && pMedium != pTarget)
|
---|
5502 | pMedium = pMedium->i_getParent();
|
---|
5503 | if (pMedium == pTarget)
|
---|
5504 | fMergeForward = false;
|
---|
5505 | else
|
---|
5506 | {
|
---|
5507 | pMedium = pTarget->i_getParent();
|
---|
5508 | while (!pMedium.isNull() && pMedium != this)
|
---|
5509 | pMedium = pMedium->i_getParent();
|
---|
5510 | if (pMedium == this)
|
---|
5511 | fMergeForward = true;
|
---|
5512 | else
|
---|
5513 | {
|
---|
5514 | Utf8Str tgtLoc;
|
---|
5515 | {
|
---|
5516 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5517 | tgtLoc = pTarget->i_getLocationFull();
|
---|
5518 | }
|
---|
5519 |
|
---|
5520 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5521 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5522 | tr("Media '%s' and '%s' are unrelated"),
|
---|
5523 | m->strLocationFull.c_str(), tgtLoc.c_str());
|
---|
5524 | }
|
---|
5525 | }
|
---|
5526 |
|
---|
5527 | /* Build the lock list. */
|
---|
5528 | aMediumLockList = new MediumLockList();
|
---|
5529 | treeLock.release();
|
---|
5530 | if (fMergeForward)
|
---|
5531 | rc = pTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5532 | pTarget /* pToLockWrite */,
|
---|
5533 | false /* fMediumLockWriteAll */,
|
---|
5534 | NULL,
|
---|
5535 | *aMediumLockList);
|
---|
5536 | else
|
---|
5537 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5538 | pTarget /* pToLockWrite */,
|
---|
5539 | false /* fMediumLockWriteAll */,
|
---|
5540 | NULL,
|
---|
5541 | *aMediumLockList);
|
---|
5542 | treeLock.acquire();
|
---|
5543 | if (FAILED(rc))
|
---|
5544 | throw rc;
|
---|
5545 |
|
---|
5546 | /* Sanity checking, must be after lock list creation as it depends on
|
---|
5547 | * valid medium states. The medium objects must be accessible. Only
|
---|
5548 | * do this if immediate locking is requested, otherwise it fails when
|
---|
5549 | * we construct a medium lock list for an already running VM. Snapshot
|
---|
5550 | * deletion uses this to simplify its life. */
|
---|
5551 | if (fLockMedia)
|
---|
5552 | {
|
---|
5553 | {
|
---|
5554 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5555 | if (m->state != MediumState_Created)
|
---|
5556 | throw i_setStateError();
|
---|
5557 | }
|
---|
5558 | {
|
---|
5559 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5560 | if (pTarget->m->state != MediumState_Created)
|
---|
5561 | throw pTarget->i_setStateError();
|
---|
5562 | }
|
---|
5563 | }
|
---|
5564 |
|
---|
5565 | /* check medium attachment and other sanity conditions */
|
---|
5566 | if (fMergeForward)
|
---|
5567 | {
|
---|
5568 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5569 | if (i_getChildren().size() > 1)
|
---|
5570 | {
|
---|
5571 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5572 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
5573 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
5574 | }
|
---|
5575 | /* One backreference is only allowed if the machine ID is not empty
|
---|
5576 | * and it matches the machine the medium is attached to (including
|
---|
5577 | * the snapshot ID if not empty). */
|
---|
5578 | if ( m->backRefs.size() != 0
|
---|
5579 | && ( !aMachineId
|
---|
5580 | || m->backRefs.size() != 1
|
---|
5581 | || aMachineId->isZero()
|
---|
5582 | || *i_getFirstMachineBackrefId() != *aMachineId
|
---|
5583 | || ( (!aSnapshotId || !aSnapshotId->isZero())
|
---|
5584 | && *i_getFirstMachineBackrefSnapshotId() != *aSnapshotId)))
|
---|
5585 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5586 | tr("Medium '%s' is attached to %d virtual machines"),
|
---|
5587 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
5588 | if (m->type == MediumType_Immutable)
|
---|
5589 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5590 | tr("Medium '%s' is immutable"),
|
---|
5591 | m->strLocationFull.c_str());
|
---|
5592 | if (m->type == MediumType_MultiAttach)
|
---|
5593 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5594 | tr("Medium '%s' is multi-attach"),
|
---|
5595 | m->strLocationFull.c_str());
|
---|
5596 | }
|
---|
5597 | else
|
---|
5598 | {
|
---|
5599 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5600 | if (pTarget->i_getChildren().size() > 1)
|
---|
5601 | {
|
---|
5602 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5603 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
5604 | pTarget->m->strLocationFull.c_str(),
|
---|
5605 | pTarget->i_getChildren().size());
|
---|
5606 | }
|
---|
5607 | if (pTarget->m->type == MediumType_Immutable)
|
---|
5608 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5609 | tr("Medium '%s' is immutable"),
|
---|
5610 | pTarget->m->strLocationFull.c_str());
|
---|
5611 | if (pTarget->m->type == MediumType_MultiAttach)
|
---|
5612 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5613 | tr("Medium '%s' is multi-attach"),
|
---|
5614 | pTarget->m->strLocationFull.c_str());
|
---|
5615 | }
|
---|
5616 | ComObjPtr<Medium> pLast(fMergeForward ? (Medium *)pTarget : this);
|
---|
5617 | ComObjPtr<Medium> pLastIntermediate = pLast->i_getParent();
|
---|
5618 | for (pLast = pLastIntermediate;
|
---|
5619 | !pLast.isNull() && pLast != pTarget && pLast != this;
|
---|
5620 | pLast = pLast->i_getParent())
|
---|
5621 | {
|
---|
5622 | AutoReadLock alock(pLast COMMA_LOCKVAL_SRC_POS);
|
---|
5623 | if (pLast->i_getChildren().size() > 1)
|
---|
5624 | {
|
---|
5625 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5626 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
5627 | pLast->m->strLocationFull.c_str(),
|
---|
5628 | pLast->i_getChildren().size());
|
---|
5629 | }
|
---|
5630 | if (pLast->m->backRefs.size() != 0)
|
---|
5631 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5632 | tr("Medium '%s' is attached to %d virtual machines"),
|
---|
5633 | pLast->m->strLocationFull.c_str(),
|
---|
5634 | pLast->m->backRefs.size());
|
---|
5635 |
|
---|
5636 | }
|
---|
5637 |
|
---|
5638 | /* Update medium states appropriately */
|
---|
5639 | {
|
---|
5640 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5641 |
|
---|
5642 | if (m->state == MediumState_Created)
|
---|
5643 | {
|
---|
5644 | rc = i_markForDeletion();
|
---|
5645 | if (FAILED(rc))
|
---|
5646 | throw rc;
|
---|
5647 | }
|
---|
5648 | else
|
---|
5649 | {
|
---|
5650 | if (fLockMedia)
|
---|
5651 | throw i_setStateError();
|
---|
5652 | else if ( m->state == MediumState_LockedWrite
|
---|
5653 | || m->state == MediumState_LockedRead)
|
---|
5654 | {
|
---|
5655 | /* Either mark it for deletion in locked state or allow
|
---|
5656 | * others to have done so. */
|
---|
5657 | if (m->preLockState == MediumState_Created)
|
---|
5658 | i_markLockedForDeletion();
|
---|
5659 | else if (m->preLockState != MediumState_Deleting)
|
---|
5660 | throw i_setStateError();
|
---|
5661 | }
|
---|
5662 | else
|
---|
5663 | throw i_setStateError();
|
---|
5664 | }
|
---|
5665 | }
|
---|
5666 |
|
---|
5667 | if (fMergeForward)
|
---|
5668 | {
|
---|
5669 | /* we will need parent to reparent target */
|
---|
5670 | pParentForTarget = i_getParent();
|
---|
5671 | }
|
---|
5672 | else
|
---|
5673 | {
|
---|
5674 | /* we will need to reparent children of the source */
|
---|
5675 | aChildrenToReparent = new MediumLockList();
|
---|
5676 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
5677 | it != i_getChildren().end();
|
---|
5678 | ++it)
|
---|
5679 | {
|
---|
5680 | pMedium = *it;
|
---|
5681 | aChildrenToReparent->Append(pMedium, true /* fLockWrite */);
|
---|
5682 | }
|
---|
5683 | if (fLockMedia && aChildrenToReparent)
|
---|
5684 | {
|
---|
5685 | treeLock.release();
|
---|
5686 | rc = aChildrenToReparent->Lock();
|
---|
5687 | treeLock.acquire();
|
---|
5688 | if (FAILED(rc))
|
---|
5689 | throw rc;
|
---|
5690 | }
|
---|
5691 | }
|
---|
5692 | for (pLast = pLastIntermediate;
|
---|
5693 | !pLast.isNull() && pLast != pTarget && pLast != this;
|
---|
5694 | pLast = pLast->i_getParent())
|
---|
5695 | {
|
---|
5696 | AutoWriteLock alock(pLast COMMA_LOCKVAL_SRC_POS);
|
---|
5697 | if (pLast->m->state == MediumState_Created)
|
---|
5698 | {
|
---|
5699 | rc = pLast->i_markForDeletion();
|
---|
5700 | if (FAILED(rc))
|
---|
5701 | throw rc;
|
---|
5702 | }
|
---|
5703 | else
|
---|
5704 | throw pLast->i_setStateError();
|
---|
5705 | }
|
---|
5706 |
|
---|
5707 | /* Tweak the lock list in the backward merge case, as the target
|
---|
5708 | * isn't marked to be locked for writing yet. */
|
---|
5709 | if (!fMergeForward)
|
---|
5710 | {
|
---|
5711 | MediumLockList::Base::iterator lockListBegin =
|
---|
5712 | aMediumLockList->GetBegin();
|
---|
5713 | MediumLockList::Base::iterator lockListEnd =
|
---|
5714 | aMediumLockList->GetEnd();
|
---|
5715 | ++lockListEnd;
|
---|
5716 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
5717 | it != lockListEnd;
|
---|
5718 | ++it)
|
---|
5719 | {
|
---|
5720 | MediumLock &mediumLock = *it;
|
---|
5721 | if (mediumLock.GetMedium() == pTarget)
|
---|
5722 | {
|
---|
5723 | HRESULT rc2 = mediumLock.UpdateLock(true);
|
---|
5724 | AssertComRC(rc2);
|
---|
5725 | break;
|
---|
5726 | }
|
---|
5727 | }
|
---|
5728 | }
|
---|
5729 |
|
---|
5730 | if (fLockMedia)
|
---|
5731 | {
|
---|
5732 | treeLock.release();
|
---|
5733 | rc = aMediumLockList->Lock();
|
---|
5734 | treeLock.acquire();
|
---|
5735 | if (FAILED(rc))
|
---|
5736 | {
|
---|
5737 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5738 | throw setError(rc,
|
---|
5739 | tr("Failed to lock media when merging to '%s'"),
|
---|
5740 | pTarget->i_getLocationFull().c_str());
|
---|
5741 | }
|
---|
5742 | }
|
---|
5743 | }
|
---|
5744 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5745 |
|
---|
5746 | if (FAILED(rc))
|
---|
5747 | {
|
---|
5748 | if (aMediumLockList)
|
---|
5749 | {
|
---|
5750 | delete aMediumLockList;
|
---|
5751 | aMediumLockList = NULL;
|
---|
5752 | }
|
---|
5753 | if (aChildrenToReparent)
|
---|
5754 | {
|
---|
5755 | delete aChildrenToReparent;
|
---|
5756 | aChildrenToReparent = NULL;
|
---|
5757 | }
|
---|
5758 | }
|
---|
5759 |
|
---|
5760 | return rc;
|
---|
5761 | }
|
---|
5762 |
|
---|
5763 | /**
|
---|
5764 | * Merges this medium to the specified medium which must be either its
|
---|
5765 | * direct ancestor or descendant.
|
---|
5766 | *
|
---|
5767 | * Given this medium is SOURCE and the specified medium is TARGET, we will
|
---|
5768 | * get two variants of the merge operation:
|
---|
5769 | *
|
---|
5770 | * forward merge
|
---|
5771 | * ------------------------->
|
---|
5772 | * [Extra] <- SOURCE <- Intermediate <- TARGET
|
---|
5773 | * Any Del Del LockWr
|
---|
5774 | *
|
---|
5775 | *
|
---|
5776 | * backward merge
|
---|
5777 | * <-------------------------
|
---|
5778 | * TARGET <- Intermediate <- SOURCE <- [Extra]
|
---|
5779 | * LockWr Del Del LockWr
|
---|
5780 | *
|
---|
5781 | * Each diagram shows the involved media on the media chain where
|
---|
5782 | * SOURCE and TARGET belong. Under each medium there is a state value which
|
---|
5783 | * the medium must have at a time of the mergeTo() call.
|
---|
5784 | *
|
---|
5785 | * The media in the square braces may be absent (e.g. when the forward
|
---|
5786 | * operation takes place and SOURCE is the base medium, or when the backward
|
---|
5787 | * merge operation takes place and TARGET is the last child in the chain) but if
|
---|
5788 | * they present they are involved too as shown.
|
---|
5789 | *
|
---|
5790 | * Neither the source medium nor intermediate media may be attached to
|
---|
5791 | * any VM directly or in the snapshot, otherwise this method will assert.
|
---|
5792 | *
|
---|
5793 | * The #prepareMergeTo() method must be called prior to this method to place all
|
---|
5794 | * involved to necessary states and perform other consistency checks.
|
---|
5795 | *
|
---|
5796 | * If @a aWait is @c true then this method will perform the operation on the
|
---|
5797 | * calling thread and will not return to the caller until the operation is
|
---|
5798 | * completed. When this method succeeds, all intermediate medium objects in
|
---|
5799 | * the chain will be uninitialized, the state of the target medium (and all
|
---|
5800 | * involved extra media) will be restored. @a aMediumLockList will not be
|
---|
5801 | * deleted, whether the operation is successful or not. The caller has to do
|
---|
5802 | * this if appropriate. Note that this (source) medium is not uninitialized
|
---|
5803 | * because of possible AutoCaller instances held by the caller of this method
|
---|
5804 | * on the current thread. It's therefore the responsibility of the caller to
|
---|
5805 | * call Medium::uninit() after releasing all callers.
|
---|
5806 | *
|
---|
5807 | * If @a aWait is @c false then this method will create a thread to perform the
|
---|
5808 | * operation asynchronously and will return immediately. If the operation
|
---|
5809 | * succeeds, the thread will uninitialize the source medium object and all
|
---|
5810 | * intermediate medium objects in the chain, reset the state of the target
|
---|
5811 | * medium (and all involved extra media) and delete @a aMediumLockList.
|
---|
5812 | * If the operation fails, the thread will only reset the states of all
|
---|
5813 | * involved media and delete @a aMediumLockList.
|
---|
5814 | *
|
---|
5815 | * When this method fails (regardless of the @a aWait mode), it is a caller's
|
---|
5816 | * responsibility to undo state changes and delete @a aMediumLockList using
|
---|
5817 | * #cancelMergeTo().
|
---|
5818 | *
|
---|
5819 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
5820 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
5821 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
5822 | * progress object is created/used at all. Note that @a aProgress cannot be
|
---|
5823 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
5824 | *
|
---|
5825 | * @param pTarget Target medium.
|
---|
5826 | * @param fMergeForward Merge direction.
|
---|
5827 | * @param pParentForTarget New parent for target medium after merge.
|
---|
5828 | * @param aChildrenToReparent List of children of the source which will have
|
---|
5829 | * to be reparented to the target after merge.
|
---|
5830 | * @param aMediumLockList Medium locking information.
|
---|
5831 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
5832 | * completion.
|
---|
5833 | * @param aWait @c true if this method should block instead of creating
|
---|
5834 | * an asynchronous thread.
|
---|
5835 | *
|
---|
5836 | * @note Locks the tree lock for writing. Locks the media from the chain
|
---|
5837 | * for writing.
|
---|
5838 | */
|
---|
5839 | HRESULT Medium::i_mergeTo(const ComObjPtr<Medium> &pTarget,
|
---|
5840 | bool fMergeForward,
|
---|
5841 | const ComObjPtr<Medium> &pParentForTarget,
|
---|
5842 | MediumLockList *aChildrenToReparent,
|
---|
5843 | MediumLockList *aMediumLockList,
|
---|
5844 | ComObjPtr<Progress> *aProgress,
|
---|
5845 | bool aWait)
|
---|
5846 | {
|
---|
5847 | AssertReturn(pTarget != NULL, E_FAIL);
|
---|
5848 | AssertReturn(pTarget != this, E_FAIL);
|
---|
5849 | AssertReturn(aMediumLockList != NULL, E_FAIL);
|
---|
5850 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
5851 |
|
---|
5852 | AutoCaller autoCaller(this);
|
---|
5853 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
5854 |
|
---|
5855 | AutoCaller targetCaller(pTarget);
|
---|
5856 | AssertComRCReturnRC(targetCaller.rc());
|
---|
5857 |
|
---|
5858 | HRESULT rc = S_OK;
|
---|
5859 | ComObjPtr<Progress> pProgress;
|
---|
5860 | Medium::Task *pTask = NULL;
|
---|
5861 |
|
---|
5862 | try
|
---|
5863 | {
|
---|
5864 | if (aProgress != NULL)
|
---|
5865 | {
|
---|
5866 | /* use the existing progress object... */
|
---|
5867 | pProgress = *aProgress;
|
---|
5868 |
|
---|
5869 | /* ...but create a new one if it is null */
|
---|
5870 | if (pProgress.isNull())
|
---|
5871 | {
|
---|
5872 | Utf8Str tgtName;
|
---|
5873 | {
|
---|
5874 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5875 | tgtName = pTarget->i_getName();
|
---|
5876 | }
|
---|
5877 |
|
---|
5878 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5879 |
|
---|
5880 | pProgress.createObject();
|
---|
5881 | rc = pProgress->init(m->pVirtualBox,
|
---|
5882 | static_cast<IMedium*>(this),
|
---|
5883 | BstrFmt(tr("Merging medium '%s' to '%s'"),
|
---|
5884 | i_getName().c_str(),
|
---|
5885 | tgtName.c_str()).raw(),
|
---|
5886 | TRUE /* aCancelable */);
|
---|
5887 | if (FAILED(rc))
|
---|
5888 | throw rc;
|
---|
5889 | }
|
---|
5890 | }
|
---|
5891 |
|
---|
5892 | /* setup task object to carry out the operation sync/async */
|
---|
5893 | pTask = new Medium::MergeTask(this, pTarget, fMergeForward,
|
---|
5894 | pParentForTarget, aChildrenToReparent,
|
---|
5895 | pProgress, aMediumLockList,
|
---|
5896 | aWait /* fKeepMediumLockList */);
|
---|
5897 | rc = pTask->rc();
|
---|
5898 | AssertComRC(rc);
|
---|
5899 | if (FAILED(rc))
|
---|
5900 | throw rc;
|
---|
5901 | }
|
---|
5902 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5903 |
|
---|
5904 | if (SUCCEEDED(rc))
|
---|
5905 | {
|
---|
5906 | if (aWait)
|
---|
5907 | {
|
---|
5908 | rc = pTask->runNow();
|
---|
5909 |
|
---|
5910 | delete pTask;
|
---|
5911 | }
|
---|
5912 | else
|
---|
5913 | rc = pTask->createThread();
|
---|
5914 |
|
---|
5915 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
5916 | *aProgress = pProgress;
|
---|
5917 | }
|
---|
5918 | else if (pTask != NULL)
|
---|
5919 | delete pTask;
|
---|
5920 |
|
---|
5921 | return rc;
|
---|
5922 | }
|
---|
5923 |
|
---|
5924 | /**
|
---|
5925 | * Undoes what #prepareMergeTo() did. Must be called if #mergeTo() is not
|
---|
5926 | * called or fails. Frees memory occupied by @a aMediumLockList and unlocks
|
---|
5927 | * the medium objects in @a aChildrenToReparent.
|
---|
5928 | *
|
---|
5929 | * @param aChildrenToReparent List of children of the source which will have
|
---|
5930 | * to be reparented to the target after merge.
|
---|
5931 | * @param aMediumLockList Medium locking information.
|
---|
5932 | *
|
---|
5933 | * @note Locks the media from the chain for writing.
|
---|
5934 | */
|
---|
5935 | void Medium::i_cancelMergeTo(MediumLockList *aChildrenToReparent,
|
---|
5936 | MediumLockList *aMediumLockList)
|
---|
5937 | {
|
---|
5938 | AutoCaller autoCaller(this);
|
---|
5939 | AssertComRCReturnVoid(autoCaller.rc());
|
---|
5940 |
|
---|
5941 | AssertReturnVoid(aMediumLockList != NULL);
|
---|
5942 |
|
---|
5943 | /* Revert media marked for deletion to previous state. */
|
---|
5944 | HRESULT rc;
|
---|
5945 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
5946 | aMediumLockList->GetBegin();
|
---|
5947 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
5948 | aMediumLockList->GetEnd();
|
---|
5949 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
5950 | it != mediumListEnd;
|
---|
5951 | ++it)
|
---|
5952 | {
|
---|
5953 | const MediumLock &mediumLock = *it;
|
---|
5954 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
5955 | AutoWriteLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
5956 |
|
---|
5957 | if (pMedium->m->state == MediumState_Deleting)
|
---|
5958 | {
|
---|
5959 | rc = pMedium->i_unmarkForDeletion();
|
---|
5960 | AssertComRC(rc);
|
---|
5961 | }
|
---|
5962 | else if ( ( pMedium->m->state == MediumState_LockedWrite
|
---|
5963 | || pMedium->m->state == MediumState_LockedRead)
|
---|
5964 | && pMedium->m->preLockState == MediumState_Deleting)
|
---|
5965 | {
|
---|
5966 | rc = pMedium->i_unmarkLockedForDeletion();
|
---|
5967 | AssertComRC(rc);
|
---|
5968 | }
|
---|
5969 | }
|
---|
5970 |
|
---|
5971 | /* the destructor will do the work */
|
---|
5972 | delete aMediumLockList;
|
---|
5973 |
|
---|
5974 | /* unlock the children which had to be reparented, the destructor will do
|
---|
5975 | * the work */
|
---|
5976 | if (aChildrenToReparent)
|
---|
5977 | delete aChildrenToReparent;
|
---|
5978 | }
|
---|
5979 |
|
---|
5980 | /**
|
---|
5981 | * Fix the parent UUID of all children to point to this medium as their
|
---|
5982 | * parent.
|
---|
5983 | */
|
---|
5984 | HRESULT Medium::i_fixParentUuidOfChildren(MediumLockList *pChildrenToReparent)
|
---|
5985 | {
|
---|
5986 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
5987 | * to lock order violations, it probably causes lock order issues related
|
---|
5988 | * to the AutoCaller usage. Likewise the code using this method seems
|
---|
5989 | * problematic. */
|
---|
5990 | Assert(!isWriteLockOnCurrentThread());
|
---|
5991 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
5992 | MediumLockList mediumLockList;
|
---|
5993 | HRESULT rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5994 | NULL /* pToLockWrite */,
|
---|
5995 | false /* fMediumLockWriteAll */,
|
---|
5996 | this,
|
---|
5997 | mediumLockList);
|
---|
5998 | AssertComRCReturnRC(rc);
|
---|
5999 |
|
---|
6000 | try
|
---|
6001 | {
|
---|
6002 | PVBOXHDD hdd;
|
---|
6003 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
6004 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6005 |
|
---|
6006 | try
|
---|
6007 | {
|
---|
6008 | MediumLockList::Base::iterator lockListBegin =
|
---|
6009 | mediumLockList.GetBegin();
|
---|
6010 | MediumLockList::Base::iterator lockListEnd =
|
---|
6011 | mediumLockList.GetEnd();
|
---|
6012 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
6013 | it != lockListEnd;
|
---|
6014 | ++it)
|
---|
6015 | {
|
---|
6016 | MediumLock &mediumLock = *it;
|
---|
6017 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
6018 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
6019 |
|
---|
6020 | // open the medium
|
---|
6021 | vrc = VDOpen(hdd,
|
---|
6022 | pMedium->m->strFormat.c_str(),
|
---|
6023 | pMedium->m->strLocationFull.c_str(),
|
---|
6024 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
6025 | pMedium->m->vdImageIfaces);
|
---|
6026 | if (RT_FAILURE(vrc))
|
---|
6027 | throw vrc;
|
---|
6028 | }
|
---|
6029 |
|
---|
6030 | MediumLockList::Base::iterator childrenBegin = pChildrenToReparent->GetBegin();
|
---|
6031 | MediumLockList::Base::iterator childrenEnd = pChildrenToReparent->GetEnd();
|
---|
6032 | for (MediumLockList::Base::iterator it = childrenBegin;
|
---|
6033 | it != childrenEnd;
|
---|
6034 | ++it)
|
---|
6035 | {
|
---|
6036 | Medium *pMedium = it->GetMedium();
|
---|
6037 | /* VD_OPEN_FLAGS_INFO since UUID is wrong yet */
|
---|
6038 | vrc = VDOpen(hdd,
|
---|
6039 | pMedium->m->strFormat.c_str(),
|
---|
6040 | pMedium->m->strLocationFull.c_str(),
|
---|
6041 | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
6042 | pMedium->m->vdImageIfaces);
|
---|
6043 | if (RT_FAILURE(vrc))
|
---|
6044 | throw vrc;
|
---|
6045 |
|
---|
6046 | vrc = VDSetParentUuid(hdd, VD_LAST_IMAGE, m->id.raw());
|
---|
6047 | if (RT_FAILURE(vrc))
|
---|
6048 | throw vrc;
|
---|
6049 |
|
---|
6050 | vrc = VDClose(hdd, false /* fDelete */);
|
---|
6051 | if (RT_FAILURE(vrc))
|
---|
6052 | throw vrc;
|
---|
6053 | }
|
---|
6054 | }
|
---|
6055 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6056 | catch (int aVRC)
|
---|
6057 | {
|
---|
6058 | rc = setError(E_FAIL,
|
---|
6059 | tr("Could not update medium UUID references to parent '%s' (%s)"),
|
---|
6060 | m->strLocationFull.c_str(),
|
---|
6061 | i_vdError(aVRC).c_str());
|
---|
6062 | }
|
---|
6063 |
|
---|
6064 | VDDestroy(hdd);
|
---|
6065 | }
|
---|
6066 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6067 |
|
---|
6068 | return rc;
|
---|
6069 | }
|
---|
6070 |
|
---|
6071 | /**
|
---|
6072 | * Used by IAppliance to export disk images.
|
---|
6073 | *
|
---|
6074 | * @param aFilename Filename to create (UTF8).
|
---|
6075 | * @param aFormat Medium format for creating @a aFilename.
|
---|
6076 | * @param aVariant Which exact image format variant to use
|
---|
6077 | * for the destination image.
|
---|
6078 | * @param pKeyStore The optional key store for decrypting the data
|
---|
6079 | * for encrypted media during the export.
|
---|
6080 | * @param aVDImageIOCallbacks Pointer to the callback table for a
|
---|
6081 | * VDINTERFACEIO interface. May be NULL.
|
---|
6082 | * @param aVDImageIOUser Opaque data for the callbacks.
|
---|
6083 | * @param aProgress Progress object to use.
|
---|
6084 | * @return
|
---|
6085 | * @note The source format is defined by the Medium instance.
|
---|
6086 | */
|
---|
6087 | HRESULT Medium::i_exportFile(const char *aFilename,
|
---|
6088 | const ComObjPtr<MediumFormat> &aFormat,
|
---|
6089 | MediumVariant_T aVariant,
|
---|
6090 | SecretKeyStore *pKeyStore,
|
---|
6091 | PVDINTERFACEIO aVDImageIOIf, void *aVDImageIOUser,
|
---|
6092 | const ComObjPtr<Progress> &aProgress)
|
---|
6093 | {
|
---|
6094 | AssertPtrReturn(aFilename, E_INVALIDARG);
|
---|
6095 | AssertReturn(!aFormat.isNull(), E_INVALIDARG);
|
---|
6096 | AssertReturn(!aProgress.isNull(), E_INVALIDARG);
|
---|
6097 |
|
---|
6098 | AutoCaller autoCaller(this);
|
---|
6099 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
6100 |
|
---|
6101 | HRESULT rc = S_OK;
|
---|
6102 | Medium::Task *pTask = NULL;
|
---|
6103 |
|
---|
6104 | try
|
---|
6105 | {
|
---|
6106 | // This needs no extra locks besides what is done in the called methods.
|
---|
6107 |
|
---|
6108 | /* Build the source lock list. */
|
---|
6109 | MediumLockList *pSourceMediumLockList(new MediumLockList());
|
---|
6110 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
6111 | NULL /* pToLockWrite */,
|
---|
6112 | false /* fMediumLockWriteAll */,
|
---|
6113 | NULL,
|
---|
6114 | *pSourceMediumLockList);
|
---|
6115 | if (FAILED(rc))
|
---|
6116 | {
|
---|
6117 | delete pSourceMediumLockList;
|
---|
6118 | throw rc;
|
---|
6119 | }
|
---|
6120 |
|
---|
6121 | rc = pSourceMediumLockList->Lock();
|
---|
6122 | if (FAILED(rc))
|
---|
6123 | {
|
---|
6124 | delete pSourceMediumLockList;
|
---|
6125 | throw setError(rc,
|
---|
6126 | tr("Failed to lock source media '%s'"),
|
---|
6127 | i_getLocationFull().c_str());
|
---|
6128 | }
|
---|
6129 |
|
---|
6130 | /* setup task object to carry out the operation asynchronously */
|
---|
6131 | pTask = new Medium::ExportTask(this, aProgress, aFilename, aFormat,
|
---|
6132 | aVariant, pKeyStore, aVDImageIOIf,
|
---|
6133 | aVDImageIOUser, pSourceMediumLockList);
|
---|
6134 | rc = pTask->rc();
|
---|
6135 | AssertComRC(rc);
|
---|
6136 | if (FAILED(rc))
|
---|
6137 | throw rc;
|
---|
6138 | }
|
---|
6139 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6140 |
|
---|
6141 | if (SUCCEEDED(rc))
|
---|
6142 | rc = pTask->createThread();
|
---|
6143 | else if (pTask != NULL)
|
---|
6144 | delete pTask;
|
---|
6145 |
|
---|
6146 | return rc;
|
---|
6147 | }
|
---|
6148 |
|
---|
6149 | /**
|
---|
6150 | * Used by IAppliance to import disk images.
|
---|
6151 | *
|
---|
6152 | * @param aFilename Filename to read (UTF8).
|
---|
6153 | * @param aFormat Medium format for reading @a aFilename.
|
---|
6154 | * @param aVariant Which exact image format variant to use
|
---|
6155 | * for the destination image.
|
---|
6156 | * @param aVfsIosSrc Handle to the source I/O stream.
|
---|
6157 | * @param aParent Parent medium. May be NULL.
|
---|
6158 | * @param aProgress Progress object to use.
|
---|
6159 | * @return
|
---|
6160 | * @note The destination format is defined by the Medium instance.
|
---|
6161 | */
|
---|
6162 | HRESULT Medium::i_importFile(const char *aFilename,
|
---|
6163 | const ComObjPtr<MediumFormat> &aFormat,
|
---|
6164 | MediumVariant_T aVariant,
|
---|
6165 | RTVFSIOSTREAM aVfsIosSrc,
|
---|
6166 | const ComObjPtr<Medium> &aParent,
|
---|
6167 | const ComObjPtr<Progress> &aProgress)
|
---|
6168 | {
|
---|
6169 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
6170 | * to lock order violations, it probably causes lock order issues related
|
---|
6171 | * to the AutoCaller usage. */
|
---|
6172 | AssertPtrReturn(aFilename, E_INVALIDARG);
|
---|
6173 | AssertReturn(!aFormat.isNull(), E_INVALIDARG);
|
---|
6174 | AssertReturn(!aProgress.isNull(), E_INVALIDARG);
|
---|
6175 |
|
---|
6176 | AutoCaller autoCaller(this);
|
---|
6177 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
6178 |
|
---|
6179 | HRESULT rc = S_OK;
|
---|
6180 | Medium::Task *pTask = NULL;
|
---|
6181 |
|
---|
6182 | try
|
---|
6183 | {
|
---|
6184 | // locking: we need the tree lock first because we access parent pointers
|
---|
6185 | // and we need to write-lock the media involved
|
---|
6186 | uint32_t cHandles = 2;
|
---|
6187 | LockHandle* pHandles[3] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
6188 | this->lockHandle() };
|
---|
6189 | /* Only add parent to the lock if it is not null */
|
---|
6190 | if (!aParent.isNull())
|
---|
6191 | pHandles[cHandles++] = aParent->lockHandle();
|
---|
6192 | AutoWriteLock alock(cHandles,
|
---|
6193 | pHandles
|
---|
6194 | COMMA_LOCKVAL_SRC_POS);
|
---|
6195 |
|
---|
6196 | if ( m->state != MediumState_NotCreated
|
---|
6197 | && m->state != MediumState_Created)
|
---|
6198 | throw i_setStateError();
|
---|
6199 |
|
---|
6200 | /* Build the target lock list. */
|
---|
6201 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
6202 | alock.release();
|
---|
6203 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
6204 | this /* pToLockWrite */,
|
---|
6205 | false /* fMediumLockWriteAll */,
|
---|
6206 | aParent,
|
---|
6207 | *pTargetMediumLockList);
|
---|
6208 | alock.acquire();
|
---|
6209 | if (FAILED(rc))
|
---|
6210 | {
|
---|
6211 | delete pTargetMediumLockList;
|
---|
6212 | throw rc;
|
---|
6213 | }
|
---|
6214 |
|
---|
6215 | alock.release();
|
---|
6216 | rc = pTargetMediumLockList->Lock();
|
---|
6217 | alock.acquire();
|
---|
6218 | if (FAILED(rc))
|
---|
6219 | {
|
---|
6220 | delete pTargetMediumLockList;
|
---|
6221 | throw setError(rc,
|
---|
6222 | tr("Failed to lock target media '%s'"),
|
---|
6223 | i_getLocationFull().c_str());
|
---|
6224 | }
|
---|
6225 |
|
---|
6226 | /* setup task object to carry out the operation asynchronously */
|
---|
6227 | pTask = new Medium::ImportTask(this, aProgress, aFilename, aFormat, aVariant,
|
---|
6228 | aVfsIosSrc, aParent, pTargetMediumLockList);
|
---|
6229 | rc = pTask->rc();
|
---|
6230 | AssertComRC(rc);
|
---|
6231 | if (FAILED(rc))
|
---|
6232 | throw rc;
|
---|
6233 |
|
---|
6234 | if (m->state == MediumState_NotCreated)
|
---|
6235 | m->state = MediumState_Creating;
|
---|
6236 | }
|
---|
6237 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6238 |
|
---|
6239 | if (SUCCEEDED(rc))
|
---|
6240 | rc = pTask->createThread();
|
---|
6241 | else if (pTask != NULL)
|
---|
6242 | delete pTask;
|
---|
6243 |
|
---|
6244 | return rc;
|
---|
6245 | }
|
---|
6246 |
|
---|
6247 | /**
|
---|
6248 | * Internal version of the public CloneTo API which allows to enable certain
|
---|
6249 | * optimizations to improve speed during VM cloning.
|
---|
6250 | *
|
---|
6251 | * @param aTarget Target medium
|
---|
6252 | * @param aVariant Which exact image format variant to use
|
---|
6253 | * for the destination image.
|
---|
6254 | * @param aParent Parent medium. May be NULL.
|
---|
6255 | * @param aProgress Progress object to use.
|
---|
6256 | * @param idxSrcImageSame The last image in the source chain which has the
|
---|
6257 | * same content as the given image in the destination
|
---|
6258 | * chain. Use UINT32_MAX to disable this optimization.
|
---|
6259 | * @param idxDstImageSame The last image in the destination chain which has the
|
---|
6260 | * same content as the given image in the source chain.
|
---|
6261 | * Use UINT32_MAX to disable this optimization.
|
---|
6262 | * @return
|
---|
6263 | */
|
---|
6264 | HRESULT Medium::i_cloneToEx(const ComObjPtr<Medium> &aTarget, ULONG aVariant,
|
---|
6265 | const ComObjPtr<Medium> &aParent, IProgress **aProgress,
|
---|
6266 | uint32_t idxSrcImageSame, uint32_t idxDstImageSame)
|
---|
6267 | {
|
---|
6268 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
6269 | * to lock order violations, it probably causes lock order issues related
|
---|
6270 | * to the AutoCaller usage. */
|
---|
6271 | CheckComArgNotNull(aTarget);
|
---|
6272 | CheckComArgOutPointerValid(aProgress);
|
---|
6273 | ComAssertRet(aTarget != this, E_INVALIDARG);
|
---|
6274 |
|
---|
6275 | AutoCaller autoCaller(this);
|
---|
6276 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
6277 |
|
---|
6278 | HRESULT rc = S_OK;
|
---|
6279 | ComObjPtr<Progress> pProgress;
|
---|
6280 | Medium::Task *pTask = NULL;
|
---|
6281 |
|
---|
6282 | try
|
---|
6283 | {
|
---|
6284 | // locking: we need the tree lock first because we access parent pointers
|
---|
6285 | // and we need to write-lock the media involved
|
---|
6286 | uint32_t cHandles = 3;
|
---|
6287 | LockHandle* pHandles[4] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
6288 | this->lockHandle(),
|
---|
6289 | aTarget->lockHandle() };
|
---|
6290 | /* Only add parent to the lock if it is not null */
|
---|
6291 | if (!aParent.isNull())
|
---|
6292 | pHandles[cHandles++] = aParent->lockHandle();
|
---|
6293 | AutoWriteLock alock(cHandles,
|
---|
6294 | pHandles
|
---|
6295 | COMMA_LOCKVAL_SRC_POS);
|
---|
6296 |
|
---|
6297 | if ( aTarget->m->state != MediumState_NotCreated
|
---|
6298 | && aTarget->m->state != MediumState_Created)
|
---|
6299 | throw aTarget->i_setStateError();
|
---|
6300 |
|
---|
6301 | /* Build the source lock list. */
|
---|
6302 | MediumLockList *pSourceMediumLockList(new MediumLockList());
|
---|
6303 | alock.release();
|
---|
6304 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
6305 | NULL /* pToLockWrite */,
|
---|
6306 | false /* fMediumLockWriteAll */,
|
---|
6307 | NULL,
|
---|
6308 | *pSourceMediumLockList);
|
---|
6309 | alock.acquire();
|
---|
6310 | if (FAILED(rc))
|
---|
6311 | {
|
---|
6312 | delete pSourceMediumLockList;
|
---|
6313 | throw rc;
|
---|
6314 | }
|
---|
6315 |
|
---|
6316 | /* Build the target lock list (including the to-be parent chain). */
|
---|
6317 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
6318 | alock.release();
|
---|
6319 | rc = aTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
6320 | aTarget /* pToLockWrite */,
|
---|
6321 | false /* fMediumLockWriteAll */,
|
---|
6322 | aParent,
|
---|
6323 | *pTargetMediumLockList);
|
---|
6324 | alock.acquire();
|
---|
6325 | if (FAILED(rc))
|
---|
6326 | {
|
---|
6327 | delete pSourceMediumLockList;
|
---|
6328 | delete pTargetMediumLockList;
|
---|
6329 | throw rc;
|
---|
6330 | }
|
---|
6331 |
|
---|
6332 | alock.release();
|
---|
6333 | rc = pSourceMediumLockList->Lock();
|
---|
6334 | alock.acquire();
|
---|
6335 | if (FAILED(rc))
|
---|
6336 | {
|
---|
6337 | delete pSourceMediumLockList;
|
---|
6338 | delete pTargetMediumLockList;
|
---|
6339 | throw setError(rc,
|
---|
6340 | tr("Failed to lock source media '%s'"),
|
---|
6341 | i_getLocationFull().c_str());
|
---|
6342 | }
|
---|
6343 | alock.release();
|
---|
6344 | rc = pTargetMediumLockList->Lock();
|
---|
6345 | alock.acquire();
|
---|
6346 | if (FAILED(rc))
|
---|
6347 | {
|
---|
6348 | delete pSourceMediumLockList;
|
---|
6349 | delete pTargetMediumLockList;
|
---|
6350 | throw setError(rc,
|
---|
6351 | tr("Failed to lock target media '%s'"),
|
---|
6352 | aTarget->i_getLocationFull().c_str());
|
---|
6353 | }
|
---|
6354 |
|
---|
6355 | pProgress.createObject();
|
---|
6356 | rc = pProgress->init(m->pVirtualBox,
|
---|
6357 | static_cast <IMedium *>(this),
|
---|
6358 | BstrFmt(tr("Creating clone medium '%s'"), aTarget->m->strLocationFull.c_str()).raw(),
|
---|
6359 | TRUE /* aCancelable */);
|
---|
6360 | if (FAILED(rc))
|
---|
6361 | {
|
---|
6362 | delete pSourceMediumLockList;
|
---|
6363 | delete pTargetMediumLockList;
|
---|
6364 | throw rc;
|
---|
6365 | }
|
---|
6366 |
|
---|
6367 | /* setup task object to carry out the operation asynchronously */
|
---|
6368 | pTask = new Medium::CloneTask(this, pProgress, aTarget,
|
---|
6369 | (MediumVariant_T)aVariant,
|
---|
6370 | aParent, idxSrcImageSame,
|
---|
6371 | idxDstImageSame, pSourceMediumLockList,
|
---|
6372 | pTargetMediumLockList);
|
---|
6373 | rc = pTask->rc();
|
---|
6374 | AssertComRC(rc);
|
---|
6375 | if (FAILED(rc))
|
---|
6376 | throw rc;
|
---|
6377 |
|
---|
6378 | if (aTarget->m->state == MediumState_NotCreated)
|
---|
6379 | aTarget->m->state = MediumState_Creating;
|
---|
6380 | }
|
---|
6381 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6382 |
|
---|
6383 | if (SUCCEEDED(rc))
|
---|
6384 | {
|
---|
6385 | rc = pTask->createThread();
|
---|
6386 |
|
---|
6387 | if (SUCCEEDED(rc))
|
---|
6388 | pProgress.queryInterfaceTo(aProgress);
|
---|
6389 | }
|
---|
6390 | else if (pTask != NULL)
|
---|
6391 | delete pTask;
|
---|
6392 |
|
---|
6393 | return rc;
|
---|
6394 | }
|
---|
6395 |
|
---|
6396 | /**
|
---|
6397 | * Returns the key identifier for this medium if encryption is configured.
|
---|
6398 | *
|
---|
6399 | * @returns Key identifier or empty string if no encryption is configured.
|
---|
6400 | */
|
---|
6401 | const Utf8Str& Medium::i_getKeyId()
|
---|
6402 | {
|
---|
6403 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
6404 |
|
---|
6405 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6406 |
|
---|
6407 | settings::StringsMap::const_iterator it = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
6408 | if (it == pBase->m->mapProperties.end())
|
---|
6409 | return Utf8Str::Empty;
|
---|
6410 |
|
---|
6411 | return it->second;
|
---|
6412 | }
|
---|
6413 |
|
---|
6414 | /**
|
---|
6415 | * Returns all filter related properties.
|
---|
6416 | *
|
---|
6417 | * @returns COM status code.
|
---|
6418 | * @param aReturnNames Where to store the properties names on success.
|
---|
6419 | * @param aReturnValues Where to store the properties values on success.
|
---|
6420 | */
|
---|
6421 | HRESULT Medium::i_getFilterProperties(std::vector<com::Utf8Str> &aReturnNames,
|
---|
6422 | std::vector<com::Utf8Str> &aReturnValues)
|
---|
6423 | {
|
---|
6424 | std::vector<com::Utf8Str> aPropNames;
|
---|
6425 | std::vector<com::Utf8Str> aPropValues;
|
---|
6426 | HRESULT hrc = getProperties(Utf8Str(""), aPropNames, aPropValues);
|
---|
6427 |
|
---|
6428 | if (SUCCEEDED(hrc))
|
---|
6429 | {
|
---|
6430 | unsigned cReturnSize = 0;
|
---|
6431 | aReturnNames.resize(0);
|
---|
6432 | aReturnValues.resize(0);
|
---|
6433 | for (unsigned idx = 0; idx < aPropNames.size(); idx++)
|
---|
6434 | {
|
---|
6435 | if (i_isPropertyForFilter(aPropNames[idx]))
|
---|
6436 | {
|
---|
6437 | aReturnNames.resize(cReturnSize + 1);
|
---|
6438 | aReturnValues.resize(cReturnSize + 1);
|
---|
6439 | aReturnNames[cReturnSize] = aPropNames[idx];
|
---|
6440 | aReturnValues[cReturnSize] = aPropValues[idx];
|
---|
6441 | cReturnSize++;
|
---|
6442 | }
|
---|
6443 | }
|
---|
6444 | }
|
---|
6445 |
|
---|
6446 | return hrc;
|
---|
6447 | }
|
---|
6448 |
|
---|
6449 | /**
|
---|
6450 | * Preparation to move this medium to a new location
|
---|
6451 | *
|
---|
6452 | * @param aLocation Location of the storage unit. If the location is a FS-path,
|
---|
6453 | * then it can be relative to the VirtualBox home directory.
|
---|
6454 | *
|
---|
6455 | * @note Must be called from under this object's write lock.
|
---|
6456 | */
|
---|
6457 | HRESULT Medium::i_preparationForMoving(const Utf8Str &aLocation)
|
---|
6458 | {
|
---|
6459 | HRESULT rc = E_FAIL;
|
---|
6460 |
|
---|
6461 | if (i_getLocationFull() != aLocation)
|
---|
6462 | {
|
---|
6463 | m->strNewLocationFull = aLocation;
|
---|
6464 | m->fMoveThisMedium = true;
|
---|
6465 | rc = S_OK;
|
---|
6466 | }
|
---|
6467 |
|
---|
6468 | return rc;
|
---|
6469 | }
|
---|
6470 |
|
---|
6471 | /**
|
---|
6472 | * Checking whether current operation "moving" or not
|
---|
6473 | */
|
---|
6474 | bool Medium::i_isMoveOperation(const ComObjPtr<Medium> &aTarget) const
|
---|
6475 | {
|
---|
6476 | RT_NOREF(aTarget);
|
---|
6477 | return (m->fMoveThisMedium == true) ? true:false;
|
---|
6478 | }
|
---|
6479 |
|
---|
6480 | bool Medium::i_resetMoveOperationData()
|
---|
6481 | {
|
---|
6482 | m->strNewLocationFull.setNull();
|
---|
6483 | m->fMoveThisMedium = false;
|
---|
6484 | return true;
|
---|
6485 | }
|
---|
6486 |
|
---|
6487 | Utf8Str Medium::i_getNewLocationForMoving() const
|
---|
6488 | {
|
---|
6489 | if(m->fMoveThisMedium == true)
|
---|
6490 | return m->strNewLocationFull;
|
---|
6491 | else
|
---|
6492 | return Utf8Str();
|
---|
6493 | }
|
---|
6494 | ////////////////////////////////////////////////////////////////////////////////
|
---|
6495 | //
|
---|
6496 | // Private methods
|
---|
6497 | //
|
---|
6498 | ////////////////////////////////////////////////////////////////////////////////
|
---|
6499 |
|
---|
6500 | /**
|
---|
6501 | * Queries information from the medium.
|
---|
6502 | *
|
---|
6503 | * As a result of this call, the accessibility state and data members such as
|
---|
6504 | * size and description will be updated with the current information.
|
---|
6505 | *
|
---|
6506 | * @note This method may block during a system I/O call that checks storage
|
---|
6507 | * accessibility.
|
---|
6508 | *
|
---|
6509 | * @note Caller MUST NOT hold the media tree or medium lock.
|
---|
6510 | *
|
---|
6511 | * @note Locks m->pParent for reading. Locks this object for writing.
|
---|
6512 | *
|
---|
6513 | * @param fSetImageId Whether to reset the UUID contained in the image file to the UUID in the medium instance data (see SetIDs())
|
---|
6514 | * @param fSetParentId Whether to reset the parent UUID contained in the image file to the parent
|
---|
6515 | * UUID in the medium instance data (see SetIDs())
|
---|
6516 | * @return
|
---|
6517 | */
|
---|
6518 | HRESULT Medium::i_queryInfo(bool fSetImageId, bool fSetParentId, AutoCaller &autoCaller)
|
---|
6519 | {
|
---|
6520 | Assert(!isWriteLockOnCurrentThread());
|
---|
6521 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6522 |
|
---|
6523 | if ( ( m->state != MediumState_Created
|
---|
6524 | && m->state != MediumState_Inaccessible
|
---|
6525 | && m->state != MediumState_LockedRead)
|
---|
6526 | || m->fClosing)
|
---|
6527 | return E_FAIL;
|
---|
6528 |
|
---|
6529 | HRESULT rc = S_OK;
|
---|
6530 |
|
---|
6531 | int vrc = VINF_SUCCESS;
|
---|
6532 |
|
---|
6533 | /* check if a blocking i_queryInfo() call is in progress on some other thread,
|
---|
6534 | * and wait for it to finish if so instead of querying data ourselves */
|
---|
6535 | if (m->queryInfoRunning)
|
---|
6536 | {
|
---|
6537 | Assert( m->state == MediumState_LockedRead
|
---|
6538 | || m->state == MediumState_LockedWrite);
|
---|
6539 |
|
---|
6540 | while (m->queryInfoRunning)
|
---|
6541 | {
|
---|
6542 | alock.release();
|
---|
6543 | /* must not hold the object lock now */
|
---|
6544 | Assert(!isWriteLockOnCurrentThread());
|
---|
6545 | {
|
---|
6546 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
6547 | }
|
---|
6548 | alock.acquire();
|
---|
6549 | }
|
---|
6550 |
|
---|
6551 | return S_OK;
|
---|
6552 | }
|
---|
6553 |
|
---|
6554 | bool success = false;
|
---|
6555 | Utf8Str lastAccessError;
|
---|
6556 |
|
---|
6557 | /* are we dealing with a new medium constructed using the existing
|
---|
6558 | * location? */
|
---|
6559 | bool isImport = m->id.isZero();
|
---|
6560 | unsigned uOpenFlags = VD_OPEN_FLAGS_INFO;
|
---|
6561 |
|
---|
6562 | /* Note that we don't use VD_OPEN_FLAGS_READONLY when opening new
|
---|
6563 | * media because that would prevent necessary modifications
|
---|
6564 | * when opening media of some third-party formats for the first
|
---|
6565 | * time in VirtualBox (such as VMDK for which VDOpen() needs to
|
---|
6566 | * generate an UUID if it is missing) */
|
---|
6567 | if ( m->hddOpenMode == OpenReadOnly
|
---|
6568 | || m->type == MediumType_Readonly
|
---|
6569 | || (!isImport && !fSetImageId && !fSetParentId)
|
---|
6570 | )
|
---|
6571 | uOpenFlags |= VD_OPEN_FLAGS_READONLY;
|
---|
6572 |
|
---|
6573 | /* Open shareable medium with the appropriate flags */
|
---|
6574 | if (m->type == MediumType_Shareable)
|
---|
6575 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
6576 |
|
---|
6577 | /* Lock the medium, which makes the behavior much more consistent, must be
|
---|
6578 | * done before dropping the object lock and setting queryInfoRunning. */
|
---|
6579 | ComPtr<IToken> pToken;
|
---|
6580 | if (uOpenFlags & (VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SHAREABLE))
|
---|
6581 | rc = LockRead(pToken.asOutParam());
|
---|
6582 | else
|
---|
6583 | rc = LockWrite(pToken.asOutParam());
|
---|
6584 | if (FAILED(rc)) return rc;
|
---|
6585 |
|
---|
6586 | /* Copies of the input state fields which are not read-only,
|
---|
6587 | * as we're dropping the lock. CAUTION: be extremely careful what
|
---|
6588 | * you do with the contents of this medium object, as you will
|
---|
6589 | * create races if there are concurrent changes. */
|
---|
6590 | Utf8Str format(m->strFormat);
|
---|
6591 | Utf8Str location(m->strLocationFull);
|
---|
6592 | ComObjPtr<MediumFormat> formatObj = m->formatObj;
|
---|
6593 |
|
---|
6594 | /* "Output" values which can't be set because the lock isn't held
|
---|
6595 | * at the time the values are determined. */
|
---|
6596 | Guid mediumId = m->id;
|
---|
6597 | uint64_t mediumSize = 0;
|
---|
6598 | uint64_t mediumLogicalSize = 0;
|
---|
6599 |
|
---|
6600 | /* Flag whether a base image has a non-zero parent UUID and thus
|
---|
6601 | * need repairing after it was closed again. */
|
---|
6602 | bool fRepairImageZeroParentUuid = false;
|
---|
6603 |
|
---|
6604 | ComObjPtr<VirtualBox> pVirtualBox = m->pVirtualBox;
|
---|
6605 |
|
---|
6606 | /* must be set before leaving the object lock the first time */
|
---|
6607 | m->queryInfoRunning = true;
|
---|
6608 |
|
---|
6609 | /* must leave object lock now, because a lock from a higher lock class
|
---|
6610 | * is needed and also a lengthy operation is coming */
|
---|
6611 | alock.release();
|
---|
6612 | autoCaller.release();
|
---|
6613 |
|
---|
6614 | /* Note that taking the queryInfoSem after leaving the object lock above
|
---|
6615 | * can lead to short spinning of the loops waiting for i_queryInfo() to
|
---|
6616 | * complete. This is unavoidable since the other order causes a lock order
|
---|
6617 | * violation: here it would be requesting the object lock (at the beginning
|
---|
6618 | * of the method), then queryInfoSem, and below the other way round. */
|
---|
6619 | AutoWriteLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
6620 |
|
---|
6621 | /* take the opportunity to have a media tree lock, released initially */
|
---|
6622 | Assert(!isWriteLockOnCurrentThread());
|
---|
6623 | Assert(!pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
6624 | AutoWriteLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
6625 | treeLock.release();
|
---|
6626 |
|
---|
6627 | /* re-take the caller, but not the object lock, to keep uninit away */
|
---|
6628 | autoCaller.add();
|
---|
6629 | if (FAILED(autoCaller.rc()))
|
---|
6630 | {
|
---|
6631 | m->queryInfoRunning = false;
|
---|
6632 | return autoCaller.rc();
|
---|
6633 | }
|
---|
6634 |
|
---|
6635 | try
|
---|
6636 | {
|
---|
6637 | /* skip accessibility checks for host drives */
|
---|
6638 | if (m->hostDrive)
|
---|
6639 | {
|
---|
6640 | success = true;
|
---|
6641 | throw S_OK;
|
---|
6642 | }
|
---|
6643 |
|
---|
6644 | PVBOXHDD hdd;
|
---|
6645 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
6646 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6647 |
|
---|
6648 | try
|
---|
6649 | {
|
---|
6650 | /** @todo This kind of opening of media is assuming that diff
|
---|
6651 | * media can be opened as base media. Should be documented that
|
---|
6652 | * it must work for all medium format backends. */
|
---|
6653 | vrc = VDOpen(hdd,
|
---|
6654 | format.c_str(),
|
---|
6655 | location.c_str(),
|
---|
6656 | uOpenFlags | m->uOpenFlagsDef,
|
---|
6657 | m->vdImageIfaces);
|
---|
6658 | if (RT_FAILURE(vrc))
|
---|
6659 | {
|
---|
6660 | lastAccessError = Utf8StrFmt(tr("Could not open the medium '%s'%s"),
|
---|
6661 | location.c_str(), i_vdError(vrc).c_str());
|
---|
6662 | throw S_OK;
|
---|
6663 | }
|
---|
6664 |
|
---|
6665 | if (formatObj->i_getCapabilities() & MediumFormatCapabilities_Uuid)
|
---|
6666 | {
|
---|
6667 | /* Modify the UUIDs if necessary. The associated fields are
|
---|
6668 | * not modified by other code, so no need to copy. */
|
---|
6669 | if (fSetImageId)
|
---|
6670 | {
|
---|
6671 | alock.acquire();
|
---|
6672 | vrc = VDSetUuid(hdd, 0, m->uuidImage.raw());
|
---|
6673 | alock.release();
|
---|
6674 | if (RT_FAILURE(vrc))
|
---|
6675 | {
|
---|
6676 | lastAccessError = Utf8StrFmt(tr("Could not update the UUID of medium '%s'%s"),
|
---|
6677 | location.c_str(), i_vdError(vrc).c_str());
|
---|
6678 | throw S_OK;
|
---|
6679 | }
|
---|
6680 | mediumId = m->uuidImage;
|
---|
6681 | }
|
---|
6682 | if (fSetParentId)
|
---|
6683 | {
|
---|
6684 | alock.acquire();
|
---|
6685 | vrc = VDSetParentUuid(hdd, 0, m->uuidParentImage.raw());
|
---|
6686 | alock.release();
|
---|
6687 | if (RT_FAILURE(vrc))
|
---|
6688 | {
|
---|
6689 | lastAccessError = Utf8StrFmt(tr("Could not update the parent UUID of medium '%s'%s"),
|
---|
6690 | location.c_str(), i_vdError(vrc).c_str());
|
---|
6691 | throw S_OK;
|
---|
6692 | }
|
---|
6693 | }
|
---|
6694 | /* zap the information, these are no long-term members */
|
---|
6695 | alock.acquire();
|
---|
6696 | unconst(m->uuidImage).clear();
|
---|
6697 | unconst(m->uuidParentImage).clear();
|
---|
6698 | alock.release();
|
---|
6699 |
|
---|
6700 | /* check the UUID */
|
---|
6701 | RTUUID uuid;
|
---|
6702 | vrc = VDGetUuid(hdd, 0, &uuid);
|
---|
6703 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6704 |
|
---|
6705 | if (isImport)
|
---|
6706 | {
|
---|
6707 | mediumId = uuid;
|
---|
6708 |
|
---|
6709 | if (mediumId.isZero() && (m->hddOpenMode == OpenReadOnly))
|
---|
6710 | // only when importing a VDMK that has no UUID, create one in memory
|
---|
6711 | mediumId.create();
|
---|
6712 | }
|
---|
6713 | else
|
---|
6714 | {
|
---|
6715 | Assert(!mediumId.isZero());
|
---|
6716 |
|
---|
6717 | if (mediumId != uuid)
|
---|
6718 | {
|
---|
6719 | /** @todo r=klaus this always refers to VirtualBox.xml as the medium registry, even for new VMs */
|
---|
6720 | lastAccessError = Utf8StrFmt(
|
---|
6721 | tr("UUID {%RTuuid} of the medium '%s' does not match the value {%RTuuid} stored in the media registry ('%s')"),
|
---|
6722 | &uuid,
|
---|
6723 | location.c_str(),
|
---|
6724 | mediumId.raw(),
|
---|
6725 | pVirtualBox->i_settingsFilePath().c_str());
|
---|
6726 | throw S_OK;
|
---|
6727 | }
|
---|
6728 | }
|
---|
6729 | }
|
---|
6730 | else
|
---|
6731 | {
|
---|
6732 | /* the backend does not support storing UUIDs within the
|
---|
6733 | * underlying storage so use what we store in XML */
|
---|
6734 |
|
---|
6735 | if (fSetImageId)
|
---|
6736 | {
|
---|
6737 | /* set the UUID if an API client wants to change it */
|
---|
6738 | alock.acquire();
|
---|
6739 | mediumId = m->uuidImage;
|
---|
6740 | alock.release();
|
---|
6741 | }
|
---|
6742 | else if (isImport)
|
---|
6743 | {
|
---|
6744 | /* generate an UUID for an imported UUID-less medium */
|
---|
6745 | mediumId.create();
|
---|
6746 | }
|
---|
6747 | }
|
---|
6748 |
|
---|
6749 | /* set the image uuid before the below parent uuid handling code
|
---|
6750 | * might place it somewhere in the media tree, so that the medium
|
---|
6751 | * UUID is valid at this point */
|
---|
6752 | alock.acquire();
|
---|
6753 | if (isImport || fSetImageId)
|
---|
6754 | unconst(m->id) = mediumId;
|
---|
6755 | alock.release();
|
---|
6756 |
|
---|
6757 | /* get the medium variant */
|
---|
6758 | unsigned uImageFlags;
|
---|
6759 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
6760 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6761 | alock.acquire();
|
---|
6762 | m->variant = (MediumVariant_T)uImageFlags;
|
---|
6763 | alock.release();
|
---|
6764 |
|
---|
6765 | /* check/get the parent uuid and update corresponding state */
|
---|
6766 | if (uImageFlags & VD_IMAGE_FLAGS_DIFF)
|
---|
6767 | {
|
---|
6768 | RTUUID parentId;
|
---|
6769 | vrc = VDGetParentUuid(hdd, 0, &parentId);
|
---|
6770 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6771 |
|
---|
6772 | /* streamOptimized VMDK images are only accepted as base
|
---|
6773 | * images, as this allows automatic repair of OVF appliances.
|
---|
6774 | * Since such images don't support random writes they will not
|
---|
6775 | * be created for diff images. Only an overly smart user might
|
---|
6776 | * manually create this case. Too bad for him. */
|
---|
6777 | if ( (isImport || fSetParentId)
|
---|
6778 | && !(uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
|
---|
6779 | {
|
---|
6780 | /* the parent must be known to us. Note that we freely
|
---|
6781 | * call locking methods of mVirtualBox and parent, as all
|
---|
6782 | * relevant locks must be already held. There may be no
|
---|
6783 | * concurrent access to the just opened medium on other
|
---|
6784 | * threads yet (and init() will fail if this method reports
|
---|
6785 | * MediumState_Inaccessible) */
|
---|
6786 |
|
---|
6787 | ComObjPtr<Medium> pParent;
|
---|
6788 | if (RTUuidIsNull(&parentId))
|
---|
6789 | rc = VBOX_E_OBJECT_NOT_FOUND;
|
---|
6790 | else
|
---|
6791 | rc = pVirtualBox->i_findHardDiskById(Guid(parentId), false /* aSetError */, &pParent);
|
---|
6792 | if (FAILED(rc))
|
---|
6793 | {
|
---|
6794 | if (fSetImageId && !fSetParentId)
|
---|
6795 | {
|
---|
6796 | /* If the image UUID gets changed for an existing
|
---|
6797 | * image then the parent UUID can be stale. In such
|
---|
6798 | * cases clear the parent information. The parent
|
---|
6799 | * information may/will be re-set later if the
|
---|
6800 | * API client wants to adjust a complete medium
|
---|
6801 | * hierarchy one by one. */
|
---|
6802 | rc = S_OK;
|
---|
6803 | alock.acquire();
|
---|
6804 | RTUuidClear(&parentId);
|
---|
6805 | vrc = VDSetParentUuid(hdd, 0, &parentId);
|
---|
6806 | alock.release();
|
---|
6807 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6808 | }
|
---|
6809 | else
|
---|
6810 | {
|
---|
6811 | lastAccessError = Utf8StrFmt(tr("Parent medium with UUID {%RTuuid} of the medium '%s' is not found in the media registry ('%s')"),
|
---|
6812 | &parentId, location.c_str(),
|
---|
6813 | pVirtualBox->i_settingsFilePath().c_str());
|
---|
6814 | throw S_OK;
|
---|
6815 | }
|
---|
6816 | }
|
---|
6817 |
|
---|
6818 | /* must drop the caller before taking the tree lock */
|
---|
6819 | autoCaller.release();
|
---|
6820 | /* we set m->pParent & children() */
|
---|
6821 | treeLock.acquire();
|
---|
6822 | autoCaller.add();
|
---|
6823 | if (FAILED(autoCaller.rc()))
|
---|
6824 | throw autoCaller.rc();
|
---|
6825 |
|
---|
6826 | if (m->pParent)
|
---|
6827 | i_deparent();
|
---|
6828 |
|
---|
6829 | if (!pParent.isNull())
|
---|
6830 | if (pParent->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
6831 | {
|
---|
6832 | AutoReadLock plock(pParent COMMA_LOCKVAL_SRC_POS);
|
---|
6833 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
6834 | 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"),
|
---|
6835 | pParent->m->strLocationFull.c_str());
|
---|
6836 | }
|
---|
6837 | i_setParent(pParent);
|
---|
6838 |
|
---|
6839 | treeLock.release();
|
---|
6840 | }
|
---|
6841 | else
|
---|
6842 | {
|
---|
6843 | /* must drop the caller before taking the tree lock */
|
---|
6844 | autoCaller.release();
|
---|
6845 | /* we access m->pParent */
|
---|
6846 | treeLock.acquire();
|
---|
6847 | autoCaller.add();
|
---|
6848 | if (FAILED(autoCaller.rc()))
|
---|
6849 | throw autoCaller.rc();
|
---|
6850 |
|
---|
6851 | /* check that parent UUIDs match. Note that there's no need
|
---|
6852 | * for the parent's AutoCaller (our lifetime is bound to
|
---|
6853 | * it) */
|
---|
6854 |
|
---|
6855 | if (m->pParent.isNull())
|
---|
6856 | {
|
---|
6857 | /* Due to a bug in VDCopy() in VirtualBox 3.0.0-3.0.14
|
---|
6858 | * and 3.1.0-3.1.8 there are base images out there
|
---|
6859 | * which have a non-zero parent UUID. No point in
|
---|
6860 | * complaining about them, instead automatically
|
---|
6861 | * repair the problem. Later we can bring back the
|
---|
6862 | * error message, but we should wait until really
|
---|
6863 | * most users have repaired their images, either with
|
---|
6864 | * VBoxFixHdd or this way. */
|
---|
6865 | #if 1
|
---|
6866 | fRepairImageZeroParentUuid = true;
|
---|
6867 | #else /* 0 */
|
---|
6868 | lastAccessError = Utf8StrFmt(
|
---|
6869 | tr("Medium type of '%s' is differencing but it is not associated with any parent medium in the media registry ('%s')"),
|
---|
6870 | location.c_str(),
|
---|
6871 | pVirtualBox->settingsFilePath().c_str());
|
---|
6872 | treeLock.release();
|
---|
6873 | throw S_OK;
|
---|
6874 | #endif /* 0 */
|
---|
6875 | }
|
---|
6876 |
|
---|
6877 | {
|
---|
6878 | autoCaller.release();
|
---|
6879 | AutoReadLock parentLock(m->pParent COMMA_LOCKVAL_SRC_POS);
|
---|
6880 | autoCaller.add();
|
---|
6881 | if (FAILED(autoCaller.rc()))
|
---|
6882 | throw autoCaller.rc();
|
---|
6883 |
|
---|
6884 | if ( !fRepairImageZeroParentUuid
|
---|
6885 | && m->pParent->i_getState() != MediumState_Inaccessible
|
---|
6886 | && m->pParent->i_getId() != parentId)
|
---|
6887 | {
|
---|
6888 | /** @todo r=klaus this always refers to VirtualBox.xml as the medium registry, even for new VMs */
|
---|
6889 | lastAccessError = Utf8StrFmt(
|
---|
6890 | tr("Parent UUID {%RTuuid} of the medium '%s' does not match UUID {%RTuuid} of its parent medium stored in the media registry ('%s')"),
|
---|
6891 | &parentId, location.c_str(),
|
---|
6892 | m->pParent->i_getId().raw(),
|
---|
6893 | pVirtualBox->i_settingsFilePath().c_str());
|
---|
6894 | parentLock.release();
|
---|
6895 | treeLock.release();
|
---|
6896 | throw S_OK;
|
---|
6897 | }
|
---|
6898 | }
|
---|
6899 |
|
---|
6900 | /// @todo NEWMEDIA what to do if the parent is not
|
---|
6901 | /// accessible while the diff is? Probably nothing. The
|
---|
6902 | /// real code will detect the mismatch anyway.
|
---|
6903 |
|
---|
6904 | treeLock.release();
|
---|
6905 | }
|
---|
6906 | }
|
---|
6907 |
|
---|
6908 | mediumSize = VDGetFileSize(hdd, 0);
|
---|
6909 | mediumLogicalSize = VDGetSize(hdd, 0);
|
---|
6910 |
|
---|
6911 | success = true;
|
---|
6912 | }
|
---|
6913 | catch (HRESULT aRC)
|
---|
6914 | {
|
---|
6915 | rc = aRC;
|
---|
6916 | }
|
---|
6917 |
|
---|
6918 | vrc = VDDestroy(hdd);
|
---|
6919 | if (RT_FAILURE(vrc))
|
---|
6920 | {
|
---|
6921 | lastAccessError = Utf8StrFmt(tr("Could not update and close the medium '%s'%s"),
|
---|
6922 | location.c_str(), i_vdError(vrc).c_str());
|
---|
6923 | success = false;
|
---|
6924 | throw S_OK;
|
---|
6925 | }
|
---|
6926 | }
|
---|
6927 | catch (HRESULT aRC)
|
---|
6928 | {
|
---|
6929 | rc = aRC;
|
---|
6930 | }
|
---|
6931 |
|
---|
6932 | autoCaller.release();
|
---|
6933 | treeLock.acquire();
|
---|
6934 | autoCaller.add();
|
---|
6935 | if (FAILED(autoCaller.rc()))
|
---|
6936 | {
|
---|
6937 | m->queryInfoRunning = false;
|
---|
6938 | return autoCaller.rc();
|
---|
6939 | }
|
---|
6940 | alock.acquire();
|
---|
6941 |
|
---|
6942 | if (success)
|
---|
6943 | {
|
---|
6944 | m->size = mediumSize;
|
---|
6945 | m->logicalSize = mediumLogicalSize;
|
---|
6946 | m->strLastAccessError.setNull();
|
---|
6947 | }
|
---|
6948 | else
|
---|
6949 | {
|
---|
6950 | m->strLastAccessError = lastAccessError;
|
---|
6951 | Log1WarningFunc(("'%s' is not accessible (error='%s', rc=%Rhrc, vrc=%Rrc)\n",
|
---|
6952 | location.c_str(), m->strLastAccessError.c_str(), rc, vrc));
|
---|
6953 | }
|
---|
6954 |
|
---|
6955 | /* Set the proper state according to the result of the check */
|
---|
6956 | if (success)
|
---|
6957 | m->preLockState = MediumState_Created;
|
---|
6958 | else
|
---|
6959 | m->preLockState = MediumState_Inaccessible;
|
---|
6960 |
|
---|
6961 | /* unblock anyone waiting for the i_queryInfo results */
|
---|
6962 | qlock.release();
|
---|
6963 | m->queryInfoRunning = false;
|
---|
6964 |
|
---|
6965 | pToken->Abandon();
|
---|
6966 | pToken.setNull();
|
---|
6967 |
|
---|
6968 | if (FAILED(rc)) return rc;
|
---|
6969 |
|
---|
6970 | /* If this is a base image which incorrectly has a parent UUID set,
|
---|
6971 | * repair the image now by zeroing the parent UUID. This is only done
|
---|
6972 | * when we have structural information from a config file, on import
|
---|
6973 | * this is not possible. If someone would accidentally call openMedium
|
---|
6974 | * with a diff image before the base is registered this would destroy
|
---|
6975 | * the diff. Not acceptable. */
|
---|
6976 | if (fRepairImageZeroParentUuid)
|
---|
6977 | {
|
---|
6978 | rc = LockWrite(pToken.asOutParam());
|
---|
6979 | if (FAILED(rc)) return rc;
|
---|
6980 |
|
---|
6981 | alock.release();
|
---|
6982 |
|
---|
6983 | try
|
---|
6984 | {
|
---|
6985 | PVBOXHDD hdd;
|
---|
6986 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
6987 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6988 |
|
---|
6989 | try
|
---|
6990 | {
|
---|
6991 | vrc = VDOpen(hdd,
|
---|
6992 | format.c_str(),
|
---|
6993 | location.c_str(),
|
---|
6994 | (uOpenFlags & ~VD_OPEN_FLAGS_READONLY) | m->uOpenFlagsDef,
|
---|
6995 | m->vdImageIfaces);
|
---|
6996 | if (RT_FAILURE(vrc))
|
---|
6997 | throw S_OK;
|
---|
6998 |
|
---|
6999 | RTUUID zeroParentUuid;
|
---|
7000 | RTUuidClear(&zeroParentUuid);
|
---|
7001 | vrc = VDSetParentUuid(hdd, 0, &zeroParentUuid);
|
---|
7002 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7003 | }
|
---|
7004 | catch (HRESULT aRC)
|
---|
7005 | {
|
---|
7006 | rc = aRC;
|
---|
7007 | }
|
---|
7008 |
|
---|
7009 | VDDestroy(hdd);
|
---|
7010 | }
|
---|
7011 | catch (HRESULT aRC)
|
---|
7012 | {
|
---|
7013 | rc = aRC;
|
---|
7014 | }
|
---|
7015 |
|
---|
7016 | pToken->Abandon();
|
---|
7017 | pToken.setNull();
|
---|
7018 | if (FAILED(rc)) return rc;
|
---|
7019 | }
|
---|
7020 |
|
---|
7021 | return rc;
|
---|
7022 | }
|
---|
7023 |
|
---|
7024 | /**
|
---|
7025 | * Performs extra checks if the medium can be closed and returns S_OK in
|
---|
7026 | * this case. Otherwise, returns a respective error message. Called by
|
---|
7027 | * Close() under the medium tree lock and the medium lock.
|
---|
7028 | *
|
---|
7029 | * @note Also reused by Medium::Reset().
|
---|
7030 | *
|
---|
7031 | * @note Caller must hold the media tree write lock!
|
---|
7032 | */
|
---|
7033 | HRESULT Medium::i_canClose()
|
---|
7034 | {
|
---|
7035 | Assert(m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
7036 |
|
---|
7037 | if (i_getChildren().size() != 0)
|
---|
7038 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
7039 | tr("Cannot close medium '%s' because it has %d child media"),
|
---|
7040 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
7041 |
|
---|
7042 | return S_OK;
|
---|
7043 | }
|
---|
7044 |
|
---|
7045 | /**
|
---|
7046 | * Unregisters this medium with mVirtualBox. Called by close() under the medium tree lock.
|
---|
7047 | *
|
---|
7048 | * @note Caller must have locked the media tree lock for writing!
|
---|
7049 | */
|
---|
7050 | HRESULT Medium::i_unregisterWithVirtualBox()
|
---|
7051 | {
|
---|
7052 | /* Note that we need to de-associate ourselves from the parent to let
|
---|
7053 | * VirtualBox::i_unregisterMedium() properly save the registry */
|
---|
7054 |
|
---|
7055 | /* we modify m->pParent and access children */
|
---|
7056 | Assert(m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
7057 |
|
---|
7058 | Medium *pParentBackup = m->pParent;
|
---|
7059 | AssertReturn(i_getChildren().size() == 0, E_FAIL);
|
---|
7060 | if (m->pParent)
|
---|
7061 | i_deparent();
|
---|
7062 |
|
---|
7063 | HRESULT rc = m->pVirtualBox->i_unregisterMedium(this);
|
---|
7064 | if (FAILED(rc))
|
---|
7065 | {
|
---|
7066 | if (pParentBackup)
|
---|
7067 | {
|
---|
7068 | // re-associate with the parent as we are still relatives in the registry
|
---|
7069 | i_setParent(pParentBackup);
|
---|
7070 | }
|
---|
7071 | }
|
---|
7072 |
|
---|
7073 | return rc;
|
---|
7074 | }
|
---|
7075 |
|
---|
7076 | /**
|
---|
7077 | * Like SetProperty but do not trigger a settings store. Only for internal use!
|
---|
7078 | */
|
---|
7079 | HRESULT Medium::i_setPropertyDirect(const Utf8Str &aName, const Utf8Str &aValue)
|
---|
7080 | {
|
---|
7081 | AutoCaller autoCaller(this);
|
---|
7082 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
7083 |
|
---|
7084 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7085 |
|
---|
7086 | switch (m->state)
|
---|
7087 | {
|
---|
7088 | case MediumState_Created:
|
---|
7089 | case MediumState_Inaccessible:
|
---|
7090 | break;
|
---|
7091 | default:
|
---|
7092 | return i_setStateError();
|
---|
7093 | }
|
---|
7094 |
|
---|
7095 | m->mapProperties[aName] = aValue;
|
---|
7096 |
|
---|
7097 | return S_OK;
|
---|
7098 | }
|
---|
7099 |
|
---|
7100 | /**
|
---|
7101 | * Sets the extended error info according to the current media state.
|
---|
7102 | *
|
---|
7103 | * @note Must be called from under this object's write or read lock.
|
---|
7104 | */
|
---|
7105 | HRESULT Medium::i_setStateError()
|
---|
7106 | {
|
---|
7107 | HRESULT rc = E_FAIL;
|
---|
7108 |
|
---|
7109 | switch (m->state)
|
---|
7110 | {
|
---|
7111 | case MediumState_NotCreated:
|
---|
7112 | {
|
---|
7113 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7114 | tr("Storage for the medium '%s' is not created"),
|
---|
7115 | m->strLocationFull.c_str());
|
---|
7116 | break;
|
---|
7117 | }
|
---|
7118 | case MediumState_Created:
|
---|
7119 | {
|
---|
7120 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7121 | tr("Storage for the medium '%s' is already created"),
|
---|
7122 | m->strLocationFull.c_str());
|
---|
7123 | break;
|
---|
7124 | }
|
---|
7125 | case MediumState_LockedRead:
|
---|
7126 | {
|
---|
7127 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7128 | tr("Medium '%s' is locked for reading by another task"),
|
---|
7129 | m->strLocationFull.c_str());
|
---|
7130 | break;
|
---|
7131 | }
|
---|
7132 | case MediumState_LockedWrite:
|
---|
7133 | {
|
---|
7134 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7135 | tr("Medium '%s' is locked for writing by another task"),
|
---|
7136 | m->strLocationFull.c_str());
|
---|
7137 | break;
|
---|
7138 | }
|
---|
7139 | case MediumState_Inaccessible:
|
---|
7140 | {
|
---|
7141 | /* be in sync with Console::powerUpThread() */
|
---|
7142 | if (!m->strLastAccessError.isEmpty())
|
---|
7143 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7144 | tr("Medium '%s' is not accessible. %s"),
|
---|
7145 | m->strLocationFull.c_str(), m->strLastAccessError.c_str());
|
---|
7146 | else
|
---|
7147 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7148 | tr("Medium '%s' is not accessible"),
|
---|
7149 | m->strLocationFull.c_str());
|
---|
7150 | break;
|
---|
7151 | }
|
---|
7152 | case MediumState_Creating:
|
---|
7153 | {
|
---|
7154 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7155 | tr("Storage for the medium '%s' is being created"),
|
---|
7156 | m->strLocationFull.c_str());
|
---|
7157 | break;
|
---|
7158 | }
|
---|
7159 | case MediumState_Deleting:
|
---|
7160 | {
|
---|
7161 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7162 | tr("Storage for the medium '%s' is being deleted"),
|
---|
7163 | m->strLocationFull.c_str());
|
---|
7164 | break;
|
---|
7165 | }
|
---|
7166 | default:
|
---|
7167 | {
|
---|
7168 | AssertFailed();
|
---|
7169 | break;
|
---|
7170 | }
|
---|
7171 | }
|
---|
7172 |
|
---|
7173 | return rc;
|
---|
7174 | }
|
---|
7175 |
|
---|
7176 | /**
|
---|
7177 | * Sets the value of m->strLocationFull. The given location must be a fully
|
---|
7178 | * qualified path; relative paths are not supported here.
|
---|
7179 | *
|
---|
7180 | * As a special exception, if the specified location is a file path that ends with '/'
|
---|
7181 | * then the file name part will be generated by this method automatically in the format
|
---|
7182 | * '{<uuid>}.<ext>' where <uuid> is a fresh UUID that this method will generate
|
---|
7183 | * and assign to this medium, and <ext> is the default extension for this
|
---|
7184 | * medium's storage format. Note that this procedure requires the media state to
|
---|
7185 | * be NotCreated and will return a failure otherwise.
|
---|
7186 | *
|
---|
7187 | * @param aLocation Location of the storage unit. If the location is a FS-path,
|
---|
7188 | * then it can be relative to the VirtualBox home directory.
|
---|
7189 | * @param aFormat Optional fallback format if it is an import and the format
|
---|
7190 | * cannot be determined.
|
---|
7191 | *
|
---|
7192 | * @note Must be called from under this object's write lock.
|
---|
7193 | */
|
---|
7194 | HRESULT Medium::i_setLocation(const Utf8Str &aLocation,
|
---|
7195 | const Utf8Str &aFormat /* = Utf8Str::Empty */)
|
---|
7196 | {
|
---|
7197 | AssertReturn(!aLocation.isEmpty(), E_FAIL);
|
---|
7198 |
|
---|
7199 | AutoCaller autoCaller(this);
|
---|
7200 | AssertComRCReturnRC(autoCaller.rc());
|
---|
7201 |
|
---|
7202 | /* formatObj may be null only when initializing from an existing path and
|
---|
7203 | * no format is known yet */
|
---|
7204 | AssertReturn( (!m->strFormat.isEmpty() && !m->formatObj.isNull())
|
---|
7205 | || ( getObjectState().getState() == ObjectState::InInit
|
---|
7206 | && m->state != MediumState_NotCreated
|
---|
7207 | && m->id.isZero()
|
---|
7208 | && m->strFormat.isEmpty()
|
---|
7209 | && m->formatObj.isNull()),
|
---|
7210 | E_FAIL);
|
---|
7211 |
|
---|
7212 | /* are we dealing with a new medium constructed using the existing
|
---|
7213 | * location? */
|
---|
7214 | bool isImport = m->strFormat.isEmpty();
|
---|
7215 |
|
---|
7216 | if ( isImport
|
---|
7217 | || ( (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
7218 | && !m->hostDrive))
|
---|
7219 | {
|
---|
7220 | Guid id;
|
---|
7221 |
|
---|
7222 | Utf8Str locationFull(aLocation);
|
---|
7223 |
|
---|
7224 | if (m->state == MediumState_NotCreated)
|
---|
7225 | {
|
---|
7226 | /* must be a file (formatObj must be already known) */
|
---|
7227 | Assert(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File);
|
---|
7228 |
|
---|
7229 | if (RTPathFilename(aLocation.c_str()) == NULL)
|
---|
7230 | {
|
---|
7231 | /* no file name is given (either an empty string or ends with a
|
---|
7232 | * slash), generate a new UUID + file name if the state allows
|
---|
7233 | * this */
|
---|
7234 |
|
---|
7235 | ComAssertMsgRet(!m->formatObj->i_getFileExtensions().empty(),
|
---|
7236 | ("Must be at least one extension if it is MediumFormatCapabilities_File\n"),
|
---|
7237 | E_FAIL);
|
---|
7238 |
|
---|
7239 | Utf8Str strExt = m->formatObj->i_getFileExtensions().front();
|
---|
7240 | ComAssertMsgRet(!strExt.isEmpty(),
|
---|
7241 | ("Default extension must not be empty\n"),
|
---|
7242 | E_FAIL);
|
---|
7243 |
|
---|
7244 | id.create();
|
---|
7245 |
|
---|
7246 | locationFull = Utf8StrFmt("%s{%RTuuid}.%s",
|
---|
7247 | aLocation.c_str(), id.raw(), strExt.c_str());
|
---|
7248 | }
|
---|
7249 | }
|
---|
7250 |
|
---|
7251 | // we must always have full paths now (if it refers to a file)
|
---|
7252 | if ( ( m->formatObj.isNull()
|
---|
7253 | || m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
7254 | && !RTPathStartsWithRoot(locationFull.c_str()))
|
---|
7255 | return setError(VBOX_E_FILE_ERROR,
|
---|
7256 | tr("The given path '%s' is not fully qualified"),
|
---|
7257 | locationFull.c_str());
|
---|
7258 |
|
---|
7259 | /* detect the backend from the storage unit if importing */
|
---|
7260 | if (isImport)
|
---|
7261 | {
|
---|
7262 | VDTYPE enmType = VDTYPE_INVALID;
|
---|
7263 | char *backendName = NULL;
|
---|
7264 |
|
---|
7265 | int vrc = VINF_SUCCESS;
|
---|
7266 |
|
---|
7267 | /* is it a file? */
|
---|
7268 | {
|
---|
7269 | RTFILE file;
|
---|
7270 | vrc = RTFileOpen(&file, locationFull.c_str(), RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
7271 | if (RT_SUCCESS(vrc))
|
---|
7272 | RTFileClose(file);
|
---|
7273 | }
|
---|
7274 | if (RT_SUCCESS(vrc))
|
---|
7275 | {
|
---|
7276 | vrc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
|
---|
7277 | locationFull.c_str(), &backendName, &enmType);
|
---|
7278 | }
|
---|
7279 | else if ( vrc != VERR_FILE_NOT_FOUND
|
---|
7280 | && vrc != VERR_PATH_NOT_FOUND
|
---|
7281 | && vrc != VERR_ACCESS_DENIED
|
---|
7282 | && locationFull != aLocation)
|
---|
7283 | {
|
---|
7284 | /* assume it's not a file, restore the original location */
|
---|
7285 | locationFull = aLocation;
|
---|
7286 | vrc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
|
---|
7287 | locationFull.c_str(), &backendName, &enmType);
|
---|
7288 | }
|
---|
7289 |
|
---|
7290 | if (RT_FAILURE(vrc))
|
---|
7291 | {
|
---|
7292 | if (vrc == VERR_ACCESS_DENIED)
|
---|
7293 | return setError(VBOX_E_FILE_ERROR,
|
---|
7294 | tr("Permission problem accessing the file for the medium '%s' (%Rrc)"),
|
---|
7295 | locationFull.c_str(), vrc);
|
---|
7296 | else if (vrc == VERR_FILE_NOT_FOUND || vrc == VERR_PATH_NOT_FOUND)
|
---|
7297 | return setError(VBOX_E_FILE_ERROR,
|
---|
7298 | tr("Could not find file for the medium '%s' (%Rrc)"),
|
---|
7299 | locationFull.c_str(), vrc);
|
---|
7300 | else if (aFormat.isEmpty())
|
---|
7301 | return setError(VBOX_E_IPRT_ERROR,
|
---|
7302 | tr("Could not get the storage format of the medium '%s' (%Rrc)"),
|
---|
7303 | locationFull.c_str(), vrc);
|
---|
7304 | else
|
---|
7305 | {
|
---|
7306 | HRESULT rc = i_setFormat(aFormat);
|
---|
7307 | /* setFormat() must not fail since we've just used the backend so
|
---|
7308 | * the format object must be there */
|
---|
7309 | AssertComRCReturnRC(rc);
|
---|
7310 | }
|
---|
7311 | }
|
---|
7312 | else if ( enmType == VDTYPE_INVALID
|
---|
7313 | || m->devType != i_convertToDeviceType(enmType))
|
---|
7314 | {
|
---|
7315 | /*
|
---|
7316 | * The user tried to use a image as a device which is not supported
|
---|
7317 | * by the backend.
|
---|
7318 | */
|
---|
7319 | return setError(E_FAIL,
|
---|
7320 | tr("The medium '%s' can't be used as the requested device type"),
|
---|
7321 | locationFull.c_str());
|
---|
7322 | }
|
---|
7323 | else
|
---|
7324 | {
|
---|
7325 | ComAssertRet(backendName != NULL && *backendName != '\0', E_FAIL);
|
---|
7326 |
|
---|
7327 | HRESULT rc = i_setFormat(backendName);
|
---|
7328 | RTStrFree(backendName);
|
---|
7329 |
|
---|
7330 | /* setFormat() must not fail since we've just used the backend so
|
---|
7331 | * the format object must be there */
|
---|
7332 | AssertComRCReturnRC(rc);
|
---|
7333 | }
|
---|
7334 | }
|
---|
7335 |
|
---|
7336 | m->strLocationFull = locationFull;
|
---|
7337 |
|
---|
7338 | /* is it still a file? */
|
---|
7339 | if ( (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
7340 | && (m->state == MediumState_NotCreated)
|
---|
7341 | )
|
---|
7342 | /* assign a new UUID (this UUID will be used when calling
|
---|
7343 | * VDCreateBase/VDCreateDiff as a wanted UUID). Note that we
|
---|
7344 | * also do that if we didn't generate it to make sure it is
|
---|
7345 | * either generated by us or reset to null */
|
---|
7346 | unconst(m->id) = id;
|
---|
7347 | }
|
---|
7348 | else
|
---|
7349 | m->strLocationFull = aLocation;
|
---|
7350 |
|
---|
7351 | return S_OK;
|
---|
7352 | }
|
---|
7353 |
|
---|
7354 | /**
|
---|
7355 | * Checks that the format ID is valid and sets it on success.
|
---|
7356 | *
|
---|
7357 | * Note that this method will caller-reference the format object on success!
|
---|
7358 | * This reference must be released somewhere to let the MediumFormat object be
|
---|
7359 | * uninitialized.
|
---|
7360 | *
|
---|
7361 | * @note Must be called from under this object's write lock.
|
---|
7362 | */
|
---|
7363 | HRESULT Medium::i_setFormat(const Utf8Str &aFormat)
|
---|
7364 | {
|
---|
7365 | /* get the format object first */
|
---|
7366 | {
|
---|
7367 | SystemProperties *pSysProps = m->pVirtualBox->i_getSystemProperties();
|
---|
7368 | AutoReadLock propsLock(pSysProps COMMA_LOCKVAL_SRC_POS);
|
---|
7369 |
|
---|
7370 | unconst(m->formatObj) = pSysProps->i_mediumFormat(aFormat);
|
---|
7371 | if (m->formatObj.isNull())
|
---|
7372 | return setError(E_INVALIDARG,
|
---|
7373 | tr("Invalid medium storage format '%s'"),
|
---|
7374 | aFormat.c_str());
|
---|
7375 |
|
---|
7376 | /* get properties (preinsert them as keys in the map). Note that the
|
---|
7377 | * map doesn't grow over the object life time since the set of
|
---|
7378 | * properties is meant to be constant. */
|
---|
7379 |
|
---|
7380 | Assert(m->mapProperties.empty());
|
---|
7381 |
|
---|
7382 | for (MediumFormat::PropertyArray::const_iterator it = m->formatObj->i_getProperties().begin();
|
---|
7383 | it != m->formatObj->i_getProperties().end();
|
---|
7384 | ++it)
|
---|
7385 | {
|
---|
7386 | m->mapProperties.insert(std::make_pair(it->strName, Utf8Str::Empty));
|
---|
7387 | }
|
---|
7388 | }
|
---|
7389 |
|
---|
7390 | unconst(m->strFormat) = aFormat;
|
---|
7391 |
|
---|
7392 | return S_OK;
|
---|
7393 | }
|
---|
7394 |
|
---|
7395 | /**
|
---|
7396 | * Converts the Medium device type to the VD type.
|
---|
7397 | */
|
---|
7398 | VDTYPE Medium::i_convertDeviceType()
|
---|
7399 | {
|
---|
7400 | VDTYPE enmType;
|
---|
7401 |
|
---|
7402 | switch (m->devType)
|
---|
7403 | {
|
---|
7404 | case DeviceType_HardDisk:
|
---|
7405 | enmType = VDTYPE_HDD;
|
---|
7406 | break;
|
---|
7407 | case DeviceType_DVD:
|
---|
7408 | enmType = VDTYPE_DVD;
|
---|
7409 | break;
|
---|
7410 | case DeviceType_Floppy:
|
---|
7411 | enmType = VDTYPE_FLOPPY;
|
---|
7412 | break;
|
---|
7413 | default:
|
---|
7414 | ComAssertFailedRet(VDTYPE_INVALID);
|
---|
7415 | }
|
---|
7416 |
|
---|
7417 | return enmType;
|
---|
7418 | }
|
---|
7419 |
|
---|
7420 | /**
|
---|
7421 | * Converts from the VD type to the medium type.
|
---|
7422 | */
|
---|
7423 | DeviceType_T Medium::i_convertToDeviceType(VDTYPE enmType)
|
---|
7424 | {
|
---|
7425 | DeviceType_T devType;
|
---|
7426 |
|
---|
7427 | switch (enmType)
|
---|
7428 | {
|
---|
7429 | case VDTYPE_HDD:
|
---|
7430 | devType = DeviceType_HardDisk;
|
---|
7431 | break;
|
---|
7432 | case VDTYPE_DVD:
|
---|
7433 | devType = DeviceType_DVD;
|
---|
7434 | break;
|
---|
7435 | case VDTYPE_FLOPPY:
|
---|
7436 | devType = DeviceType_Floppy;
|
---|
7437 | break;
|
---|
7438 | default:
|
---|
7439 | ComAssertFailedRet(DeviceType_Null);
|
---|
7440 | }
|
---|
7441 |
|
---|
7442 | return devType;
|
---|
7443 | }
|
---|
7444 |
|
---|
7445 | /**
|
---|
7446 | * Internal method which checks whether a property name is for a filter plugin.
|
---|
7447 | */
|
---|
7448 | bool Medium::i_isPropertyForFilter(const com::Utf8Str &aName)
|
---|
7449 | {
|
---|
7450 | /* If the name contains "/" use the part before as a filter name and lookup the filter. */
|
---|
7451 | size_t offSlash;
|
---|
7452 | if ((offSlash = aName.find("/", 0)) != aName.npos)
|
---|
7453 | {
|
---|
7454 | com::Utf8Str strFilter;
|
---|
7455 | com::Utf8Str strKey;
|
---|
7456 |
|
---|
7457 | HRESULT rc = strFilter.assignEx(aName, 0, offSlash);
|
---|
7458 | if (FAILED(rc))
|
---|
7459 | return false;
|
---|
7460 |
|
---|
7461 | rc = strKey.assignEx(aName, offSlash + 1, aName.length() - offSlash - 1); /* Skip slash */
|
---|
7462 | if (FAILED(rc))
|
---|
7463 | return false;
|
---|
7464 |
|
---|
7465 | VDFILTERINFO FilterInfo;
|
---|
7466 | int vrc = VDFilterInfoOne(strFilter.c_str(), &FilterInfo);
|
---|
7467 | if (RT_SUCCESS(vrc))
|
---|
7468 | {
|
---|
7469 | /* Check that the property exists. */
|
---|
7470 | PCVDCONFIGINFO paConfig = FilterInfo.paConfigInfo;
|
---|
7471 | while (paConfig->pszKey)
|
---|
7472 | {
|
---|
7473 | if (strKey.equals(paConfig->pszKey))
|
---|
7474 | return true;
|
---|
7475 | paConfig++;
|
---|
7476 | }
|
---|
7477 | }
|
---|
7478 | }
|
---|
7479 |
|
---|
7480 | return false;
|
---|
7481 | }
|
---|
7482 |
|
---|
7483 | /**
|
---|
7484 | * Returns the last error message collected by the i_vdErrorCall callback and
|
---|
7485 | * resets it.
|
---|
7486 | *
|
---|
7487 | * The error message is returned prepended with a dot and a space, like this:
|
---|
7488 | * <code>
|
---|
7489 | * ". <error_text> (%Rrc)"
|
---|
7490 | * </code>
|
---|
7491 | * to make it easily appendable to a more general error message. The @c %Rrc
|
---|
7492 | * format string is given @a aVRC as an argument.
|
---|
7493 | *
|
---|
7494 | * If there is no last error message collected by i_vdErrorCall or if it is a
|
---|
7495 | * null or empty string, then this function returns the following text:
|
---|
7496 | * <code>
|
---|
7497 | * " (%Rrc)"
|
---|
7498 | * </code>
|
---|
7499 | *
|
---|
7500 | * @note Doesn't do any object locking; it is assumed that the caller makes sure
|
---|
7501 | * the callback isn't called by more than one thread at a time.
|
---|
7502 | *
|
---|
7503 | * @param aVRC VBox error code to use when no error message is provided.
|
---|
7504 | */
|
---|
7505 | Utf8Str Medium::i_vdError(int aVRC)
|
---|
7506 | {
|
---|
7507 | Utf8Str error;
|
---|
7508 |
|
---|
7509 | if (m->vdError.isEmpty())
|
---|
7510 | error = Utf8StrFmt(" (%Rrc)", aVRC);
|
---|
7511 | else
|
---|
7512 | error = Utf8StrFmt(".\n%s", m->vdError.c_str());
|
---|
7513 |
|
---|
7514 | m->vdError.setNull();
|
---|
7515 |
|
---|
7516 | return error;
|
---|
7517 | }
|
---|
7518 |
|
---|
7519 | /**
|
---|
7520 | * Error message callback.
|
---|
7521 | *
|
---|
7522 | * Puts the reported error message to the m->vdError field.
|
---|
7523 | *
|
---|
7524 | * @note Doesn't do any object locking; it is assumed that the caller makes sure
|
---|
7525 | * the callback isn't called by more than one thread at a time.
|
---|
7526 | *
|
---|
7527 | * @param pvUser The opaque data passed on container creation.
|
---|
7528 | * @param rc The VBox error code.
|
---|
7529 | * @param RT_SRC_POS_DECL Use RT_SRC_POS.
|
---|
7530 | * @param pszFormat Error message format string.
|
---|
7531 | * @param va Error message arguments.
|
---|
7532 | */
|
---|
7533 | /*static*/
|
---|
7534 | DECLCALLBACK(void) Medium::i_vdErrorCall(void *pvUser, int rc, RT_SRC_POS_DECL,
|
---|
7535 | const char *pszFormat, va_list va)
|
---|
7536 | {
|
---|
7537 | NOREF(pszFile); NOREF(iLine); NOREF(pszFunction); /* RT_SRC_POS_DECL */
|
---|
7538 |
|
---|
7539 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
7540 | AssertReturnVoid(that != NULL);
|
---|
7541 |
|
---|
7542 | if (that->m->vdError.isEmpty())
|
---|
7543 | that->m->vdError =
|
---|
7544 | Utf8StrFmt("%s (%Rrc)", Utf8Str(pszFormat, va).c_str(), rc);
|
---|
7545 | else
|
---|
7546 | that->m->vdError =
|
---|
7547 | Utf8StrFmt("%s.\n%s (%Rrc)", that->m->vdError.c_str(),
|
---|
7548 | Utf8Str(pszFormat, va).c_str(), rc);
|
---|
7549 | }
|
---|
7550 |
|
---|
7551 | /* static */
|
---|
7552 | DECLCALLBACK(bool) Medium::i_vdConfigAreKeysValid(void *pvUser,
|
---|
7553 | const char * /* pszzValid */)
|
---|
7554 | {
|
---|
7555 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
7556 | AssertReturn(that != NULL, false);
|
---|
7557 |
|
---|
7558 | /* we always return true since the only keys we have are those found in
|
---|
7559 | * VDBACKENDINFO */
|
---|
7560 | return true;
|
---|
7561 | }
|
---|
7562 |
|
---|
7563 | /* static */
|
---|
7564 | DECLCALLBACK(int) Medium::i_vdConfigQuerySize(void *pvUser,
|
---|
7565 | const char *pszName,
|
---|
7566 | size_t *pcbValue)
|
---|
7567 | {
|
---|
7568 | AssertReturn(VALID_PTR(pcbValue), VERR_INVALID_POINTER);
|
---|
7569 |
|
---|
7570 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
7571 | AssertReturn(that != NULL, VERR_GENERAL_FAILURE);
|
---|
7572 |
|
---|
7573 | settings::StringsMap::const_iterator it = that->m->mapProperties.find(Utf8Str(pszName));
|
---|
7574 | if (it == that->m->mapProperties.end())
|
---|
7575 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7576 |
|
---|
7577 | /* we interpret null values as "no value" in Medium */
|
---|
7578 | if (it->second.isEmpty())
|
---|
7579 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7580 |
|
---|
7581 | *pcbValue = it->second.length() + 1 /* include terminator */;
|
---|
7582 |
|
---|
7583 | return VINF_SUCCESS;
|
---|
7584 | }
|
---|
7585 |
|
---|
7586 | /* static */
|
---|
7587 | DECLCALLBACK(int) Medium::i_vdConfigQuery(void *pvUser,
|
---|
7588 | const char *pszName,
|
---|
7589 | char *pszValue,
|
---|
7590 | size_t cchValue)
|
---|
7591 | {
|
---|
7592 | AssertReturn(VALID_PTR(pszValue), VERR_INVALID_POINTER);
|
---|
7593 |
|
---|
7594 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
7595 | AssertReturn(that != NULL, VERR_GENERAL_FAILURE);
|
---|
7596 |
|
---|
7597 | settings::StringsMap::const_iterator it = that->m->mapProperties.find(Utf8Str(pszName));
|
---|
7598 | if (it == that->m->mapProperties.end())
|
---|
7599 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7600 |
|
---|
7601 | /* we interpret null values as "no value" in Medium */
|
---|
7602 | if (it->second.isEmpty())
|
---|
7603 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7604 |
|
---|
7605 | const Utf8Str &value = it->second;
|
---|
7606 | if (value.length() >= cchValue)
|
---|
7607 | return VERR_CFGM_NOT_ENOUGH_SPACE;
|
---|
7608 |
|
---|
7609 | memcpy(pszValue, value.c_str(), value.length() + 1);
|
---|
7610 |
|
---|
7611 | return VINF_SUCCESS;
|
---|
7612 | }
|
---|
7613 |
|
---|
7614 | DECLCALLBACK(int) Medium::i_vdTcpSocketCreate(uint32_t fFlags, PVDSOCKET pSock)
|
---|
7615 | {
|
---|
7616 | PVDSOCKETINT pSocketInt = NULL;
|
---|
7617 |
|
---|
7618 | if ((fFlags & VD_INTERFACETCPNET_CONNECT_EXTENDED_SELECT) != 0)
|
---|
7619 | return VERR_NOT_SUPPORTED;
|
---|
7620 |
|
---|
7621 | pSocketInt = (PVDSOCKETINT)RTMemAllocZ(sizeof(VDSOCKETINT));
|
---|
7622 | if (!pSocketInt)
|
---|
7623 | return VERR_NO_MEMORY;
|
---|
7624 |
|
---|
7625 | pSocketInt->hSocket = NIL_RTSOCKET;
|
---|
7626 | *pSock = pSocketInt;
|
---|
7627 | return VINF_SUCCESS;
|
---|
7628 | }
|
---|
7629 |
|
---|
7630 | DECLCALLBACK(int) Medium::i_vdTcpSocketDestroy(VDSOCKET Sock)
|
---|
7631 | {
|
---|
7632 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7633 |
|
---|
7634 | if (pSocketInt->hSocket != NIL_RTSOCKET)
|
---|
7635 | RTTcpClientCloseEx(pSocketInt->hSocket, false /*fGracefulShutdown*/);
|
---|
7636 |
|
---|
7637 | RTMemFree(pSocketInt);
|
---|
7638 |
|
---|
7639 | return VINF_SUCCESS;
|
---|
7640 | }
|
---|
7641 |
|
---|
7642 | DECLCALLBACK(int) Medium::i_vdTcpClientConnect(VDSOCKET Sock, const char *pszAddress, uint32_t uPort,
|
---|
7643 | RTMSINTERVAL cMillies)
|
---|
7644 | {
|
---|
7645 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7646 |
|
---|
7647 | return RTTcpClientConnectEx(pszAddress, uPort, &pSocketInt->hSocket, cMillies, NULL);
|
---|
7648 | }
|
---|
7649 |
|
---|
7650 | DECLCALLBACK(int) Medium::i_vdTcpClientClose(VDSOCKET Sock)
|
---|
7651 | {
|
---|
7652 | int rc = VINF_SUCCESS;
|
---|
7653 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7654 |
|
---|
7655 | rc = RTTcpClientCloseEx(pSocketInt->hSocket, false /*fGracefulShutdown*/);
|
---|
7656 | pSocketInt->hSocket = NIL_RTSOCKET;
|
---|
7657 | return rc;
|
---|
7658 | }
|
---|
7659 |
|
---|
7660 | DECLCALLBACK(bool) Medium::i_vdTcpIsClientConnected(VDSOCKET Sock)
|
---|
7661 | {
|
---|
7662 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7663 | return pSocketInt->hSocket != NIL_RTSOCKET;
|
---|
7664 | }
|
---|
7665 |
|
---|
7666 | DECLCALLBACK(int) Medium::i_vdTcpSelectOne(VDSOCKET Sock, RTMSINTERVAL cMillies)
|
---|
7667 | {
|
---|
7668 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7669 | return RTTcpSelectOne(pSocketInt->hSocket, cMillies);
|
---|
7670 | }
|
---|
7671 |
|
---|
7672 | DECLCALLBACK(int) Medium::i_vdTcpRead(VDSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead)
|
---|
7673 | {
|
---|
7674 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7675 | return RTTcpRead(pSocketInt->hSocket, pvBuffer, cbBuffer, pcbRead);
|
---|
7676 | }
|
---|
7677 |
|
---|
7678 | DECLCALLBACK(int) Medium::i_vdTcpWrite(VDSOCKET Sock, const void *pvBuffer, size_t cbBuffer)
|
---|
7679 | {
|
---|
7680 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7681 | return RTTcpWrite(pSocketInt->hSocket, pvBuffer, cbBuffer);
|
---|
7682 | }
|
---|
7683 |
|
---|
7684 | DECLCALLBACK(int) Medium::i_vdTcpSgWrite(VDSOCKET Sock, PCRTSGBUF pSgBuf)
|
---|
7685 | {
|
---|
7686 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7687 | return RTTcpSgWrite(pSocketInt->hSocket, pSgBuf);
|
---|
7688 | }
|
---|
7689 |
|
---|
7690 | DECLCALLBACK(int) Medium::i_vdTcpFlush(VDSOCKET Sock)
|
---|
7691 | {
|
---|
7692 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7693 | return RTTcpFlush(pSocketInt->hSocket);
|
---|
7694 | }
|
---|
7695 |
|
---|
7696 | DECLCALLBACK(int) Medium::i_vdTcpSetSendCoalescing(VDSOCKET Sock, bool fEnable)
|
---|
7697 | {
|
---|
7698 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7699 | return RTTcpSetSendCoalescing(pSocketInt->hSocket, fEnable);
|
---|
7700 | }
|
---|
7701 |
|
---|
7702 | DECLCALLBACK(int) Medium::i_vdTcpGetLocalAddress(VDSOCKET Sock, PRTNETADDR pAddr)
|
---|
7703 | {
|
---|
7704 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7705 | return RTTcpGetLocalAddress(pSocketInt->hSocket, pAddr);
|
---|
7706 | }
|
---|
7707 |
|
---|
7708 | DECLCALLBACK(int) Medium::i_vdTcpGetPeerAddress(VDSOCKET Sock, PRTNETADDR pAddr)
|
---|
7709 | {
|
---|
7710 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7711 | return RTTcpGetPeerAddress(pSocketInt->hSocket, pAddr);
|
---|
7712 | }
|
---|
7713 |
|
---|
7714 | DECLCALLBACK(bool) Medium::i_vdCryptoConfigAreKeysValid(void *pvUser, const char *pszzValid)
|
---|
7715 | {
|
---|
7716 | /* Just return always true here. */
|
---|
7717 | NOREF(pvUser);
|
---|
7718 | NOREF(pszzValid);
|
---|
7719 | return true;
|
---|
7720 | }
|
---|
7721 |
|
---|
7722 | DECLCALLBACK(int) Medium::i_vdCryptoConfigQuerySize(void *pvUser, const char *pszName, size_t *pcbValue)
|
---|
7723 | {
|
---|
7724 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7725 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7726 | AssertReturn(VALID_PTR(pcbValue), VERR_INVALID_POINTER);
|
---|
7727 |
|
---|
7728 | size_t cbValue = 0;
|
---|
7729 | if (!strcmp(pszName, "Algorithm"))
|
---|
7730 | cbValue = strlen(pSettings->pszCipher) + 1;
|
---|
7731 | else if (!strcmp(pszName, "KeyId"))
|
---|
7732 | cbValue = sizeof("irrelevant");
|
---|
7733 | else if (!strcmp(pszName, "KeyStore"))
|
---|
7734 | {
|
---|
7735 | if (!pSettings->pszKeyStoreLoad)
|
---|
7736 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7737 | cbValue = strlen(pSettings->pszKeyStoreLoad) + 1;
|
---|
7738 | }
|
---|
7739 | else if (!strcmp(pszName, "CreateKeyStore"))
|
---|
7740 | cbValue = 2; /* Single digit + terminator. */
|
---|
7741 | else
|
---|
7742 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7743 |
|
---|
7744 | *pcbValue = cbValue + 1 /* include terminator */;
|
---|
7745 |
|
---|
7746 | return VINF_SUCCESS;
|
---|
7747 | }
|
---|
7748 |
|
---|
7749 | DECLCALLBACK(int) Medium::i_vdCryptoConfigQuery(void *pvUser, const char *pszName,
|
---|
7750 | char *pszValue, size_t cchValue)
|
---|
7751 | {
|
---|
7752 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7753 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7754 | AssertReturn(VALID_PTR(pszValue), VERR_INVALID_POINTER);
|
---|
7755 |
|
---|
7756 | const char *psz = NULL;
|
---|
7757 | if (!strcmp(pszName, "Algorithm"))
|
---|
7758 | psz = pSettings->pszCipher;
|
---|
7759 | else if (!strcmp(pszName, "KeyId"))
|
---|
7760 | psz = "irrelevant";
|
---|
7761 | else if (!strcmp(pszName, "KeyStore"))
|
---|
7762 | psz = pSettings->pszKeyStoreLoad;
|
---|
7763 | else if (!strcmp(pszName, "CreateKeyStore"))
|
---|
7764 | {
|
---|
7765 | if (pSettings->fCreateKeyStore)
|
---|
7766 | psz = "1";
|
---|
7767 | else
|
---|
7768 | psz = "0";
|
---|
7769 | }
|
---|
7770 | else
|
---|
7771 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7772 |
|
---|
7773 | size_t cch = strlen(psz);
|
---|
7774 | if (cch >= cchValue)
|
---|
7775 | return VERR_CFGM_NOT_ENOUGH_SPACE;
|
---|
7776 |
|
---|
7777 | memcpy(pszValue, psz, cch + 1);
|
---|
7778 | return VINF_SUCCESS;
|
---|
7779 | }
|
---|
7780 |
|
---|
7781 | DECLCALLBACK(int) Medium::i_vdCryptoKeyRetain(void *pvUser, const char *pszId,
|
---|
7782 | const uint8_t **ppbKey, size_t *pcbKey)
|
---|
7783 | {
|
---|
7784 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7785 | NOREF(pszId);
|
---|
7786 | NOREF(ppbKey);
|
---|
7787 | NOREF(pcbKey);
|
---|
7788 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7789 | AssertMsgFailedReturn(("This method should not be called here!\n"), VERR_INVALID_STATE);
|
---|
7790 | }
|
---|
7791 |
|
---|
7792 | DECLCALLBACK(int) Medium::i_vdCryptoKeyRelease(void *pvUser, const char *pszId)
|
---|
7793 | {
|
---|
7794 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7795 | NOREF(pszId);
|
---|
7796 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7797 | AssertMsgFailedReturn(("This method should not be called here!\n"), VERR_INVALID_STATE);
|
---|
7798 | }
|
---|
7799 |
|
---|
7800 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStorePasswordRetain(void *pvUser, const char *pszId, const char **ppszPassword)
|
---|
7801 | {
|
---|
7802 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7803 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7804 |
|
---|
7805 | NOREF(pszId);
|
---|
7806 | *ppszPassword = pSettings->pszPassword;
|
---|
7807 | return VINF_SUCCESS;
|
---|
7808 | }
|
---|
7809 |
|
---|
7810 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStorePasswordRelease(void *pvUser, const char *pszId)
|
---|
7811 | {
|
---|
7812 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7813 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7814 | NOREF(pszId);
|
---|
7815 | return VINF_SUCCESS;
|
---|
7816 | }
|
---|
7817 |
|
---|
7818 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStoreSave(void *pvUser, const void *pvKeyStore, size_t cbKeyStore)
|
---|
7819 | {
|
---|
7820 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7821 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7822 |
|
---|
7823 | pSettings->pszKeyStore = (char *)RTMemAllocZ(cbKeyStore);
|
---|
7824 | if (!pSettings->pszKeyStore)
|
---|
7825 | return VERR_NO_MEMORY;
|
---|
7826 |
|
---|
7827 | memcpy(pSettings->pszKeyStore, pvKeyStore, cbKeyStore);
|
---|
7828 | return VINF_SUCCESS;
|
---|
7829 | }
|
---|
7830 |
|
---|
7831 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStoreReturnParameters(void *pvUser, const char *pszCipher,
|
---|
7832 | const uint8_t *pbDek, size_t cbDek)
|
---|
7833 | {
|
---|
7834 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7835 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7836 |
|
---|
7837 | pSettings->pszCipherReturned = RTStrDup(pszCipher);
|
---|
7838 | pSettings->pbDek = pbDek;
|
---|
7839 | pSettings->cbDek = cbDek;
|
---|
7840 |
|
---|
7841 | return pSettings->pszCipherReturned ? VINF_SUCCESS : VERR_NO_MEMORY;
|
---|
7842 | }
|
---|
7843 |
|
---|
7844 | /**
|
---|
7845 | * Implementation code for the "create base" task.
|
---|
7846 | *
|
---|
7847 | * This only gets started from Medium::CreateBaseStorage() and always runs
|
---|
7848 | * asynchronously. As a result, we always save the VirtualBox.xml file when
|
---|
7849 | * we're done here.
|
---|
7850 | *
|
---|
7851 | * @param task
|
---|
7852 | * @return
|
---|
7853 | */
|
---|
7854 | HRESULT Medium::i_taskCreateBaseHandler(Medium::CreateBaseTask &task)
|
---|
7855 | {
|
---|
7856 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
7857 | * to lock order violations, it probably causes lock order issues related
|
---|
7858 | * to the AutoCaller usage. */
|
---|
7859 | HRESULT rc = S_OK;
|
---|
7860 |
|
---|
7861 | /* these parameters we need after creation */
|
---|
7862 | uint64_t size = 0, logicalSize = 0;
|
---|
7863 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
7864 | bool fGenerateUuid = false;
|
---|
7865 |
|
---|
7866 | try
|
---|
7867 | {
|
---|
7868 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7869 |
|
---|
7870 | /* The object may request a specific UUID (through a special form of
|
---|
7871 | * the setLocation() argument). Otherwise we have to generate it */
|
---|
7872 | Guid id = m->id;
|
---|
7873 |
|
---|
7874 | fGenerateUuid = id.isZero();
|
---|
7875 | if (fGenerateUuid)
|
---|
7876 | {
|
---|
7877 | id.create();
|
---|
7878 | /* VirtualBox::i_registerMedium() will need UUID */
|
---|
7879 | unconst(m->id) = id;
|
---|
7880 | }
|
---|
7881 |
|
---|
7882 | Utf8Str format(m->strFormat);
|
---|
7883 | Utf8Str location(m->strLocationFull);
|
---|
7884 | uint64_t capabilities = m->formatObj->i_getCapabilities();
|
---|
7885 | ComAssertThrow(capabilities & ( MediumFormatCapabilities_CreateFixed
|
---|
7886 | | MediumFormatCapabilities_CreateDynamic), E_FAIL);
|
---|
7887 | Assert(m->state == MediumState_Creating);
|
---|
7888 |
|
---|
7889 | PVBOXHDD hdd;
|
---|
7890 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
7891 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7892 |
|
---|
7893 | /* unlock before the potentially lengthy operation */
|
---|
7894 | thisLock.release();
|
---|
7895 |
|
---|
7896 | try
|
---|
7897 | {
|
---|
7898 | /* ensure the directory exists */
|
---|
7899 | if (capabilities & MediumFormatCapabilities_File)
|
---|
7900 | {
|
---|
7901 | rc = VirtualBox::i_ensureFilePathExists(location, !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
7902 | if (FAILED(rc))
|
---|
7903 | throw rc;
|
---|
7904 | }
|
---|
7905 |
|
---|
7906 | VDGEOMETRY geo = { 0, 0, 0 }; /* auto-detect */
|
---|
7907 |
|
---|
7908 | vrc = VDCreateBase(hdd,
|
---|
7909 | format.c_str(),
|
---|
7910 | location.c_str(),
|
---|
7911 | task.mSize,
|
---|
7912 | task.mVariant & ~MediumVariant_NoCreateDir,
|
---|
7913 | NULL,
|
---|
7914 | &geo,
|
---|
7915 | &geo,
|
---|
7916 | id.raw(),
|
---|
7917 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
7918 | m->vdImageIfaces,
|
---|
7919 | task.mVDOperationIfaces);
|
---|
7920 | if (RT_FAILURE(vrc))
|
---|
7921 | {
|
---|
7922 | if (vrc == VERR_VD_INVALID_TYPE)
|
---|
7923 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7924 | tr("Parameters for creating the medium storage unit '%s' are invalid%s"),
|
---|
7925 | location.c_str(), i_vdError(vrc).c_str());
|
---|
7926 | else
|
---|
7927 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7928 | tr("Could not create the medium storage unit '%s'%s"),
|
---|
7929 | location.c_str(), i_vdError(vrc).c_str());
|
---|
7930 | }
|
---|
7931 |
|
---|
7932 | size = VDGetFileSize(hdd, 0);
|
---|
7933 | logicalSize = VDGetSize(hdd, 0);
|
---|
7934 | unsigned uImageFlags;
|
---|
7935 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
7936 | if (RT_SUCCESS(vrc))
|
---|
7937 | variant = (MediumVariant_T)uImageFlags;
|
---|
7938 | }
|
---|
7939 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7940 |
|
---|
7941 | VDDestroy(hdd);
|
---|
7942 | }
|
---|
7943 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7944 |
|
---|
7945 | if (SUCCEEDED(rc))
|
---|
7946 | {
|
---|
7947 | /* register with mVirtualBox as the last step and move to
|
---|
7948 | * Created state only on success (leaving an orphan file is
|
---|
7949 | * better than breaking media registry consistency) */
|
---|
7950 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
7951 | ComObjPtr<Medium> pMedium;
|
---|
7952 | rc = m->pVirtualBox->i_registerMedium(this, &pMedium, treeLock);
|
---|
7953 | Assert(pMedium == NULL || this == pMedium);
|
---|
7954 | }
|
---|
7955 |
|
---|
7956 | // re-acquire the lock before changing state
|
---|
7957 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7958 |
|
---|
7959 | if (SUCCEEDED(rc))
|
---|
7960 | {
|
---|
7961 | m->state = MediumState_Created;
|
---|
7962 |
|
---|
7963 | m->size = size;
|
---|
7964 | m->logicalSize = logicalSize;
|
---|
7965 | m->variant = variant;
|
---|
7966 |
|
---|
7967 | thisLock.release();
|
---|
7968 | i_markRegistriesModified();
|
---|
7969 | if (task.isAsync())
|
---|
7970 | {
|
---|
7971 | // in asynchronous mode, save settings now
|
---|
7972 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
7973 | }
|
---|
7974 | }
|
---|
7975 | else
|
---|
7976 | {
|
---|
7977 | /* back to NotCreated on failure */
|
---|
7978 | m->state = MediumState_NotCreated;
|
---|
7979 |
|
---|
7980 | /* reset UUID to prevent it from being reused next time */
|
---|
7981 | if (fGenerateUuid)
|
---|
7982 | unconst(m->id).clear();
|
---|
7983 | }
|
---|
7984 |
|
---|
7985 | return rc;
|
---|
7986 | }
|
---|
7987 |
|
---|
7988 | /**
|
---|
7989 | * Implementation code for the "create diff" task.
|
---|
7990 | *
|
---|
7991 | * This task always gets started from Medium::createDiffStorage() and can run
|
---|
7992 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
7993 | * that function. If we run synchronously, the caller expects the medium
|
---|
7994 | * registry modification to be set before returning; otherwise (in asynchronous
|
---|
7995 | * mode), we save the settings ourselves.
|
---|
7996 | *
|
---|
7997 | * @param task
|
---|
7998 | * @return
|
---|
7999 | */
|
---|
8000 | HRESULT Medium::i_taskCreateDiffHandler(Medium::CreateDiffTask &task)
|
---|
8001 | {
|
---|
8002 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
8003 | * to lock order violations, it probably causes lock order issues related
|
---|
8004 | * to the AutoCaller usage. */
|
---|
8005 | HRESULT rcTmp = S_OK;
|
---|
8006 |
|
---|
8007 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
8008 |
|
---|
8009 | uint64_t size = 0, logicalSize = 0;
|
---|
8010 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
8011 | bool fGenerateUuid = false;
|
---|
8012 |
|
---|
8013 | try
|
---|
8014 | {
|
---|
8015 | if (i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
8016 | {
|
---|
8017 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8018 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8019 | 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"),
|
---|
8020 | m->strLocationFull.c_str());
|
---|
8021 | }
|
---|
8022 |
|
---|
8023 | /* Lock both in {parent,child} order. */
|
---|
8024 | AutoMultiWriteLock2 mediaLock(this, pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8025 |
|
---|
8026 | /* The object may request a specific UUID (through a special form of
|
---|
8027 | * the setLocation() argument). Otherwise we have to generate it */
|
---|
8028 | Guid targetId = pTarget->m->id;
|
---|
8029 |
|
---|
8030 | fGenerateUuid = targetId.isZero();
|
---|
8031 | if (fGenerateUuid)
|
---|
8032 | {
|
---|
8033 | targetId.create();
|
---|
8034 | /* VirtualBox::i_registerMedium() will need UUID */
|
---|
8035 | unconst(pTarget->m->id) = targetId;
|
---|
8036 | }
|
---|
8037 |
|
---|
8038 | Guid id = m->id;
|
---|
8039 |
|
---|
8040 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
8041 | Utf8Str targetLocation(pTarget->m->strLocationFull);
|
---|
8042 | uint64_t capabilities = pTarget->m->formatObj->i_getCapabilities();
|
---|
8043 | ComAssertThrow(capabilities & MediumFormatCapabilities_CreateDynamic, E_FAIL);
|
---|
8044 |
|
---|
8045 | Assert(pTarget->m->state == MediumState_Creating);
|
---|
8046 | Assert(m->state == MediumState_LockedRead);
|
---|
8047 |
|
---|
8048 | PVBOXHDD hdd;
|
---|
8049 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8050 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8051 |
|
---|
8052 | /* the two media are now protected by their non-default states;
|
---|
8053 | * unlock the media before the potentially lengthy operation */
|
---|
8054 | mediaLock.release();
|
---|
8055 |
|
---|
8056 | try
|
---|
8057 | {
|
---|
8058 | /* Open all media in the target chain but the last. */
|
---|
8059 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
8060 | task.mpMediumLockList->GetBegin();
|
---|
8061 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
8062 | task.mpMediumLockList->GetEnd();
|
---|
8063 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
8064 | it != targetListEnd;
|
---|
8065 | ++it)
|
---|
8066 | {
|
---|
8067 | const MediumLock &mediumLock = *it;
|
---|
8068 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8069 |
|
---|
8070 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8071 |
|
---|
8072 | /* Skip over the target diff medium */
|
---|
8073 | if (pMedium->m->state == MediumState_Creating)
|
---|
8074 | continue;
|
---|
8075 |
|
---|
8076 | /* sanity check */
|
---|
8077 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
8078 |
|
---|
8079 | /* Open all media in appropriate mode. */
|
---|
8080 | vrc = VDOpen(hdd,
|
---|
8081 | pMedium->m->strFormat.c_str(),
|
---|
8082 | pMedium->m->strLocationFull.c_str(),
|
---|
8083 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
8084 | pMedium->m->vdImageIfaces);
|
---|
8085 | if (RT_FAILURE(vrc))
|
---|
8086 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8087 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8088 | pMedium->m->strLocationFull.c_str(),
|
---|
8089 | i_vdError(vrc).c_str());
|
---|
8090 | }
|
---|
8091 |
|
---|
8092 | /* ensure the target directory exists */
|
---|
8093 | if (capabilities & MediumFormatCapabilities_File)
|
---|
8094 | {
|
---|
8095 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
8096 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
8097 | if (FAILED(rc))
|
---|
8098 | throw rc;
|
---|
8099 | }
|
---|
8100 |
|
---|
8101 | vrc = VDCreateDiff(hdd,
|
---|
8102 | targetFormat.c_str(),
|
---|
8103 | targetLocation.c_str(),
|
---|
8104 | (task.mVariant & ~MediumVariant_NoCreateDir) | VD_IMAGE_FLAGS_DIFF,
|
---|
8105 | NULL,
|
---|
8106 | targetId.raw(),
|
---|
8107 | id.raw(),
|
---|
8108 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
8109 | pTarget->m->vdImageIfaces,
|
---|
8110 | task.mVDOperationIfaces);
|
---|
8111 | if (RT_FAILURE(vrc))
|
---|
8112 | {
|
---|
8113 | if (vrc == VERR_VD_INVALID_TYPE)
|
---|
8114 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8115 | tr("Parameters for creating the differencing medium storage unit '%s' are invalid%s"),
|
---|
8116 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
8117 | else
|
---|
8118 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8119 | tr("Could not create the differencing medium storage unit '%s'%s"),
|
---|
8120 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
8121 | }
|
---|
8122 |
|
---|
8123 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
8124 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
8125 | unsigned uImageFlags;
|
---|
8126 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
8127 | if (RT_SUCCESS(vrc))
|
---|
8128 | variant = (MediumVariant_T)uImageFlags;
|
---|
8129 | }
|
---|
8130 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8131 |
|
---|
8132 | VDDestroy(hdd);
|
---|
8133 | }
|
---|
8134 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8135 |
|
---|
8136 | MultiResult mrc(rcTmp);
|
---|
8137 |
|
---|
8138 | if (SUCCEEDED(mrc))
|
---|
8139 | {
|
---|
8140 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
8141 |
|
---|
8142 | Assert(pTarget->m->pParent.isNull());
|
---|
8143 |
|
---|
8144 | /* associate child with the parent, maximum depth was checked above */
|
---|
8145 | pTarget->i_setParent(this);
|
---|
8146 |
|
---|
8147 | /* diffs for immutable media are auto-reset by default */
|
---|
8148 | bool fAutoReset;
|
---|
8149 | {
|
---|
8150 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
8151 | AutoReadLock block(pBase COMMA_LOCKVAL_SRC_POS);
|
---|
8152 | fAutoReset = (pBase->m->type == MediumType_Immutable);
|
---|
8153 | }
|
---|
8154 | {
|
---|
8155 | AutoWriteLock tlock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8156 | pTarget->m->autoReset = fAutoReset;
|
---|
8157 | }
|
---|
8158 |
|
---|
8159 | /* register with mVirtualBox as the last step and move to
|
---|
8160 | * Created state only on success (leaving an orphan file is
|
---|
8161 | * better than breaking media registry consistency) */
|
---|
8162 | ComObjPtr<Medium> pMedium;
|
---|
8163 | mrc = m->pVirtualBox->i_registerMedium(pTarget, &pMedium, treeLock);
|
---|
8164 | Assert(pTarget == pMedium);
|
---|
8165 |
|
---|
8166 | if (FAILED(mrc))
|
---|
8167 | /* break the parent association on failure to register */
|
---|
8168 | i_deparent();
|
---|
8169 | }
|
---|
8170 |
|
---|
8171 | AutoMultiWriteLock2 mediaLock(this, pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8172 |
|
---|
8173 | if (SUCCEEDED(mrc))
|
---|
8174 | {
|
---|
8175 | pTarget->m->state = MediumState_Created;
|
---|
8176 |
|
---|
8177 | pTarget->m->size = size;
|
---|
8178 | pTarget->m->logicalSize = logicalSize;
|
---|
8179 | pTarget->m->variant = variant;
|
---|
8180 | }
|
---|
8181 | else
|
---|
8182 | {
|
---|
8183 | /* back to NotCreated on failure */
|
---|
8184 | pTarget->m->state = MediumState_NotCreated;
|
---|
8185 |
|
---|
8186 | pTarget->m->autoReset = false;
|
---|
8187 |
|
---|
8188 | /* reset UUID to prevent it from being reused next time */
|
---|
8189 | if (fGenerateUuid)
|
---|
8190 | unconst(pTarget->m->id).clear();
|
---|
8191 | }
|
---|
8192 |
|
---|
8193 | // deregister the task registered in createDiffStorage()
|
---|
8194 | Assert(m->numCreateDiffTasks != 0);
|
---|
8195 | --m->numCreateDiffTasks;
|
---|
8196 |
|
---|
8197 | mediaLock.release();
|
---|
8198 | i_markRegistriesModified();
|
---|
8199 | if (task.isAsync())
|
---|
8200 | {
|
---|
8201 | // in asynchronous mode, save settings now
|
---|
8202 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
8203 | }
|
---|
8204 |
|
---|
8205 | /* Note that in sync mode, it's the caller's responsibility to
|
---|
8206 | * unlock the medium. */
|
---|
8207 |
|
---|
8208 | return mrc;
|
---|
8209 | }
|
---|
8210 |
|
---|
8211 | /**
|
---|
8212 | * Implementation code for the "merge" task.
|
---|
8213 | *
|
---|
8214 | * This task always gets started from Medium::mergeTo() and can run
|
---|
8215 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
8216 | * that function. If we run synchronously, the caller expects the medium
|
---|
8217 | * registry modification to be set before returning; otherwise (in asynchronous
|
---|
8218 | * mode), we save the settings ourselves.
|
---|
8219 | *
|
---|
8220 | * @param task
|
---|
8221 | * @return
|
---|
8222 | */
|
---|
8223 | HRESULT Medium::i_taskMergeHandler(Medium::MergeTask &task)
|
---|
8224 | {
|
---|
8225 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
8226 | * to lock order violations, it probably causes lock order issues related
|
---|
8227 | * to the AutoCaller usage. */
|
---|
8228 | HRESULT rcTmp = S_OK;
|
---|
8229 |
|
---|
8230 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
8231 |
|
---|
8232 | try
|
---|
8233 | {
|
---|
8234 | if (!task.mParentForTarget.isNull())
|
---|
8235 | if (task.mParentForTarget->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
8236 | {
|
---|
8237 | AutoReadLock plock(task.mParentForTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8238 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8239 | tr("Cannot merge image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
8240 | task.mParentForTarget->m->strLocationFull.c_str());
|
---|
8241 | }
|
---|
8242 |
|
---|
8243 | PVBOXHDD hdd;
|
---|
8244 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8245 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8246 |
|
---|
8247 | try
|
---|
8248 | {
|
---|
8249 | // Similar code appears in SessionMachine::onlineMergeMedium, so
|
---|
8250 | // if you make any changes below check whether they are applicable
|
---|
8251 | // in that context as well.
|
---|
8252 |
|
---|
8253 | unsigned uTargetIdx = VD_LAST_IMAGE;
|
---|
8254 | unsigned uSourceIdx = VD_LAST_IMAGE;
|
---|
8255 | /* Open all media in the chain. */
|
---|
8256 | MediumLockList::Base::iterator lockListBegin =
|
---|
8257 | task.mpMediumLockList->GetBegin();
|
---|
8258 | MediumLockList::Base::iterator lockListEnd =
|
---|
8259 | task.mpMediumLockList->GetEnd();
|
---|
8260 | unsigned i = 0;
|
---|
8261 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
8262 | it != lockListEnd;
|
---|
8263 | ++it)
|
---|
8264 | {
|
---|
8265 | MediumLock &mediumLock = *it;
|
---|
8266 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8267 |
|
---|
8268 | if (pMedium == this)
|
---|
8269 | uSourceIdx = i;
|
---|
8270 | else if (pMedium == pTarget)
|
---|
8271 | uTargetIdx = i;
|
---|
8272 |
|
---|
8273 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8274 |
|
---|
8275 | /*
|
---|
8276 | * complex sanity (sane complexity)
|
---|
8277 | *
|
---|
8278 | * The current medium must be in the Deleting (medium is merged)
|
---|
8279 | * or LockedRead (parent medium) state if it is not the target.
|
---|
8280 | * If it is the target it must be in the LockedWrite state.
|
---|
8281 | */
|
---|
8282 | Assert( ( pMedium != pTarget
|
---|
8283 | && ( pMedium->m->state == MediumState_Deleting
|
---|
8284 | || pMedium->m->state == MediumState_LockedRead))
|
---|
8285 | || ( pMedium == pTarget
|
---|
8286 | && pMedium->m->state == MediumState_LockedWrite));
|
---|
8287 | /*
|
---|
8288 | * Medium must be the target, in the LockedRead state
|
---|
8289 | * or Deleting state where it is not allowed to be attached
|
---|
8290 | * to a virtual machine.
|
---|
8291 | */
|
---|
8292 | Assert( pMedium == pTarget
|
---|
8293 | || pMedium->m->state == MediumState_LockedRead
|
---|
8294 | || ( pMedium->m->backRefs.size() == 0
|
---|
8295 | && pMedium->m->state == MediumState_Deleting));
|
---|
8296 | /* The source medium must be in Deleting state. */
|
---|
8297 | Assert( pMedium != this
|
---|
8298 | || pMedium->m->state == MediumState_Deleting);
|
---|
8299 |
|
---|
8300 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
8301 |
|
---|
8302 | if ( pMedium->m->state == MediumState_LockedRead
|
---|
8303 | || pMedium->m->state == MediumState_Deleting)
|
---|
8304 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
8305 | if (pMedium->m->type == MediumType_Shareable)
|
---|
8306 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
8307 |
|
---|
8308 | /* Open the medium */
|
---|
8309 | vrc = VDOpen(hdd,
|
---|
8310 | pMedium->m->strFormat.c_str(),
|
---|
8311 | pMedium->m->strLocationFull.c_str(),
|
---|
8312 | uOpenFlags | m->uOpenFlagsDef,
|
---|
8313 | pMedium->m->vdImageIfaces);
|
---|
8314 | if (RT_FAILURE(vrc))
|
---|
8315 | throw vrc;
|
---|
8316 |
|
---|
8317 | i++;
|
---|
8318 | }
|
---|
8319 |
|
---|
8320 | ComAssertThrow( uSourceIdx != VD_LAST_IMAGE
|
---|
8321 | && uTargetIdx != VD_LAST_IMAGE, E_FAIL);
|
---|
8322 |
|
---|
8323 | vrc = VDMerge(hdd, uSourceIdx, uTargetIdx,
|
---|
8324 | task.mVDOperationIfaces);
|
---|
8325 | if (RT_FAILURE(vrc))
|
---|
8326 | throw vrc;
|
---|
8327 |
|
---|
8328 | /* update parent UUIDs */
|
---|
8329 | if (!task.mfMergeForward)
|
---|
8330 | {
|
---|
8331 | /* we need to update UUIDs of all source's children
|
---|
8332 | * which cannot be part of the container at once so
|
---|
8333 | * add each one in there individually */
|
---|
8334 | if (task.mpChildrenToReparent)
|
---|
8335 | {
|
---|
8336 | MediumLockList::Base::iterator childrenBegin = task.mpChildrenToReparent->GetBegin();
|
---|
8337 | MediumLockList::Base::iterator childrenEnd = task.mpChildrenToReparent->GetEnd();
|
---|
8338 | for (MediumLockList::Base::iterator it = childrenBegin;
|
---|
8339 | it != childrenEnd;
|
---|
8340 | ++it)
|
---|
8341 | {
|
---|
8342 | Medium *pMedium = it->GetMedium();
|
---|
8343 | /* VD_OPEN_FLAGS_INFO since UUID is wrong yet */
|
---|
8344 | vrc = VDOpen(hdd,
|
---|
8345 | pMedium->m->strFormat.c_str(),
|
---|
8346 | pMedium->m->strLocationFull.c_str(),
|
---|
8347 | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
8348 | pMedium->m->vdImageIfaces);
|
---|
8349 | if (RT_FAILURE(vrc))
|
---|
8350 | throw vrc;
|
---|
8351 |
|
---|
8352 | vrc = VDSetParentUuid(hdd, VD_LAST_IMAGE,
|
---|
8353 | pTarget->m->id.raw());
|
---|
8354 | if (RT_FAILURE(vrc))
|
---|
8355 | throw vrc;
|
---|
8356 |
|
---|
8357 | vrc = VDClose(hdd, false /* fDelete */);
|
---|
8358 | if (RT_FAILURE(vrc))
|
---|
8359 | throw vrc;
|
---|
8360 | }
|
---|
8361 | }
|
---|
8362 | }
|
---|
8363 | }
|
---|
8364 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8365 | catch (int aVRC)
|
---|
8366 | {
|
---|
8367 | rcTmp = setError(VBOX_E_FILE_ERROR,
|
---|
8368 | tr("Could not merge the medium '%s' to '%s'%s"),
|
---|
8369 | m->strLocationFull.c_str(),
|
---|
8370 | pTarget->m->strLocationFull.c_str(),
|
---|
8371 | i_vdError(aVRC).c_str());
|
---|
8372 | }
|
---|
8373 |
|
---|
8374 | VDDestroy(hdd);
|
---|
8375 | }
|
---|
8376 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8377 |
|
---|
8378 | ErrorInfoKeeper eik;
|
---|
8379 | MultiResult mrc(rcTmp);
|
---|
8380 | HRESULT rc2;
|
---|
8381 |
|
---|
8382 | if (SUCCEEDED(mrc))
|
---|
8383 | {
|
---|
8384 | /* all media but the target were successfully deleted by
|
---|
8385 | * VDMerge; reparent the last one and uninitialize deleted media. */
|
---|
8386 |
|
---|
8387 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
8388 |
|
---|
8389 | if (task.mfMergeForward)
|
---|
8390 | {
|
---|
8391 | /* first, unregister the target since it may become a base
|
---|
8392 | * medium which needs re-registration */
|
---|
8393 | rc2 = m->pVirtualBox->i_unregisterMedium(pTarget);
|
---|
8394 | AssertComRC(rc2);
|
---|
8395 |
|
---|
8396 | /* then, reparent it and disconnect the deleted branch at both ends
|
---|
8397 | * (chain->parent() is source's parent). Depth check above. */
|
---|
8398 | pTarget->i_deparent();
|
---|
8399 | pTarget->i_setParent(task.mParentForTarget);
|
---|
8400 | if (task.mParentForTarget)
|
---|
8401 | i_deparent();
|
---|
8402 |
|
---|
8403 | /* then, register again */
|
---|
8404 | ComObjPtr<Medium> pMedium;
|
---|
8405 | rc2 = m->pVirtualBox->i_registerMedium(pTarget, &pMedium,
|
---|
8406 | treeLock);
|
---|
8407 | AssertComRC(rc2);
|
---|
8408 | }
|
---|
8409 | else
|
---|
8410 | {
|
---|
8411 | Assert(pTarget->i_getChildren().size() == 1);
|
---|
8412 | Medium *targetChild = pTarget->i_getChildren().front();
|
---|
8413 |
|
---|
8414 | /* disconnect the deleted branch at the elder end */
|
---|
8415 | targetChild->i_deparent();
|
---|
8416 |
|
---|
8417 | /* reparent source's children and disconnect the deleted
|
---|
8418 | * branch at the younger end */
|
---|
8419 | if (task.mpChildrenToReparent)
|
---|
8420 | {
|
---|
8421 | /* obey {parent,child} lock order */
|
---|
8422 | AutoWriteLock sourceLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8423 |
|
---|
8424 | MediumLockList::Base::iterator childrenBegin = task.mpChildrenToReparent->GetBegin();
|
---|
8425 | MediumLockList::Base::iterator childrenEnd = task.mpChildrenToReparent->GetEnd();
|
---|
8426 | for (MediumLockList::Base::iterator it = childrenBegin;
|
---|
8427 | it != childrenEnd;
|
---|
8428 | ++it)
|
---|
8429 | {
|
---|
8430 | Medium *pMedium = it->GetMedium();
|
---|
8431 | AutoWriteLock childLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8432 |
|
---|
8433 | pMedium->i_deparent(); // removes pMedium from source
|
---|
8434 | // no depth check, reduces depth
|
---|
8435 | pMedium->i_setParent(pTarget);
|
---|
8436 | }
|
---|
8437 | }
|
---|
8438 | }
|
---|
8439 |
|
---|
8440 | /* unregister and uninitialize all media removed by the merge */
|
---|
8441 | MediumLockList::Base::iterator lockListBegin =
|
---|
8442 | task.mpMediumLockList->GetBegin();
|
---|
8443 | MediumLockList::Base::iterator lockListEnd =
|
---|
8444 | task.mpMediumLockList->GetEnd();
|
---|
8445 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
8446 | it != lockListEnd;
|
---|
8447 | )
|
---|
8448 | {
|
---|
8449 | MediumLock &mediumLock = *it;
|
---|
8450 | /* Create a real copy of the medium pointer, as the medium
|
---|
8451 | * lock deletion below would invalidate the referenced object. */
|
---|
8452 | const ComObjPtr<Medium> pMedium = mediumLock.GetMedium();
|
---|
8453 |
|
---|
8454 | /* The target and all media not merged (readonly) are skipped */
|
---|
8455 | if ( pMedium == pTarget
|
---|
8456 | || pMedium->m->state == MediumState_LockedRead)
|
---|
8457 | {
|
---|
8458 | ++it;
|
---|
8459 | continue;
|
---|
8460 | }
|
---|
8461 |
|
---|
8462 | rc2 = pMedium->m->pVirtualBox->i_unregisterMedium(pMedium);
|
---|
8463 | AssertComRC(rc2);
|
---|
8464 |
|
---|
8465 | /* now, uninitialize the deleted medium (note that
|
---|
8466 | * due to the Deleting state, uninit() will not touch
|
---|
8467 | * the parent-child relationship so we need to
|
---|
8468 | * uninitialize each disk individually) */
|
---|
8469 |
|
---|
8470 | /* note that the operation initiator medium (which is
|
---|
8471 | * normally also the source medium) is a special case
|
---|
8472 | * -- there is one more caller added by Task to it which
|
---|
8473 | * we must release. Also, if we are in sync mode, the
|
---|
8474 | * caller may still hold an AutoCaller instance for it
|
---|
8475 | * and therefore we cannot uninit() it (it's therefore
|
---|
8476 | * the caller's responsibility) */
|
---|
8477 | if (pMedium == this)
|
---|
8478 | {
|
---|
8479 | Assert(i_getChildren().size() == 0);
|
---|
8480 | Assert(m->backRefs.size() == 0);
|
---|
8481 | task.mMediumCaller.release();
|
---|
8482 | }
|
---|
8483 |
|
---|
8484 | /* Delete the medium lock list entry, which also releases the
|
---|
8485 | * caller added by MergeChain before uninit() and updates the
|
---|
8486 | * iterator to point to the right place. */
|
---|
8487 | rc2 = task.mpMediumLockList->RemoveByIterator(it);
|
---|
8488 | AssertComRC(rc2);
|
---|
8489 |
|
---|
8490 | if (task.isAsync() || pMedium != this)
|
---|
8491 | {
|
---|
8492 | treeLock.release();
|
---|
8493 | pMedium->uninit();
|
---|
8494 | treeLock.acquire();
|
---|
8495 | }
|
---|
8496 | }
|
---|
8497 | }
|
---|
8498 |
|
---|
8499 | i_markRegistriesModified();
|
---|
8500 | if (task.isAsync())
|
---|
8501 | {
|
---|
8502 | // in asynchronous mode, save settings now
|
---|
8503 | eik.restore();
|
---|
8504 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
8505 | eik.fetch();
|
---|
8506 | }
|
---|
8507 |
|
---|
8508 | if (FAILED(mrc))
|
---|
8509 | {
|
---|
8510 | /* Here we come if either VDMerge() failed (in which case we
|
---|
8511 | * assume that it tried to do everything to make a further
|
---|
8512 | * retry possible -- e.g. not deleted intermediate media
|
---|
8513 | * and so on) or VirtualBox::saveRegistries() failed (where we
|
---|
8514 | * should have the original tree but with intermediate storage
|
---|
8515 | * units deleted by VDMerge()). We have to only restore states
|
---|
8516 | * (through the MergeChain dtor) unless we are run synchronously
|
---|
8517 | * in which case it's the responsibility of the caller as stated
|
---|
8518 | * in the mergeTo() docs. The latter also implies that we
|
---|
8519 | * don't own the merge chain, so release it in this case. */
|
---|
8520 | if (task.isAsync())
|
---|
8521 | i_cancelMergeTo(task.mpChildrenToReparent, task.mpMediumLockList);
|
---|
8522 | }
|
---|
8523 |
|
---|
8524 | return mrc;
|
---|
8525 | }
|
---|
8526 |
|
---|
8527 | /**
|
---|
8528 | * Implementation code for the "clone" task.
|
---|
8529 | *
|
---|
8530 | * This only gets started from Medium::CloneTo() and always runs asynchronously.
|
---|
8531 | * As a result, we always save the VirtualBox.xml file when we're done here.
|
---|
8532 | *
|
---|
8533 | * @param task
|
---|
8534 | * @return
|
---|
8535 | */
|
---|
8536 | HRESULT Medium::i_taskCloneHandler(Medium::CloneTask &task)
|
---|
8537 | {
|
---|
8538 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
8539 | * to lock order violations, it probably causes lock order issues related
|
---|
8540 | * to the AutoCaller usage. */
|
---|
8541 | HRESULT rcTmp = S_OK;
|
---|
8542 |
|
---|
8543 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
8544 | const ComObjPtr<Medium> &pParent = task.mParent;
|
---|
8545 |
|
---|
8546 | bool fCreatingTarget = false;
|
---|
8547 |
|
---|
8548 | uint64_t size = 0, logicalSize = 0;
|
---|
8549 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
8550 | bool fGenerateUuid = false;
|
---|
8551 |
|
---|
8552 | try
|
---|
8553 | {
|
---|
8554 | if (!pParent.isNull())
|
---|
8555 | {
|
---|
8556 |
|
---|
8557 | if (pParent->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
8558 | {
|
---|
8559 | AutoReadLock plock(pParent COMMA_LOCKVAL_SRC_POS);
|
---|
8560 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8561 | tr("Cannot clone image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
8562 | pParent->m->strLocationFull.c_str());
|
---|
8563 | }
|
---|
8564 | }
|
---|
8565 |
|
---|
8566 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
8567 | * signal from the task initiator (which releases it only after
|
---|
8568 | * RTThreadCreate()) that we can start the job. */
|
---|
8569 | AutoMultiWriteLock3 thisLock(this, pTarget, pParent COMMA_LOCKVAL_SRC_POS);
|
---|
8570 |
|
---|
8571 | fCreatingTarget = pTarget->m->state == MediumState_Creating;
|
---|
8572 |
|
---|
8573 | /* The object may request a specific UUID (through a special form of
|
---|
8574 | * the setLocation() argument). Otherwise we have to generate it */
|
---|
8575 | Guid targetId = pTarget->m->id;
|
---|
8576 |
|
---|
8577 | fGenerateUuid = targetId.isZero();
|
---|
8578 | if (fGenerateUuid)
|
---|
8579 | {
|
---|
8580 | targetId.create();
|
---|
8581 | /* VirtualBox::registerMedium() will need UUID */
|
---|
8582 | unconst(pTarget->m->id) = targetId;
|
---|
8583 | }
|
---|
8584 |
|
---|
8585 | PVBOXHDD hdd;
|
---|
8586 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8587 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8588 |
|
---|
8589 | try
|
---|
8590 | {
|
---|
8591 | /* Open all media in the source chain. */
|
---|
8592 | MediumLockList::Base::const_iterator sourceListBegin =
|
---|
8593 | task.mpSourceMediumLockList->GetBegin();
|
---|
8594 | MediumLockList::Base::const_iterator sourceListEnd =
|
---|
8595 | task.mpSourceMediumLockList->GetEnd();
|
---|
8596 | for (MediumLockList::Base::const_iterator it = sourceListBegin;
|
---|
8597 | it != sourceListEnd;
|
---|
8598 | ++it)
|
---|
8599 | {
|
---|
8600 | const MediumLock &mediumLock = *it;
|
---|
8601 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8602 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8603 |
|
---|
8604 | /* sanity check */
|
---|
8605 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
8606 |
|
---|
8607 | /** Open all media in read-only mode. */
|
---|
8608 | vrc = VDOpen(hdd,
|
---|
8609 | pMedium->m->strFormat.c_str(),
|
---|
8610 | pMedium->m->strLocationFull.c_str(),
|
---|
8611 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
8612 | pMedium->m->vdImageIfaces);
|
---|
8613 | if (RT_FAILURE(vrc))
|
---|
8614 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8615 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8616 | pMedium->m->strLocationFull.c_str(),
|
---|
8617 | i_vdError(vrc).c_str());
|
---|
8618 | }
|
---|
8619 |
|
---|
8620 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
8621 | Utf8Str targetLocation(pTarget->m->strLocationFull);
|
---|
8622 | uint64_t capabilities = pTarget->m->formatObj->i_getCapabilities();
|
---|
8623 |
|
---|
8624 | Assert( pTarget->m->state == MediumState_Creating
|
---|
8625 | || pTarget->m->state == MediumState_LockedWrite);
|
---|
8626 | Assert(m->state == MediumState_LockedRead);
|
---|
8627 | Assert( pParent.isNull()
|
---|
8628 | || pParent->m->state == MediumState_LockedRead);
|
---|
8629 |
|
---|
8630 | /* unlock before the potentially lengthy operation */
|
---|
8631 | thisLock.release();
|
---|
8632 |
|
---|
8633 | /* ensure the target directory exists */
|
---|
8634 | if (capabilities & MediumFormatCapabilities_File)
|
---|
8635 | {
|
---|
8636 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
8637 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
8638 | if (FAILED(rc))
|
---|
8639 | throw rc;
|
---|
8640 | }
|
---|
8641 |
|
---|
8642 | PVBOXHDD targetHdd;
|
---|
8643 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &targetHdd);
|
---|
8644 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8645 |
|
---|
8646 | try
|
---|
8647 | {
|
---|
8648 | /* Open all media in the target chain. */
|
---|
8649 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
8650 | task.mpTargetMediumLockList->GetBegin();
|
---|
8651 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
8652 | task.mpTargetMediumLockList->GetEnd();
|
---|
8653 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
8654 | it != targetListEnd;
|
---|
8655 | ++it)
|
---|
8656 | {
|
---|
8657 | const MediumLock &mediumLock = *it;
|
---|
8658 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8659 |
|
---|
8660 | /* If the target medium is not created yet there's no
|
---|
8661 | * reason to open it. */
|
---|
8662 | if (pMedium == pTarget && fCreatingTarget)
|
---|
8663 | continue;
|
---|
8664 |
|
---|
8665 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8666 |
|
---|
8667 | /* sanity check */
|
---|
8668 | Assert( pMedium->m->state == MediumState_LockedRead
|
---|
8669 | || pMedium->m->state == MediumState_LockedWrite);
|
---|
8670 |
|
---|
8671 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
8672 | if (pMedium->m->state != MediumState_LockedWrite)
|
---|
8673 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
8674 | if (pMedium->m->type == MediumType_Shareable)
|
---|
8675 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
8676 |
|
---|
8677 | /* Open all media in appropriate mode. */
|
---|
8678 | vrc = VDOpen(targetHdd,
|
---|
8679 | pMedium->m->strFormat.c_str(),
|
---|
8680 | pMedium->m->strLocationFull.c_str(),
|
---|
8681 | uOpenFlags | m->uOpenFlagsDef,
|
---|
8682 | pMedium->m->vdImageIfaces);
|
---|
8683 | if (RT_FAILURE(vrc))
|
---|
8684 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8685 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8686 | pMedium->m->strLocationFull.c_str(),
|
---|
8687 | i_vdError(vrc).c_str());
|
---|
8688 | }
|
---|
8689 |
|
---|
8690 | /* target isn't locked, but no changing data is accessed */
|
---|
8691 | if (task.midxSrcImageSame == UINT32_MAX)
|
---|
8692 | {
|
---|
8693 | vrc = VDCopy(hdd,
|
---|
8694 | VD_LAST_IMAGE,
|
---|
8695 | targetHdd,
|
---|
8696 | targetFormat.c_str(),
|
---|
8697 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
8698 | false /* fMoveByRename */,
|
---|
8699 | 0 /* cbSize */,
|
---|
8700 | task.mVariant & ~MediumVariant_NoCreateDir,
|
---|
8701 | targetId.raw(),
|
---|
8702 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
8703 | NULL /* pVDIfsOperation */,
|
---|
8704 | pTarget->m->vdImageIfaces,
|
---|
8705 | task.mVDOperationIfaces);
|
---|
8706 | }
|
---|
8707 | else
|
---|
8708 | {
|
---|
8709 | vrc = VDCopyEx(hdd,
|
---|
8710 | VD_LAST_IMAGE,
|
---|
8711 | targetHdd,
|
---|
8712 | targetFormat.c_str(),
|
---|
8713 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
8714 | false /* fMoveByRename */,
|
---|
8715 | 0 /* cbSize */,
|
---|
8716 | task.midxSrcImageSame,
|
---|
8717 | task.midxDstImageSame,
|
---|
8718 | task.mVariant & ~MediumVariant_NoCreateDir,
|
---|
8719 | targetId.raw(),
|
---|
8720 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
8721 | NULL /* pVDIfsOperation */,
|
---|
8722 | pTarget->m->vdImageIfaces,
|
---|
8723 | task.mVDOperationIfaces);
|
---|
8724 | }
|
---|
8725 | if (RT_FAILURE(vrc))
|
---|
8726 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8727 | tr("Could not create the clone medium '%s'%s"),
|
---|
8728 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
8729 |
|
---|
8730 | size = VDGetFileSize(targetHdd, VD_LAST_IMAGE);
|
---|
8731 | logicalSize = VDGetSize(targetHdd, VD_LAST_IMAGE);
|
---|
8732 | unsigned uImageFlags;
|
---|
8733 | vrc = VDGetImageFlags(targetHdd, 0, &uImageFlags);
|
---|
8734 | if (RT_SUCCESS(vrc))
|
---|
8735 | variant = (MediumVariant_T)uImageFlags;
|
---|
8736 | }
|
---|
8737 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8738 |
|
---|
8739 | VDDestroy(targetHdd);
|
---|
8740 | }
|
---|
8741 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8742 |
|
---|
8743 | VDDestroy(hdd);
|
---|
8744 | }
|
---|
8745 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8746 |
|
---|
8747 | ErrorInfoKeeper eik;
|
---|
8748 | MultiResult mrc(rcTmp);
|
---|
8749 |
|
---|
8750 | /* Only do the parent changes for newly created media. */
|
---|
8751 | if (SUCCEEDED(mrc) && fCreatingTarget)
|
---|
8752 | {
|
---|
8753 | /* we set m->pParent & children() */
|
---|
8754 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
8755 |
|
---|
8756 | Assert(pTarget->m->pParent.isNull());
|
---|
8757 |
|
---|
8758 | if (pParent)
|
---|
8759 | {
|
---|
8760 | /* Associate the clone with the parent and deassociate
|
---|
8761 | * from VirtualBox. Depth check above. */
|
---|
8762 | pTarget->i_setParent(pParent);
|
---|
8763 |
|
---|
8764 | /* register with mVirtualBox as the last step and move to
|
---|
8765 | * Created state only on success (leaving an orphan file is
|
---|
8766 | * better than breaking media registry consistency) */
|
---|
8767 | eik.restore();
|
---|
8768 | ComObjPtr<Medium> pMedium;
|
---|
8769 | mrc = pParent->m->pVirtualBox->i_registerMedium(pTarget, &pMedium,
|
---|
8770 | treeLock);
|
---|
8771 | Assert( FAILED(mrc)
|
---|
8772 | || pTarget == pMedium);
|
---|
8773 | eik.fetch();
|
---|
8774 |
|
---|
8775 | if (FAILED(mrc))
|
---|
8776 | /* break parent association on failure to register */
|
---|
8777 | pTarget->i_deparent(); // removes target from parent
|
---|
8778 | }
|
---|
8779 | else
|
---|
8780 | {
|
---|
8781 | /* just register */
|
---|
8782 | eik.restore();
|
---|
8783 | ComObjPtr<Medium> pMedium;
|
---|
8784 | mrc = m->pVirtualBox->i_registerMedium(pTarget, &pMedium,
|
---|
8785 | treeLock);
|
---|
8786 | Assert( FAILED(mrc)
|
---|
8787 | || pTarget == pMedium);
|
---|
8788 | eik.fetch();
|
---|
8789 | }
|
---|
8790 | }
|
---|
8791 |
|
---|
8792 | if (fCreatingTarget)
|
---|
8793 | {
|
---|
8794 | AutoWriteLock mLock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8795 |
|
---|
8796 | if (SUCCEEDED(mrc))
|
---|
8797 | {
|
---|
8798 | pTarget->m->state = MediumState_Created;
|
---|
8799 |
|
---|
8800 | pTarget->m->size = size;
|
---|
8801 | pTarget->m->logicalSize = logicalSize;
|
---|
8802 | pTarget->m->variant = variant;
|
---|
8803 | }
|
---|
8804 | else
|
---|
8805 | {
|
---|
8806 | /* back to NotCreated on failure */
|
---|
8807 | pTarget->m->state = MediumState_NotCreated;
|
---|
8808 |
|
---|
8809 | /* reset UUID to prevent it from being reused next time */
|
---|
8810 | if (fGenerateUuid)
|
---|
8811 | unconst(pTarget->m->id).clear();
|
---|
8812 | }
|
---|
8813 | }
|
---|
8814 |
|
---|
8815 | /* Copy any filter related settings over to the target. */
|
---|
8816 | if (SUCCEEDED(mrc))
|
---|
8817 | {
|
---|
8818 | /* Copy any filter related settings over. */
|
---|
8819 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
8820 | ComObjPtr<Medium> pTargetBase = pTarget->i_getBase();
|
---|
8821 | std::vector<com::Utf8Str> aFilterPropNames;
|
---|
8822 | std::vector<com::Utf8Str> aFilterPropValues;
|
---|
8823 | mrc = pBase->i_getFilterProperties(aFilterPropNames, aFilterPropValues);
|
---|
8824 | if (SUCCEEDED(mrc))
|
---|
8825 | {
|
---|
8826 | /* Go through the properties and add them to the target medium. */
|
---|
8827 | for (unsigned idx = 0; idx < aFilterPropNames.size(); idx++)
|
---|
8828 | {
|
---|
8829 | mrc = pTargetBase->i_setPropertyDirect(aFilterPropNames[idx], aFilterPropValues[idx]);
|
---|
8830 | if (FAILED(mrc)) break;
|
---|
8831 | }
|
---|
8832 |
|
---|
8833 | // now, at the end of this task (always asynchronous), save the settings
|
---|
8834 | if (SUCCEEDED(mrc))
|
---|
8835 | {
|
---|
8836 | // save the settings
|
---|
8837 | i_markRegistriesModified();
|
---|
8838 | /* collect multiple errors */
|
---|
8839 | eik.restore();
|
---|
8840 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
8841 | eik.fetch();
|
---|
8842 | }
|
---|
8843 | }
|
---|
8844 | }
|
---|
8845 |
|
---|
8846 | /* Everything is explicitly unlocked when the task exits,
|
---|
8847 | * as the task destruction also destroys the source chain. */
|
---|
8848 |
|
---|
8849 | /* Make sure the source chain is released early. It could happen
|
---|
8850 | * that we get a deadlock in Appliance::Import when Medium::Close
|
---|
8851 | * is called & the source chain is released at the same time. */
|
---|
8852 | task.mpSourceMediumLockList->Clear();
|
---|
8853 |
|
---|
8854 | return mrc;
|
---|
8855 | }
|
---|
8856 |
|
---|
8857 | /**
|
---|
8858 | * Implementation code for the "move" task.
|
---|
8859 | *
|
---|
8860 | * This only gets started from Medium::SetLocation() and always
|
---|
8861 | * runs asynchronously.
|
---|
8862 | *
|
---|
8863 | * @param task
|
---|
8864 | * @return
|
---|
8865 | */
|
---|
8866 | HRESULT Medium::i_taskMoveHandler(Medium::MoveTask &task)
|
---|
8867 | {
|
---|
8868 |
|
---|
8869 | HRESULT rcOut = S_OK;
|
---|
8870 |
|
---|
8871 | /* pTarget is equal "this" in our case */
|
---|
8872 | const ComObjPtr<Medium> &pTarget = task.mMedium;
|
---|
8873 |
|
---|
8874 | uint64_t size = 0; NOREF(size);
|
---|
8875 | uint64_t logicalSize = 0; NOREF(logicalSize);
|
---|
8876 | MediumVariant_T variant = MediumVariant_Standard; NOREF(variant);
|
---|
8877 |
|
---|
8878 | /*
|
---|
8879 | * it's exactly moving, not cloning
|
---|
8880 | */
|
---|
8881 | if (!i_isMoveOperation(pTarget))
|
---|
8882 | {
|
---|
8883 | HRESULT rc = setError(VBOX_E_FILE_ERROR,
|
---|
8884 | tr("Wrong preconditions for moving the medium %s"),
|
---|
8885 | pTarget->m->strLocationFull.c_str());
|
---|
8886 | return rc;
|
---|
8887 | }
|
---|
8888 |
|
---|
8889 | try
|
---|
8890 | {
|
---|
8891 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
8892 | * signal from the task initiator (which releases it only after
|
---|
8893 | * RTThreadCreate()) that we can start the job. */
|
---|
8894 |
|
---|
8895 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8896 |
|
---|
8897 | PVBOXHDD hdd;
|
---|
8898 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8899 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8900 |
|
---|
8901 | try
|
---|
8902 | {
|
---|
8903 | /* Open all media in the source chain. */
|
---|
8904 | MediumLockList::Base::const_iterator sourceListBegin =
|
---|
8905 | task.mpMediumLockList->GetBegin();
|
---|
8906 | MediumLockList::Base::const_iterator sourceListEnd =
|
---|
8907 | task.mpMediumLockList->GetEnd();
|
---|
8908 | for (MediumLockList::Base::const_iterator it = sourceListBegin;
|
---|
8909 | it != sourceListEnd;
|
---|
8910 | ++it)
|
---|
8911 | {
|
---|
8912 | const MediumLock &mediumLock = *it;
|
---|
8913 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8914 | AutoWriteLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8915 |
|
---|
8916 | /* sanity check */
|
---|
8917 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
8918 |
|
---|
8919 | vrc = VDOpen(hdd,
|
---|
8920 | pMedium->m->strFormat.c_str(),
|
---|
8921 | pMedium->m->strLocationFull.c_str(),
|
---|
8922 | VD_OPEN_FLAGS_NORMAL,
|
---|
8923 | pMedium->m->vdImageIfaces);
|
---|
8924 | if (RT_FAILURE(vrc))
|
---|
8925 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8926 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8927 | pMedium->m->strLocationFull.c_str(),
|
---|
8928 | i_vdError(vrc).c_str());
|
---|
8929 | }
|
---|
8930 |
|
---|
8931 | /* we can directly use pTarget->m->"variables" but for better reading we use local copies */
|
---|
8932 | Guid targetId = pTarget->m->id;
|
---|
8933 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
8934 | uint64_t targetCapabilities = pTarget->m->formatObj->i_getCapabilities();
|
---|
8935 |
|
---|
8936 | /*
|
---|
8937 | * change target location
|
---|
8938 | * m->strNewLocationFull has been set already together with m->fMoveThisMedium in
|
---|
8939 | * i_preparationForMoving()
|
---|
8940 | */
|
---|
8941 | Utf8Str targetLocation = i_getNewLocationForMoving();
|
---|
8942 |
|
---|
8943 | /* unlock before the potentially lengthy operation */
|
---|
8944 | thisLock.release();
|
---|
8945 |
|
---|
8946 | /* ensure the target directory exists */
|
---|
8947 | if (targetCapabilities & MediumFormatCapabilities_File)
|
---|
8948 | {
|
---|
8949 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
8950 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
8951 | if (FAILED(rc))
|
---|
8952 | throw rc;
|
---|
8953 | }
|
---|
8954 |
|
---|
8955 | try
|
---|
8956 | {
|
---|
8957 | vrc = VDCopy(hdd,
|
---|
8958 | VD_LAST_IMAGE,
|
---|
8959 | hdd,
|
---|
8960 | targetFormat.c_str(),
|
---|
8961 | targetLocation.c_str(),
|
---|
8962 | true /* fMoveByRename */,
|
---|
8963 | 0 /* cbSize */,
|
---|
8964 | VD_IMAGE_FLAGS_NONE,
|
---|
8965 | targetId.raw(),
|
---|
8966 | VD_OPEN_FLAGS_NORMAL,
|
---|
8967 | NULL /* pVDIfsOperation */,
|
---|
8968 | NULL,
|
---|
8969 | NULL);
|
---|
8970 | if (RT_FAILURE(vrc))
|
---|
8971 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8972 | tr("Could not move medium '%s'%s"),
|
---|
8973 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
8974 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
8975 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
8976 | unsigned uImageFlags;
|
---|
8977 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
8978 | if (RT_SUCCESS(vrc))
|
---|
8979 | variant = (MediumVariant_T)uImageFlags;
|
---|
8980 |
|
---|
8981 | /*
|
---|
8982 | * set current location, because VDCopy\VDCopyEx doesn't do it.
|
---|
8983 | * also reset moving flag
|
---|
8984 | */
|
---|
8985 | i_resetMoveOperationData();
|
---|
8986 | m->strLocationFull = targetLocation;
|
---|
8987 |
|
---|
8988 | }
|
---|
8989 | catch (HRESULT aRC) { rcOut = aRC; }
|
---|
8990 |
|
---|
8991 | }
|
---|
8992 | catch (HRESULT aRC) { rcOut = aRC; }
|
---|
8993 |
|
---|
8994 | VDDestroy(hdd);
|
---|
8995 | }
|
---|
8996 | catch (HRESULT aRC) { rcOut = aRC; }
|
---|
8997 |
|
---|
8998 | ErrorInfoKeeper eik;
|
---|
8999 | MultiResult mrc(rcOut);
|
---|
9000 |
|
---|
9001 | // now, at the end of this task (always asynchronous), save the settings
|
---|
9002 | if (SUCCEEDED(mrc))
|
---|
9003 | {
|
---|
9004 | // save the settings
|
---|
9005 | i_markRegistriesModified();
|
---|
9006 | /* collect multiple errors */
|
---|
9007 | eik.restore();
|
---|
9008 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
9009 | eik.fetch();
|
---|
9010 | }
|
---|
9011 |
|
---|
9012 | /* Everything is explicitly unlocked when the task exits,
|
---|
9013 | * as the task destruction also destroys the source chain. */
|
---|
9014 |
|
---|
9015 | task.mpMediumLockList->Clear();
|
---|
9016 |
|
---|
9017 | return mrc;
|
---|
9018 | }
|
---|
9019 |
|
---|
9020 | /**
|
---|
9021 | * Implementation code for the "delete" task.
|
---|
9022 | *
|
---|
9023 | * This task always gets started from Medium::deleteStorage() and can run
|
---|
9024 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
9025 | * that function.
|
---|
9026 | *
|
---|
9027 | * @param task
|
---|
9028 | * @return
|
---|
9029 | */
|
---|
9030 | HRESULT Medium::i_taskDeleteHandler(Medium::DeleteTask &task)
|
---|
9031 | {
|
---|
9032 | NOREF(task);
|
---|
9033 | HRESULT rc = S_OK;
|
---|
9034 |
|
---|
9035 | try
|
---|
9036 | {
|
---|
9037 | /* The lock is also used as a signal from the task initiator (which
|
---|
9038 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
9039 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9040 |
|
---|
9041 | PVBOXHDD hdd;
|
---|
9042 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9043 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9044 |
|
---|
9045 | Utf8Str format(m->strFormat);
|
---|
9046 | Utf8Str location(m->strLocationFull);
|
---|
9047 |
|
---|
9048 | /* unlock before the potentially lengthy operation */
|
---|
9049 | Assert(m->state == MediumState_Deleting);
|
---|
9050 | thisLock.release();
|
---|
9051 |
|
---|
9052 | try
|
---|
9053 | {
|
---|
9054 | vrc = VDOpen(hdd,
|
---|
9055 | format.c_str(),
|
---|
9056 | location.c_str(),
|
---|
9057 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
9058 | m->vdImageIfaces);
|
---|
9059 | if (RT_SUCCESS(vrc))
|
---|
9060 | vrc = VDClose(hdd, true /* fDelete */);
|
---|
9061 |
|
---|
9062 | if (RT_FAILURE(vrc))
|
---|
9063 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9064 | tr("Could not delete the medium storage unit '%s'%s"),
|
---|
9065 | location.c_str(), i_vdError(vrc).c_str());
|
---|
9066 |
|
---|
9067 | }
|
---|
9068 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9069 |
|
---|
9070 | VDDestroy(hdd);
|
---|
9071 | }
|
---|
9072 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9073 |
|
---|
9074 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9075 |
|
---|
9076 | /* go to the NotCreated state even on failure since the storage
|
---|
9077 | * may have been already partially deleted and cannot be used any
|
---|
9078 | * more. One will be able to manually re-open the storage if really
|
---|
9079 | * needed to re-register it. */
|
---|
9080 | m->state = MediumState_NotCreated;
|
---|
9081 |
|
---|
9082 | /* Reset UUID to prevent Create* from reusing it again */
|
---|
9083 | unconst(m->id).clear();
|
---|
9084 |
|
---|
9085 | return rc;
|
---|
9086 | }
|
---|
9087 |
|
---|
9088 | /**
|
---|
9089 | * Implementation code for the "reset" task.
|
---|
9090 | *
|
---|
9091 | * This always gets started asynchronously from Medium::Reset().
|
---|
9092 | *
|
---|
9093 | * @param task
|
---|
9094 | * @return
|
---|
9095 | */
|
---|
9096 | HRESULT Medium::i_taskResetHandler(Medium::ResetTask &task)
|
---|
9097 | {
|
---|
9098 | HRESULT rc = S_OK;
|
---|
9099 |
|
---|
9100 | uint64_t size = 0, logicalSize = 0;
|
---|
9101 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
9102 |
|
---|
9103 | try
|
---|
9104 | {
|
---|
9105 | /* The lock is also used as a signal from the task initiator (which
|
---|
9106 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
9107 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9108 |
|
---|
9109 | /// @todo Below we use a pair of delete/create operations to reset
|
---|
9110 | /// the diff contents but the most efficient way will of course be
|
---|
9111 | /// to add a VDResetDiff() API call
|
---|
9112 |
|
---|
9113 | PVBOXHDD hdd;
|
---|
9114 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9115 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9116 |
|
---|
9117 | Guid id = m->id;
|
---|
9118 | Utf8Str format(m->strFormat);
|
---|
9119 | Utf8Str location(m->strLocationFull);
|
---|
9120 |
|
---|
9121 | Medium *pParent = m->pParent;
|
---|
9122 | Guid parentId = pParent->m->id;
|
---|
9123 | Utf8Str parentFormat(pParent->m->strFormat);
|
---|
9124 | Utf8Str parentLocation(pParent->m->strLocationFull);
|
---|
9125 |
|
---|
9126 | Assert(m->state == MediumState_LockedWrite);
|
---|
9127 |
|
---|
9128 | /* unlock before the potentially lengthy operation */
|
---|
9129 | thisLock.release();
|
---|
9130 |
|
---|
9131 | try
|
---|
9132 | {
|
---|
9133 | /* Open all media in the target chain but the last. */
|
---|
9134 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
9135 | task.mpMediumLockList->GetBegin();
|
---|
9136 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
9137 | task.mpMediumLockList->GetEnd();
|
---|
9138 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
9139 | it != targetListEnd;
|
---|
9140 | ++it)
|
---|
9141 | {
|
---|
9142 | const MediumLock &mediumLock = *it;
|
---|
9143 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9144 |
|
---|
9145 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9146 |
|
---|
9147 | /* sanity check, "this" is checked above */
|
---|
9148 | Assert( pMedium == this
|
---|
9149 | || pMedium->m->state == MediumState_LockedRead);
|
---|
9150 |
|
---|
9151 | /* Open all media in appropriate mode. */
|
---|
9152 | vrc = VDOpen(hdd,
|
---|
9153 | pMedium->m->strFormat.c_str(),
|
---|
9154 | pMedium->m->strLocationFull.c_str(),
|
---|
9155 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
9156 | pMedium->m->vdImageIfaces);
|
---|
9157 | if (RT_FAILURE(vrc))
|
---|
9158 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9159 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9160 | pMedium->m->strLocationFull.c_str(),
|
---|
9161 | i_vdError(vrc).c_str());
|
---|
9162 |
|
---|
9163 | /* Done when we hit the media which should be reset */
|
---|
9164 | if (pMedium == this)
|
---|
9165 | break;
|
---|
9166 | }
|
---|
9167 |
|
---|
9168 | /* first, delete the storage unit */
|
---|
9169 | vrc = VDClose(hdd, true /* fDelete */);
|
---|
9170 | if (RT_FAILURE(vrc))
|
---|
9171 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9172 | tr("Could not delete the medium storage unit '%s'%s"),
|
---|
9173 | location.c_str(), i_vdError(vrc).c_str());
|
---|
9174 |
|
---|
9175 | /* next, create it again */
|
---|
9176 | vrc = VDOpen(hdd,
|
---|
9177 | parentFormat.c_str(),
|
---|
9178 | parentLocation.c_str(),
|
---|
9179 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
9180 | m->vdImageIfaces);
|
---|
9181 | if (RT_FAILURE(vrc))
|
---|
9182 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9183 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9184 | parentLocation.c_str(), i_vdError(vrc).c_str());
|
---|
9185 |
|
---|
9186 | vrc = VDCreateDiff(hdd,
|
---|
9187 | format.c_str(),
|
---|
9188 | location.c_str(),
|
---|
9189 | /// @todo use the same medium variant as before
|
---|
9190 | VD_IMAGE_FLAGS_NONE,
|
---|
9191 | NULL,
|
---|
9192 | id.raw(),
|
---|
9193 | parentId.raw(),
|
---|
9194 | VD_OPEN_FLAGS_NORMAL,
|
---|
9195 | m->vdImageIfaces,
|
---|
9196 | task.mVDOperationIfaces);
|
---|
9197 | if (RT_FAILURE(vrc))
|
---|
9198 | {
|
---|
9199 | if (vrc == VERR_VD_INVALID_TYPE)
|
---|
9200 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9201 | tr("Parameters for creating the differencing medium storage unit '%s' are invalid%s"),
|
---|
9202 | location.c_str(), i_vdError(vrc).c_str());
|
---|
9203 | else
|
---|
9204 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9205 | tr("Could not create the differencing medium storage unit '%s'%s"),
|
---|
9206 | location.c_str(), i_vdError(vrc).c_str());
|
---|
9207 | }
|
---|
9208 |
|
---|
9209 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
9210 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
9211 | unsigned uImageFlags;
|
---|
9212 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
9213 | if (RT_SUCCESS(vrc))
|
---|
9214 | variant = (MediumVariant_T)uImageFlags;
|
---|
9215 | }
|
---|
9216 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9217 |
|
---|
9218 | VDDestroy(hdd);
|
---|
9219 | }
|
---|
9220 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9221 |
|
---|
9222 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9223 |
|
---|
9224 | m->size = size;
|
---|
9225 | m->logicalSize = logicalSize;
|
---|
9226 | m->variant = variant;
|
---|
9227 |
|
---|
9228 | /* Everything is explicitly unlocked when the task exits,
|
---|
9229 | * as the task destruction also destroys the media chain. */
|
---|
9230 |
|
---|
9231 | return rc;
|
---|
9232 | }
|
---|
9233 |
|
---|
9234 | /**
|
---|
9235 | * Implementation code for the "compact" task.
|
---|
9236 | *
|
---|
9237 | * @param task
|
---|
9238 | * @return
|
---|
9239 | */
|
---|
9240 | HRESULT Medium::i_taskCompactHandler(Medium::CompactTask &task)
|
---|
9241 | {
|
---|
9242 | HRESULT rc = S_OK;
|
---|
9243 |
|
---|
9244 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
9245 | * signal from the task initiator (which releases it only after
|
---|
9246 | * RTThreadCreate()) that we can start the job. */
|
---|
9247 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9248 |
|
---|
9249 | try
|
---|
9250 | {
|
---|
9251 | PVBOXHDD hdd;
|
---|
9252 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9253 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9254 |
|
---|
9255 | try
|
---|
9256 | {
|
---|
9257 | /* Open all media in the chain. */
|
---|
9258 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
9259 | task.mpMediumLockList->GetBegin();
|
---|
9260 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
9261 | task.mpMediumLockList->GetEnd();
|
---|
9262 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
9263 | mediumListEnd;
|
---|
9264 | --mediumListLast;
|
---|
9265 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
9266 | it != mediumListEnd;
|
---|
9267 | ++it)
|
---|
9268 | {
|
---|
9269 | const MediumLock &mediumLock = *it;
|
---|
9270 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9271 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9272 |
|
---|
9273 | /* sanity check */
|
---|
9274 | if (it == mediumListLast)
|
---|
9275 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
9276 | else
|
---|
9277 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
9278 |
|
---|
9279 | /* Open all media but last in read-only mode. Do not handle
|
---|
9280 | * shareable media, as compaction and sharing are mutually
|
---|
9281 | * exclusive. */
|
---|
9282 | vrc = VDOpen(hdd,
|
---|
9283 | pMedium->m->strFormat.c_str(),
|
---|
9284 | pMedium->m->strLocationFull.c_str(),
|
---|
9285 | m->uOpenFlagsDef | (it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
9286 | pMedium->m->vdImageIfaces);
|
---|
9287 | if (RT_FAILURE(vrc))
|
---|
9288 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9289 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9290 | pMedium->m->strLocationFull.c_str(),
|
---|
9291 | i_vdError(vrc).c_str());
|
---|
9292 | }
|
---|
9293 |
|
---|
9294 | Assert(m->state == MediumState_LockedWrite);
|
---|
9295 |
|
---|
9296 | Utf8Str location(m->strLocationFull);
|
---|
9297 |
|
---|
9298 | /* unlock before the potentially lengthy operation */
|
---|
9299 | thisLock.release();
|
---|
9300 |
|
---|
9301 | vrc = VDCompact(hdd, VD_LAST_IMAGE, task.mVDOperationIfaces);
|
---|
9302 | if (RT_FAILURE(vrc))
|
---|
9303 | {
|
---|
9304 | if (vrc == VERR_NOT_SUPPORTED)
|
---|
9305 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
9306 | tr("Compacting is not yet supported for medium '%s'"),
|
---|
9307 | location.c_str());
|
---|
9308 | else if (vrc == VERR_NOT_IMPLEMENTED)
|
---|
9309 | throw setError(E_NOTIMPL,
|
---|
9310 | tr("Compacting is not implemented, medium '%s'"),
|
---|
9311 | location.c_str());
|
---|
9312 | else
|
---|
9313 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9314 | tr("Could not compact medium '%s'%s"),
|
---|
9315 | location.c_str(),
|
---|
9316 | i_vdError(vrc).c_str());
|
---|
9317 | }
|
---|
9318 | }
|
---|
9319 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9320 |
|
---|
9321 | VDDestroy(hdd);
|
---|
9322 | }
|
---|
9323 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9324 |
|
---|
9325 | /* Everything is explicitly unlocked when the task exits,
|
---|
9326 | * as the task destruction also destroys the media chain. */
|
---|
9327 |
|
---|
9328 | return rc;
|
---|
9329 | }
|
---|
9330 |
|
---|
9331 | /**
|
---|
9332 | * Implementation code for the "resize" task.
|
---|
9333 | *
|
---|
9334 | * @param task
|
---|
9335 | * @return
|
---|
9336 | */
|
---|
9337 | HRESULT Medium::i_taskResizeHandler(Medium::ResizeTask &task)
|
---|
9338 | {
|
---|
9339 | HRESULT rc = S_OK;
|
---|
9340 |
|
---|
9341 | uint64_t size = 0, logicalSize = 0;
|
---|
9342 |
|
---|
9343 | try
|
---|
9344 | {
|
---|
9345 | /* The lock is also used as a signal from the task initiator (which
|
---|
9346 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
9347 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9348 |
|
---|
9349 | PVBOXHDD hdd;
|
---|
9350 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9351 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9352 |
|
---|
9353 | try
|
---|
9354 | {
|
---|
9355 | /* Open all media in the chain. */
|
---|
9356 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
9357 | task.mpMediumLockList->GetBegin();
|
---|
9358 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
9359 | task.mpMediumLockList->GetEnd();
|
---|
9360 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
9361 | mediumListEnd;
|
---|
9362 | --mediumListLast;
|
---|
9363 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
9364 | it != mediumListEnd;
|
---|
9365 | ++it)
|
---|
9366 | {
|
---|
9367 | const MediumLock &mediumLock = *it;
|
---|
9368 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9369 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9370 |
|
---|
9371 | /* sanity check */
|
---|
9372 | if (it == mediumListLast)
|
---|
9373 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
9374 | else
|
---|
9375 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
9376 |
|
---|
9377 | /* Open all media but last in read-only mode. Do not handle
|
---|
9378 | * shareable media, as compaction and sharing are mutually
|
---|
9379 | * exclusive. */
|
---|
9380 | vrc = VDOpen(hdd,
|
---|
9381 | pMedium->m->strFormat.c_str(),
|
---|
9382 | pMedium->m->strLocationFull.c_str(),
|
---|
9383 | m->uOpenFlagsDef | (it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
9384 | pMedium->m->vdImageIfaces);
|
---|
9385 | if (RT_FAILURE(vrc))
|
---|
9386 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9387 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9388 | pMedium->m->strLocationFull.c_str(),
|
---|
9389 | i_vdError(vrc).c_str());
|
---|
9390 | }
|
---|
9391 |
|
---|
9392 | Assert(m->state == MediumState_LockedWrite);
|
---|
9393 |
|
---|
9394 | Utf8Str location(m->strLocationFull);
|
---|
9395 |
|
---|
9396 | /* unlock before the potentially lengthy operation */
|
---|
9397 | thisLock.release();
|
---|
9398 |
|
---|
9399 | VDGEOMETRY geo = {0, 0, 0}; /* auto */
|
---|
9400 | vrc = VDResize(hdd, task.mSize, &geo, &geo, task.mVDOperationIfaces);
|
---|
9401 | if (RT_FAILURE(vrc))
|
---|
9402 | {
|
---|
9403 | if (vrc == VERR_NOT_SUPPORTED)
|
---|
9404 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
9405 | tr("Resizing to new size %llu is not yet supported for medium '%s'"),
|
---|
9406 | task.mSize, location.c_str());
|
---|
9407 | else if (vrc == VERR_NOT_IMPLEMENTED)
|
---|
9408 | throw setError(E_NOTIMPL,
|
---|
9409 | tr("Resiting is not implemented, medium '%s'"),
|
---|
9410 | location.c_str());
|
---|
9411 | else
|
---|
9412 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9413 | tr("Could not resize medium '%s'%s"),
|
---|
9414 | location.c_str(),
|
---|
9415 | i_vdError(vrc).c_str());
|
---|
9416 | }
|
---|
9417 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
9418 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
9419 | }
|
---|
9420 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9421 |
|
---|
9422 | VDDestroy(hdd);
|
---|
9423 | }
|
---|
9424 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9425 |
|
---|
9426 | if (SUCCEEDED(rc))
|
---|
9427 | {
|
---|
9428 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9429 | m->size = size;
|
---|
9430 | m->logicalSize = logicalSize;
|
---|
9431 | }
|
---|
9432 |
|
---|
9433 | /* Everything is explicitly unlocked when the task exits,
|
---|
9434 | * as the task destruction also destroys the media chain. */
|
---|
9435 |
|
---|
9436 | return rc;
|
---|
9437 | }
|
---|
9438 |
|
---|
9439 | /**
|
---|
9440 | * Implementation code for the "export" task.
|
---|
9441 | *
|
---|
9442 | * This only gets started from Medium::exportFile() and always runs
|
---|
9443 | * asynchronously. It doesn't touch anything configuration related, so
|
---|
9444 | * we never save the VirtualBox.xml file here.
|
---|
9445 | *
|
---|
9446 | * @param task
|
---|
9447 | * @return
|
---|
9448 | */
|
---|
9449 | HRESULT Medium::i_taskExportHandler(Medium::ExportTask &task)
|
---|
9450 | {
|
---|
9451 | HRESULT rc = S_OK;
|
---|
9452 |
|
---|
9453 | try
|
---|
9454 | {
|
---|
9455 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
9456 | * signal from the task initiator (which releases it only after
|
---|
9457 | * RTThreadCreate()) that we can start the job. */
|
---|
9458 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
9459 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9460 |
|
---|
9461 | PVBOXHDD hdd;
|
---|
9462 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9463 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9464 |
|
---|
9465 | try
|
---|
9466 | {
|
---|
9467 | settings::StringsMap::iterator itKeyStore = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
9468 | if (itKeyStore != pBase->m->mapProperties.end())
|
---|
9469 | {
|
---|
9470 | settings::StringsMap::iterator itKeyId = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
9471 |
|
---|
9472 | #ifdef VBOX_WITH_EXTPACK
|
---|
9473 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
9474 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
9475 | {
|
---|
9476 | /* Load the plugin */
|
---|
9477 | Utf8Str strPlugin;
|
---|
9478 | rc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
9479 | if (SUCCEEDED(rc))
|
---|
9480 | {
|
---|
9481 | vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
9482 | if (RT_FAILURE(vrc))
|
---|
9483 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
9484 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
9485 | i_vdError(vrc).c_str());
|
---|
9486 | }
|
---|
9487 | else
|
---|
9488 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
9489 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
9490 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
9491 | }
|
---|
9492 | else
|
---|
9493 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
9494 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
9495 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
9496 | #else
|
---|
9497 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
9498 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
9499 | #endif
|
---|
9500 |
|
---|
9501 | if (itKeyId == pBase->m->mapProperties.end())
|
---|
9502 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
9503 | tr("Image '%s' is configured for encryption but doesn't has a key identifier set"),
|
---|
9504 | pBase->m->strLocationFull.c_str());
|
---|
9505 |
|
---|
9506 | /* Find the proper secret key in the key store. */
|
---|
9507 | if (!task.m_pSecretKeyStore)
|
---|
9508 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
9509 | tr("Image '%s' is configured for encryption but there is no key store to retrieve the password from"),
|
---|
9510 | pBase->m->strLocationFull.c_str());
|
---|
9511 |
|
---|
9512 | SecretKey *pKey = NULL;
|
---|
9513 | vrc = task.m_pSecretKeyStore->retainSecretKey(itKeyId->second, &pKey);
|
---|
9514 | if (RT_FAILURE(vrc))
|
---|
9515 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
9516 | tr("Failed to retrieve the secret key with ID \"%s\" from the store (%Rrc)"),
|
---|
9517 | itKeyId->second.c_str(), vrc);
|
---|
9518 |
|
---|
9519 | Medium::CryptoFilterSettings CryptoSettingsRead;
|
---|
9520 | i_taskEncryptSettingsSetup(&CryptoSettingsRead, NULL, itKeyStore->second.c_str(), (const char *)pKey->getKeyBuffer(),
|
---|
9521 | false /* fCreateKeyStore */);
|
---|
9522 | vrc = VDFilterAdd(hdd, "CRYPT", VD_FILTER_FLAGS_READ, CryptoSettingsRead.vdFilterIfaces);
|
---|
9523 | if (vrc == VERR_VD_PASSWORD_INCORRECT)
|
---|
9524 | {
|
---|
9525 | task.m_pSecretKeyStore->releaseSecretKey(itKeyId->second);
|
---|
9526 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
9527 | tr("The password to decrypt the image is incorrect"));
|
---|
9528 | }
|
---|
9529 | else if (RT_FAILURE(vrc))
|
---|
9530 | {
|
---|
9531 | task.m_pSecretKeyStore->releaseSecretKey(itKeyId->second);
|
---|
9532 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
9533 | tr("Failed to load the decryption filter: %s"),
|
---|
9534 | i_vdError(vrc).c_str());
|
---|
9535 | }
|
---|
9536 |
|
---|
9537 | task.m_pSecretKeyStore->releaseSecretKey(itKeyId->second);
|
---|
9538 | }
|
---|
9539 |
|
---|
9540 | /* Open all media in the source chain. */
|
---|
9541 | MediumLockList::Base::const_iterator sourceListBegin =
|
---|
9542 | task.mpSourceMediumLockList->GetBegin();
|
---|
9543 | MediumLockList::Base::const_iterator sourceListEnd =
|
---|
9544 | task.mpSourceMediumLockList->GetEnd();
|
---|
9545 | for (MediumLockList::Base::const_iterator it = sourceListBegin;
|
---|
9546 | it != sourceListEnd;
|
---|
9547 | ++it)
|
---|
9548 | {
|
---|
9549 | const MediumLock &mediumLock = *it;
|
---|
9550 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9551 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9552 |
|
---|
9553 | /* sanity check */
|
---|
9554 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
9555 |
|
---|
9556 | /* Open all media in read-only mode. */
|
---|
9557 | vrc = VDOpen(hdd,
|
---|
9558 | pMedium->m->strFormat.c_str(),
|
---|
9559 | pMedium->m->strLocationFull.c_str(),
|
---|
9560 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
9561 | pMedium->m->vdImageIfaces);
|
---|
9562 | if (RT_FAILURE(vrc))
|
---|
9563 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9564 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9565 | pMedium->m->strLocationFull.c_str(),
|
---|
9566 | i_vdError(vrc).c_str());
|
---|
9567 | }
|
---|
9568 |
|
---|
9569 | Utf8Str targetFormat(task.mFormat->i_getId());
|
---|
9570 | Utf8Str targetLocation(task.mFilename);
|
---|
9571 | uint64_t capabilities = task.mFormat->i_getCapabilities();
|
---|
9572 |
|
---|
9573 | Assert(m->state == MediumState_LockedRead);
|
---|
9574 |
|
---|
9575 | /* unlock before the potentially lengthy operation */
|
---|
9576 | thisLock.release();
|
---|
9577 |
|
---|
9578 | /* ensure the target directory exists */
|
---|
9579 | if (capabilities & MediumFormatCapabilities_File)
|
---|
9580 | {
|
---|
9581 | rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
9582 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
9583 | if (FAILED(rc))
|
---|
9584 | throw rc;
|
---|
9585 | }
|
---|
9586 |
|
---|
9587 | PVBOXHDD targetHdd;
|
---|
9588 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &targetHdd);
|
---|
9589 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9590 |
|
---|
9591 | try
|
---|
9592 | {
|
---|
9593 | vrc = VDCopy(hdd,
|
---|
9594 | VD_LAST_IMAGE,
|
---|
9595 | targetHdd,
|
---|
9596 | targetFormat.c_str(),
|
---|
9597 | targetLocation.c_str(),
|
---|
9598 | false /* fMoveByRename */,
|
---|
9599 | 0 /* cbSize */,
|
---|
9600 | task.mVariant & ~MediumVariant_NoCreateDir,
|
---|
9601 | NULL /* pDstUuid */,
|
---|
9602 | VD_OPEN_FLAGS_NORMAL | VD_OPEN_FLAGS_SEQUENTIAL,
|
---|
9603 | NULL /* pVDIfsOperation */,
|
---|
9604 | task.mVDImageIfaces,
|
---|
9605 | task.mVDOperationIfaces);
|
---|
9606 | if (RT_FAILURE(vrc))
|
---|
9607 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9608 | tr("Could not create the exported medium '%s'%s"),
|
---|
9609 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
9610 | }
|
---|
9611 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9612 |
|
---|
9613 | VDDestroy(targetHdd);
|
---|
9614 | }
|
---|
9615 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9616 |
|
---|
9617 | VDDestroy(hdd);
|
---|
9618 | }
|
---|
9619 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9620 |
|
---|
9621 | /* Everything is explicitly unlocked when the task exits,
|
---|
9622 | * as the task destruction also destroys the source chain. */
|
---|
9623 |
|
---|
9624 | /* Make sure the source chain is released early, otherwise it can
|
---|
9625 | * lead to deadlocks with concurrent IAppliance activities. */
|
---|
9626 | task.mpSourceMediumLockList->Clear();
|
---|
9627 |
|
---|
9628 | return rc;
|
---|
9629 | }
|
---|
9630 |
|
---|
9631 | /**
|
---|
9632 | * Implementation code for the "import" task.
|
---|
9633 | *
|
---|
9634 | * This only gets started from Medium::importFile() and always runs
|
---|
9635 | * asynchronously. It potentially touches the media registry, so we
|
---|
9636 | * always save the VirtualBox.xml file when we're done here.
|
---|
9637 | *
|
---|
9638 | * @param task
|
---|
9639 | * @return
|
---|
9640 | */
|
---|
9641 | HRESULT Medium::i_taskImportHandler(Medium::ImportTask &task)
|
---|
9642 | {
|
---|
9643 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
9644 | * to lock order violations, it probably causes lock order issues related
|
---|
9645 | * to the AutoCaller usage. */
|
---|
9646 | HRESULT rcTmp = S_OK;
|
---|
9647 |
|
---|
9648 | const ComObjPtr<Medium> &pParent = task.mParent;
|
---|
9649 |
|
---|
9650 | bool fCreatingTarget = false;
|
---|
9651 |
|
---|
9652 | uint64_t size = 0, logicalSize = 0;
|
---|
9653 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
9654 | bool fGenerateUuid = false;
|
---|
9655 |
|
---|
9656 | try
|
---|
9657 | {
|
---|
9658 | if (!pParent.isNull())
|
---|
9659 | if (pParent->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
9660 | {
|
---|
9661 | AutoReadLock plock(pParent COMMA_LOCKVAL_SRC_POS);
|
---|
9662 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
9663 | tr("Cannot import image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
9664 | pParent->m->strLocationFull.c_str());
|
---|
9665 | }
|
---|
9666 |
|
---|
9667 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
9668 | * signal from the task initiator (which releases it only after
|
---|
9669 | * RTThreadCreate()) that we can start the job. */
|
---|
9670 | AutoMultiWriteLock2 thisLock(this, pParent COMMA_LOCKVAL_SRC_POS);
|
---|
9671 |
|
---|
9672 | fCreatingTarget = m->state == MediumState_Creating;
|
---|
9673 |
|
---|
9674 | /* The object may request a specific UUID (through a special form of
|
---|
9675 | * the setLocation() argument). Otherwise we have to generate it */
|
---|
9676 | Guid targetId = m->id;
|
---|
9677 |
|
---|
9678 | fGenerateUuid = targetId.isZero();
|
---|
9679 | if (fGenerateUuid)
|
---|
9680 | {
|
---|
9681 | targetId.create();
|
---|
9682 | /* VirtualBox::i_registerMedium() will need UUID */
|
---|
9683 | unconst(m->id) = targetId;
|
---|
9684 | }
|
---|
9685 |
|
---|
9686 |
|
---|
9687 | PVBOXHDD hdd;
|
---|
9688 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9689 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9690 |
|
---|
9691 | try
|
---|
9692 | {
|
---|
9693 | /* Open source medium. */
|
---|
9694 | vrc = VDOpen(hdd,
|
---|
9695 | task.mFormat->i_getId().c_str(),
|
---|
9696 | task.mFilename.c_str(),
|
---|
9697 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SEQUENTIAL | m->uOpenFlagsDef,
|
---|
9698 | task.mVDImageIfaces);
|
---|
9699 | if (RT_FAILURE(vrc))
|
---|
9700 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9701 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9702 | task.mFilename.c_str(),
|
---|
9703 | i_vdError(vrc).c_str());
|
---|
9704 |
|
---|
9705 | Utf8Str targetFormat(m->strFormat);
|
---|
9706 | Utf8Str targetLocation(m->strLocationFull);
|
---|
9707 | uint64_t capabilities = task.mFormat->i_getCapabilities();
|
---|
9708 |
|
---|
9709 | Assert( m->state == MediumState_Creating
|
---|
9710 | || m->state == MediumState_LockedWrite);
|
---|
9711 | Assert( pParent.isNull()
|
---|
9712 | || pParent->m->state == MediumState_LockedRead);
|
---|
9713 |
|
---|
9714 | /* unlock before the potentially lengthy operation */
|
---|
9715 | thisLock.release();
|
---|
9716 |
|
---|
9717 | /* ensure the target directory exists */
|
---|
9718 | if (capabilities & MediumFormatCapabilities_File)
|
---|
9719 | {
|
---|
9720 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
9721 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
9722 | if (FAILED(rc))
|
---|
9723 | throw rc;
|
---|
9724 | }
|
---|
9725 |
|
---|
9726 | PVBOXHDD targetHdd;
|
---|
9727 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &targetHdd);
|
---|
9728 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9729 |
|
---|
9730 | try
|
---|
9731 | {
|
---|
9732 | /* Open all media in the target chain. */
|
---|
9733 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
9734 | task.mpTargetMediumLockList->GetBegin();
|
---|
9735 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
9736 | task.mpTargetMediumLockList->GetEnd();
|
---|
9737 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
9738 | it != targetListEnd;
|
---|
9739 | ++it)
|
---|
9740 | {
|
---|
9741 | const MediumLock &mediumLock = *it;
|
---|
9742 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9743 |
|
---|
9744 | /* If the target medium is not created yet there's no
|
---|
9745 | * reason to open it. */
|
---|
9746 | if (pMedium == this && fCreatingTarget)
|
---|
9747 | continue;
|
---|
9748 |
|
---|
9749 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9750 |
|
---|
9751 | /* sanity check */
|
---|
9752 | Assert( pMedium->m->state == MediumState_LockedRead
|
---|
9753 | || pMedium->m->state == MediumState_LockedWrite);
|
---|
9754 |
|
---|
9755 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
9756 | if (pMedium->m->state != MediumState_LockedWrite)
|
---|
9757 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
9758 | if (pMedium->m->type == MediumType_Shareable)
|
---|
9759 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
9760 |
|
---|
9761 | /* Open all media in appropriate mode. */
|
---|
9762 | vrc = VDOpen(targetHdd,
|
---|
9763 | pMedium->m->strFormat.c_str(),
|
---|
9764 | pMedium->m->strLocationFull.c_str(),
|
---|
9765 | uOpenFlags | m->uOpenFlagsDef,
|
---|
9766 | pMedium->m->vdImageIfaces);
|
---|
9767 | if (RT_FAILURE(vrc))
|
---|
9768 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9769 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9770 | pMedium->m->strLocationFull.c_str(),
|
---|
9771 | i_vdError(vrc).c_str());
|
---|
9772 | }
|
---|
9773 |
|
---|
9774 | vrc = VDCopy(hdd,
|
---|
9775 | VD_LAST_IMAGE,
|
---|
9776 | targetHdd,
|
---|
9777 | targetFormat.c_str(),
|
---|
9778 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
9779 | false /* fMoveByRename */,
|
---|
9780 | 0 /* cbSize */,
|
---|
9781 | task.mVariant & ~MediumVariant_NoCreateDir,
|
---|
9782 | targetId.raw(),
|
---|
9783 | VD_OPEN_FLAGS_NORMAL,
|
---|
9784 | NULL /* pVDIfsOperation */,
|
---|
9785 | m->vdImageIfaces,
|
---|
9786 | task.mVDOperationIfaces);
|
---|
9787 | if (RT_FAILURE(vrc))
|
---|
9788 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9789 | tr("Could not create the imported medium '%s'%s"),
|
---|
9790 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
9791 |
|
---|
9792 | size = VDGetFileSize(targetHdd, VD_LAST_IMAGE);
|
---|
9793 | logicalSize = VDGetSize(targetHdd, VD_LAST_IMAGE);
|
---|
9794 | unsigned uImageFlags;
|
---|
9795 | vrc = VDGetImageFlags(targetHdd, 0, &uImageFlags);
|
---|
9796 | if (RT_SUCCESS(vrc))
|
---|
9797 | variant = (MediumVariant_T)uImageFlags;
|
---|
9798 | }
|
---|
9799 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9800 |
|
---|
9801 | VDDestroy(targetHdd);
|
---|
9802 | }
|
---|
9803 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9804 |
|
---|
9805 | VDDestroy(hdd);
|
---|
9806 | }
|
---|
9807 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9808 |
|
---|
9809 | ErrorInfoKeeper eik;
|
---|
9810 | MultiResult mrc(rcTmp);
|
---|
9811 |
|
---|
9812 | /* Only do the parent changes for newly created media. */
|
---|
9813 | if (SUCCEEDED(mrc) && fCreatingTarget)
|
---|
9814 | {
|
---|
9815 | /* we set m->pParent & children() */
|
---|
9816 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
9817 |
|
---|
9818 | Assert(m->pParent.isNull());
|
---|
9819 |
|
---|
9820 | if (pParent)
|
---|
9821 | {
|
---|
9822 | /* Associate the imported medium with the parent and deassociate
|
---|
9823 | * from VirtualBox. Depth check above. */
|
---|
9824 | i_setParent(pParent);
|
---|
9825 |
|
---|
9826 | /* register with mVirtualBox as the last step and move to
|
---|
9827 | * Created state only on success (leaving an orphan file is
|
---|
9828 | * better than breaking media registry consistency) */
|
---|
9829 | eik.restore();
|
---|
9830 | ComObjPtr<Medium> pMedium;
|
---|
9831 | mrc = pParent->m->pVirtualBox->i_registerMedium(this, &pMedium,
|
---|
9832 | treeLock);
|
---|
9833 | Assert(this == pMedium);
|
---|
9834 | eik.fetch();
|
---|
9835 |
|
---|
9836 | if (FAILED(mrc))
|
---|
9837 | /* break parent association on failure to register */
|
---|
9838 | this->i_deparent(); // removes target from parent
|
---|
9839 | }
|
---|
9840 | else
|
---|
9841 | {
|
---|
9842 | /* just register */
|
---|
9843 | eik.restore();
|
---|
9844 | ComObjPtr<Medium> pMedium;
|
---|
9845 | mrc = m->pVirtualBox->i_registerMedium(this, &pMedium, treeLock);
|
---|
9846 | Assert(this == pMedium);
|
---|
9847 | eik.fetch();
|
---|
9848 | }
|
---|
9849 | }
|
---|
9850 |
|
---|
9851 | if (fCreatingTarget)
|
---|
9852 | {
|
---|
9853 | AutoWriteLock mLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9854 |
|
---|
9855 | if (SUCCEEDED(mrc))
|
---|
9856 | {
|
---|
9857 | m->state = MediumState_Created;
|
---|
9858 |
|
---|
9859 | m->size = size;
|
---|
9860 | m->logicalSize = logicalSize;
|
---|
9861 | m->variant = variant;
|
---|
9862 | }
|
---|
9863 | else
|
---|
9864 | {
|
---|
9865 | /* back to NotCreated on failure */
|
---|
9866 | m->state = MediumState_NotCreated;
|
---|
9867 |
|
---|
9868 | /* reset UUID to prevent it from being reused next time */
|
---|
9869 | if (fGenerateUuid)
|
---|
9870 | unconst(m->id).clear();
|
---|
9871 | }
|
---|
9872 | }
|
---|
9873 |
|
---|
9874 | // now, at the end of this task (always asynchronous), save the settings
|
---|
9875 | {
|
---|
9876 | // save the settings
|
---|
9877 | i_markRegistriesModified();
|
---|
9878 | /* collect multiple errors */
|
---|
9879 | eik.restore();
|
---|
9880 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
9881 | eik.fetch();
|
---|
9882 | }
|
---|
9883 |
|
---|
9884 | /* Everything is explicitly unlocked when the task exits,
|
---|
9885 | * as the task destruction also destroys the target chain. */
|
---|
9886 |
|
---|
9887 | /* Make sure the target chain is released early, otherwise it can
|
---|
9888 | * lead to deadlocks with concurrent IAppliance activities. */
|
---|
9889 | task.mpTargetMediumLockList->Clear();
|
---|
9890 |
|
---|
9891 | return mrc;
|
---|
9892 | }
|
---|
9893 |
|
---|
9894 | /**
|
---|
9895 | * Sets up the encryption settings for a filter.
|
---|
9896 | */
|
---|
9897 | void Medium::i_taskEncryptSettingsSetup(CryptoFilterSettings *pSettings, const char *pszCipher,
|
---|
9898 | const char *pszKeyStore, const char *pszPassword,
|
---|
9899 | bool fCreateKeyStore)
|
---|
9900 | {
|
---|
9901 | pSettings->pszCipher = pszCipher;
|
---|
9902 | pSettings->pszPassword = pszPassword;
|
---|
9903 | pSettings->pszKeyStoreLoad = pszKeyStore;
|
---|
9904 | pSettings->fCreateKeyStore = fCreateKeyStore;
|
---|
9905 | pSettings->pbDek = NULL;
|
---|
9906 | pSettings->cbDek = 0;
|
---|
9907 | pSettings->vdFilterIfaces = NULL;
|
---|
9908 |
|
---|
9909 | pSettings->vdIfCfg.pfnAreKeysValid = i_vdCryptoConfigAreKeysValid;
|
---|
9910 | pSettings->vdIfCfg.pfnQuerySize = i_vdCryptoConfigQuerySize;
|
---|
9911 | pSettings->vdIfCfg.pfnQuery = i_vdCryptoConfigQuery;
|
---|
9912 | pSettings->vdIfCfg.pfnQueryBytes = NULL;
|
---|
9913 |
|
---|
9914 | pSettings->vdIfCrypto.pfnKeyRetain = i_vdCryptoKeyRetain;
|
---|
9915 | pSettings->vdIfCrypto.pfnKeyRelease = i_vdCryptoKeyRelease;
|
---|
9916 | pSettings->vdIfCrypto.pfnKeyStorePasswordRetain = i_vdCryptoKeyStorePasswordRetain;
|
---|
9917 | pSettings->vdIfCrypto.pfnKeyStorePasswordRelease = i_vdCryptoKeyStorePasswordRelease;
|
---|
9918 | pSettings->vdIfCrypto.pfnKeyStoreSave = i_vdCryptoKeyStoreSave;
|
---|
9919 | pSettings->vdIfCrypto.pfnKeyStoreReturnParameters = i_vdCryptoKeyStoreReturnParameters;
|
---|
9920 |
|
---|
9921 | int vrc = VDInterfaceAdd(&pSettings->vdIfCfg.Core,
|
---|
9922 | "Medium::vdInterfaceCfgCrypto",
|
---|
9923 | VDINTERFACETYPE_CONFIG, pSettings,
|
---|
9924 | sizeof(VDINTERFACECONFIG), &pSettings->vdFilterIfaces);
|
---|
9925 | AssertRC(vrc);
|
---|
9926 |
|
---|
9927 | vrc = VDInterfaceAdd(&pSettings->vdIfCrypto.Core,
|
---|
9928 | "Medium::vdInterfaceCrypto",
|
---|
9929 | VDINTERFACETYPE_CRYPTO, pSettings,
|
---|
9930 | sizeof(VDINTERFACECRYPTO), &pSettings->vdFilterIfaces);
|
---|
9931 | AssertRC(vrc);
|
---|
9932 | }
|
---|
9933 |
|
---|
9934 | /**
|
---|
9935 | * Implementation code for the "encrypt" task.
|
---|
9936 | *
|
---|
9937 | * @param task
|
---|
9938 | * @return
|
---|
9939 | */
|
---|
9940 | HRESULT Medium::i_taskEncryptHandler(Medium::EncryptTask &task)
|
---|
9941 | {
|
---|
9942 | # ifndef VBOX_WITH_EXTPACK
|
---|
9943 | RT_NOREF(task);
|
---|
9944 | # endif
|
---|
9945 | HRESULT rc = S_OK;
|
---|
9946 |
|
---|
9947 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
9948 | * signal from the task initiator (which releases it only after
|
---|
9949 | * RTThreadCreate()) that we can start the job. */
|
---|
9950 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
9951 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9952 |
|
---|
9953 | try
|
---|
9954 | {
|
---|
9955 | # ifdef VBOX_WITH_EXTPACK
|
---|
9956 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
9957 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
9958 | {
|
---|
9959 | /* Load the plugin */
|
---|
9960 | Utf8Str strPlugin;
|
---|
9961 | rc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
9962 | if (SUCCEEDED(rc))
|
---|
9963 | {
|
---|
9964 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
9965 | if (RT_FAILURE(vrc))
|
---|
9966 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
9967 | tr("Encrypting the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
9968 | i_vdError(vrc).c_str());
|
---|
9969 | }
|
---|
9970 | else
|
---|
9971 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
9972 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
9973 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
9974 | }
|
---|
9975 | else
|
---|
9976 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
9977 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
9978 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
9979 |
|
---|
9980 | PVBOXHDD pDisk = NULL;
|
---|
9981 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
9982 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9983 |
|
---|
9984 | Medium::CryptoFilterSettings CryptoSettingsRead;
|
---|
9985 | Medium::CryptoFilterSettings CryptoSettingsWrite;
|
---|
9986 |
|
---|
9987 | void *pvBuf = NULL;
|
---|
9988 | const char *pszPasswordNew = NULL;
|
---|
9989 | try
|
---|
9990 | {
|
---|
9991 | /* Set up disk encryption filters. */
|
---|
9992 | if (task.mstrCurrentPassword.isEmpty())
|
---|
9993 | {
|
---|
9994 | /*
|
---|
9995 | * Query whether the medium property indicating that encryption is
|
---|
9996 | * configured is existing.
|
---|
9997 | */
|
---|
9998 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
9999 | if (it != pBase->m->mapProperties.end())
|
---|
10000 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
10001 | tr("The password given for the encrypted image is incorrect"));
|
---|
10002 | }
|
---|
10003 | else
|
---|
10004 | {
|
---|
10005 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
10006 | if (it == pBase->m->mapProperties.end())
|
---|
10007 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10008 | tr("The image is not configured for encryption"));
|
---|
10009 |
|
---|
10010 | i_taskEncryptSettingsSetup(&CryptoSettingsRead, NULL, it->second.c_str(), task.mstrCurrentPassword.c_str(),
|
---|
10011 | false /* fCreateKeyStore */);
|
---|
10012 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ, CryptoSettingsRead.vdFilterIfaces);
|
---|
10013 | if (vrc == VERR_VD_PASSWORD_INCORRECT)
|
---|
10014 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
10015 | tr("The password to decrypt the image is incorrect"));
|
---|
10016 | else if (RT_FAILURE(vrc))
|
---|
10017 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10018 | tr("Failed to load the decryption filter: %s"),
|
---|
10019 | i_vdError(vrc).c_str());
|
---|
10020 | }
|
---|
10021 |
|
---|
10022 | if (task.mstrCipher.isNotEmpty())
|
---|
10023 | {
|
---|
10024 | if ( task.mstrNewPassword.isEmpty()
|
---|
10025 | && task.mstrNewPasswordId.isEmpty()
|
---|
10026 | && task.mstrCurrentPassword.isNotEmpty())
|
---|
10027 | {
|
---|
10028 | /* An empty password and password ID will default to the current password. */
|
---|
10029 | pszPasswordNew = task.mstrCurrentPassword.c_str();
|
---|
10030 | }
|
---|
10031 | else if (task.mstrNewPassword.isEmpty())
|
---|
10032 | throw setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
10033 | tr("A password must be given for the image encryption"));
|
---|
10034 | else if (task.mstrNewPasswordId.isEmpty())
|
---|
10035 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10036 | tr("A valid identifier for the password must be given"));
|
---|
10037 | else
|
---|
10038 | pszPasswordNew = task.mstrNewPassword.c_str();
|
---|
10039 |
|
---|
10040 | i_taskEncryptSettingsSetup(&CryptoSettingsWrite, task.mstrCipher.c_str(), NULL,
|
---|
10041 | pszPasswordNew, true /* fCreateKeyStore */);
|
---|
10042 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_WRITE, CryptoSettingsWrite.vdFilterIfaces);
|
---|
10043 | if (RT_FAILURE(vrc))
|
---|
10044 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10045 | tr("Failed to load the encryption filter: %s"),
|
---|
10046 | i_vdError(vrc).c_str());
|
---|
10047 | }
|
---|
10048 | else if (task.mstrNewPasswordId.isNotEmpty() || task.mstrNewPassword.isNotEmpty())
|
---|
10049 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10050 | tr("The password and password identifier must be empty if the output should be unencrypted"));
|
---|
10051 |
|
---|
10052 | /* Open all media in the chain. */
|
---|
10053 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
10054 | task.mpMediumLockList->GetBegin();
|
---|
10055 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
10056 | task.mpMediumLockList->GetEnd();
|
---|
10057 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
10058 | mediumListEnd;
|
---|
10059 | --mediumListLast;
|
---|
10060 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
10061 | it != mediumListEnd;
|
---|
10062 | ++it)
|
---|
10063 | {
|
---|
10064 | const MediumLock &mediumLock = *it;
|
---|
10065 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
10066 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
10067 |
|
---|
10068 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
10069 |
|
---|
10070 | /* Open all media but last in read-only mode. Do not handle
|
---|
10071 | * shareable media, as compaction and sharing are mutually
|
---|
10072 | * exclusive. */
|
---|
10073 | vrc = VDOpen(pDisk,
|
---|
10074 | pMedium->m->strFormat.c_str(),
|
---|
10075 | pMedium->m->strLocationFull.c_str(),
|
---|
10076 | m->uOpenFlagsDef | (it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
10077 | pMedium->m->vdImageIfaces);
|
---|
10078 | if (RT_FAILURE(vrc))
|
---|
10079 | throw setError(VBOX_E_FILE_ERROR,
|
---|
10080 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
10081 | pMedium->m->strLocationFull.c_str(),
|
---|
10082 | i_vdError(vrc).c_str());
|
---|
10083 | }
|
---|
10084 |
|
---|
10085 | Assert(m->state == MediumState_LockedWrite);
|
---|
10086 |
|
---|
10087 | Utf8Str location(m->strLocationFull);
|
---|
10088 |
|
---|
10089 | /* unlock before the potentially lengthy operation */
|
---|
10090 | thisLock.release();
|
---|
10091 |
|
---|
10092 | vrc = VDPrepareWithFilters(pDisk, task.mVDOperationIfaces);
|
---|
10093 | if (RT_FAILURE(vrc))
|
---|
10094 | throw setError(VBOX_E_FILE_ERROR,
|
---|
10095 | tr("Could not prepare disk images for encryption (%Rrc): %s"),
|
---|
10096 | vrc, i_vdError(vrc).c_str());
|
---|
10097 |
|
---|
10098 | thisLock.acquire();
|
---|
10099 | /* If everything went well set the new key store. */
|
---|
10100 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
10101 | if (it != pBase->m->mapProperties.end())
|
---|
10102 | pBase->m->mapProperties.erase(it);
|
---|
10103 |
|
---|
10104 | /* Delete KeyId if encryption is removed or the password did change. */
|
---|
10105 | if ( task.mstrNewPasswordId.isNotEmpty()
|
---|
10106 | || task.mstrCipher.isEmpty())
|
---|
10107 | {
|
---|
10108 | it = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
10109 | if (it != pBase->m->mapProperties.end())
|
---|
10110 | pBase->m->mapProperties.erase(it);
|
---|
10111 | }
|
---|
10112 |
|
---|
10113 | if (CryptoSettingsWrite.pszKeyStore)
|
---|
10114 | {
|
---|
10115 | pBase->m->mapProperties["CRYPT/KeyStore"] = Utf8Str(CryptoSettingsWrite.pszKeyStore);
|
---|
10116 | if (task.mstrNewPasswordId.isNotEmpty())
|
---|
10117 | pBase->m->mapProperties["CRYPT/KeyId"] = task.mstrNewPasswordId;
|
---|
10118 | }
|
---|
10119 |
|
---|
10120 | if (CryptoSettingsRead.pszCipherReturned)
|
---|
10121 | RTStrFree(CryptoSettingsRead.pszCipherReturned);
|
---|
10122 |
|
---|
10123 | if (CryptoSettingsWrite.pszCipherReturned)
|
---|
10124 | RTStrFree(CryptoSettingsWrite.pszCipherReturned);
|
---|
10125 |
|
---|
10126 | thisLock.release();
|
---|
10127 | pBase->i_markRegistriesModified();
|
---|
10128 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
10129 | }
|
---|
10130 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10131 |
|
---|
10132 | if (pvBuf)
|
---|
10133 | RTMemFree(pvBuf);
|
---|
10134 |
|
---|
10135 | VDDestroy(pDisk);
|
---|
10136 | # else
|
---|
10137 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
10138 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
10139 | # endif
|
---|
10140 | }
|
---|
10141 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10142 |
|
---|
10143 | /* Everything is explicitly unlocked when the task exits,
|
---|
10144 | * as the task destruction also destroys the media chain. */
|
---|
10145 |
|
---|
10146 | return rc;
|
---|
10147 | }
|
---|
10148 |
|
---|
10149 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|