1 | /* $Id: MediumImpl.cpp 74822 2018-10-12 18:40:09Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox COM class implementation
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2017 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 | #include "MediumImpl.h"
|
---|
18 | #include "MediumIOImpl.h"
|
---|
19 | #include "TokenImpl.h"
|
---|
20 | #include "ProgressImpl.h"
|
---|
21 | #include "SystemPropertiesImpl.h"
|
---|
22 | #include "VirtualBoxImpl.h"
|
---|
23 | #include "ExtPackManagerImpl.h"
|
---|
24 |
|
---|
25 | #include "AutoCaller.h"
|
---|
26 | #include "Logging.h"
|
---|
27 | #include "ThreadTask.h"
|
---|
28 | #include "VBox/com/MultiResult.h"
|
---|
29 | #include "VBox/com/ErrorInfo.h"
|
---|
30 |
|
---|
31 | #include <VBox/err.h>
|
---|
32 | #include <VBox/settings.h>
|
---|
33 |
|
---|
34 | #include <iprt/param.h>
|
---|
35 | #include <iprt/path.h>
|
---|
36 | #include <iprt/file.h>
|
---|
37 | #include <iprt/tcp.h>
|
---|
38 | #include <iprt/cpp/utils.h>
|
---|
39 | #include <iprt/memsafer.h>
|
---|
40 | #include <iprt/base64.h>
|
---|
41 | #include <iprt/vfs.h>
|
---|
42 | #include <iprt/fsvfs.h>
|
---|
43 |
|
---|
44 | #include <VBox/vd.h>
|
---|
45 |
|
---|
46 | #include <algorithm>
|
---|
47 | #include <list>
|
---|
48 |
|
---|
49 |
|
---|
50 | typedef std::list<Guid> GuidList;
|
---|
51 |
|
---|
52 |
|
---|
53 | #ifdef VBOX_WITH_EXTPACK
|
---|
54 | static const char g_szVDPlugin[] = "VDPluginCrypt";
|
---|
55 | #endif
|
---|
56 |
|
---|
57 |
|
---|
58 | ////////////////////////////////////////////////////////////////////////////////
|
---|
59 | //
|
---|
60 | // Medium data definition
|
---|
61 | //
|
---|
62 | ////////////////////////////////////////////////////////////////////////////////
|
---|
63 |
|
---|
64 | /** Describes how a machine refers to this medium. */
|
---|
65 | struct BackRef
|
---|
66 | {
|
---|
67 | /** Equality predicate for stdc++. */
|
---|
68 | struct EqualsTo : public std::unary_function <BackRef, bool>
|
---|
69 | {
|
---|
70 | explicit EqualsTo(const Guid &aMachineId) : machineId(aMachineId) {}
|
---|
71 |
|
---|
72 | bool operator()(const argument_type &aThat) const
|
---|
73 | {
|
---|
74 | return aThat.machineId == machineId;
|
---|
75 | }
|
---|
76 |
|
---|
77 | const Guid machineId;
|
---|
78 | };
|
---|
79 |
|
---|
80 | BackRef(const Guid &aMachineId,
|
---|
81 | const Guid &aSnapshotId = Guid::Empty)
|
---|
82 | : machineId(aMachineId),
|
---|
83 | fInCurState(aSnapshotId.isZero())
|
---|
84 | {
|
---|
85 | if (aSnapshotId.isValid() && !aSnapshotId.isZero())
|
---|
86 | llSnapshotIds.push_back(aSnapshotId);
|
---|
87 | }
|
---|
88 |
|
---|
89 | Guid machineId;
|
---|
90 | bool fInCurState : 1;
|
---|
91 | GuidList llSnapshotIds;
|
---|
92 | };
|
---|
93 |
|
---|
94 | typedef std::list<BackRef> BackRefList;
|
---|
95 |
|
---|
96 | struct Medium::Data
|
---|
97 | {
|
---|
98 | Data()
|
---|
99 | : pVirtualBox(NULL),
|
---|
100 | state(MediumState_NotCreated),
|
---|
101 | variant(MediumVariant_Standard),
|
---|
102 | size(0),
|
---|
103 | readers(0),
|
---|
104 | preLockState(MediumState_NotCreated),
|
---|
105 | queryInfoSem(LOCKCLASS_MEDIUMQUERY),
|
---|
106 | queryInfoRunning(false),
|
---|
107 | type(MediumType_Normal),
|
---|
108 | devType(DeviceType_HardDisk),
|
---|
109 | logicalSize(0),
|
---|
110 | hddOpenMode(OpenReadWrite),
|
---|
111 | autoReset(false),
|
---|
112 | hostDrive(false),
|
---|
113 | implicit(false),
|
---|
114 | fClosing(false),
|
---|
115 | uOpenFlagsDef(VD_OPEN_FLAGS_IGNORE_FLUSH),
|
---|
116 | numCreateDiffTasks(0),
|
---|
117 | vdDiskIfaces(NULL),
|
---|
118 | vdImageIfaces(NULL),
|
---|
119 | fMoveThisMedium(false)
|
---|
120 | { }
|
---|
121 |
|
---|
122 | /** weak VirtualBox parent */
|
---|
123 | VirtualBox * const pVirtualBox;
|
---|
124 |
|
---|
125 | // pParent and llChildren are protected by VirtualBox::i_getMediaTreeLockHandle()
|
---|
126 | ComObjPtr<Medium> pParent;
|
---|
127 | MediaList llChildren; // to add a child, just call push_back; to remove
|
---|
128 | // a child, call child->deparent() which does a lookup
|
---|
129 |
|
---|
130 | GuidList llRegistryIDs; // media registries in which this medium is listed
|
---|
131 |
|
---|
132 | const Guid id;
|
---|
133 | Utf8Str strDescription;
|
---|
134 | MediumState_T state;
|
---|
135 | MediumVariant_T variant;
|
---|
136 | Utf8Str strLocationFull;
|
---|
137 | uint64_t size;
|
---|
138 | Utf8Str strLastAccessError;
|
---|
139 |
|
---|
140 | BackRefList backRefs;
|
---|
141 |
|
---|
142 | size_t readers;
|
---|
143 | MediumState_T preLockState;
|
---|
144 |
|
---|
145 | /** Special synchronization for operations which must wait for
|
---|
146 | * Medium::i_queryInfo in another thread to complete. Using a SemRW is
|
---|
147 | * not quite ideal, but at least it is subject to the lock validator,
|
---|
148 | * unlike the SemEventMulti which we had here for many years. Catching
|
---|
149 | * possible deadlocks is more important than a tiny bit of efficiency. */
|
---|
150 | RWLockHandle queryInfoSem;
|
---|
151 | bool queryInfoRunning : 1;
|
---|
152 |
|
---|
153 | const Utf8Str strFormat;
|
---|
154 | ComObjPtr<MediumFormat> formatObj;
|
---|
155 |
|
---|
156 | MediumType_T type;
|
---|
157 | DeviceType_T devType;
|
---|
158 | uint64_t logicalSize;
|
---|
159 |
|
---|
160 | HDDOpenMode hddOpenMode;
|
---|
161 |
|
---|
162 | bool autoReset : 1;
|
---|
163 |
|
---|
164 | /** New UUID to be set on the next Medium::i_queryInfo call. */
|
---|
165 | const Guid uuidImage;
|
---|
166 | /** New parent UUID to be set on the next Medium::i_queryInfo call. */
|
---|
167 | const Guid uuidParentImage;
|
---|
168 |
|
---|
169 | bool hostDrive : 1;
|
---|
170 |
|
---|
171 | settings::StringsMap mapProperties;
|
---|
172 |
|
---|
173 | bool implicit : 1;
|
---|
174 | /** Flag whether the medium is in the process of being closed. */
|
---|
175 | bool fClosing: 1;
|
---|
176 |
|
---|
177 | /** Default flags passed to VDOpen(). */
|
---|
178 | unsigned uOpenFlagsDef;
|
---|
179 |
|
---|
180 | uint32_t numCreateDiffTasks;
|
---|
181 |
|
---|
182 | Utf8Str vdError; /*< Error remembered by the VD error callback. */
|
---|
183 |
|
---|
184 | VDINTERFACEERROR vdIfError;
|
---|
185 |
|
---|
186 | VDINTERFACECONFIG vdIfConfig;
|
---|
187 |
|
---|
188 | VDINTERFACETCPNET vdIfTcpNet;
|
---|
189 |
|
---|
190 | PVDINTERFACE vdDiskIfaces;
|
---|
191 | PVDINTERFACE vdImageIfaces;
|
---|
192 |
|
---|
193 | /** Flag if the medium is going to move to a new
|
---|
194 | * location. */
|
---|
195 | bool fMoveThisMedium;
|
---|
196 | /** new location path */
|
---|
197 | Utf8Str strNewLocationFull;
|
---|
198 | };
|
---|
199 |
|
---|
200 | typedef struct VDSOCKETINT
|
---|
201 | {
|
---|
202 | /** Socket handle. */
|
---|
203 | RTSOCKET hSocket;
|
---|
204 | } VDSOCKETINT, *PVDSOCKETINT;
|
---|
205 |
|
---|
206 | ////////////////////////////////////////////////////////////////////////////////
|
---|
207 | //
|
---|
208 | // Globals
|
---|
209 | //
|
---|
210 | ////////////////////////////////////////////////////////////////////////////////
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Medium::Task class for asynchronous operations.
|
---|
214 | *
|
---|
215 | * @note Instances of this class must be created using new() because the
|
---|
216 | * task thread function will delete them when the task is complete.
|
---|
217 | *
|
---|
218 | * @note The constructor of this class adds a caller on the managed Medium
|
---|
219 | * object which is automatically released upon destruction.
|
---|
220 | */
|
---|
221 | class Medium::Task : public ThreadTask
|
---|
222 | {
|
---|
223 | public:
|
---|
224 | Task(Medium *aMedium, Progress *aProgress)
|
---|
225 | : ThreadTask("Medium::Task"),
|
---|
226 | mVDOperationIfaces(NULL),
|
---|
227 | mMedium(aMedium),
|
---|
228 | mMediumCaller(aMedium),
|
---|
229 | mProgress(aProgress),
|
---|
230 | mVirtualBoxCaller(NULL)
|
---|
231 | {
|
---|
232 | AssertReturnVoidStmt(aMedium, mRC = E_FAIL);
|
---|
233 | mRC = mMediumCaller.rc();
|
---|
234 | if (FAILED(mRC))
|
---|
235 | return;
|
---|
236 |
|
---|
237 | /* Get strong VirtualBox reference, see below. */
|
---|
238 | VirtualBox *pVirtualBox = aMedium->m->pVirtualBox;
|
---|
239 | mVirtualBox = pVirtualBox;
|
---|
240 | mVirtualBoxCaller.attach(pVirtualBox);
|
---|
241 | mRC = mVirtualBoxCaller.rc();
|
---|
242 | if (FAILED(mRC))
|
---|
243 | return;
|
---|
244 |
|
---|
245 | /* Set up a per-operation progress interface, can be used freely (for
|
---|
246 | * binary operations you can use it either on the source or target). */
|
---|
247 | if (mProgress)
|
---|
248 | {
|
---|
249 | mVDIfProgress.pfnProgress = aProgress->i_vdProgressCallback;
|
---|
250 | int vrc = VDInterfaceAdd(&mVDIfProgress.Core,
|
---|
251 | "Medium::Task::vdInterfaceProgress",
|
---|
252 | VDINTERFACETYPE_PROGRESS,
|
---|
253 | mProgress,
|
---|
254 | sizeof(mVDIfProgress),
|
---|
255 | &mVDOperationIfaces);
|
---|
256 | AssertRC(vrc);
|
---|
257 | if (RT_FAILURE(vrc))
|
---|
258 | mRC = E_FAIL;
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | // Make all destructors virtual. Just in case.
|
---|
263 | virtual ~Task()
|
---|
264 | {
|
---|
265 | /* send the notification of completion.*/
|
---|
266 | if ( isAsync()
|
---|
267 | && !mProgress.isNull())
|
---|
268 | mProgress->i_notifyComplete(mRC);
|
---|
269 | }
|
---|
270 |
|
---|
271 | HRESULT rc() const { return mRC; }
|
---|
272 | bool isOk() const { return SUCCEEDED(rc()); }
|
---|
273 |
|
---|
274 | const ComPtr<Progress>& GetProgressObject() const {return mProgress;}
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * Runs Medium::Task::executeTask() on the current thread
|
---|
278 | * instead of creating a new one.
|
---|
279 | */
|
---|
280 | HRESULT runNow()
|
---|
281 | {
|
---|
282 | LogFlowFuncEnter();
|
---|
283 |
|
---|
284 | mRC = executeTask();
|
---|
285 |
|
---|
286 | LogFlowFunc(("rc=%Rhrc\n", mRC));
|
---|
287 | LogFlowFuncLeave();
|
---|
288 | return mRC;
|
---|
289 | }
|
---|
290 |
|
---|
291 | /**
|
---|
292 | * Implementation code for the "create base" task.
|
---|
293 | * Used as function for execution from a standalone thread.
|
---|
294 | */
|
---|
295 | void handler()
|
---|
296 | {
|
---|
297 | LogFlowFuncEnter();
|
---|
298 | try
|
---|
299 | {
|
---|
300 | mRC = executeTask(); /* (destructor picks up mRC, see above) */
|
---|
301 | LogFlowFunc(("rc=%Rhrc\n", mRC));
|
---|
302 | }
|
---|
303 | catch (...)
|
---|
304 | {
|
---|
305 | LogRel(("Some exception in the function Medium::Task:handler()\n"));
|
---|
306 | }
|
---|
307 |
|
---|
308 | LogFlowFuncLeave();
|
---|
309 | }
|
---|
310 |
|
---|
311 | PVDINTERFACE mVDOperationIfaces;
|
---|
312 |
|
---|
313 | const ComObjPtr<Medium> mMedium;
|
---|
314 | AutoCaller mMediumCaller;
|
---|
315 |
|
---|
316 | protected:
|
---|
317 | HRESULT mRC;
|
---|
318 |
|
---|
319 | private:
|
---|
320 | virtual HRESULT executeTask() = 0;
|
---|
321 |
|
---|
322 | const ComObjPtr<Progress> mProgress;
|
---|
323 |
|
---|
324 | VDINTERFACEPROGRESS mVDIfProgress;
|
---|
325 |
|
---|
326 | /* Must have a strong VirtualBox reference during a task otherwise the
|
---|
327 | * reference count might drop to 0 while a task is still running. This
|
---|
328 | * would result in weird behavior, including deadlocks due to uninit and
|
---|
329 | * locking order issues. The deadlock often is not detectable because the
|
---|
330 | * uninit uses event semaphores which sabotages deadlock detection. */
|
---|
331 | ComObjPtr<VirtualBox> mVirtualBox;
|
---|
332 | AutoCaller mVirtualBoxCaller;
|
---|
333 | };
|
---|
334 |
|
---|
335 | HRESULT Medium::Task::executeTask()
|
---|
336 | {
|
---|
337 | return E_NOTIMPL;//ReturnComNotImplemented()
|
---|
338 | }
|
---|
339 |
|
---|
340 | class Medium::CreateBaseTask : public Medium::Task
|
---|
341 | {
|
---|
342 | public:
|
---|
343 | CreateBaseTask(Medium *aMedium,
|
---|
344 | Progress *aProgress,
|
---|
345 | uint64_t aSize,
|
---|
346 | MediumVariant_T aVariant)
|
---|
347 | : Medium::Task(aMedium, aProgress),
|
---|
348 | mSize(aSize),
|
---|
349 | mVariant(aVariant)
|
---|
350 | {
|
---|
351 | m_strTaskName = "createBase";
|
---|
352 | }
|
---|
353 |
|
---|
354 | uint64_t mSize;
|
---|
355 | MediumVariant_T mVariant;
|
---|
356 |
|
---|
357 | private:
|
---|
358 | HRESULT executeTask();
|
---|
359 | };
|
---|
360 |
|
---|
361 | class Medium::CreateDiffTask : public Medium::Task
|
---|
362 | {
|
---|
363 | public:
|
---|
364 | CreateDiffTask(Medium *aMedium,
|
---|
365 | Progress *aProgress,
|
---|
366 | Medium *aTarget,
|
---|
367 | MediumVariant_T aVariant,
|
---|
368 | MediumLockList *aMediumLockList,
|
---|
369 | bool fKeepMediumLockList = false)
|
---|
370 | : Medium::Task(aMedium, aProgress),
|
---|
371 | mpMediumLockList(aMediumLockList),
|
---|
372 | mTarget(aTarget),
|
---|
373 | mVariant(aVariant),
|
---|
374 | mTargetCaller(aTarget),
|
---|
375 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
376 | {
|
---|
377 | AssertReturnVoidStmt(aTarget != NULL, mRC = E_FAIL);
|
---|
378 | mRC = mTargetCaller.rc();
|
---|
379 | if (FAILED(mRC))
|
---|
380 | return;
|
---|
381 | m_strTaskName = "createDiff";
|
---|
382 | }
|
---|
383 |
|
---|
384 | ~CreateDiffTask()
|
---|
385 | {
|
---|
386 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
387 | delete mpMediumLockList;
|
---|
388 | }
|
---|
389 |
|
---|
390 | MediumLockList *mpMediumLockList;
|
---|
391 |
|
---|
392 | const ComObjPtr<Medium> mTarget;
|
---|
393 | MediumVariant_T mVariant;
|
---|
394 |
|
---|
395 | private:
|
---|
396 | HRESULT executeTask();
|
---|
397 | AutoCaller mTargetCaller;
|
---|
398 | bool mfKeepMediumLockList;
|
---|
399 | };
|
---|
400 |
|
---|
401 | class Medium::CloneTask : public Medium::Task
|
---|
402 | {
|
---|
403 | public:
|
---|
404 | CloneTask(Medium *aMedium,
|
---|
405 | Progress *aProgress,
|
---|
406 | Medium *aTarget,
|
---|
407 | MediumVariant_T aVariant,
|
---|
408 | Medium *aParent,
|
---|
409 | uint32_t idxSrcImageSame,
|
---|
410 | uint32_t idxDstImageSame,
|
---|
411 | MediumLockList *aSourceMediumLockList,
|
---|
412 | MediumLockList *aTargetMediumLockList,
|
---|
413 | bool fKeepSourceMediumLockList = false,
|
---|
414 | bool fKeepTargetMediumLockList = false)
|
---|
415 | : Medium::Task(aMedium, aProgress),
|
---|
416 | mTarget(aTarget),
|
---|
417 | mParent(aParent),
|
---|
418 | mpSourceMediumLockList(aSourceMediumLockList),
|
---|
419 | mpTargetMediumLockList(aTargetMediumLockList),
|
---|
420 | mVariant(aVariant),
|
---|
421 | midxSrcImageSame(idxSrcImageSame),
|
---|
422 | midxDstImageSame(idxDstImageSame),
|
---|
423 | mTargetCaller(aTarget),
|
---|
424 | mParentCaller(aParent),
|
---|
425 | mfKeepSourceMediumLockList(fKeepSourceMediumLockList),
|
---|
426 | mfKeepTargetMediumLockList(fKeepTargetMediumLockList)
|
---|
427 | {
|
---|
428 | AssertReturnVoidStmt(aTarget != NULL, mRC = E_FAIL);
|
---|
429 | mRC = mTargetCaller.rc();
|
---|
430 | if (FAILED(mRC))
|
---|
431 | return;
|
---|
432 | /* aParent may be NULL */
|
---|
433 | mRC = mParentCaller.rc();
|
---|
434 | if (FAILED(mRC))
|
---|
435 | return;
|
---|
436 | AssertReturnVoidStmt(aSourceMediumLockList != NULL, mRC = E_FAIL);
|
---|
437 | AssertReturnVoidStmt(aTargetMediumLockList != NULL, mRC = E_FAIL);
|
---|
438 | m_strTaskName = "createClone";
|
---|
439 | }
|
---|
440 |
|
---|
441 | ~CloneTask()
|
---|
442 | {
|
---|
443 | if (!mfKeepSourceMediumLockList && mpSourceMediumLockList)
|
---|
444 | delete mpSourceMediumLockList;
|
---|
445 | if (!mfKeepTargetMediumLockList && mpTargetMediumLockList)
|
---|
446 | delete mpTargetMediumLockList;
|
---|
447 | }
|
---|
448 |
|
---|
449 | const ComObjPtr<Medium> mTarget;
|
---|
450 | const ComObjPtr<Medium> mParent;
|
---|
451 | MediumLockList *mpSourceMediumLockList;
|
---|
452 | MediumLockList *mpTargetMediumLockList;
|
---|
453 | MediumVariant_T mVariant;
|
---|
454 | uint32_t midxSrcImageSame;
|
---|
455 | uint32_t midxDstImageSame;
|
---|
456 |
|
---|
457 | private:
|
---|
458 | HRESULT executeTask();
|
---|
459 | AutoCaller mTargetCaller;
|
---|
460 | AutoCaller mParentCaller;
|
---|
461 | bool mfKeepSourceMediumLockList;
|
---|
462 | bool mfKeepTargetMediumLockList;
|
---|
463 | };
|
---|
464 |
|
---|
465 | class Medium::MoveTask : public Medium::Task
|
---|
466 | {
|
---|
467 | public:
|
---|
468 | MoveTask(Medium *aMedium,
|
---|
469 | Progress *aProgress,
|
---|
470 | MediumVariant_T aVariant,
|
---|
471 | MediumLockList *aMediumLockList,
|
---|
472 | bool fKeepMediumLockList = false)
|
---|
473 | : Medium::Task(aMedium, aProgress),
|
---|
474 | mpMediumLockList(aMediumLockList),
|
---|
475 | mVariant(aVariant),
|
---|
476 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
477 | {
|
---|
478 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
479 | m_strTaskName = "createMove";
|
---|
480 | }
|
---|
481 |
|
---|
482 | ~MoveTask()
|
---|
483 | {
|
---|
484 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
485 | delete mpMediumLockList;
|
---|
486 | }
|
---|
487 |
|
---|
488 | MediumLockList *mpMediumLockList;
|
---|
489 | MediumVariant_T mVariant;
|
---|
490 |
|
---|
491 | private:
|
---|
492 | HRESULT executeTask();
|
---|
493 | bool mfKeepMediumLockList;
|
---|
494 | };
|
---|
495 |
|
---|
496 | class Medium::CompactTask : public Medium::Task
|
---|
497 | {
|
---|
498 | public:
|
---|
499 | CompactTask(Medium *aMedium,
|
---|
500 | Progress *aProgress,
|
---|
501 | MediumLockList *aMediumLockList,
|
---|
502 | bool fKeepMediumLockList = false)
|
---|
503 | : Medium::Task(aMedium, aProgress),
|
---|
504 | mpMediumLockList(aMediumLockList),
|
---|
505 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
506 | {
|
---|
507 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
508 | m_strTaskName = "createCompact";
|
---|
509 | }
|
---|
510 |
|
---|
511 | ~CompactTask()
|
---|
512 | {
|
---|
513 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
514 | delete mpMediumLockList;
|
---|
515 | }
|
---|
516 |
|
---|
517 | MediumLockList *mpMediumLockList;
|
---|
518 |
|
---|
519 | private:
|
---|
520 | HRESULT executeTask();
|
---|
521 | bool mfKeepMediumLockList;
|
---|
522 | };
|
---|
523 |
|
---|
524 | class Medium::ResizeTask : public Medium::Task
|
---|
525 | {
|
---|
526 | public:
|
---|
527 | ResizeTask(Medium *aMedium,
|
---|
528 | uint64_t aSize,
|
---|
529 | Progress *aProgress,
|
---|
530 | MediumLockList *aMediumLockList,
|
---|
531 | bool fKeepMediumLockList = false)
|
---|
532 | : Medium::Task(aMedium, aProgress),
|
---|
533 | mSize(aSize),
|
---|
534 | mpMediumLockList(aMediumLockList),
|
---|
535 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
536 | {
|
---|
537 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
538 | m_strTaskName = "createResize";
|
---|
539 | }
|
---|
540 |
|
---|
541 | ~ResizeTask()
|
---|
542 | {
|
---|
543 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
544 | delete mpMediumLockList;
|
---|
545 | }
|
---|
546 |
|
---|
547 | uint64_t mSize;
|
---|
548 | MediumLockList *mpMediumLockList;
|
---|
549 |
|
---|
550 | private:
|
---|
551 | HRESULT executeTask();
|
---|
552 | bool mfKeepMediumLockList;
|
---|
553 | };
|
---|
554 |
|
---|
555 | class Medium::ResetTask : public Medium::Task
|
---|
556 | {
|
---|
557 | public:
|
---|
558 | ResetTask(Medium *aMedium,
|
---|
559 | Progress *aProgress,
|
---|
560 | MediumLockList *aMediumLockList,
|
---|
561 | bool fKeepMediumLockList = false)
|
---|
562 | : Medium::Task(aMedium, aProgress),
|
---|
563 | mpMediumLockList(aMediumLockList),
|
---|
564 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
565 | {
|
---|
566 | m_strTaskName = "createReset";
|
---|
567 | }
|
---|
568 |
|
---|
569 | ~ResetTask()
|
---|
570 | {
|
---|
571 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
572 | delete mpMediumLockList;
|
---|
573 | }
|
---|
574 |
|
---|
575 | MediumLockList *mpMediumLockList;
|
---|
576 |
|
---|
577 | private:
|
---|
578 | HRESULT executeTask();
|
---|
579 | bool mfKeepMediumLockList;
|
---|
580 | };
|
---|
581 |
|
---|
582 | class Medium::DeleteTask : public Medium::Task
|
---|
583 | {
|
---|
584 | public:
|
---|
585 | DeleteTask(Medium *aMedium,
|
---|
586 | Progress *aProgress,
|
---|
587 | MediumLockList *aMediumLockList,
|
---|
588 | bool fKeepMediumLockList = false)
|
---|
589 | : Medium::Task(aMedium, aProgress),
|
---|
590 | mpMediumLockList(aMediumLockList),
|
---|
591 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
592 | {
|
---|
593 | m_strTaskName = "createDelete";
|
---|
594 | }
|
---|
595 |
|
---|
596 | ~DeleteTask()
|
---|
597 | {
|
---|
598 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
599 | delete mpMediumLockList;
|
---|
600 | }
|
---|
601 |
|
---|
602 | MediumLockList *mpMediumLockList;
|
---|
603 |
|
---|
604 | private:
|
---|
605 | HRESULT executeTask();
|
---|
606 | bool mfKeepMediumLockList;
|
---|
607 | };
|
---|
608 |
|
---|
609 | class Medium::MergeTask : public Medium::Task
|
---|
610 | {
|
---|
611 | public:
|
---|
612 | MergeTask(Medium *aMedium,
|
---|
613 | Medium *aTarget,
|
---|
614 | bool fMergeForward,
|
---|
615 | Medium *aParentForTarget,
|
---|
616 | MediumLockList *aChildrenToReparent,
|
---|
617 | Progress *aProgress,
|
---|
618 | MediumLockList *aMediumLockList,
|
---|
619 | bool fKeepMediumLockList = false)
|
---|
620 | : Medium::Task(aMedium, aProgress),
|
---|
621 | mTarget(aTarget),
|
---|
622 | mfMergeForward(fMergeForward),
|
---|
623 | mParentForTarget(aParentForTarget),
|
---|
624 | mpChildrenToReparent(aChildrenToReparent),
|
---|
625 | mpMediumLockList(aMediumLockList),
|
---|
626 | mTargetCaller(aTarget),
|
---|
627 | mParentForTargetCaller(aParentForTarget),
|
---|
628 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
629 | {
|
---|
630 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
631 | m_strTaskName = "createMerge";
|
---|
632 | }
|
---|
633 |
|
---|
634 | ~MergeTask()
|
---|
635 | {
|
---|
636 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
637 | delete mpMediumLockList;
|
---|
638 | if (mpChildrenToReparent)
|
---|
639 | delete mpChildrenToReparent;
|
---|
640 | }
|
---|
641 |
|
---|
642 | const ComObjPtr<Medium> mTarget;
|
---|
643 | bool mfMergeForward;
|
---|
644 | /* When mpChildrenToReparent is null then mParentForTarget is non-null and
|
---|
645 | * vice versa. In other words: they are used in different cases. */
|
---|
646 | const ComObjPtr<Medium> mParentForTarget;
|
---|
647 | MediumLockList *mpChildrenToReparent;
|
---|
648 | MediumLockList *mpMediumLockList;
|
---|
649 |
|
---|
650 | private:
|
---|
651 | HRESULT executeTask();
|
---|
652 | AutoCaller mTargetCaller;
|
---|
653 | AutoCaller mParentForTargetCaller;
|
---|
654 | bool mfKeepMediumLockList;
|
---|
655 | };
|
---|
656 |
|
---|
657 | class Medium::ImportTask : public Medium::Task
|
---|
658 | {
|
---|
659 | public:
|
---|
660 | ImportTask(Medium *aMedium,
|
---|
661 | Progress *aProgress,
|
---|
662 | const char *aFilename,
|
---|
663 | MediumFormat *aFormat,
|
---|
664 | MediumVariant_T aVariant,
|
---|
665 | RTVFSIOSTREAM aVfsIosSrc,
|
---|
666 | Medium *aParent,
|
---|
667 | MediumLockList *aTargetMediumLockList,
|
---|
668 | bool fKeepTargetMediumLockList = false)
|
---|
669 | : Medium::Task(aMedium, aProgress),
|
---|
670 | mFilename(aFilename),
|
---|
671 | mFormat(aFormat),
|
---|
672 | mVariant(aVariant),
|
---|
673 | mParent(aParent),
|
---|
674 | mpTargetMediumLockList(aTargetMediumLockList),
|
---|
675 | mpVfsIoIf(NULL),
|
---|
676 | mParentCaller(aParent),
|
---|
677 | mfKeepTargetMediumLockList(fKeepTargetMediumLockList)
|
---|
678 | {
|
---|
679 | AssertReturnVoidStmt(aTargetMediumLockList != NULL, mRC = E_FAIL);
|
---|
680 | /* aParent may be NULL */
|
---|
681 | mRC = mParentCaller.rc();
|
---|
682 | if (FAILED(mRC))
|
---|
683 | return;
|
---|
684 |
|
---|
685 | mVDImageIfaces = aMedium->m->vdImageIfaces;
|
---|
686 |
|
---|
687 | int vrc = VDIfCreateFromVfsStream(aVfsIosSrc, RTFILE_O_READ, &mpVfsIoIf);
|
---|
688 | AssertRCReturnVoidStmt(vrc, mRC = E_FAIL);
|
---|
689 |
|
---|
690 | vrc = VDInterfaceAdd(&mpVfsIoIf->Core, "Medium::ImportTaskVfsIos",
|
---|
691 | VDINTERFACETYPE_IO, mpVfsIoIf,
|
---|
692 | sizeof(VDINTERFACEIO), &mVDImageIfaces);
|
---|
693 | AssertRCReturnVoidStmt(vrc, mRC = E_FAIL);
|
---|
694 | m_strTaskName = "createImport";
|
---|
695 | }
|
---|
696 |
|
---|
697 | ~ImportTask()
|
---|
698 | {
|
---|
699 | if (!mfKeepTargetMediumLockList && mpTargetMediumLockList)
|
---|
700 | delete mpTargetMediumLockList;
|
---|
701 | if (mpVfsIoIf)
|
---|
702 | {
|
---|
703 | VDIfDestroyFromVfsStream(mpVfsIoIf);
|
---|
704 | mpVfsIoIf = NULL;
|
---|
705 | }
|
---|
706 | }
|
---|
707 |
|
---|
708 | Utf8Str mFilename;
|
---|
709 | ComObjPtr<MediumFormat> mFormat;
|
---|
710 | MediumVariant_T mVariant;
|
---|
711 | const ComObjPtr<Medium> mParent;
|
---|
712 | MediumLockList *mpTargetMediumLockList;
|
---|
713 | PVDINTERFACE mVDImageIfaces;
|
---|
714 | PVDINTERFACEIO mpVfsIoIf; /**< Pointer to the VFS I/O stream to VD I/O interface wrapper. */
|
---|
715 |
|
---|
716 | private:
|
---|
717 | HRESULT executeTask();
|
---|
718 | AutoCaller mParentCaller;
|
---|
719 | bool mfKeepTargetMediumLockList;
|
---|
720 | };
|
---|
721 |
|
---|
722 | class Medium::EncryptTask : public Medium::Task
|
---|
723 | {
|
---|
724 | public:
|
---|
725 | EncryptTask(Medium *aMedium,
|
---|
726 | const com::Utf8Str &strNewPassword,
|
---|
727 | const com::Utf8Str &strCurrentPassword,
|
---|
728 | const com::Utf8Str &strCipher,
|
---|
729 | const com::Utf8Str &strNewPasswordId,
|
---|
730 | Progress *aProgress,
|
---|
731 | MediumLockList *aMediumLockList)
|
---|
732 | : Medium::Task(aMedium, aProgress),
|
---|
733 | mstrNewPassword(strNewPassword),
|
---|
734 | mstrCurrentPassword(strCurrentPassword),
|
---|
735 | mstrCipher(strCipher),
|
---|
736 | mstrNewPasswordId(strNewPasswordId),
|
---|
737 | mpMediumLockList(aMediumLockList)
|
---|
738 | {
|
---|
739 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
740 | /* aParent may be NULL */
|
---|
741 | mRC = mParentCaller.rc();
|
---|
742 | if (FAILED(mRC))
|
---|
743 | return;
|
---|
744 |
|
---|
745 | mVDImageIfaces = aMedium->m->vdImageIfaces;
|
---|
746 | m_strTaskName = "createEncrypt";
|
---|
747 | }
|
---|
748 |
|
---|
749 | ~EncryptTask()
|
---|
750 | {
|
---|
751 | if (mstrNewPassword.length())
|
---|
752 | RTMemWipeThoroughly(mstrNewPassword.mutableRaw(), mstrNewPassword.length(), 10 /* cPasses */);
|
---|
753 | if (mstrCurrentPassword.length())
|
---|
754 | RTMemWipeThoroughly(mstrCurrentPassword.mutableRaw(), mstrCurrentPassword.length(), 10 /* cPasses */);
|
---|
755 |
|
---|
756 | /* Keep any errors which might be set when deleting the lock list. */
|
---|
757 | ErrorInfoKeeper eik;
|
---|
758 | delete mpMediumLockList;
|
---|
759 | }
|
---|
760 |
|
---|
761 | Utf8Str mstrNewPassword;
|
---|
762 | Utf8Str mstrCurrentPassword;
|
---|
763 | Utf8Str mstrCipher;
|
---|
764 | Utf8Str mstrNewPasswordId;
|
---|
765 | MediumLockList *mpMediumLockList;
|
---|
766 | PVDINTERFACE mVDImageIfaces;
|
---|
767 |
|
---|
768 | private:
|
---|
769 | HRESULT executeTask();
|
---|
770 | AutoCaller mParentCaller;
|
---|
771 | };
|
---|
772 |
|
---|
773 | /**
|
---|
774 | * Implementation code for the "create base" task.
|
---|
775 | */
|
---|
776 | HRESULT Medium::CreateBaseTask::executeTask()
|
---|
777 | {
|
---|
778 | return mMedium->i_taskCreateBaseHandler(*this);
|
---|
779 | }
|
---|
780 |
|
---|
781 | /**
|
---|
782 | * Implementation code for the "create diff" task.
|
---|
783 | */
|
---|
784 | HRESULT Medium::CreateDiffTask::executeTask()
|
---|
785 | {
|
---|
786 | return mMedium->i_taskCreateDiffHandler(*this);
|
---|
787 | }
|
---|
788 |
|
---|
789 | /**
|
---|
790 | * Implementation code for the "clone" task.
|
---|
791 | */
|
---|
792 | HRESULT Medium::CloneTask::executeTask()
|
---|
793 | {
|
---|
794 | return mMedium->i_taskCloneHandler(*this);
|
---|
795 | }
|
---|
796 |
|
---|
797 | /**
|
---|
798 | * Implementation code for the "move" task.
|
---|
799 | */
|
---|
800 | HRESULT Medium::MoveTask::executeTask()
|
---|
801 | {
|
---|
802 | return mMedium->i_taskMoveHandler(*this);
|
---|
803 | }
|
---|
804 |
|
---|
805 | /**
|
---|
806 | * Implementation code for the "compact" task.
|
---|
807 | */
|
---|
808 | HRESULT Medium::CompactTask::executeTask()
|
---|
809 | {
|
---|
810 | return mMedium->i_taskCompactHandler(*this);
|
---|
811 | }
|
---|
812 |
|
---|
813 | /**
|
---|
814 | * Implementation code for the "resize" task.
|
---|
815 | */
|
---|
816 | HRESULT Medium::ResizeTask::executeTask()
|
---|
817 | {
|
---|
818 | return mMedium->i_taskResizeHandler(*this);
|
---|
819 | }
|
---|
820 |
|
---|
821 |
|
---|
822 | /**
|
---|
823 | * Implementation code for the "reset" task.
|
---|
824 | */
|
---|
825 | HRESULT Medium::ResetTask::executeTask()
|
---|
826 | {
|
---|
827 | return mMedium->i_taskResetHandler(*this);
|
---|
828 | }
|
---|
829 |
|
---|
830 | /**
|
---|
831 | * Implementation code for the "delete" task.
|
---|
832 | */
|
---|
833 | HRESULT Medium::DeleteTask::executeTask()
|
---|
834 | {
|
---|
835 | return mMedium->i_taskDeleteHandler(*this);
|
---|
836 | }
|
---|
837 |
|
---|
838 | /**
|
---|
839 | * Implementation code for the "merge" task.
|
---|
840 | */
|
---|
841 | HRESULT Medium::MergeTask::executeTask()
|
---|
842 | {
|
---|
843 | return mMedium->i_taskMergeHandler(*this);
|
---|
844 | }
|
---|
845 |
|
---|
846 | /**
|
---|
847 | * Implementation code for the "import" task.
|
---|
848 | */
|
---|
849 | HRESULT Medium::ImportTask::executeTask()
|
---|
850 | {
|
---|
851 | return mMedium->i_taskImportHandler(*this);
|
---|
852 | }
|
---|
853 |
|
---|
854 | /**
|
---|
855 | * Implementation code for the "encrypt" task.
|
---|
856 | */
|
---|
857 | HRESULT Medium::EncryptTask::executeTask()
|
---|
858 | {
|
---|
859 | return mMedium->i_taskEncryptHandler(*this);
|
---|
860 | }
|
---|
861 |
|
---|
862 | ////////////////////////////////////////////////////////////////////////////////
|
---|
863 | //
|
---|
864 | // Medium constructor / destructor
|
---|
865 | //
|
---|
866 | ////////////////////////////////////////////////////////////////////////////////
|
---|
867 |
|
---|
868 | DEFINE_EMPTY_CTOR_DTOR(Medium)
|
---|
869 |
|
---|
870 | HRESULT Medium::FinalConstruct()
|
---|
871 | {
|
---|
872 | m = new Data;
|
---|
873 |
|
---|
874 | /* Initialize the callbacks of the VD error interface */
|
---|
875 | m->vdIfError.pfnError = i_vdErrorCall;
|
---|
876 | m->vdIfError.pfnMessage = NULL;
|
---|
877 |
|
---|
878 | /* Initialize the callbacks of the VD config interface */
|
---|
879 | m->vdIfConfig.pfnAreKeysValid = i_vdConfigAreKeysValid;
|
---|
880 | m->vdIfConfig.pfnQuerySize = i_vdConfigQuerySize;
|
---|
881 | m->vdIfConfig.pfnQuery = i_vdConfigQuery;
|
---|
882 | m->vdIfConfig.pfnQueryBytes = NULL;
|
---|
883 |
|
---|
884 | /* Initialize the callbacks of the VD TCP interface (we always use the host
|
---|
885 | * IP stack for now) */
|
---|
886 | m->vdIfTcpNet.pfnSocketCreate = i_vdTcpSocketCreate;
|
---|
887 | m->vdIfTcpNet.pfnSocketDestroy = i_vdTcpSocketDestroy;
|
---|
888 | m->vdIfTcpNet.pfnClientConnect = i_vdTcpClientConnect;
|
---|
889 | m->vdIfTcpNet.pfnClientClose = i_vdTcpClientClose;
|
---|
890 | m->vdIfTcpNet.pfnIsClientConnected = i_vdTcpIsClientConnected;
|
---|
891 | m->vdIfTcpNet.pfnSelectOne = i_vdTcpSelectOne;
|
---|
892 | m->vdIfTcpNet.pfnRead = i_vdTcpRead;
|
---|
893 | m->vdIfTcpNet.pfnWrite = i_vdTcpWrite;
|
---|
894 | m->vdIfTcpNet.pfnSgWrite = i_vdTcpSgWrite;
|
---|
895 | m->vdIfTcpNet.pfnFlush = i_vdTcpFlush;
|
---|
896 | m->vdIfTcpNet.pfnSetSendCoalescing = i_vdTcpSetSendCoalescing;
|
---|
897 | m->vdIfTcpNet.pfnGetLocalAddress = i_vdTcpGetLocalAddress;
|
---|
898 | m->vdIfTcpNet.pfnGetPeerAddress = i_vdTcpGetPeerAddress;
|
---|
899 | m->vdIfTcpNet.pfnSelectOneEx = NULL;
|
---|
900 | m->vdIfTcpNet.pfnPoke = NULL;
|
---|
901 |
|
---|
902 | /* Initialize the per-disk interface chain (could be done more globally,
|
---|
903 | * but it's not wasting much time or space so it's not worth it). */
|
---|
904 | int vrc;
|
---|
905 | vrc = VDInterfaceAdd(&m->vdIfError.Core,
|
---|
906 | "Medium::vdInterfaceError",
|
---|
907 | VDINTERFACETYPE_ERROR, this,
|
---|
908 | sizeof(VDINTERFACEERROR), &m->vdDiskIfaces);
|
---|
909 | AssertRCReturn(vrc, E_FAIL);
|
---|
910 |
|
---|
911 | /* Initialize the per-image interface chain */
|
---|
912 | vrc = VDInterfaceAdd(&m->vdIfConfig.Core,
|
---|
913 | "Medium::vdInterfaceConfig",
|
---|
914 | VDINTERFACETYPE_CONFIG, this,
|
---|
915 | sizeof(VDINTERFACECONFIG), &m->vdImageIfaces);
|
---|
916 | AssertRCReturn(vrc, E_FAIL);
|
---|
917 |
|
---|
918 | vrc = VDInterfaceAdd(&m->vdIfTcpNet.Core,
|
---|
919 | "Medium::vdInterfaceTcpNet",
|
---|
920 | VDINTERFACETYPE_TCPNET, this,
|
---|
921 | sizeof(VDINTERFACETCPNET), &m->vdImageIfaces);
|
---|
922 | AssertRCReturn(vrc, E_FAIL);
|
---|
923 |
|
---|
924 | return BaseFinalConstruct();
|
---|
925 | }
|
---|
926 |
|
---|
927 | void Medium::FinalRelease()
|
---|
928 | {
|
---|
929 | uninit();
|
---|
930 |
|
---|
931 | delete m;
|
---|
932 |
|
---|
933 | BaseFinalRelease();
|
---|
934 | }
|
---|
935 |
|
---|
936 | /**
|
---|
937 | * Initializes an empty hard disk object without creating or opening an associated
|
---|
938 | * storage unit.
|
---|
939 | *
|
---|
940 | * This gets called by VirtualBox::CreateMedium() in which case uuidMachineRegistry
|
---|
941 | * is empty since starting with VirtualBox 4.0, we no longer add opened media to a
|
---|
942 | * registry automatically (this is deferred until the medium is attached to a machine).
|
---|
943 | *
|
---|
944 | * This also gets called when VirtualBox creates diff images; in this case uuidMachineRegistry
|
---|
945 | * is set to the registry of the parent image to make sure they all end up in the same
|
---|
946 | * file.
|
---|
947 | *
|
---|
948 | * For hard disks that don't have the MediumFormatCapabilities_CreateFixed or
|
---|
949 | * MediumFormatCapabilities_CreateDynamic capability (and therefore cannot be created or deleted
|
---|
950 | * with the means of VirtualBox) the associated storage unit is assumed to be
|
---|
951 | * ready for use so the state of the hard disk object will be set to Created.
|
---|
952 | *
|
---|
953 | * @param aVirtualBox VirtualBox object.
|
---|
954 | * @param aFormat
|
---|
955 | * @param aLocation Storage unit location.
|
---|
956 | * @param uuidMachineRegistry The registry to which this medium should be added
|
---|
957 | * (global registry UUID or machine UUID or empty if none).
|
---|
958 | * @param aDeviceType Device Type.
|
---|
959 | */
|
---|
960 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
961 | const Utf8Str &aFormat,
|
---|
962 | const Utf8Str &aLocation,
|
---|
963 | const Guid &uuidMachineRegistry,
|
---|
964 | const DeviceType_T aDeviceType)
|
---|
965 | {
|
---|
966 | AssertReturn(aVirtualBox != NULL, E_FAIL);
|
---|
967 | AssertReturn(!aFormat.isEmpty(), E_FAIL);
|
---|
968 |
|
---|
969 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
970 | AutoInitSpan autoInitSpan(this);
|
---|
971 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
972 |
|
---|
973 | HRESULT rc = S_OK;
|
---|
974 |
|
---|
975 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
976 |
|
---|
977 | if (uuidMachineRegistry.isValid() && !uuidMachineRegistry.isZero())
|
---|
978 | m->llRegistryIDs.push_back(uuidMachineRegistry);
|
---|
979 |
|
---|
980 | /* no storage yet */
|
---|
981 | m->state = MediumState_NotCreated;
|
---|
982 |
|
---|
983 | /* cannot be a host drive */
|
---|
984 | m->hostDrive = false;
|
---|
985 |
|
---|
986 | m->devType = aDeviceType;
|
---|
987 |
|
---|
988 | /* No storage unit is created yet, no need to call Medium::i_queryInfo */
|
---|
989 |
|
---|
990 | rc = i_setFormat(aFormat);
|
---|
991 | if (FAILED(rc)) return rc;
|
---|
992 |
|
---|
993 | rc = i_setLocation(aLocation);
|
---|
994 | if (FAILED(rc)) return rc;
|
---|
995 |
|
---|
996 | if (!(m->formatObj->i_getCapabilities() & ( MediumFormatCapabilities_CreateFixed
|
---|
997 | | MediumFormatCapabilities_CreateDynamic))
|
---|
998 | )
|
---|
999 | {
|
---|
1000 | /* Storage for mediums of this format can neither be explicitly
|
---|
1001 | * created by VirtualBox nor deleted, so we place the medium to
|
---|
1002 | * Inaccessible state here and also add it to the registry. The
|
---|
1003 | * state means that one has to use RefreshState() to update the
|
---|
1004 | * medium format specific fields. */
|
---|
1005 | m->state = MediumState_Inaccessible;
|
---|
1006 | // create new UUID
|
---|
1007 | unconst(m->id).create();
|
---|
1008 |
|
---|
1009 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1010 | ComObjPtr<Medium> pMedium;
|
---|
1011 |
|
---|
1012 | /*
|
---|
1013 | * Check whether the UUID is taken already and create a new one
|
---|
1014 | * if required.
|
---|
1015 | * Try this only a limited amount of times in case the PRNG is broken
|
---|
1016 | * in some way to prevent an endless loop.
|
---|
1017 | */
|
---|
1018 | for (unsigned i = 0; i < 5; i++)
|
---|
1019 | {
|
---|
1020 | bool fInUse;
|
---|
1021 |
|
---|
1022 | fInUse = m->pVirtualBox->i_isMediaUuidInUse(m->id, aDeviceType);
|
---|
1023 | if (fInUse)
|
---|
1024 | {
|
---|
1025 | // create new UUID
|
---|
1026 | unconst(m->id).create();
|
---|
1027 | }
|
---|
1028 | else
|
---|
1029 | break;
|
---|
1030 | }
|
---|
1031 |
|
---|
1032 | rc = m->pVirtualBox->i_registerMedium(this, &pMedium, treeLock);
|
---|
1033 | Assert(this == pMedium || FAILED(rc));
|
---|
1034 | }
|
---|
1035 |
|
---|
1036 | /* Confirm a successful initialization when it's the case */
|
---|
1037 | if (SUCCEEDED(rc))
|
---|
1038 | autoInitSpan.setSucceeded();
|
---|
1039 |
|
---|
1040 | return rc;
|
---|
1041 | }
|
---|
1042 |
|
---|
1043 | /**
|
---|
1044 | * Initializes the medium object by opening the storage unit at the specified
|
---|
1045 | * location. The enOpenMode parameter defines whether the medium will be opened
|
---|
1046 | * read/write or read-only.
|
---|
1047 | *
|
---|
1048 | * This gets called by VirtualBox::OpenMedium() and also by
|
---|
1049 | * Machine::AttachDevice() and createImplicitDiffs() when new diff
|
---|
1050 | * images are created.
|
---|
1051 | *
|
---|
1052 | * There is no registry for this case since starting with VirtualBox 4.0, we
|
---|
1053 | * no longer add opened media to a registry automatically (this is deferred
|
---|
1054 | * until the medium is attached to a machine).
|
---|
1055 | *
|
---|
1056 | * For hard disks, the UUID, format and the parent of this medium will be
|
---|
1057 | * determined when reading the medium storage unit. For DVD and floppy images,
|
---|
1058 | * which have no UUIDs in their storage units, new UUIDs are created.
|
---|
1059 | * If the detected or set parent is not known to VirtualBox, then this method
|
---|
1060 | * will fail.
|
---|
1061 | *
|
---|
1062 | * @param aVirtualBox VirtualBox object.
|
---|
1063 | * @param aLocation Storage unit location.
|
---|
1064 | * @param enOpenMode Whether to open the medium read/write or read-only.
|
---|
1065 | * @param fForceNewUuid Whether a new UUID should be set to avoid duplicates.
|
---|
1066 | * @param aDeviceType Device type of medium.
|
---|
1067 | */
|
---|
1068 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1069 | const Utf8Str &aLocation,
|
---|
1070 | HDDOpenMode enOpenMode,
|
---|
1071 | bool fForceNewUuid,
|
---|
1072 | DeviceType_T aDeviceType)
|
---|
1073 | {
|
---|
1074 | AssertReturn(aVirtualBox, E_INVALIDARG);
|
---|
1075 | AssertReturn(!aLocation.isEmpty(), E_INVALIDARG);
|
---|
1076 |
|
---|
1077 | HRESULT rc = S_OK;
|
---|
1078 |
|
---|
1079 | {
|
---|
1080 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1081 | AutoInitSpan autoInitSpan(this);
|
---|
1082 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1083 |
|
---|
1084 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1085 |
|
---|
1086 | /* there must be a storage unit */
|
---|
1087 | m->state = MediumState_Created;
|
---|
1088 |
|
---|
1089 | /* remember device type for correct unregistering later */
|
---|
1090 | m->devType = aDeviceType;
|
---|
1091 |
|
---|
1092 | /* cannot be a host drive */
|
---|
1093 | m->hostDrive = false;
|
---|
1094 |
|
---|
1095 | /* remember the open mode (defaults to ReadWrite) */
|
---|
1096 | m->hddOpenMode = enOpenMode;
|
---|
1097 |
|
---|
1098 | if (aDeviceType == DeviceType_DVD)
|
---|
1099 | m->type = MediumType_Readonly;
|
---|
1100 | else if (aDeviceType == DeviceType_Floppy)
|
---|
1101 | m->type = MediumType_Writethrough;
|
---|
1102 |
|
---|
1103 | rc = i_setLocation(aLocation);
|
---|
1104 | if (FAILED(rc)) return rc;
|
---|
1105 |
|
---|
1106 | /* get all the information about the medium from the storage unit */
|
---|
1107 | if (fForceNewUuid)
|
---|
1108 | unconst(m->uuidImage).create();
|
---|
1109 |
|
---|
1110 | m->state = MediumState_Inaccessible;
|
---|
1111 | m->strLastAccessError = tr("Accessibility check was not yet performed");
|
---|
1112 |
|
---|
1113 | /* Confirm a successful initialization before the call to i_queryInfo.
|
---|
1114 | * Otherwise we can end up with a AutoCaller deadlock because the
|
---|
1115 | * medium becomes visible but is not marked as initialized. Causes
|
---|
1116 | * locking trouble (e.g. trying to save media registries) which is
|
---|
1117 | * hard to solve. */
|
---|
1118 | autoInitSpan.setSucceeded();
|
---|
1119 | }
|
---|
1120 |
|
---|
1121 | /* we're normal code from now on, no longer init */
|
---|
1122 | AutoCaller autoCaller(this);
|
---|
1123 | if (FAILED(autoCaller.rc()))
|
---|
1124 | return autoCaller.rc();
|
---|
1125 |
|
---|
1126 | /* need to call i_queryInfo immediately to correctly place the medium in
|
---|
1127 | * the respective media tree and update other information such as uuid */
|
---|
1128 | rc = i_queryInfo(fForceNewUuid /* fSetImageId */, false /* fSetParentId */,
|
---|
1129 | autoCaller);
|
---|
1130 | if (SUCCEEDED(rc))
|
---|
1131 | {
|
---|
1132 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1133 |
|
---|
1134 | /* if the storage unit is not accessible, it's not acceptable for the
|
---|
1135 | * newly opened media so convert this into an error */
|
---|
1136 | if (m->state == MediumState_Inaccessible)
|
---|
1137 | {
|
---|
1138 | Assert(!m->strLastAccessError.isEmpty());
|
---|
1139 | rc = setError(E_FAIL, "%s", m->strLastAccessError.c_str());
|
---|
1140 | alock.release();
|
---|
1141 | autoCaller.release();
|
---|
1142 | uninit();
|
---|
1143 | }
|
---|
1144 | else
|
---|
1145 | {
|
---|
1146 | AssertStmt(!m->id.isZero(),
|
---|
1147 | alock.release(); autoCaller.release(); uninit(); return E_FAIL);
|
---|
1148 |
|
---|
1149 | /* storage format must be detected by Medium::i_queryInfo if the
|
---|
1150 | * medium is accessible */
|
---|
1151 | AssertStmt(!m->strFormat.isEmpty(),
|
---|
1152 | alock.release(); autoCaller.release(); uninit(); return E_FAIL);
|
---|
1153 | }
|
---|
1154 | }
|
---|
1155 | else
|
---|
1156 | {
|
---|
1157 | /* opening this image failed, mark the object as dead */
|
---|
1158 | autoCaller.release();
|
---|
1159 | uninit();
|
---|
1160 | }
|
---|
1161 |
|
---|
1162 | return rc;
|
---|
1163 | }
|
---|
1164 |
|
---|
1165 | /**
|
---|
1166 | * Initializes the medium object by loading its data from the given settings
|
---|
1167 | * node. The medium will always be opened read/write.
|
---|
1168 | *
|
---|
1169 | * In this case, since we're loading from a registry, uuidMachineRegistry is
|
---|
1170 | * always set: it's either the global registry UUID or a machine UUID when
|
---|
1171 | * loading from a per-machine registry.
|
---|
1172 | *
|
---|
1173 | * @param aParent Parent medium disk or NULL for a root (base) medium.
|
---|
1174 | * @param aDeviceType Device type of the medium.
|
---|
1175 | * @param uuidMachineRegistry The registry to which this medium should be
|
---|
1176 | * added (global registry UUID or machine UUID).
|
---|
1177 | * @param data Configuration settings.
|
---|
1178 | * @param strMachineFolder The machine folder with which to resolve relative paths;
|
---|
1179 | * if empty, then we use the VirtualBox home directory
|
---|
1180 | *
|
---|
1181 | * @note Locks the medium tree for writing.
|
---|
1182 | */
|
---|
1183 | HRESULT Medium::initOne(Medium *aParent,
|
---|
1184 | DeviceType_T aDeviceType,
|
---|
1185 | const Guid &uuidMachineRegistry,
|
---|
1186 | const settings::Medium &data,
|
---|
1187 | const Utf8Str &strMachineFolder)
|
---|
1188 | {
|
---|
1189 | HRESULT rc;
|
---|
1190 |
|
---|
1191 | if (uuidMachineRegistry.isValid() && !uuidMachineRegistry.isZero())
|
---|
1192 | m->llRegistryIDs.push_back(uuidMachineRegistry);
|
---|
1193 |
|
---|
1194 | /* register with VirtualBox/parent early, since uninit() will
|
---|
1195 | * unconditionally unregister on failure */
|
---|
1196 | if (aParent)
|
---|
1197 | {
|
---|
1198 | // differencing medium: add to parent
|
---|
1199 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1200 | // no need to check maximum depth as settings reading did it
|
---|
1201 | i_setParent(aParent);
|
---|
1202 | }
|
---|
1203 |
|
---|
1204 | /* see below why we don't call Medium::i_queryInfo (and therefore treat
|
---|
1205 | * the medium as inaccessible for now */
|
---|
1206 | m->state = MediumState_Inaccessible;
|
---|
1207 | m->strLastAccessError = tr("Accessibility check was not yet performed");
|
---|
1208 |
|
---|
1209 | /* required */
|
---|
1210 | unconst(m->id) = data.uuid;
|
---|
1211 |
|
---|
1212 | /* assume not a host drive */
|
---|
1213 | m->hostDrive = false;
|
---|
1214 |
|
---|
1215 | /* optional */
|
---|
1216 | m->strDescription = data.strDescription;
|
---|
1217 |
|
---|
1218 | /* required */
|
---|
1219 | if (aDeviceType == DeviceType_HardDisk)
|
---|
1220 | {
|
---|
1221 | AssertReturn(!data.strFormat.isEmpty(), E_FAIL);
|
---|
1222 | rc = i_setFormat(data.strFormat);
|
---|
1223 | if (FAILED(rc)) return rc;
|
---|
1224 | }
|
---|
1225 | else
|
---|
1226 | {
|
---|
1227 | /// @todo handle host drive settings here as well?
|
---|
1228 | if (!data.strFormat.isEmpty())
|
---|
1229 | rc = i_setFormat(data.strFormat);
|
---|
1230 | else
|
---|
1231 | rc = i_setFormat("RAW");
|
---|
1232 | if (FAILED(rc)) return rc;
|
---|
1233 | }
|
---|
1234 |
|
---|
1235 | /* optional, only for diffs, default is false; we can only auto-reset
|
---|
1236 | * diff media so they must have a parent */
|
---|
1237 | if (aParent != NULL)
|
---|
1238 | m->autoReset = data.fAutoReset;
|
---|
1239 | else
|
---|
1240 | m->autoReset = false;
|
---|
1241 |
|
---|
1242 | /* properties (after setting the format as it populates the map). Note that
|
---|
1243 | * if some properties are not supported but present in the settings file,
|
---|
1244 | * they will still be read and accessible (for possible backward
|
---|
1245 | * compatibility; we can also clean them up from the XML upon next
|
---|
1246 | * XML format version change if we wish) */
|
---|
1247 | for (settings::StringsMap::const_iterator it = data.properties.begin();
|
---|
1248 | it != data.properties.end();
|
---|
1249 | ++it)
|
---|
1250 | {
|
---|
1251 | const Utf8Str &name = it->first;
|
---|
1252 | const Utf8Str &value = it->second;
|
---|
1253 | m->mapProperties[name] = value;
|
---|
1254 | }
|
---|
1255 |
|
---|
1256 | /* try to decrypt an optional iSCSI initiator secret */
|
---|
1257 | settings::StringsMap::const_iterator itCph = data.properties.find("InitiatorSecretEncrypted");
|
---|
1258 | if ( itCph != data.properties.end()
|
---|
1259 | && !itCph->second.isEmpty())
|
---|
1260 | {
|
---|
1261 | Utf8Str strPlaintext;
|
---|
1262 | int vrc = m->pVirtualBox->i_decryptSetting(&strPlaintext, itCph->second);
|
---|
1263 | if (RT_SUCCESS(vrc))
|
---|
1264 | m->mapProperties["InitiatorSecret"] = strPlaintext;
|
---|
1265 | }
|
---|
1266 |
|
---|
1267 | Utf8Str strFull;
|
---|
1268 | if (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
1269 | {
|
---|
1270 | // compose full path of the medium, if it's not fully qualified...
|
---|
1271 | // slightly convoluted logic here. If the caller has given us a
|
---|
1272 | // machine folder, then a relative path will be relative to that:
|
---|
1273 | if ( !strMachineFolder.isEmpty()
|
---|
1274 | && !RTPathStartsWithRoot(data.strLocation.c_str())
|
---|
1275 | )
|
---|
1276 | {
|
---|
1277 | strFull = strMachineFolder;
|
---|
1278 | strFull += RTPATH_SLASH;
|
---|
1279 | strFull += data.strLocation;
|
---|
1280 | }
|
---|
1281 | else
|
---|
1282 | {
|
---|
1283 | // Otherwise use the old VirtualBox "make absolute path" logic:
|
---|
1284 | rc = m->pVirtualBox->i_calculateFullPath(data.strLocation, strFull);
|
---|
1285 | if (FAILED(rc)) return rc;
|
---|
1286 | }
|
---|
1287 | }
|
---|
1288 | else
|
---|
1289 | strFull = data.strLocation;
|
---|
1290 |
|
---|
1291 | rc = i_setLocation(strFull);
|
---|
1292 | if (FAILED(rc)) return rc;
|
---|
1293 |
|
---|
1294 | if (aDeviceType == DeviceType_HardDisk)
|
---|
1295 | {
|
---|
1296 | /* type is only for base hard disks */
|
---|
1297 | if (m->pParent.isNull())
|
---|
1298 | m->type = data.hdType;
|
---|
1299 | }
|
---|
1300 | else if (aDeviceType == DeviceType_DVD)
|
---|
1301 | m->type = MediumType_Readonly;
|
---|
1302 | else
|
---|
1303 | m->type = MediumType_Writethrough;
|
---|
1304 |
|
---|
1305 | /* remember device type for correct unregistering later */
|
---|
1306 | m->devType = aDeviceType;
|
---|
1307 |
|
---|
1308 | LogFlowThisFunc(("m->strLocationFull='%s', m->strFormat=%s, m->id={%RTuuid}\n",
|
---|
1309 | m->strLocationFull.c_str(), m->strFormat.c_str(), m->id.raw()));
|
---|
1310 |
|
---|
1311 | return S_OK;
|
---|
1312 | }
|
---|
1313 |
|
---|
1314 | /**
|
---|
1315 | * Initializes the medium object and its children by loading its data from the
|
---|
1316 | * given settings node. The medium will always be opened read/write.
|
---|
1317 | *
|
---|
1318 | * In this case, since we're loading from a registry, uuidMachineRegistry is
|
---|
1319 | * always set: it's either the global registry UUID or a machine UUID when
|
---|
1320 | * loading from a per-machine registry.
|
---|
1321 | *
|
---|
1322 | * @param aVirtualBox VirtualBox object.
|
---|
1323 | * @param aParent Parent medium disk or NULL for a root (base) medium.
|
---|
1324 | * @param aDeviceType Device type of the medium.
|
---|
1325 | * @param uuidMachineRegistry The registry to which this medium should be added
|
---|
1326 | * (global registry UUID or machine UUID).
|
---|
1327 | * @param data Configuration settings.
|
---|
1328 | * @param strMachineFolder The machine folder with which to resolve relative
|
---|
1329 | * paths; if empty, then we use the VirtualBox home directory
|
---|
1330 | * @param mediaTreeLock Autolock.
|
---|
1331 | *
|
---|
1332 | * @note Locks the medium tree for writing.
|
---|
1333 | */
|
---|
1334 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1335 | Medium *aParent,
|
---|
1336 | DeviceType_T aDeviceType,
|
---|
1337 | const Guid &uuidMachineRegistry,
|
---|
1338 | const settings::Medium &data,
|
---|
1339 | const Utf8Str &strMachineFolder,
|
---|
1340 | AutoWriteLock &mediaTreeLock)
|
---|
1341 | {
|
---|
1342 | using namespace settings;
|
---|
1343 |
|
---|
1344 | Assert(aVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
1345 | AssertReturn(aVirtualBox, E_INVALIDARG);
|
---|
1346 |
|
---|
1347 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1348 | AutoInitSpan autoInitSpan(this);
|
---|
1349 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1350 |
|
---|
1351 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1352 |
|
---|
1353 | // Do not inline this method call, as the purpose of having this separate
|
---|
1354 | // is to save on stack size. Less local variables are the key for reaching
|
---|
1355 | // deep recursion levels with small stack (XPCOM/g++ without optimization).
|
---|
1356 | HRESULT rc = initOne(aParent, aDeviceType, uuidMachineRegistry, data, strMachineFolder);
|
---|
1357 |
|
---|
1358 |
|
---|
1359 | /* Don't call Medium::i_queryInfo for registered media to prevent the calling
|
---|
1360 | * thread (i.e. the VirtualBox server startup thread) from an unexpected
|
---|
1361 | * freeze but mark it as initially inaccessible instead. The vital UUID,
|
---|
1362 | * location and format properties are read from the registry file above; to
|
---|
1363 | * get the actual state and the rest of the data, the user will have to call
|
---|
1364 | * COMGETTER(State). */
|
---|
1365 |
|
---|
1366 | /* load all children */
|
---|
1367 | for (settings::MediaList::const_iterator it = data.llChildren.begin();
|
---|
1368 | it != data.llChildren.end();
|
---|
1369 | ++it)
|
---|
1370 | {
|
---|
1371 | const settings::Medium &med = *it;
|
---|
1372 |
|
---|
1373 | ComObjPtr<Medium> pMedium;
|
---|
1374 | pMedium.createObject();
|
---|
1375 | rc = pMedium->init(aVirtualBox,
|
---|
1376 | this, // parent
|
---|
1377 | aDeviceType,
|
---|
1378 | uuidMachineRegistry,
|
---|
1379 | med, // child data
|
---|
1380 | strMachineFolder,
|
---|
1381 | mediaTreeLock);
|
---|
1382 | if (FAILED(rc)) break;
|
---|
1383 |
|
---|
1384 | rc = m->pVirtualBox->i_registerMedium(pMedium, &pMedium, mediaTreeLock);
|
---|
1385 | if (FAILED(rc)) break;
|
---|
1386 | }
|
---|
1387 |
|
---|
1388 | /* Confirm a successful initialization when it's the case */
|
---|
1389 | if (SUCCEEDED(rc))
|
---|
1390 | autoInitSpan.setSucceeded();
|
---|
1391 |
|
---|
1392 | return rc;
|
---|
1393 | }
|
---|
1394 |
|
---|
1395 | /**
|
---|
1396 | * Initializes the medium object by providing the host drive information.
|
---|
1397 | * Not used for anything but the host floppy/host DVD case.
|
---|
1398 | *
|
---|
1399 | * There is no registry for this case.
|
---|
1400 | *
|
---|
1401 | * @param aVirtualBox VirtualBox object.
|
---|
1402 | * @param aDeviceType Device type of the medium.
|
---|
1403 | * @param aLocation Location of the host drive.
|
---|
1404 | * @param aDescription Comment for this host drive.
|
---|
1405 | *
|
---|
1406 | * @note Locks VirtualBox lock for writing.
|
---|
1407 | */
|
---|
1408 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1409 | DeviceType_T aDeviceType,
|
---|
1410 | const Utf8Str &aLocation,
|
---|
1411 | const Utf8Str &aDescription /* = Utf8Str::Empty */)
|
---|
1412 | {
|
---|
1413 | ComAssertRet(aDeviceType == DeviceType_DVD || aDeviceType == DeviceType_Floppy, E_INVALIDARG);
|
---|
1414 | ComAssertRet(!aLocation.isEmpty(), E_INVALIDARG);
|
---|
1415 |
|
---|
1416 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1417 | AutoInitSpan autoInitSpan(this);
|
---|
1418 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1419 |
|
---|
1420 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1421 |
|
---|
1422 | // We do not store host drives in VirtualBox.xml or anywhere else, so if we want
|
---|
1423 | // host drives to be identifiable by UUID and not give the drive a different UUID
|
---|
1424 | // every time VirtualBox starts, we need to fake a reproducible UUID here:
|
---|
1425 | RTUUID uuid;
|
---|
1426 | RTUuidClear(&uuid);
|
---|
1427 | if (aDeviceType == DeviceType_DVD)
|
---|
1428 | memcpy(&uuid.au8[0], "DVD", 3);
|
---|
1429 | else
|
---|
1430 | memcpy(&uuid.au8[0], "FD", 2);
|
---|
1431 | /* use device name, adjusted to the end of uuid, shortened if necessary */
|
---|
1432 | size_t lenLocation = aLocation.length();
|
---|
1433 | if (lenLocation > 12)
|
---|
1434 | memcpy(&uuid.au8[4], aLocation.c_str() + (lenLocation - 12), 12);
|
---|
1435 | else
|
---|
1436 | memcpy(&uuid.au8[4 + 12 - lenLocation], aLocation.c_str(), lenLocation);
|
---|
1437 | unconst(m->id) = uuid;
|
---|
1438 |
|
---|
1439 | if (aDeviceType == DeviceType_DVD)
|
---|
1440 | m->type = MediumType_Readonly;
|
---|
1441 | else
|
---|
1442 | m->type = MediumType_Writethrough;
|
---|
1443 | m->devType = aDeviceType;
|
---|
1444 | m->state = MediumState_Created;
|
---|
1445 | m->hostDrive = true;
|
---|
1446 | HRESULT rc = i_setFormat("RAW");
|
---|
1447 | if (FAILED(rc)) return rc;
|
---|
1448 | rc = i_setLocation(aLocation);
|
---|
1449 | if (FAILED(rc)) return rc;
|
---|
1450 | m->strDescription = aDescription;
|
---|
1451 |
|
---|
1452 | autoInitSpan.setSucceeded();
|
---|
1453 | return S_OK;
|
---|
1454 | }
|
---|
1455 |
|
---|
1456 | /**
|
---|
1457 | * Uninitializes the instance.
|
---|
1458 | *
|
---|
1459 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
1460 | *
|
---|
1461 | * @note All children of this medium get uninitialized by calling their
|
---|
1462 | * uninit() methods.
|
---|
1463 | */
|
---|
1464 | void Medium::uninit()
|
---|
1465 | {
|
---|
1466 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1467 | * the pVirtualBox reference, and in this case we don't need to continue.
|
---|
1468 | * Normally this would be handled through the AutoUninitSpan magic, however
|
---|
1469 | * this cannot be done at this point as the media tree must be locked
|
---|
1470 | * before reaching the AutoUninitSpan, otherwise deadlocks can happen.
|
---|
1471 | *
|
---|
1472 | * NOTE: The tree lock is higher priority than the medium caller and medium
|
---|
1473 | * object locks, i.e. the medium caller may have to be released and be
|
---|
1474 | * re-acquired in the right place later. See Medium::getParent() for sample
|
---|
1475 | * code how to do this safely. */
|
---|
1476 | VirtualBox *pVirtualBox = m->pVirtualBox;
|
---|
1477 | if (!pVirtualBox)
|
---|
1478 | return;
|
---|
1479 |
|
---|
1480 | /* Caller must not hold the object or media tree lock over uninit(). */
|
---|
1481 | Assert(!isWriteLockOnCurrentThread());
|
---|
1482 | Assert(!pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
1483 |
|
---|
1484 | AutoWriteLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1485 |
|
---|
1486 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
1487 | AutoUninitSpan autoUninitSpan(this);
|
---|
1488 | if (autoUninitSpan.uninitDone())
|
---|
1489 | return;
|
---|
1490 |
|
---|
1491 | if (!m->formatObj.isNull())
|
---|
1492 | m->formatObj.setNull();
|
---|
1493 |
|
---|
1494 | if (m->state == MediumState_Deleting)
|
---|
1495 | {
|
---|
1496 | /* This medium has been already deleted (directly or as part of a
|
---|
1497 | * merge). Reparenting has already been done. */
|
---|
1498 | Assert(m->pParent.isNull());
|
---|
1499 | }
|
---|
1500 | else
|
---|
1501 | {
|
---|
1502 | MediaList llChildren(m->llChildren);
|
---|
1503 | m->llChildren.clear();
|
---|
1504 | autoUninitSpan.setSucceeded();
|
---|
1505 |
|
---|
1506 | while (!llChildren.empty())
|
---|
1507 | {
|
---|
1508 | ComObjPtr<Medium> pChild = llChildren.front();
|
---|
1509 | llChildren.pop_front();
|
---|
1510 | pChild->m->pParent.setNull();
|
---|
1511 | treeLock.release();
|
---|
1512 | pChild->uninit();
|
---|
1513 | treeLock.acquire();
|
---|
1514 | }
|
---|
1515 |
|
---|
1516 | if (m->pParent)
|
---|
1517 | {
|
---|
1518 | // this is a differencing disk: then remove it from the parent's children list
|
---|
1519 | i_deparent();
|
---|
1520 | }
|
---|
1521 | }
|
---|
1522 |
|
---|
1523 | unconst(m->pVirtualBox) = NULL;
|
---|
1524 | }
|
---|
1525 |
|
---|
1526 | /**
|
---|
1527 | * Internal helper that removes "this" from the list of children of its
|
---|
1528 | * parent. Used in uninit() and other places when reparenting is necessary.
|
---|
1529 | *
|
---|
1530 | * The caller must hold the medium tree lock!
|
---|
1531 | */
|
---|
1532 | void Medium::i_deparent()
|
---|
1533 | {
|
---|
1534 | MediaList &llParent = m->pParent->m->llChildren;
|
---|
1535 | for (MediaList::iterator it = llParent.begin();
|
---|
1536 | it != llParent.end();
|
---|
1537 | ++it)
|
---|
1538 | {
|
---|
1539 | Medium *pParentsChild = *it;
|
---|
1540 | if (this == pParentsChild)
|
---|
1541 | {
|
---|
1542 | llParent.erase(it);
|
---|
1543 | break;
|
---|
1544 | }
|
---|
1545 | }
|
---|
1546 | m->pParent.setNull();
|
---|
1547 | }
|
---|
1548 |
|
---|
1549 | /**
|
---|
1550 | * Internal helper that removes "this" from the list of children of its
|
---|
1551 | * parent. Used in uninit() and other places when reparenting is necessary.
|
---|
1552 | *
|
---|
1553 | * The caller must hold the medium tree lock!
|
---|
1554 | */
|
---|
1555 | void Medium::i_setParent(const ComObjPtr<Medium> &pParent)
|
---|
1556 | {
|
---|
1557 | m->pParent = pParent;
|
---|
1558 | if (pParent)
|
---|
1559 | pParent->m->llChildren.push_back(this);
|
---|
1560 | }
|
---|
1561 |
|
---|
1562 |
|
---|
1563 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1564 | //
|
---|
1565 | // IMedium public methods
|
---|
1566 | //
|
---|
1567 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1568 |
|
---|
1569 | HRESULT Medium::getId(com::Guid &aId)
|
---|
1570 | {
|
---|
1571 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1572 |
|
---|
1573 | aId = m->id;
|
---|
1574 |
|
---|
1575 | return S_OK;
|
---|
1576 | }
|
---|
1577 |
|
---|
1578 | HRESULT Medium::getDescription(AutoCaller &autoCaller, com::Utf8Str &aDescription)
|
---|
1579 | {
|
---|
1580 | NOREF(autoCaller);
|
---|
1581 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1582 |
|
---|
1583 | aDescription = m->strDescription;
|
---|
1584 |
|
---|
1585 | return S_OK;
|
---|
1586 | }
|
---|
1587 |
|
---|
1588 | HRESULT Medium::setDescription(AutoCaller &autoCaller, const com::Utf8Str &aDescription)
|
---|
1589 | {
|
---|
1590 | /// @todo update m->strDescription and save the global registry (and local
|
---|
1591 | /// registries of portable VMs referring to this medium), this will also
|
---|
1592 | /// require to add the mRegistered flag to data
|
---|
1593 |
|
---|
1594 | HRESULT rc = S_OK;
|
---|
1595 |
|
---|
1596 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
1597 |
|
---|
1598 | try
|
---|
1599 | {
|
---|
1600 | autoCaller.release();
|
---|
1601 |
|
---|
1602 | // to avoid redundant locking, which just takes a time, just call required functions.
|
---|
1603 | // the error will be just stored and will be reported after locks will be acquired again
|
---|
1604 |
|
---|
1605 | const char *pszError = NULL;
|
---|
1606 |
|
---|
1607 |
|
---|
1608 | /* Build the lock list. */
|
---|
1609 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
1610 | this /* pToLockWrite */,
|
---|
1611 | true /* fMediumLockWriteAll */,
|
---|
1612 | NULL,
|
---|
1613 | *pMediumLockList);
|
---|
1614 | if (FAILED(rc))
|
---|
1615 | {
|
---|
1616 | pszError = tr("Failed to create medium lock list for '%s'");
|
---|
1617 | }
|
---|
1618 | else
|
---|
1619 | {
|
---|
1620 | rc = pMediumLockList->Lock();
|
---|
1621 | if (FAILED(rc))
|
---|
1622 | pszError = tr("Failed to lock media '%s'");
|
---|
1623 | }
|
---|
1624 |
|
---|
1625 | // locking: we need the tree lock first because we access parent pointers
|
---|
1626 | // and we need to write-lock the media involved
|
---|
1627 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1628 |
|
---|
1629 | autoCaller.add();
|
---|
1630 | AssertComRCThrowRC(autoCaller.rc());
|
---|
1631 |
|
---|
1632 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1633 |
|
---|
1634 | if (FAILED(rc))
|
---|
1635 | throw setError(rc, pszError, i_getLocationFull().c_str());
|
---|
1636 |
|
---|
1637 | /* Set a new description */
|
---|
1638 | m->strDescription = aDescription;
|
---|
1639 |
|
---|
1640 | // save the settings
|
---|
1641 | alock.release();
|
---|
1642 | autoCaller.release();
|
---|
1643 | treeLock.release();
|
---|
1644 | i_markRegistriesModified();
|
---|
1645 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
1646 | }
|
---|
1647 | catch (HRESULT aRC) { rc = aRC; }
|
---|
1648 |
|
---|
1649 | delete pMediumLockList;
|
---|
1650 |
|
---|
1651 | return rc;
|
---|
1652 | }
|
---|
1653 |
|
---|
1654 | HRESULT Medium::getState(MediumState_T *aState)
|
---|
1655 | {
|
---|
1656 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1657 | *aState = m->state;
|
---|
1658 |
|
---|
1659 | return S_OK;
|
---|
1660 | }
|
---|
1661 |
|
---|
1662 | HRESULT Medium::getVariant(std::vector<MediumVariant_T> &aVariant)
|
---|
1663 | {
|
---|
1664 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1665 |
|
---|
1666 | const size_t cBits = sizeof(MediumVariant_T) * 8;
|
---|
1667 | aVariant.resize(cBits);
|
---|
1668 | for (size_t i = 0; i < cBits; ++i)
|
---|
1669 | aVariant[i] = (MediumVariant_T)(m->variant & RT_BIT(i));
|
---|
1670 |
|
---|
1671 | return S_OK;
|
---|
1672 | }
|
---|
1673 |
|
---|
1674 | HRESULT Medium::getLocation(com::Utf8Str &aLocation)
|
---|
1675 | {
|
---|
1676 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1677 |
|
---|
1678 | aLocation = m->strLocationFull;
|
---|
1679 |
|
---|
1680 | return S_OK;
|
---|
1681 | }
|
---|
1682 |
|
---|
1683 | HRESULT Medium::getName(com::Utf8Str &aName)
|
---|
1684 | {
|
---|
1685 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1686 |
|
---|
1687 | aName = i_getName();
|
---|
1688 |
|
---|
1689 | return S_OK;
|
---|
1690 | }
|
---|
1691 |
|
---|
1692 | HRESULT Medium::getDeviceType(DeviceType_T *aDeviceType)
|
---|
1693 | {
|
---|
1694 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1695 |
|
---|
1696 | *aDeviceType = m->devType;
|
---|
1697 |
|
---|
1698 | return S_OK;
|
---|
1699 | }
|
---|
1700 |
|
---|
1701 | HRESULT Medium::getHostDrive(BOOL *aHostDrive)
|
---|
1702 | {
|
---|
1703 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1704 |
|
---|
1705 | *aHostDrive = m->hostDrive;
|
---|
1706 |
|
---|
1707 | return S_OK;
|
---|
1708 | }
|
---|
1709 |
|
---|
1710 | HRESULT Medium::getSize(LONG64 *aSize)
|
---|
1711 | {
|
---|
1712 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1713 |
|
---|
1714 | *aSize = m->size;
|
---|
1715 |
|
---|
1716 | return S_OK;
|
---|
1717 | }
|
---|
1718 |
|
---|
1719 | HRESULT Medium::getFormat(com::Utf8Str &aFormat)
|
---|
1720 | {
|
---|
1721 | /* no need to lock, m->strFormat is const */
|
---|
1722 |
|
---|
1723 | aFormat = m->strFormat;
|
---|
1724 | return S_OK;
|
---|
1725 | }
|
---|
1726 |
|
---|
1727 | HRESULT Medium::getMediumFormat(ComPtr<IMediumFormat> &aMediumFormat)
|
---|
1728 | {
|
---|
1729 | /* no need to lock, m->formatObj is const */
|
---|
1730 | m->formatObj.queryInterfaceTo(aMediumFormat.asOutParam());
|
---|
1731 |
|
---|
1732 | return S_OK;
|
---|
1733 | }
|
---|
1734 |
|
---|
1735 | HRESULT Medium::getType(AutoCaller &autoCaller, MediumType_T *aType)
|
---|
1736 | {
|
---|
1737 | NOREF(autoCaller);
|
---|
1738 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1739 |
|
---|
1740 | *aType = m->type;
|
---|
1741 |
|
---|
1742 | return S_OK;
|
---|
1743 | }
|
---|
1744 |
|
---|
1745 | HRESULT Medium::setType(AutoCaller &autoCaller, MediumType_T aType)
|
---|
1746 | {
|
---|
1747 | autoCaller.release();
|
---|
1748 |
|
---|
1749 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1750 | * the pVirtualBox reference, see #uninit(). */
|
---|
1751 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
1752 |
|
---|
1753 | // we access m->pParent
|
---|
1754 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
1755 |
|
---|
1756 | autoCaller.add();
|
---|
1757 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1758 |
|
---|
1759 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1760 |
|
---|
1761 | switch (m->state)
|
---|
1762 | {
|
---|
1763 | case MediumState_Created:
|
---|
1764 | case MediumState_Inaccessible:
|
---|
1765 | break;
|
---|
1766 | default:
|
---|
1767 | return i_setStateError();
|
---|
1768 | }
|
---|
1769 |
|
---|
1770 | if (m->type == aType)
|
---|
1771 | {
|
---|
1772 | /* Nothing to do */
|
---|
1773 | return S_OK;
|
---|
1774 | }
|
---|
1775 |
|
---|
1776 | DeviceType_T devType = i_getDeviceType();
|
---|
1777 | // DVD media can only be readonly.
|
---|
1778 | if (devType == DeviceType_DVD && aType != MediumType_Readonly)
|
---|
1779 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1780 | tr("Cannot change the type of DVD medium '%s'"),
|
---|
1781 | m->strLocationFull.c_str());
|
---|
1782 | // Floppy media can only be writethrough or readonly.
|
---|
1783 | if ( devType == DeviceType_Floppy
|
---|
1784 | && aType != MediumType_Writethrough
|
---|
1785 | && aType != MediumType_Readonly)
|
---|
1786 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1787 | tr("Cannot change the type of floppy medium '%s'"),
|
---|
1788 | m->strLocationFull.c_str());
|
---|
1789 |
|
---|
1790 | /* cannot change the type of a differencing medium */
|
---|
1791 | if (m->pParent)
|
---|
1792 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1793 | tr("Cannot change the type of medium '%s' because it is a differencing medium"),
|
---|
1794 | m->strLocationFull.c_str());
|
---|
1795 |
|
---|
1796 | /* Cannot change the type of a medium being in use by more than one VM.
|
---|
1797 | * If the change is to Immutable or MultiAttach then it must not be
|
---|
1798 | * directly attached to any VM, otherwise the assumptions about indirect
|
---|
1799 | * attachment elsewhere are violated and the VM becomes inaccessible.
|
---|
1800 | * Attaching an immutable medium triggers the diff creation, and this is
|
---|
1801 | * vital for the correct operation. */
|
---|
1802 | if ( m->backRefs.size() > 1
|
---|
1803 | || ( ( aType == MediumType_Immutable
|
---|
1804 | || aType == MediumType_MultiAttach)
|
---|
1805 | && m->backRefs.size() > 0))
|
---|
1806 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1807 | tr("Cannot change the type of medium '%s' because it is attached to %d virtual machines"),
|
---|
1808 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
1809 |
|
---|
1810 | switch (aType)
|
---|
1811 | {
|
---|
1812 | case MediumType_Normal:
|
---|
1813 | case MediumType_Immutable:
|
---|
1814 | case MediumType_MultiAttach:
|
---|
1815 | {
|
---|
1816 | /* normal can be easily converted to immutable and vice versa even
|
---|
1817 | * if they have children as long as they are not attached to any
|
---|
1818 | * machine themselves */
|
---|
1819 | break;
|
---|
1820 | }
|
---|
1821 | case MediumType_Writethrough:
|
---|
1822 | case MediumType_Shareable:
|
---|
1823 | case MediumType_Readonly:
|
---|
1824 | {
|
---|
1825 | /* cannot change to writethrough, shareable or readonly
|
---|
1826 | * if there are children */
|
---|
1827 | if (i_getChildren().size() != 0)
|
---|
1828 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
1829 | tr("Cannot change type for medium '%s' since it has %d child media"),
|
---|
1830 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
1831 | if (aType == MediumType_Shareable)
|
---|
1832 | {
|
---|
1833 | MediumVariant_T variant = i_getVariant();
|
---|
1834 | if (!(variant & MediumVariant_Fixed))
|
---|
1835 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1836 | tr("Cannot change type for medium '%s' to 'Shareable' since it is a dynamic medium storage unit"),
|
---|
1837 | m->strLocationFull.c_str());
|
---|
1838 | }
|
---|
1839 | else if (aType == MediumType_Readonly && devType == DeviceType_HardDisk)
|
---|
1840 | {
|
---|
1841 | // Readonly hard disks are not allowed, this medium type is reserved for
|
---|
1842 | // DVDs and floppy images at the moment. Later we might allow readonly hard
|
---|
1843 | // disks, but that's extremely unusual and many guest OSes will have trouble.
|
---|
1844 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1845 | tr("Cannot change type for medium '%s' to 'Readonly' since it is a hard disk"),
|
---|
1846 | m->strLocationFull.c_str());
|
---|
1847 | }
|
---|
1848 | break;
|
---|
1849 | }
|
---|
1850 | default:
|
---|
1851 | AssertFailedReturn(E_FAIL);
|
---|
1852 | }
|
---|
1853 |
|
---|
1854 | if (aType == MediumType_MultiAttach)
|
---|
1855 | {
|
---|
1856 | // This type is new with VirtualBox 4.0 and therefore requires settings
|
---|
1857 | // version 1.11 in the settings backend. Unfortunately it is not enough to do
|
---|
1858 | // the usual routine in MachineConfigFile::bumpSettingsVersionIfNeeded() for
|
---|
1859 | // two reasons: The medium type is a property of the media registry tree, which
|
---|
1860 | // can reside in the global config file (for pre-4.0 media); we would therefore
|
---|
1861 | // possibly need to bump the global config version. We don't want to do that though
|
---|
1862 | // because that might make downgrading to pre-4.0 impossible.
|
---|
1863 | // As a result, we can only use these two new types if the medium is NOT in the
|
---|
1864 | // global registry:
|
---|
1865 | const Guid &uuidGlobalRegistry = m->pVirtualBox->i_getGlobalRegistryId();
|
---|
1866 | if (i_isInRegistry(uuidGlobalRegistry))
|
---|
1867 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1868 | tr("Cannot change type for medium '%s': the media type 'MultiAttach' can only be used "
|
---|
1869 | "on media registered with a machine that was created with VirtualBox 4.0 or later"),
|
---|
1870 | m->strLocationFull.c_str());
|
---|
1871 | }
|
---|
1872 |
|
---|
1873 | m->type = aType;
|
---|
1874 |
|
---|
1875 | // save the settings
|
---|
1876 | mlock.release();
|
---|
1877 | autoCaller.release();
|
---|
1878 | treeLock.release();
|
---|
1879 | i_markRegistriesModified();
|
---|
1880 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
1881 |
|
---|
1882 | return S_OK;
|
---|
1883 | }
|
---|
1884 |
|
---|
1885 | HRESULT Medium::getAllowedTypes(std::vector<MediumType_T> &aAllowedTypes)
|
---|
1886 | {
|
---|
1887 | NOREF(aAllowedTypes);
|
---|
1888 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1889 |
|
---|
1890 | ReturnComNotImplemented();
|
---|
1891 | }
|
---|
1892 |
|
---|
1893 | HRESULT Medium::getParent(AutoCaller &autoCaller, ComPtr<IMedium> &aParent)
|
---|
1894 | {
|
---|
1895 | autoCaller.release();
|
---|
1896 |
|
---|
1897 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1898 | * the pVirtualBox reference, see #uninit(). */
|
---|
1899 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
1900 |
|
---|
1901 | /* we access m->pParent */
|
---|
1902 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
1903 |
|
---|
1904 | autoCaller.add();
|
---|
1905 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1906 |
|
---|
1907 | m->pParent.queryInterfaceTo(aParent.asOutParam());
|
---|
1908 |
|
---|
1909 | return S_OK;
|
---|
1910 | }
|
---|
1911 |
|
---|
1912 | HRESULT Medium::getChildren(AutoCaller &autoCaller, std::vector<ComPtr<IMedium> > &aChildren)
|
---|
1913 | {
|
---|
1914 | autoCaller.release();
|
---|
1915 |
|
---|
1916 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1917 | * the pVirtualBox reference, see #uninit(). */
|
---|
1918 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
1919 |
|
---|
1920 | /* we access children */
|
---|
1921 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
1922 |
|
---|
1923 | autoCaller.add();
|
---|
1924 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1925 |
|
---|
1926 | MediaList children(this->i_getChildren());
|
---|
1927 | aChildren.resize(children.size());
|
---|
1928 | size_t i = 0;
|
---|
1929 | for (MediaList::const_iterator it = children.begin(); it != children.end(); ++it, ++i)
|
---|
1930 | (*it).queryInterfaceTo(aChildren[i].asOutParam());
|
---|
1931 | return S_OK;
|
---|
1932 | }
|
---|
1933 |
|
---|
1934 | HRESULT Medium::getBase(AutoCaller &autoCaller, ComPtr<IMedium> &aBase)
|
---|
1935 | {
|
---|
1936 | autoCaller.release();
|
---|
1937 |
|
---|
1938 | /* i_getBase() will do callers/locking */
|
---|
1939 | i_getBase().queryInterfaceTo(aBase.asOutParam());
|
---|
1940 |
|
---|
1941 | return S_OK;
|
---|
1942 | }
|
---|
1943 |
|
---|
1944 | HRESULT Medium::getReadOnly(AutoCaller &autoCaller, BOOL *aReadOnly)
|
---|
1945 | {
|
---|
1946 | autoCaller.release();
|
---|
1947 |
|
---|
1948 | /* isReadOnly() will do locking */
|
---|
1949 | *aReadOnly = i_isReadOnly();
|
---|
1950 |
|
---|
1951 | return S_OK;
|
---|
1952 | }
|
---|
1953 |
|
---|
1954 | HRESULT Medium::getLogicalSize(LONG64 *aLogicalSize)
|
---|
1955 | {
|
---|
1956 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1957 |
|
---|
1958 | *aLogicalSize = m->logicalSize;
|
---|
1959 |
|
---|
1960 | return S_OK;
|
---|
1961 | }
|
---|
1962 |
|
---|
1963 | HRESULT Medium::getAutoReset(BOOL *aAutoReset)
|
---|
1964 | {
|
---|
1965 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1966 |
|
---|
1967 | if (m->pParent.isNull())
|
---|
1968 | *aAutoReset = FALSE;
|
---|
1969 | else
|
---|
1970 | *aAutoReset = m->autoReset;
|
---|
1971 |
|
---|
1972 | return S_OK;
|
---|
1973 | }
|
---|
1974 |
|
---|
1975 | HRESULT Medium::setAutoReset(BOOL aAutoReset)
|
---|
1976 | {
|
---|
1977 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1978 |
|
---|
1979 | if (m->pParent.isNull())
|
---|
1980 | return setError(VBOX_E_NOT_SUPPORTED,
|
---|
1981 | tr("Medium '%s' is not differencing"),
|
---|
1982 | m->strLocationFull.c_str());
|
---|
1983 |
|
---|
1984 | if (m->autoReset != !!aAutoReset)
|
---|
1985 | {
|
---|
1986 | m->autoReset = !!aAutoReset;
|
---|
1987 |
|
---|
1988 | // save the settings
|
---|
1989 | mlock.release();
|
---|
1990 | i_markRegistriesModified();
|
---|
1991 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
1992 | }
|
---|
1993 |
|
---|
1994 | return S_OK;
|
---|
1995 | }
|
---|
1996 |
|
---|
1997 | HRESULT Medium::getLastAccessError(com::Utf8Str &aLastAccessError)
|
---|
1998 | {
|
---|
1999 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2000 |
|
---|
2001 | aLastAccessError = m->strLastAccessError;
|
---|
2002 |
|
---|
2003 | return S_OK;
|
---|
2004 | }
|
---|
2005 |
|
---|
2006 | HRESULT Medium::getMachineIds(std::vector<com::Guid> &aMachineIds)
|
---|
2007 | {
|
---|
2008 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2009 |
|
---|
2010 | if (m->backRefs.size() != 0)
|
---|
2011 | {
|
---|
2012 | BackRefList brlist(m->backRefs);
|
---|
2013 | aMachineIds.resize(brlist.size());
|
---|
2014 | size_t i = 0;
|
---|
2015 | for (BackRefList::const_iterator it = brlist.begin(); it != brlist.end(); ++it, ++i)
|
---|
2016 | aMachineIds[i] = it->machineId;
|
---|
2017 | }
|
---|
2018 |
|
---|
2019 | return S_OK;
|
---|
2020 | }
|
---|
2021 |
|
---|
2022 | HRESULT Medium::setIds(AutoCaller &autoCaller,
|
---|
2023 | BOOL aSetImageId,
|
---|
2024 | const com::Guid &aImageId,
|
---|
2025 | BOOL aSetParentId,
|
---|
2026 | const com::Guid &aParentId)
|
---|
2027 | {
|
---|
2028 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2029 |
|
---|
2030 | switch (m->state)
|
---|
2031 | {
|
---|
2032 | case MediumState_Created:
|
---|
2033 | break;
|
---|
2034 | default:
|
---|
2035 | return i_setStateError();
|
---|
2036 | }
|
---|
2037 |
|
---|
2038 | Guid imageId, parentId;
|
---|
2039 | if (aSetImageId)
|
---|
2040 | {
|
---|
2041 | if (aImageId.isZero())
|
---|
2042 | imageId.create();
|
---|
2043 | else
|
---|
2044 | {
|
---|
2045 | imageId = aImageId;
|
---|
2046 | if (!imageId.isValid())
|
---|
2047 | return setError(E_INVALIDARG, tr("Argument %s is invalid"), "aImageId");
|
---|
2048 | }
|
---|
2049 | }
|
---|
2050 | if (aSetParentId)
|
---|
2051 | {
|
---|
2052 | if (aParentId.isZero())
|
---|
2053 | parentId.create();
|
---|
2054 | else
|
---|
2055 | parentId = aParentId;
|
---|
2056 | }
|
---|
2057 |
|
---|
2058 | unconst(m->uuidImage) = imageId;
|
---|
2059 | unconst(m->uuidParentImage) = parentId;
|
---|
2060 |
|
---|
2061 | // must not hold any locks before calling Medium::i_queryInfo
|
---|
2062 | alock.release();
|
---|
2063 |
|
---|
2064 | HRESULT rc = i_queryInfo(!!aSetImageId /* fSetImageId */,
|
---|
2065 | !!aSetParentId /* fSetParentId */,
|
---|
2066 | autoCaller);
|
---|
2067 |
|
---|
2068 | return rc;
|
---|
2069 | }
|
---|
2070 |
|
---|
2071 | HRESULT Medium::refreshState(AutoCaller &autoCaller, MediumState_T *aState)
|
---|
2072 | {
|
---|
2073 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2074 |
|
---|
2075 | HRESULT rc = S_OK;
|
---|
2076 |
|
---|
2077 | switch (m->state)
|
---|
2078 | {
|
---|
2079 | case MediumState_Created:
|
---|
2080 | case MediumState_Inaccessible:
|
---|
2081 | case MediumState_LockedRead:
|
---|
2082 | {
|
---|
2083 | // must not hold any locks before calling Medium::i_queryInfo
|
---|
2084 | alock.release();
|
---|
2085 |
|
---|
2086 | rc = i_queryInfo(false /* fSetImageId */, false /* fSetParentId */,
|
---|
2087 | autoCaller);
|
---|
2088 |
|
---|
2089 | alock.acquire();
|
---|
2090 | break;
|
---|
2091 | }
|
---|
2092 | default:
|
---|
2093 | break;
|
---|
2094 | }
|
---|
2095 |
|
---|
2096 | *aState = m->state;
|
---|
2097 |
|
---|
2098 | return rc;
|
---|
2099 | }
|
---|
2100 |
|
---|
2101 | HRESULT Medium::getSnapshotIds(const com::Guid &aMachineId,
|
---|
2102 | std::vector<com::Guid> &aSnapshotIds)
|
---|
2103 | {
|
---|
2104 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2105 |
|
---|
2106 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
2107 | it != m->backRefs.end(); ++it)
|
---|
2108 | {
|
---|
2109 | if (it->machineId == aMachineId)
|
---|
2110 | {
|
---|
2111 | size_t size = it->llSnapshotIds.size();
|
---|
2112 |
|
---|
2113 | /* if the medium is attached to the machine in the current state, we
|
---|
2114 | * return its ID as the first element of the array */
|
---|
2115 | if (it->fInCurState)
|
---|
2116 | ++size;
|
---|
2117 |
|
---|
2118 | if (size > 0)
|
---|
2119 | {
|
---|
2120 | aSnapshotIds.resize(size);
|
---|
2121 |
|
---|
2122 | size_t j = 0;
|
---|
2123 | if (it->fInCurState)
|
---|
2124 | aSnapshotIds[j++] = it->machineId.toUtf16();
|
---|
2125 |
|
---|
2126 | for(GuidList::const_iterator jt = it->llSnapshotIds.begin(); jt != it->llSnapshotIds.end(); ++jt, ++j)
|
---|
2127 | aSnapshotIds[j] = (*jt);
|
---|
2128 | }
|
---|
2129 |
|
---|
2130 | break;
|
---|
2131 | }
|
---|
2132 | }
|
---|
2133 |
|
---|
2134 | return S_OK;
|
---|
2135 | }
|
---|
2136 |
|
---|
2137 | HRESULT Medium::lockRead(ComPtr<IToken> &aToken)
|
---|
2138 | {
|
---|
2139 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2140 |
|
---|
2141 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
2142 | if (m->queryInfoRunning)
|
---|
2143 | {
|
---|
2144 | /* Must not hold the media tree lock, as Medium::i_queryInfo needs this
|
---|
2145 | * lock and thus we would run into a deadlock here. */
|
---|
2146 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
2147 | while (m->queryInfoRunning)
|
---|
2148 | {
|
---|
2149 | alock.release();
|
---|
2150 | /* must not hold the object lock now */
|
---|
2151 | Assert(!isWriteLockOnCurrentThread());
|
---|
2152 | {
|
---|
2153 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
2154 | }
|
---|
2155 | alock.acquire();
|
---|
2156 | }
|
---|
2157 | }
|
---|
2158 |
|
---|
2159 | HRESULT rc = S_OK;
|
---|
2160 |
|
---|
2161 | switch (m->state)
|
---|
2162 | {
|
---|
2163 | case MediumState_Created:
|
---|
2164 | case MediumState_Inaccessible:
|
---|
2165 | case MediumState_LockedRead:
|
---|
2166 | {
|
---|
2167 | ++m->readers;
|
---|
2168 |
|
---|
2169 | ComAssertMsgBreak(m->readers != 0, ("Counter overflow"), rc = E_FAIL);
|
---|
2170 |
|
---|
2171 | /* Remember pre-lock state */
|
---|
2172 | if (m->state != MediumState_LockedRead)
|
---|
2173 | m->preLockState = m->state;
|
---|
2174 |
|
---|
2175 | LogFlowThisFunc(("Okay - prev state=%d readers=%d\n", m->state, m->readers));
|
---|
2176 | m->state = MediumState_LockedRead;
|
---|
2177 |
|
---|
2178 | ComObjPtr<MediumLockToken> pToken;
|
---|
2179 | rc = pToken.createObject();
|
---|
2180 | if (SUCCEEDED(rc))
|
---|
2181 | rc = pToken->init(this, false /* fWrite */);
|
---|
2182 | if (FAILED(rc))
|
---|
2183 | {
|
---|
2184 | --m->readers;
|
---|
2185 | if (m->readers == 0)
|
---|
2186 | m->state = m->preLockState;
|
---|
2187 | return rc;
|
---|
2188 | }
|
---|
2189 |
|
---|
2190 | pToken.queryInterfaceTo(aToken.asOutParam());
|
---|
2191 | break;
|
---|
2192 | }
|
---|
2193 | default:
|
---|
2194 | {
|
---|
2195 | LogFlowThisFunc(("Failing - state=%d\n", m->state));
|
---|
2196 | rc = i_setStateError();
|
---|
2197 | break;
|
---|
2198 | }
|
---|
2199 | }
|
---|
2200 |
|
---|
2201 | return rc;
|
---|
2202 | }
|
---|
2203 |
|
---|
2204 | /**
|
---|
2205 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
2206 | * in-process calls).
|
---|
2207 | */
|
---|
2208 | HRESULT Medium::i_unlockRead(MediumState_T *aState)
|
---|
2209 | {
|
---|
2210 | AutoCaller autoCaller(this);
|
---|
2211 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2212 |
|
---|
2213 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2214 |
|
---|
2215 | HRESULT rc = S_OK;
|
---|
2216 |
|
---|
2217 | switch (m->state)
|
---|
2218 | {
|
---|
2219 | case MediumState_LockedRead:
|
---|
2220 | {
|
---|
2221 | ComAssertMsgBreak(m->readers != 0, ("Counter underflow"), rc = E_FAIL);
|
---|
2222 | --m->readers;
|
---|
2223 |
|
---|
2224 | /* Reset the state after the last reader */
|
---|
2225 | if (m->readers == 0)
|
---|
2226 | {
|
---|
2227 | m->state = m->preLockState;
|
---|
2228 | /* There are cases where we inject the deleting state into
|
---|
2229 | * a medium locked for reading. Make sure #unmarkForDeletion()
|
---|
2230 | * gets the right state afterwards. */
|
---|
2231 | if (m->preLockState == MediumState_Deleting)
|
---|
2232 | m->preLockState = MediumState_Created;
|
---|
2233 | }
|
---|
2234 |
|
---|
2235 | LogFlowThisFunc(("new state=%d\n", m->state));
|
---|
2236 | break;
|
---|
2237 | }
|
---|
2238 | default:
|
---|
2239 | {
|
---|
2240 | LogFlowThisFunc(("Failing - state=%d\n", m->state));
|
---|
2241 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2242 | tr("Medium '%s' is not locked for reading"),
|
---|
2243 | m->strLocationFull.c_str());
|
---|
2244 | break;
|
---|
2245 | }
|
---|
2246 | }
|
---|
2247 |
|
---|
2248 | /* return the current state after */
|
---|
2249 | if (aState)
|
---|
2250 | *aState = m->state;
|
---|
2251 |
|
---|
2252 | return rc;
|
---|
2253 | }
|
---|
2254 | HRESULT Medium::lockWrite(ComPtr<IToken> &aToken)
|
---|
2255 | {
|
---|
2256 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2257 |
|
---|
2258 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
2259 | if (m->queryInfoRunning)
|
---|
2260 | {
|
---|
2261 | /* Must not hold the media tree lock, as Medium::i_queryInfo needs this
|
---|
2262 | * lock and thus we would run into a deadlock here. */
|
---|
2263 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
2264 | while (m->queryInfoRunning)
|
---|
2265 | {
|
---|
2266 | alock.release();
|
---|
2267 | /* must not hold the object lock now */
|
---|
2268 | Assert(!isWriteLockOnCurrentThread());
|
---|
2269 | {
|
---|
2270 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
2271 | }
|
---|
2272 | alock.acquire();
|
---|
2273 | }
|
---|
2274 | }
|
---|
2275 |
|
---|
2276 | HRESULT rc = S_OK;
|
---|
2277 |
|
---|
2278 | switch (m->state)
|
---|
2279 | {
|
---|
2280 | case MediumState_Created:
|
---|
2281 | case MediumState_Inaccessible:
|
---|
2282 | {
|
---|
2283 | m->preLockState = m->state;
|
---|
2284 |
|
---|
2285 | LogFlowThisFunc(("Okay - prev state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2286 | m->state = MediumState_LockedWrite;
|
---|
2287 |
|
---|
2288 | ComObjPtr<MediumLockToken> pToken;
|
---|
2289 | rc = pToken.createObject();
|
---|
2290 | if (SUCCEEDED(rc))
|
---|
2291 | rc = pToken->init(this, true /* fWrite */);
|
---|
2292 | if (FAILED(rc))
|
---|
2293 | {
|
---|
2294 | m->state = m->preLockState;
|
---|
2295 | return rc;
|
---|
2296 | }
|
---|
2297 |
|
---|
2298 | pToken.queryInterfaceTo(aToken.asOutParam());
|
---|
2299 | break;
|
---|
2300 | }
|
---|
2301 | default:
|
---|
2302 | {
|
---|
2303 | LogFlowThisFunc(("Failing - state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2304 | rc = i_setStateError();
|
---|
2305 | break;
|
---|
2306 | }
|
---|
2307 | }
|
---|
2308 |
|
---|
2309 | return rc;
|
---|
2310 | }
|
---|
2311 |
|
---|
2312 | /**
|
---|
2313 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
2314 | * in-process calls).
|
---|
2315 | */
|
---|
2316 | HRESULT Medium::i_unlockWrite(MediumState_T *aState)
|
---|
2317 | {
|
---|
2318 | AutoCaller autoCaller(this);
|
---|
2319 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2320 |
|
---|
2321 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2322 |
|
---|
2323 | HRESULT rc = S_OK;
|
---|
2324 |
|
---|
2325 | switch (m->state)
|
---|
2326 | {
|
---|
2327 | case MediumState_LockedWrite:
|
---|
2328 | {
|
---|
2329 | m->state = m->preLockState;
|
---|
2330 | /* There are cases where we inject the deleting state into
|
---|
2331 | * a medium locked for writing. Make sure #unmarkForDeletion()
|
---|
2332 | * gets the right state afterwards. */
|
---|
2333 | if (m->preLockState == MediumState_Deleting)
|
---|
2334 | m->preLockState = MediumState_Created;
|
---|
2335 | LogFlowThisFunc(("new state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2336 | break;
|
---|
2337 | }
|
---|
2338 | default:
|
---|
2339 | {
|
---|
2340 | LogFlowThisFunc(("Failing - state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2341 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2342 | tr("Medium '%s' is not locked for writing"),
|
---|
2343 | m->strLocationFull.c_str());
|
---|
2344 | break;
|
---|
2345 | }
|
---|
2346 | }
|
---|
2347 |
|
---|
2348 | /* return the current state after */
|
---|
2349 | if (aState)
|
---|
2350 | *aState = m->state;
|
---|
2351 |
|
---|
2352 | return rc;
|
---|
2353 | }
|
---|
2354 |
|
---|
2355 | HRESULT Medium::close(AutoCaller &aAutoCaller)
|
---|
2356 | {
|
---|
2357 | // make a copy of VirtualBox pointer which gets nulled by uninit()
|
---|
2358 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
2359 |
|
---|
2360 | MultiResult mrc = i_close(aAutoCaller);
|
---|
2361 |
|
---|
2362 | pVirtualBox->i_saveModifiedRegistries();
|
---|
2363 |
|
---|
2364 | return mrc;
|
---|
2365 | }
|
---|
2366 |
|
---|
2367 | HRESULT Medium::getProperty(const com::Utf8Str &aName,
|
---|
2368 | com::Utf8Str &aValue)
|
---|
2369 | {
|
---|
2370 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2371 |
|
---|
2372 | settings::StringsMap::const_iterator it = m->mapProperties.find(aName);
|
---|
2373 | if (it == m->mapProperties.end())
|
---|
2374 | {
|
---|
2375 | if (!aName.startsWith("Special/"))
|
---|
2376 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2377 | tr("Property '%s' does not exist"), aName.c_str());
|
---|
2378 | else
|
---|
2379 | /* be more silent here */
|
---|
2380 | return VBOX_E_OBJECT_NOT_FOUND;
|
---|
2381 | }
|
---|
2382 |
|
---|
2383 | aValue = it->second;
|
---|
2384 |
|
---|
2385 | return S_OK;
|
---|
2386 | }
|
---|
2387 |
|
---|
2388 | HRESULT Medium::setProperty(const com::Utf8Str &aName,
|
---|
2389 | const com::Utf8Str &aValue)
|
---|
2390 | {
|
---|
2391 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2392 |
|
---|
2393 | switch (m->state)
|
---|
2394 | {
|
---|
2395 | case MediumState_Created:
|
---|
2396 | case MediumState_Inaccessible:
|
---|
2397 | break;
|
---|
2398 | default:
|
---|
2399 | return i_setStateError();
|
---|
2400 | }
|
---|
2401 |
|
---|
2402 | settings::StringsMap::iterator it = m->mapProperties.find(aName);
|
---|
2403 | if ( !aName.startsWith("Special/")
|
---|
2404 | && !i_isPropertyForFilter(aName))
|
---|
2405 | {
|
---|
2406 | if (it == m->mapProperties.end())
|
---|
2407 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2408 | tr("Property '%s' does not exist"),
|
---|
2409 | aName.c_str());
|
---|
2410 | it->second = aValue;
|
---|
2411 | }
|
---|
2412 | else
|
---|
2413 | {
|
---|
2414 | if (it == m->mapProperties.end())
|
---|
2415 | {
|
---|
2416 | if (!aValue.isEmpty())
|
---|
2417 | m->mapProperties[aName] = aValue;
|
---|
2418 | }
|
---|
2419 | else
|
---|
2420 | {
|
---|
2421 | if (!aValue.isEmpty())
|
---|
2422 | it->second = aValue;
|
---|
2423 | else
|
---|
2424 | m->mapProperties.erase(it);
|
---|
2425 | }
|
---|
2426 | }
|
---|
2427 |
|
---|
2428 | // save the settings
|
---|
2429 | mlock.release();
|
---|
2430 | i_markRegistriesModified();
|
---|
2431 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2432 |
|
---|
2433 | return S_OK;
|
---|
2434 | }
|
---|
2435 |
|
---|
2436 | HRESULT Medium::getProperties(const com::Utf8Str &aNames,
|
---|
2437 | std::vector<com::Utf8Str> &aReturnNames,
|
---|
2438 | std::vector<com::Utf8Str> &aReturnValues)
|
---|
2439 | {
|
---|
2440 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2441 |
|
---|
2442 | /// @todo make use of aNames according to the documentation
|
---|
2443 | NOREF(aNames);
|
---|
2444 |
|
---|
2445 | aReturnNames.resize(m->mapProperties.size());
|
---|
2446 | aReturnValues.resize(m->mapProperties.size());
|
---|
2447 | size_t i = 0;
|
---|
2448 | for (settings::StringsMap::const_iterator it = m->mapProperties.begin();
|
---|
2449 | it != m->mapProperties.end();
|
---|
2450 | ++it, ++i)
|
---|
2451 | {
|
---|
2452 | aReturnNames[i] = it->first;
|
---|
2453 | aReturnValues[i] = it->second;
|
---|
2454 | }
|
---|
2455 | return S_OK;
|
---|
2456 | }
|
---|
2457 |
|
---|
2458 | HRESULT Medium::setProperties(const std::vector<com::Utf8Str> &aNames,
|
---|
2459 | const std::vector<com::Utf8Str> &aValues)
|
---|
2460 | {
|
---|
2461 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2462 |
|
---|
2463 | /* first pass: validate names */
|
---|
2464 | for (size_t i = 0;
|
---|
2465 | i < aNames.size();
|
---|
2466 | ++i)
|
---|
2467 | {
|
---|
2468 | Utf8Str strName(aNames[i]);
|
---|
2469 | if ( !strName.startsWith("Special/")
|
---|
2470 | && !i_isPropertyForFilter(strName)
|
---|
2471 | && m->mapProperties.find(strName) == m->mapProperties.end())
|
---|
2472 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2473 | tr("Property '%s' does not exist"), strName.c_str());
|
---|
2474 | }
|
---|
2475 |
|
---|
2476 | /* second pass: assign */
|
---|
2477 | for (size_t i = 0;
|
---|
2478 | i < aNames.size();
|
---|
2479 | ++i)
|
---|
2480 | {
|
---|
2481 | Utf8Str strName(aNames[i]);
|
---|
2482 | Utf8Str strValue(aValues[i]);
|
---|
2483 | settings::StringsMap::iterator it = m->mapProperties.find(strName);
|
---|
2484 | if ( !strName.startsWith("Special/")
|
---|
2485 | && !i_isPropertyForFilter(strName))
|
---|
2486 | {
|
---|
2487 | AssertReturn(it != m->mapProperties.end(), E_FAIL);
|
---|
2488 | it->second = strValue;
|
---|
2489 | }
|
---|
2490 | else
|
---|
2491 | {
|
---|
2492 | if (it == m->mapProperties.end())
|
---|
2493 | {
|
---|
2494 | if (!strValue.isEmpty())
|
---|
2495 | m->mapProperties[strName] = strValue;
|
---|
2496 | }
|
---|
2497 | else
|
---|
2498 | {
|
---|
2499 | if (!strValue.isEmpty())
|
---|
2500 | it->second = strValue;
|
---|
2501 | else
|
---|
2502 | m->mapProperties.erase(it);
|
---|
2503 | }
|
---|
2504 | }
|
---|
2505 | }
|
---|
2506 |
|
---|
2507 | // save the settings
|
---|
2508 | mlock.release();
|
---|
2509 | i_markRegistriesModified();
|
---|
2510 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2511 |
|
---|
2512 | return S_OK;
|
---|
2513 | }
|
---|
2514 | HRESULT Medium::createBaseStorage(LONG64 aLogicalSize,
|
---|
2515 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2516 | ComPtr<IProgress> &aProgress)
|
---|
2517 | {
|
---|
2518 | if (aLogicalSize < 0)
|
---|
2519 | return setError(E_INVALIDARG, tr("The medium size argument (%lld) is negative"), aLogicalSize);
|
---|
2520 |
|
---|
2521 | HRESULT rc = S_OK;
|
---|
2522 | ComObjPtr<Progress> pProgress;
|
---|
2523 | Medium::Task *pTask = NULL;
|
---|
2524 |
|
---|
2525 | try
|
---|
2526 | {
|
---|
2527 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2528 |
|
---|
2529 | ULONG mediumVariantFlags = 0;
|
---|
2530 |
|
---|
2531 | if (aVariant.size())
|
---|
2532 | {
|
---|
2533 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
2534 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
2535 | }
|
---|
2536 |
|
---|
2537 | mediumVariantFlags &= ((unsigned)~MediumVariant_Diff);
|
---|
2538 |
|
---|
2539 | if ( !(mediumVariantFlags & MediumVariant_Fixed)
|
---|
2540 | && !(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_CreateDynamic))
|
---|
2541 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2542 | tr("Medium format '%s' does not support dynamic storage creation"),
|
---|
2543 | m->strFormat.c_str());
|
---|
2544 |
|
---|
2545 | if ( (mediumVariantFlags & MediumVariant_Fixed)
|
---|
2546 | && !(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_CreateFixed))
|
---|
2547 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2548 | tr("Medium format '%s' does not support fixed storage creation"),
|
---|
2549 | m->strFormat.c_str());
|
---|
2550 |
|
---|
2551 | if ( (mediumVariantFlags & MediumVariant_Formatted)
|
---|
2552 | && i_getDeviceType() != DeviceType_Floppy)
|
---|
2553 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2554 | tr("Medium variant 'formatted' applies to floppy images only"));
|
---|
2555 |
|
---|
2556 | if (m->state != MediumState_NotCreated)
|
---|
2557 | throw i_setStateError();
|
---|
2558 |
|
---|
2559 | pProgress.createObject();
|
---|
2560 | rc = pProgress->init(m->pVirtualBox,
|
---|
2561 | static_cast<IMedium*>(this),
|
---|
2562 | (mediumVariantFlags & MediumVariant_Fixed)
|
---|
2563 | ? BstrFmt(tr("Creating fixed medium storage unit '%s'"), m->strLocationFull.c_str()).raw()
|
---|
2564 | : BstrFmt(tr("Creating dynamic medium storage unit '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
2565 | TRUE /* aCancelable */);
|
---|
2566 | if (FAILED(rc))
|
---|
2567 | throw rc;
|
---|
2568 |
|
---|
2569 | /* setup task object to carry out the operation asynchronously */
|
---|
2570 | pTask = new Medium::CreateBaseTask(this, pProgress, aLogicalSize,
|
---|
2571 | (MediumVariant_T)mediumVariantFlags);
|
---|
2572 | rc = pTask->rc();
|
---|
2573 | AssertComRC(rc);
|
---|
2574 | if (FAILED(rc))
|
---|
2575 | throw rc;
|
---|
2576 |
|
---|
2577 | m->state = MediumState_Creating;
|
---|
2578 | }
|
---|
2579 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2580 |
|
---|
2581 | if (SUCCEEDED(rc))
|
---|
2582 | {
|
---|
2583 | rc = pTask->createThread();
|
---|
2584 |
|
---|
2585 | if (SUCCEEDED(rc))
|
---|
2586 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2587 | }
|
---|
2588 | else if (pTask != NULL)
|
---|
2589 | delete pTask;
|
---|
2590 |
|
---|
2591 | return rc;
|
---|
2592 | }
|
---|
2593 |
|
---|
2594 | HRESULT Medium::deleteStorage(ComPtr<IProgress> &aProgress)
|
---|
2595 | {
|
---|
2596 | ComObjPtr<Progress> pProgress;
|
---|
2597 |
|
---|
2598 | MultiResult mrc = i_deleteStorage(&pProgress,
|
---|
2599 | false /* aWait */);
|
---|
2600 | /* Must save the registries in any case, since an entry was removed. */
|
---|
2601 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2602 |
|
---|
2603 | if (SUCCEEDED(mrc))
|
---|
2604 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2605 |
|
---|
2606 | return mrc;
|
---|
2607 | }
|
---|
2608 |
|
---|
2609 | HRESULT Medium::createDiffStorage(AutoCaller &autoCaller,
|
---|
2610 | const ComPtr<IMedium> &aTarget,
|
---|
2611 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2612 | ComPtr<IProgress> &aProgress)
|
---|
2613 | {
|
---|
2614 | IMedium *aT = aTarget;
|
---|
2615 | ComObjPtr<Medium> diff = static_cast<Medium*>(aT);
|
---|
2616 |
|
---|
2617 | autoCaller.release();
|
---|
2618 |
|
---|
2619 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
2620 | * the pVirtualBox reference, see #uninit(). */
|
---|
2621 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
2622 |
|
---|
2623 | // we access m->pParent
|
---|
2624 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
2625 |
|
---|
2626 | autoCaller.add();
|
---|
2627 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2628 |
|
---|
2629 | AutoMultiWriteLock2 alock(this, diff COMMA_LOCKVAL_SRC_POS);
|
---|
2630 |
|
---|
2631 | if (m->type == MediumType_Writethrough)
|
---|
2632 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2633 | tr("Medium type of '%s' is Writethrough"),
|
---|
2634 | m->strLocationFull.c_str());
|
---|
2635 | else if (m->type == MediumType_Shareable)
|
---|
2636 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2637 | tr("Medium type of '%s' is Shareable"),
|
---|
2638 | m->strLocationFull.c_str());
|
---|
2639 | else if (m->type == MediumType_Readonly)
|
---|
2640 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2641 | tr("Medium type of '%s' is Readonly"),
|
---|
2642 | m->strLocationFull.c_str());
|
---|
2643 |
|
---|
2644 | /* Apply the normal locking logic to the entire chain. */
|
---|
2645 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
2646 | alock.release();
|
---|
2647 | autoCaller.release();
|
---|
2648 | treeLock.release();
|
---|
2649 | HRESULT rc = diff->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2650 | diff /* pToLockWrite */,
|
---|
2651 | false /* fMediumLockWriteAll */,
|
---|
2652 | this,
|
---|
2653 | *pMediumLockList);
|
---|
2654 | treeLock.acquire();
|
---|
2655 | autoCaller.add();
|
---|
2656 | if (FAILED(autoCaller.rc()))
|
---|
2657 | rc = autoCaller.rc();
|
---|
2658 | alock.acquire();
|
---|
2659 | if (FAILED(rc))
|
---|
2660 | {
|
---|
2661 | delete pMediumLockList;
|
---|
2662 | return rc;
|
---|
2663 | }
|
---|
2664 |
|
---|
2665 | alock.release();
|
---|
2666 | autoCaller.release();
|
---|
2667 | treeLock.release();
|
---|
2668 | rc = pMediumLockList->Lock();
|
---|
2669 | treeLock.acquire();
|
---|
2670 | autoCaller.add();
|
---|
2671 | if (FAILED(autoCaller.rc()))
|
---|
2672 | rc = autoCaller.rc();
|
---|
2673 | alock.acquire();
|
---|
2674 | if (FAILED(rc))
|
---|
2675 | {
|
---|
2676 | delete pMediumLockList;
|
---|
2677 |
|
---|
2678 | return setError(rc, tr("Could not lock medium when creating diff '%s'"),
|
---|
2679 | diff->i_getLocationFull().c_str());
|
---|
2680 | }
|
---|
2681 |
|
---|
2682 | Guid parentMachineRegistry;
|
---|
2683 | if (i_getFirstRegistryMachineId(parentMachineRegistry))
|
---|
2684 | {
|
---|
2685 | /* since this medium has been just created it isn't associated yet */
|
---|
2686 | diff->m->llRegistryIDs.push_back(parentMachineRegistry);
|
---|
2687 | alock.release();
|
---|
2688 | autoCaller.release();
|
---|
2689 | treeLock.release();
|
---|
2690 | diff->i_markRegistriesModified();
|
---|
2691 | treeLock.acquire();
|
---|
2692 | autoCaller.add();
|
---|
2693 | alock.acquire();
|
---|
2694 | }
|
---|
2695 |
|
---|
2696 | alock.release();
|
---|
2697 | autoCaller.release();
|
---|
2698 | treeLock.release();
|
---|
2699 |
|
---|
2700 | ComObjPtr<Progress> pProgress;
|
---|
2701 |
|
---|
2702 | ULONG mediumVariantFlags = 0;
|
---|
2703 |
|
---|
2704 | if (aVariant.size())
|
---|
2705 | {
|
---|
2706 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
2707 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
2708 | }
|
---|
2709 |
|
---|
2710 | if (mediumVariantFlags & MediumVariant_Formatted)
|
---|
2711 | {
|
---|
2712 | delete pMediumLockList;
|
---|
2713 | return setError(VBOX_E_NOT_SUPPORTED,
|
---|
2714 | tr("Medium variant 'formatted' applies to floppy images only"));
|
---|
2715 | }
|
---|
2716 |
|
---|
2717 | rc = i_createDiffStorage(diff, (MediumVariant_T)mediumVariantFlags, pMediumLockList,
|
---|
2718 | &pProgress, false /* aWait */);
|
---|
2719 | if (FAILED(rc))
|
---|
2720 | delete pMediumLockList;
|
---|
2721 | else
|
---|
2722 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2723 |
|
---|
2724 | return rc;
|
---|
2725 | }
|
---|
2726 |
|
---|
2727 | HRESULT Medium::mergeTo(const ComPtr<IMedium> &aTarget,
|
---|
2728 | ComPtr<IProgress> &aProgress)
|
---|
2729 | {
|
---|
2730 | IMedium *aT = aTarget;
|
---|
2731 |
|
---|
2732 | ComAssertRet(aT != this, E_INVALIDARG);
|
---|
2733 |
|
---|
2734 | ComObjPtr<Medium> pTarget = static_cast<Medium*>(aT);
|
---|
2735 |
|
---|
2736 | bool fMergeForward = false;
|
---|
2737 | ComObjPtr<Medium> pParentForTarget;
|
---|
2738 | MediumLockList *pChildrenToReparent = NULL;
|
---|
2739 | MediumLockList *pMediumLockList = NULL;
|
---|
2740 |
|
---|
2741 | HRESULT rc = S_OK;
|
---|
2742 |
|
---|
2743 | rc = i_prepareMergeTo(pTarget, NULL, NULL, true, fMergeForward,
|
---|
2744 | pParentForTarget, pChildrenToReparent, pMediumLockList);
|
---|
2745 | if (FAILED(rc)) return rc;
|
---|
2746 |
|
---|
2747 | ComObjPtr<Progress> pProgress;
|
---|
2748 |
|
---|
2749 | rc = i_mergeTo(pTarget, fMergeForward, pParentForTarget, pChildrenToReparent,
|
---|
2750 | pMediumLockList, &pProgress, false /* aWait */);
|
---|
2751 | if (FAILED(rc))
|
---|
2752 | i_cancelMergeTo(pChildrenToReparent, pMediumLockList);
|
---|
2753 | else
|
---|
2754 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2755 |
|
---|
2756 | return rc;
|
---|
2757 | }
|
---|
2758 |
|
---|
2759 | HRESULT Medium::cloneToBase(const ComPtr<IMedium> &aTarget,
|
---|
2760 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2761 | ComPtr<IProgress> &aProgress)
|
---|
2762 | {
|
---|
2763 | int rc = S_OK;
|
---|
2764 |
|
---|
2765 | rc = cloneTo(aTarget, aVariant, NULL, aProgress);
|
---|
2766 | return rc;
|
---|
2767 | }
|
---|
2768 |
|
---|
2769 | HRESULT Medium::cloneTo(const ComPtr<IMedium> &aTarget,
|
---|
2770 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2771 | const ComPtr<IMedium> &aParent,
|
---|
2772 | ComPtr<IProgress> &aProgress)
|
---|
2773 | {
|
---|
2774 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
2775 | * to lock order violations, it probably causes lock order issues related
|
---|
2776 | * to the AutoCaller usage. */
|
---|
2777 | ComAssertRet(aTarget != this, E_INVALIDARG);
|
---|
2778 |
|
---|
2779 | IMedium *aT = aTarget;
|
---|
2780 | ComObjPtr<Medium> pTarget = static_cast<Medium*>(aT);
|
---|
2781 | ComObjPtr<Medium> pParent;
|
---|
2782 | if (aParent)
|
---|
2783 | {
|
---|
2784 | IMedium *aP = aParent;
|
---|
2785 | pParent = static_cast<Medium*>(aP);
|
---|
2786 | }
|
---|
2787 |
|
---|
2788 | HRESULT rc = S_OK;
|
---|
2789 | ComObjPtr<Progress> pProgress;
|
---|
2790 | Medium::Task *pTask = NULL;
|
---|
2791 |
|
---|
2792 | try
|
---|
2793 | {
|
---|
2794 | // locking: we need the tree lock first because we access parent pointers
|
---|
2795 | // and we need to write-lock the media involved
|
---|
2796 | uint32_t cHandles = 3;
|
---|
2797 | LockHandle* pHandles[4] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
2798 | this->lockHandle(),
|
---|
2799 | pTarget->lockHandle() };
|
---|
2800 | /* Only add parent to the lock if it is not null */
|
---|
2801 | if (!pParent.isNull())
|
---|
2802 | pHandles[cHandles++] = pParent->lockHandle();
|
---|
2803 | AutoWriteLock alock(cHandles,
|
---|
2804 | pHandles
|
---|
2805 | COMMA_LOCKVAL_SRC_POS);
|
---|
2806 |
|
---|
2807 | if ( pTarget->m->state != MediumState_NotCreated
|
---|
2808 | && pTarget->m->state != MediumState_Created)
|
---|
2809 | throw pTarget->i_setStateError();
|
---|
2810 |
|
---|
2811 | /* Build the source lock list. */
|
---|
2812 | MediumLockList *pSourceMediumLockList(new MediumLockList());
|
---|
2813 | alock.release();
|
---|
2814 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2815 | NULL /* pToLockWrite */,
|
---|
2816 | false /* fMediumLockWriteAll */,
|
---|
2817 | NULL,
|
---|
2818 | *pSourceMediumLockList);
|
---|
2819 | alock.acquire();
|
---|
2820 | if (FAILED(rc))
|
---|
2821 | {
|
---|
2822 | delete pSourceMediumLockList;
|
---|
2823 | throw rc;
|
---|
2824 | }
|
---|
2825 |
|
---|
2826 | /* Build the target lock list (including the to-be parent chain). */
|
---|
2827 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
2828 | alock.release();
|
---|
2829 | rc = pTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2830 | pTarget /* pToLockWrite */,
|
---|
2831 | false /* fMediumLockWriteAll */,
|
---|
2832 | pParent,
|
---|
2833 | *pTargetMediumLockList);
|
---|
2834 | alock.acquire();
|
---|
2835 | if (FAILED(rc))
|
---|
2836 | {
|
---|
2837 | delete pSourceMediumLockList;
|
---|
2838 | delete pTargetMediumLockList;
|
---|
2839 | throw rc;
|
---|
2840 | }
|
---|
2841 |
|
---|
2842 | alock.release();
|
---|
2843 | rc = pSourceMediumLockList->Lock();
|
---|
2844 | alock.acquire();
|
---|
2845 | if (FAILED(rc))
|
---|
2846 | {
|
---|
2847 | delete pSourceMediumLockList;
|
---|
2848 | delete pTargetMediumLockList;
|
---|
2849 | throw setError(rc,
|
---|
2850 | tr("Failed to lock source media '%s'"),
|
---|
2851 | i_getLocationFull().c_str());
|
---|
2852 | }
|
---|
2853 | alock.release();
|
---|
2854 | rc = pTargetMediumLockList->Lock();
|
---|
2855 | alock.acquire();
|
---|
2856 | if (FAILED(rc))
|
---|
2857 | {
|
---|
2858 | delete pSourceMediumLockList;
|
---|
2859 | delete pTargetMediumLockList;
|
---|
2860 | throw setError(rc,
|
---|
2861 | tr("Failed to lock target media '%s'"),
|
---|
2862 | pTarget->i_getLocationFull().c_str());
|
---|
2863 | }
|
---|
2864 |
|
---|
2865 | pProgress.createObject();
|
---|
2866 | rc = pProgress->init(m->pVirtualBox,
|
---|
2867 | static_cast <IMedium *>(this),
|
---|
2868 | BstrFmt(tr("Creating clone medium '%s'"), pTarget->m->strLocationFull.c_str()).raw(),
|
---|
2869 | TRUE /* aCancelable */);
|
---|
2870 | if (FAILED(rc))
|
---|
2871 | {
|
---|
2872 | delete pSourceMediumLockList;
|
---|
2873 | delete pTargetMediumLockList;
|
---|
2874 | throw rc;
|
---|
2875 | }
|
---|
2876 |
|
---|
2877 | ULONG mediumVariantFlags = 0;
|
---|
2878 |
|
---|
2879 | if (aVariant.size())
|
---|
2880 | {
|
---|
2881 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
2882 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
2883 | }
|
---|
2884 |
|
---|
2885 | if (mediumVariantFlags & MediumVariant_Formatted)
|
---|
2886 | {
|
---|
2887 | delete pSourceMediumLockList;
|
---|
2888 | delete pTargetMediumLockList;
|
---|
2889 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2890 | tr("Medium variant 'formatted' applies to floppy images only"));
|
---|
2891 | }
|
---|
2892 |
|
---|
2893 | /* setup task object to carry out the operation asynchronously */
|
---|
2894 | pTask = new Medium::CloneTask(this, pProgress, pTarget,
|
---|
2895 | (MediumVariant_T)mediumVariantFlags,
|
---|
2896 | pParent, UINT32_MAX, UINT32_MAX,
|
---|
2897 | pSourceMediumLockList, pTargetMediumLockList);
|
---|
2898 | rc = pTask->rc();
|
---|
2899 | AssertComRC(rc);
|
---|
2900 | if (FAILED(rc))
|
---|
2901 | throw rc;
|
---|
2902 |
|
---|
2903 | if (pTarget->m->state == MediumState_NotCreated)
|
---|
2904 | pTarget->m->state = MediumState_Creating;
|
---|
2905 | }
|
---|
2906 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2907 |
|
---|
2908 | if (SUCCEEDED(rc))
|
---|
2909 | {
|
---|
2910 | rc = pTask->createThread();
|
---|
2911 |
|
---|
2912 | if (SUCCEEDED(rc))
|
---|
2913 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2914 | }
|
---|
2915 | else if (pTask != NULL)
|
---|
2916 | delete pTask;
|
---|
2917 |
|
---|
2918 | return rc;
|
---|
2919 | }
|
---|
2920 |
|
---|
2921 | HRESULT Medium::moveTo(AutoCaller &autoCaller, const com::Utf8Str &aLocation, ComPtr<IProgress> &aProgress)
|
---|
2922 | {
|
---|
2923 | ComObjPtr<Medium> pParent;
|
---|
2924 | ComObjPtr<Progress> pProgress;
|
---|
2925 | HRESULT rc = S_OK;
|
---|
2926 | Medium::Task *pTask = NULL;
|
---|
2927 |
|
---|
2928 | try
|
---|
2929 | {
|
---|
2930 | /// @todo NEWMEDIA for file names, add the default extension if no extension
|
---|
2931 | /// is present (using the information from the VD backend which also implies
|
---|
2932 | /// that one more parameter should be passed to moveTo() requesting
|
---|
2933 | /// that functionality since it is only allowed when called from this method
|
---|
2934 |
|
---|
2935 | /// @todo NEWMEDIA rename the file and set m->location on success, then save
|
---|
2936 | /// the global registry (and local registries of portable VMs referring to
|
---|
2937 | /// this medium), this will also require to add the mRegistered flag to data
|
---|
2938 |
|
---|
2939 | autoCaller.release();
|
---|
2940 |
|
---|
2941 | // locking: we need the tree lock first because we access parent pointers
|
---|
2942 | // and we need to write-lock the media involved
|
---|
2943 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
2944 |
|
---|
2945 | autoCaller.add();
|
---|
2946 | AssertComRCThrowRC(autoCaller.rc());
|
---|
2947 |
|
---|
2948 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2949 |
|
---|
2950 | /* play with locations */
|
---|
2951 | {
|
---|
2952 | /* get source path and filename */
|
---|
2953 | Utf8Str sourcePath = i_getLocationFull();
|
---|
2954 | Utf8Str sourceFName = i_getName();
|
---|
2955 |
|
---|
2956 | if (aLocation.isEmpty())
|
---|
2957 | {
|
---|
2958 | rc = setError(VERR_PATH_ZERO_LENGTH,
|
---|
2959 | tr("Medium '%s' can't be moved. Destination path is empty."),
|
---|
2960 | i_getLocationFull().c_str());
|
---|
2961 | throw rc;
|
---|
2962 | }
|
---|
2963 |
|
---|
2964 | /* extract destination path and filename */
|
---|
2965 | Utf8Str destPath(aLocation);
|
---|
2966 | Utf8Str destFName(destPath);
|
---|
2967 | destFName.stripPath();
|
---|
2968 |
|
---|
2969 | Utf8Str suffix(destFName);
|
---|
2970 | suffix.stripSuffix();
|
---|
2971 |
|
---|
2972 | if (suffix.equals(destFName) && !destFName.isEmpty())
|
---|
2973 | {
|
---|
2974 | /*
|
---|
2975 | * The target path has no filename: Either "/path/to/new/location" or
|
---|
2976 | * just "newname" (no trailing backslash or there is no filename with
|
---|
2977 | * extension(suffix)).
|
---|
2978 | */
|
---|
2979 | if (destPath.equals(destFName))
|
---|
2980 | {
|
---|
2981 | /* new path contains only "newname", no path, no extension */
|
---|
2982 | destFName.append(RTPathSuffix(sourceFName.c_str()));
|
---|
2983 | destPath = destFName;
|
---|
2984 | }
|
---|
2985 | else
|
---|
2986 | {
|
---|
2987 | /* new path looks like "/path/to/new/location" */
|
---|
2988 | destFName.setNull();
|
---|
2989 | destPath.append(RTPATH_SLASH);
|
---|
2990 | }
|
---|
2991 | }
|
---|
2992 |
|
---|
2993 | if (destFName.isEmpty())
|
---|
2994 | {
|
---|
2995 | /* No target name */
|
---|
2996 | destPath.append(sourceFName);
|
---|
2997 | }
|
---|
2998 | else
|
---|
2999 | {
|
---|
3000 | if (destPath.equals(destFName))
|
---|
3001 | {
|
---|
3002 | /*
|
---|
3003 | * The target path contains of only a filename without a directory.
|
---|
3004 | * Move the medium within the source directory to the new name
|
---|
3005 | * (actually rename operation).
|
---|
3006 | * Scratches sourcePath!
|
---|
3007 | */
|
---|
3008 | destPath = sourcePath.stripFilename().append(RTPATH_SLASH).append(destFName);
|
---|
3009 | }
|
---|
3010 | suffix = i_getFormat();
|
---|
3011 | if (suffix.compare("RAW", Utf8Str::CaseInsensitive) == 0)
|
---|
3012 | {
|
---|
3013 | DeviceType_T devType = i_getDeviceType();
|
---|
3014 | switch (devType)
|
---|
3015 | {
|
---|
3016 | case DeviceType_DVD:
|
---|
3017 | suffix = "iso";
|
---|
3018 | break;
|
---|
3019 | case DeviceType_Floppy:
|
---|
3020 | suffix = "img";
|
---|
3021 | break;
|
---|
3022 | default:
|
---|
3023 | rc = setError(VERR_NOT_A_FILE,
|
---|
3024 | tr("Medium '%s' has RAW type. \"Move\" operation isn't supported for this type."),
|
---|
3025 | i_getLocationFull().c_str());
|
---|
3026 | throw rc;
|
---|
3027 | }
|
---|
3028 | }
|
---|
3029 | else if (suffix.compare("Parallels", Utf8Str::CaseInsensitive) == 0)
|
---|
3030 | {
|
---|
3031 | suffix = "hdd";
|
---|
3032 | }
|
---|
3033 |
|
---|
3034 | /* Set the target extension like on the source. Any conversions are prohibited */
|
---|
3035 | suffix.toLower();
|
---|
3036 | destPath.stripSuffix().append('.').append(suffix);
|
---|
3037 | }
|
---|
3038 |
|
---|
3039 | /* Simple check for existence */
|
---|
3040 | if (RTFileExists(destPath.c_str()))
|
---|
3041 | {
|
---|
3042 | rc = setError(VBOX_E_FILE_ERROR,
|
---|
3043 | tr("The given path '%s' is an existing file. Delete or rename this file."),
|
---|
3044 | destPath.c_str());
|
---|
3045 | throw rc;
|
---|
3046 | }
|
---|
3047 |
|
---|
3048 | if (!i_isMediumFormatFile())
|
---|
3049 | {
|
---|
3050 | rc = setError(VERR_NOT_A_FILE,
|
---|
3051 | tr("Medium '%s' isn't a file object. \"Move\" operation isn't supported."),
|
---|
3052 | i_getLocationFull().c_str());
|
---|
3053 | throw rc;
|
---|
3054 | }
|
---|
3055 | /* Path must be absolute */
|
---|
3056 | if (!RTPathStartsWithRoot(destPath.c_str()))
|
---|
3057 | {
|
---|
3058 | rc = setError(VBOX_E_FILE_ERROR,
|
---|
3059 | tr("The given path '%s' is not fully qualified"),
|
---|
3060 | destPath.c_str());
|
---|
3061 | throw rc;
|
---|
3062 | }
|
---|
3063 | /* Check path for a new file object */
|
---|
3064 | rc = VirtualBox::i_ensureFilePathExists(destPath, true);
|
---|
3065 | if (FAILED(rc))
|
---|
3066 | throw rc;
|
---|
3067 |
|
---|
3068 | /* Set needed variables for "moving" procedure. It'll be used later in separate thread task */
|
---|
3069 | rc = i_preparationForMoving(destPath);
|
---|
3070 | if (FAILED(rc))
|
---|
3071 | {
|
---|
3072 | rc = setError(VERR_NO_CHANGE,
|
---|
3073 | tr("Medium '%s' is already in the correct location"),
|
---|
3074 | i_getLocationFull().c_str());
|
---|
3075 | throw rc;
|
---|
3076 | }
|
---|
3077 | }
|
---|
3078 |
|
---|
3079 | /* Check VMs which have this medium attached to*/
|
---|
3080 | std::vector<com::Guid> aMachineIds;
|
---|
3081 | rc = getMachineIds(aMachineIds);
|
---|
3082 | std::vector<com::Guid>::const_iterator currMachineID = aMachineIds.begin();
|
---|
3083 | std::vector<com::Guid>::const_iterator lastMachineID = aMachineIds.end();
|
---|
3084 |
|
---|
3085 | while (currMachineID != lastMachineID)
|
---|
3086 | {
|
---|
3087 | Guid id(*currMachineID);
|
---|
3088 | ComObjPtr<Machine> aMachine;
|
---|
3089 |
|
---|
3090 | alock.release();
|
---|
3091 | autoCaller.release();
|
---|
3092 | treeLock.release();
|
---|
3093 | rc = m->pVirtualBox->i_findMachine(id, false, true, &aMachine);
|
---|
3094 | treeLock.acquire();
|
---|
3095 | autoCaller.add();
|
---|
3096 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3097 | alock.acquire();
|
---|
3098 |
|
---|
3099 | if (SUCCEEDED(rc))
|
---|
3100 | {
|
---|
3101 | ComObjPtr<SessionMachine> sm;
|
---|
3102 | ComPtr<IInternalSessionControl> ctl;
|
---|
3103 |
|
---|
3104 | alock.release();
|
---|
3105 | autoCaller.release();
|
---|
3106 | treeLock.release();
|
---|
3107 | bool ses = aMachine->i_isSessionOpenVM(sm, &ctl);
|
---|
3108 | treeLock.acquire();
|
---|
3109 | autoCaller.add();
|
---|
3110 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3111 | alock.acquire();
|
---|
3112 |
|
---|
3113 | if (ses)
|
---|
3114 | {
|
---|
3115 | rc = setError(VERR_VM_UNEXPECTED_VM_STATE,
|
---|
3116 | tr("At least the VM '%s' to whom this medium '%s' attached has currently an opened session. Stop all VMs before relocating this medium"),
|
---|
3117 | id.toString().c_str(),
|
---|
3118 | i_getLocationFull().c_str());
|
---|
3119 | throw rc;
|
---|
3120 | }
|
---|
3121 | }
|
---|
3122 | ++currMachineID;
|
---|
3123 | }
|
---|
3124 |
|
---|
3125 | /* Build the source lock list. */
|
---|
3126 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3127 | alock.release();
|
---|
3128 | autoCaller.release();
|
---|
3129 | treeLock.release();
|
---|
3130 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
3131 | this /* pToLockWrite */,
|
---|
3132 | true /* fMediumLockWriteAll */,
|
---|
3133 | NULL,
|
---|
3134 | *pMediumLockList);
|
---|
3135 | treeLock.acquire();
|
---|
3136 | autoCaller.add();
|
---|
3137 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3138 | alock.acquire();
|
---|
3139 | if (FAILED(rc))
|
---|
3140 | {
|
---|
3141 | delete pMediumLockList;
|
---|
3142 | throw setError(rc,
|
---|
3143 | tr("Failed to create medium lock list for '%s'"),
|
---|
3144 | i_getLocationFull().c_str());
|
---|
3145 | }
|
---|
3146 | alock.release();
|
---|
3147 | autoCaller.release();
|
---|
3148 | treeLock.release();
|
---|
3149 | rc = pMediumLockList->Lock();
|
---|
3150 | treeLock.acquire();
|
---|
3151 | autoCaller.add();
|
---|
3152 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3153 | alock.acquire();
|
---|
3154 | if (FAILED(rc))
|
---|
3155 | {
|
---|
3156 | delete pMediumLockList;
|
---|
3157 | throw setError(rc,
|
---|
3158 | tr("Failed to lock media '%s'"),
|
---|
3159 | i_getLocationFull().c_str());
|
---|
3160 | }
|
---|
3161 |
|
---|
3162 | pProgress.createObject();
|
---|
3163 | rc = pProgress->init(m->pVirtualBox,
|
---|
3164 | static_cast <IMedium *>(this),
|
---|
3165 | BstrFmt(tr("Moving medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3166 | TRUE /* aCancelable */);
|
---|
3167 |
|
---|
3168 | /* Do the disk moving. */
|
---|
3169 | if (SUCCEEDED(rc))
|
---|
3170 | {
|
---|
3171 | ULONG mediumVariantFlags = i_getVariant();
|
---|
3172 |
|
---|
3173 | /* setup task object to carry out the operation asynchronously */
|
---|
3174 | pTask = new Medium::MoveTask(this, pProgress,
|
---|
3175 | (MediumVariant_T)mediumVariantFlags,
|
---|
3176 | pMediumLockList);
|
---|
3177 | rc = pTask->rc();
|
---|
3178 | AssertComRC(rc);
|
---|
3179 | if (FAILED(rc))
|
---|
3180 | throw rc;
|
---|
3181 | }
|
---|
3182 |
|
---|
3183 | }
|
---|
3184 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3185 |
|
---|
3186 | if (SUCCEEDED(rc))
|
---|
3187 | {
|
---|
3188 | rc = pTask->createThread();
|
---|
3189 |
|
---|
3190 | if (SUCCEEDED(rc))
|
---|
3191 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3192 | }
|
---|
3193 | else
|
---|
3194 | {
|
---|
3195 | if (pTask)
|
---|
3196 | delete pTask;
|
---|
3197 | }
|
---|
3198 |
|
---|
3199 | return rc;
|
---|
3200 | }
|
---|
3201 |
|
---|
3202 | HRESULT Medium::setLocation(const com::Utf8Str &aLocation)
|
---|
3203 | {
|
---|
3204 | HRESULT rc = S_OK;
|
---|
3205 |
|
---|
3206 | try
|
---|
3207 | {
|
---|
3208 | // locking: we need the tree lock first because we access parent pointers
|
---|
3209 | // and we need to write-lock the media involved
|
---|
3210 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3211 |
|
---|
3212 | AutoCaller autoCaller(this);
|
---|
3213 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3214 |
|
---|
3215 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3216 |
|
---|
3217 | Utf8Str destPath(aLocation);
|
---|
3218 |
|
---|
3219 | // some check for file based medium
|
---|
3220 | if (i_isMediumFormatFile())
|
---|
3221 | {
|
---|
3222 | /* Path must be absolute */
|
---|
3223 | if (!RTPathStartsWithRoot(destPath.c_str()))
|
---|
3224 | {
|
---|
3225 | rc = setError(VBOX_E_FILE_ERROR,
|
---|
3226 | tr("The given path '%s' is not fully qualified"),
|
---|
3227 | destPath.c_str());
|
---|
3228 | throw rc;
|
---|
3229 | }
|
---|
3230 |
|
---|
3231 | /* Simple check for existence */
|
---|
3232 | if (!RTFileExists(destPath.c_str()))
|
---|
3233 | {
|
---|
3234 | rc = setError(VBOX_E_FILE_ERROR,
|
---|
3235 | tr("The given path '%s' is not an existing file. New location is invalid."),
|
---|
3236 | destPath.c_str());
|
---|
3237 | throw rc;
|
---|
3238 | }
|
---|
3239 | }
|
---|
3240 |
|
---|
3241 | /* Check VMs which have this medium attached to*/
|
---|
3242 | std::vector<com::Guid> aMachineIds;
|
---|
3243 | rc = getMachineIds(aMachineIds);
|
---|
3244 |
|
---|
3245 | // switch locks only if there are machines with this medium attached
|
---|
3246 | if (!aMachineIds.empty())
|
---|
3247 | {
|
---|
3248 | std::vector<com::Guid>::const_iterator currMachineID = aMachineIds.begin();
|
---|
3249 | std::vector<com::Guid>::const_iterator lastMachineID = aMachineIds.end();
|
---|
3250 |
|
---|
3251 | alock.release();
|
---|
3252 | autoCaller.release();
|
---|
3253 | treeLock.release();
|
---|
3254 |
|
---|
3255 | while (currMachineID != lastMachineID)
|
---|
3256 | {
|
---|
3257 | Guid id(*currMachineID);
|
---|
3258 | ComObjPtr<Machine> aMachine;
|
---|
3259 | rc = m->pVirtualBox->i_findMachine(id, false, true, &aMachine);
|
---|
3260 | if (SUCCEEDED(rc))
|
---|
3261 | {
|
---|
3262 | ComObjPtr<SessionMachine> sm;
|
---|
3263 | ComPtr<IInternalSessionControl> ctl;
|
---|
3264 |
|
---|
3265 | bool ses = aMachine->i_isSessionOpenVM(sm, &ctl);
|
---|
3266 | if (ses)
|
---|
3267 | {
|
---|
3268 | treeLock.acquire();
|
---|
3269 | autoCaller.add();
|
---|
3270 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3271 | alock.acquire();
|
---|
3272 |
|
---|
3273 | rc = setError(VERR_VM_UNEXPECTED_VM_STATE,
|
---|
3274 | tr("At least the VM '%s' to whom this medium '%s' attached has currently an opened session. Stop all VMs before set location for this medium"),
|
---|
3275 | id.toString().c_str(),
|
---|
3276 | i_getLocationFull().c_str());
|
---|
3277 | throw rc;
|
---|
3278 | }
|
---|
3279 | }
|
---|
3280 | ++currMachineID;
|
---|
3281 | }
|
---|
3282 |
|
---|
3283 | treeLock.acquire();
|
---|
3284 | autoCaller.add();
|
---|
3285 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3286 | alock.acquire();
|
---|
3287 | }
|
---|
3288 |
|
---|
3289 | m->strLocationFull = destPath;
|
---|
3290 |
|
---|
3291 | // save the settings
|
---|
3292 | alock.release();
|
---|
3293 | autoCaller.release();
|
---|
3294 | treeLock.release();
|
---|
3295 |
|
---|
3296 | i_markRegistriesModified();
|
---|
3297 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
3298 |
|
---|
3299 | MediumState_T mediumState;
|
---|
3300 | refreshState(autoCaller, &mediumState);
|
---|
3301 | }
|
---|
3302 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3303 |
|
---|
3304 | return rc;
|
---|
3305 | }
|
---|
3306 |
|
---|
3307 | HRESULT Medium::compact(ComPtr<IProgress> &aProgress)
|
---|
3308 | {
|
---|
3309 | HRESULT rc = S_OK;
|
---|
3310 | ComObjPtr<Progress> pProgress;
|
---|
3311 | Medium::Task *pTask = NULL;
|
---|
3312 |
|
---|
3313 | try
|
---|
3314 | {
|
---|
3315 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3316 |
|
---|
3317 | /* Build the medium lock list. */
|
---|
3318 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3319 | alock.release();
|
---|
3320 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
3321 | this /* pToLockWrite */,
|
---|
3322 | false /* fMediumLockWriteAll */,
|
---|
3323 | NULL,
|
---|
3324 | *pMediumLockList);
|
---|
3325 | alock.acquire();
|
---|
3326 | if (FAILED(rc))
|
---|
3327 | {
|
---|
3328 | delete pMediumLockList;
|
---|
3329 | throw rc;
|
---|
3330 | }
|
---|
3331 |
|
---|
3332 | alock.release();
|
---|
3333 | rc = pMediumLockList->Lock();
|
---|
3334 | alock.acquire();
|
---|
3335 | if (FAILED(rc))
|
---|
3336 | {
|
---|
3337 | delete pMediumLockList;
|
---|
3338 | throw setError(rc,
|
---|
3339 | tr("Failed to lock media when compacting '%s'"),
|
---|
3340 | i_getLocationFull().c_str());
|
---|
3341 | }
|
---|
3342 |
|
---|
3343 | pProgress.createObject();
|
---|
3344 | rc = pProgress->init(m->pVirtualBox,
|
---|
3345 | static_cast <IMedium *>(this),
|
---|
3346 | BstrFmt(tr("Compacting medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3347 | TRUE /* aCancelable */);
|
---|
3348 | if (FAILED(rc))
|
---|
3349 | {
|
---|
3350 | delete pMediumLockList;
|
---|
3351 | throw rc;
|
---|
3352 | }
|
---|
3353 |
|
---|
3354 | /* setup task object to carry out the operation asynchronously */
|
---|
3355 | pTask = new Medium::CompactTask(this, pProgress, pMediumLockList);
|
---|
3356 | rc = pTask->rc();
|
---|
3357 | AssertComRC(rc);
|
---|
3358 | if (FAILED(rc))
|
---|
3359 | throw rc;
|
---|
3360 | }
|
---|
3361 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3362 |
|
---|
3363 | if (SUCCEEDED(rc))
|
---|
3364 | {
|
---|
3365 | rc = pTask->createThread();
|
---|
3366 |
|
---|
3367 | if (SUCCEEDED(rc))
|
---|
3368 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3369 | }
|
---|
3370 | else if (pTask != NULL)
|
---|
3371 | delete pTask;
|
---|
3372 |
|
---|
3373 | return rc;
|
---|
3374 | }
|
---|
3375 |
|
---|
3376 | HRESULT Medium::resize(LONG64 aLogicalSize,
|
---|
3377 | ComPtr<IProgress> &aProgress)
|
---|
3378 | {
|
---|
3379 | HRESULT rc = S_OK;
|
---|
3380 | ComObjPtr<Progress> pProgress;
|
---|
3381 | Medium::Task *pTask = NULL;
|
---|
3382 |
|
---|
3383 | try
|
---|
3384 | {
|
---|
3385 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3386 |
|
---|
3387 | /* Build the medium lock list. */
|
---|
3388 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3389 | alock.release();
|
---|
3390 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
3391 | this /* pToLockWrite */,
|
---|
3392 | false /* fMediumLockWriteAll */,
|
---|
3393 | NULL,
|
---|
3394 | *pMediumLockList);
|
---|
3395 | alock.acquire();
|
---|
3396 | if (FAILED(rc))
|
---|
3397 | {
|
---|
3398 | delete pMediumLockList;
|
---|
3399 | throw rc;
|
---|
3400 | }
|
---|
3401 |
|
---|
3402 | alock.release();
|
---|
3403 | rc = pMediumLockList->Lock();
|
---|
3404 | alock.acquire();
|
---|
3405 | if (FAILED(rc))
|
---|
3406 | {
|
---|
3407 | delete pMediumLockList;
|
---|
3408 | throw setError(rc,
|
---|
3409 | tr("Failed to lock media when compacting '%s'"),
|
---|
3410 | i_getLocationFull().c_str());
|
---|
3411 | }
|
---|
3412 |
|
---|
3413 | pProgress.createObject();
|
---|
3414 | rc = pProgress->init(m->pVirtualBox,
|
---|
3415 | static_cast <IMedium *>(this),
|
---|
3416 | BstrFmt(tr("Compacting medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3417 | TRUE /* aCancelable */);
|
---|
3418 | if (FAILED(rc))
|
---|
3419 | {
|
---|
3420 | delete pMediumLockList;
|
---|
3421 | throw rc;
|
---|
3422 | }
|
---|
3423 |
|
---|
3424 | /* setup task object to carry out the operation asynchronously */
|
---|
3425 | pTask = new Medium::ResizeTask(this, aLogicalSize, pProgress, pMediumLockList);
|
---|
3426 | rc = pTask->rc();
|
---|
3427 | AssertComRC(rc);
|
---|
3428 | if (FAILED(rc))
|
---|
3429 | throw rc;
|
---|
3430 | }
|
---|
3431 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3432 |
|
---|
3433 | if (SUCCEEDED(rc))
|
---|
3434 | {
|
---|
3435 | rc = pTask->createThread();
|
---|
3436 |
|
---|
3437 | if (SUCCEEDED(rc))
|
---|
3438 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3439 | }
|
---|
3440 | else if (pTask != NULL)
|
---|
3441 | delete pTask;
|
---|
3442 |
|
---|
3443 | return rc;
|
---|
3444 | }
|
---|
3445 |
|
---|
3446 | HRESULT Medium::reset(AutoCaller &autoCaller, ComPtr<IProgress> &aProgress)
|
---|
3447 | {
|
---|
3448 | HRESULT rc = S_OK;
|
---|
3449 | ComObjPtr<Progress> pProgress;
|
---|
3450 | Medium::Task *pTask = NULL;
|
---|
3451 |
|
---|
3452 | try
|
---|
3453 | {
|
---|
3454 | autoCaller.release();
|
---|
3455 |
|
---|
3456 | /* It is possible that some previous/concurrent uninit has already
|
---|
3457 | * cleared the pVirtualBox reference, see #uninit(). */
|
---|
3458 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
3459 |
|
---|
3460 | /* canClose() needs the tree lock */
|
---|
3461 | AutoMultiWriteLock2 multilock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL,
|
---|
3462 | this->lockHandle()
|
---|
3463 | COMMA_LOCKVAL_SRC_POS);
|
---|
3464 |
|
---|
3465 | autoCaller.add();
|
---|
3466 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
3467 |
|
---|
3468 | LogFlowThisFunc(("ENTER for medium %s\n", m->strLocationFull.c_str()));
|
---|
3469 |
|
---|
3470 | if (m->pParent.isNull())
|
---|
3471 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3472 | tr("Medium type of '%s' is not differencing"),
|
---|
3473 | m->strLocationFull.c_str());
|
---|
3474 |
|
---|
3475 | rc = i_canClose();
|
---|
3476 | if (FAILED(rc))
|
---|
3477 | throw rc;
|
---|
3478 |
|
---|
3479 | /* Build the medium lock list. */
|
---|
3480 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3481 | multilock.release();
|
---|
3482 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
3483 | this /* pToLockWrite */,
|
---|
3484 | false /* fMediumLockWriteAll */,
|
---|
3485 | NULL,
|
---|
3486 | *pMediumLockList);
|
---|
3487 | multilock.acquire();
|
---|
3488 | if (FAILED(rc))
|
---|
3489 | {
|
---|
3490 | delete pMediumLockList;
|
---|
3491 | throw rc;
|
---|
3492 | }
|
---|
3493 |
|
---|
3494 | multilock.release();
|
---|
3495 | rc = pMediumLockList->Lock();
|
---|
3496 | multilock.acquire();
|
---|
3497 | if (FAILED(rc))
|
---|
3498 | {
|
---|
3499 | delete pMediumLockList;
|
---|
3500 | throw setError(rc,
|
---|
3501 | tr("Failed to lock media when resetting '%s'"),
|
---|
3502 | i_getLocationFull().c_str());
|
---|
3503 | }
|
---|
3504 |
|
---|
3505 | pProgress.createObject();
|
---|
3506 | rc = pProgress->init(m->pVirtualBox,
|
---|
3507 | static_cast<IMedium*>(this),
|
---|
3508 | BstrFmt(tr("Resetting differencing medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3509 | FALSE /* aCancelable */);
|
---|
3510 | if (FAILED(rc))
|
---|
3511 | throw rc;
|
---|
3512 |
|
---|
3513 | /* setup task object to carry out the operation asynchronously */
|
---|
3514 | pTask = new Medium::ResetTask(this, pProgress, pMediumLockList);
|
---|
3515 | rc = pTask->rc();
|
---|
3516 | AssertComRC(rc);
|
---|
3517 | if (FAILED(rc))
|
---|
3518 | throw rc;
|
---|
3519 | }
|
---|
3520 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3521 |
|
---|
3522 | if (SUCCEEDED(rc))
|
---|
3523 | {
|
---|
3524 | rc = pTask->createThread();
|
---|
3525 |
|
---|
3526 | if (SUCCEEDED(rc))
|
---|
3527 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3528 | }
|
---|
3529 | else if (pTask != NULL)
|
---|
3530 | delete pTask;
|
---|
3531 |
|
---|
3532 | LogFlowThisFunc(("LEAVE, rc=%Rhrc\n", rc));
|
---|
3533 |
|
---|
3534 | return rc;
|
---|
3535 | }
|
---|
3536 |
|
---|
3537 | HRESULT Medium::changeEncryption(const com::Utf8Str &aCurrentPassword, const com::Utf8Str &aCipher,
|
---|
3538 | const com::Utf8Str &aNewPassword, const com::Utf8Str &aNewPasswordId,
|
---|
3539 | ComPtr<IProgress> &aProgress)
|
---|
3540 | {
|
---|
3541 | HRESULT rc = S_OK;
|
---|
3542 | ComObjPtr<Progress> pProgress;
|
---|
3543 | Medium::Task *pTask = NULL;
|
---|
3544 |
|
---|
3545 | try
|
---|
3546 | {
|
---|
3547 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3548 |
|
---|
3549 | DeviceType_T devType = i_getDeviceType();
|
---|
3550 | /* Cannot encrypt DVD or floppy images so far. */
|
---|
3551 | if ( devType == DeviceType_DVD
|
---|
3552 | || devType == DeviceType_Floppy)
|
---|
3553 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3554 | tr("Cannot encrypt DVD or Floppy medium '%s'"),
|
---|
3555 | m->strLocationFull.c_str());
|
---|
3556 |
|
---|
3557 | /* Cannot encrypt media which are attached to more than one virtual machine. */
|
---|
3558 | if (m->backRefs.size() > 1)
|
---|
3559 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3560 | tr("Cannot encrypt medium '%s' because it is attached to %d virtual machines"),
|
---|
3561 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
3562 |
|
---|
3563 | if (i_getChildren().size() != 0)
|
---|
3564 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3565 | tr("Cannot encrypt medium '%s' because it has %d children"),
|
---|
3566 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
3567 |
|
---|
3568 | /* Build the medium lock list. */
|
---|
3569 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3570 | alock.release();
|
---|
3571 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
3572 | this /* pToLockWrite */,
|
---|
3573 | true /* fMediumLockAllWrite */,
|
---|
3574 | NULL,
|
---|
3575 | *pMediumLockList);
|
---|
3576 | alock.acquire();
|
---|
3577 | if (FAILED(rc))
|
---|
3578 | {
|
---|
3579 | delete pMediumLockList;
|
---|
3580 | throw rc;
|
---|
3581 | }
|
---|
3582 |
|
---|
3583 | alock.release();
|
---|
3584 | rc = pMediumLockList->Lock();
|
---|
3585 | alock.acquire();
|
---|
3586 | if (FAILED(rc))
|
---|
3587 | {
|
---|
3588 | delete pMediumLockList;
|
---|
3589 | throw setError(rc,
|
---|
3590 | tr("Failed to lock media for encryption '%s'"),
|
---|
3591 | i_getLocationFull().c_str());
|
---|
3592 | }
|
---|
3593 |
|
---|
3594 | /*
|
---|
3595 | * Check all media in the chain to not contain any branches or references to
|
---|
3596 | * other virtual machines, we support encrypting only a list of differencing media at the moment.
|
---|
3597 | */
|
---|
3598 | MediumLockList::Base::const_iterator mediumListBegin = pMediumLockList->GetBegin();
|
---|
3599 | MediumLockList::Base::const_iterator mediumListEnd = pMediumLockList->GetEnd();
|
---|
3600 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
3601 | it != mediumListEnd;
|
---|
3602 | ++it)
|
---|
3603 | {
|
---|
3604 | const MediumLock &mediumLock = *it;
|
---|
3605 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
3606 | AutoReadLock mediumReadLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
3607 |
|
---|
3608 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
3609 |
|
---|
3610 | if (pMedium->m->backRefs.size() > 1)
|
---|
3611 | {
|
---|
3612 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3613 | tr("Cannot encrypt medium '%s' because it is attached to %d virtual machines"),
|
---|
3614 | pMedium->m->strLocationFull.c_str(), pMedium->m->backRefs.size());
|
---|
3615 | break;
|
---|
3616 | }
|
---|
3617 | else if (pMedium->i_getChildren().size() > 1)
|
---|
3618 | {
|
---|
3619 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3620 | tr("Cannot encrypt medium '%s' because it has %d children"),
|
---|
3621 | pMedium->m->strLocationFull.c_str(), pMedium->i_getChildren().size());
|
---|
3622 | break;
|
---|
3623 | }
|
---|
3624 | }
|
---|
3625 |
|
---|
3626 | if (FAILED(rc))
|
---|
3627 | {
|
---|
3628 | delete pMediumLockList;
|
---|
3629 | throw rc;
|
---|
3630 | }
|
---|
3631 |
|
---|
3632 | const char *pszAction = "Encrypting";
|
---|
3633 | if ( aCurrentPassword.isNotEmpty()
|
---|
3634 | && aCipher.isEmpty())
|
---|
3635 | pszAction = "Decrypting";
|
---|
3636 |
|
---|
3637 | pProgress.createObject();
|
---|
3638 | rc = pProgress->init(m->pVirtualBox,
|
---|
3639 | static_cast <IMedium *>(this),
|
---|
3640 | BstrFmt(tr("%s medium '%s'"), pszAction, m->strLocationFull.c_str()).raw(),
|
---|
3641 | TRUE /* aCancelable */);
|
---|
3642 | if (FAILED(rc))
|
---|
3643 | {
|
---|
3644 | delete pMediumLockList;
|
---|
3645 | throw rc;
|
---|
3646 | }
|
---|
3647 |
|
---|
3648 | /* setup task object to carry out the operation asynchronously */
|
---|
3649 | pTask = new Medium::EncryptTask(this, aNewPassword, aCurrentPassword,
|
---|
3650 | aCipher, aNewPasswordId, pProgress, pMediumLockList);
|
---|
3651 | rc = pTask->rc();
|
---|
3652 | AssertComRC(rc);
|
---|
3653 | if (FAILED(rc))
|
---|
3654 | throw rc;
|
---|
3655 | }
|
---|
3656 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3657 |
|
---|
3658 | if (SUCCEEDED(rc))
|
---|
3659 | {
|
---|
3660 | rc = pTask->createThread();
|
---|
3661 |
|
---|
3662 | if (SUCCEEDED(rc))
|
---|
3663 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3664 | }
|
---|
3665 | else if (pTask != NULL)
|
---|
3666 | delete pTask;
|
---|
3667 |
|
---|
3668 | return rc;
|
---|
3669 | }
|
---|
3670 |
|
---|
3671 | HRESULT Medium::getEncryptionSettings(AutoCaller &autoCaller, com::Utf8Str &aCipher, com::Utf8Str &aPasswordId)
|
---|
3672 | {
|
---|
3673 | #ifndef VBOX_WITH_EXTPACK
|
---|
3674 | RT_NOREF(aCipher, aPasswordId);
|
---|
3675 | #endif
|
---|
3676 | HRESULT rc = S_OK;
|
---|
3677 |
|
---|
3678 | try
|
---|
3679 | {
|
---|
3680 | autoCaller.release();
|
---|
3681 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
3682 | autoCaller.add();
|
---|
3683 | if (FAILED(autoCaller.rc()))
|
---|
3684 | throw rc;
|
---|
3685 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3686 |
|
---|
3687 | /* Check whether encryption is configured for this medium. */
|
---|
3688 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
3689 | if (it == pBase->m->mapProperties.end())
|
---|
3690 | throw VBOX_E_NOT_SUPPORTED;
|
---|
3691 |
|
---|
3692 | # ifdef VBOX_WITH_EXTPACK
|
---|
3693 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
3694 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
3695 | {
|
---|
3696 | /* Load the plugin */
|
---|
3697 | Utf8Str strPlugin;
|
---|
3698 | rc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
3699 | if (SUCCEEDED(rc))
|
---|
3700 | {
|
---|
3701 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
3702 | if (RT_FAILURE(vrc))
|
---|
3703 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
3704 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
3705 | i_vdError(vrc).c_str());
|
---|
3706 | }
|
---|
3707 | else
|
---|
3708 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3709 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
3710 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
3711 | }
|
---|
3712 | else
|
---|
3713 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3714 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
3715 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
3716 |
|
---|
3717 | PVDISK pDisk = NULL;
|
---|
3718 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
3719 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
3720 |
|
---|
3721 | MediumCryptoFilterSettings CryptoSettings;
|
---|
3722 |
|
---|
3723 | i_taskEncryptSettingsSetup(&CryptoSettings, NULL, it->second.c_str(), NULL, false /* fCreateKeyStore */);
|
---|
3724 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ | VD_FILTER_FLAGS_INFO, CryptoSettings.vdFilterIfaces);
|
---|
3725 | if (RT_FAILURE(vrc))
|
---|
3726 | throw setErrorBoth(VBOX_E_INVALID_OBJECT_STATE, vrc,
|
---|
3727 | tr("Failed to load the encryption filter: %s"),
|
---|
3728 | i_vdError(vrc).c_str());
|
---|
3729 |
|
---|
3730 | it = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
3731 | if (it == pBase->m->mapProperties.end())
|
---|
3732 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3733 | tr("Image is configured for encryption but doesn't has a KeyId set"));
|
---|
3734 |
|
---|
3735 | aPasswordId = it->second.c_str();
|
---|
3736 | aCipher = CryptoSettings.pszCipherReturned;
|
---|
3737 | RTStrFree(CryptoSettings.pszCipherReturned);
|
---|
3738 |
|
---|
3739 | VDDestroy(pDisk);
|
---|
3740 | # else
|
---|
3741 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3742 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
3743 | # endif
|
---|
3744 | }
|
---|
3745 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3746 |
|
---|
3747 | return rc;
|
---|
3748 | }
|
---|
3749 |
|
---|
3750 | HRESULT Medium::checkEncryptionPassword(const com::Utf8Str &aPassword)
|
---|
3751 | {
|
---|
3752 | HRESULT rc = S_OK;
|
---|
3753 |
|
---|
3754 | try
|
---|
3755 | {
|
---|
3756 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
3757 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3758 |
|
---|
3759 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
3760 | if (it == pBase->m->mapProperties.end())
|
---|
3761 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3762 | tr("The image is not configured for encryption"));
|
---|
3763 |
|
---|
3764 | if (aPassword.isEmpty())
|
---|
3765 | throw setError(E_INVALIDARG,
|
---|
3766 | tr("The given password must not be empty"));
|
---|
3767 |
|
---|
3768 | # ifdef VBOX_WITH_EXTPACK
|
---|
3769 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
3770 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
3771 | {
|
---|
3772 | /* Load the plugin */
|
---|
3773 | Utf8Str strPlugin;
|
---|
3774 | rc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
3775 | if (SUCCEEDED(rc))
|
---|
3776 | {
|
---|
3777 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
3778 | if (RT_FAILURE(vrc))
|
---|
3779 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
3780 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
3781 | i_vdError(vrc).c_str());
|
---|
3782 | }
|
---|
3783 | else
|
---|
3784 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3785 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
3786 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
3787 | }
|
---|
3788 | else
|
---|
3789 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3790 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
3791 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
3792 |
|
---|
3793 | PVDISK pDisk = NULL;
|
---|
3794 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
3795 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
3796 |
|
---|
3797 | MediumCryptoFilterSettings CryptoSettings;
|
---|
3798 |
|
---|
3799 | i_taskEncryptSettingsSetup(&CryptoSettings, NULL, it->second.c_str(), aPassword.c_str(),
|
---|
3800 | false /* fCreateKeyStore */);
|
---|
3801 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ, CryptoSettings.vdFilterIfaces);
|
---|
3802 | if (vrc == VERR_VD_PASSWORD_INCORRECT)
|
---|
3803 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
3804 | tr("The given password is incorrect"));
|
---|
3805 | else if (RT_FAILURE(vrc))
|
---|
3806 | throw setErrorBoth(VBOX_E_INVALID_OBJECT_STATE, vrc,
|
---|
3807 | tr("Failed to load the encryption filter: %s"),
|
---|
3808 | i_vdError(vrc).c_str());
|
---|
3809 |
|
---|
3810 | VDDestroy(pDisk);
|
---|
3811 | # else
|
---|
3812 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3813 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
3814 | # endif
|
---|
3815 | }
|
---|
3816 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3817 |
|
---|
3818 | return rc;
|
---|
3819 | }
|
---|
3820 |
|
---|
3821 | HRESULT Medium::openForIO(BOOL aWritable, com::Utf8Str const &aPassword, ComPtr<IMediumIO> &aMediumIO)
|
---|
3822 | {
|
---|
3823 | /*
|
---|
3824 | * Input validation.
|
---|
3825 | */
|
---|
3826 | if (aWritable && i_isReadOnly())
|
---|
3827 | return setError(E_ACCESSDENIED, tr("Write access denied: read-only"));
|
---|
3828 |
|
---|
3829 | com::Utf8Str const strKeyId = i_getKeyId();
|
---|
3830 | if (strKeyId.isEmpty() && aPassword.isNotEmpty())
|
---|
3831 | return setError(E_INVALIDARG, tr("Password given for unencrypted medium"));
|
---|
3832 | if (strKeyId.isNotEmpty() && aPassword.isEmpty())
|
---|
3833 | return setError(E_INVALIDARG, tr("Password needed for encrypted medium"));
|
---|
3834 |
|
---|
3835 | /*
|
---|
3836 | * Create IO object and return it.
|
---|
3837 | */
|
---|
3838 | ComObjPtr<MediumIO> ptrIO;
|
---|
3839 | HRESULT hrc = ptrIO.createObject();
|
---|
3840 | if (SUCCEEDED(hrc))
|
---|
3841 | {
|
---|
3842 | hrc = ptrIO->initForMedium(this, m->pVirtualBox, aWritable != FALSE, strKeyId, aPassword);
|
---|
3843 | if (SUCCEEDED(hrc))
|
---|
3844 | ptrIO.queryInterfaceTo(aMediumIO.asOutParam());
|
---|
3845 | }
|
---|
3846 | return hrc;
|
---|
3847 | }
|
---|
3848 |
|
---|
3849 |
|
---|
3850 | ////////////////////////////////////////////////////////////////////////////////
|
---|
3851 | //
|
---|
3852 | // Medium public internal methods
|
---|
3853 | //
|
---|
3854 | ////////////////////////////////////////////////////////////////////////////////
|
---|
3855 |
|
---|
3856 | /**
|
---|
3857 | * Internal method to return the medium's parent medium. Must have caller + locking!
|
---|
3858 | * @return
|
---|
3859 | */
|
---|
3860 | const ComObjPtr<Medium>& Medium::i_getParent() const
|
---|
3861 | {
|
---|
3862 | return m->pParent;
|
---|
3863 | }
|
---|
3864 |
|
---|
3865 | /**
|
---|
3866 | * Internal method to return the medium's list of child media. Must have caller + locking!
|
---|
3867 | * @return
|
---|
3868 | */
|
---|
3869 | const MediaList& Medium::i_getChildren() const
|
---|
3870 | {
|
---|
3871 | return m->llChildren;
|
---|
3872 | }
|
---|
3873 |
|
---|
3874 | /**
|
---|
3875 | * Internal method to return the medium's GUID. Must have caller + locking!
|
---|
3876 | * @return
|
---|
3877 | */
|
---|
3878 | const Guid& Medium::i_getId() const
|
---|
3879 | {
|
---|
3880 | return m->id;
|
---|
3881 | }
|
---|
3882 |
|
---|
3883 | /**
|
---|
3884 | * Internal method to return the medium's state. Must have caller + locking!
|
---|
3885 | * @return
|
---|
3886 | */
|
---|
3887 | MediumState_T Medium::i_getState() const
|
---|
3888 | {
|
---|
3889 | return m->state;
|
---|
3890 | }
|
---|
3891 |
|
---|
3892 | /**
|
---|
3893 | * Internal method to return the medium's variant. Must have caller + locking!
|
---|
3894 | * @return
|
---|
3895 | */
|
---|
3896 | MediumVariant_T Medium::i_getVariant() const
|
---|
3897 | {
|
---|
3898 | return m->variant;
|
---|
3899 | }
|
---|
3900 |
|
---|
3901 | /**
|
---|
3902 | * Internal method which returns true if this medium represents a host drive.
|
---|
3903 | * @return
|
---|
3904 | */
|
---|
3905 | bool Medium::i_isHostDrive() const
|
---|
3906 | {
|
---|
3907 | return m->hostDrive;
|
---|
3908 | }
|
---|
3909 |
|
---|
3910 | /**
|
---|
3911 | * Internal method to return the medium's full location. Must have caller + locking!
|
---|
3912 | * @return
|
---|
3913 | */
|
---|
3914 | const Utf8Str& Medium::i_getLocationFull() const
|
---|
3915 | {
|
---|
3916 | return m->strLocationFull;
|
---|
3917 | }
|
---|
3918 |
|
---|
3919 | /**
|
---|
3920 | * Internal method to return the medium's format string. Must have caller + locking!
|
---|
3921 | * @return
|
---|
3922 | */
|
---|
3923 | const Utf8Str& Medium::i_getFormat() const
|
---|
3924 | {
|
---|
3925 | return m->strFormat;
|
---|
3926 | }
|
---|
3927 |
|
---|
3928 | /**
|
---|
3929 | * Internal method to return the medium's format object. Must have caller + locking!
|
---|
3930 | * @return
|
---|
3931 | */
|
---|
3932 | const ComObjPtr<MediumFormat>& Medium::i_getMediumFormat() const
|
---|
3933 | {
|
---|
3934 | return m->formatObj;
|
---|
3935 | }
|
---|
3936 |
|
---|
3937 | /**
|
---|
3938 | * Internal method that returns true if the medium is represented by a file on the host disk
|
---|
3939 | * (and not iSCSI or something).
|
---|
3940 | * @return
|
---|
3941 | */
|
---|
3942 | bool Medium::i_isMediumFormatFile() const
|
---|
3943 | {
|
---|
3944 | if ( m->formatObj
|
---|
3945 | && (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
3946 | )
|
---|
3947 | return true;
|
---|
3948 | return false;
|
---|
3949 | }
|
---|
3950 |
|
---|
3951 | /**
|
---|
3952 | * Internal method to return the medium's size. Must have caller + locking!
|
---|
3953 | * @return
|
---|
3954 | */
|
---|
3955 | uint64_t Medium::i_getSize() const
|
---|
3956 | {
|
---|
3957 | return m->size;
|
---|
3958 | }
|
---|
3959 |
|
---|
3960 | /**
|
---|
3961 | * Internal method to return the medium's size. Must have caller + locking!
|
---|
3962 | * @return
|
---|
3963 | */
|
---|
3964 | uint64_t Medium::i_getLogicalSize() const
|
---|
3965 | {
|
---|
3966 | return m->logicalSize;
|
---|
3967 | }
|
---|
3968 |
|
---|
3969 | /**
|
---|
3970 | * Returns the medium device type. Must have caller + locking!
|
---|
3971 | * @return
|
---|
3972 | */
|
---|
3973 | DeviceType_T Medium::i_getDeviceType() const
|
---|
3974 | {
|
---|
3975 | return m->devType;
|
---|
3976 | }
|
---|
3977 |
|
---|
3978 | /**
|
---|
3979 | * Returns the medium type. Must have caller + locking!
|
---|
3980 | * @return
|
---|
3981 | */
|
---|
3982 | MediumType_T Medium::i_getType() const
|
---|
3983 | {
|
---|
3984 | return m->type;
|
---|
3985 | }
|
---|
3986 |
|
---|
3987 | /**
|
---|
3988 | * Returns a short version of the location attribute.
|
---|
3989 | *
|
---|
3990 | * @note Must be called from under this object's read or write lock.
|
---|
3991 | */
|
---|
3992 | Utf8Str Medium::i_getName()
|
---|
3993 | {
|
---|
3994 | Utf8Str name = RTPathFilename(m->strLocationFull.c_str());
|
---|
3995 | return name;
|
---|
3996 | }
|
---|
3997 |
|
---|
3998 | /**
|
---|
3999 | * This adds the given UUID to the list of media registries in which this
|
---|
4000 | * medium should be registered. The UUID can either be a machine UUID,
|
---|
4001 | * to add a machine registry, or the global registry UUID as returned by
|
---|
4002 | * VirtualBox::getGlobalRegistryId().
|
---|
4003 | *
|
---|
4004 | * Note that for hard disks, this method does nothing if the medium is
|
---|
4005 | * already in another registry to avoid having hard disks in more than
|
---|
4006 | * one registry, which causes trouble with keeping diff images in sync.
|
---|
4007 | * See getFirstRegistryMachineId() for details.
|
---|
4008 | *
|
---|
4009 | * @param id
|
---|
4010 | * @return true if the registry was added; false if the given id was already on the list.
|
---|
4011 | */
|
---|
4012 | bool Medium::i_addRegistry(const Guid& id)
|
---|
4013 | {
|
---|
4014 | AutoCaller autoCaller(this);
|
---|
4015 | if (FAILED(autoCaller.rc()))
|
---|
4016 | return false;
|
---|
4017 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4018 |
|
---|
4019 | bool fAdd = true;
|
---|
4020 |
|
---|
4021 | // hard disks cannot be in more than one registry
|
---|
4022 | if ( m->devType == DeviceType_HardDisk
|
---|
4023 | && m->llRegistryIDs.size() > 0)
|
---|
4024 | fAdd = false;
|
---|
4025 |
|
---|
4026 | // no need to add the UUID twice
|
---|
4027 | if (fAdd)
|
---|
4028 | {
|
---|
4029 | for (GuidList::const_iterator it = m->llRegistryIDs.begin();
|
---|
4030 | it != m->llRegistryIDs.end();
|
---|
4031 | ++it)
|
---|
4032 | {
|
---|
4033 | if ((*it) == id)
|
---|
4034 | {
|
---|
4035 | fAdd = false;
|
---|
4036 | break;
|
---|
4037 | }
|
---|
4038 | }
|
---|
4039 | }
|
---|
4040 |
|
---|
4041 | if (fAdd)
|
---|
4042 | m->llRegistryIDs.push_back(id);
|
---|
4043 |
|
---|
4044 | return fAdd;
|
---|
4045 | }
|
---|
4046 |
|
---|
4047 | /**
|
---|
4048 | * This adds the given UUID to the list of media registries in which this
|
---|
4049 | * medium should be registered. The UUID can either be a machine UUID,
|
---|
4050 | * to add a machine registry, or the global registry UUID as returned by
|
---|
4051 | * VirtualBox::getGlobalRegistryId(). This recurses over all children.
|
---|
4052 | *
|
---|
4053 | * Note that for hard disks, this method does nothing if the medium is
|
---|
4054 | * already in another registry to avoid having hard disks in more than
|
---|
4055 | * one registry, which causes trouble with keeping diff images in sync.
|
---|
4056 | * See getFirstRegistryMachineId() for details.
|
---|
4057 | *
|
---|
4058 | * @note the caller must hold the media tree lock for reading.
|
---|
4059 | *
|
---|
4060 | * @param id
|
---|
4061 | * @return true if the registry was added; false if the given id was already on the list.
|
---|
4062 | */
|
---|
4063 | bool Medium::i_addRegistryRecursive(const Guid &id)
|
---|
4064 | {
|
---|
4065 | AutoCaller autoCaller(this);
|
---|
4066 | if (FAILED(autoCaller.rc()))
|
---|
4067 | return false;
|
---|
4068 |
|
---|
4069 | bool fAdd = i_addRegistry(id);
|
---|
4070 |
|
---|
4071 | // protected by the medium tree lock held by our original caller
|
---|
4072 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
4073 | it != i_getChildren().end();
|
---|
4074 | ++it)
|
---|
4075 | {
|
---|
4076 | Medium *pChild = *it;
|
---|
4077 | fAdd |= pChild->i_addRegistryRecursive(id);
|
---|
4078 | }
|
---|
4079 |
|
---|
4080 | return fAdd;
|
---|
4081 | }
|
---|
4082 |
|
---|
4083 | /**
|
---|
4084 | * Removes the given UUID from the list of media registry UUIDs of this medium.
|
---|
4085 | *
|
---|
4086 | * @param id
|
---|
4087 | * @return true if the UUID was found or false if not.
|
---|
4088 | */
|
---|
4089 | bool Medium::i_removeRegistry(const Guid &id)
|
---|
4090 | {
|
---|
4091 | AutoCaller autoCaller(this);
|
---|
4092 | if (FAILED(autoCaller.rc()))
|
---|
4093 | return false;
|
---|
4094 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4095 |
|
---|
4096 | bool fRemove = false;
|
---|
4097 |
|
---|
4098 | /// @todo r=klaus eliminate this code, replace it by using find.
|
---|
4099 | for (GuidList::iterator it = m->llRegistryIDs.begin();
|
---|
4100 | it != m->llRegistryIDs.end();
|
---|
4101 | ++it)
|
---|
4102 | {
|
---|
4103 | if ((*it) == id)
|
---|
4104 | {
|
---|
4105 | // getting away with this as the iterator isn't used after
|
---|
4106 | m->llRegistryIDs.erase(it);
|
---|
4107 | fRemove = true;
|
---|
4108 | break;
|
---|
4109 | }
|
---|
4110 | }
|
---|
4111 |
|
---|
4112 | return fRemove;
|
---|
4113 | }
|
---|
4114 |
|
---|
4115 | /**
|
---|
4116 | * Removes the given UUID from the list of media registry UUIDs, for this
|
---|
4117 | * medium and all its children recursively.
|
---|
4118 | *
|
---|
4119 | * @note the caller must hold the media tree lock for reading.
|
---|
4120 | *
|
---|
4121 | * @param id
|
---|
4122 | * @return true if the UUID was found or false if not.
|
---|
4123 | */
|
---|
4124 | bool Medium::i_removeRegistryRecursive(const Guid &id)
|
---|
4125 | {
|
---|
4126 | AutoCaller autoCaller(this);
|
---|
4127 | if (FAILED(autoCaller.rc()))
|
---|
4128 | return false;
|
---|
4129 |
|
---|
4130 | bool fRemove = i_removeRegistry(id);
|
---|
4131 |
|
---|
4132 | // protected by the medium tree lock held by our original caller
|
---|
4133 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
4134 | it != i_getChildren().end();
|
---|
4135 | ++it)
|
---|
4136 | {
|
---|
4137 | Medium *pChild = *it;
|
---|
4138 | fRemove |= pChild->i_removeRegistryRecursive(id);
|
---|
4139 | }
|
---|
4140 |
|
---|
4141 | return fRemove;
|
---|
4142 | }
|
---|
4143 |
|
---|
4144 | /**
|
---|
4145 | * Returns true if id is in the list of media registries for this medium.
|
---|
4146 | *
|
---|
4147 | * Must have caller + read locking!
|
---|
4148 | *
|
---|
4149 | * @param id
|
---|
4150 | * @return
|
---|
4151 | */
|
---|
4152 | bool Medium::i_isInRegistry(const Guid &id)
|
---|
4153 | {
|
---|
4154 | /// @todo r=klaus eliminate this code, replace it by using find.
|
---|
4155 | for (GuidList::const_iterator it = m->llRegistryIDs.begin();
|
---|
4156 | it != m->llRegistryIDs.end();
|
---|
4157 | ++it)
|
---|
4158 | {
|
---|
4159 | if (*it == id)
|
---|
4160 | return true;
|
---|
4161 | }
|
---|
4162 |
|
---|
4163 | return false;
|
---|
4164 | }
|
---|
4165 |
|
---|
4166 | /**
|
---|
4167 | * Internal method to return the medium's first registry machine (i.e. the machine in whose
|
---|
4168 | * machine XML this medium is listed).
|
---|
4169 | *
|
---|
4170 | * Every attached medium must now (4.0) reside in at least one media registry, which is identified
|
---|
4171 | * by a UUID. This is either a machine UUID if the machine is from 4.0 or newer, in which case
|
---|
4172 | * machines have their own media registries, or it is the pseudo-UUID of the VirtualBox
|
---|
4173 | * object if the machine is old and still needs the global registry in VirtualBox.xml.
|
---|
4174 | *
|
---|
4175 | * By definition, hard disks may only be in one media registry, in which all its children
|
---|
4176 | * will be stored as well. Otherwise we run into problems with having keep multiple registries
|
---|
4177 | * in sync. (This is the "cloned VM" case in which VM1 may link to the disks of VM2; in this
|
---|
4178 | * case, only VM2's registry is used for the disk in question.)
|
---|
4179 | *
|
---|
4180 | * If there is no medium registry, particularly if the medium has not been attached yet, this
|
---|
4181 | * does not modify uuid and returns false.
|
---|
4182 | *
|
---|
4183 | * ISOs and RAWs, by contrast, can be in more than one repository to make things easier for
|
---|
4184 | * the user.
|
---|
4185 | *
|
---|
4186 | * Must have caller + locking!
|
---|
4187 | *
|
---|
4188 | * @param uuid Receives first registry machine UUID, if available.
|
---|
4189 | * @return true if uuid was set.
|
---|
4190 | */
|
---|
4191 | bool Medium::i_getFirstRegistryMachineId(Guid &uuid) const
|
---|
4192 | {
|
---|
4193 | if (m->llRegistryIDs.size())
|
---|
4194 | {
|
---|
4195 | uuid = m->llRegistryIDs.front();
|
---|
4196 | return true;
|
---|
4197 | }
|
---|
4198 | return false;
|
---|
4199 | }
|
---|
4200 |
|
---|
4201 | /**
|
---|
4202 | * Marks all the registries in which this medium is registered as modified.
|
---|
4203 | */
|
---|
4204 | void Medium::i_markRegistriesModified()
|
---|
4205 | {
|
---|
4206 | AutoCaller autoCaller(this);
|
---|
4207 | if (FAILED(autoCaller.rc())) return;
|
---|
4208 |
|
---|
4209 | // Get local copy, as keeping the lock over VirtualBox::markRegistryModified
|
---|
4210 | // causes trouble with the lock order
|
---|
4211 | GuidList llRegistryIDs;
|
---|
4212 | {
|
---|
4213 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4214 | llRegistryIDs = m->llRegistryIDs;
|
---|
4215 | }
|
---|
4216 |
|
---|
4217 | autoCaller.release();
|
---|
4218 |
|
---|
4219 | /* Save the error information now, the implicit restore when this goes
|
---|
4220 | * out of scope will throw away spurious additional errors created below. */
|
---|
4221 | ErrorInfoKeeper eik;
|
---|
4222 | for (GuidList::const_iterator it = llRegistryIDs.begin();
|
---|
4223 | it != llRegistryIDs.end();
|
---|
4224 | ++it)
|
---|
4225 | {
|
---|
4226 | m->pVirtualBox->i_markRegistryModified(*it);
|
---|
4227 | }
|
---|
4228 | }
|
---|
4229 |
|
---|
4230 | /**
|
---|
4231 | * Adds the given machine and optionally the snapshot to the list of the objects
|
---|
4232 | * this medium is attached to.
|
---|
4233 | *
|
---|
4234 | * @param aMachineId Machine ID.
|
---|
4235 | * @param aSnapshotId Snapshot ID; when non-empty, adds a snapshot attachment.
|
---|
4236 | */
|
---|
4237 | HRESULT Medium::i_addBackReference(const Guid &aMachineId,
|
---|
4238 | const Guid &aSnapshotId /*= Guid::Empty*/)
|
---|
4239 | {
|
---|
4240 | AssertReturn(aMachineId.isValid(), E_FAIL);
|
---|
4241 |
|
---|
4242 | LogFlowThisFunc(("ENTER, aMachineId: {%RTuuid}, aSnapshotId: {%RTuuid}\n", aMachineId.raw(), aSnapshotId.raw()));
|
---|
4243 |
|
---|
4244 | AutoCaller autoCaller(this);
|
---|
4245 | AssertComRCReturnRC(autoCaller.rc());
|
---|
4246 |
|
---|
4247 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4248 |
|
---|
4249 | switch (m->state)
|
---|
4250 | {
|
---|
4251 | case MediumState_Created:
|
---|
4252 | case MediumState_Inaccessible:
|
---|
4253 | case MediumState_LockedRead:
|
---|
4254 | case MediumState_LockedWrite:
|
---|
4255 | break;
|
---|
4256 |
|
---|
4257 | default:
|
---|
4258 | return i_setStateError();
|
---|
4259 | }
|
---|
4260 |
|
---|
4261 | if (m->numCreateDiffTasks > 0)
|
---|
4262 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4263 | tr("Cannot attach medium '%s' {%RTuuid}: %u differencing child media are being created"),
|
---|
4264 | m->strLocationFull.c_str(),
|
---|
4265 | m->id.raw(),
|
---|
4266 | m->numCreateDiffTasks);
|
---|
4267 |
|
---|
4268 | BackRefList::iterator it = std::find_if(m->backRefs.begin(),
|
---|
4269 | m->backRefs.end(),
|
---|
4270 | BackRef::EqualsTo(aMachineId));
|
---|
4271 | if (it == m->backRefs.end())
|
---|
4272 | {
|
---|
4273 | BackRef ref(aMachineId, aSnapshotId);
|
---|
4274 | m->backRefs.push_back(ref);
|
---|
4275 |
|
---|
4276 | return S_OK;
|
---|
4277 | }
|
---|
4278 |
|
---|
4279 | // if the caller has not supplied a snapshot ID, then we're attaching
|
---|
4280 | // to a machine a medium which represents the machine's current state,
|
---|
4281 | // so set the flag
|
---|
4282 |
|
---|
4283 | if (aSnapshotId.isZero())
|
---|
4284 | {
|
---|
4285 | /* sanity: no duplicate attachments */
|
---|
4286 | if (it->fInCurState)
|
---|
4287 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4288 | tr("Cannot attach medium '%s' {%RTuuid}: medium is already associated with the current state of machine uuid {%RTuuid}!"),
|
---|
4289 | m->strLocationFull.c_str(),
|
---|
4290 | m->id.raw(),
|
---|
4291 | aMachineId.raw());
|
---|
4292 | it->fInCurState = true;
|
---|
4293 |
|
---|
4294 | return S_OK;
|
---|
4295 | }
|
---|
4296 |
|
---|
4297 | // otherwise: a snapshot medium is being attached
|
---|
4298 |
|
---|
4299 | /* sanity: no duplicate attachments */
|
---|
4300 | for (GuidList::const_iterator jt = it->llSnapshotIds.begin();
|
---|
4301 | jt != it->llSnapshotIds.end();
|
---|
4302 | ++jt)
|
---|
4303 | {
|
---|
4304 | const Guid &idOldSnapshot = *jt;
|
---|
4305 |
|
---|
4306 | if (idOldSnapshot == aSnapshotId)
|
---|
4307 | {
|
---|
4308 | #ifdef DEBUG
|
---|
4309 | i_dumpBackRefs();
|
---|
4310 | #endif
|
---|
4311 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4312 | tr("Cannot attach medium '%s' {%RTuuid} from snapshot '%RTuuid': medium is already in use by this snapshot!"),
|
---|
4313 | m->strLocationFull.c_str(),
|
---|
4314 | m->id.raw(),
|
---|
4315 | aSnapshotId.raw());
|
---|
4316 | }
|
---|
4317 | }
|
---|
4318 |
|
---|
4319 | it->llSnapshotIds.push_back(aSnapshotId);
|
---|
4320 | // Do not touch fInCurState, as the image may be attached to the current
|
---|
4321 | // state *and* a snapshot, otherwise we lose the current state association!
|
---|
4322 |
|
---|
4323 | LogFlowThisFuncLeave();
|
---|
4324 |
|
---|
4325 | return S_OK;
|
---|
4326 | }
|
---|
4327 |
|
---|
4328 | /**
|
---|
4329 | * Removes the given machine and optionally the snapshot from the list of the
|
---|
4330 | * objects this medium is attached to.
|
---|
4331 | *
|
---|
4332 | * @param aMachineId Machine ID.
|
---|
4333 | * @param aSnapshotId Snapshot ID; when non-empty, removes the snapshot
|
---|
4334 | * attachment.
|
---|
4335 | */
|
---|
4336 | HRESULT Medium::i_removeBackReference(const Guid &aMachineId,
|
---|
4337 | const Guid &aSnapshotId /*= Guid::Empty*/)
|
---|
4338 | {
|
---|
4339 | AssertReturn(aMachineId.isValid(), E_FAIL);
|
---|
4340 |
|
---|
4341 | AutoCaller autoCaller(this);
|
---|
4342 | AssertComRCReturnRC(autoCaller.rc());
|
---|
4343 |
|
---|
4344 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4345 |
|
---|
4346 | BackRefList::iterator it =
|
---|
4347 | std::find_if(m->backRefs.begin(), m->backRefs.end(),
|
---|
4348 | BackRef::EqualsTo(aMachineId));
|
---|
4349 | AssertReturn(it != m->backRefs.end(), E_FAIL);
|
---|
4350 |
|
---|
4351 | if (aSnapshotId.isZero())
|
---|
4352 | {
|
---|
4353 | /* remove the current state attachment */
|
---|
4354 | it->fInCurState = false;
|
---|
4355 | }
|
---|
4356 | else
|
---|
4357 | {
|
---|
4358 | /* remove the snapshot attachment */
|
---|
4359 | GuidList::iterator jt = std::find(it->llSnapshotIds.begin(),
|
---|
4360 | it->llSnapshotIds.end(),
|
---|
4361 | aSnapshotId);
|
---|
4362 |
|
---|
4363 | AssertReturn(jt != it->llSnapshotIds.end(), E_FAIL);
|
---|
4364 | it->llSnapshotIds.erase(jt);
|
---|
4365 | }
|
---|
4366 |
|
---|
4367 | /* if the backref becomes empty, remove it */
|
---|
4368 | if (it->fInCurState == false && it->llSnapshotIds.size() == 0)
|
---|
4369 | m->backRefs.erase(it);
|
---|
4370 |
|
---|
4371 | return S_OK;
|
---|
4372 | }
|
---|
4373 |
|
---|
4374 | /**
|
---|
4375 | * Internal method to return the medium's list of backrefs. Must have caller + locking!
|
---|
4376 | * @return
|
---|
4377 | */
|
---|
4378 | const Guid* Medium::i_getFirstMachineBackrefId() const
|
---|
4379 | {
|
---|
4380 | if (!m->backRefs.size())
|
---|
4381 | return NULL;
|
---|
4382 |
|
---|
4383 | return &m->backRefs.front().machineId;
|
---|
4384 | }
|
---|
4385 |
|
---|
4386 | /**
|
---|
4387 | * Internal method which returns a machine that either this medium or one of its children
|
---|
4388 | * is attached to. This is used for finding a replacement media registry when an existing
|
---|
4389 | * media registry is about to be deleted in VirtualBox::unregisterMachine().
|
---|
4390 | *
|
---|
4391 | * Must have caller + locking, *and* caller must hold the media tree lock!
|
---|
4392 | * @return
|
---|
4393 | */
|
---|
4394 | const Guid* Medium::i_getAnyMachineBackref() const
|
---|
4395 | {
|
---|
4396 | if (m->backRefs.size())
|
---|
4397 | return &m->backRefs.front().machineId;
|
---|
4398 |
|
---|
4399 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
4400 | it != i_getChildren().end();
|
---|
4401 | ++it)
|
---|
4402 | {
|
---|
4403 | Medium *pChild = *it;
|
---|
4404 | // recurse for this child
|
---|
4405 | const Guid* puuid;
|
---|
4406 | if ((puuid = pChild->i_getAnyMachineBackref()))
|
---|
4407 | return puuid;
|
---|
4408 | }
|
---|
4409 |
|
---|
4410 | return NULL;
|
---|
4411 | }
|
---|
4412 |
|
---|
4413 | const Guid* Medium::i_getFirstMachineBackrefSnapshotId() const
|
---|
4414 | {
|
---|
4415 | if (!m->backRefs.size())
|
---|
4416 | return NULL;
|
---|
4417 |
|
---|
4418 | const BackRef &ref = m->backRefs.front();
|
---|
4419 | if (ref.llSnapshotIds.empty())
|
---|
4420 | return NULL;
|
---|
4421 |
|
---|
4422 | return &ref.llSnapshotIds.front();
|
---|
4423 | }
|
---|
4424 |
|
---|
4425 | size_t Medium::i_getMachineBackRefCount() const
|
---|
4426 | {
|
---|
4427 | return m->backRefs.size();
|
---|
4428 | }
|
---|
4429 |
|
---|
4430 | #ifdef DEBUG
|
---|
4431 | /**
|
---|
4432 | * Debugging helper that gets called after VirtualBox initialization that writes all
|
---|
4433 | * machine backreferences to the debug log.
|
---|
4434 | */
|
---|
4435 | void Medium::i_dumpBackRefs()
|
---|
4436 | {
|
---|
4437 | AutoCaller autoCaller(this);
|
---|
4438 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4439 |
|
---|
4440 | LogFlowThisFunc(("Dumping backrefs for medium '%s':\n", m->strLocationFull.c_str()));
|
---|
4441 |
|
---|
4442 | for (BackRefList::iterator it2 = m->backRefs.begin();
|
---|
4443 | it2 != m->backRefs.end();
|
---|
4444 | ++it2)
|
---|
4445 | {
|
---|
4446 | const BackRef &ref = *it2;
|
---|
4447 | LogFlowThisFunc((" Backref from machine {%RTuuid} (fInCurState: %d)\n", ref.machineId.raw(), ref.fInCurState));
|
---|
4448 |
|
---|
4449 | for (GuidList::const_iterator jt2 = it2->llSnapshotIds.begin();
|
---|
4450 | jt2 != it2->llSnapshotIds.end();
|
---|
4451 | ++jt2)
|
---|
4452 | {
|
---|
4453 | const Guid &id = *jt2;
|
---|
4454 | LogFlowThisFunc((" Backref from snapshot {%RTuuid}\n", id.raw()));
|
---|
4455 | }
|
---|
4456 | }
|
---|
4457 | }
|
---|
4458 | #endif
|
---|
4459 |
|
---|
4460 | /**
|
---|
4461 | * Checks if the given change of \a aOldPath to \a aNewPath affects the location
|
---|
4462 | * of this media and updates it if necessary to reflect the new location.
|
---|
4463 | *
|
---|
4464 | * @param strOldPath Old path (full).
|
---|
4465 | * @param strNewPath New path (full).
|
---|
4466 | *
|
---|
4467 | * @note Locks this object for writing.
|
---|
4468 | */
|
---|
4469 | HRESULT Medium::i_updatePath(const Utf8Str &strOldPath, const Utf8Str &strNewPath)
|
---|
4470 | {
|
---|
4471 | AssertReturn(!strOldPath.isEmpty(), E_FAIL);
|
---|
4472 | AssertReturn(!strNewPath.isEmpty(), E_FAIL);
|
---|
4473 |
|
---|
4474 | AutoCaller autoCaller(this);
|
---|
4475 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4476 |
|
---|
4477 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4478 |
|
---|
4479 | LogFlowThisFunc(("locationFull.before='%s'\n", m->strLocationFull.c_str()));
|
---|
4480 |
|
---|
4481 | const char *pcszMediumPath = m->strLocationFull.c_str();
|
---|
4482 |
|
---|
4483 | if (RTPathStartsWith(pcszMediumPath, strOldPath.c_str()))
|
---|
4484 | {
|
---|
4485 | Utf8Str newPath(strNewPath);
|
---|
4486 | newPath.append(pcszMediumPath + strOldPath.length());
|
---|
4487 | unconst(m->strLocationFull) = newPath;
|
---|
4488 |
|
---|
4489 | LogFlowThisFunc(("locationFull.after='%s'\n", m->strLocationFull.c_str()));
|
---|
4490 | // we changed something
|
---|
4491 | return S_OK;
|
---|
4492 | }
|
---|
4493 |
|
---|
4494 | // no change was necessary, signal error which the caller needs to interpret
|
---|
4495 | return VBOX_E_FILE_ERROR;
|
---|
4496 | }
|
---|
4497 |
|
---|
4498 | /**
|
---|
4499 | * Returns the base medium of the media chain this medium is part of.
|
---|
4500 | *
|
---|
4501 | * The base medium is found by walking up the parent-child relationship axis.
|
---|
4502 | * If the medium doesn't have a parent (i.e. it's a base medium), it
|
---|
4503 | * returns itself in response to this method.
|
---|
4504 | *
|
---|
4505 | * @param aLevel Where to store the number of ancestors of this medium
|
---|
4506 | * (zero for the base), may be @c NULL.
|
---|
4507 | *
|
---|
4508 | * @note Locks medium tree for reading.
|
---|
4509 | */
|
---|
4510 | ComObjPtr<Medium> Medium::i_getBase(uint32_t *aLevel /*= NULL*/)
|
---|
4511 | {
|
---|
4512 | ComObjPtr<Medium> pBase;
|
---|
4513 |
|
---|
4514 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
4515 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
4516 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
4517 | if (!pVirtualBox)
|
---|
4518 | return pBase;
|
---|
4519 |
|
---|
4520 | /* we access m->pParent */
|
---|
4521 | AutoReadLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4522 |
|
---|
4523 | AutoCaller autoCaller(this);
|
---|
4524 | AssertReturn(autoCaller.isOk(), pBase);
|
---|
4525 |
|
---|
4526 | pBase = this;
|
---|
4527 | uint32_t level = 0;
|
---|
4528 |
|
---|
4529 | if (m->pParent)
|
---|
4530 | {
|
---|
4531 | for (;;)
|
---|
4532 | {
|
---|
4533 | AutoCaller baseCaller(pBase);
|
---|
4534 | AssertReturn(baseCaller.isOk(), pBase);
|
---|
4535 |
|
---|
4536 | if (pBase->m->pParent.isNull())
|
---|
4537 | break;
|
---|
4538 |
|
---|
4539 | pBase = pBase->m->pParent;
|
---|
4540 | ++level;
|
---|
4541 | }
|
---|
4542 | }
|
---|
4543 |
|
---|
4544 | if (aLevel != NULL)
|
---|
4545 | *aLevel = level;
|
---|
4546 |
|
---|
4547 | return pBase;
|
---|
4548 | }
|
---|
4549 |
|
---|
4550 | /**
|
---|
4551 | * Returns the depth of this medium in the media chain.
|
---|
4552 | *
|
---|
4553 | * @note Locks medium tree for reading.
|
---|
4554 | */
|
---|
4555 | uint32_t Medium::i_getDepth()
|
---|
4556 | {
|
---|
4557 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
4558 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
4559 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
4560 | if (!pVirtualBox)
|
---|
4561 | return 1;
|
---|
4562 |
|
---|
4563 | /* we access m->pParent */
|
---|
4564 | AutoReadLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4565 |
|
---|
4566 | uint32_t cDepth = 0;
|
---|
4567 | ComObjPtr<Medium> pMedium(this);
|
---|
4568 | while (!pMedium.isNull())
|
---|
4569 | {
|
---|
4570 | AutoCaller autoCaller(this);
|
---|
4571 | AssertReturn(autoCaller.isOk(), cDepth + 1);
|
---|
4572 |
|
---|
4573 | pMedium = pMedium->m->pParent;
|
---|
4574 | cDepth++;
|
---|
4575 | }
|
---|
4576 |
|
---|
4577 | return cDepth;
|
---|
4578 | }
|
---|
4579 |
|
---|
4580 | /**
|
---|
4581 | * Returns @c true if this medium cannot be modified because it has
|
---|
4582 | * dependents (children) or is part of the snapshot. Related to the medium
|
---|
4583 | * type and posterity, not to the current media state.
|
---|
4584 | *
|
---|
4585 | * @note Locks this object and medium tree for reading.
|
---|
4586 | */
|
---|
4587 | bool Medium::i_isReadOnly()
|
---|
4588 | {
|
---|
4589 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
4590 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
4591 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
4592 | if (!pVirtualBox)
|
---|
4593 | return false;
|
---|
4594 |
|
---|
4595 | /* we access children */
|
---|
4596 | AutoReadLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4597 |
|
---|
4598 | AutoCaller autoCaller(this);
|
---|
4599 | AssertComRCReturn(autoCaller.rc(), false);
|
---|
4600 |
|
---|
4601 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4602 |
|
---|
4603 | switch (m->type)
|
---|
4604 | {
|
---|
4605 | case MediumType_Normal:
|
---|
4606 | {
|
---|
4607 | if (i_getChildren().size() != 0)
|
---|
4608 | return true;
|
---|
4609 |
|
---|
4610 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
4611 | it != m->backRefs.end(); ++it)
|
---|
4612 | if (it->llSnapshotIds.size() != 0)
|
---|
4613 | return true;
|
---|
4614 |
|
---|
4615 | if (m->variant & MediumVariant_VmdkStreamOptimized)
|
---|
4616 | return true;
|
---|
4617 |
|
---|
4618 | return false;
|
---|
4619 | }
|
---|
4620 | case MediumType_Immutable:
|
---|
4621 | case MediumType_MultiAttach:
|
---|
4622 | return true;
|
---|
4623 | case MediumType_Writethrough:
|
---|
4624 | case MediumType_Shareable:
|
---|
4625 | case MediumType_Readonly: /* explicit readonly media has no diffs */
|
---|
4626 | return false;
|
---|
4627 | default:
|
---|
4628 | break;
|
---|
4629 | }
|
---|
4630 |
|
---|
4631 | AssertFailedReturn(false);
|
---|
4632 | }
|
---|
4633 |
|
---|
4634 | /**
|
---|
4635 | * Internal method to return the medium's size. Must have caller + locking!
|
---|
4636 | * @return
|
---|
4637 | */
|
---|
4638 | void Medium::i_updateId(const Guid &id)
|
---|
4639 | {
|
---|
4640 | unconst(m->id) = id;
|
---|
4641 | }
|
---|
4642 |
|
---|
4643 | /**
|
---|
4644 | * Saves the settings of one medium.
|
---|
4645 | *
|
---|
4646 | * @note Caller MUST take care of the medium tree lock and caller.
|
---|
4647 | *
|
---|
4648 | * @param data Settings struct to be updated.
|
---|
4649 | * @param strHardDiskFolder Folder for which paths should be relative.
|
---|
4650 | */
|
---|
4651 | void Medium::i_saveSettingsOne(settings::Medium &data, const Utf8Str &strHardDiskFolder)
|
---|
4652 | {
|
---|
4653 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4654 |
|
---|
4655 | data.uuid = m->id;
|
---|
4656 |
|
---|
4657 | // make path relative if needed
|
---|
4658 | if ( !strHardDiskFolder.isEmpty()
|
---|
4659 | && RTPathStartsWith(m->strLocationFull.c_str(), strHardDiskFolder.c_str())
|
---|
4660 | )
|
---|
4661 | data.strLocation = m->strLocationFull.substr(strHardDiskFolder.length() + 1);
|
---|
4662 | else
|
---|
4663 | data.strLocation = m->strLocationFull;
|
---|
4664 | data.strFormat = m->strFormat;
|
---|
4665 |
|
---|
4666 | /* optional, only for diffs, default is false */
|
---|
4667 | if (m->pParent)
|
---|
4668 | data.fAutoReset = m->autoReset;
|
---|
4669 | else
|
---|
4670 | data.fAutoReset = false;
|
---|
4671 |
|
---|
4672 | /* optional */
|
---|
4673 | data.strDescription = m->strDescription;
|
---|
4674 |
|
---|
4675 | /* optional properties */
|
---|
4676 | data.properties.clear();
|
---|
4677 |
|
---|
4678 | /* handle iSCSI initiator secrets transparently */
|
---|
4679 | bool fHaveInitiatorSecretEncrypted = false;
|
---|
4680 | Utf8Str strCiphertext;
|
---|
4681 | settings::StringsMap::const_iterator itPln = m->mapProperties.find("InitiatorSecret");
|
---|
4682 | if ( itPln != m->mapProperties.end()
|
---|
4683 | && !itPln->second.isEmpty())
|
---|
4684 | {
|
---|
4685 | /* Encrypt the plain secret. If that does not work (i.e. no or wrong settings key
|
---|
4686 | * specified), just use the encrypted secret (if there is any). */
|
---|
4687 | int rc = m->pVirtualBox->i_encryptSetting(itPln->second, &strCiphertext);
|
---|
4688 | if (RT_SUCCESS(rc))
|
---|
4689 | fHaveInitiatorSecretEncrypted = true;
|
---|
4690 | }
|
---|
4691 | for (settings::StringsMap::const_iterator it = m->mapProperties.begin();
|
---|
4692 | it != m->mapProperties.end();
|
---|
4693 | ++it)
|
---|
4694 | {
|
---|
4695 | /* only save properties that have non-default values */
|
---|
4696 | if (!it->second.isEmpty())
|
---|
4697 | {
|
---|
4698 | const Utf8Str &name = it->first;
|
---|
4699 | const Utf8Str &value = it->second;
|
---|
4700 | /* do NOT store the plain InitiatorSecret */
|
---|
4701 | if ( !fHaveInitiatorSecretEncrypted
|
---|
4702 | || !name.equals("InitiatorSecret"))
|
---|
4703 | data.properties[name] = value;
|
---|
4704 | }
|
---|
4705 | }
|
---|
4706 | if (fHaveInitiatorSecretEncrypted)
|
---|
4707 | data.properties["InitiatorSecretEncrypted"] = strCiphertext;
|
---|
4708 |
|
---|
4709 | /* only for base media */
|
---|
4710 | if (m->pParent.isNull())
|
---|
4711 | data.hdType = m->type;
|
---|
4712 | }
|
---|
4713 |
|
---|
4714 | /**
|
---|
4715 | * Saves medium data by putting it into the provided data structure.
|
---|
4716 | * Recurses over all children to save their settings, too.
|
---|
4717 | *
|
---|
4718 | * @param data Settings struct to be updated.
|
---|
4719 | * @param strHardDiskFolder Folder for which paths should be relative.
|
---|
4720 | *
|
---|
4721 | * @note Locks this object, medium tree and children for reading.
|
---|
4722 | */
|
---|
4723 | HRESULT Medium::i_saveSettings(settings::Medium &data,
|
---|
4724 | const Utf8Str &strHardDiskFolder)
|
---|
4725 | {
|
---|
4726 | /* we access m->pParent */
|
---|
4727 | AutoReadLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4728 |
|
---|
4729 | AutoCaller autoCaller(this);
|
---|
4730 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4731 |
|
---|
4732 | i_saveSettingsOne(data, strHardDiskFolder);
|
---|
4733 |
|
---|
4734 | /* save all children */
|
---|
4735 | settings::MediaList &llSettingsChildren = data.llChildren;
|
---|
4736 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
4737 | it != i_getChildren().end();
|
---|
4738 | ++it)
|
---|
4739 | {
|
---|
4740 | // Use the element straight in the list to reduce both unnecessary
|
---|
4741 | // deep copying (when unwinding the recursion the entire medium
|
---|
4742 | // settings sub-tree is copied) and the stack footprint (the settings
|
---|
4743 | // need almost 1K, and there can be VMs with long image chains.
|
---|
4744 | llSettingsChildren.push_back(settings::Medium::Empty);
|
---|
4745 | HRESULT rc = (*it)->i_saveSettings(llSettingsChildren.back(), strHardDiskFolder);
|
---|
4746 | if (FAILED(rc))
|
---|
4747 | {
|
---|
4748 | llSettingsChildren.pop_back();
|
---|
4749 | return rc;
|
---|
4750 | }
|
---|
4751 | }
|
---|
4752 |
|
---|
4753 | return S_OK;
|
---|
4754 | }
|
---|
4755 |
|
---|
4756 | /**
|
---|
4757 | * Constructs a medium lock list for this medium. The lock is not taken.
|
---|
4758 | *
|
---|
4759 | * @note Caller MUST NOT hold the media tree or medium lock.
|
---|
4760 | *
|
---|
4761 | * @param fFailIfInaccessible If true, this fails with an error if a medium is inaccessible. If false,
|
---|
4762 | * inaccessible media are silently skipped and not locked (i.e. their state remains "Inaccessible");
|
---|
4763 | * this is necessary for a VM's removable media VM startup for which we do not want to fail.
|
---|
4764 | * @param pToLockWrite If not NULL, associate a write lock with this medium object.
|
---|
4765 | * @param fMediumLockWriteAll Whether to associate a write lock to all other media too.
|
---|
4766 | * @param pToBeParent Medium which will become the parent of this medium.
|
---|
4767 | * @param mediumLockList Where to store the resulting list.
|
---|
4768 | */
|
---|
4769 | HRESULT Medium::i_createMediumLockList(bool fFailIfInaccessible,
|
---|
4770 | Medium *pToLockWrite,
|
---|
4771 | bool fMediumLockWriteAll,
|
---|
4772 | Medium *pToBeParent,
|
---|
4773 | MediumLockList &mediumLockList)
|
---|
4774 | {
|
---|
4775 | /** @todo r=klaus this needs to be reworked, as the code below uses
|
---|
4776 | * i_getParent without holding the tree lock, and changing this is
|
---|
4777 | * a significant amount of effort. */
|
---|
4778 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
4779 | Assert(!isWriteLockOnCurrentThread());
|
---|
4780 |
|
---|
4781 | AutoCaller autoCaller(this);
|
---|
4782 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4783 |
|
---|
4784 | HRESULT rc = S_OK;
|
---|
4785 |
|
---|
4786 | /* paranoid sanity checking if the medium has a to-be parent medium */
|
---|
4787 | if (pToBeParent)
|
---|
4788 | {
|
---|
4789 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4790 | ComAssertRet(i_getParent().isNull(), E_FAIL);
|
---|
4791 | ComAssertRet(i_getChildren().size() == 0, E_FAIL);
|
---|
4792 | }
|
---|
4793 |
|
---|
4794 | ErrorInfoKeeper eik;
|
---|
4795 | MultiResult mrc(S_OK);
|
---|
4796 |
|
---|
4797 | ComObjPtr<Medium> pMedium = this;
|
---|
4798 | while (!pMedium.isNull())
|
---|
4799 | {
|
---|
4800 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
4801 |
|
---|
4802 | /* Accessibility check must be first, otherwise locking interferes
|
---|
4803 | * with getting the medium state. Lock lists are not created for
|
---|
4804 | * fun, and thus getting the medium status is no luxury. */
|
---|
4805 | MediumState_T mediumState = pMedium->i_getState();
|
---|
4806 | if (mediumState == MediumState_Inaccessible)
|
---|
4807 | {
|
---|
4808 | alock.release();
|
---|
4809 | rc = pMedium->i_queryInfo(false /* fSetImageId */, false /* fSetParentId */,
|
---|
4810 | autoCaller);
|
---|
4811 | alock.acquire();
|
---|
4812 | if (FAILED(rc)) return rc;
|
---|
4813 |
|
---|
4814 | mediumState = pMedium->i_getState();
|
---|
4815 | if (mediumState == MediumState_Inaccessible)
|
---|
4816 | {
|
---|
4817 | // ignore inaccessible ISO media and silently return S_OK,
|
---|
4818 | // otherwise VM startup (esp. restore) may fail without good reason
|
---|
4819 | if (!fFailIfInaccessible)
|
---|
4820 | return S_OK;
|
---|
4821 |
|
---|
4822 | // otherwise report an error
|
---|
4823 | Bstr error;
|
---|
4824 | rc = pMedium->COMGETTER(LastAccessError)(error.asOutParam());
|
---|
4825 | if (FAILED(rc)) return rc;
|
---|
4826 |
|
---|
4827 | /* collect multiple errors */
|
---|
4828 | eik.restore();
|
---|
4829 | Assert(!error.isEmpty());
|
---|
4830 | mrc = setError(E_FAIL,
|
---|
4831 | "%ls",
|
---|
4832 | error.raw());
|
---|
4833 | // error message will be something like
|
---|
4834 | // "Could not open the medium ... VD: error VERR_FILE_NOT_FOUND opening image file ... (VERR_FILE_NOT_FOUND).
|
---|
4835 | eik.fetch();
|
---|
4836 | }
|
---|
4837 | }
|
---|
4838 |
|
---|
4839 | if (pMedium == pToLockWrite)
|
---|
4840 | mediumLockList.Prepend(pMedium, true);
|
---|
4841 | else
|
---|
4842 | mediumLockList.Prepend(pMedium, fMediumLockWriteAll);
|
---|
4843 |
|
---|
4844 | pMedium = pMedium->i_getParent();
|
---|
4845 | if (pMedium.isNull() && pToBeParent)
|
---|
4846 | {
|
---|
4847 | pMedium = pToBeParent;
|
---|
4848 | pToBeParent = NULL;
|
---|
4849 | }
|
---|
4850 | }
|
---|
4851 |
|
---|
4852 | return mrc;
|
---|
4853 | }
|
---|
4854 |
|
---|
4855 | /**
|
---|
4856 | * Creates a new differencing storage unit using the format of the given target
|
---|
4857 | * medium and the location. Note that @c aTarget must be NotCreated.
|
---|
4858 | *
|
---|
4859 | * The @a aMediumLockList parameter contains the associated medium lock list,
|
---|
4860 | * which must be in locked state. If @a aWait is @c true then the caller is
|
---|
4861 | * responsible for unlocking.
|
---|
4862 | *
|
---|
4863 | * If @a aProgress is not NULL but the object it points to is @c null then a
|
---|
4864 | * new progress object will be created and assigned to @a *aProgress on
|
---|
4865 | * success, otherwise the existing progress object is used. If @a aProgress is
|
---|
4866 | * NULL, then no progress object is created/used at all.
|
---|
4867 | *
|
---|
4868 | * When @a aWait is @c false, this method will create a thread to perform the
|
---|
4869 | * create operation asynchronously and will return immediately. Otherwise, it
|
---|
4870 | * will perform the operation on the calling thread and will not return to the
|
---|
4871 | * caller until the operation is completed. Note that @a aProgress cannot be
|
---|
4872 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
4873 | *
|
---|
4874 | * @param aTarget Target medium.
|
---|
4875 | * @param aVariant Precise medium variant to create.
|
---|
4876 | * @param aMediumLockList List of media which should be locked.
|
---|
4877 | * @param aProgress Where to find/store a Progress object to track
|
---|
4878 | * operation completion.
|
---|
4879 | * @param aWait @c true if this method should block instead of
|
---|
4880 | * creating an asynchronous thread.
|
---|
4881 | *
|
---|
4882 | * @note Locks this object and @a aTarget for writing.
|
---|
4883 | */
|
---|
4884 | HRESULT Medium::i_createDiffStorage(ComObjPtr<Medium> &aTarget,
|
---|
4885 | MediumVariant_T aVariant,
|
---|
4886 | MediumLockList *aMediumLockList,
|
---|
4887 | ComObjPtr<Progress> *aProgress,
|
---|
4888 | bool aWait)
|
---|
4889 | {
|
---|
4890 | AssertReturn(!aTarget.isNull(), E_FAIL);
|
---|
4891 | AssertReturn(aMediumLockList, E_FAIL);
|
---|
4892 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
4893 |
|
---|
4894 | AutoCaller autoCaller(this);
|
---|
4895 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4896 |
|
---|
4897 | AutoCaller targetCaller(aTarget);
|
---|
4898 | if (FAILED(targetCaller.rc())) return targetCaller.rc();
|
---|
4899 |
|
---|
4900 | HRESULT rc = S_OK;
|
---|
4901 | ComObjPtr<Progress> pProgress;
|
---|
4902 | Medium::Task *pTask = NULL;
|
---|
4903 |
|
---|
4904 | try
|
---|
4905 | {
|
---|
4906 | AutoMultiWriteLock2 alock(this, aTarget COMMA_LOCKVAL_SRC_POS);
|
---|
4907 |
|
---|
4908 | ComAssertThrow( m->type != MediumType_Writethrough
|
---|
4909 | && m->type != MediumType_Shareable
|
---|
4910 | && m->type != MediumType_Readonly, E_FAIL);
|
---|
4911 | ComAssertThrow(m->state == MediumState_LockedRead, E_FAIL);
|
---|
4912 |
|
---|
4913 | if (aTarget->m->state != MediumState_NotCreated)
|
---|
4914 | throw aTarget->i_setStateError();
|
---|
4915 |
|
---|
4916 | /* Check that the medium is not attached to the current state of
|
---|
4917 | * any VM referring to it. */
|
---|
4918 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
4919 | it != m->backRefs.end();
|
---|
4920 | ++it)
|
---|
4921 | {
|
---|
4922 | if (it->fInCurState)
|
---|
4923 | {
|
---|
4924 | /* Note: when a VM snapshot is being taken, all normal media
|
---|
4925 | * attached to the VM in the current state will be, as an
|
---|
4926 | * exception, also associated with the snapshot which is about
|
---|
4927 | * to create (see SnapshotMachine::init()) before deassociating
|
---|
4928 | * them from the current state (which takes place only on
|
---|
4929 | * success in Machine::fixupHardDisks()), so that the size of
|
---|
4930 | * snapshotIds will be 1 in this case. The extra condition is
|
---|
4931 | * used to filter out this legal situation. */
|
---|
4932 | if (it->llSnapshotIds.size() == 0)
|
---|
4933 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4934 | 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"),
|
---|
4935 | m->strLocationFull.c_str(), it->machineId.raw());
|
---|
4936 |
|
---|
4937 | Assert(it->llSnapshotIds.size() == 1);
|
---|
4938 | }
|
---|
4939 | }
|
---|
4940 |
|
---|
4941 | if (aProgress != NULL)
|
---|
4942 | {
|
---|
4943 | /* use the existing progress object... */
|
---|
4944 | pProgress = *aProgress;
|
---|
4945 |
|
---|
4946 | /* ...but create a new one if it is null */
|
---|
4947 | if (pProgress.isNull())
|
---|
4948 | {
|
---|
4949 | pProgress.createObject();
|
---|
4950 | rc = pProgress->init(m->pVirtualBox,
|
---|
4951 | static_cast<IMedium*>(this),
|
---|
4952 | BstrFmt(tr("Creating differencing medium storage unit '%s'"),
|
---|
4953 | aTarget->m->strLocationFull.c_str()).raw(),
|
---|
4954 | TRUE /* aCancelable */);
|
---|
4955 | if (FAILED(rc))
|
---|
4956 | throw rc;
|
---|
4957 | }
|
---|
4958 | }
|
---|
4959 |
|
---|
4960 | /* setup task object to carry out the operation sync/async */
|
---|
4961 | pTask = new Medium::CreateDiffTask(this, pProgress, aTarget, aVariant,
|
---|
4962 | aMediumLockList,
|
---|
4963 | aWait /* fKeepMediumLockList */);
|
---|
4964 | rc = pTask->rc();
|
---|
4965 | AssertComRC(rc);
|
---|
4966 | if (FAILED(rc))
|
---|
4967 | throw rc;
|
---|
4968 |
|
---|
4969 | /* register a task (it will deregister itself when done) */
|
---|
4970 | ++m->numCreateDiffTasks;
|
---|
4971 | Assert(m->numCreateDiffTasks != 0); /* overflow? */
|
---|
4972 |
|
---|
4973 | aTarget->m->state = MediumState_Creating;
|
---|
4974 | }
|
---|
4975 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4976 |
|
---|
4977 | if (SUCCEEDED(rc))
|
---|
4978 | {
|
---|
4979 | if (aWait)
|
---|
4980 | {
|
---|
4981 | rc = pTask->runNow();
|
---|
4982 |
|
---|
4983 | delete pTask;
|
---|
4984 | }
|
---|
4985 | else
|
---|
4986 | rc = pTask->createThread();
|
---|
4987 |
|
---|
4988 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
4989 | *aProgress = pProgress;
|
---|
4990 | }
|
---|
4991 | else if (pTask != NULL)
|
---|
4992 | delete pTask;
|
---|
4993 |
|
---|
4994 | return rc;
|
---|
4995 | }
|
---|
4996 |
|
---|
4997 | /**
|
---|
4998 | * Returns a preferred format for differencing media.
|
---|
4999 | */
|
---|
5000 | Utf8Str Medium::i_getPreferredDiffFormat()
|
---|
5001 | {
|
---|
5002 | AutoCaller autoCaller(this);
|
---|
5003 | AssertComRCReturn(autoCaller.rc(), Utf8Str::Empty);
|
---|
5004 |
|
---|
5005 | /* check that our own format supports diffs */
|
---|
5006 | if (!(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_Differencing))
|
---|
5007 | {
|
---|
5008 | /* use the default format if not */
|
---|
5009 | Utf8Str tmp;
|
---|
5010 | m->pVirtualBox->i_getDefaultHardDiskFormat(tmp);
|
---|
5011 | return tmp;
|
---|
5012 | }
|
---|
5013 |
|
---|
5014 | /* m->strFormat is const, no need to lock */
|
---|
5015 | return m->strFormat;
|
---|
5016 | }
|
---|
5017 |
|
---|
5018 | /**
|
---|
5019 | * Returns a preferred variant for differencing media.
|
---|
5020 | */
|
---|
5021 | MediumVariant_T Medium::i_getPreferredDiffVariant()
|
---|
5022 | {
|
---|
5023 | AutoCaller autoCaller(this);
|
---|
5024 | AssertComRCReturn(autoCaller.rc(), MediumVariant_Standard);
|
---|
5025 |
|
---|
5026 | /* check that our own format supports diffs */
|
---|
5027 | if (!(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_Differencing))
|
---|
5028 | return MediumVariant_Standard;
|
---|
5029 |
|
---|
5030 | /* m->variant is const, no need to lock */
|
---|
5031 | ULONG mediumVariantFlags = (ULONG)m->variant;
|
---|
5032 | mediumVariantFlags &= ~(MediumVariant_Fixed | MediumVariant_VmdkStreamOptimized);
|
---|
5033 | mediumVariantFlags |= MediumVariant_Diff;
|
---|
5034 | return (MediumVariant_T)mediumVariantFlags;
|
---|
5035 | }
|
---|
5036 |
|
---|
5037 | /**
|
---|
5038 | * Implementation for the public Medium::Close() with the exception of calling
|
---|
5039 | * VirtualBox::saveRegistries(), in case someone wants to call this for several
|
---|
5040 | * media.
|
---|
5041 | *
|
---|
5042 | * After this returns with success, uninit() has been called on the medium, and
|
---|
5043 | * the object is no longer usable ("not ready" state).
|
---|
5044 | *
|
---|
5045 | * @param autoCaller AutoCaller instance which must have been created on the caller's
|
---|
5046 | * stack for this medium. This gets released hereupon
|
---|
5047 | * which the Medium instance gets uninitialized.
|
---|
5048 | * @return
|
---|
5049 | */
|
---|
5050 | HRESULT Medium::i_close(AutoCaller &autoCaller)
|
---|
5051 | {
|
---|
5052 | // must temporarily drop the caller, need the tree lock first
|
---|
5053 | autoCaller.release();
|
---|
5054 |
|
---|
5055 | // we're accessing parent/child and backrefs, so lock the tree first, then ourselves
|
---|
5056 | AutoMultiWriteLock2 multilock(&m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
5057 | this->lockHandle()
|
---|
5058 | COMMA_LOCKVAL_SRC_POS);
|
---|
5059 |
|
---|
5060 | autoCaller.add();
|
---|
5061 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
5062 |
|
---|
5063 | LogFlowFunc(("ENTER for %s\n", i_getLocationFull().c_str()));
|
---|
5064 |
|
---|
5065 | bool wasCreated = true;
|
---|
5066 |
|
---|
5067 | switch (m->state)
|
---|
5068 | {
|
---|
5069 | case MediumState_NotCreated:
|
---|
5070 | wasCreated = false;
|
---|
5071 | break;
|
---|
5072 | case MediumState_Created:
|
---|
5073 | case MediumState_Inaccessible:
|
---|
5074 | break;
|
---|
5075 | default:
|
---|
5076 | return i_setStateError();
|
---|
5077 | }
|
---|
5078 |
|
---|
5079 | if (m->backRefs.size() != 0)
|
---|
5080 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
5081 | tr("Medium '%s' cannot be closed because it is still attached to %d virtual machines"),
|
---|
5082 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
5083 |
|
---|
5084 | // perform extra media-dependent close checks
|
---|
5085 | HRESULT rc = i_canClose();
|
---|
5086 | if (FAILED(rc)) return rc;
|
---|
5087 |
|
---|
5088 | m->fClosing = true;
|
---|
5089 |
|
---|
5090 | if (wasCreated)
|
---|
5091 | {
|
---|
5092 | // remove from the list of known media before performing actual
|
---|
5093 | // uninitialization (to keep the media registry consistent on
|
---|
5094 | // failure to do so)
|
---|
5095 | rc = i_unregisterWithVirtualBox();
|
---|
5096 | if (FAILED(rc)) return rc;
|
---|
5097 |
|
---|
5098 | multilock.release();
|
---|
5099 | // Release the AutoCaller now, as otherwise uninit() will simply hang.
|
---|
5100 | // Needs to be done before mark the registries as modified and saving
|
---|
5101 | // the registry, as otherwise there may be a deadlock with someone else
|
---|
5102 | // closing this object while we're in i_saveModifiedRegistries(), which
|
---|
5103 | // needs the media tree lock, which the other thread holds until after
|
---|
5104 | // uninit() below.
|
---|
5105 | autoCaller.release();
|
---|
5106 | i_markRegistriesModified();
|
---|
5107 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
5108 | }
|
---|
5109 | else
|
---|
5110 | {
|
---|
5111 | multilock.release();
|
---|
5112 | // release the AutoCaller, as otherwise uninit() will simply hang
|
---|
5113 | autoCaller.release();
|
---|
5114 | }
|
---|
5115 |
|
---|
5116 | // Keep the locks held until after uninit, as otherwise the consistency
|
---|
5117 | // of the medium tree cannot be guaranteed.
|
---|
5118 | uninit();
|
---|
5119 |
|
---|
5120 | LogFlowFuncLeave();
|
---|
5121 |
|
---|
5122 | return rc;
|
---|
5123 | }
|
---|
5124 |
|
---|
5125 | /**
|
---|
5126 | * Deletes the medium storage unit.
|
---|
5127 | *
|
---|
5128 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
5129 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
5130 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
5131 | * progress object is created/used at all.
|
---|
5132 | *
|
---|
5133 | * When @a aWait is @c false, this method will create a thread to perform the
|
---|
5134 | * delete operation asynchronously and will return immediately. Otherwise, it
|
---|
5135 | * will perform the operation on the calling thread and will not return to the
|
---|
5136 | * caller until the operation is completed. Note that @a aProgress cannot be
|
---|
5137 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
5138 | *
|
---|
5139 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
5140 | * completion.
|
---|
5141 | * @param aWait @c true if this method should block instead of creating
|
---|
5142 | * an asynchronous thread.
|
---|
5143 | *
|
---|
5144 | * @note Locks mVirtualBox and this object for writing. Locks medium tree for
|
---|
5145 | * writing.
|
---|
5146 | */
|
---|
5147 | HRESULT Medium::i_deleteStorage(ComObjPtr<Progress> *aProgress,
|
---|
5148 | bool aWait)
|
---|
5149 | {
|
---|
5150 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
5151 |
|
---|
5152 | HRESULT rc = S_OK;
|
---|
5153 | ComObjPtr<Progress> pProgress;
|
---|
5154 | Medium::Task *pTask = NULL;
|
---|
5155 |
|
---|
5156 | try
|
---|
5157 | {
|
---|
5158 | /* we're accessing the media tree, and canClose() needs it too */
|
---|
5159 | AutoWriteLock treelock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5160 |
|
---|
5161 | AutoCaller autoCaller(this);
|
---|
5162 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5163 |
|
---|
5164 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5165 |
|
---|
5166 | LogFlowThisFunc(("aWait=%RTbool locationFull=%s\n", aWait, i_getLocationFull().c_str() ));
|
---|
5167 |
|
---|
5168 | if ( !(m->formatObj->i_getCapabilities() & ( MediumFormatCapabilities_CreateDynamic
|
---|
5169 | | MediumFormatCapabilities_CreateFixed)))
|
---|
5170 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
5171 | tr("Medium format '%s' does not support storage deletion"),
|
---|
5172 | m->strFormat.c_str());
|
---|
5173 |
|
---|
5174 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
5175 | /** @todo r=klaus would be great if this could be moved to the async
|
---|
5176 | * part of the operation as it can take quite a while */
|
---|
5177 | if (m->queryInfoRunning)
|
---|
5178 | {
|
---|
5179 | while (m->queryInfoRunning)
|
---|
5180 | {
|
---|
5181 | alock.release();
|
---|
5182 | autoCaller.release();
|
---|
5183 | treelock.release();
|
---|
5184 | /* Must not hold the media tree lock or the object lock, as
|
---|
5185 | * Medium::i_queryInfo needs this lock and thus we would run
|
---|
5186 | * into a deadlock here. */
|
---|
5187 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
5188 | Assert(!isWriteLockOnCurrentThread());
|
---|
5189 | {
|
---|
5190 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
5191 | }
|
---|
5192 | treelock.acquire();
|
---|
5193 | autoCaller.add();
|
---|
5194 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5195 | alock.acquire();
|
---|
5196 | }
|
---|
5197 | }
|
---|
5198 |
|
---|
5199 | /* Note that we are fine with Inaccessible state too: a) for symmetry
|
---|
5200 | * with create calls and b) because it doesn't really harm to try, if
|
---|
5201 | * it is really inaccessible, the delete operation will fail anyway.
|
---|
5202 | * Accepting Inaccessible state is especially important because all
|
---|
5203 | * registered media are initially Inaccessible upon VBoxSVC startup
|
---|
5204 | * until COMGETTER(RefreshState) is called. Accept Deleting state
|
---|
5205 | * because some callers need to put the medium in this state early
|
---|
5206 | * to prevent races. */
|
---|
5207 | switch (m->state)
|
---|
5208 | {
|
---|
5209 | case MediumState_Created:
|
---|
5210 | case MediumState_Deleting:
|
---|
5211 | case MediumState_Inaccessible:
|
---|
5212 | break;
|
---|
5213 | default:
|
---|
5214 | throw i_setStateError();
|
---|
5215 | }
|
---|
5216 |
|
---|
5217 | if (m->backRefs.size() != 0)
|
---|
5218 | {
|
---|
5219 | Utf8Str strMachines;
|
---|
5220 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
5221 | it != m->backRefs.end();
|
---|
5222 | ++it)
|
---|
5223 | {
|
---|
5224 | const BackRef &b = *it;
|
---|
5225 | if (strMachines.length())
|
---|
5226 | strMachines.append(", ");
|
---|
5227 | strMachines.append(b.machineId.toString().c_str());
|
---|
5228 | }
|
---|
5229 | #ifdef DEBUG
|
---|
5230 | i_dumpBackRefs();
|
---|
5231 | #endif
|
---|
5232 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5233 | tr("Cannot delete storage: medium '%s' is still attached to the following %d virtual machine(s): %s"),
|
---|
5234 | m->strLocationFull.c_str(),
|
---|
5235 | m->backRefs.size(),
|
---|
5236 | strMachines.c_str());
|
---|
5237 | }
|
---|
5238 |
|
---|
5239 | rc = i_canClose();
|
---|
5240 | if (FAILED(rc))
|
---|
5241 | throw rc;
|
---|
5242 |
|
---|
5243 | /* go to Deleting state, so that the medium is not actually locked */
|
---|
5244 | if (m->state != MediumState_Deleting)
|
---|
5245 | {
|
---|
5246 | rc = i_markForDeletion();
|
---|
5247 | if (FAILED(rc))
|
---|
5248 | throw rc;
|
---|
5249 | }
|
---|
5250 |
|
---|
5251 | /* Build the medium lock list. */
|
---|
5252 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
5253 | alock.release();
|
---|
5254 | autoCaller.release();
|
---|
5255 | treelock.release();
|
---|
5256 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5257 | this /* pToLockWrite */,
|
---|
5258 | false /* fMediumLockWriteAll */,
|
---|
5259 | NULL,
|
---|
5260 | *pMediumLockList);
|
---|
5261 | treelock.acquire();
|
---|
5262 | autoCaller.add();
|
---|
5263 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5264 | alock.acquire();
|
---|
5265 | if (FAILED(rc))
|
---|
5266 | {
|
---|
5267 | delete pMediumLockList;
|
---|
5268 | throw rc;
|
---|
5269 | }
|
---|
5270 |
|
---|
5271 | alock.release();
|
---|
5272 | autoCaller.release();
|
---|
5273 | treelock.release();
|
---|
5274 | rc = pMediumLockList->Lock();
|
---|
5275 | treelock.acquire();
|
---|
5276 | autoCaller.add();
|
---|
5277 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5278 | alock.acquire();
|
---|
5279 | if (FAILED(rc))
|
---|
5280 | {
|
---|
5281 | delete pMediumLockList;
|
---|
5282 | throw setError(rc,
|
---|
5283 | tr("Failed to lock media when deleting '%s'"),
|
---|
5284 | i_getLocationFull().c_str());
|
---|
5285 | }
|
---|
5286 |
|
---|
5287 | /* try to remove from the list of known media before performing
|
---|
5288 | * actual deletion (we favor the consistency of the media registry
|
---|
5289 | * which would have been broken if unregisterWithVirtualBox() failed
|
---|
5290 | * after we successfully deleted the storage) */
|
---|
5291 | rc = i_unregisterWithVirtualBox();
|
---|
5292 | if (FAILED(rc))
|
---|
5293 | throw rc;
|
---|
5294 | // no longer need lock
|
---|
5295 | alock.release();
|
---|
5296 | autoCaller.release();
|
---|
5297 | treelock.release();
|
---|
5298 | i_markRegistriesModified();
|
---|
5299 |
|
---|
5300 | if (aProgress != NULL)
|
---|
5301 | {
|
---|
5302 | /* use the existing progress object... */
|
---|
5303 | pProgress = *aProgress;
|
---|
5304 |
|
---|
5305 | /* ...but create a new one if it is null */
|
---|
5306 | if (pProgress.isNull())
|
---|
5307 | {
|
---|
5308 | pProgress.createObject();
|
---|
5309 | rc = pProgress->init(m->pVirtualBox,
|
---|
5310 | static_cast<IMedium*>(this),
|
---|
5311 | BstrFmt(tr("Deleting medium storage unit '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
5312 | FALSE /* aCancelable */);
|
---|
5313 | if (FAILED(rc))
|
---|
5314 | throw rc;
|
---|
5315 | }
|
---|
5316 | }
|
---|
5317 |
|
---|
5318 | /* setup task object to carry out the operation sync/async */
|
---|
5319 | pTask = new Medium::DeleteTask(this, pProgress, pMediumLockList);
|
---|
5320 | rc = pTask->rc();
|
---|
5321 | AssertComRC(rc);
|
---|
5322 | if (FAILED(rc))
|
---|
5323 | throw rc;
|
---|
5324 | }
|
---|
5325 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5326 |
|
---|
5327 | if (SUCCEEDED(rc))
|
---|
5328 | {
|
---|
5329 | if (aWait)
|
---|
5330 | {
|
---|
5331 | rc = pTask->runNow();
|
---|
5332 |
|
---|
5333 | delete pTask;
|
---|
5334 | }
|
---|
5335 | else
|
---|
5336 | rc = pTask->createThread();
|
---|
5337 |
|
---|
5338 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
5339 | *aProgress = pProgress;
|
---|
5340 |
|
---|
5341 | }
|
---|
5342 | else
|
---|
5343 | {
|
---|
5344 | if (pTask)
|
---|
5345 | delete pTask;
|
---|
5346 |
|
---|
5347 | /* Undo deleting state if necessary. */
|
---|
5348 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5349 | /* Make sure that any error signalled by unmarkForDeletion() is not
|
---|
5350 | * ending up in the error list (if the caller uses MultiResult). It
|
---|
5351 | * usually is spurious, as in most cases the medium hasn't been marked
|
---|
5352 | * for deletion when the error was thrown above. */
|
---|
5353 | ErrorInfoKeeper eik;
|
---|
5354 | i_unmarkForDeletion();
|
---|
5355 | }
|
---|
5356 |
|
---|
5357 | return rc;
|
---|
5358 | }
|
---|
5359 |
|
---|
5360 | /**
|
---|
5361 | * Mark a medium for deletion.
|
---|
5362 | *
|
---|
5363 | * @note Caller must hold the write lock on this medium!
|
---|
5364 | */
|
---|
5365 | HRESULT Medium::i_markForDeletion()
|
---|
5366 | {
|
---|
5367 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5368 | switch (m->state)
|
---|
5369 | {
|
---|
5370 | case MediumState_Created:
|
---|
5371 | case MediumState_Inaccessible:
|
---|
5372 | m->preLockState = m->state;
|
---|
5373 | m->state = MediumState_Deleting;
|
---|
5374 | return S_OK;
|
---|
5375 | default:
|
---|
5376 | return i_setStateError();
|
---|
5377 | }
|
---|
5378 | }
|
---|
5379 |
|
---|
5380 | /**
|
---|
5381 | * Removes the "mark for deletion".
|
---|
5382 | *
|
---|
5383 | * @note Caller must hold the write lock on this medium!
|
---|
5384 | */
|
---|
5385 | HRESULT Medium::i_unmarkForDeletion()
|
---|
5386 | {
|
---|
5387 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5388 | switch (m->state)
|
---|
5389 | {
|
---|
5390 | case MediumState_Deleting:
|
---|
5391 | m->state = m->preLockState;
|
---|
5392 | return S_OK;
|
---|
5393 | default:
|
---|
5394 | return i_setStateError();
|
---|
5395 | }
|
---|
5396 | }
|
---|
5397 |
|
---|
5398 | /**
|
---|
5399 | * Mark a medium for deletion which is in locked state.
|
---|
5400 | *
|
---|
5401 | * @note Caller must hold the write lock on this medium!
|
---|
5402 | */
|
---|
5403 | HRESULT Medium::i_markLockedForDeletion()
|
---|
5404 | {
|
---|
5405 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5406 | if ( ( m->state == MediumState_LockedRead
|
---|
5407 | || m->state == MediumState_LockedWrite)
|
---|
5408 | && m->preLockState == MediumState_Created)
|
---|
5409 | {
|
---|
5410 | m->preLockState = MediumState_Deleting;
|
---|
5411 | return S_OK;
|
---|
5412 | }
|
---|
5413 | else
|
---|
5414 | return i_setStateError();
|
---|
5415 | }
|
---|
5416 |
|
---|
5417 | /**
|
---|
5418 | * Removes the "mark for deletion" for a medium in locked state.
|
---|
5419 | *
|
---|
5420 | * @note Caller must hold the write lock on this medium!
|
---|
5421 | */
|
---|
5422 | HRESULT Medium::i_unmarkLockedForDeletion()
|
---|
5423 | {
|
---|
5424 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5425 | if ( ( m->state == MediumState_LockedRead
|
---|
5426 | || m->state == MediumState_LockedWrite)
|
---|
5427 | && m->preLockState == MediumState_Deleting)
|
---|
5428 | {
|
---|
5429 | m->preLockState = MediumState_Created;
|
---|
5430 | return S_OK;
|
---|
5431 | }
|
---|
5432 | else
|
---|
5433 | return i_setStateError();
|
---|
5434 | }
|
---|
5435 |
|
---|
5436 | /**
|
---|
5437 | * Queries the preferred merge direction from this to the other medium, i.e.
|
---|
5438 | * the one which requires the least amount of I/O and therefore time and
|
---|
5439 | * disk consumption.
|
---|
5440 | *
|
---|
5441 | * @returns Status code.
|
---|
5442 | * @retval E_FAIL in case determining the merge direction fails for some reason,
|
---|
5443 | * for example if getting the size of the media fails. There is no
|
---|
5444 | * error set though and the caller is free to continue to find out
|
---|
5445 | * what was going wrong later. Leaves fMergeForward unset.
|
---|
5446 | * @retval VBOX_E_INVALID_OBJECT_STATE if both media are not related to each other
|
---|
5447 | * An error is set.
|
---|
5448 | * @param pOther The other medium to merge with.
|
---|
5449 | * @param fMergeForward Resulting preferred merge direction (out).
|
---|
5450 | */
|
---|
5451 | HRESULT Medium::i_queryPreferredMergeDirection(const ComObjPtr<Medium> &pOther,
|
---|
5452 | bool &fMergeForward)
|
---|
5453 | {
|
---|
5454 | AssertReturn(pOther != NULL, E_FAIL);
|
---|
5455 | AssertReturn(pOther != this, E_FAIL);
|
---|
5456 |
|
---|
5457 | HRESULT rc = S_OK;
|
---|
5458 | bool fThisParent = false; /**<< Flag whether this medium is the parent of pOther. */
|
---|
5459 |
|
---|
5460 | try
|
---|
5461 | {
|
---|
5462 | // locking: we need the tree lock first because we access parent pointers
|
---|
5463 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5464 |
|
---|
5465 | AutoCaller autoCaller(this);
|
---|
5466 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5467 |
|
---|
5468 | AutoCaller otherCaller(pOther);
|
---|
5469 | AssertComRCThrowRC(otherCaller.rc());
|
---|
5470 |
|
---|
5471 | /* more sanity checking and figuring out the current merge direction */
|
---|
5472 | ComObjPtr<Medium> pMedium = i_getParent();
|
---|
5473 | while (!pMedium.isNull() && pMedium != pOther)
|
---|
5474 | pMedium = pMedium->i_getParent();
|
---|
5475 | if (pMedium == pOther)
|
---|
5476 | fThisParent = false;
|
---|
5477 | else
|
---|
5478 | {
|
---|
5479 | pMedium = pOther->i_getParent();
|
---|
5480 | while (!pMedium.isNull() && pMedium != this)
|
---|
5481 | pMedium = pMedium->i_getParent();
|
---|
5482 | if (pMedium == this)
|
---|
5483 | fThisParent = true;
|
---|
5484 | else
|
---|
5485 | {
|
---|
5486 | Utf8Str tgtLoc;
|
---|
5487 | {
|
---|
5488 | AutoReadLock alock(pOther COMMA_LOCKVAL_SRC_POS);
|
---|
5489 | tgtLoc = pOther->i_getLocationFull();
|
---|
5490 | }
|
---|
5491 |
|
---|
5492 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5493 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5494 | tr("Media '%s' and '%s' are unrelated"),
|
---|
5495 | m->strLocationFull.c_str(), tgtLoc.c_str());
|
---|
5496 | }
|
---|
5497 | }
|
---|
5498 |
|
---|
5499 | /*
|
---|
5500 | * Figure out the preferred merge direction. The current way is to
|
---|
5501 | * get the current sizes of file based images and select the merge
|
---|
5502 | * direction depending on the size.
|
---|
5503 | *
|
---|
5504 | * Can't use the VD API to get current size here as the media might
|
---|
5505 | * be write locked by a running VM. Resort to RTFileQuerySize().
|
---|
5506 | */
|
---|
5507 | int vrc = VINF_SUCCESS;
|
---|
5508 | uint64_t cbMediumThis = 0;
|
---|
5509 | uint64_t cbMediumOther = 0;
|
---|
5510 |
|
---|
5511 | if (i_isMediumFormatFile() && pOther->i_isMediumFormatFile())
|
---|
5512 | {
|
---|
5513 | vrc = RTFileQuerySize(this->i_getLocationFull().c_str(), &cbMediumThis);
|
---|
5514 | if (RT_SUCCESS(vrc))
|
---|
5515 | {
|
---|
5516 | vrc = RTFileQuerySize(pOther->i_getLocationFull().c_str(),
|
---|
5517 | &cbMediumOther);
|
---|
5518 | }
|
---|
5519 |
|
---|
5520 | if (RT_FAILURE(vrc))
|
---|
5521 | rc = E_FAIL;
|
---|
5522 | else
|
---|
5523 | {
|
---|
5524 | /*
|
---|
5525 | * Check which merge direction might be more optimal.
|
---|
5526 | * This method is not bullet proof of course as there might
|
---|
5527 | * be overlapping blocks in the images so the file size is
|
---|
5528 | * not the best indicator but it is good enough for our purpose
|
---|
5529 | * and everything else is too complicated, especially when the
|
---|
5530 | * media are used by a running VM.
|
---|
5531 | */
|
---|
5532 | bool fMergeIntoThis = cbMediumThis > cbMediumOther;
|
---|
5533 | fMergeForward = fMergeIntoThis != fThisParent;
|
---|
5534 | }
|
---|
5535 | }
|
---|
5536 | }
|
---|
5537 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5538 |
|
---|
5539 | return rc;
|
---|
5540 | }
|
---|
5541 |
|
---|
5542 | /**
|
---|
5543 | * Prepares this (source) medium, target medium and all intermediate media
|
---|
5544 | * for the merge operation.
|
---|
5545 | *
|
---|
5546 | * This method is to be called prior to calling the #mergeTo() to perform
|
---|
5547 | * necessary consistency checks and place involved media to appropriate
|
---|
5548 | * states. If #mergeTo() is not called or fails, the state modifications
|
---|
5549 | * performed by this method must be undone by #i_cancelMergeTo().
|
---|
5550 | *
|
---|
5551 | * See #mergeTo() for more information about merging.
|
---|
5552 | *
|
---|
5553 | * @param pTarget Target medium.
|
---|
5554 | * @param aMachineId Allowed machine attachment. NULL means do not check.
|
---|
5555 | * @param aSnapshotId Allowed snapshot attachment. NULL or empty UUID means
|
---|
5556 | * do not check.
|
---|
5557 | * @param fLockMedia Flag whether to lock the medium lock list or not.
|
---|
5558 | * If set to false and the medium lock list locking fails
|
---|
5559 | * later you must call #i_cancelMergeTo().
|
---|
5560 | * @param fMergeForward Resulting merge direction (out).
|
---|
5561 | * @param pParentForTarget New parent for target medium after merge (out).
|
---|
5562 | * @param aChildrenToReparent Medium lock list containing all children of the
|
---|
5563 | * source which will have to be reparented to the target
|
---|
5564 | * after merge (out).
|
---|
5565 | * @param aMediumLockList Medium locking information (out).
|
---|
5566 | *
|
---|
5567 | * @note Locks medium tree for reading. Locks this object, aTarget and all
|
---|
5568 | * intermediate media for writing.
|
---|
5569 | */
|
---|
5570 | HRESULT Medium::i_prepareMergeTo(const ComObjPtr<Medium> &pTarget,
|
---|
5571 | const Guid *aMachineId,
|
---|
5572 | const Guid *aSnapshotId,
|
---|
5573 | bool fLockMedia,
|
---|
5574 | bool &fMergeForward,
|
---|
5575 | ComObjPtr<Medium> &pParentForTarget,
|
---|
5576 | MediumLockList * &aChildrenToReparent,
|
---|
5577 | MediumLockList * &aMediumLockList)
|
---|
5578 | {
|
---|
5579 | AssertReturn(pTarget != NULL, E_FAIL);
|
---|
5580 | AssertReturn(pTarget != this, E_FAIL);
|
---|
5581 |
|
---|
5582 | HRESULT rc = S_OK;
|
---|
5583 | fMergeForward = false;
|
---|
5584 | pParentForTarget.setNull();
|
---|
5585 | Assert(aChildrenToReparent == NULL);
|
---|
5586 | aChildrenToReparent = NULL;
|
---|
5587 | Assert(aMediumLockList == NULL);
|
---|
5588 | aMediumLockList = NULL;
|
---|
5589 |
|
---|
5590 | try
|
---|
5591 | {
|
---|
5592 | // locking: we need the tree lock first because we access parent pointers
|
---|
5593 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5594 |
|
---|
5595 | AutoCaller autoCaller(this);
|
---|
5596 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5597 |
|
---|
5598 | AutoCaller targetCaller(pTarget);
|
---|
5599 | AssertComRCThrowRC(targetCaller.rc());
|
---|
5600 |
|
---|
5601 | /* more sanity checking and figuring out the merge direction */
|
---|
5602 | ComObjPtr<Medium> pMedium = i_getParent();
|
---|
5603 | while (!pMedium.isNull() && pMedium != pTarget)
|
---|
5604 | pMedium = pMedium->i_getParent();
|
---|
5605 | if (pMedium == pTarget)
|
---|
5606 | fMergeForward = false;
|
---|
5607 | else
|
---|
5608 | {
|
---|
5609 | pMedium = pTarget->i_getParent();
|
---|
5610 | while (!pMedium.isNull() && pMedium != this)
|
---|
5611 | pMedium = pMedium->i_getParent();
|
---|
5612 | if (pMedium == this)
|
---|
5613 | fMergeForward = true;
|
---|
5614 | else
|
---|
5615 | {
|
---|
5616 | Utf8Str tgtLoc;
|
---|
5617 | {
|
---|
5618 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5619 | tgtLoc = pTarget->i_getLocationFull();
|
---|
5620 | }
|
---|
5621 |
|
---|
5622 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5623 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5624 | tr("Media '%s' and '%s' are unrelated"),
|
---|
5625 | m->strLocationFull.c_str(), tgtLoc.c_str());
|
---|
5626 | }
|
---|
5627 | }
|
---|
5628 |
|
---|
5629 | /* Build the lock list. */
|
---|
5630 | aMediumLockList = new MediumLockList();
|
---|
5631 | targetCaller.release();
|
---|
5632 | autoCaller.release();
|
---|
5633 | treeLock.release();
|
---|
5634 | if (fMergeForward)
|
---|
5635 | rc = pTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5636 | pTarget /* pToLockWrite */,
|
---|
5637 | false /* fMediumLockWriteAll */,
|
---|
5638 | NULL,
|
---|
5639 | *aMediumLockList);
|
---|
5640 | else
|
---|
5641 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5642 | pTarget /* pToLockWrite */,
|
---|
5643 | false /* fMediumLockWriteAll */,
|
---|
5644 | NULL,
|
---|
5645 | *aMediumLockList);
|
---|
5646 | treeLock.acquire();
|
---|
5647 | autoCaller.add();
|
---|
5648 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5649 | targetCaller.add();
|
---|
5650 | AssertComRCThrowRC(targetCaller.rc());
|
---|
5651 | if (FAILED(rc))
|
---|
5652 | throw rc;
|
---|
5653 |
|
---|
5654 | /* Sanity checking, must be after lock list creation as it depends on
|
---|
5655 | * valid medium states. The medium objects must be accessible. Only
|
---|
5656 | * do this if immediate locking is requested, otherwise it fails when
|
---|
5657 | * we construct a medium lock list for an already running VM. Snapshot
|
---|
5658 | * deletion uses this to simplify its life. */
|
---|
5659 | if (fLockMedia)
|
---|
5660 | {
|
---|
5661 | {
|
---|
5662 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5663 | if (m->state != MediumState_Created)
|
---|
5664 | throw i_setStateError();
|
---|
5665 | }
|
---|
5666 | {
|
---|
5667 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5668 | if (pTarget->m->state != MediumState_Created)
|
---|
5669 | throw pTarget->i_setStateError();
|
---|
5670 | }
|
---|
5671 | }
|
---|
5672 |
|
---|
5673 | /* check medium attachment and other sanity conditions */
|
---|
5674 | if (fMergeForward)
|
---|
5675 | {
|
---|
5676 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5677 | if (i_getChildren().size() > 1)
|
---|
5678 | {
|
---|
5679 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5680 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
5681 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
5682 | }
|
---|
5683 | /* One backreference is only allowed if the machine ID is not empty
|
---|
5684 | * and it matches the machine the medium is attached to (including
|
---|
5685 | * the snapshot ID if not empty). */
|
---|
5686 | if ( m->backRefs.size() != 0
|
---|
5687 | && ( !aMachineId
|
---|
5688 | || m->backRefs.size() != 1
|
---|
5689 | || aMachineId->isZero()
|
---|
5690 | || *i_getFirstMachineBackrefId() != *aMachineId
|
---|
5691 | || ( (!aSnapshotId || !aSnapshotId->isZero())
|
---|
5692 | && *i_getFirstMachineBackrefSnapshotId() != *aSnapshotId)))
|
---|
5693 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5694 | tr("Medium '%s' is attached to %d virtual machines"),
|
---|
5695 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
5696 | if (m->type == MediumType_Immutable)
|
---|
5697 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5698 | tr("Medium '%s' is immutable"),
|
---|
5699 | m->strLocationFull.c_str());
|
---|
5700 | if (m->type == MediumType_MultiAttach)
|
---|
5701 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5702 | tr("Medium '%s' is multi-attach"),
|
---|
5703 | m->strLocationFull.c_str());
|
---|
5704 | }
|
---|
5705 | else
|
---|
5706 | {
|
---|
5707 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5708 | if (pTarget->i_getChildren().size() > 1)
|
---|
5709 | {
|
---|
5710 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5711 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
5712 | pTarget->m->strLocationFull.c_str(),
|
---|
5713 | pTarget->i_getChildren().size());
|
---|
5714 | }
|
---|
5715 | if (pTarget->m->type == MediumType_Immutable)
|
---|
5716 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5717 | tr("Medium '%s' is immutable"),
|
---|
5718 | pTarget->m->strLocationFull.c_str());
|
---|
5719 | if (pTarget->m->type == MediumType_MultiAttach)
|
---|
5720 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5721 | tr("Medium '%s' is multi-attach"),
|
---|
5722 | pTarget->m->strLocationFull.c_str());
|
---|
5723 | }
|
---|
5724 | ComObjPtr<Medium> pLast(fMergeForward ? (Medium *)pTarget : this);
|
---|
5725 | ComObjPtr<Medium> pLastIntermediate = pLast->i_getParent();
|
---|
5726 | for (pLast = pLastIntermediate;
|
---|
5727 | !pLast.isNull() && pLast != pTarget && pLast != this;
|
---|
5728 | pLast = pLast->i_getParent())
|
---|
5729 | {
|
---|
5730 | AutoReadLock alock(pLast COMMA_LOCKVAL_SRC_POS);
|
---|
5731 | if (pLast->i_getChildren().size() > 1)
|
---|
5732 | {
|
---|
5733 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5734 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
5735 | pLast->m->strLocationFull.c_str(),
|
---|
5736 | pLast->i_getChildren().size());
|
---|
5737 | }
|
---|
5738 | if (pLast->m->backRefs.size() != 0)
|
---|
5739 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5740 | tr("Medium '%s' is attached to %d virtual machines"),
|
---|
5741 | pLast->m->strLocationFull.c_str(),
|
---|
5742 | pLast->m->backRefs.size());
|
---|
5743 |
|
---|
5744 | }
|
---|
5745 |
|
---|
5746 | /* Update medium states appropriately */
|
---|
5747 | {
|
---|
5748 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5749 |
|
---|
5750 | if (m->state == MediumState_Created)
|
---|
5751 | {
|
---|
5752 | rc = i_markForDeletion();
|
---|
5753 | if (FAILED(rc))
|
---|
5754 | throw rc;
|
---|
5755 | }
|
---|
5756 | else
|
---|
5757 | {
|
---|
5758 | if (fLockMedia)
|
---|
5759 | throw i_setStateError();
|
---|
5760 | else if ( m->state == MediumState_LockedWrite
|
---|
5761 | || m->state == MediumState_LockedRead)
|
---|
5762 | {
|
---|
5763 | /* Either mark it for deletion in locked state or allow
|
---|
5764 | * others to have done so. */
|
---|
5765 | if (m->preLockState == MediumState_Created)
|
---|
5766 | i_markLockedForDeletion();
|
---|
5767 | else if (m->preLockState != MediumState_Deleting)
|
---|
5768 | throw i_setStateError();
|
---|
5769 | }
|
---|
5770 | else
|
---|
5771 | throw i_setStateError();
|
---|
5772 | }
|
---|
5773 | }
|
---|
5774 |
|
---|
5775 | if (fMergeForward)
|
---|
5776 | {
|
---|
5777 | /* we will need parent to reparent target */
|
---|
5778 | pParentForTarget = i_getParent();
|
---|
5779 | }
|
---|
5780 | else
|
---|
5781 | {
|
---|
5782 | /* we will need to reparent children of the source */
|
---|
5783 | aChildrenToReparent = new MediumLockList();
|
---|
5784 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
5785 | it != i_getChildren().end();
|
---|
5786 | ++it)
|
---|
5787 | {
|
---|
5788 | pMedium = *it;
|
---|
5789 | aChildrenToReparent->Append(pMedium, true /* fLockWrite */);
|
---|
5790 | }
|
---|
5791 | if (fLockMedia && aChildrenToReparent)
|
---|
5792 | {
|
---|
5793 | targetCaller.release();
|
---|
5794 | autoCaller.release();
|
---|
5795 | treeLock.release();
|
---|
5796 | rc = aChildrenToReparent->Lock();
|
---|
5797 | treeLock.acquire();
|
---|
5798 | autoCaller.add();
|
---|
5799 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5800 | targetCaller.add();
|
---|
5801 | AssertComRCThrowRC(targetCaller.rc());
|
---|
5802 | if (FAILED(rc))
|
---|
5803 | throw rc;
|
---|
5804 | }
|
---|
5805 | }
|
---|
5806 | for (pLast = pLastIntermediate;
|
---|
5807 | !pLast.isNull() && pLast != pTarget && pLast != this;
|
---|
5808 | pLast = pLast->i_getParent())
|
---|
5809 | {
|
---|
5810 | AutoWriteLock alock(pLast COMMA_LOCKVAL_SRC_POS);
|
---|
5811 | if (pLast->m->state == MediumState_Created)
|
---|
5812 | {
|
---|
5813 | rc = pLast->i_markForDeletion();
|
---|
5814 | if (FAILED(rc))
|
---|
5815 | throw rc;
|
---|
5816 | }
|
---|
5817 | else
|
---|
5818 | throw pLast->i_setStateError();
|
---|
5819 | }
|
---|
5820 |
|
---|
5821 | /* Tweak the lock list in the backward merge case, as the target
|
---|
5822 | * isn't marked to be locked for writing yet. */
|
---|
5823 | if (!fMergeForward)
|
---|
5824 | {
|
---|
5825 | MediumLockList::Base::iterator lockListBegin =
|
---|
5826 | aMediumLockList->GetBegin();
|
---|
5827 | MediumLockList::Base::iterator lockListEnd =
|
---|
5828 | aMediumLockList->GetEnd();
|
---|
5829 | ++lockListEnd;
|
---|
5830 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
5831 | it != lockListEnd;
|
---|
5832 | ++it)
|
---|
5833 | {
|
---|
5834 | MediumLock &mediumLock = *it;
|
---|
5835 | if (mediumLock.GetMedium() == pTarget)
|
---|
5836 | {
|
---|
5837 | HRESULT rc2 = mediumLock.UpdateLock(true);
|
---|
5838 | AssertComRC(rc2);
|
---|
5839 | break;
|
---|
5840 | }
|
---|
5841 | }
|
---|
5842 | }
|
---|
5843 |
|
---|
5844 | if (fLockMedia)
|
---|
5845 | {
|
---|
5846 | targetCaller.release();
|
---|
5847 | autoCaller.release();
|
---|
5848 | treeLock.release();
|
---|
5849 | rc = aMediumLockList->Lock();
|
---|
5850 | treeLock.acquire();
|
---|
5851 | autoCaller.add();
|
---|
5852 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5853 | targetCaller.add();
|
---|
5854 | AssertComRCThrowRC(targetCaller.rc());
|
---|
5855 | if (FAILED(rc))
|
---|
5856 | {
|
---|
5857 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5858 | throw setError(rc,
|
---|
5859 | tr("Failed to lock media when merging to '%s'"),
|
---|
5860 | pTarget->i_getLocationFull().c_str());
|
---|
5861 | }
|
---|
5862 | }
|
---|
5863 | }
|
---|
5864 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5865 |
|
---|
5866 | if (FAILED(rc))
|
---|
5867 | {
|
---|
5868 | if (aMediumLockList)
|
---|
5869 | {
|
---|
5870 | delete aMediumLockList;
|
---|
5871 | aMediumLockList = NULL;
|
---|
5872 | }
|
---|
5873 | if (aChildrenToReparent)
|
---|
5874 | {
|
---|
5875 | delete aChildrenToReparent;
|
---|
5876 | aChildrenToReparent = NULL;
|
---|
5877 | }
|
---|
5878 | }
|
---|
5879 |
|
---|
5880 | return rc;
|
---|
5881 | }
|
---|
5882 |
|
---|
5883 | /**
|
---|
5884 | * Merges this medium to the specified medium which must be either its
|
---|
5885 | * direct ancestor or descendant.
|
---|
5886 | *
|
---|
5887 | * Given this medium is SOURCE and the specified medium is TARGET, we will
|
---|
5888 | * get two variants of the merge operation:
|
---|
5889 | *
|
---|
5890 | * forward merge
|
---|
5891 | * ------------------------->
|
---|
5892 | * [Extra] <- SOURCE <- Intermediate <- TARGET
|
---|
5893 | * Any Del Del LockWr
|
---|
5894 | *
|
---|
5895 | *
|
---|
5896 | * backward merge
|
---|
5897 | * <-------------------------
|
---|
5898 | * TARGET <- Intermediate <- SOURCE <- [Extra]
|
---|
5899 | * LockWr Del Del LockWr
|
---|
5900 | *
|
---|
5901 | * Each diagram shows the involved media on the media chain where
|
---|
5902 | * SOURCE and TARGET belong. Under each medium there is a state value which
|
---|
5903 | * the medium must have at a time of the mergeTo() call.
|
---|
5904 | *
|
---|
5905 | * The media in the square braces may be absent (e.g. when the forward
|
---|
5906 | * operation takes place and SOURCE is the base medium, or when the backward
|
---|
5907 | * merge operation takes place and TARGET is the last child in the chain) but if
|
---|
5908 | * they present they are involved too as shown.
|
---|
5909 | *
|
---|
5910 | * Neither the source medium nor intermediate media may be attached to
|
---|
5911 | * any VM directly or in the snapshot, otherwise this method will assert.
|
---|
5912 | *
|
---|
5913 | * The #i_prepareMergeTo() method must be called prior to this method to place
|
---|
5914 | * all involved to necessary states and perform other consistency checks.
|
---|
5915 | *
|
---|
5916 | * If @a aWait is @c true then this method will perform the operation on the
|
---|
5917 | * calling thread and will not return to the caller until the operation is
|
---|
5918 | * completed. When this method succeeds, all intermediate medium objects in
|
---|
5919 | * the chain will be uninitialized, the state of the target medium (and all
|
---|
5920 | * involved extra media) will be restored. @a aMediumLockList will not be
|
---|
5921 | * deleted, whether the operation is successful or not. The caller has to do
|
---|
5922 | * this if appropriate. Note that this (source) medium is not uninitialized
|
---|
5923 | * because of possible AutoCaller instances held by the caller of this method
|
---|
5924 | * on the current thread. It's therefore the responsibility of the caller to
|
---|
5925 | * call Medium::uninit() after releasing all callers.
|
---|
5926 | *
|
---|
5927 | * If @a aWait is @c false then this method will create a thread to perform the
|
---|
5928 | * operation asynchronously and will return immediately. If the operation
|
---|
5929 | * succeeds, the thread will uninitialize the source medium object and all
|
---|
5930 | * intermediate medium objects in the chain, reset the state of the target
|
---|
5931 | * medium (and all involved extra media) and delete @a aMediumLockList.
|
---|
5932 | * If the operation fails, the thread will only reset the states of all
|
---|
5933 | * involved media and delete @a aMediumLockList.
|
---|
5934 | *
|
---|
5935 | * When this method fails (regardless of the @a aWait mode), it is a caller's
|
---|
5936 | * responsibility to undo state changes and delete @a aMediumLockList using
|
---|
5937 | * #i_cancelMergeTo().
|
---|
5938 | *
|
---|
5939 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
5940 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
5941 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
5942 | * progress object is created/used at all. Note that @a aProgress cannot be
|
---|
5943 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
5944 | *
|
---|
5945 | * @param pTarget Target medium.
|
---|
5946 | * @param fMergeForward Merge direction.
|
---|
5947 | * @param pParentForTarget New parent for target medium after merge.
|
---|
5948 | * @param aChildrenToReparent List of children of the source which will have
|
---|
5949 | * to be reparented to the target after merge.
|
---|
5950 | * @param aMediumLockList Medium locking information.
|
---|
5951 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
5952 | * completion.
|
---|
5953 | * @param aWait @c true if this method should block instead of creating
|
---|
5954 | * an asynchronous thread.
|
---|
5955 | *
|
---|
5956 | * @note Locks the tree lock for writing. Locks the media from the chain
|
---|
5957 | * for writing.
|
---|
5958 | */
|
---|
5959 | HRESULT Medium::i_mergeTo(const ComObjPtr<Medium> &pTarget,
|
---|
5960 | bool fMergeForward,
|
---|
5961 | const ComObjPtr<Medium> &pParentForTarget,
|
---|
5962 | MediumLockList *aChildrenToReparent,
|
---|
5963 | MediumLockList *aMediumLockList,
|
---|
5964 | ComObjPtr<Progress> *aProgress,
|
---|
5965 | bool aWait)
|
---|
5966 | {
|
---|
5967 | AssertReturn(pTarget != NULL, E_FAIL);
|
---|
5968 | AssertReturn(pTarget != this, E_FAIL);
|
---|
5969 | AssertReturn(aMediumLockList != NULL, E_FAIL);
|
---|
5970 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
5971 |
|
---|
5972 | AutoCaller autoCaller(this);
|
---|
5973 | AssertComRCReturnRC(autoCaller.rc());
|
---|
5974 |
|
---|
5975 | AutoCaller targetCaller(pTarget);
|
---|
5976 | AssertComRCReturnRC(targetCaller.rc());
|
---|
5977 |
|
---|
5978 | HRESULT rc = S_OK;
|
---|
5979 | ComObjPtr<Progress> pProgress;
|
---|
5980 | Medium::Task *pTask = NULL;
|
---|
5981 |
|
---|
5982 | try
|
---|
5983 | {
|
---|
5984 | if (aProgress != NULL)
|
---|
5985 | {
|
---|
5986 | /* use the existing progress object... */
|
---|
5987 | pProgress = *aProgress;
|
---|
5988 |
|
---|
5989 | /* ...but create a new one if it is null */
|
---|
5990 | if (pProgress.isNull())
|
---|
5991 | {
|
---|
5992 | Utf8Str tgtName;
|
---|
5993 | {
|
---|
5994 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5995 | tgtName = pTarget->i_getName();
|
---|
5996 | }
|
---|
5997 |
|
---|
5998 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5999 |
|
---|
6000 | pProgress.createObject();
|
---|
6001 | rc = pProgress->init(m->pVirtualBox,
|
---|
6002 | static_cast<IMedium*>(this),
|
---|
6003 | BstrFmt(tr("Merging medium '%s' to '%s'"),
|
---|
6004 | i_getName().c_str(),
|
---|
6005 | tgtName.c_str()).raw(),
|
---|
6006 | TRUE /* aCancelable */);
|
---|
6007 | if (FAILED(rc))
|
---|
6008 | throw rc;
|
---|
6009 | }
|
---|
6010 | }
|
---|
6011 |
|
---|
6012 | /* setup task object to carry out the operation sync/async */
|
---|
6013 | pTask = new Medium::MergeTask(this, pTarget, fMergeForward,
|
---|
6014 | pParentForTarget, aChildrenToReparent,
|
---|
6015 | pProgress, aMediumLockList,
|
---|
6016 | aWait /* fKeepMediumLockList */);
|
---|
6017 | rc = pTask->rc();
|
---|
6018 | AssertComRC(rc);
|
---|
6019 | if (FAILED(rc))
|
---|
6020 | throw rc;
|
---|
6021 | }
|
---|
6022 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6023 |
|
---|
6024 | if (SUCCEEDED(rc))
|
---|
6025 | {
|
---|
6026 | if (aWait)
|
---|
6027 | {
|
---|
6028 | rc = pTask->runNow();
|
---|
6029 |
|
---|
6030 | delete pTask;
|
---|
6031 | }
|
---|
6032 | else
|
---|
6033 | rc = pTask->createThread();
|
---|
6034 |
|
---|
6035 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
6036 | *aProgress = pProgress;
|
---|
6037 | }
|
---|
6038 | else if (pTask != NULL)
|
---|
6039 | delete pTask;
|
---|
6040 |
|
---|
6041 | return rc;
|
---|
6042 | }
|
---|
6043 |
|
---|
6044 | /**
|
---|
6045 | * Undoes what #i_prepareMergeTo() did. Must be called if #mergeTo() is not
|
---|
6046 | * called or fails. Frees memory occupied by @a aMediumLockList and unlocks
|
---|
6047 | * the medium objects in @a aChildrenToReparent.
|
---|
6048 | *
|
---|
6049 | * @param aChildrenToReparent List of children of the source which will have
|
---|
6050 | * to be reparented to the target after merge.
|
---|
6051 | * @param aMediumLockList Medium locking information.
|
---|
6052 | *
|
---|
6053 | * @note Locks the media from the chain for writing.
|
---|
6054 | */
|
---|
6055 | void Medium::i_cancelMergeTo(MediumLockList *aChildrenToReparent,
|
---|
6056 | MediumLockList *aMediumLockList)
|
---|
6057 | {
|
---|
6058 | AutoCaller autoCaller(this);
|
---|
6059 | AssertComRCReturnVoid(autoCaller.rc());
|
---|
6060 |
|
---|
6061 | AssertReturnVoid(aMediumLockList != NULL);
|
---|
6062 |
|
---|
6063 | /* Revert media marked for deletion to previous state. */
|
---|
6064 | HRESULT rc;
|
---|
6065 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
6066 | aMediumLockList->GetBegin();
|
---|
6067 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
6068 | aMediumLockList->GetEnd();
|
---|
6069 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
6070 | it != mediumListEnd;
|
---|
6071 | ++it)
|
---|
6072 | {
|
---|
6073 | const MediumLock &mediumLock = *it;
|
---|
6074 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
6075 | AutoWriteLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
6076 |
|
---|
6077 | if (pMedium->m->state == MediumState_Deleting)
|
---|
6078 | {
|
---|
6079 | rc = pMedium->i_unmarkForDeletion();
|
---|
6080 | AssertComRC(rc);
|
---|
6081 | }
|
---|
6082 | else if ( ( pMedium->m->state == MediumState_LockedWrite
|
---|
6083 | || pMedium->m->state == MediumState_LockedRead)
|
---|
6084 | && pMedium->m->preLockState == MediumState_Deleting)
|
---|
6085 | {
|
---|
6086 | rc = pMedium->i_unmarkLockedForDeletion();
|
---|
6087 | AssertComRC(rc);
|
---|
6088 | }
|
---|
6089 | }
|
---|
6090 |
|
---|
6091 | /* the destructor will do the work */
|
---|
6092 | delete aMediumLockList;
|
---|
6093 |
|
---|
6094 | /* unlock the children which had to be reparented, the destructor will do
|
---|
6095 | * the work */
|
---|
6096 | if (aChildrenToReparent)
|
---|
6097 | delete aChildrenToReparent;
|
---|
6098 | }
|
---|
6099 |
|
---|
6100 | /**
|
---|
6101 | * Fix the parent UUID of all children to point to this medium as their
|
---|
6102 | * parent.
|
---|
6103 | */
|
---|
6104 | HRESULT Medium::i_fixParentUuidOfChildren(MediumLockList *pChildrenToReparent)
|
---|
6105 | {
|
---|
6106 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
6107 | * to lock order violations, it probably causes lock order issues related
|
---|
6108 | * to the AutoCaller usage. Likewise the code using this method seems
|
---|
6109 | * problematic. */
|
---|
6110 | Assert(!isWriteLockOnCurrentThread());
|
---|
6111 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
6112 | MediumLockList mediumLockList;
|
---|
6113 | HRESULT rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
6114 | NULL /* pToLockWrite */,
|
---|
6115 | false /* fMediumLockWriteAll */,
|
---|
6116 | this,
|
---|
6117 | mediumLockList);
|
---|
6118 | AssertComRCReturnRC(rc);
|
---|
6119 |
|
---|
6120 | try
|
---|
6121 | {
|
---|
6122 | PVDISK hdd;
|
---|
6123 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
6124 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6125 |
|
---|
6126 | try
|
---|
6127 | {
|
---|
6128 | MediumLockList::Base::iterator lockListBegin =
|
---|
6129 | mediumLockList.GetBegin();
|
---|
6130 | MediumLockList::Base::iterator lockListEnd =
|
---|
6131 | mediumLockList.GetEnd();
|
---|
6132 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
6133 | it != lockListEnd;
|
---|
6134 | ++it)
|
---|
6135 | {
|
---|
6136 | MediumLock &mediumLock = *it;
|
---|
6137 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
6138 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
6139 |
|
---|
6140 | // open the medium
|
---|
6141 | vrc = VDOpen(hdd,
|
---|
6142 | pMedium->m->strFormat.c_str(),
|
---|
6143 | pMedium->m->strLocationFull.c_str(),
|
---|
6144 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
6145 | pMedium->m->vdImageIfaces);
|
---|
6146 | if (RT_FAILURE(vrc))
|
---|
6147 | throw vrc;
|
---|
6148 | }
|
---|
6149 |
|
---|
6150 | MediumLockList::Base::iterator childrenBegin = pChildrenToReparent->GetBegin();
|
---|
6151 | MediumLockList::Base::iterator childrenEnd = pChildrenToReparent->GetEnd();
|
---|
6152 | for (MediumLockList::Base::iterator it = childrenBegin;
|
---|
6153 | it != childrenEnd;
|
---|
6154 | ++it)
|
---|
6155 | {
|
---|
6156 | Medium *pMedium = it->GetMedium();
|
---|
6157 | /* VD_OPEN_FLAGS_INFO since UUID is wrong yet */
|
---|
6158 | vrc = VDOpen(hdd,
|
---|
6159 | pMedium->m->strFormat.c_str(),
|
---|
6160 | pMedium->m->strLocationFull.c_str(),
|
---|
6161 | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
6162 | pMedium->m->vdImageIfaces);
|
---|
6163 | if (RT_FAILURE(vrc))
|
---|
6164 | throw vrc;
|
---|
6165 |
|
---|
6166 | vrc = VDSetParentUuid(hdd, VD_LAST_IMAGE, m->id.raw());
|
---|
6167 | if (RT_FAILURE(vrc))
|
---|
6168 | throw vrc;
|
---|
6169 |
|
---|
6170 | vrc = VDClose(hdd, false /* fDelete */);
|
---|
6171 | if (RT_FAILURE(vrc))
|
---|
6172 | throw vrc;
|
---|
6173 | }
|
---|
6174 | }
|
---|
6175 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6176 | catch (int aVRC)
|
---|
6177 | {
|
---|
6178 | rc = setErrorBoth(E_FAIL, aVRC,
|
---|
6179 | tr("Could not update medium UUID references to parent '%s' (%s)"),
|
---|
6180 | m->strLocationFull.c_str(),
|
---|
6181 | i_vdError(aVRC).c_str());
|
---|
6182 | }
|
---|
6183 |
|
---|
6184 | VDDestroy(hdd);
|
---|
6185 | }
|
---|
6186 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6187 |
|
---|
6188 | return rc;
|
---|
6189 | }
|
---|
6190 |
|
---|
6191 | /**
|
---|
6192 | *
|
---|
6193 | * @note Similar code exists in i_taskExportHandler.
|
---|
6194 | */
|
---|
6195 | HRESULT Medium::i_addRawToFss(const char *aFilename, SecretKeyStore *pKeyStore, RTVFSFSSTREAM hVfsFssDst,
|
---|
6196 | const ComObjPtr<Progress> &aProgress, bool fSparse)
|
---|
6197 | {
|
---|
6198 | AutoCaller autoCaller(this);
|
---|
6199 | HRESULT hrc = autoCaller.rc();
|
---|
6200 | if (SUCCEEDED(hrc))
|
---|
6201 | {
|
---|
6202 | /*
|
---|
6203 | * Get a readonly hdd for this medium.
|
---|
6204 | */
|
---|
6205 | MediumCryptoFilterSettings CryptoSettingsRead;
|
---|
6206 | MediumLockList SourceMediumLockList;
|
---|
6207 | PVDISK pHdd;
|
---|
6208 | hrc = i_openForIO(false /*fWritable*/, pKeyStore, &pHdd, &SourceMediumLockList, &CryptoSettingsRead);
|
---|
6209 | if (SUCCEEDED(hrc))
|
---|
6210 | {
|
---|
6211 | /*
|
---|
6212 | * Create a VFS file interface to the HDD and attach a progress wrapper
|
---|
6213 | * that monitors the progress reading of the raw image. The image will
|
---|
6214 | * be read twice if hVfsFssDst does sparse processing.
|
---|
6215 | */
|
---|
6216 | RTVFSFILE hVfsFileDisk = NIL_RTVFSFILE;
|
---|
6217 | int vrc = VDCreateVfsFileFromDisk(pHdd, 0 /*fFlags*/, &hVfsFileDisk);
|
---|
6218 | if (RT_SUCCESS(vrc))
|
---|
6219 | {
|
---|
6220 | RTVFSFILE hVfsFileProgress = NIL_RTVFSFILE;
|
---|
6221 | vrc = RTVfsCreateProgressForFile(hVfsFileDisk, aProgress->i_iprtProgressCallback, &*aProgress,
|
---|
6222 | RTVFSPROGRESS_F_CANCELABLE | RTVFSPROGRESS_F_FORWARD_SEEK_AS_READ,
|
---|
6223 | VDGetSize(pHdd, VD_LAST_IMAGE) * (fSparse ? 2 : 1) /*cbExpectedRead*/,
|
---|
6224 | 0 /*cbExpectedWritten*/, &hVfsFileProgress);
|
---|
6225 | RTVfsFileRelease(hVfsFileDisk);
|
---|
6226 | if (RT_SUCCESS(vrc))
|
---|
6227 | {
|
---|
6228 | RTVFSOBJ hVfsObj = RTVfsObjFromFile(hVfsFileProgress);
|
---|
6229 | RTVfsFileRelease(hVfsFileProgress);
|
---|
6230 |
|
---|
6231 | vrc = RTVfsFsStrmAdd(hVfsFssDst, aFilename, hVfsObj, 0 /*fFlags*/);
|
---|
6232 | RTVfsObjRelease(hVfsObj);
|
---|
6233 | if (RT_FAILURE(vrc))
|
---|
6234 | hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Failed to add '%s' to output (%Rrc)"), aFilename, vrc);
|
---|
6235 | }
|
---|
6236 | else
|
---|
6237 | hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
6238 | tr("RTVfsCreateProgressForFile failed when processing '%s' (%Rrc)"), aFilename, vrc);
|
---|
6239 | }
|
---|
6240 | else
|
---|
6241 | hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("VDCreateVfsFileFromDisk failed for '%s' (%Rrc)"), aFilename, vrc);
|
---|
6242 | VDDestroy(pHdd);
|
---|
6243 | }
|
---|
6244 | }
|
---|
6245 | return hrc;
|
---|
6246 | }
|
---|
6247 |
|
---|
6248 | /**
|
---|
6249 | * Used by IAppliance to export disk images.
|
---|
6250 | *
|
---|
6251 | * @param aFilename Filename to create (UTF8).
|
---|
6252 | * @param aFormat Medium format for creating @a aFilename.
|
---|
6253 | * @param aVariant Which exact image format variant to use for the
|
---|
6254 | * destination image.
|
---|
6255 | * @param pKeyStore The optional key store for decrypting the data for
|
---|
6256 | * encrypted media during the export.
|
---|
6257 | * @param hVfsIosDst The destination I/O stream object.
|
---|
6258 | * @param aProgress Progress object to use.
|
---|
6259 | * @return
|
---|
6260 | *
|
---|
6261 | * @note The source format is defined by the Medium instance.
|
---|
6262 | */
|
---|
6263 | HRESULT Medium::i_exportFile(const char *aFilename,
|
---|
6264 | const ComObjPtr<MediumFormat> &aFormat,
|
---|
6265 | MediumVariant_T aVariant,
|
---|
6266 | SecretKeyStore *pKeyStore,
|
---|
6267 | RTVFSIOSTREAM hVfsIosDst,
|
---|
6268 | const ComObjPtr<Progress> &aProgress)
|
---|
6269 | {
|
---|
6270 | AssertPtrReturn(aFilename, E_INVALIDARG);
|
---|
6271 | AssertReturn(aFormat.isNotNull(), E_INVALIDARG);
|
---|
6272 | AssertReturn(aProgress.isNotNull(), E_INVALIDARG);
|
---|
6273 |
|
---|
6274 | AutoCaller autoCaller(this);
|
---|
6275 | HRESULT hrc = autoCaller.rc();
|
---|
6276 | if (SUCCEEDED(hrc))
|
---|
6277 | {
|
---|
6278 | /*
|
---|
6279 | * Setup VD interfaces.
|
---|
6280 | */
|
---|
6281 | PVDINTERFACE pVDImageIfaces = m->vdImageIfaces;
|
---|
6282 | PVDINTERFACEIO pVfsIoIf;
|
---|
6283 | int vrc = VDIfCreateFromVfsStream(hVfsIosDst, RTFILE_O_WRITE, &pVfsIoIf);
|
---|
6284 | if (RT_SUCCESS(vrc))
|
---|
6285 | {
|
---|
6286 | vrc = VDInterfaceAdd(&pVfsIoIf->Core, "Medium::ExportTaskVfsIos", VDINTERFACETYPE_IO,
|
---|
6287 | pVfsIoIf, sizeof(VDINTERFACEIO), &pVDImageIfaces);
|
---|
6288 | if (RT_SUCCESS(vrc))
|
---|
6289 | {
|
---|
6290 | /*
|
---|
6291 | * Get a readonly hdd for this medium (source).
|
---|
6292 | */
|
---|
6293 | MediumCryptoFilterSettings CryptoSettingsRead;
|
---|
6294 | MediumLockList SourceMediumLockList;
|
---|
6295 | PVDISK pSrcHdd;
|
---|
6296 | hrc = i_openForIO(false /*fWritable*/, pKeyStore, &pSrcHdd, &SourceMediumLockList, &CryptoSettingsRead);
|
---|
6297 | if (SUCCEEDED(hrc))
|
---|
6298 | {
|
---|
6299 | /*
|
---|
6300 | * Create the target medium.
|
---|
6301 | */
|
---|
6302 | Utf8Str strDstFormat(aFormat->i_getId());
|
---|
6303 |
|
---|
6304 | /* ensure the target directory exists */
|
---|
6305 | uint64_t fDstCapabilities = aFormat->i_getCapabilities();
|
---|
6306 | if (fDstCapabilities & MediumFormatCapabilities_File)
|
---|
6307 | {
|
---|
6308 | Utf8Str strDstLocation(aFilename);
|
---|
6309 | hrc = VirtualBox::i_ensureFilePathExists(strDstLocation.c_str(),
|
---|
6310 | !(aVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
6311 | }
|
---|
6312 | if (SUCCEEDED(hrc))
|
---|
6313 | {
|
---|
6314 | PVDISK pDstHdd;
|
---|
6315 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDstHdd);
|
---|
6316 | if (RT_SUCCESS(vrc))
|
---|
6317 | {
|
---|
6318 | /*
|
---|
6319 | * Create an interface for getting progress callbacks.
|
---|
6320 | */
|
---|
6321 | VDINTERFACEPROGRESS ProgressIf = VDINTERFACEPROGRESS_INITALIZER(aProgress->i_vdProgressCallback);
|
---|
6322 | PVDINTERFACE pProgress = NULL;
|
---|
6323 | vrc = VDInterfaceAdd(&ProgressIf.Core, "export-progress", VDINTERFACETYPE_PROGRESS,
|
---|
6324 | &*aProgress, sizeof(ProgressIf), &pProgress);
|
---|
6325 | AssertRC(vrc);
|
---|
6326 |
|
---|
6327 | /*
|
---|
6328 | * Do the exporting.
|
---|
6329 | */
|
---|
6330 | vrc = VDCopy(pSrcHdd,
|
---|
6331 | VD_LAST_IMAGE,
|
---|
6332 | pDstHdd,
|
---|
6333 | strDstFormat.c_str(),
|
---|
6334 | aFilename,
|
---|
6335 | false /* fMoveByRename */,
|
---|
6336 | 0 /* cbSize */,
|
---|
6337 | aVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted),
|
---|
6338 | NULL /* pDstUuid */,
|
---|
6339 | VD_OPEN_FLAGS_NORMAL | VD_OPEN_FLAGS_SEQUENTIAL,
|
---|
6340 | pProgress,
|
---|
6341 | pVDImageIfaces,
|
---|
6342 | NULL);
|
---|
6343 | if (RT_SUCCESS(vrc))
|
---|
6344 | hrc = S_OK;
|
---|
6345 | else
|
---|
6346 | hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Could not create the exported medium '%s'%s"),
|
---|
6347 | aFilename, i_vdError(vrc).c_str());
|
---|
6348 | VDDestroy(pDstHdd);
|
---|
6349 | }
|
---|
6350 | else
|
---|
6351 | hrc = setErrorVrc(vrc);
|
---|
6352 | }
|
---|
6353 | }
|
---|
6354 | VDDestroy(pSrcHdd);
|
---|
6355 | }
|
---|
6356 | else
|
---|
6357 | hrc = setErrorVrc(vrc, "VDInterfaceAdd -> %Rrc", vrc);
|
---|
6358 | VDIfDestroyFromVfsStream(pVfsIoIf);
|
---|
6359 | }
|
---|
6360 | else
|
---|
6361 | hrc = setErrorVrc(vrc, "VDIfCreateFromVfsStream -> %Rrc", vrc);
|
---|
6362 | }
|
---|
6363 | return hrc;
|
---|
6364 | }
|
---|
6365 |
|
---|
6366 | /**
|
---|
6367 | * Used by IAppliance to import disk images.
|
---|
6368 | *
|
---|
6369 | * @param aFilename Filename to read (UTF8).
|
---|
6370 | * @param aFormat Medium format for reading @a aFilename.
|
---|
6371 | * @param aVariant Which exact image format variant to use
|
---|
6372 | * for the destination image.
|
---|
6373 | * @param aVfsIosSrc Handle to the source I/O stream.
|
---|
6374 | * @param aParent Parent medium. May be NULL.
|
---|
6375 | * @param aProgress Progress object to use.
|
---|
6376 | * @return
|
---|
6377 | * @note The destination format is defined by the Medium instance.
|
---|
6378 | *
|
---|
6379 | * @todo The only consumer of this method (Appliance::i_importOneDiskImage) is
|
---|
6380 | * already on a worker thread, so perhaps consider bypassing the thread
|
---|
6381 | * here and run in the task synchronously? VBoxSVC has enough threads as
|
---|
6382 | * it is...
|
---|
6383 | */
|
---|
6384 | HRESULT Medium::i_importFile(const char *aFilename,
|
---|
6385 | const ComObjPtr<MediumFormat> &aFormat,
|
---|
6386 | MediumVariant_T aVariant,
|
---|
6387 | RTVFSIOSTREAM aVfsIosSrc,
|
---|
6388 | const ComObjPtr<Medium> &aParent,
|
---|
6389 | const ComObjPtr<Progress> &aProgress)
|
---|
6390 | {
|
---|
6391 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
6392 | * to lock order violations, it probably causes lock order issues related
|
---|
6393 | * to the AutoCaller usage. */
|
---|
6394 | AssertPtrReturn(aFilename, E_INVALIDARG);
|
---|
6395 | AssertReturn(!aFormat.isNull(), E_INVALIDARG);
|
---|
6396 | AssertReturn(!aProgress.isNull(), E_INVALIDARG);
|
---|
6397 |
|
---|
6398 | AutoCaller autoCaller(this);
|
---|
6399 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
6400 |
|
---|
6401 | HRESULT rc = S_OK;
|
---|
6402 | Medium::Task *pTask = NULL;
|
---|
6403 |
|
---|
6404 | try
|
---|
6405 | {
|
---|
6406 | // locking: we need the tree lock first because we access parent pointers
|
---|
6407 | // and we need to write-lock the media involved
|
---|
6408 | uint32_t cHandles = 2;
|
---|
6409 | LockHandle* pHandles[3] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
6410 | this->lockHandle() };
|
---|
6411 | /* Only add parent to the lock if it is not null */
|
---|
6412 | if (!aParent.isNull())
|
---|
6413 | pHandles[cHandles++] = aParent->lockHandle();
|
---|
6414 | AutoWriteLock alock(cHandles,
|
---|
6415 | pHandles
|
---|
6416 | COMMA_LOCKVAL_SRC_POS);
|
---|
6417 |
|
---|
6418 | if ( m->state != MediumState_NotCreated
|
---|
6419 | && m->state != MediumState_Created)
|
---|
6420 | throw i_setStateError();
|
---|
6421 |
|
---|
6422 | /* Build the target lock list. */
|
---|
6423 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
6424 | alock.release();
|
---|
6425 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
6426 | this /* pToLockWrite */,
|
---|
6427 | false /* fMediumLockWriteAll */,
|
---|
6428 | aParent,
|
---|
6429 | *pTargetMediumLockList);
|
---|
6430 | alock.acquire();
|
---|
6431 | if (FAILED(rc))
|
---|
6432 | {
|
---|
6433 | delete pTargetMediumLockList;
|
---|
6434 | throw rc;
|
---|
6435 | }
|
---|
6436 |
|
---|
6437 | alock.release();
|
---|
6438 | rc = pTargetMediumLockList->Lock();
|
---|
6439 | alock.acquire();
|
---|
6440 | if (FAILED(rc))
|
---|
6441 | {
|
---|
6442 | delete pTargetMediumLockList;
|
---|
6443 | throw setError(rc,
|
---|
6444 | tr("Failed to lock target media '%s'"),
|
---|
6445 | i_getLocationFull().c_str());
|
---|
6446 | }
|
---|
6447 |
|
---|
6448 | /* setup task object to carry out the operation asynchronously */
|
---|
6449 | pTask = new Medium::ImportTask(this, aProgress, aFilename, aFormat, aVariant,
|
---|
6450 | aVfsIosSrc, aParent, pTargetMediumLockList);
|
---|
6451 | rc = pTask->rc();
|
---|
6452 | AssertComRC(rc);
|
---|
6453 | if (FAILED(rc))
|
---|
6454 | throw rc;
|
---|
6455 |
|
---|
6456 | if (m->state == MediumState_NotCreated)
|
---|
6457 | m->state = MediumState_Creating;
|
---|
6458 | }
|
---|
6459 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6460 |
|
---|
6461 | if (SUCCEEDED(rc))
|
---|
6462 | rc = pTask->createThread();
|
---|
6463 | else if (pTask != NULL)
|
---|
6464 | delete pTask;
|
---|
6465 |
|
---|
6466 | return rc;
|
---|
6467 | }
|
---|
6468 |
|
---|
6469 | /**
|
---|
6470 | * Internal version of the public CloneTo API which allows to enable certain
|
---|
6471 | * optimizations to improve speed during VM cloning.
|
---|
6472 | *
|
---|
6473 | * @param aTarget Target medium
|
---|
6474 | * @param aVariant Which exact image format variant to use
|
---|
6475 | * for the destination image.
|
---|
6476 | * @param aParent Parent medium. May be NULL.
|
---|
6477 | * @param aProgress Progress object to use.
|
---|
6478 | * @param idxSrcImageSame The last image in the source chain which has the
|
---|
6479 | * same content as the given image in the destination
|
---|
6480 | * chain. Use UINT32_MAX to disable this optimization.
|
---|
6481 | * @param idxDstImageSame The last image in the destination chain which has the
|
---|
6482 | * same content as the given image in the source chain.
|
---|
6483 | * Use UINT32_MAX to disable this optimization.
|
---|
6484 | * @return
|
---|
6485 | */
|
---|
6486 | HRESULT Medium::i_cloneToEx(const ComObjPtr<Medium> &aTarget, MediumVariant_T aVariant,
|
---|
6487 | const ComObjPtr<Medium> &aParent, IProgress **aProgress,
|
---|
6488 | uint32_t idxSrcImageSame, uint32_t idxDstImageSame)
|
---|
6489 | {
|
---|
6490 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
6491 | * to lock order violations, it probably causes lock order issues related
|
---|
6492 | * to the AutoCaller usage. */
|
---|
6493 | CheckComArgNotNull(aTarget);
|
---|
6494 | CheckComArgOutPointerValid(aProgress);
|
---|
6495 | ComAssertRet(aTarget != this, E_INVALIDARG);
|
---|
6496 |
|
---|
6497 | AutoCaller autoCaller(this);
|
---|
6498 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
6499 |
|
---|
6500 | HRESULT rc = S_OK;
|
---|
6501 | ComObjPtr<Progress> pProgress;
|
---|
6502 | Medium::Task *pTask = NULL;
|
---|
6503 |
|
---|
6504 | try
|
---|
6505 | {
|
---|
6506 | // locking: we need the tree lock first because we access parent pointers
|
---|
6507 | // and we need to write-lock the media involved
|
---|
6508 | uint32_t cHandles = 3;
|
---|
6509 | LockHandle* pHandles[4] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
6510 | this->lockHandle(),
|
---|
6511 | aTarget->lockHandle() };
|
---|
6512 | /* Only add parent to the lock if it is not null */
|
---|
6513 | if (!aParent.isNull())
|
---|
6514 | pHandles[cHandles++] = aParent->lockHandle();
|
---|
6515 | AutoWriteLock alock(cHandles,
|
---|
6516 | pHandles
|
---|
6517 | COMMA_LOCKVAL_SRC_POS);
|
---|
6518 |
|
---|
6519 | if ( aTarget->m->state != MediumState_NotCreated
|
---|
6520 | && aTarget->m->state != MediumState_Created)
|
---|
6521 | throw aTarget->i_setStateError();
|
---|
6522 |
|
---|
6523 | /* Build the source lock list. */
|
---|
6524 | MediumLockList *pSourceMediumLockList(new MediumLockList());
|
---|
6525 | alock.release();
|
---|
6526 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
6527 | NULL /* pToLockWrite */,
|
---|
6528 | false /* fMediumLockWriteAll */,
|
---|
6529 | NULL,
|
---|
6530 | *pSourceMediumLockList);
|
---|
6531 | alock.acquire();
|
---|
6532 | if (FAILED(rc))
|
---|
6533 | {
|
---|
6534 | delete pSourceMediumLockList;
|
---|
6535 | throw rc;
|
---|
6536 | }
|
---|
6537 |
|
---|
6538 | /* Build the target lock list (including the to-be parent chain). */
|
---|
6539 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
6540 | alock.release();
|
---|
6541 | rc = aTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
6542 | aTarget /* pToLockWrite */,
|
---|
6543 | false /* fMediumLockWriteAll */,
|
---|
6544 | aParent,
|
---|
6545 | *pTargetMediumLockList);
|
---|
6546 | alock.acquire();
|
---|
6547 | if (FAILED(rc))
|
---|
6548 | {
|
---|
6549 | delete pSourceMediumLockList;
|
---|
6550 | delete pTargetMediumLockList;
|
---|
6551 | throw rc;
|
---|
6552 | }
|
---|
6553 |
|
---|
6554 | alock.release();
|
---|
6555 | rc = pSourceMediumLockList->Lock();
|
---|
6556 | alock.acquire();
|
---|
6557 | if (FAILED(rc))
|
---|
6558 | {
|
---|
6559 | delete pSourceMediumLockList;
|
---|
6560 | delete pTargetMediumLockList;
|
---|
6561 | throw setError(rc,
|
---|
6562 | tr("Failed to lock source media '%s'"),
|
---|
6563 | i_getLocationFull().c_str());
|
---|
6564 | }
|
---|
6565 | alock.release();
|
---|
6566 | rc = pTargetMediumLockList->Lock();
|
---|
6567 | alock.acquire();
|
---|
6568 | if (FAILED(rc))
|
---|
6569 | {
|
---|
6570 | delete pSourceMediumLockList;
|
---|
6571 | delete pTargetMediumLockList;
|
---|
6572 | throw setError(rc,
|
---|
6573 | tr("Failed to lock target media '%s'"),
|
---|
6574 | aTarget->i_getLocationFull().c_str());
|
---|
6575 | }
|
---|
6576 |
|
---|
6577 | pProgress.createObject();
|
---|
6578 | rc = pProgress->init(m->pVirtualBox,
|
---|
6579 | static_cast <IMedium *>(this),
|
---|
6580 | BstrFmt(tr("Creating clone medium '%s'"), aTarget->m->strLocationFull.c_str()).raw(),
|
---|
6581 | TRUE /* aCancelable */);
|
---|
6582 | if (FAILED(rc))
|
---|
6583 | {
|
---|
6584 | delete pSourceMediumLockList;
|
---|
6585 | delete pTargetMediumLockList;
|
---|
6586 | throw rc;
|
---|
6587 | }
|
---|
6588 |
|
---|
6589 | /* setup task object to carry out the operation asynchronously */
|
---|
6590 | pTask = new Medium::CloneTask(this, pProgress, aTarget, aVariant,
|
---|
6591 | aParent, idxSrcImageSame,
|
---|
6592 | idxDstImageSame, pSourceMediumLockList,
|
---|
6593 | pTargetMediumLockList);
|
---|
6594 | rc = pTask->rc();
|
---|
6595 | AssertComRC(rc);
|
---|
6596 | if (FAILED(rc))
|
---|
6597 | throw rc;
|
---|
6598 |
|
---|
6599 | if (aTarget->m->state == MediumState_NotCreated)
|
---|
6600 | aTarget->m->state = MediumState_Creating;
|
---|
6601 | }
|
---|
6602 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6603 |
|
---|
6604 | if (SUCCEEDED(rc))
|
---|
6605 | {
|
---|
6606 | rc = pTask->createThread();
|
---|
6607 |
|
---|
6608 | if (SUCCEEDED(rc))
|
---|
6609 | pProgress.queryInterfaceTo(aProgress);
|
---|
6610 | }
|
---|
6611 | else if (pTask != NULL)
|
---|
6612 | delete pTask;
|
---|
6613 |
|
---|
6614 | return rc;
|
---|
6615 | }
|
---|
6616 |
|
---|
6617 | /**
|
---|
6618 | * Returns the key identifier for this medium if encryption is configured.
|
---|
6619 | *
|
---|
6620 | * @returns Key identifier or empty string if no encryption is configured.
|
---|
6621 | */
|
---|
6622 | const Utf8Str& Medium::i_getKeyId()
|
---|
6623 | {
|
---|
6624 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
6625 |
|
---|
6626 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6627 |
|
---|
6628 | settings::StringsMap::const_iterator it = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
6629 | if (it == pBase->m->mapProperties.end())
|
---|
6630 | return Utf8Str::Empty;
|
---|
6631 |
|
---|
6632 | return it->second;
|
---|
6633 | }
|
---|
6634 |
|
---|
6635 |
|
---|
6636 | /**
|
---|
6637 | * Returns all filter related properties.
|
---|
6638 | *
|
---|
6639 | * @returns COM status code.
|
---|
6640 | * @param aReturnNames Where to store the properties names on success.
|
---|
6641 | * @param aReturnValues Where to store the properties values on success.
|
---|
6642 | */
|
---|
6643 | HRESULT Medium::i_getFilterProperties(std::vector<com::Utf8Str> &aReturnNames,
|
---|
6644 | std::vector<com::Utf8Str> &aReturnValues)
|
---|
6645 | {
|
---|
6646 | std::vector<com::Utf8Str> aPropNames;
|
---|
6647 | std::vector<com::Utf8Str> aPropValues;
|
---|
6648 | HRESULT hrc = getProperties(Utf8Str(""), aPropNames, aPropValues);
|
---|
6649 |
|
---|
6650 | if (SUCCEEDED(hrc))
|
---|
6651 | {
|
---|
6652 | unsigned cReturnSize = 0;
|
---|
6653 | aReturnNames.resize(0);
|
---|
6654 | aReturnValues.resize(0);
|
---|
6655 | for (unsigned idx = 0; idx < aPropNames.size(); idx++)
|
---|
6656 | {
|
---|
6657 | if (i_isPropertyForFilter(aPropNames[idx]))
|
---|
6658 | {
|
---|
6659 | aReturnNames.resize(cReturnSize + 1);
|
---|
6660 | aReturnValues.resize(cReturnSize + 1);
|
---|
6661 | aReturnNames[cReturnSize] = aPropNames[idx];
|
---|
6662 | aReturnValues[cReturnSize] = aPropValues[idx];
|
---|
6663 | cReturnSize++;
|
---|
6664 | }
|
---|
6665 | }
|
---|
6666 | }
|
---|
6667 |
|
---|
6668 | return hrc;
|
---|
6669 | }
|
---|
6670 |
|
---|
6671 | /**
|
---|
6672 | * Preparation to move this medium to a new location
|
---|
6673 | *
|
---|
6674 | * @param aLocation Location of the storage unit. If the location is a FS-path,
|
---|
6675 | * then it can be relative to the VirtualBox home directory.
|
---|
6676 | *
|
---|
6677 | * @note Must be called from under this object's write lock.
|
---|
6678 | */
|
---|
6679 | HRESULT Medium::i_preparationForMoving(const Utf8Str &aLocation)
|
---|
6680 | {
|
---|
6681 | HRESULT rc = E_FAIL;
|
---|
6682 |
|
---|
6683 | if (i_getLocationFull() != aLocation)
|
---|
6684 | {
|
---|
6685 | m->strNewLocationFull = aLocation;
|
---|
6686 | m->fMoveThisMedium = true;
|
---|
6687 | rc = S_OK;
|
---|
6688 | }
|
---|
6689 |
|
---|
6690 | return rc;
|
---|
6691 | }
|
---|
6692 |
|
---|
6693 | /**
|
---|
6694 | * Checking whether current operation "moving" or not
|
---|
6695 | */
|
---|
6696 | bool Medium::i_isMoveOperation(const ComObjPtr<Medium> &aTarget) const
|
---|
6697 | {
|
---|
6698 | RT_NOREF(aTarget);
|
---|
6699 | return (m->fMoveThisMedium == true) ? true:false;
|
---|
6700 | }
|
---|
6701 |
|
---|
6702 | bool Medium::i_resetMoveOperationData()
|
---|
6703 | {
|
---|
6704 | m->strNewLocationFull.setNull();
|
---|
6705 | m->fMoveThisMedium = false;
|
---|
6706 | return true;
|
---|
6707 | }
|
---|
6708 |
|
---|
6709 | Utf8Str Medium::i_getNewLocationForMoving() const
|
---|
6710 | {
|
---|
6711 | if (m->fMoveThisMedium == true)
|
---|
6712 | return m->strNewLocationFull;
|
---|
6713 | else
|
---|
6714 | return Utf8Str();
|
---|
6715 | }
|
---|
6716 | ////////////////////////////////////////////////////////////////////////////////
|
---|
6717 | //
|
---|
6718 | // Private methods
|
---|
6719 | //
|
---|
6720 | ////////////////////////////////////////////////////////////////////////////////
|
---|
6721 |
|
---|
6722 | /**
|
---|
6723 | * Queries information from the medium.
|
---|
6724 | *
|
---|
6725 | * As a result of this call, the accessibility state and data members such as
|
---|
6726 | * size and description will be updated with the current information.
|
---|
6727 | *
|
---|
6728 | * @note This method may block during a system I/O call that checks storage
|
---|
6729 | * accessibility.
|
---|
6730 | *
|
---|
6731 | * @note Caller MUST NOT hold the media tree or medium lock.
|
---|
6732 | *
|
---|
6733 | * @note Locks m->pParent for reading. Locks this object for writing.
|
---|
6734 | *
|
---|
6735 | * @param fSetImageId Whether to reset the UUID contained in the image file
|
---|
6736 | * to the UUID in the medium instance data (see SetIDs())
|
---|
6737 | * @param fSetParentId Whether to reset the parent UUID contained in the image
|
---|
6738 | * file to the parent UUID in the medium instance data (see
|
---|
6739 | * SetIDs())
|
---|
6740 | * @param autoCaller
|
---|
6741 | * @return
|
---|
6742 | */
|
---|
6743 | HRESULT Medium::i_queryInfo(bool fSetImageId, bool fSetParentId, AutoCaller &autoCaller)
|
---|
6744 | {
|
---|
6745 | Assert(!isWriteLockOnCurrentThread());
|
---|
6746 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6747 |
|
---|
6748 | if ( ( m->state != MediumState_Created
|
---|
6749 | && m->state != MediumState_Inaccessible
|
---|
6750 | && m->state != MediumState_LockedRead)
|
---|
6751 | || m->fClosing)
|
---|
6752 | return E_FAIL;
|
---|
6753 |
|
---|
6754 | HRESULT rc = S_OK;
|
---|
6755 |
|
---|
6756 | int vrc = VINF_SUCCESS;
|
---|
6757 |
|
---|
6758 | /* check if a blocking i_queryInfo() call is in progress on some other thread,
|
---|
6759 | * and wait for it to finish if so instead of querying data ourselves */
|
---|
6760 | if (m->queryInfoRunning)
|
---|
6761 | {
|
---|
6762 | Assert( m->state == MediumState_LockedRead
|
---|
6763 | || m->state == MediumState_LockedWrite);
|
---|
6764 |
|
---|
6765 | while (m->queryInfoRunning)
|
---|
6766 | {
|
---|
6767 | alock.release();
|
---|
6768 | /* must not hold the object lock now */
|
---|
6769 | Assert(!isWriteLockOnCurrentThread());
|
---|
6770 | {
|
---|
6771 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
6772 | }
|
---|
6773 | alock.acquire();
|
---|
6774 | }
|
---|
6775 |
|
---|
6776 | return S_OK;
|
---|
6777 | }
|
---|
6778 |
|
---|
6779 | bool success = false;
|
---|
6780 | Utf8Str lastAccessError;
|
---|
6781 |
|
---|
6782 | /* are we dealing with a new medium constructed using the existing
|
---|
6783 | * location? */
|
---|
6784 | bool isImport = m->id.isZero();
|
---|
6785 | unsigned uOpenFlags = VD_OPEN_FLAGS_INFO;
|
---|
6786 |
|
---|
6787 | /* Note that we don't use VD_OPEN_FLAGS_READONLY when opening new
|
---|
6788 | * media because that would prevent necessary modifications
|
---|
6789 | * when opening media of some third-party formats for the first
|
---|
6790 | * time in VirtualBox (such as VMDK for which VDOpen() needs to
|
---|
6791 | * generate an UUID if it is missing) */
|
---|
6792 | if ( m->hddOpenMode == OpenReadOnly
|
---|
6793 | || m->type == MediumType_Readonly
|
---|
6794 | || (!isImport && !fSetImageId && !fSetParentId)
|
---|
6795 | )
|
---|
6796 | uOpenFlags |= VD_OPEN_FLAGS_READONLY;
|
---|
6797 |
|
---|
6798 | /* Open shareable medium with the appropriate flags */
|
---|
6799 | if (m->type == MediumType_Shareable)
|
---|
6800 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
6801 |
|
---|
6802 | /* Lock the medium, which makes the behavior much more consistent, must be
|
---|
6803 | * done before dropping the object lock and setting queryInfoRunning. */
|
---|
6804 | ComPtr<IToken> pToken;
|
---|
6805 | if (uOpenFlags & (VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SHAREABLE))
|
---|
6806 | rc = LockRead(pToken.asOutParam());
|
---|
6807 | else
|
---|
6808 | rc = LockWrite(pToken.asOutParam());
|
---|
6809 | if (FAILED(rc)) return rc;
|
---|
6810 |
|
---|
6811 | /* Copies of the input state fields which are not read-only,
|
---|
6812 | * as we're dropping the lock. CAUTION: be extremely careful what
|
---|
6813 | * you do with the contents of this medium object, as you will
|
---|
6814 | * create races if there are concurrent changes. */
|
---|
6815 | Utf8Str format(m->strFormat);
|
---|
6816 | Utf8Str location(m->strLocationFull);
|
---|
6817 | ComObjPtr<MediumFormat> formatObj = m->formatObj;
|
---|
6818 |
|
---|
6819 | /* "Output" values which can't be set because the lock isn't held
|
---|
6820 | * at the time the values are determined. */
|
---|
6821 | Guid mediumId = m->id;
|
---|
6822 | uint64_t mediumSize = 0;
|
---|
6823 | uint64_t mediumLogicalSize = 0;
|
---|
6824 |
|
---|
6825 | /* Flag whether a base image has a non-zero parent UUID and thus
|
---|
6826 | * need repairing after it was closed again. */
|
---|
6827 | bool fRepairImageZeroParentUuid = false;
|
---|
6828 |
|
---|
6829 | ComObjPtr<VirtualBox> pVirtualBox = m->pVirtualBox;
|
---|
6830 |
|
---|
6831 | /* must be set before leaving the object lock the first time */
|
---|
6832 | m->queryInfoRunning = true;
|
---|
6833 |
|
---|
6834 | /* must leave object lock now, because a lock from a higher lock class
|
---|
6835 | * is needed and also a lengthy operation is coming */
|
---|
6836 | alock.release();
|
---|
6837 | autoCaller.release();
|
---|
6838 |
|
---|
6839 | /* Note that taking the queryInfoSem after leaving the object lock above
|
---|
6840 | * can lead to short spinning of the loops waiting for i_queryInfo() to
|
---|
6841 | * complete. This is unavoidable since the other order causes a lock order
|
---|
6842 | * violation: here it would be requesting the object lock (at the beginning
|
---|
6843 | * of the method), then queryInfoSem, and below the other way round. */
|
---|
6844 | AutoWriteLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
6845 |
|
---|
6846 | /* take the opportunity to have a media tree lock, released initially */
|
---|
6847 | Assert(!isWriteLockOnCurrentThread());
|
---|
6848 | Assert(!pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
6849 | AutoWriteLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
6850 | treeLock.release();
|
---|
6851 |
|
---|
6852 | /* re-take the caller, but not the object lock, to keep uninit away */
|
---|
6853 | autoCaller.add();
|
---|
6854 | if (FAILED(autoCaller.rc()))
|
---|
6855 | {
|
---|
6856 | m->queryInfoRunning = false;
|
---|
6857 | return autoCaller.rc();
|
---|
6858 | }
|
---|
6859 |
|
---|
6860 | try
|
---|
6861 | {
|
---|
6862 | /* skip accessibility checks for host drives */
|
---|
6863 | if (m->hostDrive)
|
---|
6864 | {
|
---|
6865 | success = true;
|
---|
6866 | throw S_OK;
|
---|
6867 | }
|
---|
6868 |
|
---|
6869 | PVDISK hdd;
|
---|
6870 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
6871 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6872 |
|
---|
6873 | try
|
---|
6874 | {
|
---|
6875 | /** @todo This kind of opening of media is assuming that diff
|
---|
6876 | * media can be opened as base media. Should be documented that
|
---|
6877 | * it must work for all medium format backends. */
|
---|
6878 | vrc = VDOpen(hdd,
|
---|
6879 | format.c_str(),
|
---|
6880 | location.c_str(),
|
---|
6881 | uOpenFlags | m->uOpenFlagsDef,
|
---|
6882 | m->vdImageIfaces);
|
---|
6883 | if (RT_FAILURE(vrc))
|
---|
6884 | {
|
---|
6885 | lastAccessError = Utf8StrFmt(tr("Could not open the medium '%s'%s"),
|
---|
6886 | location.c_str(), i_vdError(vrc).c_str());
|
---|
6887 | throw S_OK;
|
---|
6888 | }
|
---|
6889 |
|
---|
6890 | if (formatObj->i_getCapabilities() & MediumFormatCapabilities_Uuid)
|
---|
6891 | {
|
---|
6892 | /* Modify the UUIDs if necessary. The associated fields are
|
---|
6893 | * not modified by other code, so no need to copy. */
|
---|
6894 | if (fSetImageId)
|
---|
6895 | {
|
---|
6896 | alock.acquire();
|
---|
6897 | vrc = VDSetUuid(hdd, 0, m->uuidImage.raw());
|
---|
6898 | alock.release();
|
---|
6899 | if (RT_FAILURE(vrc))
|
---|
6900 | {
|
---|
6901 | lastAccessError = Utf8StrFmt(tr("Could not update the UUID of medium '%s'%s"),
|
---|
6902 | location.c_str(), i_vdError(vrc).c_str());
|
---|
6903 | throw S_OK;
|
---|
6904 | }
|
---|
6905 | mediumId = m->uuidImage;
|
---|
6906 | }
|
---|
6907 | if (fSetParentId)
|
---|
6908 | {
|
---|
6909 | alock.acquire();
|
---|
6910 | vrc = VDSetParentUuid(hdd, 0, m->uuidParentImage.raw());
|
---|
6911 | alock.release();
|
---|
6912 | if (RT_FAILURE(vrc))
|
---|
6913 | {
|
---|
6914 | lastAccessError = Utf8StrFmt(tr("Could not update the parent UUID of medium '%s'%s"),
|
---|
6915 | location.c_str(), i_vdError(vrc).c_str());
|
---|
6916 | throw S_OK;
|
---|
6917 | }
|
---|
6918 | }
|
---|
6919 | /* zap the information, these are no long-term members */
|
---|
6920 | alock.acquire();
|
---|
6921 | unconst(m->uuidImage).clear();
|
---|
6922 | unconst(m->uuidParentImage).clear();
|
---|
6923 | alock.release();
|
---|
6924 |
|
---|
6925 | /* check the UUID */
|
---|
6926 | RTUUID uuid;
|
---|
6927 | vrc = VDGetUuid(hdd, 0, &uuid);
|
---|
6928 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6929 |
|
---|
6930 | if (isImport)
|
---|
6931 | {
|
---|
6932 | mediumId = uuid;
|
---|
6933 |
|
---|
6934 | if (mediumId.isZero() && (m->hddOpenMode == OpenReadOnly))
|
---|
6935 | // only when importing a VDMK that has no UUID, create one in memory
|
---|
6936 | mediumId.create();
|
---|
6937 | }
|
---|
6938 | else
|
---|
6939 | {
|
---|
6940 | Assert(!mediumId.isZero());
|
---|
6941 |
|
---|
6942 | if (mediumId != uuid)
|
---|
6943 | {
|
---|
6944 | /** @todo r=klaus this always refers to VirtualBox.xml as the medium registry, even for new VMs */
|
---|
6945 | lastAccessError = Utf8StrFmt(
|
---|
6946 | tr("UUID {%RTuuid} of the medium '%s' does not match the value {%RTuuid} stored in the media registry ('%s')"),
|
---|
6947 | &uuid,
|
---|
6948 | location.c_str(),
|
---|
6949 | mediumId.raw(),
|
---|
6950 | pVirtualBox->i_settingsFilePath().c_str());
|
---|
6951 | throw S_OK;
|
---|
6952 | }
|
---|
6953 | }
|
---|
6954 | }
|
---|
6955 | else
|
---|
6956 | {
|
---|
6957 | /* the backend does not support storing UUIDs within the
|
---|
6958 | * underlying storage so use what we store in XML */
|
---|
6959 |
|
---|
6960 | if (fSetImageId)
|
---|
6961 | {
|
---|
6962 | /* set the UUID if an API client wants to change it */
|
---|
6963 | alock.acquire();
|
---|
6964 | mediumId = m->uuidImage;
|
---|
6965 | alock.release();
|
---|
6966 | }
|
---|
6967 | else if (isImport)
|
---|
6968 | {
|
---|
6969 | /* generate an UUID for an imported UUID-less medium */
|
---|
6970 | mediumId.create();
|
---|
6971 | }
|
---|
6972 | }
|
---|
6973 |
|
---|
6974 | /* set the image uuid before the below parent uuid handling code
|
---|
6975 | * might place it somewhere in the media tree, so that the medium
|
---|
6976 | * UUID is valid at this point */
|
---|
6977 | alock.acquire();
|
---|
6978 | if (isImport || fSetImageId)
|
---|
6979 | unconst(m->id) = mediumId;
|
---|
6980 | alock.release();
|
---|
6981 |
|
---|
6982 | /* get the medium variant */
|
---|
6983 | unsigned uImageFlags;
|
---|
6984 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
6985 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6986 | alock.acquire();
|
---|
6987 | m->variant = (MediumVariant_T)uImageFlags;
|
---|
6988 | alock.release();
|
---|
6989 |
|
---|
6990 | /* check/get the parent uuid and update corresponding state */
|
---|
6991 | if (uImageFlags & VD_IMAGE_FLAGS_DIFF)
|
---|
6992 | {
|
---|
6993 | RTUUID parentId;
|
---|
6994 | vrc = VDGetParentUuid(hdd, 0, &parentId);
|
---|
6995 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6996 |
|
---|
6997 | /* streamOptimized VMDK images are only accepted as base
|
---|
6998 | * images, as this allows automatic repair of OVF appliances.
|
---|
6999 | * Since such images don't support random writes they will not
|
---|
7000 | * be created for diff images. Only an overly smart user might
|
---|
7001 | * manually create this case. Too bad for him. */
|
---|
7002 | if ( (isImport || fSetParentId)
|
---|
7003 | && !(uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
|
---|
7004 | {
|
---|
7005 | /* the parent must be known to us. Note that we freely
|
---|
7006 | * call locking methods of mVirtualBox and parent, as all
|
---|
7007 | * relevant locks must be already held. There may be no
|
---|
7008 | * concurrent access to the just opened medium on other
|
---|
7009 | * threads yet (and init() will fail if this method reports
|
---|
7010 | * MediumState_Inaccessible) */
|
---|
7011 |
|
---|
7012 | ComObjPtr<Medium> pParent;
|
---|
7013 | if (RTUuidIsNull(&parentId))
|
---|
7014 | rc = VBOX_E_OBJECT_NOT_FOUND;
|
---|
7015 | else
|
---|
7016 | rc = pVirtualBox->i_findHardDiskById(Guid(parentId), false /* aSetError */, &pParent);
|
---|
7017 | if (FAILED(rc))
|
---|
7018 | {
|
---|
7019 | if (fSetImageId && !fSetParentId)
|
---|
7020 | {
|
---|
7021 | /* If the image UUID gets changed for an existing
|
---|
7022 | * image then the parent UUID can be stale. In such
|
---|
7023 | * cases clear the parent information. The parent
|
---|
7024 | * information may/will be re-set later if the
|
---|
7025 | * API client wants to adjust a complete medium
|
---|
7026 | * hierarchy one by one. */
|
---|
7027 | rc = S_OK;
|
---|
7028 | alock.acquire();
|
---|
7029 | RTUuidClear(&parentId);
|
---|
7030 | vrc = VDSetParentUuid(hdd, 0, &parentId);
|
---|
7031 | alock.release();
|
---|
7032 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7033 | }
|
---|
7034 | else
|
---|
7035 | {
|
---|
7036 | lastAccessError = Utf8StrFmt(tr("Parent medium with UUID {%RTuuid} of the medium '%s' is not found in the media registry ('%s')"),
|
---|
7037 | &parentId, location.c_str(),
|
---|
7038 | pVirtualBox->i_settingsFilePath().c_str());
|
---|
7039 | throw S_OK;
|
---|
7040 | }
|
---|
7041 | }
|
---|
7042 |
|
---|
7043 | /* must drop the caller before taking the tree lock */
|
---|
7044 | autoCaller.release();
|
---|
7045 | /* we set m->pParent & children() */
|
---|
7046 | treeLock.acquire();
|
---|
7047 | autoCaller.add();
|
---|
7048 | if (FAILED(autoCaller.rc()))
|
---|
7049 | throw autoCaller.rc();
|
---|
7050 |
|
---|
7051 | if (m->pParent)
|
---|
7052 | i_deparent();
|
---|
7053 |
|
---|
7054 | if (!pParent.isNull())
|
---|
7055 | if (pParent->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
7056 | {
|
---|
7057 | AutoReadLock plock(pParent COMMA_LOCKVAL_SRC_POS);
|
---|
7058 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7059 | 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"),
|
---|
7060 | pParent->m->strLocationFull.c_str());
|
---|
7061 | }
|
---|
7062 | i_setParent(pParent);
|
---|
7063 |
|
---|
7064 | treeLock.release();
|
---|
7065 | }
|
---|
7066 | else
|
---|
7067 | {
|
---|
7068 | /* must drop the caller before taking the tree lock */
|
---|
7069 | autoCaller.release();
|
---|
7070 | /* we access m->pParent */
|
---|
7071 | treeLock.acquire();
|
---|
7072 | autoCaller.add();
|
---|
7073 | if (FAILED(autoCaller.rc()))
|
---|
7074 | throw autoCaller.rc();
|
---|
7075 |
|
---|
7076 | /* check that parent UUIDs match. Note that there's no need
|
---|
7077 | * for the parent's AutoCaller (our lifetime is bound to
|
---|
7078 | * it) */
|
---|
7079 |
|
---|
7080 | if (m->pParent.isNull())
|
---|
7081 | {
|
---|
7082 | /* Due to a bug in VDCopy() in VirtualBox 3.0.0-3.0.14
|
---|
7083 | * and 3.1.0-3.1.8 there are base images out there
|
---|
7084 | * which have a non-zero parent UUID. No point in
|
---|
7085 | * complaining about them, instead automatically
|
---|
7086 | * repair the problem. Later we can bring back the
|
---|
7087 | * error message, but we should wait until really
|
---|
7088 | * most users have repaired their images, either with
|
---|
7089 | * VBoxFixHdd or this way. */
|
---|
7090 | #if 1
|
---|
7091 | fRepairImageZeroParentUuid = true;
|
---|
7092 | #else /* 0 */
|
---|
7093 | lastAccessError = Utf8StrFmt(
|
---|
7094 | tr("Medium type of '%s' is differencing but it is not associated with any parent medium in the media registry ('%s')"),
|
---|
7095 | location.c_str(),
|
---|
7096 | pVirtualBox->settingsFilePath().c_str());
|
---|
7097 | treeLock.release();
|
---|
7098 | throw S_OK;
|
---|
7099 | #endif /* 0 */
|
---|
7100 | }
|
---|
7101 |
|
---|
7102 | {
|
---|
7103 | autoCaller.release();
|
---|
7104 | AutoReadLock parentLock(m->pParent COMMA_LOCKVAL_SRC_POS);
|
---|
7105 | autoCaller.add();
|
---|
7106 | if (FAILED(autoCaller.rc()))
|
---|
7107 | throw autoCaller.rc();
|
---|
7108 |
|
---|
7109 | if ( !fRepairImageZeroParentUuid
|
---|
7110 | && m->pParent->i_getState() != MediumState_Inaccessible
|
---|
7111 | && m->pParent->i_getId() != parentId)
|
---|
7112 | {
|
---|
7113 | /** @todo r=klaus this always refers to VirtualBox.xml as the medium registry, even for new VMs */
|
---|
7114 | lastAccessError = Utf8StrFmt(
|
---|
7115 | tr("Parent UUID {%RTuuid} of the medium '%s' does not match UUID {%RTuuid} of its parent medium stored in the media registry ('%s')"),
|
---|
7116 | &parentId, location.c_str(),
|
---|
7117 | m->pParent->i_getId().raw(),
|
---|
7118 | pVirtualBox->i_settingsFilePath().c_str());
|
---|
7119 | parentLock.release();
|
---|
7120 | treeLock.release();
|
---|
7121 | throw S_OK;
|
---|
7122 | }
|
---|
7123 | }
|
---|
7124 |
|
---|
7125 | /// @todo NEWMEDIA what to do if the parent is not
|
---|
7126 | /// accessible while the diff is? Probably nothing. The
|
---|
7127 | /// real code will detect the mismatch anyway.
|
---|
7128 |
|
---|
7129 | treeLock.release();
|
---|
7130 | }
|
---|
7131 | }
|
---|
7132 |
|
---|
7133 | mediumSize = VDGetFileSize(hdd, 0);
|
---|
7134 | mediumLogicalSize = VDGetSize(hdd, 0);
|
---|
7135 |
|
---|
7136 | success = true;
|
---|
7137 | }
|
---|
7138 | catch (HRESULT aRC)
|
---|
7139 | {
|
---|
7140 | rc = aRC;
|
---|
7141 | }
|
---|
7142 |
|
---|
7143 | vrc = VDDestroy(hdd);
|
---|
7144 | if (RT_FAILURE(vrc))
|
---|
7145 | {
|
---|
7146 | lastAccessError = Utf8StrFmt(tr("Could not update and close the medium '%s'%s"),
|
---|
7147 | location.c_str(), i_vdError(vrc).c_str());
|
---|
7148 | success = false;
|
---|
7149 | throw S_OK;
|
---|
7150 | }
|
---|
7151 | }
|
---|
7152 | catch (HRESULT aRC)
|
---|
7153 | {
|
---|
7154 | rc = aRC;
|
---|
7155 | }
|
---|
7156 |
|
---|
7157 | autoCaller.release();
|
---|
7158 | treeLock.acquire();
|
---|
7159 | autoCaller.add();
|
---|
7160 | if (FAILED(autoCaller.rc()))
|
---|
7161 | {
|
---|
7162 | m->queryInfoRunning = false;
|
---|
7163 | return autoCaller.rc();
|
---|
7164 | }
|
---|
7165 | alock.acquire();
|
---|
7166 |
|
---|
7167 | if (success)
|
---|
7168 | {
|
---|
7169 | m->size = mediumSize;
|
---|
7170 | m->logicalSize = mediumLogicalSize;
|
---|
7171 | m->strLastAccessError.setNull();
|
---|
7172 | }
|
---|
7173 | else
|
---|
7174 | {
|
---|
7175 | m->strLastAccessError = lastAccessError;
|
---|
7176 | Log1WarningFunc(("'%s' is not accessible (error='%s', rc=%Rhrc, vrc=%Rrc)\n",
|
---|
7177 | location.c_str(), m->strLastAccessError.c_str(), rc, vrc));
|
---|
7178 | }
|
---|
7179 |
|
---|
7180 | /* Set the proper state according to the result of the check */
|
---|
7181 | if (success)
|
---|
7182 | m->preLockState = MediumState_Created;
|
---|
7183 | else
|
---|
7184 | m->preLockState = MediumState_Inaccessible;
|
---|
7185 |
|
---|
7186 | /* unblock anyone waiting for the i_queryInfo results */
|
---|
7187 | qlock.release();
|
---|
7188 | m->queryInfoRunning = false;
|
---|
7189 |
|
---|
7190 | pToken->Abandon();
|
---|
7191 | pToken.setNull();
|
---|
7192 |
|
---|
7193 | if (FAILED(rc)) return rc;
|
---|
7194 |
|
---|
7195 | /* If this is a base image which incorrectly has a parent UUID set,
|
---|
7196 | * repair the image now by zeroing the parent UUID. This is only done
|
---|
7197 | * when we have structural information from a config file, on import
|
---|
7198 | * this is not possible. If someone would accidentally call openMedium
|
---|
7199 | * with a diff image before the base is registered this would destroy
|
---|
7200 | * the diff. Not acceptable. */
|
---|
7201 | if (fRepairImageZeroParentUuid)
|
---|
7202 | {
|
---|
7203 | rc = LockWrite(pToken.asOutParam());
|
---|
7204 | if (FAILED(rc)) return rc;
|
---|
7205 |
|
---|
7206 | alock.release();
|
---|
7207 |
|
---|
7208 | try
|
---|
7209 | {
|
---|
7210 | PVDISK hdd;
|
---|
7211 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
7212 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7213 |
|
---|
7214 | try
|
---|
7215 | {
|
---|
7216 | vrc = VDOpen(hdd,
|
---|
7217 | format.c_str(),
|
---|
7218 | location.c_str(),
|
---|
7219 | (uOpenFlags & ~VD_OPEN_FLAGS_READONLY) | m->uOpenFlagsDef,
|
---|
7220 | m->vdImageIfaces);
|
---|
7221 | if (RT_FAILURE(vrc))
|
---|
7222 | throw S_OK;
|
---|
7223 |
|
---|
7224 | RTUUID zeroParentUuid;
|
---|
7225 | RTUuidClear(&zeroParentUuid);
|
---|
7226 | vrc = VDSetParentUuid(hdd, 0, &zeroParentUuid);
|
---|
7227 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7228 | }
|
---|
7229 | catch (HRESULT aRC)
|
---|
7230 | {
|
---|
7231 | rc = aRC;
|
---|
7232 | }
|
---|
7233 |
|
---|
7234 | VDDestroy(hdd);
|
---|
7235 | }
|
---|
7236 | catch (HRESULT aRC)
|
---|
7237 | {
|
---|
7238 | rc = aRC;
|
---|
7239 | }
|
---|
7240 |
|
---|
7241 | pToken->Abandon();
|
---|
7242 | pToken.setNull();
|
---|
7243 | if (FAILED(rc)) return rc;
|
---|
7244 | }
|
---|
7245 |
|
---|
7246 | return rc;
|
---|
7247 | }
|
---|
7248 |
|
---|
7249 | /**
|
---|
7250 | * Performs extra checks if the medium can be closed and returns S_OK in
|
---|
7251 | * this case. Otherwise, returns a respective error message. Called by
|
---|
7252 | * Close() under the medium tree lock and the medium lock.
|
---|
7253 | *
|
---|
7254 | * @note Also reused by Medium::Reset().
|
---|
7255 | *
|
---|
7256 | * @note Caller must hold the media tree write lock!
|
---|
7257 | */
|
---|
7258 | HRESULT Medium::i_canClose()
|
---|
7259 | {
|
---|
7260 | Assert(m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
7261 |
|
---|
7262 | if (i_getChildren().size() != 0)
|
---|
7263 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
7264 | tr("Cannot close medium '%s' because it has %d child media"),
|
---|
7265 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
7266 |
|
---|
7267 | return S_OK;
|
---|
7268 | }
|
---|
7269 |
|
---|
7270 | /**
|
---|
7271 | * Unregisters this medium with mVirtualBox. Called by close() under the medium tree lock.
|
---|
7272 | *
|
---|
7273 | * @note Caller must have locked the media tree lock for writing!
|
---|
7274 | */
|
---|
7275 | HRESULT Medium::i_unregisterWithVirtualBox()
|
---|
7276 | {
|
---|
7277 | /* Note that we need to de-associate ourselves from the parent to let
|
---|
7278 | * VirtualBox::i_unregisterMedium() properly save the registry */
|
---|
7279 |
|
---|
7280 | /* we modify m->pParent and access children */
|
---|
7281 | Assert(m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
7282 |
|
---|
7283 | Medium *pParentBackup = m->pParent;
|
---|
7284 | AssertReturn(i_getChildren().size() == 0, E_FAIL);
|
---|
7285 | if (m->pParent)
|
---|
7286 | i_deparent();
|
---|
7287 |
|
---|
7288 | HRESULT rc = m->pVirtualBox->i_unregisterMedium(this);
|
---|
7289 | if (FAILED(rc))
|
---|
7290 | {
|
---|
7291 | if (pParentBackup)
|
---|
7292 | {
|
---|
7293 | // re-associate with the parent as we are still relatives in the registry
|
---|
7294 | i_setParent(pParentBackup);
|
---|
7295 | }
|
---|
7296 | }
|
---|
7297 |
|
---|
7298 | return rc;
|
---|
7299 | }
|
---|
7300 |
|
---|
7301 | /**
|
---|
7302 | * Like SetProperty but do not trigger a settings store. Only for internal use!
|
---|
7303 | */
|
---|
7304 | HRESULT Medium::i_setPropertyDirect(const Utf8Str &aName, const Utf8Str &aValue)
|
---|
7305 | {
|
---|
7306 | AutoCaller autoCaller(this);
|
---|
7307 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
7308 |
|
---|
7309 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7310 |
|
---|
7311 | switch (m->state)
|
---|
7312 | {
|
---|
7313 | case MediumState_Created:
|
---|
7314 | case MediumState_Inaccessible:
|
---|
7315 | break;
|
---|
7316 | default:
|
---|
7317 | return i_setStateError();
|
---|
7318 | }
|
---|
7319 |
|
---|
7320 | m->mapProperties[aName] = aValue;
|
---|
7321 |
|
---|
7322 | return S_OK;
|
---|
7323 | }
|
---|
7324 |
|
---|
7325 | /**
|
---|
7326 | * Sets the extended error info according to the current media state.
|
---|
7327 | *
|
---|
7328 | * @note Must be called from under this object's write or read lock.
|
---|
7329 | */
|
---|
7330 | HRESULT Medium::i_setStateError()
|
---|
7331 | {
|
---|
7332 | HRESULT rc = E_FAIL;
|
---|
7333 |
|
---|
7334 | switch (m->state)
|
---|
7335 | {
|
---|
7336 | case MediumState_NotCreated:
|
---|
7337 | {
|
---|
7338 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7339 | tr("Storage for the medium '%s' is not created"),
|
---|
7340 | m->strLocationFull.c_str());
|
---|
7341 | break;
|
---|
7342 | }
|
---|
7343 | case MediumState_Created:
|
---|
7344 | {
|
---|
7345 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7346 | tr("Storage for the medium '%s' is already created"),
|
---|
7347 | m->strLocationFull.c_str());
|
---|
7348 | break;
|
---|
7349 | }
|
---|
7350 | case MediumState_LockedRead:
|
---|
7351 | {
|
---|
7352 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7353 | tr("Medium '%s' is locked for reading by another task"),
|
---|
7354 | m->strLocationFull.c_str());
|
---|
7355 | break;
|
---|
7356 | }
|
---|
7357 | case MediumState_LockedWrite:
|
---|
7358 | {
|
---|
7359 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7360 | tr("Medium '%s' is locked for writing by another task"),
|
---|
7361 | m->strLocationFull.c_str());
|
---|
7362 | break;
|
---|
7363 | }
|
---|
7364 | case MediumState_Inaccessible:
|
---|
7365 | {
|
---|
7366 | /* be in sync with Console::powerUpThread() */
|
---|
7367 | if (!m->strLastAccessError.isEmpty())
|
---|
7368 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7369 | tr("Medium '%s' is not accessible. %s"),
|
---|
7370 | m->strLocationFull.c_str(), m->strLastAccessError.c_str());
|
---|
7371 | else
|
---|
7372 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7373 | tr("Medium '%s' is not accessible"),
|
---|
7374 | m->strLocationFull.c_str());
|
---|
7375 | break;
|
---|
7376 | }
|
---|
7377 | case MediumState_Creating:
|
---|
7378 | {
|
---|
7379 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7380 | tr("Storage for the medium '%s' is being created"),
|
---|
7381 | m->strLocationFull.c_str());
|
---|
7382 | break;
|
---|
7383 | }
|
---|
7384 | case MediumState_Deleting:
|
---|
7385 | {
|
---|
7386 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7387 | tr("Storage for the medium '%s' is being deleted"),
|
---|
7388 | m->strLocationFull.c_str());
|
---|
7389 | break;
|
---|
7390 | }
|
---|
7391 | default:
|
---|
7392 | {
|
---|
7393 | AssertFailed();
|
---|
7394 | break;
|
---|
7395 | }
|
---|
7396 | }
|
---|
7397 |
|
---|
7398 | return rc;
|
---|
7399 | }
|
---|
7400 |
|
---|
7401 | /**
|
---|
7402 | * Sets the value of m->strLocationFull. The given location must be a fully
|
---|
7403 | * qualified path; relative paths are not supported here.
|
---|
7404 | *
|
---|
7405 | * As a special exception, if the specified location is a file path that ends with '/'
|
---|
7406 | * then the file name part will be generated by this method automatically in the format
|
---|
7407 | * '{\<uuid\>}.\<ext\>' where \<uuid\> is a fresh UUID that this method will generate
|
---|
7408 | * and assign to this medium, and \<ext\> is the default extension for this
|
---|
7409 | * medium's storage format. Note that this procedure requires the media state to
|
---|
7410 | * be NotCreated and will return a failure otherwise.
|
---|
7411 | *
|
---|
7412 | * @param aLocation Location of the storage unit. If the location is a FS-path,
|
---|
7413 | * then it can be relative to the VirtualBox home directory.
|
---|
7414 | * @param aFormat Optional fallback format if it is an import and the format
|
---|
7415 | * cannot be determined.
|
---|
7416 | *
|
---|
7417 | * @note Must be called from under this object's write lock.
|
---|
7418 | */
|
---|
7419 | HRESULT Medium::i_setLocation(const Utf8Str &aLocation,
|
---|
7420 | const Utf8Str &aFormat /* = Utf8Str::Empty */)
|
---|
7421 | {
|
---|
7422 | AssertReturn(!aLocation.isEmpty(), E_FAIL);
|
---|
7423 |
|
---|
7424 | AutoCaller autoCaller(this);
|
---|
7425 | AssertComRCReturnRC(autoCaller.rc());
|
---|
7426 |
|
---|
7427 | /* formatObj may be null only when initializing from an existing path and
|
---|
7428 | * no format is known yet */
|
---|
7429 | AssertReturn( (!m->strFormat.isEmpty() && !m->formatObj.isNull())
|
---|
7430 | || ( getObjectState().getState() == ObjectState::InInit
|
---|
7431 | && m->state != MediumState_NotCreated
|
---|
7432 | && m->id.isZero()
|
---|
7433 | && m->strFormat.isEmpty()
|
---|
7434 | && m->formatObj.isNull()),
|
---|
7435 | E_FAIL);
|
---|
7436 |
|
---|
7437 | /* are we dealing with a new medium constructed using the existing
|
---|
7438 | * location? */
|
---|
7439 | bool isImport = m->strFormat.isEmpty();
|
---|
7440 |
|
---|
7441 | if ( isImport
|
---|
7442 | || ( (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
7443 | && !m->hostDrive))
|
---|
7444 | {
|
---|
7445 | Guid id;
|
---|
7446 |
|
---|
7447 | Utf8Str locationFull(aLocation);
|
---|
7448 |
|
---|
7449 | if (m->state == MediumState_NotCreated)
|
---|
7450 | {
|
---|
7451 | /* must be a file (formatObj must be already known) */
|
---|
7452 | Assert(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File);
|
---|
7453 |
|
---|
7454 | if (RTPathFilename(aLocation.c_str()) == NULL)
|
---|
7455 | {
|
---|
7456 | /* no file name is given (either an empty string or ends with a
|
---|
7457 | * slash), generate a new UUID + file name if the state allows
|
---|
7458 | * this */
|
---|
7459 |
|
---|
7460 | ComAssertMsgRet(!m->formatObj->i_getFileExtensions().empty(),
|
---|
7461 | ("Must be at least one extension if it is MediumFormatCapabilities_File\n"),
|
---|
7462 | E_FAIL);
|
---|
7463 |
|
---|
7464 | Utf8Str strExt = m->formatObj->i_getFileExtensions().front();
|
---|
7465 | ComAssertMsgRet(!strExt.isEmpty(),
|
---|
7466 | ("Default extension must not be empty\n"),
|
---|
7467 | E_FAIL);
|
---|
7468 |
|
---|
7469 | id.create();
|
---|
7470 |
|
---|
7471 | locationFull = Utf8StrFmt("%s{%RTuuid}.%s",
|
---|
7472 | aLocation.c_str(), id.raw(), strExt.c_str());
|
---|
7473 | }
|
---|
7474 | }
|
---|
7475 |
|
---|
7476 | // we must always have full paths now (if it refers to a file)
|
---|
7477 | if ( ( m->formatObj.isNull()
|
---|
7478 | || m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
7479 | && !RTPathStartsWithRoot(locationFull.c_str()))
|
---|
7480 | return setError(VBOX_E_FILE_ERROR,
|
---|
7481 | tr("The given path '%s' is not fully qualified"),
|
---|
7482 | locationFull.c_str());
|
---|
7483 |
|
---|
7484 | /* detect the backend from the storage unit if importing */
|
---|
7485 | if (isImport)
|
---|
7486 | {
|
---|
7487 | VDTYPE enmType = VDTYPE_INVALID;
|
---|
7488 | char *backendName = NULL;
|
---|
7489 |
|
---|
7490 | int vrc = VINF_SUCCESS;
|
---|
7491 |
|
---|
7492 | /* is it a file? */
|
---|
7493 | {
|
---|
7494 | RTFILE file;
|
---|
7495 | vrc = RTFileOpen(&file, locationFull.c_str(), RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
7496 | if (RT_SUCCESS(vrc))
|
---|
7497 | RTFileClose(file);
|
---|
7498 | }
|
---|
7499 | if (RT_SUCCESS(vrc))
|
---|
7500 | {
|
---|
7501 | vrc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
|
---|
7502 | locationFull.c_str(), &backendName, &enmType);
|
---|
7503 | }
|
---|
7504 | else if ( vrc != VERR_FILE_NOT_FOUND
|
---|
7505 | && vrc != VERR_PATH_NOT_FOUND
|
---|
7506 | && vrc != VERR_ACCESS_DENIED
|
---|
7507 | && locationFull != aLocation)
|
---|
7508 | {
|
---|
7509 | /* assume it's not a file, restore the original location */
|
---|
7510 | locationFull = aLocation;
|
---|
7511 | vrc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
|
---|
7512 | locationFull.c_str(), &backendName, &enmType);
|
---|
7513 | }
|
---|
7514 |
|
---|
7515 | if (RT_FAILURE(vrc))
|
---|
7516 | {
|
---|
7517 | if (vrc == VERR_ACCESS_DENIED)
|
---|
7518 | return setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
7519 | tr("Permission problem accessing the file for the medium '%s' (%Rrc)"),
|
---|
7520 | locationFull.c_str(), vrc);
|
---|
7521 | else if (vrc == VERR_FILE_NOT_FOUND || vrc == VERR_PATH_NOT_FOUND)
|
---|
7522 | return setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
7523 | tr("Could not find file for the medium '%s' (%Rrc)"),
|
---|
7524 | locationFull.c_str(), vrc);
|
---|
7525 | else if (aFormat.isEmpty())
|
---|
7526 | return setErrorBoth(VBOX_E_IPRT_ERROR, vrc,
|
---|
7527 | tr("Could not get the storage format of the medium '%s' (%Rrc)"),
|
---|
7528 | locationFull.c_str(), vrc);
|
---|
7529 | else
|
---|
7530 | {
|
---|
7531 | HRESULT rc = i_setFormat(aFormat);
|
---|
7532 | /* setFormat() must not fail since we've just used the backend so
|
---|
7533 | * the format object must be there */
|
---|
7534 | AssertComRCReturnRC(rc);
|
---|
7535 | }
|
---|
7536 | }
|
---|
7537 | else if ( enmType == VDTYPE_INVALID
|
---|
7538 | || m->devType != i_convertToDeviceType(enmType))
|
---|
7539 | {
|
---|
7540 | /*
|
---|
7541 | * The user tried to use a image as a device which is not supported
|
---|
7542 | * by the backend.
|
---|
7543 | */
|
---|
7544 | return setError(E_FAIL,
|
---|
7545 | tr("The medium '%s' can't be used as the requested device type"),
|
---|
7546 | locationFull.c_str());
|
---|
7547 | }
|
---|
7548 | else
|
---|
7549 | {
|
---|
7550 | ComAssertRet(backendName != NULL && *backendName != '\0', E_FAIL);
|
---|
7551 |
|
---|
7552 | HRESULT rc = i_setFormat(backendName);
|
---|
7553 | RTStrFree(backendName);
|
---|
7554 |
|
---|
7555 | /* setFormat() must not fail since we've just used the backend so
|
---|
7556 | * the format object must be there */
|
---|
7557 | AssertComRCReturnRC(rc);
|
---|
7558 | }
|
---|
7559 | }
|
---|
7560 |
|
---|
7561 | m->strLocationFull = locationFull;
|
---|
7562 |
|
---|
7563 | /* is it still a file? */
|
---|
7564 | if ( (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
7565 | && (m->state == MediumState_NotCreated)
|
---|
7566 | )
|
---|
7567 | /* assign a new UUID (this UUID will be used when calling
|
---|
7568 | * VDCreateBase/VDCreateDiff as a wanted UUID). Note that we
|
---|
7569 | * also do that if we didn't generate it to make sure it is
|
---|
7570 | * either generated by us or reset to null */
|
---|
7571 | unconst(m->id) = id;
|
---|
7572 | }
|
---|
7573 | else
|
---|
7574 | m->strLocationFull = aLocation;
|
---|
7575 |
|
---|
7576 | return S_OK;
|
---|
7577 | }
|
---|
7578 |
|
---|
7579 | /**
|
---|
7580 | * Checks that the format ID is valid and sets it on success.
|
---|
7581 | *
|
---|
7582 | * Note that this method will caller-reference the format object on success!
|
---|
7583 | * This reference must be released somewhere to let the MediumFormat object be
|
---|
7584 | * uninitialized.
|
---|
7585 | *
|
---|
7586 | * @note Must be called from under this object's write lock.
|
---|
7587 | */
|
---|
7588 | HRESULT Medium::i_setFormat(const Utf8Str &aFormat)
|
---|
7589 | {
|
---|
7590 | /* get the format object first */
|
---|
7591 | {
|
---|
7592 | SystemProperties *pSysProps = m->pVirtualBox->i_getSystemProperties();
|
---|
7593 | AutoReadLock propsLock(pSysProps COMMA_LOCKVAL_SRC_POS);
|
---|
7594 |
|
---|
7595 | unconst(m->formatObj) = pSysProps->i_mediumFormat(aFormat);
|
---|
7596 | if (m->formatObj.isNull())
|
---|
7597 | return setError(E_INVALIDARG,
|
---|
7598 | tr("Invalid medium storage format '%s'"),
|
---|
7599 | aFormat.c_str());
|
---|
7600 |
|
---|
7601 | /* get properties (preinsert them as keys in the map). Note that the
|
---|
7602 | * map doesn't grow over the object life time since the set of
|
---|
7603 | * properties is meant to be constant. */
|
---|
7604 |
|
---|
7605 | Assert(m->mapProperties.empty());
|
---|
7606 |
|
---|
7607 | for (MediumFormat::PropertyArray::const_iterator it = m->formatObj->i_getProperties().begin();
|
---|
7608 | it != m->formatObj->i_getProperties().end();
|
---|
7609 | ++it)
|
---|
7610 | {
|
---|
7611 | m->mapProperties.insert(std::make_pair(it->strName, Utf8Str::Empty));
|
---|
7612 | }
|
---|
7613 | }
|
---|
7614 |
|
---|
7615 | unconst(m->strFormat) = aFormat;
|
---|
7616 |
|
---|
7617 | return S_OK;
|
---|
7618 | }
|
---|
7619 |
|
---|
7620 | /**
|
---|
7621 | * Converts the Medium device type to the VD type.
|
---|
7622 | */
|
---|
7623 | VDTYPE Medium::i_convertDeviceType()
|
---|
7624 | {
|
---|
7625 | VDTYPE enmType;
|
---|
7626 |
|
---|
7627 | switch (m->devType)
|
---|
7628 | {
|
---|
7629 | case DeviceType_HardDisk:
|
---|
7630 | enmType = VDTYPE_HDD;
|
---|
7631 | break;
|
---|
7632 | case DeviceType_DVD:
|
---|
7633 | enmType = VDTYPE_OPTICAL_DISC;
|
---|
7634 | break;
|
---|
7635 | case DeviceType_Floppy:
|
---|
7636 | enmType = VDTYPE_FLOPPY;
|
---|
7637 | break;
|
---|
7638 | default:
|
---|
7639 | ComAssertFailedRet(VDTYPE_INVALID);
|
---|
7640 | }
|
---|
7641 |
|
---|
7642 | return enmType;
|
---|
7643 | }
|
---|
7644 |
|
---|
7645 | /**
|
---|
7646 | * Converts from the VD type to the medium type.
|
---|
7647 | */
|
---|
7648 | DeviceType_T Medium::i_convertToDeviceType(VDTYPE enmType)
|
---|
7649 | {
|
---|
7650 | DeviceType_T devType;
|
---|
7651 |
|
---|
7652 | switch (enmType)
|
---|
7653 | {
|
---|
7654 | case VDTYPE_HDD:
|
---|
7655 | devType = DeviceType_HardDisk;
|
---|
7656 | break;
|
---|
7657 | case VDTYPE_OPTICAL_DISC:
|
---|
7658 | devType = DeviceType_DVD;
|
---|
7659 | break;
|
---|
7660 | case VDTYPE_FLOPPY:
|
---|
7661 | devType = DeviceType_Floppy;
|
---|
7662 | break;
|
---|
7663 | default:
|
---|
7664 | ComAssertFailedRet(DeviceType_Null);
|
---|
7665 | }
|
---|
7666 |
|
---|
7667 | return devType;
|
---|
7668 | }
|
---|
7669 |
|
---|
7670 | /**
|
---|
7671 | * Internal method which checks whether a property name is for a filter plugin.
|
---|
7672 | */
|
---|
7673 | bool Medium::i_isPropertyForFilter(const com::Utf8Str &aName)
|
---|
7674 | {
|
---|
7675 | /* If the name contains "/" use the part before as a filter name and lookup the filter. */
|
---|
7676 | size_t offSlash;
|
---|
7677 | if ((offSlash = aName.find("/", 0)) != aName.npos)
|
---|
7678 | {
|
---|
7679 | com::Utf8Str strFilter;
|
---|
7680 | com::Utf8Str strKey;
|
---|
7681 |
|
---|
7682 | HRESULT rc = strFilter.assignEx(aName, 0, offSlash);
|
---|
7683 | if (FAILED(rc))
|
---|
7684 | return false;
|
---|
7685 |
|
---|
7686 | rc = strKey.assignEx(aName, offSlash + 1, aName.length() - offSlash - 1); /* Skip slash */
|
---|
7687 | if (FAILED(rc))
|
---|
7688 | return false;
|
---|
7689 |
|
---|
7690 | VDFILTERINFO FilterInfo;
|
---|
7691 | int vrc = VDFilterInfoOne(strFilter.c_str(), &FilterInfo);
|
---|
7692 | if (RT_SUCCESS(vrc))
|
---|
7693 | {
|
---|
7694 | /* Check that the property exists. */
|
---|
7695 | PCVDCONFIGINFO paConfig = FilterInfo.paConfigInfo;
|
---|
7696 | while (paConfig->pszKey)
|
---|
7697 | {
|
---|
7698 | if (strKey.equals(paConfig->pszKey))
|
---|
7699 | return true;
|
---|
7700 | paConfig++;
|
---|
7701 | }
|
---|
7702 | }
|
---|
7703 | }
|
---|
7704 |
|
---|
7705 | return false;
|
---|
7706 | }
|
---|
7707 |
|
---|
7708 | /**
|
---|
7709 | * Returns the last error message collected by the i_vdErrorCall callback and
|
---|
7710 | * resets it.
|
---|
7711 | *
|
---|
7712 | * The error message is returned prepended with a dot and a space, like this:
|
---|
7713 | * <code>
|
---|
7714 | * ". <error_text> (%Rrc)"
|
---|
7715 | * </code>
|
---|
7716 | * to make it easily appendable to a more general error message. The @c %Rrc
|
---|
7717 | * format string is given @a aVRC as an argument.
|
---|
7718 | *
|
---|
7719 | * If there is no last error message collected by i_vdErrorCall or if it is a
|
---|
7720 | * null or empty string, then this function returns the following text:
|
---|
7721 | * <code>
|
---|
7722 | * " (%Rrc)"
|
---|
7723 | * </code>
|
---|
7724 | *
|
---|
7725 | * @note Doesn't do any object locking; it is assumed that the caller makes sure
|
---|
7726 | * the callback isn't called by more than one thread at a time.
|
---|
7727 | *
|
---|
7728 | * @param aVRC VBox error code to use when no error message is provided.
|
---|
7729 | */
|
---|
7730 | Utf8Str Medium::i_vdError(int aVRC)
|
---|
7731 | {
|
---|
7732 | Utf8Str error;
|
---|
7733 |
|
---|
7734 | if (m->vdError.isEmpty())
|
---|
7735 | error = Utf8StrFmt(" (%Rrc)", aVRC);
|
---|
7736 | else
|
---|
7737 | error = Utf8StrFmt(".\n%s", m->vdError.c_str());
|
---|
7738 |
|
---|
7739 | m->vdError.setNull();
|
---|
7740 |
|
---|
7741 | return error;
|
---|
7742 | }
|
---|
7743 |
|
---|
7744 | /**
|
---|
7745 | * Error message callback.
|
---|
7746 | *
|
---|
7747 | * Puts the reported error message to the m->vdError field.
|
---|
7748 | *
|
---|
7749 | * @note Doesn't do any object locking; it is assumed that the caller makes sure
|
---|
7750 | * the callback isn't called by more than one thread at a time.
|
---|
7751 | *
|
---|
7752 | * @param pvUser The opaque data passed on container creation.
|
---|
7753 | * @param rc The VBox error code.
|
---|
7754 | * @param SRC_POS Use RT_SRC_POS.
|
---|
7755 | * @param pszFormat Error message format string.
|
---|
7756 | * @param va Error message arguments.
|
---|
7757 | */
|
---|
7758 | /*static*/
|
---|
7759 | DECLCALLBACK(void) Medium::i_vdErrorCall(void *pvUser, int rc, RT_SRC_POS_DECL,
|
---|
7760 | const char *pszFormat, va_list va)
|
---|
7761 | {
|
---|
7762 | NOREF(pszFile); NOREF(iLine); NOREF(pszFunction); /* RT_SRC_POS_DECL */
|
---|
7763 |
|
---|
7764 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
7765 | AssertReturnVoid(that != NULL);
|
---|
7766 |
|
---|
7767 | if (that->m->vdError.isEmpty())
|
---|
7768 | that->m->vdError =
|
---|
7769 | Utf8StrFmt("%s (%Rrc)", Utf8Str(pszFormat, va).c_str(), rc);
|
---|
7770 | else
|
---|
7771 | that->m->vdError =
|
---|
7772 | Utf8StrFmt("%s.\n%s (%Rrc)", that->m->vdError.c_str(),
|
---|
7773 | Utf8Str(pszFormat, va).c_str(), rc);
|
---|
7774 | }
|
---|
7775 |
|
---|
7776 | /* static */
|
---|
7777 | DECLCALLBACK(bool) Medium::i_vdConfigAreKeysValid(void *pvUser,
|
---|
7778 | const char * /* pszzValid */)
|
---|
7779 | {
|
---|
7780 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
7781 | AssertReturn(that != NULL, false);
|
---|
7782 |
|
---|
7783 | /* we always return true since the only keys we have are those found in
|
---|
7784 | * VDBACKENDINFO */
|
---|
7785 | return true;
|
---|
7786 | }
|
---|
7787 |
|
---|
7788 | /* static */
|
---|
7789 | DECLCALLBACK(int) Medium::i_vdConfigQuerySize(void *pvUser,
|
---|
7790 | const char *pszName,
|
---|
7791 | size_t *pcbValue)
|
---|
7792 | {
|
---|
7793 | AssertReturn(VALID_PTR(pcbValue), VERR_INVALID_POINTER);
|
---|
7794 |
|
---|
7795 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
7796 | AssertReturn(that != NULL, VERR_GENERAL_FAILURE);
|
---|
7797 |
|
---|
7798 | settings::StringsMap::const_iterator it = that->m->mapProperties.find(Utf8Str(pszName));
|
---|
7799 | if (it == that->m->mapProperties.end())
|
---|
7800 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7801 |
|
---|
7802 | /* we interpret null values as "no value" in Medium */
|
---|
7803 | if (it->second.isEmpty())
|
---|
7804 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7805 |
|
---|
7806 | *pcbValue = it->second.length() + 1 /* include terminator */;
|
---|
7807 |
|
---|
7808 | return VINF_SUCCESS;
|
---|
7809 | }
|
---|
7810 |
|
---|
7811 | /* static */
|
---|
7812 | DECLCALLBACK(int) Medium::i_vdConfigQuery(void *pvUser,
|
---|
7813 | const char *pszName,
|
---|
7814 | char *pszValue,
|
---|
7815 | size_t cchValue)
|
---|
7816 | {
|
---|
7817 | AssertReturn(VALID_PTR(pszValue), VERR_INVALID_POINTER);
|
---|
7818 |
|
---|
7819 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
7820 | AssertReturn(that != NULL, VERR_GENERAL_FAILURE);
|
---|
7821 |
|
---|
7822 | settings::StringsMap::const_iterator it = that->m->mapProperties.find(Utf8Str(pszName));
|
---|
7823 | if (it == that->m->mapProperties.end())
|
---|
7824 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7825 |
|
---|
7826 | /* we interpret null values as "no value" in Medium */
|
---|
7827 | if (it->second.isEmpty())
|
---|
7828 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7829 |
|
---|
7830 | const Utf8Str &value = it->second;
|
---|
7831 | if (value.length() >= cchValue)
|
---|
7832 | return VERR_CFGM_NOT_ENOUGH_SPACE;
|
---|
7833 |
|
---|
7834 | memcpy(pszValue, value.c_str(), value.length() + 1);
|
---|
7835 |
|
---|
7836 | return VINF_SUCCESS;
|
---|
7837 | }
|
---|
7838 |
|
---|
7839 | DECLCALLBACK(int) Medium::i_vdTcpSocketCreate(uint32_t fFlags, PVDSOCKET pSock)
|
---|
7840 | {
|
---|
7841 | PVDSOCKETINT pSocketInt = NULL;
|
---|
7842 |
|
---|
7843 | if ((fFlags & VD_INTERFACETCPNET_CONNECT_EXTENDED_SELECT) != 0)
|
---|
7844 | return VERR_NOT_SUPPORTED;
|
---|
7845 |
|
---|
7846 | pSocketInt = (PVDSOCKETINT)RTMemAllocZ(sizeof(VDSOCKETINT));
|
---|
7847 | if (!pSocketInt)
|
---|
7848 | return VERR_NO_MEMORY;
|
---|
7849 |
|
---|
7850 | pSocketInt->hSocket = NIL_RTSOCKET;
|
---|
7851 | *pSock = pSocketInt;
|
---|
7852 | return VINF_SUCCESS;
|
---|
7853 | }
|
---|
7854 |
|
---|
7855 | DECLCALLBACK(int) Medium::i_vdTcpSocketDestroy(VDSOCKET Sock)
|
---|
7856 | {
|
---|
7857 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7858 |
|
---|
7859 | if (pSocketInt->hSocket != NIL_RTSOCKET)
|
---|
7860 | RTTcpClientCloseEx(pSocketInt->hSocket, false /*fGracefulShutdown*/);
|
---|
7861 |
|
---|
7862 | RTMemFree(pSocketInt);
|
---|
7863 |
|
---|
7864 | return VINF_SUCCESS;
|
---|
7865 | }
|
---|
7866 |
|
---|
7867 | DECLCALLBACK(int) Medium::i_vdTcpClientConnect(VDSOCKET Sock, const char *pszAddress, uint32_t uPort,
|
---|
7868 | RTMSINTERVAL cMillies)
|
---|
7869 | {
|
---|
7870 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7871 |
|
---|
7872 | return RTTcpClientConnectEx(pszAddress, uPort, &pSocketInt->hSocket, cMillies, NULL);
|
---|
7873 | }
|
---|
7874 |
|
---|
7875 | DECLCALLBACK(int) Medium::i_vdTcpClientClose(VDSOCKET Sock)
|
---|
7876 | {
|
---|
7877 | int rc = VINF_SUCCESS;
|
---|
7878 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7879 |
|
---|
7880 | rc = RTTcpClientCloseEx(pSocketInt->hSocket, false /*fGracefulShutdown*/);
|
---|
7881 | pSocketInt->hSocket = NIL_RTSOCKET;
|
---|
7882 | return rc;
|
---|
7883 | }
|
---|
7884 |
|
---|
7885 | DECLCALLBACK(bool) Medium::i_vdTcpIsClientConnected(VDSOCKET Sock)
|
---|
7886 | {
|
---|
7887 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7888 | return pSocketInt->hSocket != NIL_RTSOCKET;
|
---|
7889 | }
|
---|
7890 |
|
---|
7891 | DECLCALLBACK(int) Medium::i_vdTcpSelectOne(VDSOCKET Sock, RTMSINTERVAL cMillies)
|
---|
7892 | {
|
---|
7893 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7894 | return RTTcpSelectOne(pSocketInt->hSocket, cMillies);
|
---|
7895 | }
|
---|
7896 |
|
---|
7897 | DECLCALLBACK(int) Medium::i_vdTcpRead(VDSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead)
|
---|
7898 | {
|
---|
7899 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7900 | return RTTcpRead(pSocketInt->hSocket, pvBuffer, cbBuffer, pcbRead);
|
---|
7901 | }
|
---|
7902 |
|
---|
7903 | DECLCALLBACK(int) Medium::i_vdTcpWrite(VDSOCKET Sock, const void *pvBuffer, size_t cbBuffer)
|
---|
7904 | {
|
---|
7905 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7906 | return RTTcpWrite(pSocketInt->hSocket, pvBuffer, cbBuffer);
|
---|
7907 | }
|
---|
7908 |
|
---|
7909 | DECLCALLBACK(int) Medium::i_vdTcpSgWrite(VDSOCKET Sock, PCRTSGBUF pSgBuf)
|
---|
7910 | {
|
---|
7911 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7912 | return RTTcpSgWrite(pSocketInt->hSocket, pSgBuf);
|
---|
7913 | }
|
---|
7914 |
|
---|
7915 | DECLCALLBACK(int) Medium::i_vdTcpFlush(VDSOCKET Sock)
|
---|
7916 | {
|
---|
7917 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7918 | return RTTcpFlush(pSocketInt->hSocket);
|
---|
7919 | }
|
---|
7920 |
|
---|
7921 | DECLCALLBACK(int) Medium::i_vdTcpSetSendCoalescing(VDSOCKET Sock, bool fEnable)
|
---|
7922 | {
|
---|
7923 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7924 | return RTTcpSetSendCoalescing(pSocketInt->hSocket, fEnable);
|
---|
7925 | }
|
---|
7926 |
|
---|
7927 | DECLCALLBACK(int) Medium::i_vdTcpGetLocalAddress(VDSOCKET Sock, PRTNETADDR pAddr)
|
---|
7928 | {
|
---|
7929 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7930 | return RTTcpGetLocalAddress(pSocketInt->hSocket, pAddr);
|
---|
7931 | }
|
---|
7932 |
|
---|
7933 | DECLCALLBACK(int) Medium::i_vdTcpGetPeerAddress(VDSOCKET Sock, PRTNETADDR pAddr)
|
---|
7934 | {
|
---|
7935 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7936 | return RTTcpGetPeerAddress(pSocketInt->hSocket, pAddr);
|
---|
7937 | }
|
---|
7938 |
|
---|
7939 | DECLCALLBACK(bool) Medium::i_vdCryptoConfigAreKeysValid(void *pvUser, const char *pszzValid)
|
---|
7940 | {
|
---|
7941 | /* Just return always true here. */
|
---|
7942 | NOREF(pvUser);
|
---|
7943 | NOREF(pszzValid);
|
---|
7944 | return true;
|
---|
7945 | }
|
---|
7946 |
|
---|
7947 | DECLCALLBACK(int) Medium::i_vdCryptoConfigQuerySize(void *pvUser, const char *pszName, size_t *pcbValue)
|
---|
7948 | {
|
---|
7949 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
7950 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7951 | AssertReturn(VALID_PTR(pcbValue), VERR_INVALID_POINTER);
|
---|
7952 |
|
---|
7953 | size_t cbValue = 0;
|
---|
7954 | if (!strcmp(pszName, "Algorithm"))
|
---|
7955 | cbValue = strlen(pSettings->pszCipher) + 1;
|
---|
7956 | else if (!strcmp(pszName, "KeyId"))
|
---|
7957 | cbValue = sizeof("irrelevant");
|
---|
7958 | else if (!strcmp(pszName, "KeyStore"))
|
---|
7959 | {
|
---|
7960 | if (!pSettings->pszKeyStoreLoad)
|
---|
7961 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7962 | cbValue = strlen(pSettings->pszKeyStoreLoad) + 1;
|
---|
7963 | }
|
---|
7964 | else if (!strcmp(pszName, "CreateKeyStore"))
|
---|
7965 | cbValue = 2; /* Single digit + terminator. */
|
---|
7966 | else
|
---|
7967 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7968 |
|
---|
7969 | *pcbValue = cbValue + 1 /* include terminator */;
|
---|
7970 |
|
---|
7971 | return VINF_SUCCESS;
|
---|
7972 | }
|
---|
7973 |
|
---|
7974 | DECLCALLBACK(int) Medium::i_vdCryptoConfigQuery(void *pvUser, const char *pszName,
|
---|
7975 | char *pszValue, size_t cchValue)
|
---|
7976 | {
|
---|
7977 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
7978 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7979 | AssertReturn(VALID_PTR(pszValue), VERR_INVALID_POINTER);
|
---|
7980 |
|
---|
7981 | const char *psz = NULL;
|
---|
7982 | if (!strcmp(pszName, "Algorithm"))
|
---|
7983 | psz = pSettings->pszCipher;
|
---|
7984 | else if (!strcmp(pszName, "KeyId"))
|
---|
7985 | psz = "irrelevant";
|
---|
7986 | else if (!strcmp(pszName, "KeyStore"))
|
---|
7987 | psz = pSettings->pszKeyStoreLoad;
|
---|
7988 | else if (!strcmp(pszName, "CreateKeyStore"))
|
---|
7989 | {
|
---|
7990 | if (pSettings->fCreateKeyStore)
|
---|
7991 | psz = "1";
|
---|
7992 | else
|
---|
7993 | psz = "0";
|
---|
7994 | }
|
---|
7995 | else
|
---|
7996 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7997 |
|
---|
7998 | size_t cch = strlen(psz);
|
---|
7999 | if (cch >= cchValue)
|
---|
8000 | return VERR_CFGM_NOT_ENOUGH_SPACE;
|
---|
8001 |
|
---|
8002 | memcpy(pszValue, psz, cch + 1);
|
---|
8003 | return VINF_SUCCESS;
|
---|
8004 | }
|
---|
8005 |
|
---|
8006 | DECLCALLBACK(int) Medium::i_vdCryptoKeyRetain(void *pvUser, const char *pszId,
|
---|
8007 | const uint8_t **ppbKey, size_t *pcbKey)
|
---|
8008 | {
|
---|
8009 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8010 | NOREF(pszId);
|
---|
8011 | NOREF(ppbKey);
|
---|
8012 | NOREF(pcbKey);
|
---|
8013 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8014 | AssertMsgFailedReturn(("This method should not be called here!\n"), VERR_INVALID_STATE);
|
---|
8015 | }
|
---|
8016 |
|
---|
8017 | DECLCALLBACK(int) Medium::i_vdCryptoKeyRelease(void *pvUser, const char *pszId)
|
---|
8018 | {
|
---|
8019 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8020 | NOREF(pszId);
|
---|
8021 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8022 | AssertMsgFailedReturn(("This method should not be called here!\n"), VERR_INVALID_STATE);
|
---|
8023 | }
|
---|
8024 |
|
---|
8025 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStorePasswordRetain(void *pvUser, const char *pszId, const char **ppszPassword)
|
---|
8026 | {
|
---|
8027 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8028 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8029 |
|
---|
8030 | NOREF(pszId);
|
---|
8031 | *ppszPassword = pSettings->pszPassword;
|
---|
8032 | return VINF_SUCCESS;
|
---|
8033 | }
|
---|
8034 |
|
---|
8035 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStorePasswordRelease(void *pvUser, const char *pszId)
|
---|
8036 | {
|
---|
8037 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8038 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8039 | NOREF(pszId);
|
---|
8040 | return VINF_SUCCESS;
|
---|
8041 | }
|
---|
8042 |
|
---|
8043 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStoreSave(void *pvUser, const void *pvKeyStore, size_t cbKeyStore)
|
---|
8044 | {
|
---|
8045 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8046 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8047 |
|
---|
8048 | pSettings->pszKeyStore = (char *)RTMemAllocZ(cbKeyStore);
|
---|
8049 | if (!pSettings->pszKeyStore)
|
---|
8050 | return VERR_NO_MEMORY;
|
---|
8051 |
|
---|
8052 | memcpy(pSettings->pszKeyStore, pvKeyStore, cbKeyStore);
|
---|
8053 | return VINF_SUCCESS;
|
---|
8054 | }
|
---|
8055 |
|
---|
8056 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStoreReturnParameters(void *pvUser, const char *pszCipher,
|
---|
8057 | const uint8_t *pbDek, size_t cbDek)
|
---|
8058 | {
|
---|
8059 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8060 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8061 |
|
---|
8062 | pSettings->pszCipherReturned = RTStrDup(pszCipher);
|
---|
8063 | pSettings->pbDek = pbDek;
|
---|
8064 | pSettings->cbDek = cbDek;
|
---|
8065 |
|
---|
8066 | return pSettings->pszCipherReturned ? VINF_SUCCESS : VERR_NO_MEMORY;
|
---|
8067 | }
|
---|
8068 |
|
---|
8069 | /**
|
---|
8070 | * Creates a VDISK instance for this medium.
|
---|
8071 | *
|
---|
8072 | * @note Caller should not hold any medium related locks as this method will
|
---|
8073 | * acquire the medium lock for writing and others (VirtualBox).
|
---|
8074 | *
|
---|
8075 | * @returns COM status code.
|
---|
8076 | * @param fWritable Whether to return a writable VDISK instance
|
---|
8077 | * (true) or a read-only one (false).
|
---|
8078 | * @param pKeyStore The key store.
|
---|
8079 | * @param ppHdd Where to return the pointer to the VDISK on
|
---|
8080 | * success.
|
---|
8081 | * @param pMediumLockList The lock list to populate and lock. Caller
|
---|
8082 | * is responsible for calling the destructor or
|
---|
8083 | * MediumLockList::Clear() after destroying
|
---|
8084 | * @a *ppHdd
|
---|
8085 | * @param pCryptoSettings The crypto settings to use for setting up
|
---|
8086 | * decryption/encryption of the VDISK. This object
|
---|
8087 | * must be alive until the VDISK is destroyed!
|
---|
8088 | */
|
---|
8089 | HRESULT Medium::i_openForIO(bool fWritable, SecretKeyStore *pKeyStore, PVDISK *ppHdd, MediumLockList *pMediumLockList,
|
---|
8090 | MediumCryptoFilterSettings *pCryptoSettings)
|
---|
8091 | {
|
---|
8092 | /*
|
---|
8093 | * Create the media lock list and lock the media.
|
---|
8094 | */
|
---|
8095 | HRESULT hrc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
8096 | fWritable ? this : NULL /* pToLockWrite */,
|
---|
8097 | false /* fMediumLockWriteAll */,
|
---|
8098 | NULL,
|
---|
8099 | *pMediumLockList);
|
---|
8100 | if (SUCCEEDED(hrc))
|
---|
8101 | hrc = pMediumLockList->Lock();
|
---|
8102 | if (FAILED(hrc))
|
---|
8103 | return hrc;
|
---|
8104 |
|
---|
8105 | /*
|
---|
8106 | * Get the base medium before write locking this medium.
|
---|
8107 | */
|
---|
8108 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
8109 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8110 |
|
---|
8111 | /*
|
---|
8112 | * Create the VDISK instance.
|
---|
8113 | */
|
---|
8114 | PVDISK pHdd;
|
---|
8115 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pHdd);
|
---|
8116 | AssertRCReturn(vrc, E_FAIL);
|
---|
8117 |
|
---|
8118 | /*
|
---|
8119 | * Goto avoidance using try/catch/throw(HRESULT).
|
---|
8120 | */
|
---|
8121 | try
|
---|
8122 | {
|
---|
8123 | settings::StringsMap::iterator itKeyStore = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
8124 | if (itKeyStore != pBase->m->mapProperties.end())
|
---|
8125 | {
|
---|
8126 | settings::StringsMap::iterator itKeyId = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
8127 |
|
---|
8128 | #ifdef VBOX_WITH_EXTPACK
|
---|
8129 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
8130 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
8131 | {
|
---|
8132 | /* Load the plugin */
|
---|
8133 | Utf8Str strPlugin;
|
---|
8134 | hrc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
8135 | if (SUCCEEDED(hrc))
|
---|
8136 | {
|
---|
8137 | vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
8138 | if (RT_FAILURE(vrc))
|
---|
8139 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
8140 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
8141 | i_vdError(vrc).c_str());
|
---|
8142 | }
|
---|
8143 | else
|
---|
8144 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
8145 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
8146 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
8147 | }
|
---|
8148 | else
|
---|
8149 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
8150 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
8151 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
8152 | #else
|
---|
8153 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
8154 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
8155 | #endif
|
---|
8156 |
|
---|
8157 | if (itKeyId == pBase->m->mapProperties.end())
|
---|
8158 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8159 | tr("Image '%s' is configured for encryption but doesn't has a key identifier set"),
|
---|
8160 | pBase->m->strLocationFull.c_str());
|
---|
8161 |
|
---|
8162 | /* Find the proper secret key in the key store. */
|
---|
8163 | if (!pKeyStore)
|
---|
8164 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8165 | tr("Image '%s' is configured for encryption but there is no key store to retrieve the password from"),
|
---|
8166 | pBase->m->strLocationFull.c_str());
|
---|
8167 |
|
---|
8168 | SecretKey *pKey = NULL;
|
---|
8169 | vrc = pKeyStore->retainSecretKey(itKeyId->second, &pKey);
|
---|
8170 | if (RT_FAILURE(vrc))
|
---|
8171 | throw setErrorBoth(VBOX_E_INVALID_OBJECT_STATE, vrc,
|
---|
8172 | tr("Failed to retrieve the secret key with ID \"%s\" from the store (%Rrc)"),
|
---|
8173 | itKeyId->second.c_str(), vrc);
|
---|
8174 |
|
---|
8175 | i_taskEncryptSettingsSetup(pCryptoSettings, NULL, itKeyStore->second.c_str(), (const char *)pKey->getKeyBuffer(),
|
---|
8176 | false /* fCreateKeyStore */);
|
---|
8177 | vrc = VDFilterAdd(pHdd, "CRYPT", VD_FILTER_FLAGS_DEFAULT, pCryptoSettings->vdFilterIfaces);
|
---|
8178 | pKeyStore->releaseSecretKey(itKeyId->second);
|
---|
8179 | if (vrc == VERR_VD_PASSWORD_INCORRECT)
|
---|
8180 | throw setErrorBoth(VBOX_E_PASSWORD_INCORRECT, vrc, tr("The password to decrypt the image is incorrect"));
|
---|
8181 | if (RT_FAILURE(vrc))
|
---|
8182 | throw setErrorBoth(VBOX_E_INVALID_OBJECT_STATE, vrc, tr("Failed to load the decryption filter: %s"),
|
---|
8183 | i_vdError(vrc).c_str());
|
---|
8184 | }
|
---|
8185 |
|
---|
8186 | /*
|
---|
8187 | * Open all media in the source chain.
|
---|
8188 | */
|
---|
8189 | MediumLockList::Base::const_iterator sourceListBegin = pMediumLockList->GetBegin();
|
---|
8190 | MediumLockList::Base::const_iterator sourceListEnd = pMediumLockList->GetEnd();
|
---|
8191 | MediumLockList::Base::const_iterator mediumListLast = sourceListEnd;
|
---|
8192 | --mediumListLast;
|
---|
8193 |
|
---|
8194 | for (MediumLockList::Base::const_iterator it = sourceListBegin; it != sourceListEnd; ++it)
|
---|
8195 | {
|
---|
8196 | const MediumLock &mediumLock = *it;
|
---|
8197 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8198 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8199 |
|
---|
8200 | /* sanity check */
|
---|
8201 | Assert(pMedium->m->state == (fWritable && it == mediumListLast ? MediumState_LockedWrite : MediumState_LockedRead));
|
---|
8202 |
|
---|
8203 | /* Open all media in read-only mode. */
|
---|
8204 | vrc = VDOpen(pHdd,
|
---|
8205 | pMedium->m->strFormat.c_str(),
|
---|
8206 | pMedium->m->strLocationFull.c_str(),
|
---|
8207 | m->uOpenFlagsDef | (fWritable && it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
8208 | pMedium->m->vdImageIfaces);
|
---|
8209 | if (RT_FAILURE(vrc))
|
---|
8210 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
8211 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8212 | pMedium->m->strLocationFull.c_str(),
|
---|
8213 | i_vdError(vrc).c_str());
|
---|
8214 | }
|
---|
8215 |
|
---|
8216 | Assert(m->state == (fWritable ? MediumState_LockedWrite : MediumState_LockedRead));
|
---|
8217 |
|
---|
8218 | /*
|
---|
8219 | * Done!
|
---|
8220 | */
|
---|
8221 | *ppHdd = pHdd;
|
---|
8222 | return S_OK;
|
---|
8223 | }
|
---|
8224 | catch (HRESULT hrc2)
|
---|
8225 | {
|
---|
8226 | hrc = hrc2;
|
---|
8227 | }
|
---|
8228 |
|
---|
8229 | VDDestroy(pHdd);
|
---|
8230 | return hrc;
|
---|
8231 |
|
---|
8232 | }
|
---|
8233 |
|
---|
8234 | /**
|
---|
8235 | * Implementation code for the "create base" task.
|
---|
8236 | *
|
---|
8237 | * This only gets started from Medium::CreateBaseStorage() and always runs
|
---|
8238 | * asynchronously. As a result, we always save the VirtualBox.xml file when
|
---|
8239 | * we're done here.
|
---|
8240 | *
|
---|
8241 | * @param task
|
---|
8242 | * @return
|
---|
8243 | */
|
---|
8244 | HRESULT Medium::i_taskCreateBaseHandler(Medium::CreateBaseTask &task)
|
---|
8245 | {
|
---|
8246 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
8247 | * to lock order violations, it probably causes lock order issues related
|
---|
8248 | * to the AutoCaller usage. */
|
---|
8249 | HRESULT rc = S_OK;
|
---|
8250 |
|
---|
8251 | /* these parameters we need after creation */
|
---|
8252 | uint64_t size = 0, logicalSize = 0;
|
---|
8253 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
8254 | bool fGenerateUuid = false;
|
---|
8255 |
|
---|
8256 | try
|
---|
8257 | {
|
---|
8258 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8259 |
|
---|
8260 | /* The object may request a specific UUID (through a special form of
|
---|
8261 | * the moveTo() argument). Otherwise we have to generate it */
|
---|
8262 | Guid id = m->id;
|
---|
8263 |
|
---|
8264 | fGenerateUuid = id.isZero();
|
---|
8265 | if (fGenerateUuid)
|
---|
8266 | {
|
---|
8267 | id.create();
|
---|
8268 | /* VirtualBox::i_registerMedium() will need UUID */
|
---|
8269 | unconst(m->id) = id;
|
---|
8270 | }
|
---|
8271 |
|
---|
8272 | Utf8Str format(m->strFormat);
|
---|
8273 | Utf8Str location(m->strLocationFull);
|
---|
8274 | uint64_t capabilities = m->formatObj->i_getCapabilities();
|
---|
8275 | ComAssertThrow(capabilities & ( MediumFormatCapabilities_CreateFixed
|
---|
8276 | | MediumFormatCapabilities_CreateDynamic), E_FAIL);
|
---|
8277 | Assert(m->state == MediumState_Creating);
|
---|
8278 |
|
---|
8279 | PVDISK hdd;
|
---|
8280 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8281 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8282 |
|
---|
8283 | /* unlock before the potentially lengthy operation */
|
---|
8284 | thisLock.release();
|
---|
8285 |
|
---|
8286 | try
|
---|
8287 | {
|
---|
8288 | /* ensure the directory exists */
|
---|
8289 | if (capabilities & MediumFormatCapabilities_File)
|
---|
8290 | {
|
---|
8291 | rc = VirtualBox::i_ensureFilePathExists(location, !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
8292 | if (FAILED(rc))
|
---|
8293 | throw rc;
|
---|
8294 | }
|
---|
8295 |
|
---|
8296 | VDGEOMETRY geo = { 0, 0, 0 }; /* auto-detect */
|
---|
8297 |
|
---|
8298 | vrc = VDCreateBase(hdd,
|
---|
8299 | format.c_str(),
|
---|
8300 | location.c_str(),
|
---|
8301 | task.mSize,
|
---|
8302 | task.mVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted),
|
---|
8303 | NULL,
|
---|
8304 | &geo,
|
---|
8305 | &geo,
|
---|
8306 | id.raw(),
|
---|
8307 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
8308 | m->vdImageIfaces,
|
---|
8309 | task.mVDOperationIfaces);
|
---|
8310 | if (RT_FAILURE(vrc))
|
---|
8311 | {
|
---|
8312 | if (vrc == VERR_VD_INVALID_TYPE)
|
---|
8313 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
8314 | tr("Parameters for creating the medium storage unit '%s' are invalid%s"),
|
---|
8315 | location.c_str(), i_vdError(vrc).c_str());
|
---|
8316 | else
|
---|
8317 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
8318 | tr("Could not create the medium storage unit '%s'%s"),
|
---|
8319 | location.c_str(), i_vdError(vrc).c_str());
|
---|
8320 | }
|
---|
8321 |
|
---|
8322 | if (task.mVariant & MediumVariant_Formatted)
|
---|
8323 | {
|
---|
8324 | RTVFSFILE hVfsFile;
|
---|
8325 | vrc = VDCreateVfsFileFromDisk(hdd, 0 /*fFlags*/, &hVfsFile);
|
---|
8326 | if (RT_FAILURE(vrc))
|
---|
8327 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Opening medium storage unit '%s' failed%s"),
|
---|
8328 | location.c_str(), i_vdError(vrc).c_str());
|
---|
8329 | RTERRINFOSTATIC ErrInfo;
|
---|
8330 | vrc = RTFsFatVolFormat(hVfsFile, 0 /* offVol */, 0 /* cbVol */, RTFSFATVOL_FMT_F_FULL,
|
---|
8331 | 0 /* cbSector */, 0 /* cbSectorPerCluster */, RTFSFATTYPE_INVALID,
|
---|
8332 | 0 /* cHeads */, 0 /* cSectorsPerTrack*/, 0 /* bMedia */,
|
---|
8333 | 0 /* cRootDirEntries */, 0 /* cHiddenSectors */,
|
---|
8334 | RTErrInfoInitStatic(&ErrInfo));
|
---|
8335 | RTVfsFileRelease(hVfsFile);
|
---|
8336 | if (RT_FAILURE(vrc) && RTErrInfoIsSet(&ErrInfo.Core))
|
---|
8337 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Formatting medium storage unit '%s' failed: %s"),
|
---|
8338 | location.c_str(), ErrInfo.Core.pszMsg);
|
---|
8339 | if (RT_FAILURE(vrc))
|
---|
8340 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Formatting medium storage unit '%s' failed%s"),
|
---|
8341 | location.c_str(), i_vdError(vrc).c_str());
|
---|
8342 | }
|
---|
8343 |
|
---|
8344 | size = VDGetFileSize(hdd, 0);
|
---|
8345 | logicalSize = VDGetSize(hdd, 0);
|
---|
8346 | unsigned uImageFlags;
|
---|
8347 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
8348 | if (RT_SUCCESS(vrc))
|
---|
8349 | variant = (MediumVariant_T)uImageFlags;
|
---|
8350 | }
|
---|
8351 | catch (HRESULT aRC) { rc = aRC; }
|
---|
8352 |
|
---|
8353 | VDDestroy(hdd);
|
---|
8354 | }
|
---|
8355 | catch (HRESULT aRC) { rc = aRC; }
|
---|
8356 |
|
---|
8357 | if (SUCCEEDED(rc))
|
---|
8358 | {
|
---|
8359 | /* register with mVirtualBox as the last step and move to
|
---|
8360 | * Created state only on success (leaving an orphan file is
|
---|
8361 | * better than breaking media registry consistency) */
|
---|
8362 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
8363 | ComObjPtr<Medium> pMedium;
|
---|
8364 | rc = m->pVirtualBox->i_registerMedium(this, &pMedium, treeLock);
|
---|
8365 | Assert(pMedium == NULL || this == pMedium);
|
---|
8366 | }
|
---|
8367 |
|
---|
8368 | // re-acquire the lock before changing state
|
---|
8369 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8370 |
|
---|
8371 | if (SUCCEEDED(rc))
|
---|
8372 | {
|
---|
8373 | m->state = MediumState_Created;
|
---|
8374 |
|
---|
8375 | m->size = size;
|
---|
8376 | m->logicalSize = logicalSize;
|
---|
8377 | m->variant = variant;
|
---|
8378 |
|
---|
8379 | thisLock.release();
|
---|
8380 | i_markRegistriesModified();
|
---|
8381 | if (task.isAsync())
|
---|
8382 | {
|
---|
8383 | // in asynchronous mode, save settings now
|
---|
8384 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
8385 | }
|
---|
8386 | }
|
---|
8387 | else
|
---|
8388 | {
|
---|
8389 | /* back to NotCreated on failure */
|
---|
8390 | m->state = MediumState_NotCreated;
|
---|
8391 |
|
---|
8392 | /* reset UUID to prevent it from being reused next time */
|
---|
8393 | if (fGenerateUuid)
|
---|
8394 | unconst(m->id).clear();
|
---|
8395 | }
|
---|
8396 |
|
---|
8397 | return rc;
|
---|
8398 | }
|
---|
8399 |
|
---|
8400 | /**
|
---|
8401 | * Implementation code for the "create diff" task.
|
---|
8402 | *
|
---|
8403 | * This task always gets started from Medium::createDiffStorage() and can run
|
---|
8404 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
8405 | * that function. If we run synchronously, the caller expects the medium
|
---|
8406 | * registry modification to be set before returning; otherwise (in asynchronous
|
---|
8407 | * mode), we save the settings ourselves.
|
---|
8408 | *
|
---|
8409 | * @param task
|
---|
8410 | * @return
|
---|
8411 | */
|
---|
8412 | HRESULT Medium::i_taskCreateDiffHandler(Medium::CreateDiffTask &task)
|
---|
8413 | {
|
---|
8414 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
8415 | * to lock order violations, it probably causes lock order issues related
|
---|
8416 | * to the AutoCaller usage. */
|
---|
8417 | HRESULT rcTmp = S_OK;
|
---|
8418 |
|
---|
8419 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
8420 |
|
---|
8421 | uint64_t size = 0, logicalSize = 0;
|
---|
8422 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
8423 | bool fGenerateUuid = false;
|
---|
8424 |
|
---|
8425 | try
|
---|
8426 | {
|
---|
8427 | if (i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
8428 | {
|
---|
8429 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8430 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8431 | 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"),
|
---|
8432 | m->strLocationFull.c_str());
|
---|
8433 | }
|
---|
8434 |
|
---|
8435 | /* Lock both in {parent,child} order. */
|
---|
8436 | AutoMultiWriteLock2 mediaLock(this, pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8437 |
|
---|
8438 | /* The object may request a specific UUID (through a special form of
|
---|
8439 | * the moveTo() argument). Otherwise we have to generate it */
|
---|
8440 | Guid targetId = pTarget->m->id;
|
---|
8441 |
|
---|
8442 | fGenerateUuid = targetId.isZero();
|
---|
8443 | if (fGenerateUuid)
|
---|
8444 | {
|
---|
8445 | targetId.create();
|
---|
8446 | /* VirtualBox::i_registerMedium() will need UUID */
|
---|
8447 | unconst(pTarget->m->id) = targetId;
|
---|
8448 | }
|
---|
8449 |
|
---|
8450 | Guid id = m->id;
|
---|
8451 |
|
---|
8452 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
8453 | Utf8Str targetLocation(pTarget->m->strLocationFull);
|
---|
8454 | uint64_t capabilities = pTarget->m->formatObj->i_getCapabilities();
|
---|
8455 | ComAssertThrow(capabilities & MediumFormatCapabilities_CreateDynamic, E_FAIL);
|
---|
8456 |
|
---|
8457 | Assert(pTarget->m->state == MediumState_Creating);
|
---|
8458 | Assert(m->state == MediumState_LockedRead);
|
---|
8459 |
|
---|
8460 | PVDISK hdd;
|
---|
8461 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8462 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8463 |
|
---|
8464 | /* the two media are now protected by their non-default states;
|
---|
8465 | * unlock the media before the potentially lengthy operation */
|
---|
8466 | mediaLock.release();
|
---|
8467 |
|
---|
8468 | try
|
---|
8469 | {
|
---|
8470 | /* Open all media in the target chain but the last. */
|
---|
8471 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
8472 | task.mpMediumLockList->GetBegin();
|
---|
8473 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
8474 | task.mpMediumLockList->GetEnd();
|
---|
8475 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
8476 | it != targetListEnd;
|
---|
8477 | ++it)
|
---|
8478 | {
|
---|
8479 | const MediumLock &mediumLock = *it;
|
---|
8480 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8481 |
|
---|
8482 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8483 |
|
---|
8484 | /* Skip over the target diff medium */
|
---|
8485 | if (pMedium->m->state == MediumState_Creating)
|
---|
8486 | continue;
|
---|
8487 |
|
---|
8488 | /* sanity check */
|
---|
8489 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
8490 |
|
---|
8491 | /* Open all media in appropriate mode. */
|
---|
8492 | vrc = VDOpen(hdd,
|
---|
8493 | pMedium->m->strFormat.c_str(),
|
---|
8494 | pMedium->m->strLocationFull.c_str(),
|
---|
8495 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
8496 | pMedium->m->vdImageIfaces);
|
---|
8497 | if (RT_FAILURE(vrc))
|
---|
8498 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
8499 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8500 | pMedium->m->strLocationFull.c_str(),
|
---|
8501 | i_vdError(vrc).c_str());
|
---|
8502 | }
|
---|
8503 |
|
---|
8504 | /* ensure the target directory exists */
|
---|
8505 | if (capabilities & MediumFormatCapabilities_File)
|
---|
8506 | {
|
---|
8507 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
8508 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
8509 | if (FAILED(rc))
|
---|
8510 | throw rc;
|
---|
8511 | }
|
---|
8512 |
|
---|
8513 | vrc = VDCreateDiff(hdd,
|
---|
8514 | targetFormat.c_str(),
|
---|
8515 | targetLocation.c_str(),
|
---|
8516 | (task.mVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted | MediumVariant_VmdkESX))
|
---|
8517 | | VD_IMAGE_FLAGS_DIFF,
|
---|
8518 | NULL,
|
---|
8519 | targetId.raw(),
|
---|
8520 | id.raw(),
|
---|
8521 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
8522 | pTarget->m->vdImageIfaces,
|
---|
8523 | task.mVDOperationIfaces);
|
---|
8524 | if (RT_FAILURE(vrc))
|
---|
8525 | {
|
---|
8526 | if (vrc == VERR_VD_INVALID_TYPE)
|
---|
8527 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
8528 | tr("Parameters for creating the differencing medium storage unit '%s' are invalid%s"),
|
---|
8529 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
8530 | else
|
---|
8531 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
8532 | tr("Could not create the differencing medium storage unit '%s'%s"),
|
---|
8533 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
8534 | }
|
---|
8535 |
|
---|
8536 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
8537 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
8538 | unsigned uImageFlags;
|
---|
8539 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
8540 | if (RT_SUCCESS(vrc))
|
---|
8541 | variant = (MediumVariant_T)uImageFlags;
|
---|
8542 | }
|
---|
8543 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8544 |
|
---|
8545 | VDDestroy(hdd);
|
---|
8546 | }
|
---|
8547 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8548 |
|
---|
8549 | MultiResult mrc(rcTmp);
|
---|
8550 |
|
---|
8551 | if (SUCCEEDED(mrc))
|
---|
8552 | {
|
---|
8553 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
8554 |
|
---|
8555 | Assert(pTarget->m->pParent.isNull());
|
---|
8556 |
|
---|
8557 | /* associate child with the parent, maximum depth was checked above */
|
---|
8558 | pTarget->i_setParent(this);
|
---|
8559 |
|
---|
8560 | /* diffs for immutable media are auto-reset by default */
|
---|
8561 | bool fAutoReset;
|
---|
8562 | {
|
---|
8563 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
8564 | AutoReadLock block(pBase COMMA_LOCKVAL_SRC_POS);
|
---|
8565 | fAutoReset = (pBase->m->type == MediumType_Immutable);
|
---|
8566 | }
|
---|
8567 | {
|
---|
8568 | AutoWriteLock tlock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8569 | pTarget->m->autoReset = fAutoReset;
|
---|
8570 | }
|
---|
8571 |
|
---|
8572 | /* register with mVirtualBox as the last step and move to
|
---|
8573 | * Created state only on success (leaving an orphan file is
|
---|
8574 | * better than breaking media registry consistency) */
|
---|
8575 | ComObjPtr<Medium> pMedium;
|
---|
8576 | mrc = m->pVirtualBox->i_registerMedium(pTarget, &pMedium, treeLock);
|
---|
8577 | Assert(pTarget == pMedium);
|
---|
8578 |
|
---|
8579 | if (FAILED(mrc))
|
---|
8580 | /* break the parent association on failure to register */
|
---|
8581 | i_deparent();
|
---|
8582 | }
|
---|
8583 |
|
---|
8584 | AutoMultiWriteLock2 mediaLock(this, pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8585 |
|
---|
8586 | if (SUCCEEDED(mrc))
|
---|
8587 | {
|
---|
8588 | pTarget->m->state = MediumState_Created;
|
---|
8589 |
|
---|
8590 | pTarget->m->size = size;
|
---|
8591 | pTarget->m->logicalSize = logicalSize;
|
---|
8592 | pTarget->m->variant = variant;
|
---|
8593 | }
|
---|
8594 | else
|
---|
8595 | {
|
---|
8596 | /* back to NotCreated on failure */
|
---|
8597 | pTarget->m->state = MediumState_NotCreated;
|
---|
8598 |
|
---|
8599 | pTarget->m->autoReset = false;
|
---|
8600 |
|
---|
8601 | /* reset UUID to prevent it from being reused next time */
|
---|
8602 | if (fGenerateUuid)
|
---|
8603 | unconst(pTarget->m->id).clear();
|
---|
8604 | }
|
---|
8605 |
|
---|
8606 | // deregister the task registered in createDiffStorage()
|
---|
8607 | Assert(m->numCreateDiffTasks != 0);
|
---|
8608 | --m->numCreateDiffTasks;
|
---|
8609 |
|
---|
8610 | mediaLock.release();
|
---|
8611 | i_markRegistriesModified();
|
---|
8612 | if (task.isAsync())
|
---|
8613 | {
|
---|
8614 | // in asynchronous mode, save settings now
|
---|
8615 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
8616 | }
|
---|
8617 |
|
---|
8618 | /* Note that in sync mode, it's the caller's responsibility to
|
---|
8619 | * unlock the medium. */
|
---|
8620 |
|
---|
8621 | return mrc;
|
---|
8622 | }
|
---|
8623 |
|
---|
8624 | /**
|
---|
8625 | * Implementation code for the "merge" task.
|
---|
8626 | *
|
---|
8627 | * This task always gets started from Medium::mergeTo() and can run
|
---|
8628 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
8629 | * that function. If we run synchronously, the caller expects the medium
|
---|
8630 | * registry modification to be set before returning; otherwise (in asynchronous
|
---|
8631 | * mode), we save the settings ourselves.
|
---|
8632 | *
|
---|
8633 | * @param task
|
---|
8634 | * @return
|
---|
8635 | */
|
---|
8636 | HRESULT Medium::i_taskMergeHandler(Medium::MergeTask &task)
|
---|
8637 | {
|
---|
8638 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
8639 | * to lock order violations, it probably causes lock order issues related
|
---|
8640 | * to the AutoCaller usage. */
|
---|
8641 | HRESULT rcTmp = S_OK;
|
---|
8642 |
|
---|
8643 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
8644 |
|
---|
8645 | try
|
---|
8646 | {
|
---|
8647 | if (!task.mParentForTarget.isNull())
|
---|
8648 | if (task.mParentForTarget->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
8649 | {
|
---|
8650 | AutoReadLock plock(task.mParentForTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8651 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8652 | tr("Cannot merge image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
8653 | task.mParentForTarget->m->strLocationFull.c_str());
|
---|
8654 | }
|
---|
8655 |
|
---|
8656 | PVDISK hdd;
|
---|
8657 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8658 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8659 |
|
---|
8660 | try
|
---|
8661 | {
|
---|
8662 | // Similar code appears in SessionMachine::onlineMergeMedium, so
|
---|
8663 | // if you make any changes below check whether they are applicable
|
---|
8664 | // in that context as well.
|
---|
8665 |
|
---|
8666 | unsigned uTargetIdx = VD_LAST_IMAGE;
|
---|
8667 | unsigned uSourceIdx = VD_LAST_IMAGE;
|
---|
8668 | /* Open all media in the chain. */
|
---|
8669 | MediumLockList::Base::iterator lockListBegin =
|
---|
8670 | task.mpMediumLockList->GetBegin();
|
---|
8671 | MediumLockList::Base::iterator lockListEnd =
|
---|
8672 | task.mpMediumLockList->GetEnd();
|
---|
8673 | unsigned i = 0;
|
---|
8674 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
8675 | it != lockListEnd;
|
---|
8676 | ++it)
|
---|
8677 | {
|
---|
8678 | MediumLock &mediumLock = *it;
|
---|
8679 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8680 |
|
---|
8681 | if (pMedium == this)
|
---|
8682 | uSourceIdx = i;
|
---|
8683 | else if (pMedium == pTarget)
|
---|
8684 | uTargetIdx = i;
|
---|
8685 |
|
---|
8686 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8687 |
|
---|
8688 | /*
|
---|
8689 | * complex sanity (sane complexity)
|
---|
8690 | *
|
---|
8691 | * The current medium must be in the Deleting (medium is merged)
|
---|
8692 | * or LockedRead (parent medium) state if it is not the target.
|
---|
8693 | * If it is the target it must be in the LockedWrite state.
|
---|
8694 | */
|
---|
8695 | Assert( ( pMedium != pTarget
|
---|
8696 | && ( pMedium->m->state == MediumState_Deleting
|
---|
8697 | || pMedium->m->state == MediumState_LockedRead))
|
---|
8698 | || ( pMedium == pTarget
|
---|
8699 | && pMedium->m->state == MediumState_LockedWrite));
|
---|
8700 | /*
|
---|
8701 | * Medium must be the target, in the LockedRead state
|
---|
8702 | * or Deleting state where it is not allowed to be attached
|
---|
8703 | * to a virtual machine.
|
---|
8704 | */
|
---|
8705 | Assert( pMedium == pTarget
|
---|
8706 | || pMedium->m->state == MediumState_LockedRead
|
---|
8707 | || ( pMedium->m->backRefs.size() == 0
|
---|
8708 | && pMedium->m->state == MediumState_Deleting));
|
---|
8709 | /* The source medium must be in Deleting state. */
|
---|
8710 | Assert( pMedium != this
|
---|
8711 | || pMedium->m->state == MediumState_Deleting);
|
---|
8712 |
|
---|
8713 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
8714 |
|
---|
8715 | if ( pMedium->m->state == MediumState_LockedRead
|
---|
8716 | || pMedium->m->state == MediumState_Deleting)
|
---|
8717 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
8718 | if (pMedium->m->type == MediumType_Shareable)
|
---|
8719 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
8720 |
|
---|
8721 | /* Open the medium */
|
---|
8722 | vrc = VDOpen(hdd,
|
---|
8723 | pMedium->m->strFormat.c_str(),
|
---|
8724 | pMedium->m->strLocationFull.c_str(),
|
---|
8725 | uOpenFlags | m->uOpenFlagsDef,
|
---|
8726 | pMedium->m->vdImageIfaces);
|
---|
8727 | if (RT_FAILURE(vrc))
|
---|
8728 | throw vrc;
|
---|
8729 |
|
---|
8730 | i++;
|
---|
8731 | }
|
---|
8732 |
|
---|
8733 | ComAssertThrow( uSourceIdx != VD_LAST_IMAGE
|
---|
8734 | && uTargetIdx != VD_LAST_IMAGE, E_FAIL);
|
---|
8735 |
|
---|
8736 | vrc = VDMerge(hdd, uSourceIdx, uTargetIdx,
|
---|
8737 | task.mVDOperationIfaces);
|
---|
8738 | if (RT_FAILURE(vrc))
|
---|
8739 | throw vrc;
|
---|
8740 |
|
---|
8741 | /* update parent UUIDs */
|
---|
8742 | if (!task.mfMergeForward)
|
---|
8743 | {
|
---|
8744 | /* we need to update UUIDs of all source's children
|
---|
8745 | * which cannot be part of the container at once so
|
---|
8746 | * add each one in there individually */
|
---|
8747 | if (task.mpChildrenToReparent)
|
---|
8748 | {
|
---|
8749 | MediumLockList::Base::iterator childrenBegin = task.mpChildrenToReparent->GetBegin();
|
---|
8750 | MediumLockList::Base::iterator childrenEnd = task.mpChildrenToReparent->GetEnd();
|
---|
8751 | for (MediumLockList::Base::iterator it = childrenBegin;
|
---|
8752 | it != childrenEnd;
|
---|
8753 | ++it)
|
---|
8754 | {
|
---|
8755 | Medium *pMedium = it->GetMedium();
|
---|
8756 | /* VD_OPEN_FLAGS_INFO since UUID is wrong yet */
|
---|
8757 | vrc = VDOpen(hdd,
|
---|
8758 | pMedium->m->strFormat.c_str(),
|
---|
8759 | pMedium->m->strLocationFull.c_str(),
|
---|
8760 | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
8761 | pMedium->m->vdImageIfaces);
|
---|
8762 | if (RT_FAILURE(vrc))
|
---|
8763 | throw vrc;
|
---|
8764 |
|
---|
8765 | vrc = VDSetParentUuid(hdd, VD_LAST_IMAGE,
|
---|
8766 | pTarget->m->id.raw());
|
---|
8767 | if (RT_FAILURE(vrc))
|
---|
8768 | throw vrc;
|
---|
8769 |
|
---|
8770 | vrc = VDClose(hdd, false /* fDelete */);
|
---|
8771 | if (RT_FAILURE(vrc))
|
---|
8772 | throw vrc;
|
---|
8773 | }
|
---|
8774 | }
|
---|
8775 | }
|
---|
8776 | }
|
---|
8777 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8778 | catch (int aVRC)
|
---|
8779 | {
|
---|
8780 | rcTmp = setErrorBoth(VBOX_E_FILE_ERROR, aVRC,
|
---|
8781 | tr("Could not merge the medium '%s' to '%s'%s"),
|
---|
8782 | m->strLocationFull.c_str(),
|
---|
8783 | pTarget->m->strLocationFull.c_str(),
|
---|
8784 | i_vdError(aVRC).c_str());
|
---|
8785 | }
|
---|
8786 |
|
---|
8787 | VDDestroy(hdd);
|
---|
8788 | }
|
---|
8789 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8790 |
|
---|
8791 | ErrorInfoKeeper eik;
|
---|
8792 | MultiResult mrc(rcTmp);
|
---|
8793 | HRESULT rc2;
|
---|
8794 |
|
---|
8795 | if (SUCCEEDED(mrc))
|
---|
8796 | {
|
---|
8797 | /* all media but the target were successfully deleted by
|
---|
8798 | * VDMerge; reparent the last one and uninitialize deleted media. */
|
---|
8799 |
|
---|
8800 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
8801 |
|
---|
8802 | if (task.mfMergeForward)
|
---|
8803 | {
|
---|
8804 | /* first, unregister the target since it may become a base
|
---|
8805 | * medium which needs re-registration */
|
---|
8806 | rc2 = m->pVirtualBox->i_unregisterMedium(pTarget);
|
---|
8807 | AssertComRC(rc2);
|
---|
8808 |
|
---|
8809 | /* then, reparent it and disconnect the deleted branch at both ends
|
---|
8810 | * (chain->parent() is source's parent). Depth check above. */
|
---|
8811 | pTarget->i_deparent();
|
---|
8812 | pTarget->i_setParent(task.mParentForTarget);
|
---|
8813 | if (task.mParentForTarget)
|
---|
8814 | i_deparent();
|
---|
8815 |
|
---|
8816 | /* then, register again */
|
---|
8817 | ComObjPtr<Medium> pMedium;
|
---|
8818 | rc2 = m->pVirtualBox->i_registerMedium(pTarget, &pMedium,
|
---|
8819 | treeLock);
|
---|
8820 | AssertComRC(rc2);
|
---|
8821 | }
|
---|
8822 | else
|
---|
8823 | {
|
---|
8824 | Assert(pTarget->i_getChildren().size() == 1);
|
---|
8825 | Medium *targetChild = pTarget->i_getChildren().front();
|
---|
8826 |
|
---|
8827 | /* disconnect the deleted branch at the elder end */
|
---|
8828 | targetChild->i_deparent();
|
---|
8829 |
|
---|
8830 | /* reparent source's children and disconnect the deleted
|
---|
8831 | * branch at the younger end */
|
---|
8832 | if (task.mpChildrenToReparent)
|
---|
8833 | {
|
---|
8834 | /* obey {parent,child} lock order */
|
---|
8835 | AutoWriteLock sourceLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8836 |
|
---|
8837 | MediumLockList::Base::iterator childrenBegin = task.mpChildrenToReparent->GetBegin();
|
---|
8838 | MediumLockList::Base::iterator childrenEnd = task.mpChildrenToReparent->GetEnd();
|
---|
8839 | for (MediumLockList::Base::iterator it = childrenBegin;
|
---|
8840 | it != childrenEnd;
|
---|
8841 | ++it)
|
---|
8842 | {
|
---|
8843 | Medium *pMedium = it->GetMedium();
|
---|
8844 | AutoWriteLock childLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8845 |
|
---|
8846 | pMedium->i_deparent(); // removes pMedium from source
|
---|
8847 | // no depth check, reduces depth
|
---|
8848 | pMedium->i_setParent(pTarget);
|
---|
8849 | }
|
---|
8850 | }
|
---|
8851 | }
|
---|
8852 |
|
---|
8853 | /* unregister and uninitialize all media removed by the merge */
|
---|
8854 | MediumLockList::Base::iterator lockListBegin =
|
---|
8855 | task.mpMediumLockList->GetBegin();
|
---|
8856 | MediumLockList::Base::iterator lockListEnd =
|
---|
8857 | task.mpMediumLockList->GetEnd();
|
---|
8858 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
8859 | it != lockListEnd;
|
---|
8860 | )
|
---|
8861 | {
|
---|
8862 | MediumLock &mediumLock = *it;
|
---|
8863 | /* Create a real copy of the medium pointer, as the medium
|
---|
8864 | * lock deletion below would invalidate the referenced object. */
|
---|
8865 | const ComObjPtr<Medium> pMedium = mediumLock.GetMedium();
|
---|
8866 |
|
---|
8867 | /* The target and all media not merged (readonly) are skipped */
|
---|
8868 | if ( pMedium == pTarget
|
---|
8869 | || pMedium->m->state == MediumState_LockedRead)
|
---|
8870 | {
|
---|
8871 | ++it;
|
---|
8872 | continue;
|
---|
8873 | }
|
---|
8874 |
|
---|
8875 | rc2 = pMedium->m->pVirtualBox->i_unregisterMedium(pMedium);
|
---|
8876 | AssertComRC(rc2);
|
---|
8877 |
|
---|
8878 | /* now, uninitialize the deleted medium (note that
|
---|
8879 | * due to the Deleting state, uninit() will not touch
|
---|
8880 | * the parent-child relationship so we need to
|
---|
8881 | * uninitialize each disk individually) */
|
---|
8882 |
|
---|
8883 | /* note that the operation initiator medium (which is
|
---|
8884 | * normally also the source medium) is a special case
|
---|
8885 | * -- there is one more caller added by Task to it which
|
---|
8886 | * we must release. Also, if we are in sync mode, the
|
---|
8887 | * caller may still hold an AutoCaller instance for it
|
---|
8888 | * and therefore we cannot uninit() it (it's therefore
|
---|
8889 | * the caller's responsibility) */
|
---|
8890 | if (pMedium == this)
|
---|
8891 | {
|
---|
8892 | Assert(i_getChildren().size() == 0);
|
---|
8893 | Assert(m->backRefs.size() == 0);
|
---|
8894 | task.mMediumCaller.release();
|
---|
8895 | }
|
---|
8896 |
|
---|
8897 | /* Delete the medium lock list entry, which also releases the
|
---|
8898 | * caller added by MergeChain before uninit() and updates the
|
---|
8899 | * iterator to point to the right place. */
|
---|
8900 | rc2 = task.mpMediumLockList->RemoveByIterator(it);
|
---|
8901 | AssertComRC(rc2);
|
---|
8902 |
|
---|
8903 | if (task.isAsync() || pMedium != this)
|
---|
8904 | {
|
---|
8905 | treeLock.release();
|
---|
8906 | pMedium->uninit();
|
---|
8907 | treeLock.acquire();
|
---|
8908 | }
|
---|
8909 | }
|
---|
8910 | }
|
---|
8911 |
|
---|
8912 | i_markRegistriesModified();
|
---|
8913 | if (task.isAsync())
|
---|
8914 | {
|
---|
8915 | // in asynchronous mode, save settings now
|
---|
8916 | eik.restore();
|
---|
8917 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
8918 | eik.fetch();
|
---|
8919 | }
|
---|
8920 |
|
---|
8921 | if (FAILED(mrc))
|
---|
8922 | {
|
---|
8923 | /* Here we come if either VDMerge() failed (in which case we
|
---|
8924 | * assume that it tried to do everything to make a further
|
---|
8925 | * retry possible -- e.g. not deleted intermediate media
|
---|
8926 | * and so on) or VirtualBox::saveRegistries() failed (where we
|
---|
8927 | * should have the original tree but with intermediate storage
|
---|
8928 | * units deleted by VDMerge()). We have to only restore states
|
---|
8929 | * (through the MergeChain dtor) unless we are run synchronously
|
---|
8930 | * in which case it's the responsibility of the caller as stated
|
---|
8931 | * in the mergeTo() docs. The latter also implies that we
|
---|
8932 | * don't own the merge chain, so release it in this case. */
|
---|
8933 | if (task.isAsync())
|
---|
8934 | i_cancelMergeTo(task.mpChildrenToReparent, task.mpMediumLockList);
|
---|
8935 | }
|
---|
8936 |
|
---|
8937 | return mrc;
|
---|
8938 | }
|
---|
8939 |
|
---|
8940 | /**
|
---|
8941 | * Implementation code for the "clone" task.
|
---|
8942 | *
|
---|
8943 | * This only gets started from Medium::CloneTo() and always runs asynchronously.
|
---|
8944 | * As a result, we always save the VirtualBox.xml file when we're done here.
|
---|
8945 | *
|
---|
8946 | * @param task
|
---|
8947 | * @return
|
---|
8948 | */
|
---|
8949 | HRESULT Medium::i_taskCloneHandler(Medium::CloneTask &task)
|
---|
8950 | {
|
---|
8951 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
8952 | * to lock order violations, it probably causes lock order issues related
|
---|
8953 | * to the AutoCaller usage. */
|
---|
8954 | HRESULT rcTmp = S_OK;
|
---|
8955 |
|
---|
8956 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
8957 | const ComObjPtr<Medium> &pParent = task.mParent;
|
---|
8958 |
|
---|
8959 | bool fCreatingTarget = false;
|
---|
8960 |
|
---|
8961 | uint64_t size = 0, logicalSize = 0;
|
---|
8962 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
8963 | bool fGenerateUuid = false;
|
---|
8964 |
|
---|
8965 | try
|
---|
8966 | {
|
---|
8967 | if (!pParent.isNull())
|
---|
8968 | {
|
---|
8969 |
|
---|
8970 | if (pParent->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
8971 | {
|
---|
8972 | AutoReadLock plock(pParent COMMA_LOCKVAL_SRC_POS);
|
---|
8973 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8974 | tr("Cannot clone image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
8975 | pParent->m->strLocationFull.c_str());
|
---|
8976 | }
|
---|
8977 | }
|
---|
8978 |
|
---|
8979 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
8980 | * signal from the task initiator (which releases it only after
|
---|
8981 | * RTThreadCreate()) that we can start the job. */
|
---|
8982 | AutoMultiWriteLock3 thisLock(this, pTarget, pParent COMMA_LOCKVAL_SRC_POS);
|
---|
8983 |
|
---|
8984 | fCreatingTarget = pTarget->m->state == MediumState_Creating;
|
---|
8985 |
|
---|
8986 | /* The object may request a specific UUID (through a special form of
|
---|
8987 | * the moveTo() argument). Otherwise we have to generate it */
|
---|
8988 | Guid targetId = pTarget->m->id;
|
---|
8989 |
|
---|
8990 | fGenerateUuid = targetId.isZero();
|
---|
8991 | if (fGenerateUuid)
|
---|
8992 | {
|
---|
8993 | targetId.create();
|
---|
8994 | /* VirtualBox::registerMedium() will need UUID */
|
---|
8995 | unconst(pTarget->m->id) = targetId;
|
---|
8996 | }
|
---|
8997 |
|
---|
8998 | PVDISK hdd;
|
---|
8999 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9000 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9001 |
|
---|
9002 | try
|
---|
9003 | {
|
---|
9004 | /* Open all media in the source chain. */
|
---|
9005 | MediumLockList::Base::const_iterator sourceListBegin =
|
---|
9006 | task.mpSourceMediumLockList->GetBegin();
|
---|
9007 | MediumLockList::Base::const_iterator sourceListEnd =
|
---|
9008 | task.mpSourceMediumLockList->GetEnd();
|
---|
9009 | for (MediumLockList::Base::const_iterator it = sourceListBegin;
|
---|
9010 | it != sourceListEnd;
|
---|
9011 | ++it)
|
---|
9012 | {
|
---|
9013 | const MediumLock &mediumLock = *it;
|
---|
9014 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9015 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9016 |
|
---|
9017 | /* sanity check */
|
---|
9018 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
9019 |
|
---|
9020 | /** Open all media in read-only mode. */
|
---|
9021 | vrc = VDOpen(hdd,
|
---|
9022 | pMedium->m->strFormat.c_str(),
|
---|
9023 | pMedium->m->strLocationFull.c_str(),
|
---|
9024 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
9025 | pMedium->m->vdImageIfaces);
|
---|
9026 | if (RT_FAILURE(vrc))
|
---|
9027 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9028 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9029 | pMedium->m->strLocationFull.c_str(),
|
---|
9030 | i_vdError(vrc).c_str());
|
---|
9031 | }
|
---|
9032 |
|
---|
9033 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
9034 | Utf8Str targetLocation(pTarget->m->strLocationFull);
|
---|
9035 | uint64_t capabilities = pTarget->m->formatObj->i_getCapabilities();
|
---|
9036 |
|
---|
9037 | Assert( pTarget->m->state == MediumState_Creating
|
---|
9038 | || pTarget->m->state == MediumState_LockedWrite);
|
---|
9039 | Assert(m->state == MediumState_LockedRead);
|
---|
9040 | Assert( pParent.isNull()
|
---|
9041 | || pParent->m->state == MediumState_LockedRead);
|
---|
9042 |
|
---|
9043 | /* unlock before the potentially lengthy operation */
|
---|
9044 | thisLock.release();
|
---|
9045 |
|
---|
9046 | /* ensure the target directory exists */
|
---|
9047 | if (capabilities & MediumFormatCapabilities_File)
|
---|
9048 | {
|
---|
9049 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
9050 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
9051 | if (FAILED(rc))
|
---|
9052 | throw rc;
|
---|
9053 | }
|
---|
9054 |
|
---|
9055 | PVDISK targetHdd;
|
---|
9056 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &targetHdd);
|
---|
9057 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9058 |
|
---|
9059 | try
|
---|
9060 | {
|
---|
9061 | /* Open all media in the target chain. */
|
---|
9062 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
9063 | task.mpTargetMediumLockList->GetBegin();
|
---|
9064 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
9065 | task.mpTargetMediumLockList->GetEnd();
|
---|
9066 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
9067 | it != targetListEnd;
|
---|
9068 | ++it)
|
---|
9069 | {
|
---|
9070 | const MediumLock &mediumLock = *it;
|
---|
9071 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9072 |
|
---|
9073 | /* If the target medium is not created yet there's no
|
---|
9074 | * reason to open it. */
|
---|
9075 | if (pMedium == pTarget && fCreatingTarget)
|
---|
9076 | continue;
|
---|
9077 |
|
---|
9078 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9079 |
|
---|
9080 | /* sanity check */
|
---|
9081 | Assert( pMedium->m->state == MediumState_LockedRead
|
---|
9082 | || pMedium->m->state == MediumState_LockedWrite);
|
---|
9083 |
|
---|
9084 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
9085 | if (pMedium->m->state != MediumState_LockedWrite)
|
---|
9086 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
9087 | if (pMedium->m->type == MediumType_Shareable)
|
---|
9088 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
9089 |
|
---|
9090 | /* Open all media in appropriate mode. */
|
---|
9091 | vrc = VDOpen(targetHdd,
|
---|
9092 | pMedium->m->strFormat.c_str(),
|
---|
9093 | pMedium->m->strLocationFull.c_str(),
|
---|
9094 | uOpenFlags | m->uOpenFlagsDef,
|
---|
9095 | pMedium->m->vdImageIfaces);
|
---|
9096 | if (RT_FAILURE(vrc))
|
---|
9097 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9098 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9099 | pMedium->m->strLocationFull.c_str(),
|
---|
9100 | i_vdError(vrc).c_str());
|
---|
9101 | }
|
---|
9102 |
|
---|
9103 | /* target isn't locked, but no changing data is accessed */
|
---|
9104 | if (task.midxSrcImageSame == UINT32_MAX)
|
---|
9105 | {
|
---|
9106 | vrc = VDCopy(hdd,
|
---|
9107 | VD_LAST_IMAGE,
|
---|
9108 | targetHdd,
|
---|
9109 | targetFormat.c_str(),
|
---|
9110 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
9111 | false /* fMoveByRename */,
|
---|
9112 | 0 /* cbSize */,
|
---|
9113 | task.mVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted),
|
---|
9114 | targetId.raw(),
|
---|
9115 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
9116 | NULL /* pVDIfsOperation */,
|
---|
9117 | pTarget->m->vdImageIfaces,
|
---|
9118 | task.mVDOperationIfaces);
|
---|
9119 | }
|
---|
9120 | else
|
---|
9121 | {
|
---|
9122 | vrc = VDCopyEx(hdd,
|
---|
9123 | VD_LAST_IMAGE,
|
---|
9124 | targetHdd,
|
---|
9125 | targetFormat.c_str(),
|
---|
9126 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
9127 | false /* fMoveByRename */,
|
---|
9128 | 0 /* cbSize */,
|
---|
9129 | task.midxSrcImageSame,
|
---|
9130 | task.midxDstImageSame,
|
---|
9131 | task.mVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted),
|
---|
9132 | targetId.raw(),
|
---|
9133 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
9134 | NULL /* pVDIfsOperation */,
|
---|
9135 | pTarget->m->vdImageIfaces,
|
---|
9136 | task.mVDOperationIfaces);
|
---|
9137 | }
|
---|
9138 | if (RT_FAILURE(vrc))
|
---|
9139 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9140 | tr("Could not create the clone medium '%s'%s"),
|
---|
9141 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
9142 |
|
---|
9143 | size = VDGetFileSize(targetHdd, VD_LAST_IMAGE);
|
---|
9144 | logicalSize = VDGetSize(targetHdd, VD_LAST_IMAGE);
|
---|
9145 | unsigned uImageFlags;
|
---|
9146 | vrc = VDGetImageFlags(targetHdd, 0, &uImageFlags);
|
---|
9147 | if (RT_SUCCESS(vrc))
|
---|
9148 | variant = (MediumVariant_T)uImageFlags;
|
---|
9149 | }
|
---|
9150 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9151 |
|
---|
9152 | VDDestroy(targetHdd);
|
---|
9153 | }
|
---|
9154 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9155 |
|
---|
9156 | VDDestroy(hdd);
|
---|
9157 | }
|
---|
9158 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9159 |
|
---|
9160 | ErrorInfoKeeper eik;
|
---|
9161 | MultiResult mrc(rcTmp);
|
---|
9162 |
|
---|
9163 | /* Only do the parent changes for newly created media. */
|
---|
9164 | if (SUCCEEDED(mrc) && fCreatingTarget)
|
---|
9165 | {
|
---|
9166 | /* we set m->pParent & children() */
|
---|
9167 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
9168 |
|
---|
9169 | Assert(pTarget->m->pParent.isNull());
|
---|
9170 |
|
---|
9171 | if (pParent)
|
---|
9172 | {
|
---|
9173 | /* Associate the clone with the parent and deassociate
|
---|
9174 | * from VirtualBox. Depth check above. */
|
---|
9175 | pTarget->i_setParent(pParent);
|
---|
9176 |
|
---|
9177 | /* register with mVirtualBox as the last step and move to
|
---|
9178 | * Created state only on success (leaving an orphan file is
|
---|
9179 | * better than breaking media registry consistency) */
|
---|
9180 | eik.restore();
|
---|
9181 | ComObjPtr<Medium> pMedium;
|
---|
9182 | mrc = pParent->m->pVirtualBox->i_registerMedium(pTarget, &pMedium,
|
---|
9183 | treeLock);
|
---|
9184 | Assert( FAILED(mrc)
|
---|
9185 | || pTarget == pMedium);
|
---|
9186 | eik.fetch();
|
---|
9187 |
|
---|
9188 | if (FAILED(mrc))
|
---|
9189 | /* break parent association on failure to register */
|
---|
9190 | pTarget->i_deparent(); // removes target from parent
|
---|
9191 | }
|
---|
9192 | else
|
---|
9193 | {
|
---|
9194 | /* just register */
|
---|
9195 | eik.restore();
|
---|
9196 | ComObjPtr<Medium> pMedium;
|
---|
9197 | mrc = m->pVirtualBox->i_registerMedium(pTarget, &pMedium,
|
---|
9198 | treeLock);
|
---|
9199 | Assert( FAILED(mrc)
|
---|
9200 | || pTarget == pMedium);
|
---|
9201 | eik.fetch();
|
---|
9202 | }
|
---|
9203 | }
|
---|
9204 |
|
---|
9205 | if (fCreatingTarget)
|
---|
9206 | {
|
---|
9207 | AutoWriteLock mLock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
9208 |
|
---|
9209 | if (SUCCEEDED(mrc))
|
---|
9210 | {
|
---|
9211 | pTarget->m->state = MediumState_Created;
|
---|
9212 |
|
---|
9213 | pTarget->m->size = size;
|
---|
9214 | pTarget->m->logicalSize = logicalSize;
|
---|
9215 | pTarget->m->variant = variant;
|
---|
9216 | }
|
---|
9217 | else
|
---|
9218 | {
|
---|
9219 | /* back to NotCreated on failure */
|
---|
9220 | pTarget->m->state = MediumState_NotCreated;
|
---|
9221 |
|
---|
9222 | /* reset UUID to prevent it from being reused next time */
|
---|
9223 | if (fGenerateUuid)
|
---|
9224 | unconst(pTarget->m->id).clear();
|
---|
9225 | }
|
---|
9226 | }
|
---|
9227 |
|
---|
9228 | /* Copy any filter related settings over to the target. */
|
---|
9229 | if (SUCCEEDED(mrc))
|
---|
9230 | {
|
---|
9231 | /* Copy any filter related settings over. */
|
---|
9232 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
9233 | ComObjPtr<Medium> pTargetBase = pTarget->i_getBase();
|
---|
9234 | std::vector<com::Utf8Str> aFilterPropNames;
|
---|
9235 | std::vector<com::Utf8Str> aFilterPropValues;
|
---|
9236 | mrc = pBase->i_getFilterProperties(aFilterPropNames, aFilterPropValues);
|
---|
9237 | if (SUCCEEDED(mrc))
|
---|
9238 | {
|
---|
9239 | /* Go through the properties and add them to the target medium. */
|
---|
9240 | for (unsigned idx = 0; idx < aFilterPropNames.size(); idx++)
|
---|
9241 | {
|
---|
9242 | mrc = pTargetBase->i_setPropertyDirect(aFilterPropNames[idx], aFilterPropValues[idx]);
|
---|
9243 | if (FAILED(mrc)) break;
|
---|
9244 | }
|
---|
9245 |
|
---|
9246 | // now, at the end of this task (always asynchronous), save the settings
|
---|
9247 | if (SUCCEEDED(mrc))
|
---|
9248 | {
|
---|
9249 | // save the settings
|
---|
9250 | i_markRegistriesModified();
|
---|
9251 | /* collect multiple errors */
|
---|
9252 | eik.restore();
|
---|
9253 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
9254 | eik.fetch();
|
---|
9255 | }
|
---|
9256 | }
|
---|
9257 | }
|
---|
9258 |
|
---|
9259 | /* Everything is explicitly unlocked when the task exits,
|
---|
9260 | * as the task destruction also destroys the source chain. */
|
---|
9261 |
|
---|
9262 | /* Make sure the source chain is released early. It could happen
|
---|
9263 | * that we get a deadlock in Appliance::Import when Medium::Close
|
---|
9264 | * is called & the source chain is released at the same time. */
|
---|
9265 | task.mpSourceMediumLockList->Clear();
|
---|
9266 |
|
---|
9267 | return mrc;
|
---|
9268 | }
|
---|
9269 |
|
---|
9270 | /**
|
---|
9271 | * Implementation code for the "move" task.
|
---|
9272 | *
|
---|
9273 | * This only gets started from Medium::MoveTo() and always
|
---|
9274 | * runs asynchronously.
|
---|
9275 | *
|
---|
9276 | * @param task
|
---|
9277 | * @return
|
---|
9278 | */
|
---|
9279 | HRESULT Medium::i_taskMoveHandler(Medium::MoveTask &task)
|
---|
9280 | {
|
---|
9281 |
|
---|
9282 | HRESULT rcOut = S_OK;
|
---|
9283 |
|
---|
9284 | /* pTarget is equal "this" in our case */
|
---|
9285 | const ComObjPtr<Medium> &pTarget = task.mMedium;
|
---|
9286 |
|
---|
9287 | uint64_t size = 0; NOREF(size);
|
---|
9288 | uint64_t logicalSize = 0; NOREF(logicalSize);
|
---|
9289 | MediumVariant_T variant = MediumVariant_Standard; NOREF(variant);
|
---|
9290 |
|
---|
9291 | /*
|
---|
9292 | * it's exactly moving, not cloning
|
---|
9293 | */
|
---|
9294 | if (!i_isMoveOperation(pTarget))
|
---|
9295 | {
|
---|
9296 | HRESULT rc = setError(VBOX_E_FILE_ERROR,
|
---|
9297 | tr("Wrong preconditions for moving the medium %s"),
|
---|
9298 | pTarget->m->strLocationFull.c_str());
|
---|
9299 | return rc;
|
---|
9300 | }
|
---|
9301 |
|
---|
9302 | try
|
---|
9303 | {
|
---|
9304 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
9305 | * signal from the task initiator (which releases it only after
|
---|
9306 | * RTThreadCreate()) that we can start the job. */
|
---|
9307 |
|
---|
9308 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9309 |
|
---|
9310 | PVDISK hdd;
|
---|
9311 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9312 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9313 |
|
---|
9314 | try
|
---|
9315 | {
|
---|
9316 | /* Open all media in the source chain. */
|
---|
9317 | MediumLockList::Base::const_iterator sourceListBegin =
|
---|
9318 | task.mpMediumLockList->GetBegin();
|
---|
9319 | MediumLockList::Base::const_iterator sourceListEnd =
|
---|
9320 | task.mpMediumLockList->GetEnd();
|
---|
9321 | for (MediumLockList::Base::const_iterator it = sourceListBegin;
|
---|
9322 | it != sourceListEnd;
|
---|
9323 | ++it)
|
---|
9324 | {
|
---|
9325 | const MediumLock &mediumLock = *it;
|
---|
9326 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9327 | AutoWriteLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9328 |
|
---|
9329 | /* sanity check */
|
---|
9330 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
9331 |
|
---|
9332 | vrc = VDOpen(hdd,
|
---|
9333 | pMedium->m->strFormat.c_str(),
|
---|
9334 | pMedium->m->strLocationFull.c_str(),
|
---|
9335 | VD_OPEN_FLAGS_NORMAL,
|
---|
9336 | pMedium->m->vdImageIfaces);
|
---|
9337 | if (RT_FAILURE(vrc))
|
---|
9338 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9339 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9340 | pMedium->m->strLocationFull.c_str(),
|
---|
9341 | i_vdError(vrc).c_str());
|
---|
9342 | }
|
---|
9343 |
|
---|
9344 | /* we can directly use pTarget->m->"variables" but for better reading we use local copies */
|
---|
9345 | Guid targetId = pTarget->m->id;
|
---|
9346 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
9347 | uint64_t targetCapabilities = pTarget->m->formatObj->i_getCapabilities();
|
---|
9348 |
|
---|
9349 | /*
|
---|
9350 | * change target location
|
---|
9351 | * m->strNewLocationFull has been set already together with m->fMoveThisMedium in
|
---|
9352 | * i_preparationForMoving()
|
---|
9353 | */
|
---|
9354 | Utf8Str targetLocation = i_getNewLocationForMoving();
|
---|
9355 |
|
---|
9356 | /* unlock before the potentially lengthy operation */
|
---|
9357 | thisLock.release();
|
---|
9358 |
|
---|
9359 | /* ensure the target directory exists */
|
---|
9360 | if (targetCapabilities & MediumFormatCapabilities_File)
|
---|
9361 | {
|
---|
9362 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
9363 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
9364 | if (FAILED(rc))
|
---|
9365 | throw rc;
|
---|
9366 | }
|
---|
9367 |
|
---|
9368 | try
|
---|
9369 | {
|
---|
9370 | vrc = VDCopy(hdd,
|
---|
9371 | VD_LAST_IMAGE,
|
---|
9372 | hdd,
|
---|
9373 | targetFormat.c_str(),
|
---|
9374 | targetLocation.c_str(),
|
---|
9375 | true /* fMoveByRename */,
|
---|
9376 | 0 /* cbSize */,
|
---|
9377 | VD_IMAGE_FLAGS_NONE,
|
---|
9378 | targetId.raw(),
|
---|
9379 | VD_OPEN_FLAGS_NORMAL,
|
---|
9380 | NULL /* pVDIfsOperation */,
|
---|
9381 | NULL,
|
---|
9382 | NULL);
|
---|
9383 | if (RT_FAILURE(vrc))
|
---|
9384 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9385 | tr("Could not move medium '%s'%s"),
|
---|
9386 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
9387 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
9388 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
9389 | unsigned uImageFlags;
|
---|
9390 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
9391 | if (RT_SUCCESS(vrc))
|
---|
9392 | variant = (MediumVariant_T)uImageFlags;
|
---|
9393 |
|
---|
9394 | /*
|
---|
9395 | * set current location, because VDCopy\VDCopyEx doesn't do it.
|
---|
9396 | * also reset moving flag
|
---|
9397 | */
|
---|
9398 | i_resetMoveOperationData();
|
---|
9399 | m->strLocationFull = targetLocation;
|
---|
9400 |
|
---|
9401 | }
|
---|
9402 | catch (HRESULT aRC) { rcOut = aRC; }
|
---|
9403 |
|
---|
9404 | }
|
---|
9405 | catch (HRESULT aRC) { rcOut = aRC; }
|
---|
9406 |
|
---|
9407 | VDDestroy(hdd);
|
---|
9408 | }
|
---|
9409 | catch (HRESULT aRC) { rcOut = aRC; }
|
---|
9410 |
|
---|
9411 | ErrorInfoKeeper eik;
|
---|
9412 | MultiResult mrc(rcOut);
|
---|
9413 |
|
---|
9414 | // now, at the end of this task (always asynchronous), save the settings
|
---|
9415 | if (SUCCEEDED(mrc))
|
---|
9416 | {
|
---|
9417 | // save the settings
|
---|
9418 | i_markRegistriesModified();
|
---|
9419 | /* collect multiple errors */
|
---|
9420 | eik.restore();
|
---|
9421 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
9422 | eik.fetch();
|
---|
9423 | }
|
---|
9424 |
|
---|
9425 | /* Everything is explicitly unlocked when the task exits,
|
---|
9426 | * as the task destruction also destroys the source chain. */
|
---|
9427 |
|
---|
9428 | task.mpMediumLockList->Clear();
|
---|
9429 |
|
---|
9430 | return mrc;
|
---|
9431 | }
|
---|
9432 |
|
---|
9433 | /**
|
---|
9434 | * Implementation code for the "delete" task.
|
---|
9435 | *
|
---|
9436 | * This task always gets started from Medium::deleteStorage() and can run
|
---|
9437 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
9438 | * that function.
|
---|
9439 | *
|
---|
9440 | * @param task
|
---|
9441 | * @return
|
---|
9442 | */
|
---|
9443 | HRESULT Medium::i_taskDeleteHandler(Medium::DeleteTask &task)
|
---|
9444 | {
|
---|
9445 | NOREF(task);
|
---|
9446 | HRESULT rc = S_OK;
|
---|
9447 |
|
---|
9448 | try
|
---|
9449 | {
|
---|
9450 | /* The lock is also used as a signal from the task initiator (which
|
---|
9451 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
9452 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9453 |
|
---|
9454 | PVDISK hdd;
|
---|
9455 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9456 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9457 |
|
---|
9458 | Utf8Str format(m->strFormat);
|
---|
9459 | Utf8Str location(m->strLocationFull);
|
---|
9460 |
|
---|
9461 | /* unlock before the potentially lengthy operation */
|
---|
9462 | Assert(m->state == MediumState_Deleting);
|
---|
9463 | thisLock.release();
|
---|
9464 |
|
---|
9465 | try
|
---|
9466 | {
|
---|
9467 | vrc = VDOpen(hdd,
|
---|
9468 | format.c_str(),
|
---|
9469 | location.c_str(),
|
---|
9470 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
9471 | m->vdImageIfaces);
|
---|
9472 | if (RT_SUCCESS(vrc))
|
---|
9473 | vrc = VDClose(hdd, true /* fDelete */);
|
---|
9474 |
|
---|
9475 | if (RT_FAILURE(vrc) && vrc != VERR_FILE_NOT_FOUND)
|
---|
9476 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9477 | tr("Could not delete the medium storage unit '%s'%s"),
|
---|
9478 | location.c_str(), i_vdError(vrc).c_str());
|
---|
9479 |
|
---|
9480 | }
|
---|
9481 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9482 |
|
---|
9483 | VDDestroy(hdd);
|
---|
9484 | }
|
---|
9485 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9486 |
|
---|
9487 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9488 |
|
---|
9489 | /* go to the NotCreated state even on failure since the storage
|
---|
9490 | * may have been already partially deleted and cannot be used any
|
---|
9491 | * more. One will be able to manually re-open the storage if really
|
---|
9492 | * needed to re-register it. */
|
---|
9493 | m->state = MediumState_NotCreated;
|
---|
9494 |
|
---|
9495 | /* Reset UUID to prevent Create* from reusing it again */
|
---|
9496 | unconst(m->id).clear();
|
---|
9497 |
|
---|
9498 | return rc;
|
---|
9499 | }
|
---|
9500 |
|
---|
9501 | /**
|
---|
9502 | * Implementation code for the "reset" task.
|
---|
9503 | *
|
---|
9504 | * This always gets started asynchronously from Medium::Reset().
|
---|
9505 | *
|
---|
9506 | * @param task
|
---|
9507 | * @return
|
---|
9508 | */
|
---|
9509 | HRESULT Medium::i_taskResetHandler(Medium::ResetTask &task)
|
---|
9510 | {
|
---|
9511 | HRESULT rc = S_OK;
|
---|
9512 |
|
---|
9513 | uint64_t size = 0, logicalSize = 0;
|
---|
9514 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
9515 |
|
---|
9516 | try
|
---|
9517 | {
|
---|
9518 | /* The lock is also used as a signal from the task initiator (which
|
---|
9519 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
9520 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9521 |
|
---|
9522 | /// @todo Below we use a pair of delete/create operations to reset
|
---|
9523 | /// the diff contents but the most efficient way will of course be
|
---|
9524 | /// to add a VDResetDiff() API call
|
---|
9525 |
|
---|
9526 | PVDISK hdd;
|
---|
9527 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9528 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9529 |
|
---|
9530 | Guid id = m->id;
|
---|
9531 | Utf8Str format(m->strFormat);
|
---|
9532 | Utf8Str location(m->strLocationFull);
|
---|
9533 |
|
---|
9534 | Medium *pParent = m->pParent;
|
---|
9535 | Guid parentId = pParent->m->id;
|
---|
9536 | Utf8Str parentFormat(pParent->m->strFormat);
|
---|
9537 | Utf8Str parentLocation(pParent->m->strLocationFull);
|
---|
9538 |
|
---|
9539 | Assert(m->state == MediumState_LockedWrite);
|
---|
9540 |
|
---|
9541 | /* unlock before the potentially lengthy operation */
|
---|
9542 | thisLock.release();
|
---|
9543 |
|
---|
9544 | try
|
---|
9545 | {
|
---|
9546 | /* Open all media in the target chain but the last. */
|
---|
9547 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
9548 | task.mpMediumLockList->GetBegin();
|
---|
9549 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
9550 | task.mpMediumLockList->GetEnd();
|
---|
9551 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
9552 | it != targetListEnd;
|
---|
9553 | ++it)
|
---|
9554 | {
|
---|
9555 | const MediumLock &mediumLock = *it;
|
---|
9556 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9557 |
|
---|
9558 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9559 |
|
---|
9560 | /* sanity check, "this" is checked above */
|
---|
9561 | Assert( pMedium == this
|
---|
9562 | || pMedium->m->state == MediumState_LockedRead);
|
---|
9563 |
|
---|
9564 | /* Open all media in appropriate mode. */
|
---|
9565 | vrc = VDOpen(hdd,
|
---|
9566 | pMedium->m->strFormat.c_str(),
|
---|
9567 | pMedium->m->strLocationFull.c_str(),
|
---|
9568 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
9569 | pMedium->m->vdImageIfaces);
|
---|
9570 | if (RT_FAILURE(vrc))
|
---|
9571 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9572 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9573 | pMedium->m->strLocationFull.c_str(),
|
---|
9574 | i_vdError(vrc).c_str());
|
---|
9575 |
|
---|
9576 | /* Done when we hit the media which should be reset */
|
---|
9577 | if (pMedium == this)
|
---|
9578 | break;
|
---|
9579 | }
|
---|
9580 |
|
---|
9581 | /* first, delete the storage unit */
|
---|
9582 | vrc = VDClose(hdd, true /* fDelete */);
|
---|
9583 | if (RT_FAILURE(vrc))
|
---|
9584 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9585 | tr("Could not delete the medium storage unit '%s'%s"),
|
---|
9586 | location.c_str(), i_vdError(vrc).c_str());
|
---|
9587 |
|
---|
9588 | /* next, create it again */
|
---|
9589 | vrc = VDOpen(hdd,
|
---|
9590 | parentFormat.c_str(),
|
---|
9591 | parentLocation.c_str(),
|
---|
9592 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
9593 | m->vdImageIfaces);
|
---|
9594 | if (RT_FAILURE(vrc))
|
---|
9595 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9596 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9597 | parentLocation.c_str(), i_vdError(vrc).c_str());
|
---|
9598 |
|
---|
9599 | vrc = VDCreateDiff(hdd,
|
---|
9600 | format.c_str(),
|
---|
9601 | location.c_str(),
|
---|
9602 | /// @todo use the same medium variant as before
|
---|
9603 | VD_IMAGE_FLAGS_NONE,
|
---|
9604 | NULL,
|
---|
9605 | id.raw(),
|
---|
9606 | parentId.raw(),
|
---|
9607 | VD_OPEN_FLAGS_NORMAL,
|
---|
9608 | m->vdImageIfaces,
|
---|
9609 | task.mVDOperationIfaces);
|
---|
9610 | if (RT_FAILURE(vrc))
|
---|
9611 | {
|
---|
9612 | if (vrc == VERR_VD_INVALID_TYPE)
|
---|
9613 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9614 | tr("Parameters for creating the differencing medium storage unit '%s' are invalid%s"),
|
---|
9615 | location.c_str(), i_vdError(vrc).c_str());
|
---|
9616 | else
|
---|
9617 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9618 | tr("Could not create the differencing medium storage unit '%s'%s"),
|
---|
9619 | location.c_str(), i_vdError(vrc).c_str());
|
---|
9620 | }
|
---|
9621 |
|
---|
9622 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
9623 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
9624 | unsigned uImageFlags;
|
---|
9625 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
9626 | if (RT_SUCCESS(vrc))
|
---|
9627 | variant = (MediumVariant_T)uImageFlags;
|
---|
9628 | }
|
---|
9629 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9630 |
|
---|
9631 | VDDestroy(hdd);
|
---|
9632 | }
|
---|
9633 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9634 |
|
---|
9635 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9636 |
|
---|
9637 | m->size = size;
|
---|
9638 | m->logicalSize = logicalSize;
|
---|
9639 | m->variant = variant;
|
---|
9640 |
|
---|
9641 | /* Everything is explicitly unlocked when the task exits,
|
---|
9642 | * as the task destruction also destroys the media chain. */
|
---|
9643 |
|
---|
9644 | return rc;
|
---|
9645 | }
|
---|
9646 |
|
---|
9647 | /**
|
---|
9648 | * Implementation code for the "compact" task.
|
---|
9649 | *
|
---|
9650 | * @param task
|
---|
9651 | * @return
|
---|
9652 | */
|
---|
9653 | HRESULT Medium::i_taskCompactHandler(Medium::CompactTask &task)
|
---|
9654 | {
|
---|
9655 | HRESULT rc = S_OK;
|
---|
9656 |
|
---|
9657 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
9658 | * signal from the task initiator (which releases it only after
|
---|
9659 | * RTThreadCreate()) that we can start the job. */
|
---|
9660 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9661 |
|
---|
9662 | try
|
---|
9663 | {
|
---|
9664 | PVDISK hdd;
|
---|
9665 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9666 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9667 |
|
---|
9668 | try
|
---|
9669 | {
|
---|
9670 | /* Open all media in the chain. */
|
---|
9671 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
9672 | task.mpMediumLockList->GetBegin();
|
---|
9673 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
9674 | task.mpMediumLockList->GetEnd();
|
---|
9675 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
9676 | mediumListEnd;
|
---|
9677 | --mediumListLast;
|
---|
9678 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
9679 | it != mediumListEnd;
|
---|
9680 | ++it)
|
---|
9681 | {
|
---|
9682 | const MediumLock &mediumLock = *it;
|
---|
9683 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9684 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9685 |
|
---|
9686 | /* sanity check */
|
---|
9687 | if (it == mediumListLast)
|
---|
9688 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
9689 | else
|
---|
9690 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
9691 |
|
---|
9692 | /* Open all media but last in read-only mode. Do not handle
|
---|
9693 | * shareable media, as compaction and sharing are mutually
|
---|
9694 | * exclusive. */
|
---|
9695 | vrc = VDOpen(hdd,
|
---|
9696 | pMedium->m->strFormat.c_str(),
|
---|
9697 | pMedium->m->strLocationFull.c_str(),
|
---|
9698 | m->uOpenFlagsDef | (it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
9699 | pMedium->m->vdImageIfaces);
|
---|
9700 | if (RT_FAILURE(vrc))
|
---|
9701 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9702 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9703 | pMedium->m->strLocationFull.c_str(),
|
---|
9704 | i_vdError(vrc).c_str());
|
---|
9705 | }
|
---|
9706 |
|
---|
9707 | Assert(m->state == MediumState_LockedWrite);
|
---|
9708 |
|
---|
9709 | Utf8Str location(m->strLocationFull);
|
---|
9710 |
|
---|
9711 | /* unlock before the potentially lengthy operation */
|
---|
9712 | thisLock.release();
|
---|
9713 |
|
---|
9714 | vrc = VDCompact(hdd, VD_LAST_IMAGE, task.mVDOperationIfaces);
|
---|
9715 | if (RT_FAILURE(vrc))
|
---|
9716 | {
|
---|
9717 | if (vrc == VERR_NOT_SUPPORTED)
|
---|
9718 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
9719 | tr("Compacting is not yet supported for medium '%s'"),
|
---|
9720 | location.c_str());
|
---|
9721 | else if (vrc == VERR_NOT_IMPLEMENTED)
|
---|
9722 | throw setErrorBoth(E_NOTIMPL, vrc,
|
---|
9723 | tr("Compacting is not implemented, medium '%s'"),
|
---|
9724 | location.c_str());
|
---|
9725 | else
|
---|
9726 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9727 | tr("Could not compact medium '%s'%s"),
|
---|
9728 | location.c_str(),
|
---|
9729 | i_vdError(vrc).c_str());
|
---|
9730 | }
|
---|
9731 | }
|
---|
9732 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9733 |
|
---|
9734 | VDDestroy(hdd);
|
---|
9735 | }
|
---|
9736 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9737 |
|
---|
9738 | /* Everything is explicitly unlocked when the task exits,
|
---|
9739 | * as the task destruction also destroys the media chain. */
|
---|
9740 |
|
---|
9741 | return rc;
|
---|
9742 | }
|
---|
9743 |
|
---|
9744 | /**
|
---|
9745 | * Implementation code for the "resize" task.
|
---|
9746 | *
|
---|
9747 | * @param task
|
---|
9748 | * @return
|
---|
9749 | */
|
---|
9750 | HRESULT Medium::i_taskResizeHandler(Medium::ResizeTask &task)
|
---|
9751 | {
|
---|
9752 | HRESULT rc = S_OK;
|
---|
9753 |
|
---|
9754 | uint64_t size = 0, logicalSize = 0;
|
---|
9755 |
|
---|
9756 | try
|
---|
9757 | {
|
---|
9758 | /* The lock is also used as a signal from the task initiator (which
|
---|
9759 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
9760 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9761 |
|
---|
9762 | PVDISK hdd;
|
---|
9763 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9764 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9765 |
|
---|
9766 | try
|
---|
9767 | {
|
---|
9768 | /* Open all media in the chain. */
|
---|
9769 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
9770 | task.mpMediumLockList->GetBegin();
|
---|
9771 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
9772 | task.mpMediumLockList->GetEnd();
|
---|
9773 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
9774 | mediumListEnd;
|
---|
9775 | --mediumListLast;
|
---|
9776 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
9777 | it != mediumListEnd;
|
---|
9778 | ++it)
|
---|
9779 | {
|
---|
9780 | const MediumLock &mediumLock = *it;
|
---|
9781 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9782 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9783 |
|
---|
9784 | /* sanity check */
|
---|
9785 | if (it == mediumListLast)
|
---|
9786 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
9787 | else
|
---|
9788 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
9789 |
|
---|
9790 | /* Open all media but last in read-only mode. Do not handle
|
---|
9791 | * shareable media, as compaction and sharing are mutually
|
---|
9792 | * exclusive. */
|
---|
9793 | vrc = VDOpen(hdd,
|
---|
9794 | pMedium->m->strFormat.c_str(),
|
---|
9795 | pMedium->m->strLocationFull.c_str(),
|
---|
9796 | m->uOpenFlagsDef | (it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
9797 | pMedium->m->vdImageIfaces);
|
---|
9798 | if (RT_FAILURE(vrc))
|
---|
9799 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9800 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9801 | pMedium->m->strLocationFull.c_str(),
|
---|
9802 | i_vdError(vrc).c_str());
|
---|
9803 | }
|
---|
9804 |
|
---|
9805 | Assert(m->state == MediumState_LockedWrite);
|
---|
9806 |
|
---|
9807 | Utf8Str location(m->strLocationFull);
|
---|
9808 |
|
---|
9809 | /* unlock before the potentially lengthy operation */
|
---|
9810 | thisLock.release();
|
---|
9811 |
|
---|
9812 | VDGEOMETRY geo = {0, 0, 0}; /* auto */
|
---|
9813 | vrc = VDResize(hdd, task.mSize, &geo, &geo, task.mVDOperationIfaces);
|
---|
9814 | if (RT_FAILURE(vrc))
|
---|
9815 | {
|
---|
9816 | if (vrc == VERR_NOT_SUPPORTED)
|
---|
9817 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
9818 | tr("Resizing to new size %llu is not yet supported for medium '%s'"),
|
---|
9819 | task.mSize, location.c_str());
|
---|
9820 | else if (vrc == VERR_NOT_IMPLEMENTED)
|
---|
9821 | throw setErrorBoth(E_NOTIMPL, vrc,
|
---|
9822 | tr("Resiting is not implemented, medium '%s'"),
|
---|
9823 | location.c_str());
|
---|
9824 | else
|
---|
9825 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9826 | tr("Could not resize medium '%s'%s"),
|
---|
9827 | location.c_str(),
|
---|
9828 | i_vdError(vrc).c_str());
|
---|
9829 | }
|
---|
9830 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
9831 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
9832 | }
|
---|
9833 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9834 |
|
---|
9835 | VDDestroy(hdd);
|
---|
9836 | }
|
---|
9837 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9838 |
|
---|
9839 | if (SUCCEEDED(rc))
|
---|
9840 | {
|
---|
9841 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9842 | m->size = size;
|
---|
9843 | m->logicalSize = logicalSize;
|
---|
9844 | }
|
---|
9845 |
|
---|
9846 | /* Everything is explicitly unlocked when the task exits,
|
---|
9847 | * as the task destruction also destroys the media chain. */
|
---|
9848 |
|
---|
9849 | return rc;
|
---|
9850 | }
|
---|
9851 |
|
---|
9852 | /**
|
---|
9853 | * Implementation code for the "import" task.
|
---|
9854 | *
|
---|
9855 | * This only gets started from Medium::importFile() and always runs
|
---|
9856 | * asynchronously. It potentially touches the media registry, so we
|
---|
9857 | * always save the VirtualBox.xml file when we're done here.
|
---|
9858 | *
|
---|
9859 | * @param task
|
---|
9860 | * @return
|
---|
9861 | */
|
---|
9862 | HRESULT Medium::i_taskImportHandler(Medium::ImportTask &task)
|
---|
9863 | {
|
---|
9864 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
9865 | * to lock order violations, it probably causes lock order issues related
|
---|
9866 | * to the AutoCaller usage. */
|
---|
9867 | HRESULT rcTmp = S_OK;
|
---|
9868 |
|
---|
9869 | const ComObjPtr<Medium> &pParent = task.mParent;
|
---|
9870 |
|
---|
9871 | bool fCreatingTarget = false;
|
---|
9872 |
|
---|
9873 | uint64_t size = 0, logicalSize = 0;
|
---|
9874 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
9875 | bool fGenerateUuid = false;
|
---|
9876 |
|
---|
9877 | try
|
---|
9878 | {
|
---|
9879 | if (!pParent.isNull())
|
---|
9880 | if (pParent->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
9881 | {
|
---|
9882 | AutoReadLock plock(pParent COMMA_LOCKVAL_SRC_POS);
|
---|
9883 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
9884 | tr("Cannot import image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
9885 | pParent->m->strLocationFull.c_str());
|
---|
9886 | }
|
---|
9887 |
|
---|
9888 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
9889 | * signal from the task initiator (which releases it only after
|
---|
9890 | * RTThreadCreate()) that we can start the job. */
|
---|
9891 | AutoMultiWriteLock2 thisLock(this, pParent COMMA_LOCKVAL_SRC_POS);
|
---|
9892 |
|
---|
9893 | fCreatingTarget = m->state == MediumState_Creating;
|
---|
9894 |
|
---|
9895 | /* The object may request a specific UUID (through a special form of
|
---|
9896 | * the moveTo() argument). Otherwise we have to generate it */
|
---|
9897 | Guid targetId = m->id;
|
---|
9898 |
|
---|
9899 | fGenerateUuid = targetId.isZero();
|
---|
9900 | if (fGenerateUuid)
|
---|
9901 | {
|
---|
9902 | targetId.create();
|
---|
9903 | /* VirtualBox::i_registerMedium() will need UUID */
|
---|
9904 | unconst(m->id) = targetId;
|
---|
9905 | }
|
---|
9906 |
|
---|
9907 |
|
---|
9908 | PVDISK hdd;
|
---|
9909 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9910 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9911 |
|
---|
9912 | try
|
---|
9913 | {
|
---|
9914 | /* Open source medium. */
|
---|
9915 | vrc = VDOpen(hdd,
|
---|
9916 | task.mFormat->i_getId().c_str(),
|
---|
9917 | task.mFilename.c_str(),
|
---|
9918 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SEQUENTIAL | m->uOpenFlagsDef,
|
---|
9919 | task.mVDImageIfaces);
|
---|
9920 | if (RT_FAILURE(vrc))
|
---|
9921 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9922 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9923 | task.mFilename.c_str(),
|
---|
9924 | i_vdError(vrc).c_str());
|
---|
9925 |
|
---|
9926 | Utf8Str targetFormat(m->strFormat);
|
---|
9927 | Utf8Str targetLocation(m->strLocationFull);
|
---|
9928 | uint64_t capabilities = task.mFormat->i_getCapabilities();
|
---|
9929 |
|
---|
9930 | Assert( m->state == MediumState_Creating
|
---|
9931 | || m->state == MediumState_LockedWrite);
|
---|
9932 | Assert( pParent.isNull()
|
---|
9933 | || pParent->m->state == MediumState_LockedRead);
|
---|
9934 |
|
---|
9935 | /* unlock before the potentially lengthy operation */
|
---|
9936 | thisLock.release();
|
---|
9937 |
|
---|
9938 | /* ensure the target directory exists */
|
---|
9939 | if (capabilities & MediumFormatCapabilities_File)
|
---|
9940 | {
|
---|
9941 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
9942 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
9943 | if (FAILED(rc))
|
---|
9944 | throw rc;
|
---|
9945 | }
|
---|
9946 |
|
---|
9947 | PVDISK targetHdd;
|
---|
9948 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &targetHdd);
|
---|
9949 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9950 |
|
---|
9951 | try
|
---|
9952 | {
|
---|
9953 | /* Open all media in the target chain. */
|
---|
9954 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
9955 | task.mpTargetMediumLockList->GetBegin();
|
---|
9956 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
9957 | task.mpTargetMediumLockList->GetEnd();
|
---|
9958 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
9959 | it != targetListEnd;
|
---|
9960 | ++it)
|
---|
9961 | {
|
---|
9962 | const MediumLock &mediumLock = *it;
|
---|
9963 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9964 |
|
---|
9965 | /* If the target medium is not created yet there's no
|
---|
9966 | * reason to open it. */
|
---|
9967 | if (pMedium == this && fCreatingTarget)
|
---|
9968 | continue;
|
---|
9969 |
|
---|
9970 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9971 |
|
---|
9972 | /* sanity check */
|
---|
9973 | Assert( pMedium->m->state == MediumState_LockedRead
|
---|
9974 | || pMedium->m->state == MediumState_LockedWrite);
|
---|
9975 |
|
---|
9976 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
9977 | if (pMedium->m->state != MediumState_LockedWrite)
|
---|
9978 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
9979 | if (pMedium->m->type == MediumType_Shareable)
|
---|
9980 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
9981 |
|
---|
9982 | /* Open all media in appropriate mode. */
|
---|
9983 | vrc = VDOpen(targetHdd,
|
---|
9984 | pMedium->m->strFormat.c_str(),
|
---|
9985 | pMedium->m->strLocationFull.c_str(),
|
---|
9986 | uOpenFlags | m->uOpenFlagsDef,
|
---|
9987 | pMedium->m->vdImageIfaces);
|
---|
9988 | if (RT_FAILURE(vrc))
|
---|
9989 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9990 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9991 | pMedium->m->strLocationFull.c_str(),
|
---|
9992 | i_vdError(vrc).c_str());
|
---|
9993 | }
|
---|
9994 |
|
---|
9995 | vrc = VDCopy(hdd,
|
---|
9996 | VD_LAST_IMAGE,
|
---|
9997 | targetHdd,
|
---|
9998 | targetFormat.c_str(),
|
---|
9999 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
10000 | false /* fMoveByRename */,
|
---|
10001 | 0 /* cbSize */,
|
---|
10002 | task.mVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted),
|
---|
10003 | targetId.raw(),
|
---|
10004 | VD_OPEN_FLAGS_NORMAL,
|
---|
10005 | NULL /* pVDIfsOperation */,
|
---|
10006 | m->vdImageIfaces,
|
---|
10007 | task.mVDOperationIfaces);
|
---|
10008 | if (RT_FAILURE(vrc))
|
---|
10009 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10010 | tr("Could not create the imported medium '%s'%s"),
|
---|
10011 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
10012 |
|
---|
10013 | size = VDGetFileSize(targetHdd, VD_LAST_IMAGE);
|
---|
10014 | logicalSize = VDGetSize(targetHdd, VD_LAST_IMAGE);
|
---|
10015 | unsigned uImageFlags;
|
---|
10016 | vrc = VDGetImageFlags(targetHdd, 0, &uImageFlags);
|
---|
10017 | if (RT_SUCCESS(vrc))
|
---|
10018 | variant = (MediumVariant_T)uImageFlags;
|
---|
10019 | }
|
---|
10020 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
10021 |
|
---|
10022 | VDDestroy(targetHdd);
|
---|
10023 | }
|
---|
10024 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
10025 |
|
---|
10026 | VDDestroy(hdd);
|
---|
10027 | }
|
---|
10028 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
10029 |
|
---|
10030 | ErrorInfoKeeper eik;
|
---|
10031 | MultiResult mrc(rcTmp);
|
---|
10032 |
|
---|
10033 | /* Only do the parent changes for newly created media. */
|
---|
10034 | if (SUCCEEDED(mrc) && fCreatingTarget)
|
---|
10035 | {
|
---|
10036 | /* we set m->pParent & children() */
|
---|
10037 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
10038 |
|
---|
10039 | Assert(m->pParent.isNull());
|
---|
10040 |
|
---|
10041 | if (pParent)
|
---|
10042 | {
|
---|
10043 | /* Associate the imported medium with the parent and deassociate
|
---|
10044 | * from VirtualBox. Depth check above. */
|
---|
10045 | i_setParent(pParent);
|
---|
10046 |
|
---|
10047 | /* register with mVirtualBox as the last step and move to
|
---|
10048 | * Created state only on success (leaving an orphan file is
|
---|
10049 | * better than breaking media registry consistency) */
|
---|
10050 | eik.restore();
|
---|
10051 | ComObjPtr<Medium> pMedium;
|
---|
10052 | mrc = pParent->m->pVirtualBox->i_registerMedium(this, &pMedium,
|
---|
10053 | treeLock);
|
---|
10054 | Assert(this == pMedium);
|
---|
10055 | eik.fetch();
|
---|
10056 |
|
---|
10057 | if (FAILED(mrc))
|
---|
10058 | /* break parent association on failure to register */
|
---|
10059 | this->i_deparent(); // removes target from parent
|
---|
10060 | }
|
---|
10061 | else
|
---|
10062 | {
|
---|
10063 | /* just register */
|
---|
10064 | eik.restore();
|
---|
10065 | ComObjPtr<Medium> pMedium;
|
---|
10066 | mrc = m->pVirtualBox->i_registerMedium(this, &pMedium, treeLock);
|
---|
10067 | Assert(this == pMedium);
|
---|
10068 | eik.fetch();
|
---|
10069 | }
|
---|
10070 | }
|
---|
10071 |
|
---|
10072 | if (fCreatingTarget)
|
---|
10073 | {
|
---|
10074 | AutoWriteLock mLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
10075 |
|
---|
10076 | if (SUCCEEDED(mrc))
|
---|
10077 | {
|
---|
10078 | m->state = MediumState_Created;
|
---|
10079 |
|
---|
10080 | m->size = size;
|
---|
10081 | m->logicalSize = logicalSize;
|
---|
10082 | m->variant = variant;
|
---|
10083 | }
|
---|
10084 | else
|
---|
10085 | {
|
---|
10086 | /* back to NotCreated on failure */
|
---|
10087 | m->state = MediumState_NotCreated;
|
---|
10088 |
|
---|
10089 | /* reset UUID to prevent it from being reused next time */
|
---|
10090 | if (fGenerateUuid)
|
---|
10091 | unconst(m->id).clear();
|
---|
10092 | }
|
---|
10093 | }
|
---|
10094 |
|
---|
10095 | // now, at the end of this task (always asynchronous), save the settings
|
---|
10096 | {
|
---|
10097 | // save the settings
|
---|
10098 | i_markRegistriesModified();
|
---|
10099 | /* collect multiple errors */
|
---|
10100 | eik.restore();
|
---|
10101 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
10102 | eik.fetch();
|
---|
10103 | }
|
---|
10104 |
|
---|
10105 | /* Everything is explicitly unlocked when the task exits,
|
---|
10106 | * as the task destruction also destroys the target chain. */
|
---|
10107 |
|
---|
10108 | /* Make sure the target chain is released early, otherwise it can
|
---|
10109 | * lead to deadlocks with concurrent IAppliance activities. */
|
---|
10110 | task.mpTargetMediumLockList->Clear();
|
---|
10111 |
|
---|
10112 | return mrc;
|
---|
10113 | }
|
---|
10114 |
|
---|
10115 | /**
|
---|
10116 | * Sets up the encryption settings for a filter.
|
---|
10117 | */
|
---|
10118 | void Medium::i_taskEncryptSettingsSetup(MediumCryptoFilterSettings *pSettings, const char *pszCipher,
|
---|
10119 | const char *pszKeyStore, const char *pszPassword,
|
---|
10120 | bool fCreateKeyStore)
|
---|
10121 | {
|
---|
10122 | pSettings->pszCipher = pszCipher;
|
---|
10123 | pSettings->pszPassword = pszPassword;
|
---|
10124 | pSettings->pszKeyStoreLoad = pszKeyStore;
|
---|
10125 | pSettings->fCreateKeyStore = fCreateKeyStore;
|
---|
10126 | pSettings->pbDek = NULL;
|
---|
10127 | pSettings->cbDek = 0;
|
---|
10128 | pSettings->vdFilterIfaces = NULL;
|
---|
10129 |
|
---|
10130 | pSettings->vdIfCfg.pfnAreKeysValid = i_vdCryptoConfigAreKeysValid;
|
---|
10131 | pSettings->vdIfCfg.pfnQuerySize = i_vdCryptoConfigQuerySize;
|
---|
10132 | pSettings->vdIfCfg.pfnQuery = i_vdCryptoConfigQuery;
|
---|
10133 | pSettings->vdIfCfg.pfnQueryBytes = NULL;
|
---|
10134 |
|
---|
10135 | pSettings->vdIfCrypto.pfnKeyRetain = i_vdCryptoKeyRetain;
|
---|
10136 | pSettings->vdIfCrypto.pfnKeyRelease = i_vdCryptoKeyRelease;
|
---|
10137 | pSettings->vdIfCrypto.pfnKeyStorePasswordRetain = i_vdCryptoKeyStorePasswordRetain;
|
---|
10138 | pSettings->vdIfCrypto.pfnKeyStorePasswordRelease = i_vdCryptoKeyStorePasswordRelease;
|
---|
10139 | pSettings->vdIfCrypto.pfnKeyStoreSave = i_vdCryptoKeyStoreSave;
|
---|
10140 | pSettings->vdIfCrypto.pfnKeyStoreReturnParameters = i_vdCryptoKeyStoreReturnParameters;
|
---|
10141 |
|
---|
10142 | int vrc = VDInterfaceAdd(&pSettings->vdIfCfg.Core,
|
---|
10143 | "Medium::vdInterfaceCfgCrypto",
|
---|
10144 | VDINTERFACETYPE_CONFIG, pSettings,
|
---|
10145 | sizeof(VDINTERFACECONFIG), &pSettings->vdFilterIfaces);
|
---|
10146 | AssertRC(vrc);
|
---|
10147 |
|
---|
10148 | vrc = VDInterfaceAdd(&pSettings->vdIfCrypto.Core,
|
---|
10149 | "Medium::vdInterfaceCrypto",
|
---|
10150 | VDINTERFACETYPE_CRYPTO, pSettings,
|
---|
10151 | sizeof(VDINTERFACECRYPTO), &pSettings->vdFilterIfaces);
|
---|
10152 | AssertRC(vrc);
|
---|
10153 | }
|
---|
10154 |
|
---|
10155 | /**
|
---|
10156 | * Implementation code for the "encrypt" task.
|
---|
10157 | *
|
---|
10158 | * @param task
|
---|
10159 | * @return
|
---|
10160 | */
|
---|
10161 | HRESULT Medium::i_taskEncryptHandler(Medium::EncryptTask &task)
|
---|
10162 | {
|
---|
10163 | # ifndef VBOX_WITH_EXTPACK
|
---|
10164 | RT_NOREF(task);
|
---|
10165 | # endif
|
---|
10166 | HRESULT rc = S_OK;
|
---|
10167 |
|
---|
10168 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
10169 | * signal from the task initiator (which releases it only after
|
---|
10170 | * RTThreadCreate()) that we can start the job. */
|
---|
10171 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
10172 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
10173 |
|
---|
10174 | try
|
---|
10175 | {
|
---|
10176 | # ifdef VBOX_WITH_EXTPACK
|
---|
10177 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
10178 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
10179 | {
|
---|
10180 | /* Load the plugin */
|
---|
10181 | Utf8Str strPlugin;
|
---|
10182 | rc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
10183 | if (SUCCEEDED(rc))
|
---|
10184 | {
|
---|
10185 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
10186 | if (RT_FAILURE(vrc))
|
---|
10187 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
10188 | tr("Encrypting the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
10189 | i_vdError(vrc).c_str());
|
---|
10190 | }
|
---|
10191 | else
|
---|
10192 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
10193 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
10194 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
10195 | }
|
---|
10196 | else
|
---|
10197 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
10198 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
10199 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
10200 |
|
---|
10201 | PVDISK pDisk = NULL;
|
---|
10202 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
10203 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
10204 |
|
---|
10205 | MediumCryptoFilterSettings CryptoSettingsRead;
|
---|
10206 | MediumCryptoFilterSettings CryptoSettingsWrite;
|
---|
10207 |
|
---|
10208 | void *pvBuf = NULL;
|
---|
10209 | const char *pszPasswordNew = NULL;
|
---|
10210 | try
|
---|
10211 | {
|
---|
10212 | /* Set up disk encryption filters. */
|
---|
10213 | if (task.mstrCurrentPassword.isEmpty())
|
---|
10214 | {
|
---|
10215 | /*
|
---|
10216 | * Query whether the medium property indicating that encryption is
|
---|
10217 | * configured is existing.
|
---|
10218 | */
|
---|
10219 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
10220 | if (it != pBase->m->mapProperties.end())
|
---|
10221 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
10222 | tr("The password given for the encrypted image is incorrect"));
|
---|
10223 | }
|
---|
10224 | else
|
---|
10225 | {
|
---|
10226 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
10227 | if (it == pBase->m->mapProperties.end())
|
---|
10228 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10229 | tr("The image is not configured for encryption"));
|
---|
10230 |
|
---|
10231 | i_taskEncryptSettingsSetup(&CryptoSettingsRead, NULL, it->second.c_str(), task.mstrCurrentPassword.c_str(),
|
---|
10232 | false /* fCreateKeyStore */);
|
---|
10233 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ, CryptoSettingsRead.vdFilterIfaces);
|
---|
10234 | if (vrc == VERR_VD_PASSWORD_INCORRECT)
|
---|
10235 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
10236 | tr("The password to decrypt the image is incorrect"));
|
---|
10237 | else if (RT_FAILURE(vrc))
|
---|
10238 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10239 | tr("Failed to load the decryption filter: %s"),
|
---|
10240 | i_vdError(vrc).c_str());
|
---|
10241 | }
|
---|
10242 |
|
---|
10243 | if (task.mstrCipher.isNotEmpty())
|
---|
10244 | {
|
---|
10245 | if ( task.mstrNewPassword.isEmpty()
|
---|
10246 | && task.mstrNewPasswordId.isEmpty()
|
---|
10247 | && task.mstrCurrentPassword.isNotEmpty())
|
---|
10248 | {
|
---|
10249 | /* An empty password and password ID will default to the current password. */
|
---|
10250 | pszPasswordNew = task.mstrCurrentPassword.c_str();
|
---|
10251 | }
|
---|
10252 | else if (task.mstrNewPassword.isEmpty())
|
---|
10253 | throw setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
10254 | tr("A password must be given for the image encryption"));
|
---|
10255 | else if (task.mstrNewPasswordId.isEmpty())
|
---|
10256 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10257 | tr("A valid identifier for the password must be given"));
|
---|
10258 | else
|
---|
10259 | pszPasswordNew = task.mstrNewPassword.c_str();
|
---|
10260 |
|
---|
10261 | i_taskEncryptSettingsSetup(&CryptoSettingsWrite, task.mstrCipher.c_str(), NULL,
|
---|
10262 | pszPasswordNew, true /* fCreateKeyStore */);
|
---|
10263 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_WRITE, CryptoSettingsWrite.vdFilterIfaces);
|
---|
10264 | if (RT_FAILURE(vrc))
|
---|
10265 | throw setErrorBoth(VBOX_E_INVALID_OBJECT_STATE, vrc,
|
---|
10266 | tr("Failed to load the encryption filter: %s"),
|
---|
10267 | i_vdError(vrc).c_str());
|
---|
10268 | }
|
---|
10269 | else if (task.mstrNewPasswordId.isNotEmpty() || task.mstrNewPassword.isNotEmpty())
|
---|
10270 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10271 | tr("The password and password identifier must be empty if the output should be unencrypted"));
|
---|
10272 |
|
---|
10273 | /* Open all media in the chain. */
|
---|
10274 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
10275 | task.mpMediumLockList->GetBegin();
|
---|
10276 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
10277 | task.mpMediumLockList->GetEnd();
|
---|
10278 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
10279 | mediumListEnd;
|
---|
10280 | --mediumListLast;
|
---|
10281 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
10282 | it != mediumListEnd;
|
---|
10283 | ++it)
|
---|
10284 | {
|
---|
10285 | const MediumLock &mediumLock = *it;
|
---|
10286 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
10287 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
10288 |
|
---|
10289 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
10290 |
|
---|
10291 | /* Open all media but last in read-only mode. Do not handle
|
---|
10292 | * shareable media, as compaction and sharing are mutually
|
---|
10293 | * exclusive. */
|
---|
10294 | vrc = VDOpen(pDisk,
|
---|
10295 | pMedium->m->strFormat.c_str(),
|
---|
10296 | pMedium->m->strLocationFull.c_str(),
|
---|
10297 | m->uOpenFlagsDef | (it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
10298 | pMedium->m->vdImageIfaces);
|
---|
10299 | if (RT_FAILURE(vrc))
|
---|
10300 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10301 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
10302 | pMedium->m->strLocationFull.c_str(),
|
---|
10303 | i_vdError(vrc).c_str());
|
---|
10304 | }
|
---|
10305 |
|
---|
10306 | Assert(m->state == MediumState_LockedWrite);
|
---|
10307 |
|
---|
10308 | Utf8Str location(m->strLocationFull);
|
---|
10309 |
|
---|
10310 | /* unlock before the potentially lengthy operation */
|
---|
10311 | thisLock.release();
|
---|
10312 |
|
---|
10313 | vrc = VDPrepareWithFilters(pDisk, task.mVDOperationIfaces);
|
---|
10314 | if (RT_FAILURE(vrc))
|
---|
10315 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10316 | tr("Could not prepare disk images for encryption (%Rrc): %s"),
|
---|
10317 | vrc, i_vdError(vrc).c_str());
|
---|
10318 |
|
---|
10319 | thisLock.acquire();
|
---|
10320 | /* If everything went well set the new key store. */
|
---|
10321 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
10322 | if (it != pBase->m->mapProperties.end())
|
---|
10323 | pBase->m->mapProperties.erase(it);
|
---|
10324 |
|
---|
10325 | /* Delete KeyId if encryption is removed or the password did change. */
|
---|
10326 | if ( task.mstrNewPasswordId.isNotEmpty()
|
---|
10327 | || task.mstrCipher.isEmpty())
|
---|
10328 | {
|
---|
10329 | it = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
10330 | if (it != pBase->m->mapProperties.end())
|
---|
10331 | pBase->m->mapProperties.erase(it);
|
---|
10332 | }
|
---|
10333 |
|
---|
10334 | if (CryptoSettingsWrite.pszKeyStore)
|
---|
10335 | {
|
---|
10336 | pBase->m->mapProperties["CRYPT/KeyStore"] = Utf8Str(CryptoSettingsWrite.pszKeyStore);
|
---|
10337 | if (task.mstrNewPasswordId.isNotEmpty())
|
---|
10338 | pBase->m->mapProperties["CRYPT/KeyId"] = task.mstrNewPasswordId;
|
---|
10339 | }
|
---|
10340 |
|
---|
10341 | if (CryptoSettingsRead.pszCipherReturned)
|
---|
10342 | RTStrFree(CryptoSettingsRead.pszCipherReturned);
|
---|
10343 |
|
---|
10344 | if (CryptoSettingsWrite.pszCipherReturned)
|
---|
10345 | RTStrFree(CryptoSettingsWrite.pszCipherReturned);
|
---|
10346 |
|
---|
10347 | thisLock.release();
|
---|
10348 | pBase->i_markRegistriesModified();
|
---|
10349 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
10350 | }
|
---|
10351 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10352 |
|
---|
10353 | if (pvBuf)
|
---|
10354 | RTMemFree(pvBuf);
|
---|
10355 |
|
---|
10356 | VDDestroy(pDisk);
|
---|
10357 | # else
|
---|
10358 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
10359 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
10360 | # endif
|
---|
10361 | }
|
---|
10362 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10363 |
|
---|
10364 | /* Everything is explicitly unlocked when the task exits,
|
---|
10365 | * as the task destruction also destroys the media chain. */
|
---|
10366 |
|
---|
10367 | return rc;
|
---|
10368 | }
|
---|
10369 |
|
---|
10370 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|