1 | /* $Id: MediumImpl.cpp 78403 2019-05-06 18:48:00Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox COM class implementation
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2019 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 |
|
---|
18 | #define LOG_GROUP LOG_GROUP_MAIN_MEDIUM
|
---|
19 | #include "MediumImpl.h"
|
---|
20 | #include "MediumIOImpl.h"
|
---|
21 | #include "TokenImpl.h"
|
---|
22 | #include "ProgressImpl.h"
|
---|
23 | #include "SystemPropertiesImpl.h"
|
---|
24 | #include "VirtualBoxImpl.h"
|
---|
25 | #include "ExtPackManagerImpl.h"
|
---|
26 |
|
---|
27 | #include "AutoCaller.h"
|
---|
28 | #include "LoggingNew.h"
|
---|
29 | #include "ThreadTask.h"
|
---|
30 | #include "VBox/com/MultiResult.h"
|
---|
31 | #include "VBox/com/ErrorInfo.h"
|
---|
32 |
|
---|
33 | #include <VBox/err.h>
|
---|
34 | #include <VBox/settings.h>
|
---|
35 |
|
---|
36 | #include <iprt/param.h>
|
---|
37 | #include <iprt/path.h>
|
---|
38 | #include <iprt/file.h>
|
---|
39 | #include <iprt/cpp/utils.h>
|
---|
40 | #include <iprt/memsafer.h>
|
---|
41 | #include <iprt/base64.h>
|
---|
42 | #include <iprt/vfs.h>
|
---|
43 | #include <iprt/fsvfs.h>
|
---|
44 |
|
---|
45 | #include <VBox/vd.h>
|
---|
46 |
|
---|
47 | #include <algorithm>
|
---|
48 | #include <list>
|
---|
49 | #include <set>
|
---|
50 | #include <map>
|
---|
51 |
|
---|
52 |
|
---|
53 | typedef std::list<Guid> GuidList;
|
---|
54 |
|
---|
55 |
|
---|
56 | #ifdef VBOX_WITH_EXTPACK
|
---|
57 | static const char g_szVDPlugin[] = "VDPluginCrypt";
|
---|
58 | #endif
|
---|
59 |
|
---|
60 |
|
---|
61 | ////////////////////////////////////////////////////////////////////////////////
|
---|
62 | //
|
---|
63 | // Medium data definition
|
---|
64 | //
|
---|
65 | ////////////////////////////////////////////////////////////////////////////////
|
---|
66 |
|
---|
67 | /** Describes how a machine refers to this medium. */
|
---|
68 | struct BackRef
|
---|
69 | {
|
---|
70 | /** Equality predicate for stdc++. */
|
---|
71 | struct EqualsTo : public std::unary_function <BackRef, bool>
|
---|
72 | {
|
---|
73 | explicit EqualsTo(const Guid &aMachineId) : machineId(aMachineId) {}
|
---|
74 |
|
---|
75 | bool operator()(const argument_type &aThat) const
|
---|
76 | {
|
---|
77 | return aThat.machineId == machineId;
|
---|
78 | }
|
---|
79 |
|
---|
80 | const Guid machineId;
|
---|
81 | };
|
---|
82 |
|
---|
83 | BackRef(const Guid &aMachineId,
|
---|
84 | const Guid &aSnapshotId = Guid::Empty)
|
---|
85 | : machineId(aMachineId),
|
---|
86 | fInCurState(aSnapshotId.isZero())
|
---|
87 | {
|
---|
88 | if (aSnapshotId.isValid() && !aSnapshotId.isZero())
|
---|
89 | llSnapshotIds.push_back(aSnapshotId);
|
---|
90 | }
|
---|
91 |
|
---|
92 | Guid machineId;
|
---|
93 | bool fInCurState : 1;
|
---|
94 | GuidList llSnapshotIds;
|
---|
95 | };
|
---|
96 |
|
---|
97 | typedef std::list<BackRef> BackRefList;
|
---|
98 |
|
---|
99 | struct Medium::Data
|
---|
100 | {
|
---|
101 | Data()
|
---|
102 | : pVirtualBox(NULL),
|
---|
103 | state(MediumState_NotCreated),
|
---|
104 | variant(MediumVariant_Standard),
|
---|
105 | size(0),
|
---|
106 | readers(0),
|
---|
107 | preLockState(MediumState_NotCreated),
|
---|
108 | queryInfoSem(LOCKCLASS_MEDIUMQUERY),
|
---|
109 | queryInfoRunning(false),
|
---|
110 | type(MediumType_Normal),
|
---|
111 | devType(DeviceType_HardDisk),
|
---|
112 | logicalSize(0),
|
---|
113 | hddOpenMode(OpenReadWrite),
|
---|
114 | autoReset(false),
|
---|
115 | hostDrive(false),
|
---|
116 | implicit(false),
|
---|
117 | fClosing(false),
|
---|
118 | uOpenFlagsDef(VD_OPEN_FLAGS_IGNORE_FLUSH),
|
---|
119 | numCreateDiffTasks(0),
|
---|
120 | vdDiskIfaces(NULL),
|
---|
121 | vdImageIfaces(NULL),
|
---|
122 | fMoveThisMedium(false)
|
---|
123 | { }
|
---|
124 |
|
---|
125 | /** weak VirtualBox parent */
|
---|
126 | VirtualBox * const pVirtualBox;
|
---|
127 |
|
---|
128 | // pParent and llChildren are protected by VirtualBox::i_getMediaTreeLockHandle()
|
---|
129 | ComObjPtr<Medium> pParent;
|
---|
130 | MediaList llChildren; // to add a child, just call push_back; to remove
|
---|
131 | // a child, call child->deparent() which does a lookup
|
---|
132 |
|
---|
133 | GuidList llRegistryIDs; // media registries in which this medium is listed
|
---|
134 |
|
---|
135 | const Guid id;
|
---|
136 | Utf8Str strDescription;
|
---|
137 | MediumState_T state;
|
---|
138 | MediumVariant_T variant;
|
---|
139 | Utf8Str strLocationFull;
|
---|
140 | uint64_t size;
|
---|
141 | Utf8Str strLastAccessError;
|
---|
142 |
|
---|
143 | BackRefList backRefs;
|
---|
144 |
|
---|
145 | size_t readers;
|
---|
146 | MediumState_T preLockState;
|
---|
147 |
|
---|
148 | /** Special synchronization for operations which must wait for
|
---|
149 | * Medium::i_queryInfo in another thread to complete. Using a SemRW is
|
---|
150 | * not quite ideal, but at least it is subject to the lock validator,
|
---|
151 | * unlike the SemEventMulti which we had here for many years. Catching
|
---|
152 | * possible deadlocks is more important than a tiny bit of efficiency. */
|
---|
153 | RWLockHandle queryInfoSem;
|
---|
154 | bool queryInfoRunning : 1;
|
---|
155 |
|
---|
156 | const Utf8Str strFormat;
|
---|
157 | ComObjPtr<MediumFormat> formatObj;
|
---|
158 |
|
---|
159 | MediumType_T type;
|
---|
160 | DeviceType_T devType;
|
---|
161 | uint64_t logicalSize;
|
---|
162 |
|
---|
163 | HDDOpenMode hddOpenMode;
|
---|
164 |
|
---|
165 | bool autoReset : 1;
|
---|
166 |
|
---|
167 | /** New UUID to be set on the next Medium::i_queryInfo call. */
|
---|
168 | const Guid uuidImage;
|
---|
169 | /** New parent UUID to be set on the next Medium::i_queryInfo call. */
|
---|
170 | const Guid uuidParentImage;
|
---|
171 |
|
---|
172 | bool hostDrive : 1;
|
---|
173 |
|
---|
174 | settings::StringsMap mapProperties;
|
---|
175 |
|
---|
176 | bool implicit : 1;
|
---|
177 | /** Flag whether the medium is in the process of being closed. */
|
---|
178 | bool fClosing: 1;
|
---|
179 |
|
---|
180 | /** Default flags passed to VDOpen(). */
|
---|
181 | unsigned uOpenFlagsDef;
|
---|
182 |
|
---|
183 | uint32_t numCreateDiffTasks;
|
---|
184 |
|
---|
185 | Utf8Str vdError; /*< Error remembered by the VD error callback. */
|
---|
186 |
|
---|
187 | VDINTERFACEERROR vdIfError;
|
---|
188 |
|
---|
189 | VDINTERFACECONFIG vdIfConfig;
|
---|
190 |
|
---|
191 | /** The handle to the default VD TCP/IP interface. */
|
---|
192 | VDIFINST hTcpNetInst;
|
---|
193 |
|
---|
194 | PVDINTERFACE vdDiskIfaces;
|
---|
195 | PVDINTERFACE vdImageIfaces;
|
---|
196 |
|
---|
197 | /** Flag if the medium is going to move to a new
|
---|
198 | * location. */
|
---|
199 | bool fMoveThisMedium;
|
---|
200 | /** new location path */
|
---|
201 | Utf8Str strNewLocationFull;
|
---|
202 | };
|
---|
203 |
|
---|
204 | typedef struct VDSOCKETINT
|
---|
205 | {
|
---|
206 | /** Socket handle. */
|
---|
207 | RTSOCKET hSocket;
|
---|
208 | } VDSOCKETINT, *PVDSOCKETINT;
|
---|
209 |
|
---|
210 | ////////////////////////////////////////////////////////////////////////////////
|
---|
211 | //
|
---|
212 | // Globals
|
---|
213 | //
|
---|
214 | ////////////////////////////////////////////////////////////////////////////////
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Medium::Task class for asynchronous operations.
|
---|
218 | *
|
---|
219 | * @note Instances of this class must be created using new() because the
|
---|
220 | * task thread function will delete them when the task is complete.
|
---|
221 | *
|
---|
222 | * @note The constructor of this class adds a caller on the managed Medium
|
---|
223 | * object which is automatically released upon destruction.
|
---|
224 | */
|
---|
225 | class Medium::Task : public ThreadTask
|
---|
226 | {
|
---|
227 | public:
|
---|
228 | Task(Medium *aMedium, Progress *aProgress, bool fNotifyAboutChanges = true)
|
---|
229 | : ThreadTask("Medium::Task"),
|
---|
230 | mVDOperationIfaces(NULL),
|
---|
231 | mMedium(aMedium),
|
---|
232 | mMediumCaller(aMedium),
|
---|
233 | mProgress(aProgress),
|
---|
234 | mVirtualBoxCaller(NULL),
|
---|
235 | mNotifyAboutChanges(fNotifyAboutChanges)
|
---|
236 | {
|
---|
237 | AssertReturnVoidStmt(aMedium, mRC = E_FAIL);
|
---|
238 | mRC = mMediumCaller.rc();
|
---|
239 | if (FAILED(mRC))
|
---|
240 | return;
|
---|
241 |
|
---|
242 | /* Get strong VirtualBox reference, see below. */
|
---|
243 | VirtualBox *pVirtualBox = aMedium->m->pVirtualBox;
|
---|
244 | mVirtualBox = pVirtualBox;
|
---|
245 | mVirtualBoxCaller.attach(pVirtualBox);
|
---|
246 | mRC = mVirtualBoxCaller.rc();
|
---|
247 | if (FAILED(mRC))
|
---|
248 | return;
|
---|
249 |
|
---|
250 | /* Set up a per-operation progress interface, can be used freely (for
|
---|
251 | * binary operations you can use it either on the source or target). */
|
---|
252 | if (mProgress)
|
---|
253 | {
|
---|
254 | mVDIfProgress.pfnProgress = aProgress->i_vdProgressCallback;
|
---|
255 | int vrc = VDInterfaceAdd(&mVDIfProgress.Core,
|
---|
256 | "Medium::Task::vdInterfaceProgress",
|
---|
257 | VDINTERFACETYPE_PROGRESS,
|
---|
258 | mProgress,
|
---|
259 | sizeof(mVDIfProgress),
|
---|
260 | &mVDOperationIfaces);
|
---|
261 | AssertRC(vrc);
|
---|
262 | if (RT_FAILURE(vrc))
|
---|
263 | mRC = E_FAIL;
|
---|
264 | }
|
---|
265 | }
|
---|
266 |
|
---|
267 | // Make all destructors virtual. Just in case.
|
---|
268 | virtual ~Task()
|
---|
269 | {
|
---|
270 | /* send the notification of completion.*/
|
---|
271 | if ( isAsync()
|
---|
272 | && !mProgress.isNull())
|
---|
273 | mProgress->i_notifyComplete(mRC);
|
---|
274 | }
|
---|
275 |
|
---|
276 | HRESULT rc() const { return mRC; }
|
---|
277 | bool isOk() const { return SUCCEEDED(rc()); }
|
---|
278 | bool NotifyAboutChanges() const { return mNotifyAboutChanges; }
|
---|
279 |
|
---|
280 | const ComPtr<Progress>& GetProgressObject() const {return mProgress;}
|
---|
281 |
|
---|
282 | /**
|
---|
283 | * Runs Medium::Task::executeTask() on the current thread
|
---|
284 | * instead of creating a new one.
|
---|
285 | */
|
---|
286 | HRESULT runNow()
|
---|
287 | {
|
---|
288 | LogFlowFuncEnter();
|
---|
289 |
|
---|
290 | mRC = executeTask();
|
---|
291 |
|
---|
292 | LogFlowFunc(("rc=%Rhrc\n", mRC));
|
---|
293 | LogFlowFuncLeave();
|
---|
294 | return mRC;
|
---|
295 | }
|
---|
296 |
|
---|
297 | /**
|
---|
298 | * Implementation code for the "create base" task.
|
---|
299 | * Used as function for execution from a standalone thread.
|
---|
300 | */
|
---|
301 | void handler()
|
---|
302 | {
|
---|
303 | LogFlowFuncEnter();
|
---|
304 | try
|
---|
305 | {
|
---|
306 | mRC = executeTask(); /* (destructor picks up mRC, see above) */
|
---|
307 | LogFlowFunc(("rc=%Rhrc\n", mRC));
|
---|
308 | }
|
---|
309 | catch (...)
|
---|
310 | {
|
---|
311 | LogRel(("Some exception in the function Medium::Task:handler()\n"));
|
---|
312 | }
|
---|
313 |
|
---|
314 | LogFlowFuncLeave();
|
---|
315 | }
|
---|
316 |
|
---|
317 | PVDINTERFACE mVDOperationIfaces;
|
---|
318 |
|
---|
319 | const ComObjPtr<Medium> mMedium;
|
---|
320 | AutoCaller mMediumCaller;
|
---|
321 |
|
---|
322 | protected:
|
---|
323 | HRESULT mRC;
|
---|
324 |
|
---|
325 | private:
|
---|
326 | virtual HRESULT executeTask() = 0;
|
---|
327 |
|
---|
328 | const ComObjPtr<Progress> mProgress;
|
---|
329 |
|
---|
330 | VDINTERFACEPROGRESS mVDIfProgress;
|
---|
331 |
|
---|
332 | /* Must have a strong VirtualBox reference during a task otherwise the
|
---|
333 | * reference count might drop to 0 while a task is still running. This
|
---|
334 | * would result in weird behavior, including deadlocks due to uninit and
|
---|
335 | * locking order issues. The deadlock often is not detectable because the
|
---|
336 | * uninit uses event semaphores which sabotages deadlock detection. */
|
---|
337 | ComObjPtr<VirtualBox> mVirtualBox;
|
---|
338 | AutoCaller mVirtualBoxCaller;
|
---|
339 | bool mNotifyAboutChanges;
|
---|
340 | };
|
---|
341 |
|
---|
342 | class Medium::CreateBaseTask : public Medium::Task
|
---|
343 | {
|
---|
344 | public:
|
---|
345 | CreateBaseTask(Medium *aMedium,
|
---|
346 | Progress *aProgress,
|
---|
347 | uint64_t aSize,
|
---|
348 | MediumVariant_T aVariant,
|
---|
349 | bool fNotifyAboutChanges = true)
|
---|
350 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
351 | mSize(aSize),
|
---|
352 | mVariant(aVariant)
|
---|
353 | {
|
---|
354 | m_strTaskName = "createBase";
|
---|
355 | }
|
---|
356 |
|
---|
357 | uint64_t mSize;
|
---|
358 | MediumVariant_T mVariant;
|
---|
359 |
|
---|
360 | private:
|
---|
361 | HRESULT executeTask()
|
---|
362 | {
|
---|
363 | return mMedium->i_taskCreateBaseHandler(*this);
|
---|
364 | }
|
---|
365 | };
|
---|
366 |
|
---|
367 | class Medium::CreateDiffTask : public Medium::Task
|
---|
368 | {
|
---|
369 | public:
|
---|
370 | CreateDiffTask(Medium *aMedium,
|
---|
371 | Progress *aProgress,
|
---|
372 | Medium *aTarget,
|
---|
373 | MediumVariant_T aVariant,
|
---|
374 | MediumLockList *aMediumLockList,
|
---|
375 | bool fKeepMediumLockList = false,
|
---|
376 | bool fNotifyAboutChanges = true)
|
---|
377 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
378 | mpMediumLockList(aMediumLockList),
|
---|
379 | mTarget(aTarget),
|
---|
380 | mVariant(aVariant),
|
---|
381 | mTargetCaller(aTarget),
|
---|
382 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
383 | {
|
---|
384 | AssertReturnVoidStmt(aTarget != NULL, mRC = E_FAIL);
|
---|
385 | mRC = mTargetCaller.rc();
|
---|
386 | if (FAILED(mRC))
|
---|
387 | return;
|
---|
388 | m_strTaskName = "createDiff";
|
---|
389 | }
|
---|
390 |
|
---|
391 | ~CreateDiffTask()
|
---|
392 | {
|
---|
393 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
394 | delete mpMediumLockList;
|
---|
395 | }
|
---|
396 |
|
---|
397 | MediumLockList *mpMediumLockList;
|
---|
398 |
|
---|
399 | const ComObjPtr<Medium> mTarget;
|
---|
400 | MediumVariant_T mVariant;
|
---|
401 |
|
---|
402 | private:
|
---|
403 | HRESULT executeTask()
|
---|
404 | {
|
---|
405 | return mMedium->i_taskCreateDiffHandler(*this);
|
---|
406 | }
|
---|
407 |
|
---|
408 | AutoCaller mTargetCaller;
|
---|
409 | bool mfKeepMediumLockList;
|
---|
410 | };
|
---|
411 |
|
---|
412 | class Medium::CloneTask : public Medium::Task
|
---|
413 | {
|
---|
414 | public:
|
---|
415 | CloneTask(Medium *aMedium,
|
---|
416 | Progress *aProgress,
|
---|
417 | Medium *aTarget,
|
---|
418 | MediumVariant_T aVariant,
|
---|
419 | Medium *aParent,
|
---|
420 | uint32_t idxSrcImageSame,
|
---|
421 | uint32_t idxDstImageSame,
|
---|
422 | MediumLockList *aSourceMediumLockList,
|
---|
423 | MediumLockList *aTargetMediumLockList,
|
---|
424 | bool fKeepSourceMediumLockList = false,
|
---|
425 | bool fKeepTargetMediumLockList = false,
|
---|
426 | bool fNotifyAboutChanges = true)
|
---|
427 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
428 | mTarget(aTarget),
|
---|
429 | mParent(aParent),
|
---|
430 | mpSourceMediumLockList(aSourceMediumLockList),
|
---|
431 | mpTargetMediumLockList(aTargetMediumLockList),
|
---|
432 | mVariant(aVariant),
|
---|
433 | midxSrcImageSame(idxSrcImageSame),
|
---|
434 | midxDstImageSame(idxDstImageSame),
|
---|
435 | mTargetCaller(aTarget),
|
---|
436 | mParentCaller(aParent),
|
---|
437 | mfKeepSourceMediumLockList(fKeepSourceMediumLockList),
|
---|
438 | mfKeepTargetMediumLockList(fKeepTargetMediumLockList)
|
---|
439 | {
|
---|
440 | AssertReturnVoidStmt(aTarget != NULL, mRC = E_FAIL);
|
---|
441 | mRC = mTargetCaller.rc();
|
---|
442 | if (FAILED(mRC))
|
---|
443 | return;
|
---|
444 | /* aParent may be NULL */
|
---|
445 | mRC = mParentCaller.rc();
|
---|
446 | if (FAILED(mRC))
|
---|
447 | return;
|
---|
448 | AssertReturnVoidStmt(aSourceMediumLockList != NULL, mRC = E_FAIL);
|
---|
449 | AssertReturnVoidStmt(aTargetMediumLockList != NULL, mRC = E_FAIL);
|
---|
450 | m_strTaskName = "createClone";
|
---|
451 | }
|
---|
452 |
|
---|
453 | ~CloneTask()
|
---|
454 | {
|
---|
455 | if (!mfKeepSourceMediumLockList && mpSourceMediumLockList)
|
---|
456 | delete mpSourceMediumLockList;
|
---|
457 | if (!mfKeepTargetMediumLockList && mpTargetMediumLockList)
|
---|
458 | delete mpTargetMediumLockList;
|
---|
459 | }
|
---|
460 |
|
---|
461 | const ComObjPtr<Medium> mTarget;
|
---|
462 | const ComObjPtr<Medium> mParent;
|
---|
463 | MediumLockList *mpSourceMediumLockList;
|
---|
464 | MediumLockList *mpTargetMediumLockList;
|
---|
465 | MediumVariant_T mVariant;
|
---|
466 | uint32_t midxSrcImageSame;
|
---|
467 | uint32_t midxDstImageSame;
|
---|
468 |
|
---|
469 | private:
|
---|
470 | HRESULT executeTask()
|
---|
471 | {
|
---|
472 | return mMedium->i_taskCloneHandler(*this);
|
---|
473 | }
|
---|
474 |
|
---|
475 | AutoCaller mTargetCaller;
|
---|
476 | AutoCaller mParentCaller;
|
---|
477 | bool mfKeepSourceMediumLockList;
|
---|
478 | bool mfKeepTargetMediumLockList;
|
---|
479 | };
|
---|
480 |
|
---|
481 | class Medium::MoveTask : public Medium::Task
|
---|
482 | {
|
---|
483 | public:
|
---|
484 | MoveTask(Medium *aMedium,
|
---|
485 | Progress *aProgress,
|
---|
486 | MediumVariant_T aVariant,
|
---|
487 | MediumLockList *aMediumLockList,
|
---|
488 | bool fKeepMediumLockList = false,
|
---|
489 | bool fNotifyAboutChanges = true)
|
---|
490 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
491 | mpMediumLockList(aMediumLockList),
|
---|
492 | mVariant(aVariant),
|
---|
493 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
494 | {
|
---|
495 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
496 | m_strTaskName = "createMove";
|
---|
497 | }
|
---|
498 |
|
---|
499 | ~MoveTask()
|
---|
500 | {
|
---|
501 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
502 | delete mpMediumLockList;
|
---|
503 | }
|
---|
504 |
|
---|
505 | MediumLockList *mpMediumLockList;
|
---|
506 | MediumVariant_T mVariant;
|
---|
507 |
|
---|
508 | private:
|
---|
509 | HRESULT executeTask()
|
---|
510 | {
|
---|
511 | return mMedium->i_taskMoveHandler(*this);
|
---|
512 | }
|
---|
513 |
|
---|
514 | bool mfKeepMediumLockList;
|
---|
515 | };
|
---|
516 |
|
---|
517 | class Medium::CompactTask : public Medium::Task
|
---|
518 | {
|
---|
519 | public:
|
---|
520 | CompactTask(Medium *aMedium,
|
---|
521 | Progress *aProgress,
|
---|
522 | MediumLockList *aMediumLockList,
|
---|
523 | bool fKeepMediumLockList = false,
|
---|
524 | bool fNotifyAboutChanges = true)
|
---|
525 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
526 | mpMediumLockList(aMediumLockList),
|
---|
527 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
528 | {
|
---|
529 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
530 | m_strTaskName = "createCompact";
|
---|
531 | }
|
---|
532 |
|
---|
533 | ~CompactTask()
|
---|
534 | {
|
---|
535 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
536 | delete mpMediumLockList;
|
---|
537 | }
|
---|
538 |
|
---|
539 | MediumLockList *mpMediumLockList;
|
---|
540 |
|
---|
541 | private:
|
---|
542 | HRESULT executeTask()
|
---|
543 | {
|
---|
544 | return mMedium->i_taskCompactHandler(*this);
|
---|
545 | }
|
---|
546 |
|
---|
547 | bool mfKeepMediumLockList;
|
---|
548 | };
|
---|
549 |
|
---|
550 | class Medium::ResizeTask : public Medium::Task
|
---|
551 | {
|
---|
552 | public:
|
---|
553 | ResizeTask(Medium *aMedium,
|
---|
554 | uint64_t aSize,
|
---|
555 | Progress *aProgress,
|
---|
556 | MediumLockList *aMediumLockList,
|
---|
557 | bool fKeepMediumLockList = false,
|
---|
558 | bool fNotifyAboutChanges = true)
|
---|
559 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
560 | mSize(aSize),
|
---|
561 | mpMediumLockList(aMediumLockList),
|
---|
562 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
563 | {
|
---|
564 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
565 | m_strTaskName = "createResize";
|
---|
566 | }
|
---|
567 |
|
---|
568 | ~ResizeTask()
|
---|
569 | {
|
---|
570 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
571 | delete mpMediumLockList;
|
---|
572 | }
|
---|
573 |
|
---|
574 | uint64_t mSize;
|
---|
575 | MediumLockList *mpMediumLockList;
|
---|
576 |
|
---|
577 | private:
|
---|
578 | HRESULT executeTask()
|
---|
579 | {
|
---|
580 | return mMedium->i_taskResizeHandler(*this);
|
---|
581 | }
|
---|
582 |
|
---|
583 | bool mfKeepMediumLockList;
|
---|
584 | };
|
---|
585 |
|
---|
586 | class Medium::ResetTask : public Medium::Task
|
---|
587 | {
|
---|
588 | public:
|
---|
589 | ResetTask(Medium *aMedium,
|
---|
590 | Progress *aProgress,
|
---|
591 | MediumLockList *aMediumLockList,
|
---|
592 | bool fKeepMediumLockList = false,
|
---|
593 | bool fNotifyAboutChanges = true)
|
---|
594 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
595 | mpMediumLockList(aMediumLockList),
|
---|
596 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
597 | {
|
---|
598 | m_strTaskName = "createReset";
|
---|
599 | }
|
---|
600 |
|
---|
601 | ~ResetTask()
|
---|
602 | {
|
---|
603 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
604 | delete mpMediumLockList;
|
---|
605 | }
|
---|
606 |
|
---|
607 | MediumLockList *mpMediumLockList;
|
---|
608 |
|
---|
609 | private:
|
---|
610 | HRESULT executeTask()
|
---|
611 | {
|
---|
612 | return mMedium->i_taskResetHandler(*this);
|
---|
613 | }
|
---|
614 |
|
---|
615 | bool mfKeepMediumLockList;
|
---|
616 | };
|
---|
617 |
|
---|
618 | class Medium::DeleteTask : public Medium::Task
|
---|
619 | {
|
---|
620 | public:
|
---|
621 | DeleteTask(Medium *aMedium,
|
---|
622 | Progress *aProgress,
|
---|
623 | MediumLockList *aMediumLockList,
|
---|
624 | bool fKeepMediumLockList = false,
|
---|
625 | bool fNotifyAboutChanges = true)
|
---|
626 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
627 | mpMediumLockList(aMediumLockList),
|
---|
628 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
629 | {
|
---|
630 | m_strTaskName = "createDelete";
|
---|
631 | }
|
---|
632 |
|
---|
633 | ~DeleteTask()
|
---|
634 | {
|
---|
635 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
636 | delete mpMediumLockList;
|
---|
637 | }
|
---|
638 |
|
---|
639 | MediumLockList *mpMediumLockList;
|
---|
640 |
|
---|
641 | private:
|
---|
642 | HRESULT executeTask()
|
---|
643 | {
|
---|
644 | return mMedium->i_taskDeleteHandler(*this);
|
---|
645 | }
|
---|
646 |
|
---|
647 | bool mfKeepMediumLockList;
|
---|
648 | };
|
---|
649 |
|
---|
650 | class Medium::MergeTask : public Medium::Task
|
---|
651 | {
|
---|
652 | public:
|
---|
653 | MergeTask(Medium *aMedium,
|
---|
654 | Medium *aTarget,
|
---|
655 | bool fMergeForward,
|
---|
656 | Medium *aParentForTarget,
|
---|
657 | MediumLockList *aChildrenToReparent,
|
---|
658 | Progress *aProgress,
|
---|
659 | MediumLockList *aMediumLockList,
|
---|
660 | bool fKeepMediumLockList = false,
|
---|
661 | bool fNotifyAboutChanges = true)
|
---|
662 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
663 | mTarget(aTarget),
|
---|
664 | mfMergeForward(fMergeForward),
|
---|
665 | mParentForTarget(aParentForTarget),
|
---|
666 | mpChildrenToReparent(aChildrenToReparent),
|
---|
667 | mpMediumLockList(aMediumLockList),
|
---|
668 | mTargetCaller(aTarget),
|
---|
669 | mParentForTargetCaller(aParentForTarget),
|
---|
670 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
671 | {
|
---|
672 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
673 | m_strTaskName = "createMerge";
|
---|
674 | }
|
---|
675 |
|
---|
676 | ~MergeTask()
|
---|
677 | {
|
---|
678 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
679 | delete mpMediumLockList;
|
---|
680 | if (mpChildrenToReparent)
|
---|
681 | delete mpChildrenToReparent;
|
---|
682 | }
|
---|
683 |
|
---|
684 | const ComObjPtr<Medium> mTarget;
|
---|
685 | bool mfMergeForward;
|
---|
686 | /* When mpChildrenToReparent is null then mParentForTarget is non-null and
|
---|
687 | * vice versa. In other words: they are used in different cases. */
|
---|
688 | const ComObjPtr<Medium> mParentForTarget;
|
---|
689 | MediumLockList *mpChildrenToReparent;
|
---|
690 | MediumLockList *mpMediumLockList;
|
---|
691 |
|
---|
692 | private:
|
---|
693 | HRESULT executeTask()
|
---|
694 | {
|
---|
695 | return mMedium->i_taskMergeHandler(*this);
|
---|
696 | }
|
---|
697 |
|
---|
698 | AutoCaller mTargetCaller;
|
---|
699 | AutoCaller mParentForTargetCaller;
|
---|
700 | bool mfKeepMediumLockList;
|
---|
701 | };
|
---|
702 |
|
---|
703 | class Medium::ImportTask : public Medium::Task
|
---|
704 | {
|
---|
705 | public:
|
---|
706 | ImportTask(Medium *aMedium,
|
---|
707 | Progress *aProgress,
|
---|
708 | const char *aFilename,
|
---|
709 | MediumFormat *aFormat,
|
---|
710 | MediumVariant_T aVariant,
|
---|
711 | RTVFSIOSTREAM aVfsIosSrc,
|
---|
712 | Medium *aParent,
|
---|
713 | MediumLockList *aTargetMediumLockList,
|
---|
714 | bool fKeepTargetMediumLockList = false,
|
---|
715 | bool fNotifyAboutChanges = true)
|
---|
716 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
717 | mFilename(aFilename),
|
---|
718 | mFormat(aFormat),
|
---|
719 | mVariant(aVariant),
|
---|
720 | mParent(aParent),
|
---|
721 | mpTargetMediumLockList(aTargetMediumLockList),
|
---|
722 | mpVfsIoIf(NULL),
|
---|
723 | mParentCaller(aParent),
|
---|
724 | mfKeepTargetMediumLockList(fKeepTargetMediumLockList)
|
---|
725 | {
|
---|
726 | AssertReturnVoidStmt(aTargetMediumLockList != NULL, mRC = E_FAIL);
|
---|
727 | /* aParent may be NULL */
|
---|
728 | mRC = mParentCaller.rc();
|
---|
729 | if (FAILED(mRC))
|
---|
730 | return;
|
---|
731 |
|
---|
732 | mVDImageIfaces = aMedium->m->vdImageIfaces;
|
---|
733 |
|
---|
734 | int vrc = VDIfCreateFromVfsStream(aVfsIosSrc, RTFILE_O_READ, &mpVfsIoIf);
|
---|
735 | AssertRCReturnVoidStmt(vrc, mRC = E_FAIL);
|
---|
736 |
|
---|
737 | vrc = VDInterfaceAdd(&mpVfsIoIf->Core, "Medium::ImportTaskVfsIos",
|
---|
738 | VDINTERFACETYPE_IO, mpVfsIoIf,
|
---|
739 | sizeof(VDINTERFACEIO), &mVDImageIfaces);
|
---|
740 | AssertRCReturnVoidStmt(vrc, mRC = E_FAIL);
|
---|
741 | m_strTaskName = "createImport";
|
---|
742 | }
|
---|
743 |
|
---|
744 | ~ImportTask()
|
---|
745 | {
|
---|
746 | if (!mfKeepTargetMediumLockList && mpTargetMediumLockList)
|
---|
747 | delete mpTargetMediumLockList;
|
---|
748 | if (mpVfsIoIf)
|
---|
749 | {
|
---|
750 | VDIfDestroyFromVfsStream(mpVfsIoIf);
|
---|
751 | mpVfsIoIf = NULL;
|
---|
752 | }
|
---|
753 | }
|
---|
754 |
|
---|
755 | Utf8Str mFilename;
|
---|
756 | ComObjPtr<MediumFormat> mFormat;
|
---|
757 | MediumVariant_T mVariant;
|
---|
758 | const ComObjPtr<Medium> mParent;
|
---|
759 | MediumLockList *mpTargetMediumLockList;
|
---|
760 | PVDINTERFACE mVDImageIfaces;
|
---|
761 | PVDINTERFACEIO mpVfsIoIf; /**< Pointer to the VFS I/O stream to VD I/O interface wrapper. */
|
---|
762 |
|
---|
763 | private:
|
---|
764 | HRESULT executeTask()
|
---|
765 | {
|
---|
766 | return mMedium->i_taskImportHandler(*this);
|
---|
767 | }
|
---|
768 |
|
---|
769 | AutoCaller mParentCaller;
|
---|
770 | bool mfKeepTargetMediumLockList;
|
---|
771 | };
|
---|
772 |
|
---|
773 | class Medium::EncryptTask : public Medium::Task
|
---|
774 | {
|
---|
775 | public:
|
---|
776 | EncryptTask(Medium *aMedium,
|
---|
777 | const com::Utf8Str &strNewPassword,
|
---|
778 | const com::Utf8Str &strCurrentPassword,
|
---|
779 | const com::Utf8Str &strCipher,
|
---|
780 | const com::Utf8Str &strNewPasswordId,
|
---|
781 | Progress *aProgress,
|
---|
782 | MediumLockList *aMediumLockList)
|
---|
783 | : Medium::Task(aMedium, aProgress, false),
|
---|
784 | mstrNewPassword(strNewPassword),
|
---|
785 | mstrCurrentPassword(strCurrentPassword),
|
---|
786 | mstrCipher(strCipher),
|
---|
787 | mstrNewPasswordId(strNewPasswordId),
|
---|
788 | mpMediumLockList(aMediumLockList)
|
---|
789 | {
|
---|
790 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
791 | /* aParent may be NULL */
|
---|
792 | mRC = mParentCaller.rc();
|
---|
793 | if (FAILED(mRC))
|
---|
794 | return;
|
---|
795 |
|
---|
796 | mVDImageIfaces = aMedium->m->vdImageIfaces;
|
---|
797 | m_strTaskName = "createEncrypt";
|
---|
798 | }
|
---|
799 |
|
---|
800 | ~EncryptTask()
|
---|
801 | {
|
---|
802 | if (mstrNewPassword.length())
|
---|
803 | RTMemWipeThoroughly(mstrNewPassword.mutableRaw(), mstrNewPassword.length(), 10 /* cPasses */);
|
---|
804 | if (mstrCurrentPassword.length())
|
---|
805 | RTMemWipeThoroughly(mstrCurrentPassword.mutableRaw(), mstrCurrentPassword.length(), 10 /* cPasses */);
|
---|
806 |
|
---|
807 | /* Keep any errors which might be set when deleting the lock list. */
|
---|
808 | ErrorInfoKeeper eik;
|
---|
809 | delete mpMediumLockList;
|
---|
810 | }
|
---|
811 |
|
---|
812 | Utf8Str mstrNewPassword;
|
---|
813 | Utf8Str mstrCurrentPassword;
|
---|
814 | Utf8Str mstrCipher;
|
---|
815 | Utf8Str mstrNewPasswordId;
|
---|
816 | MediumLockList *mpMediumLockList;
|
---|
817 | PVDINTERFACE mVDImageIfaces;
|
---|
818 |
|
---|
819 | private:
|
---|
820 | HRESULT executeTask()
|
---|
821 | {
|
---|
822 | return mMedium->i_taskEncryptHandler(*this);
|
---|
823 | }
|
---|
824 |
|
---|
825 | AutoCaller mParentCaller;
|
---|
826 | };
|
---|
827 |
|
---|
828 |
|
---|
829 | ////////////////////////////////////////////////////////////////////////////////
|
---|
830 | //
|
---|
831 | // Medium constructor / destructor
|
---|
832 | //
|
---|
833 | ////////////////////////////////////////////////////////////////////////////////
|
---|
834 |
|
---|
835 | DEFINE_EMPTY_CTOR_DTOR(Medium)
|
---|
836 |
|
---|
837 | HRESULT Medium::FinalConstruct()
|
---|
838 | {
|
---|
839 | m = new Data;
|
---|
840 |
|
---|
841 | /* Initialize the callbacks of the VD error interface */
|
---|
842 | m->vdIfError.pfnError = i_vdErrorCall;
|
---|
843 | m->vdIfError.pfnMessage = NULL;
|
---|
844 |
|
---|
845 | /* Initialize the callbacks of the VD config interface */
|
---|
846 | m->vdIfConfig.pfnAreKeysValid = i_vdConfigAreKeysValid;
|
---|
847 | m->vdIfConfig.pfnQuerySize = i_vdConfigQuerySize;
|
---|
848 | m->vdIfConfig.pfnQuery = i_vdConfigQuery;
|
---|
849 | m->vdIfConfig.pfnQueryBytes = NULL;
|
---|
850 |
|
---|
851 | /* Initialize the per-disk interface chain (could be done more globally,
|
---|
852 | * but it's not wasting much time or space so it's not worth it). */
|
---|
853 | int vrc;
|
---|
854 | vrc = VDInterfaceAdd(&m->vdIfError.Core,
|
---|
855 | "Medium::vdInterfaceError",
|
---|
856 | VDINTERFACETYPE_ERROR, this,
|
---|
857 | sizeof(VDINTERFACEERROR), &m->vdDiskIfaces);
|
---|
858 | AssertRCReturn(vrc, E_FAIL);
|
---|
859 |
|
---|
860 | /* Initialize the per-image interface chain */
|
---|
861 | vrc = VDInterfaceAdd(&m->vdIfConfig.Core,
|
---|
862 | "Medium::vdInterfaceConfig",
|
---|
863 | VDINTERFACETYPE_CONFIG, this,
|
---|
864 | sizeof(VDINTERFACECONFIG), &m->vdImageIfaces);
|
---|
865 | AssertRCReturn(vrc, E_FAIL);
|
---|
866 |
|
---|
867 | /* Initialize the callbacks of the VD TCP interface (we always use the host
|
---|
868 | * IP stack for now) */
|
---|
869 | vrc = VDIfTcpNetInstDefaultCreate(&m->hTcpNetInst, &m->vdImageIfaces);
|
---|
870 | AssertRCReturn(vrc, E_FAIL);
|
---|
871 |
|
---|
872 | return BaseFinalConstruct();
|
---|
873 | }
|
---|
874 |
|
---|
875 | void Medium::FinalRelease()
|
---|
876 | {
|
---|
877 | uninit();
|
---|
878 |
|
---|
879 | VDIfTcpNetInstDefaultDestroy(m->hTcpNetInst);
|
---|
880 | delete m;
|
---|
881 |
|
---|
882 | BaseFinalRelease();
|
---|
883 | }
|
---|
884 |
|
---|
885 | /**
|
---|
886 | * Initializes an empty hard disk object without creating or opening an associated
|
---|
887 | * storage unit.
|
---|
888 | *
|
---|
889 | * This gets called by VirtualBox::CreateMedium() in which case uuidMachineRegistry
|
---|
890 | * is empty since starting with VirtualBox 4.0, we no longer add opened media to a
|
---|
891 | * registry automatically (this is deferred until the medium is attached to a machine).
|
---|
892 | *
|
---|
893 | * This also gets called when VirtualBox creates diff images; in this case uuidMachineRegistry
|
---|
894 | * is set to the registry of the parent image to make sure they all end up in the same
|
---|
895 | * file.
|
---|
896 | *
|
---|
897 | * For hard disks that don't have the MediumFormatCapabilities_CreateFixed or
|
---|
898 | * MediumFormatCapabilities_CreateDynamic capability (and therefore cannot be created or deleted
|
---|
899 | * with the means of VirtualBox) the associated storage unit is assumed to be
|
---|
900 | * ready for use so the state of the hard disk object will be set to Created.
|
---|
901 | *
|
---|
902 | * @param aVirtualBox VirtualBox object.
|
---|
903 | * @param aFormat
|
---|
904 | * @param aLocation Storage unit location.
|
---|
905 | * @param uuidMachineRegistry The registry to which this medium should be added
|
---|
906 | * (global registry UUID or machine UUID or empty if none).
|
---|
907 | * @param aDeviceType Device Type.
|
---|
908 | */
|
---|
909 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
910 | const Utf8Str &aFormat,
|
---|
911 | const Utf8Str &aLocation,
|
---|
912 | const Guid &uuidMachineRegistry,
|
---|
913 | const DeviceType_T aDeviceType)
|
---|
914 | {
|
---|
915 | AssertReturn(aVirtualBox != NULL, E_FAIL);
|
---|
916 | AssertReturn(!aFormat.isEmpty(), E_FAIL);
|
---|
917 |
|
---|
918 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
919 | AutoInitSpan autoInitSpan(this);
|
---|
920 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
921 |
|
---|
922 | HRESULT rc = S_OK;
|
---|
923 |
|
---|
924 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
925 |
|
---|
926 | if (uuidMachineRegistry.isValid() && !uuidMachineRegistry.isZero())
|
---|
927 | m->llRegistryIDs.push_back(uuidMachineRegistry);
|
---|
928 |
|
---|
929 | /* no storage yet */
|
---|
930 | m->state = MediumState_NotCreated;
|
---|
931 |
|
---|
932 | /* cannot be a host drive */
|
---|
933 | m->hostDrive = false;
|
---|
934 |
|
---|
935 | m->devType = aDeviceType;
|
---|
936 |
|
---|
937 | /* No storage unit is created yet, no need to call Medium::i_queryInfo */
|
---|
938 |
|
---|
939 | rc = i_setFormat(aFormat);
|
---|
940 | if (FAILED(rc)) return rc;
|
---|
941 |
|
---|
942 | rc = i_setLocation(aLocation);
|
---|
943 | if (FAILED(rc)) return rc;
|
---|
944 |
|
---|
945 | if (!(m->formatObj->i_getCapabilities() & ( MediumFormatCapabilities_CreateFixed
|
---|
946 | | MediumFormatCapabilities_CreateDynamic))
|
---|
947 | )
|
---|
948 | {
|
---|
949 | /* Storage for mediums of this format can neither be explicitly
|
---|
950 | * created by VirtualBox nor deleted, so we place the medium to
|
---|
951 | * Inaccessible state here and also add it to the registry. The
|
---|
952 | * state means that one has to use RefreshState() to update the
|
---|
953 | * medium format specific fields. */
|
---|
954 | m->state = MediumState_Inaccessible;
|
---|
955 | // create new UUID
|
---|
956 | unconst(m->id).create();
|
---|
957 |
|
---|
958 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
959 | ComObjPtr<Medium> pMedium;
|
---|
960 |
|
---|
961 | /*
|
---|
962 | * Check whether the UUID is taken already and create a new one
|
---|
963 | * if required.
|
---|
964 | * Try this only a limited amount of times in case the PRNG is broken
|
---|
965 | * in some way to prevent an endless loop.
|
---|
966 | */
|
---|
967 | for (unsigned i = 0; i < 5; i++)
|
---|
968 | {
|
---|
969 | bool fInUse;
|
---|
970 |
|
---|
971 | fInUse = m->pVirtualBox->i_isMediaUuidInUse(m->id, aDeviceType);
|
---|
972 | if (fInUse)
|
---|
973 | {
|
---|
974 | // create new UUID
|
---|
975 | unconst(m->id).create();
|
---|
976 | }
|
---|
977 | else
|
---|
978 | break;
|
---|
979 | }
|
---|
980 |
|
---|
981 | rc = m->pVirtualBox->i_registerMedium(this, &pMedium, treeLock);
|
---|
982 | Assert(this == pMedium || FAILED(rc));
|
---|
983 | }
|
---|
984 |
|
---|
985 | /* Confirm a successful initialization when it's the case */
|
---|
986 | if (SUCCEEDED(rc))
|
---|
987 | autoInitSpan.setSucceeded();
|
---|
988 |
|
---|
989 | return rc;
|
---|
990 | }
|
---|
991 |
|
---|
992 | /**
|
---|
993 | * Initializes the medium object by opening the storage unit at the specified
|
---|
994 | * location. The enOpenMode parameter defines whether the medium will be opened
|
---|
995 | * read/write or read-only.
|
---|
996 | *
|
---|
997 | * This gets called by VirtualBox::OpenMedium() and also by
|
---|
998 | * Machine::AttachDevice() and createImplicitDiffs() when new diff
|
---|
999 | * images are created.
|
---|
1000 | *
|
---|
1001 | * There is no registry for this case since starting with VirtualBox 4.0, we
|
---|
1002 | * no longer add opened media to a registry automatically (this is deferred
|
---|
1003 | * until the medium is attached to a machine).
|
---|
1004 | *
|
---|
1005 | * For hard disks, the UUID, format and the parent of this medium will be
|
---|
1006 | * determined when reading the medium storage unit. For DVD and floppy images,
|
---|
1007 | * which have no UUIDs in their storage units, new UUIDs are created.
|
---|
1008 | * If the detected or set parent is not known to VirtualBox, then this method
|
---|
1009 | * will fail.
|
---|
1010 | *
|
---|
1011 | * @param aVirtualBox VirtualBox object.
|
---|
1012 | * @param aLocation Storage unit location.
|
---|
1013 | * @param enOpenMode Whether to open the medium read/write or read-only.
|
---|
1014 | * @param fForceNewUuid Whether a new UUID should be set to avoid duplicates.
|
---|
1015 | * @param aDeviceType Device type of medium.
|
---|
1016 | */
|
---|
1017 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1018 | const Utf8Str &aLocation,
|
---|
1019 | HDDOpenMode enOpenMode,
|
---|
1020 | bool fForceNewUuid,
|
---|
1021 | DeviceType_T aDeviceType)
|
---|
1022 | {
|
---|
1023 | AssertReturn(aVirtualBox, E_INVALIDARG);
|
---|
1024 | AssertReturn(!aLocation.isEmpty(), E_INVALIDARG);
|
---|
1025 |
|
---|
1026 | HRESULT rc = S_OK;
|
---|
1027 |
|
---|
1028 | {
|
---|
1029 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1030 | AutoInitSpan autoInitSpan(this);
|
---|
1031 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1032 |
|
---|
1033 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1034 |
|
---|
1035 | /* there must be a storage unit */
|
---|
1036 | m->state = MediumState_Created;
|
---|
1037 |
|
---|
1038 | /* remember device type for correct unregistering later */
|
---|
1039 | m->devType = aDeviceType;
|
---|
1040 |
|
---|
1041 | /* cannot be a host drive */
|
---|
1042 | m->hostDrive = false;
|
---|
1043 |
|
---|
1044 | /* remember the open mode (defaults to ReadWrite) */
|
---|
1045 | m->hddOpenMode = enOpenMode;
|
---|
1046 |
|
---|
1047 | if (aDeviceType == DeviceType_DVD)
|
---|
1048 | m->type = MediumType_Readonly;
|
---|
1049 | else if (aDeviceType == DeviceType_Floppy)
|
---|
1050 | m->type = MediumType_Writethrough;
|
---|
1051 |
|
---|
1052 | rc = i_setLocation(aLocation);
|
---|
1053 | if (FAILED(rc)) return rc;
|
---|
1054 |
|
---|
1055 | /* get all the information about the medium from the storage unit */
|
---|
1056 | if (fForceNewUuid)
|
---|
1057 | unconst(m->uuidImage).create();
|
---|
1058 |
|
---|
1059 | m->state = MediumState_Inaccessible;
|
---|
1060 | m->strLastAccessError = tr("Accessibility check was not yet performed");
|
---|
1061 |
|
---|
1062 | /* Confirm a successful initialization before the call to i_queryInfo.
|
---|
1063 | * Otherwise we can end up with a AutoCaller deadlock because the
|
---|
1064 | * medium becomes visible but is not marked as initialized. Causes
|
---|
1065 | * locking trouble (e.g. trying to save media registries) which is
|
---|
1066 | * hard to solve. */
|
---|
1067 | autoInitSpan.setSucceeded();
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | /* we're normal code from now on, no longer init */
|
---|
1071 | AutoCaller autoCaller(this);
|
---|
1072 | if (FAILED(autoCaller.rc()))
|
---|
1073 | return autoCaller.rc();
|
---|
1074 |
|
---|
1075 | /* need to call i_queryInfo immediately to correctly place the medium in
|
---|
1076 | * the respective media tree and update other information such as uuid */
|
---|
1077 | rc = i_queryInfo(fForceNewUuid /* fSetImageId */, false /* fSetParentId */,
|
---|
1078 | autoCaller);
|
---|
1079 | if (SUCCEEDED(rc))
|
---|
1080 | {
|
---|
1081 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1082 |
|
---|
1083 | /* if the storage unit is not accessible, it's not acceptable for the
|
---|
1084 | * newly opened media so convert this into an error */
|
---|
1085 | if (m->state == MediumState_Inaccessible)
|
---|
1086 | {
|
---|
1087 | Assert(!m->strLastAccessError.isEmpty());
|
---|
1088 | rc = setError(E_FAIL, "%s", m->strLastAccessError.c_str());
|
---|
1089 | alock.release();
|
---|
1090 | autoCaller.release();
|
---|
1091 | uninit();
|
---|
1092 | }
|
---|
1093 | else
|
---|
1094 | {
|
---|
1095 | AssertStmt(!m->id.isZero(),
|
---|
1096 | alock.release(); autoCaller.release(); uninit(); return E_FAIL);
|
---|
1097 |
|
---|
1098 | /* storage format must be detected by Medium::i_queryInfo if the
|
---|
1099 | * medium is accessible */
|
---|
1100 | AssertStmt(!m->strFormat.isEmpty(),
|
---|
1101 | alock.release(); autoCaller.release(); uninit(); return E_FAIL);
|
---|
1102 | }
|
---|
1103 | }
|
---|
1104 | else
|
---|
1105 | {
|
---|
1106 | /* opening this image failed, mark the object as dead */
|
---|
1107 | autoCaller.release();
|
---|
1108 | uninit();
|
---|
1109 | }
|
---|
1110 |
|
---|
1111 | return rc;
|
---|
1112 | }
|
---|
1113 |
|
---|
1114 | /**
|
---|
1115 | * Initializes the medium object by loading its data from the given settings
|
---|
1116 | * node. The medium will always be opened read/write.
|
---|
1117 | *
|
---|
1118 | * In this case, since we're loading from a registry, uuidMachineRegistry is
|
---|
1119 | * always set: it's either the global registry UUID or a machine UUID when
|
---|
1120 | * loading from a per-machine registry.
|
---|
1121 | *
|
---|
1122 | * @param aParent Parent medium disk or NULL for a root (base) medium.
|
---|
1123 | * @param aDeviceType Device type of the medium.
|
---|
1124 | * @param uuidMachineRegistry The registry to which this medium should be
|
---|
1125 | * added (global registry UUID or machine UUID).
|
---|
1126 | * @param data Configuration settings.
|
---|
1127 | * @param strMachineFolder The machine folder with which to resolve relative paths;
|
---|
1128 | * if empty, then we use the VirtualBox home directory
|
---|
1129 | *
|
---|
1130 | * @note Locks the medium tree for writing.
|
---|
1131 | */
|
---|
1132 | HRESULT Medium::initOne(Medium *aParent,
|
---|
1133 | DeviceType_T aDeviceType,
|
---|
1134 | const Guid &uuidMachineRegistry,
|
---|
1135 | const settings::Medium &data,
|
---|
1136 | const Utf8Str &strMachineFolder)
|
---|
1137 | {
|
---|
1138 | HRESULT rc;
|
---|
1139 |
|
---|
1140 | if (uuidMachineRegistry.isValid() && !uuidMachineRegistry.isZero())
|
---|
1141 | m->llRegistryIDs.push_back(uuidMachineRegistry);
|
---|
1142 |
|
---|
1143 | /* register with VirtualBox/parent early, since uninit() will
|
---|
1144 | * unconditionally unregister on failure */
|
---|
1145 | if (aParent)
|
---|
1146 | {
|
---|
1147 | // differencing medium: add to parent
|
---|
1148 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1149 | // no need to check maximum depth as settings reading did it
|
---|
1150 | i_setParent(aParent);
|
---|
1151 | }
|
---|
1152 |
|
---|
1153 | /* see below why we don't call Medium::i_queryInfo (and therefore treat
|
---|
1154 | * the medium as inaccessible for now */
|
---|
1155 | m->state = MediumState_Inaccessible;
|
---|
1156 | m->strLastAccessError = tr("Accessibility check was not yet performed");
|
---|
1157 |
|
---|
1158 | /* required */
|
---|
1159 | unconst(m->id) = data.uuid;
|
---|
1160 |
|
---|
1161 | /* assume not a host drive */
|
---|
1162 | m->hostDrive = false;
|
---|
1163 |
|
---|
1164 | /* optional */
|
---|
1165 | m->strDescription = data.strDescription;
|
---|
1166 |
|
---|
1167 | /* required */
|
---|
1168 | if (aDeviceType == DeviceType_HardDisk)
|
---|
1169 | {
|
---|
1170 | AssertReturn(!data.strFormat.isEmpty(), E_FAIL);
|
---|
1171 | rc = i_setFormat(data.strFormat);
|
---|
1172 | if (FAILED(rc)) return rc;
|
---|
1173 | }
|
---|
1174 | else
|
---|
1175 | {
|
---|
1176 | /// @todo handle host drive settings here as well?
|
---|
1177 | if (!data.strFormat.isEmpty())
|
---|
1178 | rc = i_setFormat(data.strFormat);
|
---|
1179 | else
|
---|
1180 | rc = i_setFormat("RAW");
|
---|
1181 | if (FAILED(rc)) return rc;
|
---|
1182 | }
|
---|
1183 |
|
---|
1184 | /* optional, only for diffs, default is false; we can only auto-reset
|
---|
1185 | * diff media so they must have a parent */
|
---|
1186 | if (aParent != NULL)
|
---|
1187 | m->autoReset = data.fAutoReset;
|
---|
1188 | else
|
---|
1189 | m->autoReset = false;
|
---|
1190 |
|
---|
1191 | /* properties (after setting the format as it populates the map). Note that
|
---|
1192 | * if some properties are not supported but present in the settings file,
|
---|
1193 | * they will still be read and accessible (for possible backward
|
---|
1194 | * compatibility; we can also clean them up from the XML upon next
|
---|
1195 | * XML format version change if we wish) */
|
---|
1196 | for (settings::StringsMap::const_iterator it = data.properties.begin();
|
---|
1197 | it != data.properties.end();
|
---|
1198 | ++it)
|
---|
1199 | {
|
---|
1200 | const Utf8Str &name = it->first;
|
---|
1201 | const Utf8Str &value = it->second;
|
---|
1202 | m->mapProperties[name] = value;
|
---|
1203 | }
|
---|
1204 |
|
---|
1205 | /* try to decrypt an optional iSCSI initiator secret */
|
---|
1206 | settings::StringsMap::const_iterator itCph = data.properties.find("InitiatorSecretEncrypted");
|
---|
1207 | if ( itCph != data.properties.end()
|
---|
1208 | && !itCph->second.isEmpty())
|
---|
1209 | {
|
---|
1210 | Utf8Str strPlaintext;
|
---|
1211 | int vrc = m->pVirtualBox->i_decryptSetting(&strPlaintext, itCph->second);
|
---|
1212 | if (RT_SUCCESS(vrc))
|
---|
1213 | m->mapProperties["InitiatorSecret"] = strPlaintext;
|
---|
1214 | }
|
---|
1215 |
|
---|
1216 | Utf8Str strFull;
|
---|
1217 | if (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
1218 | {
|
---|
1219 | // compose full path of the medium, if it's not fully qualified...
|
---|
1220 | // slightly convoluted logic here. If the caller has given us a
|
---|
1221 | // machine folder, then a relative path will be relative to that:
|
---|
1222 | if ( !strMachineFolder.isEmpty()
|
---|
1223 | && !RTPathStartsWithRoot(data.strLocation.c_str())
|
---|
1224 | )
|
---|
1225 | {
|
---|
1226 | strFull = strMachineFolder;
|
---|
1227 | strFull += RTPATH_SLASH;
|
---|
1228 | strFull += data.strLocation;
|
---|
1229 | }
|
---|
1230 | else
|
---|
1231 | {
|
---|
1232 | // Otherwise use the old VirtualBox "make absolute path" logic:
|
---|
1233 | rc = m->pVirtualBox->i_calculateFullPath(data.strLocation, strFull);
|
---|
1234 | if (FAILED(rc)) return rc;
|
---|
1235 | }
|
---|
1236 | }
|
---|
1237 | else
|
---|
1238 | strFull = data.strLocation;
|
---|
1239 |
|
---|
1240 | rc = i_setLocation(strFull);
|
---|
1241 | if (FAILED(rc)) return rc;
|
---|
1242 |
|
---|
1243 | if (aDeviceType == DeviceType_HardDisk)
|
---|
1244 | {
|
---|
1245 | /* type is only for base hard disks */
|
---|
1246 | if (m->pParent.isNull())
|
---|
1247 | m->type = data.hdType;
|
---|
1248 | }
|
---|
1249 | else if (aDeviceType == DeviceType_DVD)
|
---|
1250 | m->type = MediumType_Readonly;
|
---|
1251 | else
|
---|
1252 | m->type = MediumType_Writethrough;
|
---|
1253 |
|
---|
1254 | /* remember device type for correct unregistering later */
|
---|
1255 | m->devType = aDeviceType;
|
---|
1256 |
|
---|
1257 | LogFlowThisFunc(("m->strLocationFull='%s', m->strFormat=%s, m->id={%RTuuid}\n",
|
---|
1258 | m->strLocationFull.c_str(), m->strFormat.c_str(), m->id.raw()));
|
---|
1259 |
|
---|
1260 | return S_OK;
|
---|
1261 | }
|
---|
1262 |
|
---|
1263 | /**
|
---|
1264 | * Initializes the medium object and its children by loading its data from the
|
---|
1265 | * given settings node. The medium will always be opened read/write.
|
---|
1266 | *
|
---|
1267 | * In this case, since we're loading from a registry, uuidMachineRegistry is
|
---|
1268 | * always set: it's either the global registry UUID or a machine UUID when
|
---|
1269 | * loading from a per-machine registry.
|
---|
1270 | *
|
---|
1271 | * @param aVirtualBox VirtualBox object.
|
---|
1272 | * @param aParent Parent medium disk or NULL for a root (base) medium.
|
---|
1273 | * @param aDeviceType Device type of the medium.
|
---|
1274 | * @param uuidMachineRegistry The registry to which this medium should be added
|
---|
1275 | * (global registry UUID or machine UUID).
|
---|
1276 | * @param data Configuration settings.
|
---|
1277 | * @param strMachineFolder The machine folder with which to resolve relative
|
---|
1278 | * paths; if empty, then we use the VirtualBox home directory
|
---|
1279 | * @param mediaTreeLock Autolock.
|
---|
1280 | *
|
---|
1281 | * @note Locks the medium tree for writing.
|
---|
1282 | */
|
---|
1283 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1284 | Medium *aParent,
|
---|
1285 | DeviceType_T aDeviceType,
|
---|
1286 | const Guid &uuidMachineRegistry,
|
---|
1287 | const settings::Medium &data,
|
---|
1288 | const Utf8Str &strMachineFolder,
|
---|
1289 | AutoWriteLock &mediaTreeLock)
|
---|
1290 | {
|
---|
1291 | using namespace settings;
|
---|
1292 |
|
---|
1293 | Assert(aVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
1294 | AssertReturn(aVirtualBox, E_INVALIDARG);
|
---|
1295 |
|
---|
1296 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1297 | AutoInitSpan autoInitSpan(this);
|
---|
1298 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1299 |
|
---|
1300 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1301 |
|
---|
1302 | // Do not inline this method call, as the purpose of having this separate
|
---|
1303 | // is to save on stack size. Less local variables are the key for reaching
|
---|
1304 | // deep recursion levels with small stack (XPCOM/g++ without optimization).
|
---|
1305 | HRESULT rc = initOne(aParent, aDeviceType, uuidMachineRegistry, data, strMachineFolder);
|
---|
1306 |
|
---|
1307 |
|
---|
1308 | /* Don't call Medium::i_queryInfo for registered media to prevent the calling
|
---|
1309 | * thread (i.e. the VirtualBox server startup thread) from an unexpected
|
---|
1310 | * freeze but mark it as initially inaccessible instead. The vital UUID,
|
---|
1311 | * location and format properties are read from the registry file above; to
|
---|
1312 | * get the actual state and the rest of the data, the user will have to call
|
---|
1313 | * COMGETTER(State). */
|
---|
1314 |
|
---|
1315 | /* load all children */
|
---|
1316 | for (settings::MediaList::const_iterator it = data.llChildren.begin();
|
---|
1317 | it != data.llChildren.end();
|
---|
1318 | ++it)
|
---|
1319 | {
|
---|
1320 | const settings::Medium &med = *it;
|
---|
1321 |
|
---|
1322 | ComObjPtr<Medium> pMedium;
|
---|
1323 | pMedium.createObject();
|
---|
1324 | rc = pMedium->init(aVirtualBox,
|
---|
1325 | this, // parent
|
---|
1326 | aDeviceType,
|
---|
1327 | uuidMachineRegistry,
|
---|
1328 | med, // child data
|
---|
1329 | strMachineFolder,
|
---|
1330 | mediaTreeLock);
|
---|
1331 | if (FAILED(rc)) break;
|
---|
1332 |
|
---|
1333 | rc = m->pVirtualBox->i_registerMedium(pMedium, &pMedium, mediaTreeLock);
|
---|
1334 | if (FAILED(rc)) break;
|
---|
1335 | }
|
---|
1336 |
|
---|
1337 | /* Confirm a successful initialization when it's the case */
|
---|
1338 | if (SUCCEEDED(rc))
|
---|
1339 | autoInitSpan.setSucceeded();
|
---|
1340 |
|
---|
1341 | return rc;
|
---|
1342 | }
|
---|
1343 |
|
---|
1344 | /**
|
---|
1345 | * Initializes the medium object by providing the host drive information.
|
---|
1346 | * Not used for anything but the host floppy/host DVD case.
|
---|
1347 | *
|
---|
1348 | * There is no registry for this case.
|
---|
1349 | *
|
---|
1350 | * @param aVirtualBox VirtualBox object.
|
---|
1351 | * @param aDeviceType Device type of the medium.
|
---|
1352 | * @param aLocation Location of the host drive.
|
---|
1353 | * @param aDescription Comment for this host drive.
|
---|
1354 | *
|
---|
1355 | * @note Locks VirtualBox lock for writing.
|
---|
1356 | */
|
---|
1357 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1358 | DeviceType_T aDeviceType,
|
---|
1359 | const Utf8Str &aLocation,
|
---|
1360 | const Utf8Str &aDescription /* = Utf8Str::Empty */)
|
---|
1361 | {
|
---|
1362 | ComAssertRet(aDeviceType == DeviceType_DVD || aDeviceType == DeviceType_Floppy, E_INVALIDARG);
|
---|
1363 | ComAssertRet(!aLocation.isEmpty(), E_INVALIDARG);
|
---|
1364 |
|
---|
1365 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1366 | AutoInitSpan autoInitSpan(this);
|
---|
1367 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1368 |
|
---|
1369 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1370 |
|
---|
1371 | // We do not store host drives in VirtualBox.xml or anywhere else, so if we want
|
---|
1372 | // host drives to be identifiable by UUID and not give the drive a different UUID
|
---|
1373 | // every time VirtualBox starts, we need to fake a reproducible UUID here:
|
---|
1374 | RTUUID uuid;
|
---|
1375 | RTUuidClear(&uuid);
|
---|
1376 | if (aDeviceType == DeviceType_DVD)
|
---|
1377 | memcpy(&uuid.au8[0], "DVD", 3);
|
---|
1378 | else
|
---|
1379 | memcpy(&uuid.au8[0], "FD", 2);
|
---|
1380 | /* use device name, adjusted to the end of uuid, shortened if necessary */
|
---|
1381 | size_t lenLocation = aLocation.length();
|
---|
1382 | if (lenLocation > 12)
|
---|
1383 | memcpy(&uuid.au8[4], aLocation.c_str() + (lenLocation - 12), 12);
|
---|
1384 | else
|
---|
1385 | memcpy(&uuid.au8[4 + 12 - lenLocation], aLocation.c_str(), lenLocation);
|
---|
1386 | unconst(m->id) = uuid;
|
---|
1387 |
|
---|
1388 | if (aDeviceType == DeviceType_DVD)
|
---|
1389 | m->type = MediumType_Readonly;
|
---|
1390 | else
|
---|
1391 | m->type = MediumType_Writethrough;
|
---|
1392 | m->devType = aDeviceType;
|
---|
1393 | m->state = MediumState_Created;
|
---|
1394 | m->hostDrive = true;
|
---|
1395 | HRESULT rc = i_setFormat("RAW");
|
---|
1396 | if (FAILED(rc)) return rc;
|
---|
1397 | rc = i_setLocation(aLocation);
|
---|
1398 | if (FAILED(rc)) return rc;
|
---|
1399 | m->strDescription = aDescription;
|
---|
1400 |
|
---|
1401 | autoInitSpan.setSucceeded();
|
---|
1402 | return S_OK;
|
---|
1403 | }
|
---|
1404 |
|
---|
1405 | /**
|
---|
1406 | * Uninitializes the instance.
|
---|
1407 | *
|
---|
1408 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
1409 | *
|
---|
1410 | * @note All children of this medium get uninitialized by calling their
|
---|
1411 | * uninit() methods.
|
---|
1412 | */
|
---|
1413 | void Medium::uninit()
|
---|
1414 | {
|
---|
1415 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1416 | * the pVirtualBox reference, and in this case we don't need to continue.
|
---|
1417 | * Normally this would be handled through the AutoUninitSpan magic, however
|
---|
1418 | * this cannot be done at this point as the media tree must be locked
|
---|
1419 | * before reaching the AutoUninitSpan, otherwise deadlocks can happen.
|
---|
1420 | *
|
---|
1421 | * NOTE: The tree lock is higher priority than the medium caller and medium
|
---|
1422 | * object locks, i.e. the medium caller may have to be released and be
|
---|
1423 | * re-acquired in the right place later. See Medium::getParent() for sample
|
---|
1424 | * code how to do this safely. */
|
---|
1425 | VirtualBox *pVirtualBox = m->pVirtualBox;
|
---|
1426 | if (!pVirtualBox)
|
---|
1427 | return;
|
---|
1428 |
|
---|
1429 | /* Caller must not hold the object or media tree lock over uninit(). */
|
---|
1430 | Assert(!isWriteLockOnCurrentThread());
|
---|
1431 | Assert(!pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
1432 |
|
---|
1433 | AutoWriteLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1434 |
|
---|
1435 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
1436 | AutoUninitSpan autoUninitSpan(this);
|
---|
1437 | if (autoUninitSpan.uninitDone())
|
---|
1438 | return;
|
---|
1439 |
|
---|
1440 | if (!m->formatObj.isNull())
|
---|
1441 | m->formatObj.setNull();
|
---|
1442 |
|
---|
1443 | if (m->state == MediumState_Deleting)
|
---|
1444 | {
|
---|
1445 | /* This medium has been already deleted (directly or as part of a
|
---|
1446 | * merge). Reparenting has already been done. */
|
---|
1447 | Assert(m->pParent.isNull());
|
---|
1448 | }
|
---|
1449 | else
|
---|
1450 | {
|
---|
1451 | MediaList llChildren(m->llChildren);
|
---|
1452 | m->llChildren.clear();
|
---|
1453 | autoUninitSpan.setSucceeded();
|
---|
1454 |
|
---|
1455 | while (!llChildren.empty())
|
---|
1456 | {
|
---|
1457 | ComObjPtr<Medium> pChild = llChildren.front();
|
---|
1458 | llChildren.pop_front();
|
---|
1459 | pChild->m->pParent.setNull();
|
---|
1460 | treeLock.release();
|
---|
1461 | pChild->uninit();
|
---|
1462 | treeLock.acquire();
|
---|
1463 | }
|
---|
1464 |
|
---|
1465 | if (m->pParent)
|
---|
1466 | {
|
---|
1467 | // this is a differencing disk: then remove it from the parent's children list
|
---|
1468 | i_deparent();
|
---|
1469 | }
|
---|
1470 | }
|
---|
1471 |
|
---|
1472 | unconst(m->pVirtualBox) = NULL;
|
---|
1473 | }
|
---|
1474 |
|
---|
1475 | /**
|
---|
1476 | * Internal helper that removes "this" from the list of children of its
|
---|
1477 | * parent. Used in uninit() and other places when reparenting is necessary.
|
---|
1478 | *
|
---|
1479 | * The caller must hold the medium tree lock!
|
---|
1480 | */
|
---|
1481 | void Medium::i_deparent()
|
---|
1482 | {
|
---|
1483 | MediaList &llParent = m->pParent->m->llChildren;
|
---|
1484 | for (MediaList::iterator it = llParent.begin();
|
---|
1485 | it != llParent.end();
|
---|
1486 | ++it)
|
---|
1487 | {
|
---|
1488 | Medium *pParentsChild = *it;
|
---|
1489 | if (this == pParentsChild)
|
---|
1490 | {
|
---|
1491 | llParent.erase(it);
|
---|
1492 | break;
|
---|
1493 | }
|
---|
1494 | }
|
---|
1495 | m->pParent.setNull();
|
---|
1496 | }
|
---|
1497 |
|
---|
1498 | /**
|
---|
1499 | * Internal helper that removes "this" from the list of children of its
|
---|
1500 | * parent. Used in uninit() and other places when reparenting is necessary.
|
---|
1501 | *
|
---|
1502 | * The caller must hold the medium tree lock!
|
---|
1503 | */
|
---|
1504 | void Medium::i_setParent(const ComObjPtr<Medium> &pParent)
|
---|
1505 | {
|
---|
1506 | m->pParent = pParent;
|
---|
1507 | if (pParent)
|
---|
1508 | pParent->m->llChildren.push_back(this);
|
---|
1509 | }
|
---|
1510 |
|
---|
1511 |
|
---|
1512 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1513 | //
|
---|
1514 | // IMedium public methods
|
---|
1515 | //
|
---|
1516 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1517 |
|
---|
1518 | HRESULT Medium::getId(com::Guid &aId)
|
---|
1519 | {
|
---|
1520 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1521 |
|
---|
1522 | aId = m->id;
|
---|
1523 |
|
---|
1524 | return S_OK;
|
---|
1525 | }
|
---|
1526 |
|
---|
1527 | HRESULT Medium::getDescription(AutoCaller &autoCaller, com::Utf8Str &aDescription)
|
---|
1528 | {
|
---|
1529 | NOREF(autoCaller);
|
---|
1530 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1531 |
|
---|
1532 | aDescription = m->strDescription;
|
---|
1533 |
|
---|
1534 | return S_OK;
|
---|
1535 | }
|
---|
1536 |
|
---|
1537 | HRESULT Medium::setDescription(AutoCaller &autoCaller, const com::Utf8Str &aDescription)
|
---|
1538 | {
|
---|
1539 | /// @todo update m->strDescription and save the global registry (and local
|
---|
1540 | /// registries of portable VMs referring to this medium), this will also
|
---|
1541 | /// require to add the mRegistered flag to data
|
---|
1542 |
|
---|
1543 | HRESULT rc = S_OK;
|
---|
1544 |
|
---|
1545 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
1546 |
|
---|
1547 | try
|
---|
1548 | {
|
---|
1549 | autoCaller.release();
|
---|
1550 |
|
---|
1551 | // to avoid redundant locking, which just takes a time, just call required functions.
|
---|
1552 | // the error will be just stored and will be reported after locks will be acquired again
|
---|
1553 |
|
---|
1554 | const char *pszError = NULL;
|
---|
1555 |
|
---|
1556 |
|
---|
1557 | /* Build the lock list. */
|
---|
1558 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
1559 | this /* pToLockWrite */,
|
---|
1560 | true /* fMediumLockWriteAll */,
|
---|
1561 | NULL,
|
---|
1562 | *pMediumLockList);
|
---|
1563 | if (FAILED(rc))
|
---|
1564 | {
|
---|
1565 | pszError = tr("Failed to create medium lock list for '%s'");
|
---|
1566 | }
|
---|
1567 | else
|
---|
1568 | {
|
---|
1569 | rc = pMediumLockList->Lock();
|
---|
1570 | if (FAILED(rc))
|
---|
1571 | pszError = tr("Failed to lock media '%s'");
|
---|
1572 | }
|
---|
1573 |
|
---|
1574 | // locking: we need the tree lock first because we access parent pointers
|
---|
1575 | // and we need to write-lock the media involved
|
---|
1576 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1577 |
|
---|
1578 | autoCaller.add();
|
---|
1579 | AssertComRCThrowRC(autoCaller.rc());
|
---|
1580 |
|
---|
1581 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1582 |
|
---|
1583 | if (FAILED(rc))
|
---|
1584 | throw setError(rc, pszError, i_getLocationFull().c_str());
|
---|
1585 |
|
---|
1586 | /* Set a new description */
|
---|
1587 | m->strDescription = aDescription;
|
---|
1588 |
|
---|
1589 | // save the settings
|
---|
1590 | alock.release();
|
---|
1591 | autoCaller.release();
|
---|
1592 | treeLock.release();
|
---|
1593 | i_markRegistriesModified();
|
---|
1594 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
1595 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
1596 | }
|
---|
1597 | catch (HRESULT aRC) { rc = aRC; }
|
---|
1598 |
|
---|
1599 | delete pMediumLockList;
|
---|
1600 |
|
---|
1601 | return rc;
|
---|
1602 | }
|
---|
1603 |
|
---|
1604 | HRESULT Medium::getState(MediumState_T *aState)
|
---|
1605 | {
|
---|
1606 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1607 | *aState = m->state;
|
---|
1608 |
|
---|
1609 | return S_OK;
|
---|
1610 | }
|
---|
1611 |
|
---|
1612 | HRESULT Medium::getVariant(std::vector<MediumVariant_T> &aVariant)
|
---|
1613 | {
|
---|
1614 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1615 |
|
---|
1616 | const size_t cBits = sizeof(MediumVariant_T) * 8;
|
---|
1617 | aVariant.resize(cBits);
|
---|
1618 | for (size_t i = 0; i < cBits; ++i)
|
---|
1619 | aVariant[i] = (MediumVariant_T)(m->variant & RT_BIT(i));
|
---|
1620 |
|
---|
1621 | return S_OK;
|
---|
1622 | }
|
---|
1623 |
|
---|
1624 | HRESULT Medium::getLocation(com::Utf8Str &aLocation)
|
---|
1625 | {
|
---|
1626 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1627 |
|
---|
1628 | aLocation = m->strLocationFull;
|
---|
1629 |
|
---|
1630 | return S_OK;
|
---|
1631 | }
|
---|
1632 |
|
---|
1633 | HRESULT Medium::getName(com::Utf8Str &aName)
|
---|
1634 | {
|
---|
1635 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1636 |
|
---|
1637 | aName = i_getName();
|
---|
1638 |
|
---|
1639 | return S_OK;
|
---|
1640 | }
|
---|
1641 |
|
---|
1642 | HRESULT Medium::getDeviceType(DeviceType_T *aDeviceType)
|
---|
1643 | {
|
---|
1644 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1645 |
|
---|
1646 | *aDeviceType = m->devType;
|
---|
1647 |
|
---|
1648 | return S_OK;
|
---|
1649 | }
|
---|
1650 |
|
---|
1651 | HRESULT Medium::getHostDrive(BOOL *aHostDrive)
|
---|
1652 | {
|
---|
1653 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1654 |
|
---|
1655 | *aHostDrive = m->hostDrive;
|
---|
1656 |
|
---|
1657 | return S_OK;
|
---|
1658 | }
|
---|
1659 |
|
---|
1660 | HRESULT Medium::getSize(LONG64 *aSize)
|
---|
1661 | {
|
---|
1662 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1663 |
|
---|
1664 | *aSize = m->size;
|
---|
1665 |
|
---|
1666 | return S_OK;
|
---|
1667 | }
|
---|
1668 |
|
---|
1669 | HRESULT Medium::getFormat(com::Utf8Str &aFormat)
|
---|
1670 | {
|
---|
1671 | /* no need to lock, m->strFormat is const */
|
---|
1672 |
|
---|
1673 | aFormat = m->strFormat;
|
---|
1674 | return S_OK;
|
---|
1675 | }
|
---|
1676 |
|
---|
1677 | HRESULT Medium::getMediumFormat(ComPtr<IMediumFormat> &aMediumFormat)
|
---|
1678 | {
|
---|
1679 | /* no need to lock, m->formatObj is const */
|
---|
1680 | m->formatObj.queryInterfaceTo(aMediumFormat.asOutParam());
|
---|
1681 |
|
---|
1682 | return S_OK;
|
---|
1683 | }
|
---|
1684 |
|
---|
1685 | HRESULT Medium::getType(AutoCaller &autoCaller, MediumType_T *aType)
|
---|
1686 | {
|
---|
1687 | NOREF(autoCaller);
|
---|
1688 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1689 |
|
---|
1690 | *aType = m->type;
|
---|
1691 |
|
---|
1692 | return S_OK;
|
---|
1693 | }
|
---|
1694 |
|
---|
1695 | HRESULT Medium::setType(AutoCaller &autoCaller, MediumType_T aType)
|
---|
1696 | {
|
---|
1697 | autoCaller.release();
|
---|
1698 |
|
---|
1699 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1700 | * the pVirtualBox reference, see #uninit(). */
|
---|
1701 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
1702 |
|
---|
1703 | // we access m->pParent
|
---|
1704 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
1705 |
|
---|
1706 | autoCaller.add();
|
---|
1707 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1708 |
|
---|
1709 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1710 |
|
---|
1711 | switch (m->state)
|
---|
1712 | {
|
---|
1713 | case MediumState_Created:
|
---|
1714 | case MediumState_Inaccessible:
|
---|
1715 | break;
|
---|
1716 | default:
|
---|
1717 | return i_setStateError();
|
---|
1718 | }
|
---|
1719 |
|
---|
1720 | if (m->type == aType)
|
---|
1721 | {
|
---|
1722 | /* Nothing to do */
|
---|
1723 | return S_OK;
|
---|
1724 | }
|
---|
1725 |
|
---|
1726 | DeviceType_T devType = i_getDeviceType();
|
---|
1727 | // DVD media can only be readonly.
|
---|
1728 | if (devType == DeviceType_DVD && aType != MediumType_Readonly)
|
---|
1729 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1730 | tr("Cannot change the type of DVD medium '%s'"),
|
---|
1731 | m->strLocationFull.c_str());
|
---|
1732 | // Floppy media can only be writethrough or readonly.
|
---|
1733 | if ( devType == DeviceType_Floppy
|
---|
1734 | && aType != MediumType_Writethrough
|
---|
1735 | && aType != MediumType_Readonly)
|
---|
1736 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1737 | tr("Cannot change the type of floppy medium '%s'"),
|
---|
1738 | m->strLocationFull.c_str());
|
---|
1739 |
|
---|
1740 | /* cannot change the type of a differencing medium */
|
---|
1741 | if (m->pParent)
|
---|
1742 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1743 | tr("Cannot change the type of medium '%s' because it is a differencing medium"),
|
---|
1744 | m->strLocationFull.c_str());
|
---|
1745 |
|
---|
1746 | /* Cannot change the type of a medium being in use by more than one VM.
|
---|
1747 | * If the change is to Immutable or MultiAttach then it must not be
|
---|
1748 | * directly attached to any VM, otherwise the assumptions about indirect
|
---|
1749 | * attachment elsewhere are violated and the VM becomes inaccessible.
|
---|
1750 | * Attaching an immutable medium triggers the diff creation, and this is
|
---|
1751 | * vital for the correct operation. */
|
---|
1752 | if ( m->backRefs.size() > 1
|
---|
1753 | || ( ( aType == MediumType_Immutable
|
---|
1754 | || aType == MediumType_MultiAttach)
|
---|
1755 | && m->backRefs.size() > 0))
|
---|
1756 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1757 | tr("Cannot change the type of medium '%s' because it is attached to %d virtual machines"),
|
---|
1758 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
1759 |
|
---|
1760 | switch (aType)
|
---|
1761 | {
|
---|
1762 | case MediumType_Normal:
|
---|
1763 | case MediumType_Immutable:
|
---|
1764 | case MediumType_MultiAttach:
|
---|
1765 | {
|
---|
1766 | /* normal can be easily converted to immutable and vice versa even
|
---|
1767 | * if they have children as long as they are not attached to any
|
---|
1768 | * machine themselves */
|
---|
1769 | break;
|
---|
1770 | }
|
---|
1771 | case MediumType_Writethrough:
|
---|
1772 | case MediumType_Shareable:
|
---|
1773 | case MediumType_Readonly:
|
---|
1774 | {
|
---|
1775 | /* cannot change to writethrough, shareable or readonly
|
---|
1776 | * if there are children */
|
---|
1777 | if (i_getChildren().size() != 0)
|
---|
1778 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
1779 | tr("Cannot change type for medium '%s' since it has %d child media"),
|
---|
1780 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
1781 | if (aType == MediumType_Shareable)
|
---|
1782 | {
|
---|
1783 | MediumVariant_T variant = i_getVariant();
|
---|
1784 | if (!(variant & MediumVariant_Fixed))
|
---|
1785 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1786 | tr("Cannot change type for medium '%s' to 'Shareable' since it is a dynamic medium storage unit"),
|
---|
1787 | m->strLocationFull.c_str());
|
---|
1788 | }
|
---|
1789 | else if (aType == MediumType_Readonly && devType == DeviceType_HardDisk)
|
---|
1790 | {
|
---|
1791 | // Readonly hard disks are not allowed, this medium type is reserved for
|
---|
1792 | // DVDs and floppy images at the moment. Later we might allow readonly hard
|
---|
1793 | // disks, but that's extremely unusual and many guest OSes will have trouble.
|
---|
1794 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1795 | tr("Cannot change type for medium '%s' to 'Readonly' since it is a hard disk"),
|
---|
1796 | m->strLocationFull.c_str());
|
---|
1797 | }
|
---|
1798 | break;
|
---|
1799 | }
|
---|
1800 | default:
|
---|
1801 | AssertFailedReturn(E_FAIL);
|
---|
1802 | }
|
---|
1803 |
|
---|
1804 | if (aType == MediumType_MultiAttach)
|
---|
1805 | {
|
---|
1806 | // This type is new with VirtualBox 4.0 and therefore requires settings
|
---|
1807 | // version 1.11 in the settings backend. Unfortunately it is not enough to do
|
---|
1808 | // the usual routine in MachineConfigFile::bumpSettingsVersionIfNeeded() for
|
---|
1809 | // two reasons: The medium type is a property of the media registry tree, which
|
---|
1810 | // can reside in the global config file (for pre-4.0 media); we would therefore
|
---|
1811 | // possibly need to bump the global config version. We don't want to do that though
|
---|
1812 | // because that might make downgrading to pre-4.0 impossible.
|
---|
1813 | // As a result, we can only use these two new types if the medium is NOT in the
|
---|
1814 | // global registry:
|
---|
1815 | const Guid &uuidGlobalRegistry = m->pVirtualBox->i_getGlobalRegistryId();
|
---|
1816 | if (i_isInRegistry(uuidGlobalRegistry))
|
---|
1817 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1818 | tr("Cannot change type for medium '%s': the media type 'MultiAttach' can only be used "
|
---|
1819 | "on media registered with a machine that was created with VirtualBox 4.0 or later"),
|
---|
1820 | m->strLocationFull.c_str());
|
---|
1821 | }
|
---|
1822 |
|
---|
1823 | m->type = aType;
|
---|
1824 |
|
---|
1825 | // save the settings
|
---|
1826 | mlock.release();
|
---|
1827 | autoCaller.release();
|
---|
1828 | treeLock.release();
|
---|
1829 | i_markRegistriesModified();
|
---|
1830 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
1831 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
1832 |
|
---|
1833 | return S_OK;
|
---|
1834 | }
|
---|
1835 |
|
---|
1836 | HRESULT Medium::getAllowedTypes(std::vector<MediumType_T> &aAllowedTypes)
|
---|
1837 | {
|
---|
1838 | NOREF(aAllowedTypes);
|
---|
1839 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1840 |
|
---|
1841 | ReturnComNotImplemented();
|
---|
1842 | }
|
---|
1843 |
|
---|
1844 | HRESULT Medium::getParent(AutoCaller &autoCaller, ComPtr<IMedium> &aParent)
|
---|
1845 | {
|
---|
1846 | autoCaller.release();
|
---|
1847 |
|
---|
1848 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1849 | * the pVirtualBox reference, see #uninit(). */
|
---|
1850 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
1851 |
|
---|
1852 | /* we access m->pParent */
|
---|
1853 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
1854 |
|
---|
1855 | autoCaller.add();
|
---|
1856 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1857 |
|
---|
1858 | m->pParent.queryInterfaceTo(aParent.asOutParam());
|
---|
1859 |
|
---|
1860 | return S_OK;
|
---|
1861 | }
|
---|
1862 |
|
---|
1863 | HRESULT Medium::getChildren(AutoCaller &autoCaller, std::vector<ComPtr<IMedium> > &aChildren)
|
---|
1864 | {
|
---|
1865 | autoCaller.release();
|
---|
1866 |
|
---|
1867 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1868 | * the pVirtualBox reference, see #uninit(). */
|
---|
1869 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
1870 |
|
---|
1871 | /* we access children */
|
---|
1872 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
1873 |
|
---|
1874 | autoCaller.add();
|
---|
1875 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1876 |
|
---|
1877 | MediaList children(this->i_getChildren());
|
---|
1878 | aChildren.resize(children.size());
|
---|
1879 | size_t i = 0;
|
---|
1880 | for (MediaList::const_iterator it = children.begin(); it != children.end(); ++it, ++i)
|
---|
1881 | (*it).queryInterfaceTo(aChildren[i].asOutParam());
|
---|
1882 | return S_OK;
|
---|
1883 | }
|
---|
1884 |
|
---|
1885 | HRESULT Medium::getBase(AutoCaller &autoCaller, ComPtr<IMedium> &aBase)
|
---|
1886 | {
|
---|
1887 | autoCaller.release();
|
---|
1888 |
|
---|
1889 | /* i_getBase() will do callers/locking */
|
---|
1890 | i_getBase().queryInterfaceTo(aBase.asOutParam());
|
---|
1891 |
|
---|
1892 | return S_OK;
|
---|
1893 | }
|
---|
1894 |
|
---|
1895 | HRESULT Medium::getReadOnly(AutoCaller &autoCaller, BOOL *aReadOnly)
|
---|
1896 | {
|
---|
1897 | autoCaller.release();
|
---|
1898 |
|
---|
1899 | /* isReadOnly() will do locking */
|
---|
1900 | *aReadOnly = i_isReadOnly();
|
---|
1901 |
|
---|
1902 | return S_OK;
|
---|
1903 | }
|
---|
1904 |
|
---|
1905 | HRESULT Medium::getLogicalSize(LONG64 *aLogicalSize)
|
---|
1906 | {
|
---|
1907 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1908 |
|
---|
1909 | *aLogicalSize = m->logicalSize;
|
---|
1910 |
|
---|
1911 | return S_OK;
|
---|
1912 | }
|
---|
1913 |
|
---|
1914 | HRESULT Medium::getAutoReset(BOOL *aAutoReset)
|
---|
1915 | {
|
---|
1916 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1917 |
|
---|
1918 | if (m->pParent.isNull())
|
---|
1919 | *aAutoReset = FALSE;
|
---|
1920 | else
|
---|
1921 | *aAutoReset = m->autoReset;
|
---|
1922 |
|
---|
1923 | return S_OK;
|
---|
1924 | }
|
---|
1925 |
|
---|
1926 | HRESULT Medium::setAutoReset(BOOL aAutoReset)
|
---|
1927 | {
|
---|
1928 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1929 |
|
---|
1930 | if (m->pParent.isNull())
|
---|
1931 | return setError(VBOX_E_NOT_SUPPORTED,
|
---|
1932 | tr("Medium '%s' is not differencing"),
|
---|
1933 | m->strLocationFull.c_str());
|
---|
1934 |
|
---|
1935 | if (m->autoReset != !!aAutoReset)
|
---|
1936 | {
|
---|
1937 | m->autoReset = !!aAutoReset;
|
---|
1938 |
|
---|
1939 | // save the settings
|
---|
1940 | mlock.release();
|
---|
1941 | i_markRegistriesModified();
|
---|
1942 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
1943 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
1944 | }
|
---|
1945 |
|
---|
1946 | return S_OK;
|
---|
1947 | }
|
---|
1948 |
|
---|
1949 | HRESULT Medium::getLastAccessError(com::Utf8Str &aLastAccessError)
|
---|
1950 | {
|
---|
1951 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1952 |
|
---|
1953 | aLastAccessError = m->strLastAccessError;
|
---|
1954 |
|
---|
1955 | return S_OK;
|
---|
1956 | }
|
---|
1957 |
|
---|
1958 | HRESULT Medium::getMachineIds(std::vector<com::Guid> &aMachineIds)
|
---|
1959 | {
|
---|
1960 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1961 |
|
---|
1962 | if (m->backRefs.size() != 0)
|
---|
1963 | {
|
---|
1964 | BackRefList brlist(m->backRefs);
|
---|
1965 | aMachineIds.resize(brlist.size());
|
---|
1966 | size_t i = 0;
|
---|
1967 | for (BackRefList::const_iterator it = brlist.begin(); it != brlist.end(); ++it, ++i)
|
---|
1968 | aMachineIds[i] = it->machineId;
|
---|
1969 | }
|
---|
1970 |
|
---|
1971 | return S_OK;
|
---|
1972 | }
|
---|
1973 |
|
---|
1974 | HRESULT Medium::setIds(AutoCaller &autoCaller,
|
---|
1975 | BOOL aSetImageId,
|
---|
1976 | const com::Guid &aImageId,
|
---|
1977 | BOOL aSetParentId,
|
---|
1978 | const com::Guid &aParentId)
|
---|
1979 | {
|
---|
1980 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1981 |
|
---|
1982 | switch (m->state)
|
---|
1983 | {
|
---|
1984 | case MediumState_Created:
|
---|
1985 | break;
|
---|
1986 | default:
|
---|
1987 | return i_setStateError();
|
---|
1988 | }
|
---|
1989 |
|
---|
1990 | Guid imageId, parentId;
|
---|
1991 | if (aSetImageId)
|
---|
1992 | {
|
---|
1993 | if (aImageId.isZero())
|
---|
1994 | imageId.create();
|
---|
1995 | else
|
---|
1996 | {
|
---|
1997 | imageId = aImageId;
|
---|
1998 | if (!imageId.isValid())
|
---|
1999 | return setError(E_INVALIDARG, tr("Argument %s is invalid"), "aImageId");
|
---|
2000 | }
|
---|
2001 | }
|
---|
2002 | if (aSetParentId)
|
---|
2003 | {
|
---|
2004 | if (aParentId.isZero())
|
---|
2005 | parentId.create();
|
---|
2006 | else
|
---|
2007 | parentId = aParentId;
|
---|
2008 | }
|
---|
2009 |
|
---|
2010 | const Guid uPrevImage = m->uuidImage;
|
---|
2011 | unconst(m->uuidImage) = imageId;
|
---|
2012 | ComObjPtr<Medium> pPrevParent = i_getParent();
|
---|
2013 | unconst(m->uuidParentImage) = parentId;
|
---|
2014 |
|
---|
2015 | // must not hold any locks before calling Medium::i_queryInfo
|
---|
2016 | alock.release();
|
---|
2017 |
|
---|
2018 | HRESULT rc = i_queryInfo(!!aSetImageId /* fSetImageId */,
|
---|
2019 | !!aSetParentId /* fSetParentId */,
|
---|
2020 | autoCaller);
|
---|
2021 |
|
---|
2022 | AutoReadLock arlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2023 | const Guid uCurrImage = m->uuidImage;
|
---|
2024 | ComObjPtr<Medium> pCurrParent = i_getParent();
|
---|
2025 | arlock.release();
|
---|
2026 |
|
---|
2027 | if (SUCCEEDED(rc))
|
---|
2028 | {
|
---|
2029 | if (uCurrImage != uPrevImage)
|
---|
2030 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
2031 | if (pPrevParent != pCurrParent)
|
---|
2032 | {
|
---|
2033 | if (pPrevParent)
|
---|
2034 | m->pVirtualBox->i_onMediumConfigChanged(pPrevParent);
|
---|
2035 | if (pCurrParent)
|
---|
2036 | m->pVirtualBox->i_onMediumConfigChanged(pCurrParent);
|
---|
2037 | }
|
---|
2038 | }
|
---|
2039 |
|
---|
2040 | return rc;
|
---|
2041 | }
|
---|
2042 |
|
---|
2043 | HRESULT Medium::refreshState(AutoCaller &autoCaller, MediumState_T *aState)
|
---|
2044 | {
|
---|
2045 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2046 |
|
---|
2047 | HRESULT rc = S_OK;
|
---|
2048 |
|
---|
2049 | switch (m->state)
|
---|
2050 | {
|
---|
2051 | case MediumState_Created:
|
---|
2052 | case MediumState_Inaccessible:
|
---|
2053 | case MediumState_LockedRead:
|
---|
2054 | {
|
---|
2055 | // must not hold any locks before calling Medium::i_queryInfo
|
---|
2056 | alock.release();
|
---|
2057 |
|
---|
2058 | rc = i_queryInfo(false /* fSetImageId */, false /* fSetParentId */,
|
---|
2059 | autoCaller);
|
---|
2060 |
|
---|
2061 | alock.acquire();
|
---|
2062 | break;
|
---|
2063 | }
|
---|
2064 | default:
|
---|
2065 | break;
|
---|
2066 | }
|
---|
2067 |
|
---|
2068 | *aState = m->state;
|
---|
2069 |
|
---|
2070 | return rc;
|
---|
2071 | }
|
---|
2072 |
|
---|
2073 | HRESULT Medium::getSnapshotIds(const com::Guid &aMachineId,
|
---|
2074 | std::vector<com::Guid> &aSnapshotIds)
|
---|
2075 | {
|
---|
2076 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2077 |
|
---|
2078 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
2079 | it != m->backRefs.end(); ++it)
|
---|
2080 | {
|
---|
2081 | if (it->machineId == aMachineId)
|
---|
2082 | {
|
---|
2083 | size_t size = it->llSnapshotIds.size();
|
---|
2084 |
|
---|
2085 | /* if the medium is attached to the machine in the current state, we
|
---|
2086 | * return its ID as the first element of the array */
|
---|
2087 | if (it->fInCurState)
|
---|
2088 | ++size;
|
---|
2089 |
|
---|
2090 | if (size > 0)
|
---|
2091 | {
|
---|
2092 | aSnapshotIds.resize(size);
|
---|
2093 |
|
---|
2094 | size_t j = 0;
|
---|
2095 | if (it->fInCurState)
|
---|
2096 | aSnapshotIds[j++] = it->machineId.toUtf16();
|
---|
2097 |
|
---|
2098 | for(GuidList::const_iterator jt = it->llSnapshotIds.begin(); jt != it->llSnapshotIds.end(); ++jt, ++j)
|
---|
2099 | aSnapshotIds[j] = (*jt);
|
---|
2100 | }
|
---|
2101 |
|
---|
2102 | break;
|
---|
2103 | }
|
---|
2104 | }
|
---|
2105 |
|
---|
2106 | return S_OK;
|
---|
2107 | }
|
---|
2108 |
|
---|
2109 | HRESULT Medium::lockRead(ComPtr<IToken> &aToken)
|
---|
2110 | {
|
---|
2111 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2112 |
|
---|
2113 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
2114 | if (m->queryInfoRunning)
|
---|
2115 | {
|
---|
2116 | /* Must not hold the media tree lock, as Medium::i_queryInfo needs this
|
---|
2117 | * lock and thus we would run into a deadlock here. */
|
---|
2118 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
2119 | while (m->queryInfoRunning)
|
---|
2120 | {
|
---|
2121 | alock.release();
|
---|
2122 | /* must not hold the object lock now */
|
---|
2123 | Assert(!isWriteLockOnCurrentThread());
|
---|
2124 | {
|
---|
2125 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
2126 | }
|
---|
2127 | alock.acquire();
|
---|
2128 | }
|
---|
2129 | }
|
---|
2130 |
|
---|
2131 | HRESULT rc = S_OK;
|
---|
2132 |
|
---|
2133 | switch (m->state)
|
---|
2134 | {
|
---|
2135 | case MediumState_Created:
|
---|
2136 | case MediumState_Inaccessible:
|
---|
2137 | case MediumState_LockedRead:
|
---|
2138 | {
|
---|
2139 | ++m->readers;
|
---|
2140 |
|
---|
2141 | ComAssertMsgBreak(m->readers != 0, ("Counter overflow"), rc = E_FAIL);
|
---|
2142 |
|
---|
2143 | /* Remember pre-lock state */
|
---|
2144 | if (m->state != MediumState_LockedRead)
|
---|
2145 | m->preLockState = m->state;
|
---|
2146 |
|
---|
2147 | LogFlowThisFunc(("Okay - prev state=%d readers=%d\n", m->state, m->readers));
|
---|
2148 | m->state = MediumState_LockedRead;
|
---|
2149 |
|
---|
2150 | ComObjPtr<MediumLockToken> pToken;
|
---|
2151 | rc = pToken.createObject();
|
---|
2152 | if (SUCCEEDED(rc))
|
---|
2153 | rc = pToken->init(this, false /* fWrite */);
|
---|
2154 | if (FAILED(rc))
|
---|
2155 | {
|
---|
2156 | --m->readers;
|
---|
2157 | if (m->readers == 0)
|
---|
2158 | m->state = m->preLockState;
|
---|
2159 | return rc;
|
---|
2160 | }
|
---|
2161 |
|
---|
2162 | pToken.queryInterfaceTo(aToken.asOutParam());
|
---|
2163 | break;
|
---|
2164 | }
|
---|
2165 | default:
|
---|
2166 | {
|
---|
2167 | LogFlowThisFunc(("Failing - state=%d\n", m->state));
|
---|
2168 | rc = i_setStateError();
|
---|
2169 | break;
|
---|
2170 | }
|
---|
2171 | }
|
---|
2172 |
|
---|
2173 | return rc;
|
---|
2174 | }
|
---|
2175 |
|
---|
2176 | /**
|
---|
2177 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
2178 | * in-process calls).
|
---|
2179 | */
|
---|
2180 | HRESULT Medium::i_unlockRead(MediumState_T *aState)
|
---|
2181 | {
|
---|
2182 | AutoCaller autoCaller(this);
|
---|
2183 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2184 |
|
---|
2185 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2186 |
|
---|
2187 | HRESULT rc = S_OK;
|
---|
2188 |
|
---|
2189 | switch (m->state)
|
---|
2190 | {
|
---|
2191 | case MediumState_LockedRead:
|
---|
2192 | {
|
---|
2193 | ComAssertMsgBreak(m->readers != 0, ("Counter underflow"), rc = E_FAIL);
|
---|
2194 | --m->readers;
|
---|
2195 |
|
---|
2196 | /* Reset the state after the last reader */
|
---|
2197 | if (m->readers == 0)
|
---|
2198 | {
|
---|
2199 | m->state = m->preLockState;
|
---|
2200 | /* There are cases where we inject the deleting state into
|
---|
2201 | * a medium locked for reading. Make sure #unmarkForDeletion()
|
---|
2202 | * gets the right state afterwards. */
|
---|
2203 | if (m->preLockState == MediumState_Deleting)
|
---|
2204 | m->preLockState = MediumState_Created;
|
---|
2205 | }
|
---|
2206 |
|
---|
2207 | LogFlowThisFunc(("new state=%d\n", m->state));
|
---|
2208 | break;
|
---|
2209 | }
|
---|
2210 | default:
|
---|
2211 | {
|
---|
2212 | LogFlowThisFunc(("Failing - state=%d\n", m->state));
|
---|
2213 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2214 | tr("Medium '%s' is not locked for reading"),
|
---|
2215 | m->strLocationFull.c_str());
|
---|
2216 | break;
|
---|
2217 | }
|
---|
2218 | }
|
---|
2219 |
|
---|
2220 | /* return the current state after */
|
---|
2221 | if (aState)
|
---|
2222 | *aState = m->state;
|
---|
2223 |
|
---|
2224 | return rc;
|
---|
2225 | }
|
---|
2226 | HRESULT Medium::lockWrite(ComPtr<IToken> &aToken)
|
---|
2227 | {
|
---|
2228 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2229 |
|
---|
2230 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
2231 | if (m->queryInfoRunning)
|
---|
2232 | {
|
---|
2233 | /* Must not hold the media tree lock, as Medium::i_queryInfo needs this
|
---|
2234 | * lock and thus we would run into a deadlock here. */
|
---|
2235 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
2236 | while (m->queryInfoRunning)
|
---|
2237 | {
|
---|
2238 | alock.release();
|
---|
2239 | /* must not hold the object lock now */
|
---|
2240 | Assert(!isWriteLockOnCurrentThread());
|
---|
2241 | {
|
---|
2242 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
2243 | }
|
---|
2244 | alock.acquire();
|
---|
2245 | }
|
---|
2246 | }
|
---|
2247 |
|
---|
2248 | HRESULT rc = S_OK;
|
---|
2249 |
|
---|
2250 | switch (m->state)
|
---|
2251 | {
|
---|
2252 | case MediumState_Created:
|
---|
2253 | case MediumState_Inaccessible:
|
---|
2254 | {
|
---|
2255 | m->preLockState = m->state;
|
---|
2256 |
|
---|
2257 | LogFlowThisFunc(("Okay - prev state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2258 | m->state = MediumState_LockedWrite;
|
---|
2259 |
|
---|
2260 | ComObjPtr<MediumLockToken> pToken;
|
---|
2261 | rc = pToken.createObject();
|
---|
2262 | if (SUCCEEDED(rc))
|
---|
2263 | rc = pToken->init(this, true /* fWrite */);
|
---|
2264 | if (FAILED(rc))
|
---|
2265 | {
|
---|
2266 | m->state = m->preLockState;
|
---|
2267 | return rc;
|
---|
2268 | }
|
---|
2269 |
|
---|
2270 | pToken.queryInterfaceTo(aToken.asOutParam());
|
---|
2271 | break;
|
---|
2272 | }
|
---|
2273 | default:
|
---|
2274 | {
|
---|
2275 | LogFlowThisFunc(("Failing - state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2276 | rc = i_setStateError();
|
---|
2277 | break;
|
---|
2278 | }
|
---|
2279 | }
|
---|
2280 |
|
---|
2281 | return rc;
|
---|
2282 | }
|
---|
2283 |
|
---|
2284 | /**
|
---|
2285 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
2286 | * in-process calls).
|
---|
2287 | */
|
---|
2288 | HRESULT Medium::i_unlockWrite(MediumState_T *aState)
|
---|
2289 | {
|
---|
2290 | AutoCaller autoCaller(this);
|
---|
2291 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2292 |
|
---|
2293 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2294 |
|
---|
2295 | HRESULT rc = S_OK;
|
---|
2296 |
|
---|
2297 | switch (m->state)
|
---|
2298 | {
|
---|
2299 | case MediumState_LockedWrite:
|
---|
2300 | {
|
---|
2301 | m->state = m->preLockState;
|
---|
2302 | /* There are cases where we inject the deleting state into
|
---|
2303 | * a medium locked for writing. Make sure #unmarkForDeletion()
|
---|
2304 | * gets the right state afterwards. */
|
---|
2305 | if (m->preLockState == MediumState_Deleting)
|
---|
2306 | m->preLockState = MediumState_Created;
|
---|
2307 | LogFlowThisFunc(("new state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2308 | break;
|
---|
2309 | }
|
---|
2310 | default:
|
---|
2311 | {
|
---|
2312 | LogFlowThisFunc(("Failing - state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2313 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2314 | tr("Medium '%s' is not locked for writing"),
|
---|
2315 | m->strLocationFull.c_str());
|
---|
2316 | break;
|
---|
2317 | }
|
---|
2318 | }
|
---|
2319 |
|
---|
2320 | /* return the current state after */
|
---|
2321 | if (aState)
|
---|
2322 | *aState = m->state;
|
---|
2323 |
|
---|
2324 | return rc;
|
---|
2325 | }
|
---|
2326 |
|
---|
2327 | HRESULT Medium::close(AutoCaller &aAutoCaller)
|
---|
2328 | {
|
---|
2329 | // make a copy of VirtualBox pointer which gets nulled by uninit()
|
---|
2330 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
2331 |
|
---|
2332 | Guid uId = i_getId();
|
---|
2333 | DeviceType_T devType = i_getDeviceType();
|
---|
2334 | MultiResult mrc = i_close(aAutoCaller);
|
---|
2335 |
|
---|
2336 | pVirtualBox->i_saveModifiedRegistries();
|
---|
2337 |
|
---|
2338 | if (SUCCEEDED(mrc))
|
---|
2339 | pVirtualBox->i_onMediumRegistered(uId, devType, FALSE);
|
---|
2340 |
|
---|
2341 | return mrc;
|
---|
2342 | }
|
---|
2343 |
|
---|
2344 | HRESULT Medium::getProperty(const com::Utf8Str &aName,
|
---|
2345 | com::Utf8Str &aValue)
|
---|
2346 | {
|
---|
2347 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2348 |
|
---|
2349 | settings::StringsMap::const_iterator it = m->mapProperties.find(aName);
|
---|
2350 | if (it == m->mapProperties.end())
|
---|
2351 | {
|
---|
2352 | if (!aName.startsWith("Special/"))
|
---|
2353 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2354 | tr("Property '%s' does not exist"), aName.c_str());
|
---|
2355 | else
|
---|
2356 | /* be more silent here */
|
---|
2357 | return VBOX_E_OBJECT_NOT_FOUND;
|
---|
2358 | }
|
---|
2359 |
|
---|
2360 | aValue = it->second;
|
---|
2361 |
|
---|
2362 | return S_OK;
|
---|
2363 | }
|
---|
2364 |
|
---|
2365 | HRESULT Medium::setProperty(const com::Utf8Str &aName,
|
---|
2366 | const com::Utf8Str &aValue)
|
---|
2367 | {
|
---|
2368 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2369 |
|
---|
2370 | switch (m->state)
|
---|
2371 | {
|
---|
2372 | case MediumState_NotCreated:
|
---|
2373 | case MediumState_Created:
|
---|
2374 | case MediumState_Inaccessible:
|
---|
2375 | break;
|
---|
2376 | default:
|
---|
2377 | return i_setStateError();
|
---|
2378 | }
|
---|
2379 |
|
---|
2380 | settings::StringsMap::iterator it = m->mapProperties.find(aName);
|
---|
2381 | if ( !aName.startsWith("Special/")
|
---|
2382 | && !i_isPropertyForFilter(aName))
|
---|
2383 | {
|
---|
2384 | if (it == m->mapProperties.end())
|
---|
2385 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2386 | tr("Property '%s' does not exist"),
|
---|
2387 | aName.c_str());
|
---|
2388 | it->second = aValue;
|
---|
2389 | }
|
---|
2390 | else
|
---|
2391 | {
|
---|
2392 | if (it == m->mapProperties.end())
|
---|
2393 | {
|
---|
2394 | if (!aValue.isEmpty())
|
---|
2395 | m->mapProperties[aName] = aValue;
|
---|
2396 | }
|
---|
2397 | else
|
---|
2398 | {
|
---|
2399 | if (!aValue.isEmpty())
|
---|
2400 | it->second = aValue;
|
---|
2401 | else
|
---|
2402 | m->mapProperties.erase(it);
|
---|
2403 | }
|
---|
2404 | }
|
---|
2405 |
|
---|
2406 | // save the settings
|
---|
2407 | mlock.release();
|
---|
2408 | i_markRegistriesModified();
|
---|
2409 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2410 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
2411 |
|
---|
2412 | return S_OK;
|
---|
2413 | }
|
---|
2414 |
|
---|
2415 | HRESULT Medium::getProperties(const com::Utf8Str &aNames,
|
---|
2416 | std::vector<com::Utf8Str> &aReturnNames,
|
---|
2417 | std::vector<com::Utf8Str> &aReturnValues)
|
---|
2418 | {
|
---|
2419 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2420 |
|
---|
2421 | /// @todo make use of aNames according to the documentation
|
---|
2422 | NOREF(aNames);
|
---|
2423 |
|
---|
2424 | aReturnNames.resize(m->mapProperties.size());
|
---|
2425 | aReturnValues.resize(m->mapProperties.size());
|
---|
2426 | size_t i = 0;
|
---|
2427 | for (settings::StringsMap::const_iterator it = m->mapProperties.begin();
|
---|
2428 | it != m->mapProperties.end();
|
---|
2429 | ++it, ++i)
|
---|
2430 | {
|
---|
2431 | aReturnNames[i] = it->first;
|
---|
2432 | aReturnValues[i] = it->second;
|
---|
2433 | }
|
---|
2434 | return S_OK;
|
---|
2435 | }
|
---|
2436 |
|
---|
2437 | HRESULT Medium::setProperties(const std::vector<com::Utf8Str> &aNames,
|
---|
2438 | const std::vector<com::Utf8Str> &aValues)
|
---|
2439 | {
|
---|
2440 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2441 |
|
---|
2442 | /* first pass: validate names */
|
---|
2443 | for (size_t i = 0;
|
---|
2444 | i < aNames.size();
|
---|
2445 | ++i)
|
---|
2446 | {
|
---|
2447 | Utf8Str strName(aNames[i]);
|
---|
2448 | if ( !strName.startsWith("Special/")
|
---|
2449 | && !i_isPropertyForFilter(strName)
|
---|
2450 | && m->mapProperties.find(strName) == m->mapProperties.end())
|
---|
2451 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2452 | tr("Property '%s' does not exist"), strName.c_str());
|
---|
2453 | }
|
---|
2454 |
|
---|
2455 | /* second pass: assign */
|
---|
2456 | for (size_t i = 0;
|
---|
2457 | i < aNames.size();
|
---|
2458 | ++i)
|
---|
2459 | {
|
---|
2460 | Utf8Str strName(aNames[i]);
|
---|
2461 | Utf8Str strValue(aValues[i]);
|
---|
2462 | settings::StringsMap::iterator it = m->mapProperties.find(strName);
|
---|
2463 | if ( !strName.startsWith("Special/")
|
---|
2464 | && !i_isPropertyForFilter(strName))
|
---|
2465 | {
|
---|
2466 | AssertReturn(it != m->mapProperties.end(), E_FAIL);
|
---|
2467 | it->second = strValue;
|
---|
2468 | }
|
---|
2469 | else
|
---|
2470 | {
|
---|
2471 | if (it == m->mapProperties.end())
|
---|
2472 | {
|
---|
2473 | if (!strValue.isEmpty())
|
---|
2474 | m->mapProperties[strName] = strValue;
|
---|
2475 | }
|
---|
2476 | else
|
---|
2477 | {
|
---|
2478 | if (!strValue.isEmpty())
|
---|
2479 | it->second = strValue;
|
---|
2480 | else
|
---|
2481 | m->mapProperties.erase(it);
|
---|
2482 | }
|
---|
2483 | }
|
---|
2484 | }
|
---|
2485 |
|
---|
2486 | // save the settings
|
---|
2487 | mlock.release();
|
---|
2488 | i_markRegistriesModified();
|
---|
2489 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2490 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
2491 |
|
---|
2492 | return S_OK;
|
---|
2493 | }
|
---|
2494 |
|
---|
2495 | HRESULT Medium::createBaseStorage(LONG64 aLogicalSize,
|
---|
2496 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2497 | ComPtr<IProgress> &aProgress)
|
---|
2498 | {
|
---|
2499 | if (aLogicalSize < 0)
|
---|
2500 | return setError(E_INVALIDARG, tr("The medium size argument (%lld) is negative"), aLogicalSize);
|
---|
2501 |
|
---|
2502 | HRESULT rc = S_OK;
|
---|
2503 | ComObjPtr<Progress> pProgress;
|
---|
2504 | Medium::Task *pTask = NULL;
|
---|
2505 |
|
---|
2506 | try
|
---|
2507 | {
|
---|
2508 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2509 |
|
---|
2510 | ULONG mediumVariantFlags = 0;
|
---|
2511 |
|
---|
2512 | if (aVariant.size())
|
---|
2513 | {
|
---|
2514 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
2515 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
2516 | }
|
---|
2517 |
|
---|
2518 | mediumVariantFlags &= ((unsigned)~MediumVariant_Diff);
|
---|
2519 |
|
---|
2520 | if ( !(mediumVariantFlags & MediumVariant_Fixed)
|
---|
2521 | && !(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_CreateDynamic))
|
---|
2522 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2523 | tr("Medium format '%s' does not support dynamic storage creation"),
|
---|
2524 | m->strFormat.c_str());
|
---|
2525 |
|
---|
2526 | if ( (mediumVariantFlags & MediumVariant_Fixed)
|
---|
2527 | && !(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_CreateFixed))
|
---|
2528 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2529 | tr("Medium format '%s' does not support fixed storage creation"),
|
---|
2530 | m->strFormat.c_str());
|
---|
2531 |
|
---|
2532 | if ( (mediumVariantFlags & MediumVariant_Formatted)
|
---|
2533 | && i_getDeviceType() != DeviceType_Floppy)
|
---|
2534 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2535 | tr("Medium variant 'formatted' applies to floppy images only"));
|
---|
2536 |
|
---|
2537 | if (m->state != MediumState_NotCreated)
|
---|
2538 | throw i_setStateError();
|
---|
2539 |
|
---|
2540 | pProgress.createObject();
|
---|
2541 | rc = pProgress->init(m->pVirtualBox,
|
---|
2542 | static_cast<IMedium*>(this),
|
---|
2543 | (mediumVariantFlags & MediumVariant_Fixed)
|
---|
2544 | ? BstrFmt(tr("Creating fixed medium storage unit '%s'"), m->strLocationFull.c_str()).raw()
|
---|
2545 | : BstrFmt(tr("Creating dynamic medium storage unit '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
2546 | TRUE /* aCancelable */);
|
---|
2547 | if (FAILED(rc))
|
---|
2548 | throw rc;
|
---|
2549 |
|
---|
2550 | /* setup task object to carry out the operation asynchronously */
|
---|
2551 | pTask = new Medium::CreateBaseTask(this, pProgress, aLogicalSize,
|
---|
2552 | (MediumVariant_T)mediumVariantFlags);
|
---|
2553 | rc = pTask->rc();
|
---|
2554 | AssertComRC(rc);
|
---|
2555 | if (FAILED(rc))
|
---|
2556 | throw rc;
|
---|
2557 |
|
---|
2558 | m->state = MediumState_Creating;
|
---|
2559 | }
|
---|
2560 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2561 |
|
---|
2562 | if (SUCCEEDED(rc))
|
---|
2563 | {
|
---|
2564 | rc = pTask->createThread();
|
---|
2565 |
|
---|
2566 | if (SUCCEEDED(rc))
|
---|
2567 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2568 | }
|
---|
2569 | else if (pTask != NULL)
|
---|
2570 | delete pTask;
|
---|
2571 |
|
---|
2572 | return rc;
|
---|
2573 | }
|
---|
2574 |
|
---|
2575 | HRESULT Medium::deleteStorage(ComPtr<IProgress> &aProgress)
|
---|
2576 | {
|
---|
2577 | ComObjPtr<Progress> pProgress;
|
---|
2578 |
|
---|
2579 | MultiResult mrc = i_deleteStorage(&pProgress,
|
---|
2580 | false /* aWait */,
|
---|
2581 | true /* aNotify */);
|
---|
2582 | /* Must save the registries in any case, since an entry was removed. */
|
---|
2583 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2584 |
|
---|
2585 | if (SUCCEEDED(mrc))
|
---|
2586 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2587 |
|
---|
2588 | return mrc;
|
---|
2589 | }
|
---|
2590 |
|
---|
2591 | HRESULT Medium::createDiffStorage(AutoCaller &autoCaller,
|
---|
2592 | const ComPtr<IMedium> &aTarget,
|
---|
2593 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2594 | ComPtr<IProgress> &aProgress)
|
---|
2595 | {
|
---|
2596 | IMedium *aT = aTarget;
|
---|
2597 | ComObjPtr<Medium> diff = static_cast<Medium*>(aT);
|
---|
2598 |
|
---|
2599 | autoCaller.release();
|
---|
2600 |
|
---|
2601 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
2602 | * the pVirtualBox reference, see #uninit(). */
|
---|
2603 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
2604 |
|
---|
2605 | // we access m->pParent
|
---|
2606 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
2607 |
|
---|
2608 | autoCaller.add();
|
---|
2609 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2610 |
|
---|
2611 | AutoMultiWriteLock2 alock(this, diff COMMA_LOCKVAL_SRC_POS);
|
---|
2612 |
|
---|
2613 | if (m->type == MediumType_Writethrough)
|
---|
2614 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2615 | tr("Medium type of '%s' is Writethrough"),
|
---|
2616 | m->strLocationFull.c_str());
|
---|
2617 | else if (m->type == MediumType_Shareable)
|
---|
2618 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2619 | tr("Medium type of '%s' is Shareable"),
|
---|
2620 | m->strLocationFull.c_str());
|
---|
2621 | else if (m->type == MediumType_Readonly)
|
---|
2622 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2623 | tr("Medium type of '%s' is Readonly"),
|
---|
2624 | m->strLocationFull.c_str());
|
---|
2625 |
|
---|
2626 | /* Apply the normal locking logic to the entire chain. */
|
---|
2627 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
2628 | alock.release();
|
---|
2629 | autoCaller.release();
|
---|
2630 | treeLock.release();
|
---|
2631 | HRESULT rc = diff->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2632 | diff /* pToLockWrite */,
|
---|
2633 | false /* fMediumLockWriteAll */,
|
---|
2634 | this,
|
---|
2635 | *pMediumLockList);
|
---|
2636 | treeLock.acquire();
|
---|
2637 | autoCaller.add();
|
---|
2638 | if (FAILED(autoCaller.rc()))
|
---|
2639 | rc = autoCaller.rc();
|
---|
2640 | alock.acquire();
|
---|
2641 | if (FAILED(rc))
|
---|
2642 | {
|
---|
2643 | delete pMediumLockList;
|
---|
2644 | return rc;
|
---|
2645 | }
|
---|
2646 |
|
---|
2647 | alock.release();
|
---|
2648 | autoCaller.release();
|
---|
2649 | treeLock.release();
|
---|
2650 | rc = pMediumLockList->Lock();
|
---|
2651 | treeLock.acquire();
|
---|
2652 | autoCaller.add();
|
---|
2653 | if (FAILED(autoCaller.rc()))
|
---|
2654 | rc = autoCaller.rc();
|
---|
2655 | alock.acquire();
|
---|
2656 | if (FAILED(rc))
|
---|
2657 | {
|
---|
2658 | delete pMediumLockList;
|
---|
2659 |
|
---|
2660 | return setError(rc, tr("Could not lock medium when creating diff '%s'"),
|
---|
2661 | diff->i_getLocationFull().c_str());
|
---|
2662 | }
|
---|
2663 |
|
---|
2664 | Guid parentMachineRegistry;
|
---|
2665 | if (i_getFirstRegistryMachineId(parentMachineRegistry))
|
---|
2666 | {
|
---|
2667 | /* since this medium has been just created it isn't associated yet */
|
---|
2668 | diff->m->llRegistryIDs.push_back(parentMachineRegistry);
|
---|
2669 | alock.release();
|
---|
2670 | autoCaller.release();
|
---|
2671 | treeLock.release();
|
---|
2672 | diff->i_markRegistriesModified();
|
---|
2673 | treeLock.acquire();
|
---|
2674 | autoCaller.add();
|
---|
2675 | alock.acquire();
|
---|
2676 | }
|
---|
2677 |
|
---|
2678 | alock.release();
|
---|
2679 | autoCaller.release();
|
---|
2680 | treeLock.release();
|
---|
2681 |
|
---|
2682 | ComObjPtr<Progress> pProgress;
|
---|
2683 |
|
---|
2684 | ULONG mediumVariantFlags = 0;
|
---|
2685 |
|
---|
2686 | if (aVariant.size())
|
---|
2687 | {
|
---|
2688 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
2689 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
2690 | }
|
---|
2691 |
|
---|
2692 | if (mediumVariantFlags & MediumVariant_Formatted)
|
---|
2693 | {
|
---|
2694 | delete pMediumLockList;
|
---|
2695 | return setError(VBOX_E_NOT_SUPPORTED,
|
---|
2696 | tr("Medium variant 'formatted' applies to floppy images only"));
|
---|
2697 | }
|
---|
2698 |
|
---|
2699 | rc = i_createDiffStorage(diff, (MediumVariant_T)mediumVariantFlags, pMediumLockList,
|
---|
2700 | &pProgress, false /* aWait */, true /* aNotify */);
|
---|
2701 | if (FAILED(rc))
|
---|
2702 | delete pMediumLockList;
|
---|
2703 | else
|
---|
2704 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2705 |
|
---|
2706 | return rc;
|
---|
2707 | }
|
---|
2708 |
|
---|
2709 | HRESULT Medium::mergeTo(const ComPtr<IMedium> &aTarget,
|
---|
2710 | ComPtr<IProgress> &aProgress)
|
---|
2711 | {
|
---|
2712 | IMedium *aT = aTarget;
|
---|
2713 |
|
---|
2714 | ComAssertRet(aT != this, E_INVALIDARG);
|
---|
2715 |
|
---|
2716 | ComObjPtr<Medium> pTarget = static_cast<Medium*>(aT);
|
---|
2717 |
|
---|
2718 | bool fMergeForward = false;
|
---|
2719 | ComObjPtr<Medium> pParentForTarget;
|
---|
2720 | MediumLockList *pChildrenToReparent = NULL;
|
---|
2721 | MediumLockList *pMediumLockList = NULL;
|
---|
2722 |
|
---|
2723 | HRESULT rc = S_OK;
|
---|
2724 |
|
---|
2725 | rc = i_prepareMergeTo(pTarget, NULL, NULL, true, fMergeForward,
|
---|
2726 | pParentForTarget, pChildrenToReparent, pMediumLockList);
|
---|
2727 | if (FAILED(rc)) return rc;
|
---|
2728 |
|
---|
2729 | ComObjPtr<Progress> pProgress;
|
---|
2730 |
|
---|
2731 | rc = i_mergeTo(pTarget, fMergeForward, pParentForTarget, pChildrenToReparent,
|
---|
2732 | pMediumLockList, &pProgress, false /* aWait */, true /* aNotify */);
|
---|
2733 | if (FAILED(rc))
|
---|
2734 | i_cancelMergeTo(pChildrenToReparent, pMediumLockList);
|
---|
2735 | else
|
---|
2736 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2737 |
|
---|
2738 | return rc;
|
---|
2739 | }
|
---|
2740 |
|
---|
2741 | HRESULT Medium::cloneToBase(const ComPtr<IMedium> &aTarget,
|
---|
2742 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2743 | ComPtr<IProgress> &aProgress)
|
---|
2744 | {
|
---|
2745 | int rc = S_OK;
|
---|
2746 |
|
---|
2747 | rc = cloneTo(aTarget, aVariant, NULL, aProgress);
|
---|
2748 | return rc;
|
---|
2749 | }
|
---|
2750 |
|
---|
2751 | HRESULT Medium::cloneTo(const ComPtr<IMedium> &aTarget,
|
---|
2752 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2753 | const ComPtr<IMedium> &aParent,
|
---|
2754 | ComPtr<IProgress> &aProgress)
|
---|
2755 | {
|
---|
2756 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
2757 | * to lock order violations, it probably causes lock order issues related
|
---|
2758 | * to the AutoCaller usage. */
|
---|
2759 | ComAssertRet(aTarget != this, E_INVALIDARG);
|
---|
2760 |
|
---|
2761 | IMedium *aT = aTarget;
|
---|
2762 | ComObjPtr<Medium> pTarget = static_cast<Medium*>(aT);
|
---|
2763 | ComObjPtr<Medium> pParent;
|
---|
2764 | if (aParent)
|
---|
2765 | {
|
---|
2766 | IMedium *aP = aParent;
|
---|
2767 | pParent = static_cast<Medium*>(aP);
|
---|
2768 | }
|
---|
2769 |
|
---|
2770 | HRESULT rc = S_OK;
|
---|
2771 | ComObjPtr<Progress> pProgress;
|
---|
2772 | Medium::Task *pTask = NULL;
|
---|
2773 |
|
---|
2774 | try
|
---|
2775 | {
|
---|
2776 | // locking: we need the tree lock first because we access parent pointers
|
---|
2777 | // and we need to write-lock the media involved
|
---|
2778 | uint32_t cHandles = 3;
|
---|
2779 | LockHandle* pHandles[4] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
2780 | this->lockHandle(),
|
---|
2781 | pTarget->lockHandle() };
|
---|
2782 | /* Only add parent to the lock if it is not null */
|
---|
2783 | if (!pParent.isNull())
|
---|
2784 | pHandles[cHandles++] = pParent->lockHandle();
|
---|
2785 | AutoWriteLock alock(cHandles,
|
---|
2786 | pHandles
|
---|
2787 | COMMA_LOCKVAL_SRC_POS);
|
---|
2788 |
|
---|
2789 | if ( pTarget->m->state != MediumState_NotCreated
|
---|
2790 | && pTarget->m->state != MediumState_Created)
|
---|
2791 | throw pTarget->i_setStateError();
|
---|
2792 |
|
---|
2793 | /* Build the source lock list. */
|
---|
2794 | MediumLockList *pSourceMediumLockList(new MediumLockList());
|
---|
2795 | alock.release();
|
---|
2796 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2797 | NULL /* pToLockWrite */,
|
---|
2798 | false /* fMediumLockWriteAll */,
|
---|
2799 | NULL,
|
---|
2800 | *pSourceMediumLockList);
|
---|
2801 | alock.acquire();
|
---|
2802 | if (FAILED(rc))
|
---|
2803 | {
|
---|
2804 | delete pSourceMediumLockList;
|
---|
2805 | throw rc;
|
---|
2806 | }
|
---|
2807 |
|
---|
2808 | /* Build the target lock list (including the to-be parent chain). */
|
---|
2809 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
2810 | alock.release();
|
---|
2811 | rc = pTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2812 | pTarget /* pToLockWrite */,
|
---|
2813 | false /* fMediumLockWriteAll */,
|
---|
2814 | pParent,
|
---|
2815 | *pTargetMediumLockList);
|
---|
2816 | alock.acquire();
|
---|
2817 | if (FAILED(rc))
|
---|
2818 | {
|
---|
2819 | delete pSourceMediumLockList;
|
---|
2820 | delete pTargetMediumLockList;
|
---|
2821 | throw rc;
|
---|
2822 | }
|
---|
2823 |
|
---|
2824 | alock.release();
|
---|
2825 | rc = pSourceMediumLockList->Lock();
|
---|
2826 | alock.acquire();
|
---|
2827 | if (FAILED(rc))
|
---|
2828 | {
|
---|
2829 | delete pSourceMediumLockList;
|
---|
2830 | delete pTargetMediumLockList;
|
---|
2831 | throw setError(rc,
|
---|
2832 | tr("Failed to lock source media '%s'"),
|
---|
2833 | i_getLocationFull().c_str());
|
---|
2834 | }
|
---|
2835 | alock.release();
|
---|
2836 | rc = pTargetMediumLockList->Lock();
|
---|
2837 | alock.acquire();
|
---|
2838 | if (FAILED(rc))
|
---|
2839 | {
|
---|
2840 | delete pSourceMediumLockList;
|
---|
2841 | delete pTargetMediumLockList;
|
---|
2842 | throw setError(rc,
|
---|
2843 | tr("Failed to lock target media '%s'"),
|
---|
2844 | pTarget->i_getLocationFull().c_str());
|
---|
2845 | }
|
---|
2846 |
|
---|
2847 | pProgress.createObject();
|
---|
2848 | rc = pProgress->init(m->pVirtualBox,
|
---|
2849 | static_cast <IMedium *>(this),
|
---|
2850 | BstrFmt(tr("Creating clone medium '%s'"), pTarget->m->strLocationFull.c_str()).raw(),
|
---|
2851 | TRUE /* aCancelable */);
|
---|
2852 | if (FAILED(rc))
|
---|
2853 | {
|
---|
2854 | delete pSourceMediumLockList;
|
---|
2855 | delete pTargetMediumLockList;
|
---|
2856 | throw rc;
|
---|
2857 | }
|
---|
2858 |
|
---|
2859 | ULONG mediumVariantFlags = 0;
|
---|
2860 |
|
---|
2861 | if (aVariant.size())
|
---|
2862 | {
|
---|
2863 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
2864 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
2865 | }
|
---|
2866 |
|
---|
2867 | if (mediumVariantFlags & MediumVariant_Formatted)
|
---|
2868 | {
|
---|
2869 | delete pSourceMediumLockList;
|
---|
2870 | delete pTargetMediumLockList;
|
---|
2871 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2872 | tr("Medium variant 'formatted' applies to floppy images only"));
|
---|
2873 | }
|
---|
2874 |
|
---|
2875 | /* setup task object to carry out the operation asynchronously */
|
---|
2876 | pTask = new Medium::CloneTask(this, pProgress, pTarget,
|
---|
2877 | (MediumVariant_T)mediumVariantFlags,
|
---|
2878 | pParent, UINT32_MAX, UINT32_MAX,
|
---|
2879 | pSourceMediumLockList, pTargetMediumLockList);
|
---|
2880 | rc = pTask->rc();
|
---|
2881 | AssertComRC(rc);
|
---|
2882 | if (FAILED(rc))
|
---|
2883 | throw rc;
|
---|
2884 |
|
---|
2885 | if (pTarget->m->state == MediumState_NotCreated)
|
---|
2886 | pTarget->m->state = MediumState_Creating;
|
---|
2887 | }
|
---|
2888 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2889 |
|
---|
2890 | if (SUCCEEDED(rc))
|
---|
2891 | {
|
---|
2892 | rc = pTask->createThread();
|
---|
2893 |
|
---|
2894 | if (SUCCEEDED(rc))
|
---|
2895 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2896 | }
|
---|
2897 | else if (pTask != NULL)
|
---|
2898 | delete pTask;
|
---|
2899 |
|
---|
2900 | return rc;
|
---|
2901 | }
|
---|
2902 |
|
---|
2903 | HRESULT Medium::moveTo(AutoCaller &autoCaller, const com::Utf8Str &aLocation, ComPtr<IProgress> &aProgress)
|
---|
2904 | {
|
---|
2905 | ComObjPtr<Medium> pParent;
|
---|
2906 | ComObjPtr<Progress> pProgress;
|
---|
2907 | HRESULT rc = S_OK;
|
---|
2908 | Medium::Task *pTask = NULL;
|
---|
2909 |
|
---|
2910 | try
|
---|
2911 | {
|
---|
2912 | /// @todo NEWMEDIA for file names, add the default extension if no extension
|
---|
2913 | /// is present (using the information from the VD backend which also implies
|
---|
2914 | /// that one more parameter should be passed to moveTo() requesting
|
---|
2915 | /// that functionality since it is only allowed when called from this method
|
---|
2916 |
|
---|
2917 | /// @todo NEWMEDIA rename the file and set m->location on success, then save
|
---|
2918 | /// the global registry (and local registries of portable VMs referring to
|
---|
2919 | /// this medium), this will also require to add the mRegistered flag to data
|
---|
2920 |
|
---|
2921 | autoCaller.release();
|
---|
2922 |
|
---|
2923 | // locking: we need the tree lock first because we access parent pointers
|
---|
2924 | // and we need to write-lock the media involved
|
---|
2925 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
2926 |
|
---|
2927 | autoCaller.add();
|
---|
2928 | AssertComRCThrowRC(autoCaller.rc());
|
---|
2929 |
|
---|
2930 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2931 |
|
---|
2932 | /* play with locations */
|
---|
2933 | {
|
---|
2934 | /* get source path and filename */
|
---|
2935 | Utf8Str sourcePath = i_getLocationFull();
|
---|
2936 | Utf8Str sourceFName = i_getName();
|
---|
2937 |
|
---|
2938 | if (aLocation.isEmpty())
|
---|
2939 | {
|
---|
2940 | rc = setError(VERR_PATH_ZERO_LENGTH,
|
---|
2941 | tr("Medium '%s' can't be moved. Destination path is empty."),
|
---|
2942 | i_getLocationFull().c_str());
|
---|
2943 | throw rc;
|
---|
2944 | }
|
---|
2945 |
|
---|
2946 | /* extract destination path and filename */
|
---|
2947 | Utf8Str destPath(aLocation);
|
---|
2948 | Utf8Str destFName(destPath);
|
---|
2949 | destFName.stripPath();
|
---|
2950 |
|
---|
2951 | Utf8Str suffix(destFName);
|
---|
2952 | suffix.stripSuffix();
|
---|
2953 |
|
---|
2954 | if (suffix.equals(destFName) && !destFName.isEmpty())
|
---|
2955 | {
|
---|
2956 | /*
|
---|
2957 | * The target path has no filename: Either "/path/to/new/location" or
|
---|
2958 | * just "newname" (no trailing backslash or there is no filename with
|
---|
2959 | * extension(suffix)).
|
---|
2960 | */
|
---|
2961 | if (destPath.equals(destFName))
|
---|
2962 | {
|
---|
2963 | /* new path contains only "newname", no path, no extension */
|
---|
2964 | destFName.append(RTPathSuffix(sourceFName.c_str()));
|
---|
2965 | destPath = destFName;
|
---|
2966 | }
|
---|
2967 | else
|
---|
2968 | {
|
---|
2969 | /* new path looks like "/path/to/new/location" */
|
---|
2970 | destFName.setNull();
|
---|
2971 | destPath.append(RTPATH_SLASH);
|
---|
2972 | }
|
---|
2973 | }
|
---|
2974 |
|
---|
2975 | if (destFName.isEmpty())
|
---|
2976 | {
|
---|
2977 | /* No target name */
|
---|
2978 | destPath.append(sourceFName);
|
---|
2979 | }
|
---|
2980 | else
|
---|
2981 | {
|
---|
2982 | if (destPath.equals(destFName))
|
---|
2983 | {
|
---|
2984 | /*
|
---|
2985 | * The target path contains of only a filename without a directory.
|
---|
2986 | * Move the medium within the source directory to the new name
|
---|
2987 | * (actually rename operation).
|
---|
2988 | * Scratches sourcePath!
|
---|
2989 | */
|
---|
2990 | destPath = sourcePath.stripFilename().append(RTPATH_SLASH).append(destFName);
|
---|
2991 | }
|
---|
2992 | suffix = i_getFormat();
|
---|
2993 | if (suffix.compare("RAW", Utf8Str::CaseInsensitive) == 0)
|
---|
2994 | {
|
---|
2995 | DeviceType_T devType = i_getDeviceType();
|
---|
2996 | switch (devType)
|
---|
2997 | {
|
---|
2998 | case DeviceType_DVD:
|
---|
2999 | suffix = "iso";
|
---|
3000 | break;
|
---|
3001 | case DeviceType_Floppy:
|
---|
3002 | suffix = "img";
|
---|
3003 | break;
|
---|
3004 | default:
|
---|
3005 | rc = setError(VERR_NOT_A_FILE,
|
---|
3006 | tr("Medium '%s' has RAW type. \"Move\" operation isn't supported for this type."),
|
---|
3007 | i_getLocationFull().c_str());
|
---|
3008 | throw rc;
|
---|
3009 | }
|
---|
3010 | }
|
---|
3011 | else if (suffix.compare("Parallels", Utf8Str::CaseInsensitive) == 0)
|
---|
3012 | {
|
---|
3013 | suffix = "hdd";
|
---|
3014 | }
|
---|
3015 |
|
---|
3016 | /* Set the target extension like on the source. Any conversions are prohibited */
|
---|
3017 | suffix.toLower();
|
---|
3018 | destPath.stripSuffix().append('.').append(suffix);
|
---|
3019 | }
|
---|
3020 |
|
---|
3021 | /* Simple check for existence */
|
---|
3022 | if (RTFileExists(destPath.c_str()))
|
---|
3023 | {
|
---|
3024 | rc = setError(VBOX_E_FILE_ERROR,
|
---|
3025 | tr("The given path '%s' is an existing file. Delete or rename this file."),
|
---|
3026 | destPath.c_str());
|
---|
3027 | throw rc;
|
---|
3028 | }
|
---|
3029 |
|
---|
3030 | if (!i_isMediumFormatFile())
|
---|
3031 | {
|
---|
3032 | rc = setError(VERR_NOT_A_FILE,
|
---|
3033 | tr("Medium '%s' isn't a file object. \"Move\" operation isn't supported."),
|
---|
3034 | i_getLocationFull().c_str());
|
---|
3035 | throw rc;
|
---|
3036 | }
|
---|
3037 | /* Path must be absolute */
|
---|
3038 | if (!RTPathStartsWithRoot(destPath.c_str()))
|
---|
3039 | {
|
---|
3040 | rc = setError(VBOX_E_FILE_ERROR,
|
---|
3041 | tr("The given path '%s' is not fully qualified"),
|
---|
3042 | destPath.c_str());
|
---|
3043 | throw rc;
|
---|
3044 | }
|
---|
3045 | /* Check path for a new file object */
|
---|
3046 | rc = VirtualBox::i_ensureFilePathExists(destPath, true);
|
---|
3047 | if (FAILED(rc))
|
---|
3048 | throw rc;
|
---|
3049 |
|
---|
3050 | /* Set needed variables for "moving" procedure. It'll be used later in separate thread task */
|
---|
3051 | rc = i_preparationForMoving(destPath);
|
---|
3052 | if (FAILED(rc))
|
---|
3053 | {
|
---|
3054 | rc = setError(VERR_NO_CHANGE,
|
---|
3055 | tr("Medium '%s' is already in the correct location"),
|
---|
3056 | i_getLocationFull().c_str());
|
---|
3057 | throw rc;
|
---|
3058 | }
|
---|
3059 | }
|
---|
3060 |
|
---|
3061 | /* Check VMs which have this medium attached to*/
|
---|
3062 | std::vector<com::Guid> aMachineIds;
|
---|
3063 | rc = getMachineIds(aMachineIds);
|
---|
3064 | std::vector<com::Guid>::const_iterator currMachineID = aMachineIds.begin();
|
---|
3065 | std::vector<com::Guid>::const_iterator lastMachineID = aMachineIds.end();
|
---|
3066 |
|
---|
3067 | while (currMachineID != lastMachineID)
|
---|
3068 | {
|
---|
3069 | Guid id(*currMachineID);
|
---|
3070 | ComObjPtr<Machine> aMachine;
|
---|
3071 |
|
---|
3072 | alock.release();
|
---|
3073 | autoCaller.release();
|
---|
3074 | treeLock.release();
|
---|
3075 | rc = m->pVirtualBox->i_findMachine(id, false, true, &aMachine);
|
---|
3076 | treeLock.acquire();
|
---|
3077 | autoCaller.add();
|
---|
3078 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3079 | alock.acquire();
|
---|
3080 |
|
---|
3081 | if (SUCCEEDED(rc))
|
---|
3082 | {
|
---|
3083 | ComObjPtr<SessionMachine> sm;
|
---|
3084 | ComPtr<IInternalSessionControl> ctl;
|
---|
3085 |
|
---|
3086 | alock.release();
|
---|
3087 | autoCaller.release();
|
---|
3088 | treeLock.release();
|
---|
3089 | bool ses = aMachine->i_isSessionOpenVM(sm, &ctl);
|
---|
3090 | treeLock.acquire();
|
---|
3091 | autoCaller.add();
|
---|
3092 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3093 | alock.acquire();
|
---|
3094 |
|
---|
3095 | if (ses)
|
---|
3096 | {
|
---|
3097 | rc = setError(VERR_VM_UNEXPECTED_VM_STATE,
|
---|
3098 | tr("At least the VM '%s' to whom this medium '%s' attached has currently an opened session. Stop all VMs before relocating this medium"),
|
---|
3099 | id.toString().c_str(),
|
---|
3100 | i_getLocationFull().c_str());
|
---|
3101 | throw rc;
|
---|
3102 | }
|
---|
3103 | }
|
---|
3104 | ++currMachineID;
|
---|
3105 | }
|
---|
3106 |
|
---|
3107 | /* Build the source lock list. */
|
---|
3108 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3109 | alock.release();
|
---|
3110 | autoCaller.release();
|
---|
3111 | treeLock.release();
|
---|
3112 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
3113 | this /* pToLockWrite */,
|
---|
3114 | true /* fMediumLockWriteAll */,
|
---|
3115 | NULL,
|
---|
3116 | *pMediumLockList);
|
---|
3117 | treeLock.acquire();
|
---|
3118 | autoCaller.add();
|
---|
3119 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3120 | alock.acquire();
|
---|
3121 | if (FAILED(rc))
|
---|
3122 | {
|
---|
3123 | delete pMediumLockList;
|
---|
3124 | throw setError(rc,
|
---|
3125 | tr("Failed to create medium lock list for '%s'"),
|
---|
3126 | i_getLocationFull().c_str());
|
---|
3127 | }
|
---|
3128 | alock.release();
|
---|
3129 | autoCaller.release();
|
---|
3130 | treeLock.release();
|
---|
3131 | rc = pMediumLockList->Lock();
|
---|
3132 | treeLock.acquire();
|
---|
3133 | autoCaller.add();
|
---|
3134 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3135 | alock.acquire();
|
---|
3136 | if (FAILED(rc))
|
---|
3137 | {
|
---|
3138 | delete pMediumLockList;
|
---|
3139 | throw setError(rc,
|
---|
3140 | tr("Failed to lock media '%s'"),
|
---|
3141 | i_getLocationFull().c_str());
|
---|
3142 | }
|
---|
3143 |
|
---|
3144 | pProgress.createObject();
|
---|
3145 | rc = pProgress->init(m->pVirtualBox,
|
---|
3146 | static_cast <IMedium *>(this),
|
---|
3147 | BstrFmt(tr("Moving medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3148 | TRUE /* aCancelable */);
|
---|
3149 |
|
---|
3150 | /* Do the disk moving. */
|
---|
3151 | if (SUCCEEDED(rc))
|
---|
3152 | {
|
---|
3153 | ULONG mediumVariantFlags = i_getVariant();
|
---|
3154 |
|
---|
3155 | /* setup task object to carry out the operation asynchronously */
|
---|
3156 | pTask = new Medium::MoveTask(this, pProgress,
|
---|
3157 | (MediumVariant_T)mediumVariantFlags,
|
---|
3158 | pMediumLockList);
|
---|
3159 | rc = pTask->rc();
|
---|
3160 | AssertComRC(rc);
|
---|
3161 | if (FAILED(rc))
|
---|
3162 | throw rc;
|
---|
3163 | }
|
---|
3164 |
|
---|
3165 | }
|
---|
3166 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3167 |
|
---|
3168 | if (SUCCEEDED(rc))
|
---|
3169 | {
|
---|
3170 | rc = pTask->createThread();
|
---|
3171 |
|
---|
3172 | if (SUCCEEDED(rc))
|
---|
3173 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3174 | }
|
---|
3175 | else
|
---|
3176 | {
|
---|
3177 | if (pTask)
|
---|
3178 | delete pTask;
|
---|
3179 | }
|
---|
3180 |
|
---|
3181 | return rc;
|
---|
3182 | }
|
---|
3183 |
|
---|
3184 | HRESULT Medium::setLocation(const com::Utf8Str &aLocation)
|
---|
3185 | {
|
---|
3186 | HRESULT rc = S_OK;
|
---|
3187 |
|
---|
3188 | try
|
---|
3189 | {
|
---|
3190 | // locking: we need the tree lock first because we access parent pointers
|
---|
3191 | // and we need to write-lock the media involved
|
---|
3192 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3193 |
|
---|
3194 | AutoCaller autoCaller(this);
|
---|
3195 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3196 |
|
---|
3197 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3198 |
|
---|
3199 | Utf8Str destPath(aLocation);
|
---|
3200 |
|
---|
3201 | // some check for file based medium
|
---|
3202 | if (i_isMediumFormatFile())
|
---|
3203 | {
|
---|
3204 | /* Path must be absolute */
|
---|
3205 | if (!RTPathStartsWithRoot(destPath.c_str()))
|
---|
3206 | {
|
---|
3207 | rc = setError(VBOX_E_FILE_ERROR,
|
---|
3208 | tr("The given path '%s' is not fully qualified"),
|
---|
3209 | destPath.c_str());
|
---|
3210 | throw rc;
|
---|
3211 | }
|
---|
3212 |
|
---|
3213 | /* Simple check for existence */
|
---|
3214 | if (!RTFileExists(destPath.c_str()))
|
---|
3215 | {
|
---|
3216 | rc = setError(VBOX_E_FILE_ERROR,
|
---|
3217 | tr("The given path '%s' is not an existing file. New location is invalid."),
|
---|
3218 | destPath.c_str());
|
---|
3219 | throw rc;
|
---|
3220 | }
|
---|
3221 | }
|
---|
3222 |
|
---|
3223 | /* Check VMs which have this medium attached to*/
|
---|
3224 | std::vector<com::Guid> aMachineIds;
|
---|
3225 | rc = getMachineIds(aMachineIds);
|
---|
3226 |
|
---|
3227 | // switch locks only if there are machines with this medium attached
|
---|
3228 | if (!aMachineIds.empty())
|
---|
3229 | {
|
---|
3230 | std::vector<com::Guid>::const_iterator currMachineID = aMachineIds.begin();
|
---|
3231 | std::vector<com::Guid>::const_iterator lastMachineID = aMachineIds.end();
|
---|
3232 |
|
---|
3233 | alock.release();
|
---|
3234 | autoCaller.release();
|
---|
3235 | treeLock.release();
|
---|
3236 |
|
---|
3237 | while (currMachineID != lastMachineID)
|
---|
3238 | {
|
---|
3239 | Guid id(*currMachineID);
|
---|
3240 | ComObjPtr<Machine> aMachine;
|
---|
3241 | rc = m->pVirtualBox->i_findMachine(id, false, true, &aMachine);
|
---|
3242 | if (SUCCEEDED(rc))
|
---|
3243 | {
|
---|
3244 | ComObjPtr<SessionMachine> sm;
|
---|
3245 | ComPtr<IInternalSessionControl> ctl;
|
---|
3246 |
|
---|
3247 | bool ses = aMachine->i_isSessionOpenVM(sm, &ctl);
|
---|
3248 | if (ses)
|
---|
3249 | {
|
---|
3250 | treeLock.acquire();
|
---|
3251 | autoCaller.add();
|
---|
3252 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3253 | alock.acquire();
|
---|
3254 |
|
---|
3255 | rc = setError(VERR_VM_UNEXPECTED_VM_STATE,
|
---|
3256 | 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"),
|
---|
3257 | id.toString().c_str(),
|
---|
3258 | i_getLocationFull().c_str());
|
---|
3259 | throw rc;
|
---|
3260 | }
|
---|
3261 | }
|
---|
3262 | ++currMachineID;
|
---|
3263 | }
|
---|
3264 |
|
---|
3265 | treeLock.acquire();
|
---|
3266 | autoCaller.add();
|
---|
3267 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3268 | alock.acquire();
|
---|
3269 | }
|
---|
3270 |
|
---|
3271 | m->strLocationFull = destPath;
|
---|
3272 |
|
---|
3273 | // save the settings
|
---|
3274 | alock.release();
|
---|
3275 | autoCaller.release();
|
---|
3276 | treeLock.release();
|
---|
3277 |
|
---|
3278 | i_markRegistriesModified();
|
---|
3279 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
3280 |
|
---|
3281 | MediumState_T mediumState;
|
---|
3282 | refreshState(autoCaller, &mediumState);
|
---|
3283 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
3284 | }
|
---|
3285 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3286 |
|
---|
3287 | return rc;
|
---|
3288 | }
|
---|
3289 |
|
---|
3290 | HRESULT Medium::compact(ComPtr<IProgress> &aProgress)
|
---|
3291 | {
|
---|
3292 | HRESULT rc = S_OK;
|
---|
3293 | ComObjPtr<Progress> pProgress;
|
---|
3294 | Medium::Task *pTask = NULL;
|
---|
3295 |
|
---|
3296 | try
|
---|
3297 | {
|
---|
3298 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3299 |
|
---|
3300 | /* Build the medium lock list. */
|
---|
3301 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3302 | alock.release();
|
---|
3303 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
3304 | this /* pToLockWrite */,
|
---|
3305 | false /* fMediumLockWriteAll */,
|
---|
3306 | NULL,
|
---|
3307 | *pMediumLockList);
|
---|
3308 | alock.acquire();
|
---|
3309 | if (FAILED(rc))
|
---|
3310 | {
|
---|
3311 | delete pMediumLockList;
|
---|
3312 | throw rc;
|
---|
3313 | }
|
---|
3314 |
|
---|
3315 | alock.release();
|
---|
3316 | rc = pMediumLockList->Lock();
|
---|
3317 | alock.acquire();
|
---|
3318 | if (FAILED(rc))
|
---|
3319 | {
|
---|
3320 | delete pMediumLockList;
|
---|
3321 | throw setError(rc,
|
---|
3322 | tr("Failed to lock media when compacting '%s'"),
|
---|
3323 | i_getLocationFull().c_str());
|
---|
3324 | }
|
---|
3325 |
|
---|
3326 | pProgress.createObject();
|
---|
3327 | rc = pProgress->init(m->pVirtualBox,
|
---|
3328 | static_cast <IMedium *>(this),
|
---|
3329 | BstrFmt(tr("Compacting medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3330 | TRUE /* aCancelable */);
|
---|
3331 | if (FAILED(rc))
|
---|
3332 | {
|
---|
3333 | delete pMediumLockList;
|
---|
3334 | throw rc;
|
---|
3335 | }
|
---|
3336 |
|
---|
3337 | /* setup task object to carry out the operation asynchronously */
|
---|
3338 | pTask = new Medium::CompactTask(this, pProgress, pMediumLockList);
|
---|
3339 | rc = pTask->rc();
|
---|
3340 | AssertComRC(rc);
|
---|
3341 | if (FAILED(rc))
|
---|
3342 | throw rc;
|
---|
3343 | }
|
---|
3344 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3345 |
|
---|
3346 | if (SUCCEEDED(rc))
|
---|
3347 | {
|
---|
3348 | rc = pTask->createThread();
|
---|
3349 |
|
---|
3350 | if (SUCCEEDED(rc))
|
---|
3351 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3352 | }
|
---|
3353 | else if (pTask != NULL)
|
---|
3354 | delete pTask;
|
---|
3355 |
|
---|
3356 | return rc;
|
---|
3357 | }
|
---|
3358 |
|
---|
3359 | HRESULT Medium::resize(LONG64 aLogicalSize,
|
---|
3360 | ComPtr<IProgress> &aProgress)
|
---|
3361 | {
|
---|
3362 | HRESULT rc = S_OK;
|
---|
3363 | ComObjPtr<Progress> pProgress;
|
---|
3364 |
|
---|
3365 | /* Build the medium lock list. */
|
---|
3366 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3367 |
|
---|
3368 | try
|
---|
3369 | {
|
---|
3370 | const char *pszError = NULL;
|
---|
3371 |
|
---|
3372 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
3373 | this /* pToLockWrite */,
|
---|
3374 | false /* fMediumLockWriteAll */,
|
---|
3375 | NULL,
|
---|
3376 | *pMediumLockList);
|
---|
3377 | if (FAILED(rc))
|
---|
3378 | {
|
---|
3379 | pszError = tr("Failed to create medium lock list when resize '%s'");
|
---|
3380 | }
|
---|
3381 | else
|
---|
3382 | {
|
---|
3383 | rc = pMediumLockList->Lock();
|
---|
3384 | if (FAILED(rc))
|
---|
3385 | pszError = tr("Failed to lock media when compacting '%s'");
|
---|
3386 | }
|
---|
3387 |
|
---|
3388 |
|
---|
3389 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3390 |
|
---|
3391 | if (FAILED(rc))
|
---|
3392 | {
|
---|
3393 | delete pMediumLockList;
|
---|
3394 | throw setError(rc, pszError, i_getLocationFull().c_str());
|
---|
3395 | }
|
---|
3396 |
|
---|
3397 | pProgress.createObject();
|
---|
3398 | rc = pProgress->init(m->pVirtualBox,
|
---|
3399 | static_cast <IMedium *>(this),
|
---|
3400 | BstrFmt(tr("Resizing medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3401 | TRUE /* aCancelable */);
|
---|
3402 | if (FAILED(rc))
|
---|
3403 | {
|
---|
3404 | delete pMediumLockList;
|
---|
3405 | throw rc;
|
---|
3406 | }
|
---|
3407 | }
|
---|
3408 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3409 |
|
---|
3410 | if (SUCCEEDED(rc))
|
---|
3411 | rc = i_resize(aLogicalSize, pMediumLockList, &pProgress, false /* aWait */, true /* aNotify */);
|
---|
3412 |
|
---|
3413 | if (SUCCEEDED(rc))
|
---|
3414 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3415 | else
|
---|
3416 | delete pMediumLockList;
|
---|
3417 |
|
---|
3418 | return rc;
|
---|
3419 | }
|
---|
3420 |
|
---|
3421 | HRESULT Medium::reset(AutoCaller &autoCaller, ComPtr<IProgress> &aProgress)
|
---|
3422 | {
|
---|
3423 | HRESULT rc = S_OK;
|
---|
3424 | ComObjPtr<Progress> pProgress;
|
---|
3425 | Medium::Task *pTask = NULL;
|
---|
3426 |
|
---|
3427 | try
|
---|
3428 | {
|
---|
3429 | autoCaller.release();
|
---|
3430 |
|
---|
3431 | /* It is possible that some previous/concurrent uninit has already
|
---|
3432 | * cleared the pVirtualBox reference, see #uninit(). */
|
---|
3433 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
3434 |
|
---|
3435 | /* canClose() needs the tree lock */
|
---|
3436 | AutoMultiWriteLock2 multilock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL,
|
---|
3437 | this->lockHandle()
|
---|
3438 | COMMA_LOCKVAL_SRC_POS);
|
---|
3439 |
|
---|
3440 | autoCaller.add();
|
---|
3441 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
3442 |
|
---|
3443 | LogFlowThisFunc(("ENTER for medium %s\n", m->strLocationFull.c_str()));
|
---|
3444 |
|
---|
3445 | if (m->pParent.isNull())
|
---|
3446 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3447 | tr("Medium type of '%s' is not differencing"),
|
---|
3448 | m->strLocationFull.c_str());
|
---|
3449 |
|
---|
3450 | rc = i_canClose();
|
---|
3451 | if (FAILED(rc))
|
---|
3452 | throw rc;
|
---|
3453 |
|
---|
3454 | /* Build the medium lock list. */
|
---|
3455 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3456 | multilock.release();
|
---|
3457 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
3458 | this /* pToLockWrite */,
|
---|
3459 | false /* fMediumLockWriteAll */,
|
---|
3460 | NULL,
|
---|
3461 | *pMediumLockList);
|
---|
3462 | multilock.acquire();
|
---|
3463 | if (FAILED(rc))
|
---|
3464 | {
|
---|
3465 | delete pMediumLockList;
|
---|
3466 | throw rc;
|
---|
3467 | }
|
---|
3468 |
|
---|
3469 | multilock.release();
|
---|
3470 | rc = pMediumLockList->Lock();
|
---|
3471 | multilock.acquire();
|
---|
3472 | if (FAILED(rc))
|
---|
3473 | {
|
---|
3474 | delete pMediumLockList;
|
---|
3475 | throw setError(rc,
|
---|
3476 | tr("Failed to lock media when resetting '%s'"),
|
---|
3477 | i_getLocationFull().c_str());
|
---|
3478 | }
|
---|
3479 |
|
---|
3480 | pProgress.createObject();
|
---|
3481 | rc = pProgress->init(m->pVirtualBox,
|
---|
3482 | static_cast<IMedium*>(this),
|
---|
3483 | BstrFmt(tr("Resetting differencing medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3484 | FALSE /* aCancelable */);
|
---|
3485 | if (FAILED(rc))
|
---|
3486 | throw rc;
|
---|
3487 |
|
---|
3488 | /* setup task object to carry out the operation asynchronously */
|
---|
3489 | pTask = new Medium::ResetTask(this, pProgress, pMediumLockList);
|
---|
3490 | rc = pTask->rc();
|
---|
3491 | AssertComRC(rc);
|
---|
3492 | if (FAILED(rc))
|
---|
3493 | throw rc;
|
---|
3494 | }
|
---|
3495 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3496 |
|
---|
3497 | if (SUCCEEDED(rc))
|
---|
3498 | {
|
---|
3499 | rc = pTask->createThread();
|
---|
3500 |
|
---|
3501 | if (SUCCEEDED(rc))
|
---|
3502 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3503 | }
|
---|
3504 | else if (pTask != NULL)
|
---|
3505 | delete pTask;
|
---|
3506 |
|
---|
3507 | LogFlowThisFunc(("LEAVE, rc=%Rhrc\n", rc));
|
---|
3508 |
|
---|
3509 | return rc;
|
---|
3510 | }
|
---|
3511 |
|
---|
3512 | HRESULT Medium::changeEncryption(const com::Utf8Str &aCurrentPassword, const com::Utf8Str &aCipher,
|
---|
3513 | const com::Utf8Str &aNewPassword, const com::Utf8Str &aNewPasswordId,
|
---|
3514 | ComPtr<IProgress> &aProgress)
|
---|
3515 | {
|
---|
3516 | HRESULT rc = S_OK;
|
---|
3517 | ComObjPtr<Progress> pProgress;
|
---|
3518 | Medium::Task *pTask = NULL;
|
---|
3519 |
|
---|
3520 | try
|
---|
3521 | {
|
---|
3522 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3523 |
|
---|
3524 | DeviceType_T devType = i_getDeviceType();
|
---|
3525 | /* Cannot encrypt DVD or floppy images so far. */
|
---|
3526 | if ( devType == DeviceType_DVD
|
---|
3527 | || devType == DeviceType_Floppy)
|
---|
3528 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3529 | tr("Cannot encrypt DVD or Floppy medium '%s'"),
|
---|
3530 | m->strLocationFull.c_str());
|
---|
3531 |
|
---|
3532 | /* Cannot encrypt media which are attached to more than one virtual machine. */
|
---|
3533 | if (m->backRefs.size() > 1)
|
---|
3534 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3535 | tr("Cannot encrypt medium '%s' because it is attached to %d virtual machines"),
|
---|
3536 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
3537 |
|
---|
3538 | if (i_getChildren().size() != 0)
|
---|
3539 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3540 | tr("Cannot encrypt medium '%s' because it has %d children"),
|
---|
3541 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
3542 |
|
---|
3543 | /* Build the medium lock list. */
|
---|
3544 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3545 | alock.release();
|
---|
3546 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
3547 | this /* pToLockWrite */,
|
---|
3548 | true /* fMediumLockAllWrite */,
|
---|
3549 | NULL,
|
---|
3550 | *pMediumLockList);
|
---|
3551 | alock.acquire();
|
---|
3552 | if (FAILED(rc))
|
---|
3553 | {
|
---|
3554 | delete pMediumLockList;
|
---|
3555 | throw rc;
|
---|
3556 | }
|
---|
3557 |
|
---|
3558 | alock.release();
|
---|
3559 | rc = pMediumLockList->Lock();
|
---|
3560 | alock.acquire();
|
---|
3561 | if (FAILED(rc))
|
---|
3562 | {
|
---|
3563 | delete pMediumLockList;
|
---|
3564 | throw setError(rc,
|
---|
3565 | tr("Failed to lock media for encryption '%s'"),
|
---|
3566 | i_getLocationFull().c_str());
|
---|
3567 | }
|
---|
3568 |
|
---|
3569 | /*
|
---|
3570 | * Check all media in the chain to not contain any branches or references to
|
---|
3571 | * other virtual machines, we support encrypting only a list of differencing media at the moment.
|
---|
3572 | */
|
---|
3573 | MediumLockList::Base::const_iterator mediumListBegin = pMediumLockList->GetBegin();
|
---|
3574 | MediumLockList::Base::const_iterator mediumListEnd = pMediumLockList->GetEnd();
|
---|
3575 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
3576 | it != mediumListEnd;
|
---|
3577 | ++it)
|
---|
3578 | {
|
---|
3579 | const MediumLock &mediumLock = *it;
|
---|
3580 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
3581 | AutoReadLock mediumReadLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
3582 |
|
---|
3583 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
3584 |
|
---|
3585 | if (pMedium->m->backRefs.size() > 1)
|
---|
3586 | {
|
---|
3587 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3588 | tr("Cannot encrypt medium '%s' because it is attached to %d virtual machines"),
|
---|
3589 | pMedium->m->strLocationFull.c_str(), pMedium->m->backRefs.size());
|
---|
3590 | break;
|
---|
3591 | }
|
---|
3592 | else if (pMedium->i_getChildren().size() > 1)
|
---|
3593 | {
|
---|
3594 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3595 | tr("Cannot encrypt medium '%s' because it has %d children"),
|
---|
3596 | pMedium->m->strLocationFull.c_str(), pMedium->i_getChildren().size());
|
---|
3597 | break;
|
---|
3598 | }
|
---|
3599 | }
|
---|
3600 |
|
---|
3601 | if (FAILED(rc))
|
---|
3602 | {
|
---|
3603 | delete pMediumLockList;
|
---|
3604 | throw rc;
|
---|
3605 | }
|
---|
3606 |
|
---|
3607 | const char *pszAction = "Encrypting";
|
---|
3608 | if ( aCurrentPassword.isNotEmpty()
|
---|
3609 | && aCipher.isEmpty())
|
---|
3610 | pszAction = "Decrypting";
|
---|
3611 |
|
---|
3612 | pProgress.createObject();
|
---|
3613 | rc = pProgress->init(m->pVirtualBox,
|
---|
3614 | static_cast <IMedium *>(this),
|
---|
3615 | BstrFmt(tr("%s medium '%s'"), pszAction, m->strLocationFull.c_str()).raw(),
|
---|
3616 | TRUE /* aCancelable */);
|
---|
3617 | if (FAILED(rc))
|
---|
3618 | {
|
---|
3619 | delete pMediumLockList;
|
---|
3620 | throw rc;
|
---|
3621 | }
|
---|
3622 |
|
---|
3623 | /* setup task object to carry out the operation asynchronously */
|
---|
3624 | pTask = new Medium::EncryptTask(this, aNewPassword, aCurrentPassword,
|
---|
3625 | aCipher, aNewPasswordId, pProgress, pMediumLockList);
|
---|
3626 | rc = pTask->rc();
|
---|
3627 | AssertComRC(rc);
|
---|
3628 | if (FAILED(rc))
|
---|
3629 | throw rc;
|
---|
3630 | }
|
---|
3631 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3632 |
|
---|
3633 | if (SUCCEEDED(rc))
|
---|
3634 | {
|
---|
3635 | rc = pTask->createThread();
|
---|
3636 |
|
---|
3637 | if (SUCCEEDED(rc))
|
---|
3638 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3639 | }
|
---|
3640 | else if (pTask != NULL)
|
---|
3641 | delete pTask;
|
---|
3642 |
|
---|
3643 | return rc;
|
---|
3644 | }
|
---|
3645 |
|
---|
3646 | HRESULT Medium::getEncryptionSettings(AutoCaller &autoCaller, com::Utf8Str &aCipher, com::Utf8Str &aPasswordId)
|
---|
3647 | {
|
---|
3648 | #ifndef VBOX_WITH_EXTPACK
|
---|
3649 | RT_NOREF(aCipher, aPasswordId);
|
---|
3650 | #endif
|
---|
3651 | HRESULT rc = S_OK;
|
---|
3652 |
|
---|
3653 | try
|
---|
3654 | {
|
---|
3655 | autoCaller.release();
|
---|
3656 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
3657 | autoCaller.add();
|
---|
3658 | if (FAILED(autoCaller.rc()))
|
---|
3659 | throw rc;
|
---|
3660 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3661 |
|
---|
3662 | /* Check whether encryption is configured for this medium. */
|
---|
3663 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
3664 | if (it == pBase->m->mapProperties.end())
|
---|
3665 | throw VBOX_E_NOT_SUPPORTED;
|
---|
3666 |
|
---|
3667 | # ifdef VBOX_WITH_EXTPACK
|
---|
3668 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
3669 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
3670 | {
|
---|
3671 | /* Load the plugin */
|
---|
3672 | Utf8Str strPlugin;
|
---|
3673 | rc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
3674 | if (SUCCEEDED(rc))
|
---|
3675 | {
|
---|
3676 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
3677 | if (RT_FAILURE(vrc))
|
---|
3678 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
3679 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
3680 | i_vdError(vrc).c_str());
|
---|
3681 | }
|
---|
3682 | else
|
---|
3683 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3684 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
3685 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
3686 | }
|
---|
3687 | else
|
---|
3688 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3689 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
3690 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
3691 |
|
---|
3692 | PVDISK pDisk = NULL;
|
---|
3693 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
3694 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
3695 |
|
---|
3696 | MediumCryptoFilterSettings CryptoSettings;
|
---|
3697 |
|
---|
3698 | i_taskEncryptSettingsSetup(&CryptoSettings, NULL, it->second.c_str(), NULL, false /* fCreateKeyStore */);
|
---|
3699 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ | VD_FILTER_FLAGS_INFO, CryptoSettings.vdFilterIfaces);
|
---|
3700 | if (RT_FAILURE(vrc))
|
---|
3701 | throw setErrorBoth(VBOX_E_INVALID_OBJECT_STATE, vrc,
|
---|
3702 | tr("Failed to load the encryption filter: %s"),
|
---|
3703 | i_vdError(vrc).c_str());
|
---|
3704 |
|
---|
3705 | it = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
3706 | if (it == pBase->m->mapProperties.end())
|
---|
3707 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3708 | tr("Image is configured for encryption but doesn't has a KeyId set"));
|
---|
3709 |
|
---|
3710 | aPasswordId = it->second.c_str();
|
---|
3711 | aCipher = CryptoSettings.pszCipherReturned;
|
---|
3712 | RTStrFree(CryptoSettings.pszCipherReturned);
|
---|
3713 |
|
---|
3714 | VDDestroy(pDisk);
|
---|
3715 | # else
|
---|
3716 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3717 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
3718 | # endif
|
---|
3719 | }
|
---|
3720 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3721 |
|
---|
3722 | return rc;
|
---|
3723 | }
|
---|
3724 |
|
---|
3725 | HRESULT Medium::checkEncryptionPassword(const com::Utf8Str &aPassword)
|
---|
3726 | {
|
---|
3727 | HRESULT rc = S_OK;
|
---|
3728 |
|
---|
3729 | try
|
---|
3730 | {
|
---|
3731 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
3732 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3733 |
|
---|
3734 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
3735 | if (it == pBase->m->mapProperties.end())
|
---|
3736 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3737 | tr("The image is not configured for encryption"));
|
---|
3738 |
|
---|
3739 | if (aPassword.isEmpty())
|
---|
3740 | throw setError(E_INVALIDARG,
|
---|
3741 | tr("The given password must not be empty"));
|
---|
3742 |
|
---|
3743 | # ifdef VBOX_WITH_EXTPACK
|
---|
3744 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
3745 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
3746 | {
|
---|
3747 | /* Load the plugin */
|
---|
3748 | Utf8Str strPlugin;
|
---|
3749 | rc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
3750 | if (SUCCEEDED(rc))
|
---|
3751 | {
|
---|
3752 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
3753 | if (RT_FAILURE(vrc))
|
---|
3754 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
3755 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
3756 | i_vdError(vrc).c_str());
|
---|
3757 | }
|
---|
3758 | else
|
---|
3759 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3760 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
3761 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
3762 | }
|
---|
3763 | else
|
---|
3764 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3765 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
3766 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
3767 |
|
---|
3768 | PVDISK pDisk = NULL;
|
---|
3769 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
3770 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
3771 |
|
---|
3772 | MediumCryptoFilterSettings CryptoSettings;
|
---|
3773 |
|
---|
3774 | i_taskEncryptSettingsSetup(&CryptoSettings, NULL, it->second.c_str(), aPassword.c_str(),
|
---|
3775 | false /* fCreateKeyStore */);
|
---|
3776 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ, CryptoSettings.vdFilterIfaces);
|
---|
3777 | if (vrc == VERR_VD_PASSWORD_INCORRECT)
|
---|
3778 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
3779 | tr("The given password is incorrect"));
|
---|
3780 | else if (RT_FAILURE(vrc))
|
---|
3781 | throw setErrorBoth(VBOX_E_INVALID_OBJECT_STATE, vrc,
|
---|
3782 | tr("Failed to load the encryption filter: %s"),
|
---|
3783 | i_vdError(vrc).c_str());
|
---|
3784 |
|
---|
3785 | VDDestroy(pDisk);
|
---|
3786 | # else
|
---|
3787 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3788 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
3789 | # endif
|
---|
3790 | }
|
---|
3791 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3792 |
|
---|
3793 | return rc;
|
---|
3794 | }
|
---|
3795 |
|
---|
3796 | HRESULT Medium::openForIO(BOOL aWritable, com::Utf8Str const &aPassword, ComPtr<IMediumIO> &aMediumIO)
|
---|
3797 | {
|
---|
3798 | /*
|
---|
3799 | * Input validation.
|
---|
3800 | */
|
---|
3801 | if (aWritable && i_isReadOnly())
|
---|
3802 | return setError(E_ACCESSDENIED, tr("Write access denied: read-only"));
|
---|
3803 |
|
---|
3804 | com::Utf8Str const strKeyId = i_getKeyId();
|
---|
3805 | if (strKeyId.isEmpty() && aPassword.isNotEmpty())
|
---|
3806 | return setError(E_INVALIDARG, tr("Password given for unencrypted medium"));
|
---|
3807 | if (strKeyId.isNotEmpty() && aPassword.isEmpty())
|
---|
3808 | return setError(E_INVALIDARG, tr("Password needed for encrypted medium"));
|
---|
3809 |
|
---|
3810 | /*
|
---|
3811 | * Create IO object and return it.
|
---|
3812 | */
|
---|
3813 | ComObjPtr<MediumIO> ptrIO;
|
---|
3814 | HRESULT hrc = ptrIO.createObject();
|
---|
3815 | if (SUCCEEDED(hrc))
|
---|
3816 | {
|
---|
3817 | hrc = ptrIO->initForMedium(this, m->pVirtualBox, aWritable != FALSE, strKeyId, aPassword);
|
---|
3818 | if (SUCCEEDED(hrc))
|
---|
3819 | ptrIO.queryInterfaceTo(aMediumIO.asOutParam());
|
---|
3820 | }
|
---|
3821 | return hrc;
|
---|
3822 | }
|
---|
3823 |
|
---|
3824 |
|
---|
3825 | ////////////////////////////////////////////////////////////////////////////////
|
---|
3826 | //
|
---|
3827 | // Medium public internal methods
|
---|
3828 | //
|
---|
3829 | ////////////////////////////////////////////////////////////////////////////////
|
---|
3830 |
|
---|
3831 | /**
|
---|
3832 | * Internal method to return the medium's parent medium. Must have caller + locking!
|
---|
3833 | * @return
|
---|
3834 | */
|
---|
3835 | const ComObjPtr<Medium>& Medium::i_getParent() const
|
---|
3836 | {
|
---|
3837 | return m->pParent;
|
---|
3838 | }
|
---|
3839 |
|
---|
3840 | /**
|
---|
3841 | * Internal method to return the medium's list of child media. Must have caller + locking!
|
---|
3842 | * @return
|
---|
3843 | */
|
---|
3844 | const MediaList& Medium::i_getChildren() const
|
---|
3845 | {
|
---|
3846 | return m->llChildren;
|
---|
3847 | }
|
---|
3848 |
|
---|
3849 | /**
|
---|
3850 | * Internal method to return the medium's GUID. Must have caller + locking!
|
---|
3851 | * @return
|
---|
3852 | */
|
---|
3853 | const Guid& Medium::i_getId() const
|
---|
3854 | {
|
---|
3855 | return m->id;
|
---|
3856 | }
|
---|
3857 |
|
---|
3858 | /**
|
---|
3859 | * Internal method to return the medium's state. Must have caller + locking!
|
---|
3860 | * @return
|
---|
3861 | */
|
---|
3862 | MediumState_T Medium::i_getState() const
|
---|
3863 | {
|
---|
3864 | return m->state;
|
---|
3865 | }
|
---|
3866 |
|
---|
3867 | /**
|
---|
3868 | * Internal method to return the medium's variant. Must have caller + locking!
|
---|
3869 | * @return
|
---|
3870 | */
|
---|
3871 | MediumVariant_T Medium::i_getVariant() const
|
---|
3872 | {
|
---|
3873 | return m->variant;
|
---|
3874 | }
|
---|
3875 |
|
---|
3876 | /**
|
---|
3877 | * Internal method which returns true if this medium represents a host drive.
|
---|
3878 | * @return
|
---|
3879 | */
|
---|
3880 | bool Medium::i_isHostDrive() const
|
---|
3881 | {
|
---|
3882 | return m->hostDrive;
|
---|
3883 | }
|
---|
3884 |
|
---|
3885 | /**
|
---|
3886 | * Internal method to return the medium's full location. Must have caller + locking!
|
---|
3887 | * @return
|
---|
3888 | */
|
---|
3889 | const Utf8Str& Medium::i_getLocationFull() const
|
---|
3890 | {
|
---|
3891 | return m->strLocationFull;
|
---|
3892 | }
|
---|
3893 |
|
---|
3894 | /**
|
---|
3895 | * Internal method to return the medium's format string. Must have caller + locking!
|
---|
3896 | * @return
|
---|
3897 | */
|
---|
3898 | const Utf8Str& Medium::i_getFormat() const
|
---|
3899 | {
|
---|
3900 | return m->strFormat;
|
---|
3901 | }
|
---|
3902 |
|
---|
3903 | /**
|
---|
3904 | * Internal method to return the medium's format object. Must have caller + locking!
|
---|
3905 | * @return
|
---|
3906 | */
|
---|
3907 | const ComObjPtr<MediumFormat>& Medium::i_getMediumFormat() const
|
---|
3908 | {
|
---|
3909 | return m->formatObj;
|
---|
3910 | }
|
---|
3911 |
|
---|
3912 | /**
|
---|
3913 | * Internal method that returns true if the medium is represented by a file on the host disk
|
---|
3914 | * (and not iSCSI or something).
|
---|
3915 | * @return
|
---|
3916 | */
|
---|
3917 | bool Medium::i_isMediumFormatFile() const
|
---|
3918 | {
|
---|
3919 | if ( m->formatObj
|
---|
3920 | && (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
3921 | )
|
---|
3922 | return true;
|
---|
3923 | return false;
|
---|
3924 | }
|
---|
3925 |
|
---|
3926 | /**
|
---|
3927 | * Internal method to return the medium's size. Must have caller + locking!
|
---|
3928 | * @return
|
---|
3929 | */
|
---|
3930 | uint64_t Medium::i_getSize() const
|
---|
3931 | {
|
---|
3932 | return m->size;
|
---|
3933 | }
|
---|
3934 |
|
---|
3935 | /**
|
---|
3936 | * Internal method to return the medium's size. Must have caller + locking!
|
---|
3937 | * @return
|
---|
3938 | */
|
---|
3939 | uint64_t Medium::i_getLogicalSize() const
|
---|
3940 | {
|
---|
3941 | return m->logicalSize;
|
---|
3942 | }
|
---|
3943 |
|
---|
3944 | /**
|
---|
3945 | * Returns the medium device type. Must have caller + locking!
|
---|
3946 | * @return
|
---|
3947 | */
|
---|
3948 | DeviceType_T Medium::i_getDeviceType() const
|
---|
3949 | {
|
---|
3950 | return m->devType;
|
---|
3951 | }
|
---|
3952 |
|
---|
3953 | /**
|
---|
3954 | * Returns the medium type. Must have caller + locking!
|
---|
3955 | * @return
|
---|
3956 | */
|
---|
3957 | MediumType_T Medium::i_getType() const
|
---|
3958 | {
|
---|
3959 | return m->type;
|
---|
3960 | }
|
---|
3961 |
|
---|
3962 | /**
|
---|
3963 | * Returns a short version of the location attribute.
|
---|
3964 | *
|
---|
3965 | * @note Must be called from under this object's read or write lock.
|
---|
3966 | */
|
---|
3967 | Utf8Str Medium::i_getName()
|
---|
3968 | {
|
---|
3969 | Utf8Str name = RTPathFilename(m->strLocationFull.c_str());
|
---|
3970 | return name;
|
---|
3971 | }
|
---|
3972 |
|
---|
3973 | /**
|
---|
3974 | * This adds the given UUID to the list of media registries in which this
|
---|
3975 | * medium should be registered. The UUID can either be a machine UUID,
|
---|
3976 | * to add a machine registry, or the global registry UUID as returned by
|
---|
3977 | * VirtualBox::getGlobalRegistryId().
|
---|
3978 | *
|
---|
3979 | * Note that for hard disks, this method does nothing if the medium is
|
---|
3980 | * already in another registry to avoid having hard disks in more than
|
---|
3981 | * one registry, which causes trouble with keeping diff images in sync.
|
---|
3982 | * See getFirstRegistryMachineId() for details.
|
---|
3983 | *
|
---|
3984 | * @param id
|
---|
3985 | * @return true if the registry was added; false if the given id was already on the list.
|
---|
3986 | */
|
---|
3987 | bool Medium::i_addRegistry(const Guid& id)
|
---|
3988 | {
|
---|
3989 | AutoCaller autoCaller(this);
|
---|
3990 | if (FAILED(autoCaller.rc()))
|
---|
3991 | return false;
|
---|
3992 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3993 |
|
---|
3994 | bool fAdd = true;
|
---|
3995 |
|
---|
3996 | // hard disks cannot be in more than one registry
|
---|
3997 | if ( m->devType == DeviceType_HardDisk
|
---|
3998 | && m->llRegistryIDs.size() > 0)
|
---|
3999 | fAdd = false;
|
---|
4000 |
|
---|
4001 | // no need to add the UUID twice
|
---|
4002 | if (fAdd)
|
---|
4003 | {
|
---|
4004 | for (GuidList::const_iterator it = m->llRegistryIDs.begin();
|
---|
4005 | it != m->llRegistryIDs.end();
|
---|
4006 | ++it)
|
---|
4007 | {
|
---|
4008 | if ((*it) == id)
|
---|
4009 | {
|
---|
4010 | fAdd = false;
|
---|
4011 | break;
|
---|
4012 | }
|
---|
4013 | }
|
---|
4014 | }
|
---|
4015 |
|
---|
4016 | if (fAdd)
|
---|
4017 | m->llRegistryIDs.push_back(id);
|
---|
4018 |
|
---|
4019 | return fAdd;
|
---|
4020 | }
|
---|
4021 |
|
---|
4022 | /**
|
---|
4023 | * This adds the given UUID to the list of media registries in which this
|
---|
4024 | * medium should be registered. The UUID can either be a machine UUID,
|
---|
4025 | * to add a machine registry, or the global registry UUID as returned by
|
---|
4026 | * VirtualBox::getGlobalRegistryId(). This recurses over all children.
|
---|
4027 | *
|
---|
4028 | * Note that for hard disks, this method does nothing if the medium is
|
---|
4029 | * already in another registry to avoid having hard disks in more than
|
---|
4030 | * one registry, which causes trouble with keeping diff images in sync.
|
---|
4031 | * See getFirstRegistryMachineId() for details.
|
---|
4032 | *
|
---|
4033 | * @note the caller must hold the media tree lock for reading.
|
---|
4034 | *
|
---|
4035 | * @param id
|
---|
4036 | * @return true if the registry was added; false if the given id was already on the list.
|
---|
4037 | */
|
---|
4038 | bool Medium::i_addRegistryRecursive(const Guid &id)
|
---|
4039 | {
|
---|
4040 | AutoCaller autoCaller(this);
|
---|
4041 | if (FAILED(autoCaller.rc()))
|
---|
4042 | return false;
|
---|
4043 |
|
---|
4044 | bool fAdd = i_addRegistry(id);
|
---|
4045 |
|
---|
4046 | // protected by the medium tree lock held by our original caller
|
---|
4047 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
4048 | it != i_getChildren().end();
|
---|
4049 | ++it)
|
---|
4050 | {
|
---|
4051 | Medium *pChild = *it;
|
---|
4052 | fAdd |= pChild->i_addRegistryRecursive(id);
|
---|
4053 | }
|
---|
4054 |
|
---|
4055 | return fAdd;
|
---|
4056 | }
|
---|
4057 |
|
---|
4058 | /**
|
---|
4059 | * Removes the given UUID from the list of media registry UUIDs of this medium.
|
---|
4060 | *
|
---|
4061 | * @param id
|
---|
4062 | * @return true if the UUID was found or false if not.
|
---|
4063 | */
|
---|
4064 | bool Medium::i_removeRegistry(const Guid &id)
|
---|
4065 | {
|
---|
4066 | AutoCaller autoCaller(this);
|
---|
4067 | if (FAILED(autoCaller.rc()))
|
---|
4068 | return false;
|
---|
4069 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4070 |
|
---|
4071 | bool fRemove = false;
|
---|
4072 |
|
---|
4073 | /// @todo r=klaus eliminate this code, replace it by using find.
|
---|
4074 | for (GuidList::iterator it = m->llRegistryIDs.begin();
|
---|
4075 | it != m->llRegistryIDs.end();
|
---|
4076 | ++it)
|
---|
4077 | {
|
---|
4078 | if ((*it) == id)
|
---|
4079 | {
|
---|
4080 | // getting away with this as the iterator isn't used after
|
---|
4081 | m->llRegistryIDs.erase(it);
|
---|
4082 | fRemove = true;
|
---|
4083 | break;
|
---|
4084 | }
|
---|
4085 | }
|
---|
4086 |
|
---|
4087 | return fRemove;
|
---|
4088 | }
|
---|
4089 |
|
---|
4090 | /**
|
---|
4091 | * Removes the given UUID from the list of media registry UUIDs, for this
|
---|
4092 | * medium and all its children recursively.
|
---|
4093 | *
|
---|
4094 | * @note the caller must hold the media tree lock for reading.
|
---|
4095 | *
|
---|
4096 | * @param id
|
---|
4097 | * @return true if the UUID was found or false if not.
|
---|
4098 | */
|
---|
4099 | bool Medium::i_removeRegistryRecursive(const Guid &id)
|
---|
4100 | {
|
---|
4101 | AutoCaller autoCaller(this);
|
---|
4102 | if (FAILED(autoCaller.rc()))
|
---|
4103 | return false;
|
---|
4104 |
|
---|
4105 | bool fRemove = i_removeRegistry(id);
|
---|
4106 |
|
---|
4107 | // protected by the medium tree lock held by our original caller
|
---|
4108 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
4109 | it != i_getChildren().end();
|
---|
4110 | ++it)
|
---|
4111 | {
|
---|
4112 | Medium *pChild = *it;
|
---|
4113 | fRemove |= pChild->i_removeRegistryRecursive(id);
|
---|
4114 | }
|
---|
4115 |
|
---|
4116 | return fRemove;
|
---|
4117 | }
|
---|
4118 |
|
---|
4119 | /**
|
---|
4120 | * Returns true if id is in the list of media registries for this medium.
|
---|
4121 | *
|
---|
4122 | * Must have caller + read locking!
|
---|
4123 | *
|
---|
4124 | * @param id
|
---|
4125 | * @return
|
---|
4126 | */
|
---|
4127 | bool Medium::i_isInRegistry(const Guid &id)
|
---|
4128 | {
|
---|
4129 | /// @todo r=klaus eliminate this code, replace it by using find.
|
---|
4130 | for (GuidList::const_iterator it = m->llRegistryIDs.begin();
|
---|
4131 | it != m->llRegistryIDs.end();
|
---|
4132 | ++it)
|
---|
4133 | {
|
---|
4134 | if (*it == id)
|
---|
4135 | return true;
|
---|
4136 | }
|
---|
4137 |
|
---|
4138 | return false;
|
---|
4139 | }
|
---|
4140 |
|
---|
4141 | /**
|
---|
4142 | * Internal method to return the medium's first registry machine (i.e. the machine in whose
|
---|
4143 | * machine XML this medium is listed).
|
---|
4144 | *
|
---|
4145 | * Every attached medium must now (4.0) reside in at least one media registry, which is identified
|
---|
4146 | * by a UUID. This is either a machine UUID if the machine is from 4.0 or newer, in which case
|
---|
4147 | * machines have their own media registries, or it is the pseudo-UUID of the VirtualBox
|
---|
4148 | * object if the machine is old and still needs the global registry in VirtualBox.xml.
|
---|
4149 | *
|
---|
4150 | * By definition, hard disks may only be in one media registry, in which all its children
|
---|
4151 | * will be stored as well. Otherwise we run into problems with having keep multiple registries
|
---|
4152 | * in sync. (This is the "cloned VM" case in which VM1 may link to the disks of VM2; in this
|
---|
4153 | * case, only VM2's registry is used for the disk in question.)
|
---|
4154 | *
|
---|
4155 | * If there is no medium registry, particularly if the medium has not been attached yet, this
|
---|
4156 | * does not modify uuid and returns false.
|
---|
4157 | *
|
---|
4158 | * ISOs and RAWs, by contrast, can be in more than one repository to make things easier for
|
---|
4159 | * the user.
|
---|
4160 | *
|
---|
4161 | * Must have caller + locking!
|
---|
4162 | *
|
---|
4163 | * @param uuid Receives first registry machine UUID, if available.
|
---|
4164 | * @return true if uuid was set.
|
---|
4165 | */
|
---|
4166 | bool Medium::i_getFirstRegistryMachineId(Guid &uuid) const
|
---|
4167 | {
|
---|
4168 | if (m->llRegistryIDs.size())
|
---|
4169 | {
|
---|
4170 | uuid = m->llRegistryIDs.front();
|
---|
4171 | return true;
|
---|
4172 | }
|
---|
4173 | return false;
|
---|
4174 | }
|
---|
4175 |
|
---|
4176 | /**
|
---|
4177 | * Marks all the registries in which this medium is registered as modified.
|
---|
4178 | */
|
---|
4179 | void Medium::i_markRegistriesModified()
|
---|
4180 | {
|
---|
4181 | AutoCaller autoCaller(this);
|
---|
4182 | if (FAILED(autoCaller.rc())) return;
|
---|
4183 |
|
---|
4184 | // Get local copy, as keeping the lock over VirtualBox::markRegistryModified
|
---|
4185 | // causes trouble with the lock order
|
---|
4186 | GuidList llRegistryIDs;
|
---|
4187 | {
|
---|
4188 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4189 | llRegistryIDs = m->llRegistryIDs;
|
---|
4190 | }
|
---|
4191 |
|
---|
4192 | autoCaller.release();
|
---|
4193 |
|
---|
4194 | /* Save the error information now, the implicit restore when this goes
|
---|
4195 | * out of scope will throw away spurious additional errors created below. */
|
---|
4196 | ErrorInfoKeeper eik;
|
---|
4197 | for (GuidList::const_iterator it = llRegistryIDs.begin();
|
---|
4198 | it != llRegistryIDs.end();
|
---|
4199 | ++it)
|
---|
4200 | {
|
---|
4201 | m->pVirtualBox->i_markRegistryModified(*it);
|
---|
4202 | }
|
---|
4203 | }
|
---|
4204 |
|
---|
4205 | /**
|
---|
4206 | * Adds the given machine and optionally the snapshot to the list of the objects
|
---|
4207 | * this medium is attached to.
|
---|
4208 | *
|
---|
4209 | * @param aMachineId Machine ID.
|
---|
4210 | * @param aSnapshotId Snapshot ID; when non-empty, adds a snapshot attachment.
|
---|
4211 | */
|
---|
4212 | HRESULT Medium::i_addBackReference(const Guid &aMachineId,
|
---|
4213 | const Guid &aSnapshotId /*= Guid::Empty*/)
|
---|
4214 | {
|
---|
4215 | AssertReturn(aMachineId.isValid(), E_FAIL);
|
---|
4216 |
|
---|
4217 | LogFlowThisFunc(("ENTER, aMachineId: {%RTuuid}, aSnapshotId: {%RTuuid}\n", aMachineId.raw(), aSnapshotId.raw()));
|
---|
4218 |
|
---|
4219 | AutoCaller autoCaller(this);
|
---|
4220 | AssertComRCReturnRC(autoCaller.rc());
|
---|
4221 |
|
---|
4222 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4223 |
|
---|
4224 | switch (m->state)
|
---|
4225 | {
|
---|
4226 | case MediumState_Created:
|
---|
4227 | case MediumState_Inaccessible:
|
---|
4228 | case MediumState_LockedRead:
|
---|
4229 | case MediumState_LockedWrite:
|
---|
4230 | break;
|
---|
4231 |
|
---|
4232 | default:
|
---|
4233 | return i_setStateError();
|
---|
4234 | }
|
---|
4235 |
|
---|
4236 | if (m->numCreateDiffTasks > 0)
|
---|
4237 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4238 | tr("Cannot attach medium '%s' {%RTuuid}: %u differencing child media are being created"),
|
---|
4239 | m->strLocationFull.c_str(),
|
---|
4240 | m->id.raw(),
|
---|
4241 | m->numCreateDiffTasks);
|
---|
4242 |
|
---|
4243 | BackRefList::iterator it = std::find_if(m->backRefs.begin(),
|
---|
4244 | m->backRefs.end(),
|
---|
4245 | BackRef::EqualsTo(aMachineId));
|
---|
4246 | if (it == m->backRefs.end())
|
---|
4247 | {
|
---|
4248 | BackRef ref(aMachineId, aSnapshotId);
|
---|
4249 | m->backRefs.push_back(ref);
|
---|
4250 |
|
---|
4251 | return S_OK;
|
---|
4252 | }
|
---|
4253 |
|
---|
4254 | // if the caller has not supplied a snapshot ID, then we're attaching
|
---|
4255 | // to a machine a medium which represents the machine's current state,
|
---|
4256 | // so set the flag
|
---|
4257 |
|
---|
4258 | if (aSnapshotId.isZero())
|
---|
4259 | {
|
---|
4260 | /* sanity: no duplicate attachments */
|
---|
4261 | if (it->fInCurState)
|
---|
4262 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4263 | tr("Cannot attach medium '%s' {%RTuuid}: medium is already associated with the current state of machine uuid {%RTuuid}!"),
|
---|
4264 | m->strLocationFull.c_str(),
|
---|
4265 | m->id.raw(),
|
---|
4266 | aMachineId.raw());
|
---|
4267 | it->fInCurState = true;
|
---|
4268 |
|
---|
4269 | return S_OK;
|
---|
4270 | }
|
---|
4271 |
|
---|
4272 | // otherwise: a snapshot medium is being attached
|
---|
4273 |
|
---|
4274 | /* sanity: no duplicate attachments */
|
---|
4275 | for (GuidList::const_iterator jt = it->llSnapshotIds.begin();
|
---|
4276 | jt != it->llSnapshotIds.end();
|
---|
4277 | ++jt)
|
---|
4278 | {
|
---|
4279 | const Guid &idOldSnapshot = *jt;
|
---|
4280 |
|
---|
4281 | if (idOldSnapshot == aSnapshotId)
|
---|
4282 | {
|
---|
4283 | #ifdef DEBUG
|
---|
4284 | i_dumpBackRefs();
|
---|
4285 | #endif
|
---|
4286 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4287 | tr("Cannot attach medium '%s' {%RTuuid} from snapshot '%RTuuid': medium is already in use by this snapshot!"),
|
---|
4288 | m->strLocationFull.c_str(),
|
---|
4289 | m->id.raw(),
|
---|
4290 | aSnapshotId.raw());
|
---|
4291 | }
|
---|
4292 | }
|
---|
4293 |
|
---|
4294 | it->llSnapshotIds.push_back(aSnapshotId);
|
---|
4295 | // Do not touch fInCurState, as the image may be attached to the current
|
---|
4296 | // state *and* a snapshot, otherwise we lose the current state association!
|
---|
4297 |
|
---|
4298 | LogFlowThisFuncLeave();
|
---|
4299 |
|
---|
4300 | return S_OK;
|
---|
4301 | }
|
---|
4302 |
|
---|
4303 | /**
|
---|
4304 | * Removes the given machine and optionally the snapshot from the list of the
|
---|
4305 | * objects this medium is attached to.
|
---|
4306 | *
|
---|
4307 | * @param aMachineId Machine ID.
|
---|
4308 | * @param aSnapshotId Snapshot ID; when non-empty, removes the snapshot
|
---|
4309 | * attachment.
|
---|
4310 | */
|
---|
4311 | HRESULT Medium::i_removeBackReference(const Guid &aMachineId,
|
---|
4312 | const Guid &aSnapshotId /*= Guid::Empty*/)
|
---|
4313 | {
|
---|
4314 | AssertReturn(aMachineId.isValid(), E_FAIL);
|
---|
4315 |
|
---|
4316 | AutoCaller autoCaller(this);
|
---|
4317 | AssertComRCReturnRC(autoCaller.rc());
|
---|
4318 |
|
---|
4319 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4320 |
|
---|
4321 | BackRefList::iterator it =
|
---|
4322 | std::find_if(m->backRefs.begin(), m->backRefs.end(),
|
---|
4323 | BackRef::EqualsTo(aMachineId));
|
---|
4324 | AssertReturn(it != m->backRefs.end(), E_FAIL);
|
---|
4325 |
|
---|
4326 | if (aSnapshotId.isZero())
|
---|
4327 | {
|
---|
4328 | /* remove the current state attachment */
|
---|
4329 | it->fInCurState = false;
|
---|
4330 | }
|
---|
4331 | else
|
---|
4332 | {
|
---|
4333 | /* remove the snapshot attachment */
|
---|
4334 | GuidList::iterator jt = std::find(it->llSnapshotIds.begin(),
|
---|
4335 | it->llSnapshotIds.end(),
|
---|
4336 | aSnapshotId);
|
---|
4337 |
|
---|
4338 | AssertReturn(jt != it->llSnapshotIds.end(), E_FAIL);
|
---|
4339 | it->llSnapshotIds.erase(jt);
|
---|
4340 | }
|
---|
4341 |
|
---|
4342 | /* if the backref becomes empty, remove it */
|
---|
4343 | if (it->fInCurState == false && it->llSnapshotIds.size() == 0)
|
---|
4344 | m->backRefs.erase(it);
|
---|
4345 |
|
---|
4346 | return S_OK;
|
---|
4347 | }
|
---|
4348 |
|
---|
4349 | /**
|
---|
4350 | * Internal method to return the medium's list of backrefs. Must have caller + locking!
|
---|
4351 | * @return
|
---|
4352 | */
|
---|
4353 | const Guid* Medium::i_getFirstMachineBackrefId() const
|
---|
4354 | {
|
---|
4355 | if (!m->backRefs.size())
|
---|
4356 | return NULL;
|
---|
4357 |
|
---|
4358 | return &m->backRefs.front().machineId;
|
---|
4359 | }
|
---|
4360 |
|
---|
4361 | /**
|
---|
4362 | * Internal method which returns a machine that either this medium or one of its children
|
---|
4363 | * is attached to. This is used for finding a replacement media registry when an existing
|
---|
4364 | * media registry is about to be deleted in VirtualBox::unregisterMachine().
|
---|
4365 | *
|
---|
4366 | * Must have caller + locking, *and* caller must hold the media tree lock!
|
---|
4367 | * @return
|
---|
4368 | */
|
---|
4369 | const Guid* Medium::i_getAnyMachineBackref() const
|
---|
4370 | {
|
---|
4371 | if (m->backRefs.size())
|
---|
4372 | return &m->backRefs.front().machineId;
|
---|
4373 |
|
---|
4374 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
4375 | it != i_getChildren().end();
|
---|
4376 | ++it)
|
---|
4377 | {
|
---|
4378 | Medium *pChild = *it;
|
---|
4379 | // recurse for this child
|
---|
4380 | const Guid* puuid;
|
---|
4381 | if ((puuid = pChild->i_getAnyMachineBackref()))
|
---|
4382 | return puuid;
|
---|
4383 | }
|
---|
4384 |
|
---|
4385 | return NULL;
|
---|
4386 | }
|
---|
4387 |
|
---|
4388 | const Guid* Medium::i_getFirstMachineBackrefSnapshotId() const
|
---|
4389 | {
|
---|
4390 | if (!m->backRefs.size())
|
---|
4391 | return NULL;
|
---|
4392 |
|
---|
4393 | const BackRef &ref = m->backRefs.front();
|
---|
4394 | if (ref.llSnapshotIds.empty())
|
---|
4395 | return NULL;
|
---|
4396 |
|
---|
4397 | return &ref.llSnapshotIds.front();
|
---|
4398 | }
|
---|
4399 |
|
---|
4400 | size_t Medium::i_getMachineBackRefCount() const
|
---|
4401 | {
|
---|
4402 | return m->backRefs.size();
|
---|
4403 | }
|
---|
4404 |
|
---|
4405 | #ifdef DEBUG
|
---|
4406 | /**
|
---|
4407 | * Debugging helper that gets called after VirtualBox initialization that writes all
|
---|
4408 | * machine backreferences to the debug log.
|
---|
4409 | */
|
---|
4410 | void Medium::i_dumpBackRefs()
|
---|
4411 | {
|
---|
4412 | AutoCaller autoCaller(this);
|
---|
4413 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4414 |
|
---|
4415 | LogFlowThisFunc(("Dumping backrefs for medium '%s':\n", m->strLocationFull.c_str()));
|
---|
4416 |
|
---|
4417 | for (BackRefList::iterator it2 = m->backRefs.begin();
|
---|
4418 | it2 != m->backRefs.end();
|
---|
4419 | ++it2)
|
---|
4420 | {
|
---|
4421 | const BackRef &ref = *it2;
|
---|
4422 | LogFlowThisFunc((" Backref from machine {%RTuuid} (fInCurState: %d)\n", ref.machineId.raw(), ref.fInCurState));
|
---|
4423 |
|
---|
4424 | for (GuidList::const_iterator jt2 = it2->llSnapshotIds.begin();
|
---|
4425 | jt2 != it2->llSnapshotIds.end();
|
---|
4426 | ++jt2)
|
---|
4427 | {
|
---|
4428 | const Guid &id = *jt2;
|
---|
4429 | LogFlowThisFunc((" Backref from snapshot {%RTuuid}\n", id.raw()));
|
---|
4430 | }
|
---|
4431 | }
|
---|
4432 | }
|
---|
4433 | #endif
|
---|
4434 |
|
---|
4435 | /**
|
---|
4436 | * Checks if the given change of \a aOldPath to \a aNewPath affects the location
|
---|
4437 | * of this media and updates it if necessary to reflect the new location.
|
---|
4438 | *
|
---|
4439 | * @param strOldPath Old path (full).
|
---|
4440 | * @param strNewPath New path (full).
|
---|
4441 | *
|
---|
4442 | * @note Locks this object for writing.
|
---|
4443 | */
|
---|
4444 | HRESULT Medium::i_updatePath(const Utf8Str &strOldPath, const Utf8Str &strNewPath)
|
---|
4445 | {
|
---|
4446 | AssertReturn(!strOldPath.isEmpty(), E_FAIL);
|
---|
4447 | AssertReturn(!strNewPath.isEmpty(), E_FAIL);
|
---|
4448 |
|
---|
4449 | AutoCaller autoCaller(this);
|
---|
4450 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4451 |
|
---|
4452 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4453 |
|
---|
4454 | LogFlowThisFunc(("locationFull.before='%s'\n", m->strLocationFull.c_str()));
|
---|
4455 |
|
---|
4456 | const char *pcszMediumPath = m->strLocationFull.c_str();
|
---|
4457 |
|
---|
4458 | if (RTPathStartsWith(pcszMediumPath, strOldPath.c_str()))
|
---|
4459 | {
|
---|
4460 | Utf8Str newPath(strNewPath);
|
---|
4461 | newPath.append(pcszMediumPath + strOldPath.length());
|
---|
4462 | unconst(m->strLocationFull) = newPath;
|
---|
4463 |
|
---|
4464 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
4465 |
|
---|
4466 | LogFlowThisFunc(("locationFull.after='%s'\n", m->strLocationFull.c_str()));
|
---|
4467 | // we changed something
|
---|
4468 | return S_OK;
|
---|
4469 | }
|
---|
4470 |
|
---|
4471 | // no change was necessary, signal error which the caller needs to interpret
|
---|
4472 | return VBOX_E_FILE_ERROR;
|
---|
4473 | }
|
---|
4474 |
|
---|
4475 | /**
|
---|
4476 | * Returns the base medium of the media chain this medium is part of.
|
---|
4477 | *
|
---|
4478 | * The base medium is found by walking up the parent-child relationship axis.
|
---|
4479 | * If the medium doesn't have a parent (i.e. it's a base medium), it
|
---|
4480 | * returns itself in response to this method.
|
---|
4481 | *
|
---|
4482 | * @param aLevel Where to store the number of ancestors of this medium
|
---|
4483 | * (zero for the base), may be @c NULL.
|
---|
4484 | *
|
---|
4485 | * @note Locks medium tree for reading.
|
---|
4486 | */
|
---|
4487 | ComObjPtr<Medium> Medium::i_getBase(uint32_t *aLevel /*= NULL*/)
|
---|
4488 | {
|
---|
4489 | ComObjPtr<Medium> pBase;
|
---|
4490 |
|
---|
4491 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
4492 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
4493 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
4494 | if (!pVirtualBox)
|
---|
4495 | return pBase;
|
---|
4496 |
|
---|
4497 | /* we access m->pParent */
|
---|
4498 | AutoReadLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4499 |
|
---|
4500 | AutoCaller autoCaller(this);
|
---|
4501 | AssertReturn(autoCaller.isOk(), pBase);
|
---|
4502 |
|
---|
4503 | pBase = this;
|
---|
4504 | uint32_t level = 0;
|
---|
4505 |
|
---|
4506 | if (m->pParent)
|
---|
4507 | {
|
---|
4508 | for (;;)
|
---|
4509 | {
|
---|
4510 | AutoCaller baseCaller(pBase);
|
---|
4511 | AssertReturn(baseCaller.isOk(), pBase);
|
---|
4512 |
|
---|
4513 | if (pBase->m->pParent.isNull())
|
---|
4514 | break;
|
---|
4515 |
|
---|
4516 | pBase = pBase->m->pParent;
|
---|
4517 | ++level;
|
---|
4518 | }
|
---|
4519 | }
|
---|
4520 |
|
---|
4521 | if (aLevel != NULL)
|
---|
4522 | *aLevel = level;
|
---|
4523 |
|
---|
4524 | return pBase;
|
---|
4525 | }
|
---|
4526 |
|
---|
4527 | /**
|
---|
4528 | * Returns the depth of this medium in the media chain.
|
---|
4529 | *
|
---|
4530 | * @note Locks medium tree for reading.
|
---|
4531 | */
|
---|
4532 | uint32_t Medium::i_getDepth()
|
---|
4533 | {
|
---|
4534 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
4535 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
4536 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
4537 | if (!pVirtualBox)
|
---|
4538 | return 1;
|
---|
4539 |
|
---|
4540 | /* we access m->pParent */
|
---|
4541 | AutoReadLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4542 |
|
---|
4543 | uint32_t cDepth = 0;
|
---|
4544 | ComObjPtr<Medium> pMedium(this);
|
---|
4545 | while (!pMedium.isNull())
|
---|
4546 | {
|
---|
4547 | AutoCaller autoCaller(this);
|
---|
4548 | AssertReturn(autoCaller.isOk(), cDepth + 1);
|
---|
4549 |
|
---|
4550 | pMedium = pMedium->m->pParent;
|
---|
4551 | cDepth++;
|
---|
4552 | }
|
---|
4553 |
|
---|
4554 | return cDepth;
|
---|
4555 | }
|
---|
4556 |
|
---|
4557 | /**
|
---|
4558 | * Returns @c true if this medium cannot be modified because it has
|
---|
4559 | * dependents (children) or is part of the snapshot. Related to the medium
|
---|
4560 | * type and posterity, not to the current media state.
|
---|
4561 | *
|
---|
4562 | * @note Locks this object and medium tree for reading.
|
---|
4563 | */
|
---|
4564 | bool Medium::i_isReadOnly()
|
---|
4565 | {
|
---|
4566 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
4567 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
4568 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
4569 | if (!pVirtualBox)
|
---|
4570 | return false;
|
---|
4571 |
|
---|
4572 | /* we access children */
|
---|
4573 | AutoReadLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4574 |
|
---|
4575 | AutoCaller autoCaller(this);
|
---|
4576 | AssertComRCReturn(autoCaller.rc(), false);
|
---|
4577 |
|
---|
4578 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4579 |
|
---|
4580 | switch (m->type)
|
---|
4581 | {
|
---|
4582 | case MediumType_Normal:
|
---|
4583 | {
|
---|
4584 | if (i_getChildren().size() != 0)
|
---|
4585 | return true;
|
---|
4586 |
|
---|
4587 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
4588 | it != m->backRefs.end(); ++it)
|
---|
4589 | if (it->llSnapshotIds.size() != 0)
|
---|
4590 | return true;
|
---|
4591 |
|
---|
4592 | if (m->variant & MediumVariant_VmdkStreamOptimized)
|
---|
4593 | return true;
|
---|
4594 |
|
---|
4595 | return false;
|
---|
4596 | }
|
---|
4597 | case MediumType_Immutable:
|
---|
4598 | case MediumType_MultiAttach:
|
---|
4599 | return true;
|
---|
4600 | case MediumType_Writethrough:
|
---|
4601 | case MediumType_Shareable:
|
---|
4602 | case MediumType_Readonly: /* explicit readonly media has no diffs */
|
---|
4603 | return false;
|
---|
4604 | default:
|
---|
4605 | break;
|
---|
4606 | }
|
---|
4607 |
|
---|
4608 | AssertFailedReturn(false);
|
---|
4609 | }
|
---|
4610 |
|
---|
4611 | /**
|
---|
4612 | * Internal method to update the medium's id. Must have caller + locking!
|
---|
4613 | * @return
|
---|
4614 | */
|
---|
4615 | void Medium::i_updateId(const Guid &id)
|
---|
4616 | {
|
---|
4617 | unconst(m->id) = id;
|
---|
4618 | }
|
---|
4619 |
|
---|
4620 | /**
|
---|
4621 | * Saves the settings of one medium.
|
---|
4622 | *
|
---|
4623 | * @note Caller MUST take care of the medium tree lock and caller.
|
---|
4624 | *
|
---|
4625 | * @param data Settings struct to be updated.
|
---|
4626 | * @param strHardDiskFolder Folder for which paths should be relative.
|
---|
4627 | */
|
---|
4628 | void Medium::i_saveSettingsOne(settings::Medium &data, const Utf8Str &strHardDiskFolder)
|
---|
4629 | {
|
---|
4630 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4631 |
|
---|
4632 | data.uuid = m->id;
|
---|
4633 |
|
---|
4634 | // make path relative if needed
|
---|
4635 | if ( !strHardDiskFolder.isEmpty()
|
---|
4636 | && RTPathStartsWith(m->strLocationFull.c_str(), strHardDiskFolder.c_str())
|
---|
4637 | )
|
---|
4638 | data.strLocation = m->strLocationFull.substr(strHardDiskFolder.length() + 1);
|
---|
4639 | else
|
---|
4640 | data.strLocation = m->strLocationFull;
|
---|
4641 | data.strFormat = m->strFormat;
|
---|
4642 |
|
---|
4643 | /* optional, only for diffs, default is false */
|
---|
4644 | if (m->pParent)
|
---|
4645 | data.fAutoReset = m->autoReset;
|
---|
4646 | else
|
---|
4647 | data.fAutoReset = false;
|
---|
4648 |
|
---|
4649 | /* optional */
|
---|
4650 | data.strDescription = m->strDescription;
|
---|
4651 |
|
---|
4652 | /* optional properties */
|
---|
4653 | data.properties.clear();
|
---|
4654 |
|
---|
4655 | /* handle iSCSI initiator secrets transparently */
|
---|
4656 | bool fHaveInitiatorSecretEncrypted = false;
|
---|
4657 | Utf8Str strCiphertext;
|
---|
4658 | settings::StringsMap::const_iterator itPln = m->mapProperties.find("InitiatorSecret");
|
---|
4659 | if ( itPln != m->mapProperties.end()
|
---|
4660 | && !itPln->second.isEmpty())
|
---|
4661 | {
|
---|
4662 | /* Encrypt the plain secret. If that does not work (i.e. no or wrong settings key
|
---|
4663 | * specified), just use the encrypted secret (if there is any). */
|
---|
4664 | int rc = m->pVirtualBox->i_encryptSetting(itPln->second, &strCiphertext);
|
---|
4665 | if (RT_SUCCESS(rc))
|
---|
4666 | fHaveInitiatorSecretEncrypted = true;
|
---|
4667 | }
|
---|
4668 | for (settings::StringsMap::const_iterator it = m->mapProperties.begin();
|
---|
4669 | it != m->mapProperties.end();
|
---|
4670 | ++it)
|
---|
4671 | {
|
---|
4672 | /* only save properties that have non-default values */
|
---|
4673 | if (!it->second.isEmpty())
|
---|
4674 | {
|
---|
4675 | const Utf8Str &name = it->first;
|
---|
4676 | const Utf8Str &value = it->second;
|
---|
4677 | /* do NOT store the plain InitiatorSecret */
|
---|
4678 | if ( !fHaveInitiatorSecretEncrypted
|
---|
4679 | || !name.equals("InitiatorSecret"))
|
---|
4680 | data.properties[name] = value;
|
---|
4681 | }
|
---|
4682 | }
|
---|
4683 | if (fHaveInitiatorSecretEncrypted)
|
---|
4684 | data.properties["InitiatorSecretEncrypted"] = strCiphertext;
|
---|
4685 |
|
---|
4686 | /* only for base media */
|
---|
4687 | if (m->pParent.isNull())
|
---|
4688 | data.hdType = m->type;
|
---|
4689 | }
|
---|
4690 |
|
---|
4691 | /**
|
---|
4692 | * Saves medium data by putting it into the provided data structure.
|
---|
4693 | * Recurses over all children to save their settings, too.
|
---|
4694 | *
|
---|
4695 | * @param data Settings struct to be updated.
|
---|
4696 | * @param strHardDiskFolder Folder for which paths should be relative.
|
---|
4697 | *
|
---|
4698 | * @note Locks this object, medium tree and children for reading.
|
---|
4699 | */
|
---|
4700 | HRESULT Medium::i_saveSettings(settings::Medium &data,
|
---|
4701 | const Utf8Str &strHardDiskFolder)
|
---|
4702 | {
|
---|
4703 | /* we access m->pParent */
|
---|
4704 | AutoReadLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4705 |
|
---|
4706 | AutoCaller autoCaller(this);
|
---|
4707 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4708 |
|
---|
4709 | i_saveSettingsOne(data, strHardDiskFolder);
|
---|
4710 |
|
---|
4711 | /* save all children */
|
---|
4712 | settings::MediaList &llSettingsChildren = data.llChildren;
|
---|
4713 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
4714 | it != i_getChildren().end();
|
---|
4715 | ++it)
|
---|
4716 | {
|
---|
4717 | // Use the element straight in the list to reduce both unnecessary
|
---|
4718 | // deep copying (when unwinding the recursion the entire medium
|
---|
4719 | // settings sub-tree is copied) and the stack footprint (the settings
|
---|
4720 | // need almost 1K, and there can be VMs with long image chains.
|
---|
4721 | llSettingsChildren.push_back(settings::Medium::Empty);
|
---|
4722 | HRESULT rc = (*it)->i_saveSettings(llSettingsChildren.back(), strHardDiskFolder);
|
---|
4723 | if (FAILED(rc))
|
---|
4724 | {
|
---|
4725 | llSettingsChildren.pop_back();
|
---|
4726 | return rc;
|
---|
4727 | }
|
---|
4728 | }
|
---|
4729 |
|
---|
4730 | return S_OK;
|
---|
4731 | }
|
---|
4732 |
|
---|
4733 | /**
|
---|
4734 | * Constructs a medium lock list for this medium. The lock is not taken.
|
---|
4735 | *
|
---|
4736 | * @note Caller MUST NOT hold the media tree or medium lock.
|
---|
4737 | *
|
---|
4738 | * @param fFailIfInaccessible If true, this fails with an error if a medium is inaccessible. If false,
|
---|
4739 | * inaccessible media are silently skipped and not locked (i.e. their state remains "Inaccessible");
|
---|
4740 | * this is necessary for a VM's removable media VM startup for which we do not want to fail.
|
---|
4741 | * @param pToLockWrite If not NULL, associate a write lock with this medium object.
|
---|
4742 | * @param fMediumLockWriteAll Whether to associate a write lock to all other media too.
|
---|
4743 | * @param pToBeParent Medium which will become the parent of this medium.
|
---|
4744 | * @param mediumLockList Where to store the resulting list.
|
---|
4745 | */
|
---|
4746 | HRESULT Medium::i_createMediumLockList(bool fFailIfInaccessible,
|
---|
4747 | Medium *pToLockWrite,
|
---|
4748 | bool fMediumLockWriteAll,
|
---|
4749 | Medium *pToBeParent,
|
---|
4750 | MediumLockList &mediumLockList)
|
---|
4751 | {
|
---|
4752 | /** @todo r=klaus this needs to be reworked, as the code below uses
|
---|
4753 | * i_getParent without holding the tree lock, and changing this is
|
---|
4754 | * a significant amount of effort. */
|
---|
4755 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
4756 | Assert(!isWriteLockOnCurrentThread());
|
---|
4757 |
|
---|
4758 | AutoCaller autoCaller(this);
|
---|
4759 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4760 |
|
---|
4761 | HRESULT rc = S_OK;
|
---|
4762 |
|
---|
4763 | /* paranoid sanity checking if the medium has a to-be parent medium */
|
---|
4764 | if (pToBeParent)
|
---|
4765 | {
|
---|
4766 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4767 | ComAssertRet(i_getParent().isNull(), E_FAIL);
|
---|
4768 | ComAssertRet(i_getChildren().size() == 0, E_FAIL);
|
---|
4769 | }
|
---|
4770 |
|
---|
4771 | ErrorInfoKeeper eik;
|
---|
4772 | MultiResult mrc(S_OK);
|
---|
4773 |
|
---|
4774 | ComObjPtr<Medium> pMedium = this;
|
---|
4775 | while (!pMedium.isNull())
|
---|
4776 | {
|
---|
4777 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
4778 |
|
---|
4779 | /* Accessibility check must be first, otherwise locking interferes
|
---|
4780 | * with getting the medium state. Lock lists are not created for
|
---|
4781 | * fun, and thus getting the medium status is no luxury. */
|
---|
4782 | MediumState_T mediumState = pMedium->i_getState();
|
---|
4783 | if (mediumState == MediumState_Inaccessible)
|
---|
4784 | {
|
---|
4785 | alock.release();
|
---|
4786 | rc = pMedium->i_queryInfo(false /* fSetImageId */, false /* fSetParentId */,
|
---|
4787 | autoCaller);
|
---|
4788 | alock.acquire();
|
---|
4789 | if (FAILED(rc)) return rc;
|
---|
4790 |
|
---|
4791 | mediumState = pMedium->i_getState();
|
---|
4792 | if (mediumState == MediumState_Inaccessible)
|
---|
4793 | {
|
---|
4794 | // ignore inaccessible ISO media and silently return S_OK,
|
---|
4795 | // otherwise VM startup (esp. restore) may fail without good reason
|
---|
4796 | if (!fFailIfInaccessible)
|
---|
4797 | return S_OK;
|
---|
4798 |
|
---|
4799 | // otherwise report an error
|
---|
4800 | Bstr error;
|
---|
4801 | rc = pMedium->COMGETTER(LastAccessError)(error.asOutParam());
|
---|
4802 | if (FAILED(rc)) return rc;
|
---|
4803 |
|
---|
4804 | /* collect multiple errors */
|
---|
4805 | eik.restore();
|
---|
4806 | Assert(!error.isEmpty());
|
---|
4807 | mrc = setError(E_FAIL,
|
---|
4808 | "%ls",
|
---|
4809 | error.raw());
|
---|
4810 | // error message will be something like
|
---|
4811 | // "Could not open the medium ... VD: error VERR_FILE_NOT_FOUND opening image file ... (VERR_FILE_NOT_FOUND).
|
---|
4812 | eik.fetch();
|
---|
4813 | }
|
---|
4814 | }
|
---|
4815 |
|
---|
4816 | if (pMedium == pToLockWrite)
|
---|
4817 | mediumLockList.Prepend(pMedium, true);
|
---|
4818 | else
|
---|
4819 | mediumLockList.Prepend(pMedium, fMediumLockWriteAll);
|
---|
4820 |
|
---|
4821 | pMedium = pMedium->i_getParent();
|
---|
4822 | if (pMedium.isNull() && pToBeParent)
|
---|
4823 | {
|
---|
4824 | pMedium = pToBeParent;
|
---|
4825 | pToBeParent = NULL;
|
---|
4826 | }
|
---|
4827 | }
|
---|
4828 |
|
---|
4829 | return mrc;
|
---|
4830 | }
|
---|
4831 |
|
---|
4832 | /**
|
---|
4833 | * Creates a new differencing storage unit using the format of the given target
|
---|
4834 | * medium and the location. Note that @c aTarget must be NotCreated.
|
---|
4835 | *
|
---|
4836 | * The @a aMediumLockList parameter contains the associated medium lock list,
|
---|
4837 | * which must be in locked state. If @a aWait is @c true then the caller is
|
---|
4838 | * responsible for unlocking.
|
---|
4839 | *
|
---|
4840 | * If @a aProgress is not NULL but the object it points to is @c null then a
|
---|
4841 | * new progress object will be created and assigned to @a *aProgress on
|
---|
4842 | * success, otherwise the existing progress object is used. If @a aProgress is
|
---|
4843 | * NULL, then no progress object is created/used at all.
|
---|
4844 | *
|
---|
4845 | * When @a aWait is @c false, this method will create a thread to perform the
|
---|
4846 | * create operation asynchronously and will return immediately. Otherwise, it
|
---|
4847 | * will perform the operation on the calling thread and will not return to the
|
---|
4848 | * caller until the operation is completed. Note that @a aProgress cannot be
|
---|
4849 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
4850 | *
|
---|
4851 | * @param aTarget Target medium.
|
---|
4852 | * @param aVariant Precise medium variant to create.
|
---|
4853 | * @param aMediumLockList List of media which should be locked.
|
---|
4854 | * @param aProgress Where to find/store a Progress object to track
|
---|
4855 | * operation completion.
|
---|
4856 | * @param aWait @c true if this method should block instead of
|
---|
4857 | * creating an asynchronous thread.
|
---|
4858 | * @param aNotify Notify about mediums which metadatа are changed
|
---|
4859 | * during execution of the function.
|
---|
4860 | *
|
---|
4861 | * @note Locks this object and @a aTarget for writing.
|
---|
4862 | */
|
---|
4863 | HRESULT Medium::i_createDiffStorage(ComObjPtr<Medium> &aTarget,
|
---|
4864 | MediumVariant_T aVariant,
|
---|
4865 | MediumLockList *aMediumLockList,
|
---|
4866 | ComObjPtr<Progress> *aProgress,
|
---|
4867 | bool aWait,
|
---|
4868 | bool aNotify)
|
---|
4869 | {
|
---|
4870 | AssertReturn(!aTarget.isNull(), E_FAIL);
|
---|
4871 | AssertReturn(aMediumLockList, E_FAIL);
|
---|
4872 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
4873 |
|
---|
4874 | AutoCaller autoCaller(this);
|
---|
4875 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4876 |
|
---|
4877 | AutoCaller targetCaller(aTarget);
|
---|
4878 | if (FAILED(targetCaller.rc())) return targetCaller.rc();
|
---|
4879 |
|
---|
4880 | HRESULT rc = S_OK;
|
---|
4881 | ComObjPtr<Progress> pProgress;
|
---|
4882 | Medium::Task *pTask = NULL;
|
---|
4883 |
|
---|
4884 | try
|
---|
4885 | {
|
---|
4886 | AutoMultiWriteLock2 alock(this, aTarget COMMA_LOCKVAL_SRC_POS);
|
---|
4887 |
|
---|
4888 | ComAssertThrow( m->type != MediumType_Writethrough
|
---|
4889 | && m->type != MediumType_Shareable
|
---|
4890 | && m->type != MediumType_Readonly, E_FAIL);
|
---|
4891 | ComAssertThrow(m->state == MediumState_LockedRead, E_FAIL);
|
---|
4892 |
|
---|
4893 | if (aTarget->m->state != MediumState_NotCreated)
|
---|
4894 | throw aTarget->i_setStateError();
|
---|
4895 |
|
---|
4896 | /* Check that the medium is not attached to the current state of
|
---|
4897 | * any VM referring to it. */
|
---|
4898 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
4899 | it != m->backRefs.end();
|
---|
4900 | ++it)
|
---|
4901 | {
|
---|
4902 | if (it->fInCurState)
|
---|
4903 | {
|
---|
4904 | /* Note: when a VM snapshot is being taken, all normal media
|
---|
4905 | * attached to the VM in the current state will be, as an
|
---|
4906 | * exception, also associated with the snapshot which is about
|
---|
4907 | * to create (see SnapshotMachine::init()) before deassociating
|
---|
4908 | * them from the current state (which takes place only on
|
---|
4909 | * success in Machine::fixupHardDisks()), so that the size of
|
---|
4910 | * snapshotIds will be 1 in this case. The extra condition is
|
---|
4911 | * used to filter out this legal situation. */
|
---|
4912 | if (it->llSnapshotIds.size() == 0)
|
---|
4913 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4914 | 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"),
|
---|
4915 | m->strLocationFull.c_str(), it->machineId.raw());
|
---|
4916 |
|
---|
4917 | Assert(it->llSnapshotIds.size() == 1);
|
---|
4918 | }
|
---|
4919 | }
|
---|
4920 |
|
---|
4921 | if (aProgress != NULL)
|
---|
4922 | {
|
---|
4923 | /* use the existing progress object... */
|
---|
4924 | pProgress = *aProgress;
|
---|
4925 |
|
---|
4926 | /* ...but create a new one if it is null */
|
---|
4927 | if (pProgress.isNull())
|
---|
4928 | {
|
---|
4929 | pProgress.createObject();
|
---|
4930 | rc = pProgress->init(m->pVirtualBox,
|
---|
4931 | static_cast<IMedium*>(this),
|
---|
4932 | BstrFmt(tr("Creating differencing medium storage unit '%s'"),
|
---|
4933 | aTarget->m->strLocationFull.c_str()).raw(),
|
---|
4934 | TRUE /* aCancelable */);
|
---|
4935 | if (FAILED(rc))
|
---|
4936 | throw rc;
|
---|
4937 | }
|
---|
4938 | }
|
---|
4939 |
|
---|
4940 | /* setup task object to carry out the operation sync/async */
|
---|
4941 | pTask = new Medium::CreateDiffTask(this, pProgress, aTarget, aVariant,
|
---|
4942 | aMediumLockList,
|
---|
4943 | aWait /* fKeepMediumLockList */,
|
---|
4944 | aNotify);
|
---|
4945 | rc = pTask->rc();
|
---|
4946 | AssertComRC(rc);
|
---|
4947 | if (FAILED(rc))
|
---|
4948 | throw rc;
|
---|
4949 |
|
---|
4950 | /* register a task (it will deregister itself when done) */
|
---|
4951 | ++m->numCreateDiffTasks;
|
---|
4952 | Assert(m->numCreateDiffTasks != 0); /* overflow? */
|
---|
4953 |
|
---|
4954 | aTarget->m->state = MediumState_Creating;
|
---|
4955 | }
|
---|
4956 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4957 |
|
---|
4958 | if (SUCCEEDED(rc))
|
---|
4959 | {
|
---|
4960 | if (aWait)
|
---|
4961 | {
|
---|
4962 | rc = pTask->runNow();
|
---|
4963 |
|
---|
4964 | delete pTask;
|
---|
4965 | }
|
---|
4966 | else
|
---|
4967 | rc = pTask->createThread();
|
---|
4968 |
|
---|
4969 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
4970 | *aProgress = pProgress;
|
---|
4971 | }
|
---|
4972 | else if (pTask != NULL)
|
---|
4973 | delete pTask;
|
---|
4974 |
|
---|
4975 | return rc;
|
---|
4976 | }
|
---|
4977 |
|
---|
4978 | /**
|
---|
4979 | * Returns a preferred format for differencing media.
|
---|
4980 | */
|
---|
4981 | Utf8Str Medium::i_getPreferredDiffFormat()
|
---|
4982 | {
|
---|
4983 | AutoCaller autoCaller(this);
|
---|
4984 | AssertComRCReturn(autoCaller.rc(), Utf8Str::Empty);
|
---|
4985 |
|
---|
4986 | /* check that our own format supports diffs */
|
---|
4987 | if (!(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_Differencing))
|
---|
4988 | {
|
---|
4989 | /* use the default format if not */
|
---|
4990 | Utf8Str tmp;
|
---|
4991 | m->pVirtualBox->i_getDefaultHardDiskFormat(tmp);
|
---|
4992 | return tmp;
|
---|
4993 | }
|
---|
4994 |
|
---|
4995 | /* m->strFormat is const, no need to lock */
|
---|
4996 | return m->strFormat;
|
---|
4997 | }
|
---|
4998 |
|
---|
4999 | /**
|
---|
5000 | * Returns a preferred variant for differencing media.
|
---|
5001 | */
|
---|
5002 | MediumVariant_T Medium::i_getPreferredDiffVariant()
|
---|
5003 | {
|
---|
5004 | AutoCaller autoCaller(this);
|
---|
5005 | AssertComRCReturn(autoCaller.rc(), MediumVariant_Standard);
|
---|
5006 |
|
---|
5007 | /* check that our own format supports diffs */
|
---|
5008 | if (!(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_Differencing))
|
---|
5009 | return MediumVariant_Standard;
|
---|
5010 |
|
---|
5011 | /* m->variant is const, no need to lock */
|
---|
5012 | ULONG mediumVariantFlags = (ULONG)m->variant;
|
---|
5013 | mediumVariantFlags &= ~(MediumVariant_Fixed | MediumVariant_VmdkStreamOptimized);
|
---|
5014 | mediumVariantFlags |= MediumVariant_Diff;
|
---|
5015 | return (MediumVariant_T)mediumVariantFlags;
|
---|
5016 | }
|
---|
5017 |
|
---|
5018 | /**
|
---|
5019 | * Implementation for the public Medium::Close() with the exception of calling
|
---|
5020 | * VirtualBox::saveRegistries(), in case someone wants to call this for several
|
---|
5021 | * media.
|
---|
5022 | *
|
---|
5023 | * After this returns with success, uninit() has been called on the medium, and
|
---|
5024 | * the object is no longer usable ("not ready" state).
|
---|
5025 | *
|
---|
5026 | * @param autoCaller AutoCaller instance which must have been created on the caller's
|
---|
5027 | * stack for this medium. This gets released hereupon
|
---|
5028 | * which the Medium instance gets uninitialized.
|
---|
5029 | * @return
|
---|
5030 | */
|
---|
5031 | HRESULT Medium::i_close(AutoCaller &autoCaller)
|
---|
5032 | {
|
---|
5033 | // must temporarily drop the caller, need the tree lock first
|
---|
5034 | autoCaller.release();
|
---|
5035 |
|
---|
5036 | // we're accessing parent/child and backrefs, so lock the tree first, then ourselves
|
---|
5037 | AutoMultiWriteLock2 multilock(&m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
5038 | this->lockHandle()
|
---|
5039 | COMMA_LOCKVAL_SRC_POS);
|
---|
5040 |
|
---|
5041 | autoCaller.add();
|
---|
5042 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
5043 |
|
---|
5044 | LogFlowFunc(("ENTER for %s\n", i_getLocationFull().c_str()));
|
---|
5045 |
|
---|
5046 | bool wasCreated = true;
|
---|
5047 |
|
---|
5048 | switch (m->state)
|
---|
5049 | {
|
---|
5050 | case MediumState_NotCreated:
|
---|
5051 | wasCreated = false;
|
---|
5052 | break;
|
---|
5053 | case MediumState_Created:
|
---|
5054 | case MediumState_Inaccessible:
|
---|
5055 | break;
|
---|
5056 | default:
|
---|
5057 | return i_setStateError();
|
---|
5058 | }
|
---|
5059 |
|
---|
5060 | if (m->backRefs.size() != 0)
|
---|
5061 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
5062 | tr("Medium '%s' cannot be closed because it is still attached to %d virtual machines"),
|
---|
5063 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
5064 |
|
---|
5065 | // perform extra media-dependent close checks
|
---|
5066 | HRESULT rc = i_canClose();
|
---|
5067 | if (FAILED(rc)) return rc;
|
---|
5068 |
|
---|
5069 | m->fClosing = true;
|
---|
5070 |
|
---|
5071 | if (wasCreated)
|
---|
5072 | {
|
---|
5073 | // remove from the list of known media before performing actual
|
---|
5074 | // uninitialization (to keep the media registry consistent on
|
---|
5075 | // failure to do so)
|
---|
5076 | rc = i_unregisterWithVirtualBox();
|
---|
5077 | if (FAILED(rc)) return rc;
|
---|
5078 |
|
---|
5079 | multilock.release();
|
---|
5080 | // Release the AutoCaller now, as otherwise uninit() will simply hang.
|
---|
5081 | // Needs to be done before mark the registries as modified and saving
|
---|
5082 | // the registry, as otherwise there may be a deadlock with someone else
|
---|
5083 | // closing this object while we're in i_saveModifiedRegistries(), which
|
---|
5084 | // needs the media tree lock, which the other thread holds until after
|
---|
5085 | // uninit() below.
|
---|
5086 | autoCaller.release();
|
---|
5087 | i_markRegistriesModified();
|
---|
5088 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
5089 | }
|
---|
5090 | else
|
---|
5091 | {
|
---|
5092 | multilock.release();
|
---|
5093 | // release the AutoCaller, as otherwise uninit() will simply hang
|
---|
5094 | autoCaller.release();
|
---|
5095 | }
|
---|
5096 |
|
---|
5097 | // Keep the locks held until after uninit, as otherwise the consistency
|
---|
5098 | // of the medium tree cannot be guaranteed.
|
---|
5099 | uninit();
|
---|
5100 |
|
---|
5101 | LogFlowFuncLeave();
|
---|
5102 |
|
---|
5103 | return rc;
|
---|
5104 | }
|
---|
5105 |
|
---|
5106 | /**
|
---|
5107 | * Deletes the medium storage unit.
|
---|
5108 | *
|
---|
5109 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
5110 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
5111 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
5112 | * progress object is created/used at all.
|
---|
5113 | *
|
---|
5114 | * When @a aWait is @c false, this method will create a thread to perform the
|
---|
5115 | * delete operation asynchronously and will return immediately. Otherwise, it
|
---|
5116 | * will perform the operation on the calling thread and will not return to the
|
---|
5117 | * caller until the operation is completed. Note that @a aProgress cannot be
|
---|
5118 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
5119 | *
|
---|
5120 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
5121 | * completion.
|
---|
5122 | * @param aWait @c true if this method should block instead of creating
|
---|
5123 | * an asynchronous thread.
|
---|
5124 | * @param aNotify Notify about mediums which metadatа are changed
|
---|
5125 | * during execution of the function.
|
---|
5126 | *
|
---|
5127 | * @note Locks mVirtualBox and this object for writing. Locks medium tree for
|
---|
5128 | * writing.
|
---|
5129 | */
|
---|
5130 | HRESULT Medium::i_deleteStorage(ComObjPtr<Progress> *aProgress,
|
---|
5131 | bool aWait, bool aNotify)
|
---|
5132 | {
|
---|
5133 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
5134 |
|
---|
5135 | HRESULT rc = S_OK;
|
---|
5136 | ComObjPtr<Progress> pProgress;
|
---|
5137 | Medium::Task *pTask = NULL;
|
---|
5138 |
|
---|
5139 | try
|
---|
5140 | {
|
---|
5141 | /* we're accessing the media tree, and canClose() needs it too */
|
---|
5142 | AutoWriteLock treelock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5143 |
|
---|
5144 | AutoCaller autoCaller(this);
|
---|
5145 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5146 |
|
---|
5147 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5148 |
|
---|
5149 | LogFlowThisFunc(("aWait=%RTbool locationFull=%s\n", aWait, i_getLocationFull().c_str() ));
|
---|
5150 |
|
---|
5151 | if ( !(m->formatObj->i_getCapabilities() & ( MediumFormatCapabilities_CreateDynamic
|
---|
5152 | | MediumFormatCapabilities_CreateFixed)))
|
---|
5153 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
5154 | tr("Medium format '%s' does not support storage deletion"),
|
---|
5155 | m->strFormat.c_str());
|
---|
5156 |
|
---|
5157 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
5158 | /** @todo r=klaus would be great if this could be moved to the async
|
---|
5159 | * part of the operation as it can take quite a while */
|
---|
5160 | if (m->queryInfoRunning)
|
---|
5161 | {
|
---|
5162 | while (m->queryInfoRunning)
|
---|
5163 | {
|
---|
5164 | alock.release();
|
---|
5165 | autoCaller.release();
|
---|
5166 | treelock.release();
|
---|
5167 | /* Must not hold the media tree lock or the object lock, as
|
---|
5168 | * Medium::i_queryInfo needs this lock and thus we would run
|
---|
5169 | * into a deadlock here. */
|
---|
5170 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
5171 | Assert(!isWriteLockOnCurrentThread());
|
---|
5172 | {
|
---|
5173 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
5174 | }
|
---|
5175 | treelock.acquire();
|
---|
5176 | autoCaller.add();
|
---|
5177 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5178 | alock.acquire();
|
---|
5179 | }
|
---|
5180 | }
|
---|
5181 |
|
---|
5182 | /* Note that we are fine with Inaccessible state too: a) for symmetry
|
---|
5183 | * with create calls and b) because it doesn't really harm to try, if
|
---|
5184 | * it is really inaccessible, the delete operation will fail anyway.
|
---|
5185 | * Accepting Inaccessible state is especially important because all
|
---|
5186 | * registered media are initially Inaccessible upon VBoxSVC startup
|
---|
5187 | * until COMGETTER(RefreshState) is called. Accept Deleting state
|
---|
5188 | * because some callers need to put the medium in this state early
|
---|
5189 | * to prevent races. */
|
---|
5190 | switch (m->state)
|
---|
5191 | {
|
---|
5192 | case MediumState_Created:
|
---|
5193 | case MediumState_Deleting:
|
---|
5194 | case MediumState_Inaccessible:
|
---|
5195 | break;
|
---|
5196 | default:
|
---|
5197 | throw i_setStateError();
|
---|
5198 | }
|
---|
5199 |
|
---|
5200 | if (m->backRefs.size() != 0)
|
---|
5201 | {
|
---|
5202 | Utf8Str strMachines;
|
---|
5203 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
5204 | it != m->backRefs.end();
|
---|
5205 | ++it)
|
---|
5206 | {
|
---|
5207 | const BackRef &b = *it;
|
---|
5208 | if (strMachines.length())
|
---|
5209 | strMachines.append(", ");
|
---|
5210 | strMachines.append(b.machineId.toString().c_str());
|
---|
5211 | }
|
---|
5212 | #ifdef DEBUG
|
---|
5213 | i_dumpBackRefs();
|
---|
5214 | #endif
|
---|
5215 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5216 | tr("Cannot delete storage: medium '%s' is still attached to the following %d virtual machine(s): %s"),
|
---|
5217 | m->strLocationFull.c_str(),
|
---|
5218 | m->backRefs.size(),
|
---|
5219 | strMachines.c_str());
|
---|
5220 | }
|
---|
5221 |
|
---|
5222 | rc = i_canClose();
|
---|
5223 | if (FAILED(rc))
|
---|
5224 | throw rc;
|
---|
5225 |
|
---|
5226 | /* go to Deleting state, so that the medium is not actually locked */
|
---|
5227 | if (m->state != MediumState_Deleting)
|
---|
5228 | {
|
---|
5229 | rc = i_markForDeletion();
|
---|
5230 | if (FAILED(rc))
|
---|
5231 | throw rc;
|
---|
5232 | }
|
---|
5233 |
|
---|
5234 | /* Build the medium lock list. */
|
---|
5235 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
5236 | alock.release();
|
---|
5237 | autoCaller.release();
|
---|
5238 | treelock.release();
|
---|
5239 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5240 | this /* pToLockWrite */,
|
---|
5241 | false /* fMediumLockWriteAll */,
|
---|
5242 | NULL,
|
---|
5243 | *pMediumLockList);
|
---|
5244 | treelock.acquire();
|
---|
5245 | autoCaller.add();
|
---|
5246 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5247 | alock.acquire();
|
---|
5248 | if (FAILED(rc))
|
---|
5249 | {
|
---|
5250 | delete pMediumLockList;
|
---|
5251 | throw rc;
|
---|
5252 | }
|
---|
5253 |
|
---|
5254 | alock.release();
|
---|
5255 | autoCaller.release();
|
---|
5256 | treelock.release();
|
---|
5257 | rc = pMediumLockList->Lock();
|
---|
5258 | treelock.acquire();
|
---|
5259 | autoCaller.add();
|
---|
5260 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5261 | alock.acquire();
|
---|
5262 | if (FAILED(rc))
|
---|
5263 | {
|
---|
5264 | delete pMediumLockList;
|
---|
5265 | throw setError(rc,
|
---|
5266 | tr("Failed to lock media when deleting '%s'"),
|
---|
5267 | i_getLocationFull().c_str());
|
---|
5268 | }
|
---|
5269 |
|
---|
5270 | /* try to remove from the list of known media before performing
|
---|
5271 | * actual deletion (we favor the consistency of the media registry
|
---|
5272 | * which would have been broken if unregisterWithVirtualBox() failed
|
---|
5273 | * after we successfully deleted the storage) */
|
---|
5274 | rc = i_unregisterWithVirtualBox();
|
---|
5275 | if (FAILED(rc))
|
---|
5276 | throw rc;
|
---|
5277 | // no longer need lock
|
---|
5278 | alock.release();
|
---|
5279 | autoCaller.release();
|
---|
5280 | treelock.release();
|
---|
5281 | i_markRegistriesModified();
|
---|
5282 |
|
---|
5283 | if (aProgress != NULL)
|
---|
5284 | {
|
---|
5285 | /* use the existing progress object... */
|
---|
5286 | pProgress = *aProgress;
|
---|
5287 |
|
---|
5288 | /* ...but create a new one if it is null */
|
---|
5289 | if (pProgress.isNull())
|
---|
5290 | {
|
---|
5291 | pProgress.createObject();
|
---|
5292 | rc = pProgress->init(m->pVirtualBox,
|
---|
5293 | static_cast<IMedium*>(this),
|
---|
5294 | BstrFmt(tr("Deleting medium storage unit '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
5295 | FALSE /* aCancelable */);
|
---|
5296 | if (FAILED(rc))
|
---|
5297 | throw rc;
|
---|
5298 | }
|
---|
5299 | }
|
---|
5300 |
|
---|
5301 | /* setup task object to carry out the operation sync/async */
|
---|
5302 | pTask = new Medium::DeleteTask(this, pProgress, pMediumLockList, false, aNotify);
|
---|
5303 | rc = pTask->rc();
|
---|
5304 | AssertComRC(rc);
|
---|
5305 | if (FAILED(rc))
|
---|
5306 | throw rc;
|
---|
5307 | }
|
---|
5308 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5309 |
|
---|
5310 | if (SUCCEEDED(rc))
|
---|
5311 | {
|
---|
5312 | if (aWait)
|
---|
5313 | {
|
---|
5314 | rc = pTask->runNow();
|
---|
5315 |
|
---|
5316 | delete pTask;
|
---|
5317 | }
|
---|
5318 | else
|
---|
5319 | rc = pTask->createThread();
|
---|
5320 |
|
---|
5321 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
5322 | *aProgress = pProgress;
|
---|
5323 |
|
---|
5324 | }
|
---|
5325 | else
|
---|
5326 | {
|
---|
5327 | if (pTask)
|
---|
5328 | delete pTask;
|
---|
5329 |
|
---|
5330 | /* Undo deleting state if necessary. */
|
---|
5331 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5332 | /* Make sure that any error signalled by unmarkForDeletion() is not
|
---|
5333 | * ending up in the error list (if the caller uses MultiResult). It
|
---|
5334 | * usually is spurious, as in most cases the medium hasn't been marked
|
---|
5335 | * for deletion when the error was thrown above. */
|
---|
5336 | ErrorInfoKeeper eik;
|
---|
5337 | i_unmarkForDeletion();
|
---|
5338 | }
|
---|
5339 |
|
---|
5340 | return rc;
|
---|
5341 | }
|
---|
5342 |
|
---|
5343 | /**
|
---|
5344 | * Mark a medium for deletion.
|
---|
5345 | *
|
---|
5346 | * @note Caller must hold the write lock on this medium!
|
---|
5347 | */
|
---|
5348 | HRESULT Medium::i_markForDeletion()
|
---|
5349 | {
|
---|
5350 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5351 | switch (m->state)
|
---|
5352 | {
|
---|
5353 | case MediumState_Created:
|
---|
5354 | case MediumState_Inaccessible:
|
---|
5355 | m->preLockState = m->state;
|
---|
5356 | m->state = MediumState_Deleting;
|
---|
5357 | return S_OK;
|
---|
5358 | default:
|
---|
5359 | return i_setStateError();
|
---|
5360 | }
|
---|
5361 | }
|
---|
5362 |
|
---|
5363 | /**
|
---|
5364 | * Removes the "mark for deletion".
|
---|
5365 | *
|
---|
5366 | * @note Caller must hold the write lock on this medium!
|
---|
5367 | */
|
---|
5368 | HRESULT Medium::i_unmarkForDeletion()
|
---|
5369 | {
|
---|
5370 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5371 | switch (m->state)
|
---|
5372 | {
|
---|
5373 | case MediumState_Deleting:
|
---|
5374 | m->state = m->preLockState;
|
---|
5375 | return S_OK;
|
---|
5376 | default:
|
---|
5377 | return i_setStateError();
|
---|
5378 | }
|
---|
5379 | }
|
---|
5380 |
|
---|
5381 | /**
|
---|
5382 | * Mark a medium for deletion which is in locked state.
|
---|
5383 | *
|
---|
5384 | * @note Caller must hold the write lock on this medium!
|
---|
5385 | */
|
---|
5386 | HRESULT Medium::i_markLockedForDeletion()
|
---|
5387 | {
|
---|
5388 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5389 | if ( ( m->state == MediumState_LockedRead
|
---|
5390 | || m->state == MediumState_LockedWrite)
|
---|
5391 | && m->preLockState == MediumState_Created)
|
---|
5392 | {
|
---|
5393 | m->preLockState = MediumState_Deleting;
|
---|
5394 | return S_OK;
|
---|
5395 | }
|
---|
5396 | else
|
---|
5397 | return i_setStateError();
|
---|
5398 | }
|
---|
5399 |
|
---|
5400 | /**
|
---|
5401 | * Removes the "mark for deletion" for a medium in locked state.
|
---|
5402 | *
|
---|
5403 | * @note Caller must hold the write lock on this medium!
|
---|
5404 | */
|
---|
5405 | HRESULT Medium::i_unmarkLockedForDeletion()
|
---|
5406 | {
|
---|
5407 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5408 | if ( ( m->state == MediumState_LockedRead
|
---|
5409 | || m->state == MediumState_LockedWrite)
|
---|
5410 | && m->preLockState == MediumState_Deleting)
|
---|
5411 | {
|
---|
5412 | m->preLockState = MediumState_Created;
|
---|
5413 | return S_OK;
|
---|
5414 | }
|
---|
5415 | else
|
---|
5416 | return i_setStateError();
|
---|
5417 | }
|
---|
5418 |
|
---|
5419 | /**
|
---|
5420 | * Queries the preferred merge direction from this to the other medium, i.e.
|
---|
5421 | * the one which requires the least amount of I/O and therefore time and
|
---|
5422 | * disk consumption.
|
---|
5423 | *
|
---|
5424 | * @returns Status code.
|
---|
5425 | * @retval E_FAIL in case determining the merge direction fails for some reason,
|
---|
5426 | * for example if getting the size of the media fails. There is no
|
---|
5427 | * error set though and the caller is free to continue to find out
|
---|
5428 | * what was going wrong later. Leaves fMergeForward unset.
|
---|
5429 | * @retval VBOX_E_INVALID_OBJECT_STATE if both media are not related to each other
|
---|
5430 | * An error is set.
|
---|
5431 | * @param pOther The other medium to merge with.
|
---|
5432 | * @param fMergeForward Resulting preferred merge direction (out).
|
---|
5433 | */
|
---|
5434 | HRESULT Medium::i_queryPreferredMergeDirection(const ComObjPtr<Medium> &pOther,
|
---|
5435 | bool &fMergeForward)
|
---|
5436 | {
|
---|
5437 | AssertReturn(pOther != NULL, E_FAIL);
|
---|
5438 | AssertReturn(pOther != this, E_FAIL);
|
---|
5439 |
|
---|
5440 | HRESULT rc = S_OK;
|
---|
5441 | bool fThisParent = false; /**<< Flag whether this medium is the parent of pOther. */
|
---|
5442 |
|
---|
5443 | try
|
---|
5444 | {
|
---|
5445 | // locking: we need the tree lock first because we access parent pointers
|
---|
5446 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5447 |
|
---|
5448 | AutoCaller autoCaller(this);
|
---|
5449 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5450 |
|
---|
5451 | AutoCaller otherCaller(pOther);
|
---|
5452 | AssertComRCThrowRC(otherCaller.rc());
|
---|
5453 |
|
---|
5454 | /* more sanity checking and figuring out the current merge direction */
|
---|
5455 | ComObjPtr<Medium> pMedium = i_getParent();
|
---|
5456 | while (!pMedium.isNull() && pMedium != pOther)
|
---|
5457 | pMedium = pMedium->i_getParent();
|
---|
5458 | if (pMedium == pOther)
|
---|
5459 | fThisParent = false;
|
---|
5460 | else
|
---|
5461 | {
|
---|
5462 | pMedium = pOther->i_getParent();
|
---|
5463 | while (!pMedium.isNull() && pMedium != this)
|
---|
5464 | pMedium = pMedium->i_getParent();
|
---|
5465 | if (pMedium == this)
|
---|
5466 | fThisParent = true;
|
---|
5467 | else
|
---|
5468 | {
|
---|
5469 | Utf8Str tgtLoc;
|
---|
5470 | {
|
---|
5471 | AutoReadLock alock(pOther COMMA_LOCKVAL_SRC_POS);
|
---|
5472 | tgtLoc = pOther->i_getLocationFull();
|
---|
5473 | }
|
---|
5474 |
|
---|
5475 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5476 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5477 | tr("Media '%s' and '%s' are unrelated"),
|
---|
5478 | m->strLocationFull.c_str(), tgtLoc.c_str());
|
---|
5479 | }
|
---|
5480 | }
|
---|
5481 |
|
---|
5482 | /*
|
---|
5483 | * Figure out the preferred merge direction. The current way is to
|
---|
5484 | * get the current sizes of file based images and select the merge
|
---|
5485 | * direction depending on the size.
|
---|
5486 | *
|
---|
5487 | * Can't use the VD API to get current size here as the media might
|
---|
5488 | * be write locked by a running VM. Resort to RTFileQuerySize().
|
---|
5489 | */
|
---|
5490 | int vrc = VINF_SUCCESS;
|
---|
5491 | uint64_t cbMediumThis = 0;
|
---|
5492 | uint64_t cbMediumOther = 0;
|
---|
5493 |
|
---|
5494 | if (i_isMediumFormatFile() && pOther->i_isMediumFormatFile())
|
---|
5495 | {
|
---|
5496 | vrc = RTFileQuerySize(this->i_getLocationFull().c_str(), &cbMediumThis);
|
---|
5497 | if (RT_SUCCESS(vrc))
|
---|
5498 | {
|
---|
5499 | vrc = RTFileQuerySize(pOther->i_getLocationFull().c_str(),
|
---|
5500 | &cbMediumOther);
|
---|
5501 | }
|
---|
5502 |
|
---|
5503 | if (RT_FAILURE(vrc))
|
---|
5504 | rc = E_FAIL;
|
---|
5505 | else
|
---|
5506 | {
|
---|
5507 | /*
|
---|
5508 | * Check which merge direction might be more optimal.
|
---|
5509 | * This method is not bullet proof of course as there might
|
---|
5510 | * be overlapping blocks in the images so the file size is
|
---|
5511 | * not the best indicator but it is good enough for our purpose
|
---|
5512 | * and everything else is too complicated, especially when the
|
---|
5513 | * media are used by a running VM.
|
---|
5514 | */
|
---|
5515 |
|
---|
5516 | uint32_t mediumVariants = MediumVariant_Fixed | MediumVariant_VmdkStreamOptimized;
|
---|
5517 | uint32_t mediumCaps = MediumFormatCapabilities_CreateDynamic | MediumFormatCapabilities_File;
|
---|
5518 |
|
---|
5519 | bool fDynamicOther = pOther->i_getMediumFormat()->i_getCapabilities() & mediumCaps
|
---|
5520 | && pOther->i_getVariant() & ~mediumVariants;
|
---|
5521 | bool fDynamicThis = i_getMediumFormat()->i_getCapabilities() & mediumCaps
|
---|
5522 | && i_getVariant() & ~mediumVariants;
|
---|
5523 | bool fMergeIntoThis = (fDynamicThis && !fDynamicOther)
|
---|
5524 | || (fDynamicThis == fDynamicOther && cbMediumThis > cbMediumOther);
|
---|
5525 | fMergeForward = fMergeIntoThis != fThisParent;
|
---|
5526 | }
|
---|
5527 | }
|
---|
5528 | }
|
---|
5529 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5530 |
|
---|
5531 | return rc;
|
---|
5532 | }
|
---|
5533 |
|
---|
5534 | /**
|
---|
5535 | * Prepares this (source) medium, target medium and all intermediate media
|
---|
5536 | * for the merge operation.
|
---|
5537 | *
|
---|
5538 | * This method is to be called prior to calling the #mergeTo() to perform
|
---|
5539 | * necessary consistency checks and place involved media to appropriate
|
---|
5540 | * states. If #mergeTo() is not called or fails, the state modifications
|
---|
5541 | * performed by this method must be undone by #i_cancelMergeTo().
|
---|
5542 | *
|
---|
5543 | * See #mergeTo() for more information about merging.
|
---|
5544 | *
|
---|
5545 | * @param pTarget Target medium.
|
---|
5546 | * @param aMachineId Allowed machine attachment. NULL means do not check.
|
---|
5547 | * @param aSnapshotId Allowed snapshot attachment. NULL or empty UUID means
|
---|
5548 | * do not check.
|
---|
5549 | * @param fLockMedia Flag whether to lock the medium lock list or not.
|
---|
5550 | * If set to false and the medium lock list locking fails
|
---|
5551 | * later you must call #i_cancelMergeTo().
|
---|
5552 | * @param fMergeForward Resulting merge direction (out).
|
---|
5553 | * @param pParentForTarget New parent for target medium after merge (out).
|
---|
5554 | * @param aChildrenToReparent Medium lock list containing all children of the
|
---|
5555 | * source which will have to be reparented to the target
|
---|
5556 | * after merge (out).
|
---|
5557 | * @param aMediumLockList Medium locking information (out).
|
---|
5558 | *
|
---|
5559 | * @note Locks medium tree for reading. Locks this object, aTarget and all
|
---|
5560 | * intermediate media for writing.
|
---|
5561 | */
|
---|
5562 | HRESULT Medium::i_prepareMergeTo(const ComObjPtr<Medium> &pTarget,
|
---|
5563 | const Guid *aMachineId,
|
---|
5564 | const Guid *aSnapshotId,
|
---|
5565 | bool fLockMedia,
|
---|
5566 | bool &fMergeForward,
|
---|
5567 | ComObjPtr<Medium> &pParentForTarget,
|
---|
5568 | MediumLockList * &aChildrenToReparent,
|
---|
5569 | MediumLockList * &aMediumLockList)
|
---|
5570 | {
|
---|
5571 | AssertReturn(pTarget != NULL, E_FAIL);
|
---|
5572 | AssertReturn(pTarget != this, E_FAIL);
|
---|
5573 |
|
---|
5574 | HRESULT rc = S_OK;
|
---|
5575 | fMergeForward = false;
|
---|
5576 | pParentForTarget.setNull();
|
---|
5577 | Assert(aChildrenToReparent == NULL);
|
---|
5578 | aChildrenToReparent = NULL;
|
---|
5579 | Assert(aMediumLockList == NULL);
|
---|
5580 | aMediumLockList = NULL;
|
---|
5581 |
|
---|
5582 | try
|
---|
5583 | {
|
---|
5584 | // locking: we need the tree lock first because we access parent pointers
|
---|
5585 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5586 |
|
---|
5587 | AutoCaller autoCaller(this);
|
---|
5588 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5589 |
|
---|
5590 | AutoCaller targetCaller(pTarget);
|
---|
5591 | AssertComRCThrowRC(targetCaller.rc());
|
---|
5592 |
|
---|
5593 | /* more sanity checking and figuring out the merge direction */
|
---|
5594 | ComObjPtr<Medium> pMedium = i_getParent();
|
---|
5595 | while (!pMedium.isNull() && pMedium != pTarget)
|
---|
5596 | pMedium = pMedium->i_getParent();
|
---|
5597 | if (pMedium == pTarget)
|
---|
5598 | fMergeForward = false;
|
---|
5599 | else
|
---|
5600 | {
|
---|
5601 | pMedium = pTarget->i_getParent();
|
---|
5602 | while (!pMedium.isNull() && pMedium != this)
|
---|
5603 | pMedium = pMedium->i_getParent();
|
---|
5604 | if (pMedium == this)
|
---|
5605 | fMergeForward = true;
|
---|
5606 | else
|
---|
5607 | {
|
---|
5608 | Utf8Str tgtLoc;
|
---|
5609 | {
|
---|
5610 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5611 | tgtLoc = pTarget->i_getLocationFull();
|
---|
5612 | }
|
---|
5613 |
|
---|
5614 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5615 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5616 | tr("Media '%s' and '%s' are unrelated"),
|
---|
5617 | m->strLocationFull.c_str(), tgtLoc.c_str());
|
---|
5618 | }
|
---|
5619 | }
|
---|
5620 |
|
---|
5621 | /* Build the lock list. */
|
---|
5622 | aMediumLockList = new MediumLockList();
|
---|
5623 | targetCaller.release();
|
---|
5624 | autoCaller.release();
|
---|
5625 | treeLock.release();
|
---|
5626 | if (fMergeForward)
|
---|
5627 | rc = pTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5628 | pTarget /* pToLockWrite */,
|
---|
5629 | false /* fMediumLockWriteAll */,
|
---|
5630 | NULL,
|
---|
5631 | *aMediumLockList);
|
---|
5632 | else
|
---|
5633 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5634 | pTarget /* pToLockWrite */,
|
---|
5635 | false /* fMediumLockWriteAll */,
|
---|
5636 | NULL,
|
---|
5637 | *aMediumLockList);
|
---|
5638 | treeLock.acquire();
|
---|
5639 | autoCaller.add();
|
---|
5640 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5641 | targetCaller.add();
|
---|
5642 | AssertComRCThrowRC(targetCaller.rc());
|
---|
5643 | if (FAILED(rc))
|
---|
5644 | throw rc;
|
---|
5645 |
|
---|
5646 | /* Sanity checking, must be after lock list creation as it depends on
|
---|
5647 | * valid medium states. The medium objects must be accessible. Only
|
---|
5648 | * do this if immediate locking is requested, otherwise it fails when
|
---|
5649 | * we construct a medium lock list for an already running VM. Snapshot
|
---|
5650 | * deletion uses this to simplify its life. */
|
---|
5651 | if (fLockMedia)
|
---|
5652 | {
|
---|
5653 | {
|
---|
5654 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5655 | if (m->state != MediumState_Created)
|
---|
5656 | throw i_setStateError();
|
---|
5657 | }
|
---|
5658 | {
|
---|
5659 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5660 | if (pTarget->m->state != MediumState_Created)
|
---|
5661 | throw pTarget->i_setStateError();
|
---|
5662 | }
|
---|
5663 | }
|
---|
5664 |
|
---|
5665 | /* check medium attachment and other sanity conditions */
|
---|
5666 | if (fMergeForward)
|
---|
5667 | {
|
---|
5668 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5669 | if (i_getChildren().size() > 1)
|
---|
5670 | {
|
---|
5671 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5672 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
5673 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
5674 | }
|
---|
5675 | /* One backreference is only allowed if the machine ID is not empty
|
---|
5676 | * and it matches the machine the medium is attached to (including
|
---|
5677 | * the snapshot ID if not empty). */
|
---|
5678 | if ( m->backRefs.size() != 0
|
---|
5679 | && ( !aMachineId
|
---|
5680 | || m->backRefs.size() != 1
|
---|
5681 | || aMachineId->isZero()
|
---|
5682 | || *i_getFirstMachineBackrefId() != *aMachineId
|
---|
5683 | || ( (!aSnapshotId || !aSnapshotId->isZero())
|
---|
5684 | && *i_getFirstMachineBackrefSnapshotId() != *aSnapshotId)))
|
---|
5685 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5686 | tr("Medium '%s' is attached to %d virtual machines"),
|
---|
5687 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
5688 | if (m->type == MediumType_Immutable)
|
---|
5689 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5690 | tr("Medium '%s' is immutable"),
|
---|
5691 | m->strLocationFull.c_str());
|
---|
5692 | if (m->type == MediumType_MultiAttach)
|
---|
5693 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5694 | tr("Medium '%s' is multi-attach"),
|
---|
5695 | m->strLocationFull.c_str());
|
---|
5696 | }
|
---|
5697 | else
|
---|
5698 | {
|
---|
5699 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5700 | if (pTarget->i_getChildren().size() > 1)
|
---|
5701 | {
|
---|
5702 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5703 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
5704 | pTarget->m->strLocationFull.c_str(),
|
---|
5705 | pTarget->i_getChildren().size());
|
---|
5706 | }
|
---|
5707 | if (pTarget->m->type == MediumType_Immutable)
|
---|
5708 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5709 | tr("Medium '%s' is immutable"),
|
---|
5710 | pTarget->m->strLocationFull.c_str());
|
---|
5711 | if (pTarget->m->type == MediumType_MultiAttach)
|
---|
5712 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5713 | tr("Medium '%s' is multi-attach"),
|
---|
5714 | pTarget->m->strLocationFull.c_str());
|
---|
5715 | }
|
---|
5716 | ComObjPtr<Medium> pLast(fMergeForward ? (Medium *)pTarget : this);
|
---|
5717 | ComObjPtr<Medium> pLastIntermediate = pLast->i_getParent();
|
---|
5718 | for (pLast = pLastIntermediate;
|
---|
5719 | !pLast.isNull() && pLast != pTarget && pLast != this;
|
---|
5720 | pLast = pLast->i_getParent())
|
---|
5721 | {
|
---|
5722 | AutoReadLock alock(pLast COMMA_LOCKVAL_SRC_POS);
|
---|
5723 | if (pLast->i_getChildren().size() > 1)
|
---|
5724 | {
|
---|
5725 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5726 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
5727 | pLast->m->strLocationFull.c_str(),
|
---|
5728 | pLast->i_getChildren().size());
|
---|
5729 | }
|
---|
5730 | if (pLast->m->backRefs.size() != 0)
|
---|
5731 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5732 | tr("Medium '%s' is attached to %d virtual machines"),
|
---|
5733 | pLast->m->strLocationFull.c_str(),
|
---|
5734 | pLast->m->backRefs.size());
|
---|
5735 |
|
---|
5736 | }
|
---|
5737 |
|
---|
5738 | /* Update medium states appropriately */
|
---|
5739 | {
|
---|
5740 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5741 |
|
---|
5742 | if (m->state == MediumState_Created)
|
---|
5743 | {
|
---|
5744 | rc = i_markForDeletion();
|
---|
5745 | if (FAILED(rc))
|
---|
5746 | throw rc;
|
---|
5747 | }
|
---|
5748 | else
|
---|
5749 | {
|
---|
5750 | if (fLockMedia)
|
---|
5751 | throw i_setStateError();
|
---|
5752 | else if ( m->state == MediumState_LockedWrite
|
---|
5753 | || m->state == MediumState_LockedRead)
|
---|
5754 | {
|
---|
5755 | /* Either mark it for deletion in locked state or allow
|
---|
5756 | * others to have done so. */
|
---|
5757 | if (m->preLockState == MediumState_Created)
|
---|
5758 | i_markLockedForDeletion();
|
---|
5759 | else if (m->preLockState != MediumState_Deleting)
|
---|
5760 | throw i_setStateError();
|
---|
5761 | }
|
---|
5762 | else
|
---|
5763 | throw i_setStateError();
|
---|
5764 | }
|
---|
5765 | }
|
---|
5766 |
|
---|
5767 | if (fMergeForward)
|
---|
5768 | {
|
---|
5769 | /* we will need parent to reparent target */
|
---|
5770 | pParentForTarget = i_getParent();
|
---|
5771 | }
|
---|
5772 | else
|
---|
5773 | {
|
---|
5774 | /* we will need to reparent children of the source */
|
---|
5775 | aChildrenToReparent = new MediumLockList();
|
---|
5776 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
5777 | it != i_getChildren().end();
|
---|
5778 | ++it)
|
---|
5779 | {
|
---|
5780 | pMedium = *it;
|
---|
5781 | aChildrenToReparent->Append(pMedium, true /* fLockWrite */);
|
---|
5782 | }
|
---|
5783 | if (fLockMedia && aChildrenToReparent)
|
---|
5784 | {
|
---|
5785 | targetCaller.release();
|
---|
5786 | autoCaller.release();
|
---|
5787 | treeLock.release();
|
---|
5788 | rc = aChildrenToReparent->Lock();
|
---|
5789 | treeLock.acquire();
|
---|
5790 | autoCaller.add();
|
---|
5791 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5792 | targetCaller.add();
|
---|
5793 | AssertComRCThrowRC(targetCaller.rc());
|
---|
5794 | if (FAILED(rc))
|
---|
5795 | throw rc;
|
---|
5796 | }
|
---|
5797 | }
|
---|
5798 | for (pLast = pLastIntermediate;
|
---|
5799 | !pLast.isNull() && pLast != pTarget && pLast != this;
|
---|
5800 | pLast = pLast->i_getParent())
|
---|
5801 | {
|
---|
5802 | AutoWriteLock alock(pLast COMMA_LOCKVAL_SRC_POS);
|
---|
5803 | if (pLast->m->state == MediumState_Created)
|
---|
5804 | {
|
---|
5805 | rc = pLast->i_markForDeletion();
|
---|
5806 | if (FAILED(rc))
|
---|
5807 | throw rc;
|
---|
5808 | }
|
---|
5809 | else
|
---|
5810 | throw pLast->i_setStateError();
|
---|
5811 | }
|
---|
5812 |
|
---|
5813 | /* Tweak the lock list in the backward merge case, as the target
|
---|
5814 | * isn't marked to be locked for writing yet. */
|
---|
5815 | if (!fMergeForward)
|
---|
5816 | {
|
---|
5817 | MediumLockList::Base::iterator lockListBegin =
|
---|
5818 | aMediumLockList->GetBegin();
|
---|
5819 | MediumLockList::Base::iterator lockListEnd =
|
---|
5820 | aMediumLockList->GetEnd();
|
---|
5821 | ++lockListEnd;
|
---|
5822 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
5823 | it != lockListEnd;
|
---|
5824 | ++it)
|
---|
5825 | {
|
---|
5826 | MediumLock &mediumLock = *it;
|
---|
5827 | if (mediumLock.GetMedium() == pTarget)
|
---|
5828 | {
|
---|
5829 | HRESULT rc2 = mediumLock.UpdateLock(true);
|
---|
5830 | AssertComRC(rc2);
|
---|
5831 | break;
|
---|
5832 | }
|
---|
5833 | }
|
---|
5834 | }
|
---|
5835 |
|
---|
5836 | if (fLockMedia)
|
---|
5837 | {
|
---|
5838 | targetCaller.release();
|
---|
5839 | autoCaller.release();
|
---|
5840 | treeLock.release();
|
---|
5841 | rc = aMediumLockList->Lock();
|
---|
5842 | treeLock.acquire();
|
---|
5843 | autoCaller.add();
|
---|
5844 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5845 | targetCaller.add();
|
---|
5846 | AssertComRCThrowRC(targetCaller.rc());
|
---|
5847 | if (FAILED(rc))
|
---|
5848 | {
|
---|
5849 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5850 | throw setError(rc,
|
---|
5851 | tr("Failed to lock media when merging to '%s'"),
|
---|
5852 | pTarget->i_getLocationFull().c_str());
|
---|
5853 | }
|
---|
5854 | }
|
---|
5855 | }
|
---|
5856 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5857 |
|
---|
5858 | if (FAILED(rc))
|
---|
5859 | {
|
---|
5860 | if (aMediumLockList)
|
---|
5861 | {
|
---|
5862 | delete aMediumLockList;
|
---|
5863 | aMediumLockList = NULL;
|
---|
5864 | }
|
---|
5865 | if (aChildrenToReparent)
|
---|
5866 | {
|
---|
5867 | delete aChildrenToReparent;
|
---|
5868 | aChildrenToReparent = NULL;
|
---|
5869 | }
|
---|
5870 | }
|
---|
5871 |
|
---|
5872 | return rc;
|
---|
5873 | }
|
---|
5874 |
|
---|
5875 | /**
|
---|
5876 | * Merges this medium to the specified medium which must be either its
|
---|
5877 | * direct ancestor or descendant.
|
---|
5878 | *
|
---|
5879 | * Given this medium is SOURCE and the specified medium is TARGET, we will
|
---|
5880 | * get two variants of the merge operation:
|
---|
5881 | *
|
---|
5882 | * forward merge
|
---|
5883 | * ------------------------->
|
---|
5884 | * [Extra] <- SOURCE <- Intermediate <- TARGET
|
---|
5885 | * Any Del Del LockWr
|
---|
5886 | *
|
---|
5887 | *
|
---|
5888 | * backward merge
|
---|
5889 | * <-------------------------
|
---|
5890 | * TARGET <- Intermediate <- SOURCE <- [Extra]
|
---|
5891 | * LockWr Del Del LockWr
|
---|
5892 | *
|
---|
5893 | * Each diagram shows the involved media on the media chain where
|
---|
5894 | * SOURCE and TARGET belong. Under each medium there is a state value which
|
---|
5895 | * the medium must have at a time of the mergeTo() call.
|
---|
5896 | *
|
---|
5897 | * The media in the square braces may be absent (e.g. when the forward
|
---|
5898 | * operation takes place and SOURCE is the base medium, or when the backward
|
---|
5899 | * merge operation takes place and TARGET is the last child in the chain) but if
|
---|
5900 | * they present they are involved too as shown.
|
---|
5901 | *
|
---|
5902 | * Neither the source medium nor intermediate media may be attached to
|
---|
5903 | * any VM directly or in the snapshot, otherwise this method will assert.
|
---|
5904 | *
|
---|
5905 | * The #i_prepareMergeTo() method must be called prior to this method to place
|
---|
5906 | * all involved to necessary states and perform other consistency checks.
|
---|
5907 | *
|
---|
5908 | * If @a aWait is @c true then this method will perform the operation on the
|
---|
5909 | * calling thread and will not return to the caller until the operation is
|
---|
5910 | * completed. When this method succeeds, all intermediate medium objects in
|
---|
5911 | * the chain will be uninitialized, the state of the target medium (and all
|
---|
5912 | * involved extra media) will be restored. @a aMediumLockList will not be
|
---|
5913 | * deleted, whether the operation is successful or not. The caller has to do
|
---|
5914 | * this if appropriate. Note that this (source) medium is not uninitialized
|
---|
5915 | * because of possible AutoCaller instances held by the caller of this method
|
---|
5916 | * on the current thread. It's therefore the responsibility of the caller to
|
---|
5917 | * call Medium::uninit() after releasing all callers.
|
---|
5918 | *
|
---|
5919 | * If @a aWait is @c false then this method will create a thread to perform the
|
---|
5920 | * operation asynchronously and will return immediately. If the operation
|
---|
5921 | * succeeds, the thread will uninitialize the source medium object and all
|
---|
5922 | * intermediate medium objects in the chain, reset the state of the target
|
---|
5923 | * medium (and all involved extra media) and delete @a aMediumLockList.
|
---|
5924 | * If the operation fails, the thread will only reset the states of all
|
---|
5925 | * involved media and delete @a aMediumLockList.
|
---|
5926 | *
|
---|
5927 | * When this method fails (regardless of the @a aWait mode), it is a caller's
|
---|
5928 | * responsibility to undo state changes and delete @a aMediumLockList using
|
---|
5929 | * #i_cancelMergeTo().
|
---|
5930 | *
|
---|
5931 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
5932 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
5933 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
5934 | * progress object is created/used at all. Note that @a aProgress cannot be
|
---|
5935 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
5936 | *
|
---|
5937 | * @param pTarget Target medium.
|
---|
5938 | * @param fMergeForward Merge direction.
|
---|
5939 | * @param pParentForTarget New parent for target medium after merge.
|
---|
5940 | * @param aChildrenToReparent List of children of the source which will have
|
---|
5941 | * to be reparented to the target after merge.
|
---|
5942 | * @param aMediumLockList Medium locking information.
|
---|
5943 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
5944 | * completion.
|
---|
5945 | * @param aWait @c true if this method should block instead of creating
|
---|
5946 | * an asynchronous thread.
|
---|
5947 | * @param aNotify Notify about mediums which metadatа are changed
|
---|
5948 | * during execution of the function.
|
---|
5949 | *
|
---|
5950 | * @note Locks the tree lock for writing. Locks the media from the chain
|
---|
5951 | * for writing.
|
---|
5952 | */
|
---|
5953 | HRESULT Medium::i_mergeTo(const ComObjPtr<Medium> &pTarget,
|
---|
5954 | bool fMergeForward,
|
---|
5955 | const ComObjPtr<Medium> &pParentForTarget,
|
---|
5956 | MediumLockList *aChildrenToReparent,
|
---|
5957 | MediumLockList *aMediumLockList,
|
---|
5958 | ComObjPtr<Progress> *aProgress,
|
---|
5959 | bool aWait, bool aNotify)
|
---|
5960 | {
|
---|
5961 | AssertReturn(pTarget != NULL, E_FAIL);
|
---|
5962 | AssertReturn(pTarget != this, E_FAIL);
|
---|
5963 | AssertReturn(aMediumLockList != NULL, E_FAIL);
|
---|
5964 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
5965 |
|
---|
5966 | AutoCaller autoCaller(this);
|
---|
5967 | AssertComRCReturnRC(autoCaller.rc());
|
---|
5968 |
|
---|
5969 | AutoCaller targetCaller(pTarget);
|
---|
5970 | AssertComRCReturnRC(targetCaller.rc());
|
---|
5971 |
|
---|
5972 | HRESULT rc = S_OK;
|
---|
5973 | ComObjPtr<Progress> pProgress;
|
---|
5974 | Medium::Task *pTask = NULL;
|
---|
5975 |
|
---|
5976 | try
|
---|
5977 | {
|
---|
5978 | if (aProgress != NULL)
|
---|
5979 | {
|
---|
5980 | /* use the existing progress object... */
|
---|
5981 | pProgress = *aProgress;
|
---|
5982 |
|
---|
5983 | /* ...but create a new one if it is null */
|
---|
5984 | if (pProgress.isNull())
|
---|
5985 | {
|
---|
5986 | Utf8Str tgtName;
|
---|
5987 | {
|
---|
5988 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5989 | tgtName = pTarget->i_getName();
|
---|
5990 | }
|
---|
5991 |
|
---|
5992 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5993 |
|
---|
5994 | pProgress.createObject();
|
---|
5995 | rc = pProgress->init(m->pVirtualBox,
|
---|
5996 | static_cast<IMedium*>(this),
|
---|
5997 | BstrFmt(tr("Merging medium '%s' to '%s'"),
|
---|
5998 | i_getName().c_str(),
|
---|
5999 | tgtName.c_str()).raw(),
|
---|
6000 | TRUE, /* aCancelable */
|
---|
6001 | 2, /* Number of opearations */
|
---|
6002 | BstrFmt(tr("Resizing medium '%s' before merge"),
|
---|
6003 | tgtName.c_str()).raw()
|
---|
6004 | );
|
---|
6005 | if (FAILED(rc))
|
---|
6006 | throw rc;
|
---|
6007 | }
|
---|
6008 | }
|
---|
6009 |
|
---|
6010 | /* setup task object to carry out the operation sync/async */
|
---|
6011 | pTask = new Medium::MergeTask(this, pTarget, fMergeForward,
|
---|
6012 | pParentForTarget, aChildrenToReparent,
|
---|
6013 | pProgress, aMediumLockList,
|
---|
6014 | aWait /* fKeepMediumLockList */,
|
---|
6015 | aNotify);
|
---|
6016 | rc = pTask->rc();
|
---|
6017 | AssertComRC(rc);
|
---|
6018 | if (FAILED(rc))
|
---|
6019 | throw rc;
|
---|
6020 | }
|
---|
6021 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6022 |
|
---|
6023 | if (SUCCEEDED(rc))
|
---|
6024 | {
|
---|
6025 | if (aWait)
|
---|
6026 | {
|
---|
6027 | rc = pTask->runNow();
|
---|
6028 |
|
---|
6029 | delete pTask;
|
---|
6030 | }
|
---|
6031 | else
|
---|
6032 | rc = pTask->createThread();
|
---|
6033 |
|
---|
6034 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
6035 | *aProgress = pProgress;
|
---|
6036 | }
|
---|
6037 | else if (pTask != NULL)
|
---|
6038 | delete pTask;
|
---|
6039 |
|
---|
6040 | return rc;
|
---|
6041 | }
|
---|
6042 |
|
---|
6043 | /**
|
---|
6044 | * Undoes what #i_prepareMergeTo() did. Must be called if #mergeTo() is not
|
---|
6045 | * called or fails. Frees memory occupied by @a aMediumLockList and unlocks
|
---|
6046 | * the medium objects in @a aChildrenToReparent.
|
---|
6047 | *
|
---|
6048 | * @param aChildrenToReparent List of children of the source which will have
|
---|
6049 | * to be reparented to the target after merge.
|
---|
6050 | * @param aMediumLockList Medium locking information.
|
---|
6051 | *
|
---|
6052 | * @note Locks the tree lock for writing. Locks the media from the chain
|
---|
6053 | * 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 | * Resizes the media.
|
---|
6102 | *
|
---|
6103 | * If @a aWait is @c true then this method will perform the operation on the
|
---|
6104 | * calling thread and will not return to the caller until the operation is
|
---|
6105 | * completed. When this method succeeds, the state of the target medium (and all
|
---|
6106 | * involved extra media) will be restored. @a aMediumLockList will not be
|
---|
6107 | * deleted, whether the operation is successful or not. The caller has to do
|
---|
6108 | * this if appropriate.
|
---|
6109 | *
|
---|
6110 | * If @a aWait is @c false then this method will create a thread to perform the
|
---|
6111 | * operation asynchronously and will return immediately. The thread will reset
|
---|
6112 | * the state of the target medium (and all involved extra media) and delete
|
---|
6113 | * @a aMediumLockList.
|
---|
6114 | *
|
---|
6115 | * When this method fails (regardless of the @a aWait mode), it is a caller's
|
---|
6116 | * responsibility to undo state changes and delete @a aMediumLockList.
|
---|
6117 | *
|
---|
6118 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
6119 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
6120 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
6121 | * progress object is created/used at all. Note that @a aProgress cannot be
|
---|
6122 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
6123 | *
|
---|
6124 | * @param aLogicalSize New nominal capacity of the medium in bytes.
|
---|
6125 | * @param aMediumLockList Medium locking information.
|
---|
6126 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
6127 | * completion.
|
---|
6128 | * @param aWait @c true if this method should block instead of creating
|
---|
6129 | * an asynchronous thread.
|
---|
6130 | * @param aNotify Notify about mediums which metadatа are changed
|
---|
6131 | * during execution of the function.
|
---|
6132 | *
|
---|
6133 | * @note Locks the media from the chain for writing.
|
---|
6134 | */
|
---|
6135 |
|
---|
6136 | HRESULT Medium::i_resize(LONG64 aLogicalSize,
|
---|
6137 | MediumLockList *aMediumLockList,
|
---|
6138 | ComObjPtr<Progress> *aProgress,
|
---|
6139 | bool aWait,
|
---|
6140 | bool aNotify)
|
---|
6141 | {
|
---|
6142 | AssertReturn(aMediumLockList != NULL, E_FAIL);
|
---|
6143 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
6144 |
|
---|
6145 | AutoCaller autoCaller(this);
|
---|
6146 | AssertComRCReturnRC(autoCaller.rc());
|
---|
6147 |
|
---|
6148 | HRESULT rc = S_OK;
|
---|
6149 | ComObjPtr<Progress> pProgress;
|
---|
6150 | Medium::Task *pTask = NULL;
|
---|
6151 |
|
---|
6152 | try
|
---|
6153 | {
|
---|
6154 | if (aProgress != NULL)
|
---|
6155 | {
|
---|
6156 | /* use the existing progress object... */
|
---|
6157 | pProgress = *aProgress;
|
---|
6158 |
|
---|
6159 | /* ...but create a new one if it is null */
|
---|
6160 | if (pProgress.isNull())
|
---|
6161 | {
|
---|
6162 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6163 |
|
---|
6164 | pProgress.createObject();
|
---|
6165 | rc = pProgress->init(m->pVirtualBox,
|
---|
6166 | static_cast <IMedium *>(this),
|
---|
6167 | BstrFmt(tr("Resizing medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
6168 | TRUE /* aCancelable */);
|
---|
6169 | if (FAILED(rc))
|
---|
6170 | throw rc;
|
---|
6171 | }
|
---|
6172 | }
|
---|
6173 |
|
---|
6174 | /* setup task object to carry out the operation asynchronously */
|
---|
6175 | pTask = new Medium::ResizeTask(this,
|
---|
6176 | aLogicalSize,
|
---|
6177 | pProgress,
|
---|
6178 | aMediumLockList,
|
---|
6179 | aWait /* fKeepMediumLockList */,
|
---|
6180 | aNotify);
|
---|
6181 | rc = pTask->rc();
|
---|
6182 | AssertComRC(rc);
|
---|
6183 | if (FAILED(rc))
|
---|
6184 | throw rc;
|
---|
6185 | }
|
---|
6186 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6187 |
|
---|
6188 | if (SUCCEEDED(rc))
|
---|
6189 | {
|
---|
6190 | if (aWait)
|
---|
6191 | {
|
---|
6192 | rc = pTask->runNow();
|
---|
6193 | delete pTask;
|
---|
6194 | }
|
---|
6195 | else
|
---|
6196 | rc = pTask->createThread();
|
---|
6197 |
|
---|
6198 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
6199 | *aProgress = pProgress;
|
---|
6200 | }
|
---|
6201 | else if (pTask != NULL)
|
---|
6202 | delete pTask;
|
---|
6203 |
|
---|
6204 | return rc;
|
---|
6205 | }
|
---|
6206 |
|
---|
6207 | /**
|
---|
6208 | * Fix the parent UUID of all children to point to this medium as their
|
---|
6209 | * parent.
|
---|
6210 | */
|
---|
6211 | HRESULT Medium::i_fixParentUuidOfChildren(MediumLockList *pChildrenToReparent)
|
---|
6212 | {
|
---|
6213 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
6214 | * to lock order violations, it probably causes lock order issues related
|
---|
6215 | * to the AutoCaller usage. Likewise the code using this method seems
|
---|
6216 | * problematic. */
|
---|
6217 | Assert(!isWriteLockOnCurrentThread());
|
---|
6218 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
6219 | MediumLockList mediumLockList;
|
---|
6220 | HRESULT rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
6221 | NULL /* pToLockWrite */,
|
---|
6222 | false /* fMediumLockWriteAll */,
|
---|
6223 | this,
|
---|
6224 | mediumLockList);
|
---|
6225 | AssertComRCReturnRC(rc);
|
---|
6226 |
|
---|
6227 | try
|
---|
6228 | {
|
---|
6229 | PVDISK hdd;
|
---|
6230 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
6231 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6232 |
|
---|
6233 | try
|
---|
6234 | {
|
---|
6235 | MediumLockList::Base::iterator lockListBegin =
|
---|
6236 | mediumLockList.GetBegin();
|
---|
6237 | MediumLockList::Base::iterator lockListEnd =
|
---|
6238 | mediumLockList.GetEnd();
|
---|
6239 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
6240 | it != lockListEnd;
|
---|
6241 | ++it)
|
---|
6242 | {
|
---|
6243 | MediumLock &mediumLock = *it;
|
---|
6244 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
6245 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
6246 |
|
---|
6247 | // open the medium
|
---|
6248 | vrc = VDOpen(hdd,
|
---|
6249 | pMedium->m->strFormat.c_str(),
|
---|
6250 | pMedium->m->strLocationFull.c_str(),
|
---|
6251 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
6252 | pMedium->m->vdImageIfaces);
|
---|
6253 | if (RT_FAILURE(vrc))
|
---|
6254 | throw vrc;
|
---|
6255 | }
|
---|
6256 |
|
---|
6257 | MediumLockList::Base::iterator childrenBegin = pChildrenToReparent->GetBegin();
|
---|
6258 | MediumLockList::Base::iterator childrenEnd = pChildrenToReparent->GetEnd();
|
---|
6259 | for (MediumLockList::Base::iterator it = childrenBegin;
|
---|
6260 | it != childrenEnd;
|
---|
6261 | ++it)
|
---|
6262 | {
|
---|
6263 | Medium *pMedium = it->GetMedium();
|
---|
6264 | /* VD_OPEN_FLAGS_INFO since UUID is wrong yet */
|
---|
6265 | vrc = VDOpen(hdd,
|
---|
6266 | pMedium->m->strFormat.c_str(),
|
---|
6267 | pMedium->m->strLocationFull.c_str(),
|
---|
6268 | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
6269 | pMedium->m->vdImageIfaces);
|
---|
6270 | if (RT_FAILURE(vrc))
|
---|
6271 | throw vrc;
|
---|
6272 |
|
---|
6273 | vrc = VDSetParentUuid(hdd, VD_LAST_IMAGE, m->id.raw());
|
---|
6274 | if (RT_FAILURE(vrc))
|
---|
6275 | throw vrc;
|
---|
6276 |
|
---|
6277 | vrc = VDClose(hdd, false /* fDelete */);
|
---|
6278 | if (RT_FAILURE(vrc))
|
---|
6279 | throw vrc;
|
---|
6280 | }
|
---|
6281 | }
|
---|
6282 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6283 | catch (int aVRC)
|
---|
6284 | {
|
---|
6285 | rc = setErrorBoth(E_FAIL, aVRC,
|
---|
6286 | tr("Could not update medium UUID references to parent '%s' (%s)"),
|
---|
6287 | m->strLocationFull.c_str(),
|
---|
6288 | i_vdError(aVRC).c_str());
|
---|
6289 | }
|
---|
6290 |
|
---|
6291 | VDDestroy(hdd);
|
---|
6292 | }
|
---|
6293 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6294 |
|
---|
6295 | return rc;
|
---|
6296 | }
|
---|
6297 |
|
---|
6298 | /**
|
---|
6299 | *
|
---|
6300 | * @note Similar code exists in i_taskExportHandler.
|
---|
6301 | */
|
---|
6302 | HRESULT Medium::i_addRawToFss(const char *aFilename, SecretKeyStore *pKeyStore, RTVFSFSSTREAM hVfsFssDst,
|
---|
6303 | const ComObjPtr<Progress> &aProgress, bool fSparse)
|
---|
6304 | {
|
---|
6305 | AutoCaller autoCaller(this);
|
---|
6306 | HRESULT hrc = autoCaller.rc();
|
---|
6307 | if (SUCCEEDED(hrc))
|
---|
6308 | {
|
---|
6309 | /*
|
---|
6310 | * Get a readonly hdd for this medium.
|
---|
6311 | */
|
---|
6312 | MediumCryptoFilterSettings CryptoSettingsRead;
|
---|
6313 | MediumLockList SourceMediumLockList;
|
---|
6314 | PVDISK pHdd;
|
---|
6315 | hrc = i_openForIO(false /*fWritable*/, pKeyStore, &pHdd, &SourceMediumLockList, &CryptoSettingsRead);
|
---|
6316 | if (SUCCEEDED(hrc))
|
---|
6317 | {
|
---|
6318 | /*
|
---|
6319 | * Create a VFS file interface to the HDD and attach a progress wrapper
|
---|
6320 | * that monitors the progress reading of the raw image. The image will
|
---|
6321 | * be read twice if hVfsFssDst does sparse processing.
|
---|
6322 | */
|
---|
6323 | RTVFSFILE hVfsFileDisk = NIL_RTVFSFILE;
|
---|
6324 | int vrc = VDCreateVfsFileFromDisk(pHdd, 0 /*fFlags*/, &hVfsFileDisk);
|
---|
6325 | if (RT_SUCCESS(vrc))
|
---|
6326 | {
|
---|
6327 | RTVFSFILE hVfsFileProgress = NIL_RTVFSFILE;
|
---|
6328 | vrc = RTVfsCreateProgressForFile(hVfsFileDisk, aProgress->i_iprtProgressCallback, &*aProgress,
|
---|
6329 | RTVFSPROGRESS_F_CANCELABLE | RTVFSPROGRESS_F_FORWARD_SEEK_AS_READ,
|
---|
6330 | VDGetSize(pHdd, VD_LAST_IMAGE) * (fSparse ? 2 : 1) /*cbExpectedRead*/,
|
---|
6331 | 0 /*cbExpectedWritten*/, &hVfsFileProgress);
|
---|
6332 | RTVfsFileRelease(hVfsFileDisk);
|
---|
6333 | if (RT_SUCCESS(vrc))
|
---|
6334 | {
|
---|
6335 | RTVFSOBJ hVfsObj = RTVfsObjFromFile(hVfsFileProgress);
|
---|
6336 | RTVfsFileRelease(hVfsFileProgress);
|
---|
6337 |
|
---|
6338 | vrc = RTVfsFsStrmAdd(hVfsFssDst, aFilename, hVfsObj, 0 /*fFlags*/);
|
---|
6339 | RTVfsObjRelease(hVfsObj);
|
---|
6340 | if (RT_FAILURE(vrc))
|
---|
6341 | hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Failed to add '%s' to output (%Rrc)"), aFilename, vrc);
|
---|
6342 | }
|
---|
6343 | else
|
---|
6344 | hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
6345 | tr("RTVfsCreateProgressForFile failed when processing '%s' (%Rrc)"), aFilename, vrc);
|
---|
6346 | }
|
---|
6347 | else
|
---|
6348 | hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("VDCreateVfsFileFromDisk failed for '%s' (%Rrc)"), aFilename, vrc);
|
---|
6349 | VDDestroy(pHdd);
|
---|
6350 | }
|
---|
6351 | }
|
---|
6352 | return hrc;
|
---|
6353 | }
|
---|
6354 |
|
---|
6355 | /**
|
---|
6356 | * Used by IAppliance to export disk images.
|
---|
6357 | *
|
---|
6358 | * @param aFilename Filename to create (UTF8).
|
---|
6359 | * @param aFormat Medium format for creating @a aFilename.
|
---|
6360 | * @param aVariant Which exact image format variant to use for the
|
---|
6361 | * destination image.
|
---|
6362 | * @param pKeyStore The optional key store for decrypting the data for
|
---|
6363 | * encrypted media during the export.
|
---|
6364 | * @param hVfsIosDst The destination I/O stream object.
|
---|
6365 | * @param aProgress Progress object to use.
|
---|
6366 | * @return
|
---|
6367 | *
|
---|
6368 | * @note The source format is defined by the Medium instance.
|
---|
6369 | */
|
---|
6370 | HRESULT Medium::i_exportFile(const char *aFilename,
|
---|
6371 | const ComObjPtr<MediumFormat> &aFormat,
|
---|
6372 | MediumVariant_T aVariant,
|
---|
6373 | SecretKeyStore *pKeyStore,
|
---|
6374 | RTVFSIOSTREAM hVfsIosDst,
|
---|
6375 | const ComObjPtr<Progress> &aProgress)
|
---|
6376 | {
|
---|
6377 | AssertPtrReturn(aFilename, E_INVALIDARG);
|
---|
6378 | AssertReturn(aFormat.isNotNull(), E_INVALIDARG);
|
---|
6379 | AssertReturn(aProgress.isNotNull(), E_INVALIDARG);
|
---|
6380 |
|
---|
6381 | AutoCaller autoCaller(this);
|
---|
6382 | HRESULT hrc = autoCaller.rc();
|
---|
6383 | if (SUCCEEDED(hrc))
|
---|
6384 | {
|
---|
6385 | /*
|
---|
6386 | * Setup VD interfaces.
|
---|
6387 | */
|
---|
6388 | PVDINTERFACE pVDImageIfaces = m->vdImageIfaces;
|
---|
6389 | PVDINTERFACEIO pVfsIoIf;
|
---|
6390 | int vrc = VDIfCreateFromVfsStream(hVfsIosDst, RTFILE_O_WRITE, &pVfsIoIf);
|
---|
6391 | if (RT_SUCCESS(vrc))
|
---|
6392 | {
|
---|
6393 | vrc = VDInterfaceAdd(&pVfsIoIf->Core, "Medium::ExportTaskVfsIos", VDINTERFACETYPE_IO,
|
---|
6394 | pVfsIoIf, sizeof(VDINTERFACEIO), &pVDImageIfaces);
|
---|
6395 | if (RT_SUCCESS(vrc))
|
---|
6396 | {
|
---|
6397 | /*
|
---|
6398 | * Get a readonly hdd for this medium (source).
|
---|
6399 | */
|
---|
6400 | MediumCryptoFilterSettings CryptoSettingsRead;
|
---|
6401 | MediumLockList SourceMediumLockList;
|
---|
6402 | PVDISK pSrcHdd;
|
---|
6403 | hrc = i_openForIO(false /*fWritable*/, pKeyStore, &pSrcHdd, &SourceMediumLockList, &CryptoSettingsRead);
|
---|
6404 | if (SUCCEEDED(hrc))
|
---|
6405 | {
|
---|
6406 | /*
|
---|
6407 | * Create the target medium.
|
---|
6408 | */
|
---|
6409 | Utf8Str strDstFormat(aFormat->i_getId());
|
---|
6410 |
|
---|
6411 | /* ensure the target directory exists */
|
---|
6412 | uint64_t fDstCapabilities = aFormat->i_getCapabilities();
|
---|
6413 | if (fDstCapabilities & MediumFormatCapabilities_File)
|
---|
6414 | {
|
---|
6415 | Utf8Str strDstLocation(aFilename);
|
---|
6416 | hrc = VirtualBox::i_ensureFilePathExists(strDstLocation.c_str(),
|
---|
6417 | !(aVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
6418 | }
|
---|
6419 | if (SUCCEEDED(hrc))
|
---|
6420 | {
|
---|
6421 | PVDISK pDstHdd;
|
---|
6422 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDstHdd);
|
---|
6423 | if (RT_SUCCESS(vrc))
|
---|
6424 | {
|
---|
6425 | /*
|
---|
6426 | * Create an interface for getting progress callbacks.
|
---|
6427 | */
|
---|
6428 | VDINTERFACEPROGRESS ProgressIf = VDINTERFACEPROGRESS_INITALIZER(aProgress->i_vdProgressCallback);
|
---|
6429 | PVDINTERFACE pProgress = NULL;
|
---|
6430 | vrc = VDInterfaceAdd(&ProgressIf.Core, "export-progress", VDINTERFACETYPE_PROGRESS,
|
---|
6431 | &*aProgress, sizeof(ProgressIf), &pProgress);
|
---|
6432 | AssertRC(vrc);
|
---|
6433 |
|
---|
6434 | /*
|
---|
6435 | * Do the exporting.
|
---|
6436 | */
|
---|
6437 | vrc = VDCopy(pSrcHdd,
|
---|
6438 | VD_LAST_IMAGE,
|
---|
6439 | pDstHdd,
|
---|
6440 | strDstFormat.c_str(),
|
---|
6441 | aFilename,
|
---|
6442 | false /* fMoveByRename */,
|
---|
6443 | 0 /* cbSize */,
|
---|
6444 | aVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted),
|
---|
6445 | NULL /* pDstUuid */,
|
---|
6446 | VD_OPEN_FLAGS_NORMAL | VD_OPEN_FLAGS_SEQUENTIAL,
|
---|
6447 | pProgress,
|
---|
6448 | pVDImageIfaces,
|
---|
6449 | NULL);
|
---|
6450 | if (RT_SUCCESS(vrc))
|
---|
6451 | hrc = S_OK;
|
---|
6452 | else
|
---|
6453 | hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Could not create the exported medium '%s'%s"),
|
---|
6454 | aFilename, i_vdError(vrc).c_str());
|
---|
6455 | VDDestroy(pDstHdd);
|
---|
6456 | }
|
---|
6457 | else
|
---|
6458 | hrc = setErrorVrc(vrc);
|
---|
6459 | }
|
---|
6460 | }
|
---|
6461 | VDDestroy(pSrcHdd);
|
---|
6462 | }
|
---|
6463 | else
|
---|
6464 | hrc = setErrorVrc(vrc, "VDInterfaceAdd -> %Rrc", vrc);
|
---|
6465 | VDIfDestroyFromVfsStream(pVfsIoIf);
|
---|
6466 | }
|
---|
6467 | else
|
---|
6468 | hrc = setErrorVrc(vrc, "VDIfCreateFromVfsStream -> %Rrc", vrc);
|
---|
6469 | }
|
---|
6470 | return hrc;
|
---|
6471 | }
|
---|
6472 |
|
---|
6473 | /**
|
---|
6474 | * Used by IAppliance to import disk images.
|
---|
6475 | *
|
---|
6476 | * @param aFilename Filename to read (UTF8).
|
---|
6477 | * @param aFormat Medium format for reading @a aFilename.
|
---|
6478 | * @param aVariant Which exact image format variant to use
|
---|
6479 | * for the destination image.
|
---|
6480 | * @param aVfsIosSrc Handle to the source I/O stream.
|
---|
6481 | * @param aParent Parent medium. May be NULL.
|
---|
6482 | * @param aProgress Progress object to use.
|
---|
6483 | * @param aNotify Notify about mediums which metadatа are changed
|
---|
6484 | * during execution of the function.
|
---|
6485 | * @return
|
---|
6486 | * @note The destination format is defined by the Medium instance.
|
---|
6487 | *
|
---|
6488 | * @todo The only consumer of this method (Appliance::i_importOneDiskImage) is
|
---|
6489 | * already on a worker thread, so perhaps consider bypassing the thread
|
---|
6490 | * here and run in the task synchronously? VBoxSVC has enough threads as
|
---|
6491 | * it is...
|
---|
6492 | */
|
---|
6493 | HRESULT Medium::i_importFile(const char *aFilename,
|
---|
6494 | const ComObjPtr<MediumFormat> &aFormat,
|
---|
6495 | MediumVariant_T aVariant,
|
---|
6496 | RTVFSIOSTREAM aVfsIosSrc,
|
---|
6497 | const ComObjPtr<Medium> &aParent,
|
---|
6498 | const ComObjPtr<Progress> &aProgress,
|
---|
6499 | bool aNotify)
|
---|
6500 | {
|
---|
6501 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
6502 | * to lock order violations, it probably causes lock order issues related
|
---|
6503 | * to the AutoCaller usage. */
|
---|
6504 | AssertPtrReturn(aFilename, E_INVALIDARG);
|
---|
6505 | AssertReturn(!aFormat.isNull(), E_INVALIDARG);
|
---|
6506 | AssertReturn(!aProgress.isNull(), E_INVALIDARG);
|
---|
6507 |
|
---|
6508 | AutoCaller autoCaller(this);
|
---|
6509 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
6510 |
|
---|
6511 | HRESULT rc = S_OK;
|
---|
6512 | Medium::Task *pTask = NULL;
|
---|
6513 |
|
---|
6514 | try
|
---|
6515 | {
|
---|
6516 | // locking: we need the tree lock first because we access parent pointers
|
---|
6517 | // and we need to write-lock the media involved
|
---|
6518 | uint32_t cHandles = 2;
|
---|
6519 | LockHandle* pHandles[3] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
6520 | this->lockHandle() };
|
---|
6521 | /* Only add parent to the lock if it is not null */
|
---|
6522 | if (!aParent.isNull())
|
---|
6523 | pHandles[cHandles++] = aParent->lockHandle();
|
---|
6524 | AutoWriteLock alock(cHandles,
|
---|
6525 | pHandles
|
---|
6526 | COMMA_LOCKVAL_SRC_POS);
|
---|
6527 |
|
---|
6528 | if ( m->state != MediumState_NotCreated
|
---|
6529 | && m->state != MediumState_Created)
|
---|
6530 | throw i_setStateError();
|
---|
6531 |
|
---|
6532 | /* Build the target lock list. */
|
---|
6533 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
6534 | alock.release();
|
---|
6535 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
6536 | this /* pToLockWrite */,
|
---|
6537 | false /* fMediumLockWriteAll */,
|
---|
6538 | aParent,
|
---|
6539 | *pTargetMediumLockList);
|
---|
6540 | alock.acquire();
|
---|
6541 | if (FAILED(rc))
|
---|
6542 | {
|
---|
6543 | delete pTargetMediumLockList;
|
---|
6544 | throw rc;
|
---|
6545 | }
|
---|
6546 |
|
---|
6547 | alock.release();
|
---|
6548 | rc = pTargetMediumLockList->Lock();
|
---|
6549 | alock.acquire();
|
---|
6550 | if (FAILED(rc))
|
---|
6551 | {
|
---|
6552 | delete pTargetMediumLockList;
|
---|
6553 | throw setError(rc,
|
---|
6554 | tr("Failed to lock target media '%s'"),
|
---|
6555 | i_getLocationFull().c_str());
|
---|
6556 | }
|
---|
6557 |
|
---|
6558 | /* setup task object to carry out the operation asynchronously */
|
---|
6559 | pTask = new Medium::ImportTask(this, aProgress, aFilename, aFormat, aVariant,
|
---|
6560 | aVfsIosSrc, aParent, pTargetMediumLockList, false, aNotify);
|
---|
6561 | rc = pTask->rc();
|
---|
6562 | AssertComRC(rc);
|
---|
6563 | if (FAILED(rc))
|
---|
6564 | throw rc;
|
---|
6565 |
|
---|
6566 | if (m->state == MediumState_NotCreated)
|
---|
6567 | m->state = MediumState_Creating;
|
---|
6568 | }
|
---|
6569 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6570 |
|
---|
6571 | if (SUCCEEDED(rc))
|
---|
6572 | rc = pTask->createThread();
|
---|
6573 | else if (pTask != NULL)
|
---|
6574 | delete pTask;
|
---|
6575 |
|
---|
6576 | return rc;
|
---|
6577 | }
|
---|
6578 |
|
---|
6579 | /**
|
---|
6580 | * Internal version of the public CloneTo API which allows to enable certain
|
---|
6581 | * optimizations to improve speed during VM cloning.
|
---|
6582 | *
|
---|
6583 | * @param aTarget Target medium
|
---|
6584 | * @param aVariant Which exact image format variant to use
|
---|
6585 | * for the destination image.
|
---|
6586 | * @param aParent Parent medium. May be NULL.
|
---|
6587 | * @param aProgress Progress object to use.
|
---|
6588 | * @param idxSrcImageSame The last image in the source chain which has the
|
---|
6589 | * same content as the given image in the destination
|
---|
6590 | * chain. Use UINT32_MAX to disable this optimization.
|
---|
6591 | * @param idxDstImageSame The last image in the destination chain which has the
|
---|
6592 | * same content as the given image in the source chain.
|
---|
6593 | * Use UINT32_MAX to disable this optimization.
|
---|
6594 | * @param aNotify Notify about mediums which metadatа are changed
|
---|
6595 | * during execution of the function.
|
---|
6596 | * @return
|
---|
6597 | */
|
---|
6598 | HRESULT Medium::i_cloneToEx(const ComObjPtr<Medium> &aTarget, MediumVariant_T aVariant,
|
---|
6599 | const ComObjPtr<Medium> &aParent, IProgress **aProgress,
|
---|
6600 | uint32_t idxSrcImageSame, uint32_t idxDstImageSame, bool aNotify)
|
---|
6601 | {
|
---|
6602 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
6603 | * to lock order violations, it probably causes lock order issues related
|
---|
6604 | * to the AutoCaller usage. */
|
---|
6605 | CheckComArgNotNull(aTarget);
|
---|
6606 | CheckComArgOutPointerValid(aProgress);
|
---|
6607 | ComAssertRet(aTarget != this, E_INVALIDARG);
|
---|
6608 |
|
---|
6609 | AutoCaller autoCaller(this);
|
---|
6610 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
6611 |
|
---|
6612 | HRESULT rc = S_OK;
|
---|
6613 | ComObjPtr<Progress> pProgress;
|
---|
6614 | Medium::Task *pTask = NULL;
|
---|
6615 |
|
---|
6616 | try
|
---|
6617 | {
|
---|
6618 | // locking: we need the tree lock first because we access parent pointers
|
---|
6619 | // and we need to write-lock the media involved
|
---|
6620 | uint32_t cHandles = 3;
|
---|
6621 | LockHandle* pHandles[4] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
6622 | this->lockHandle(),
|
---|
6623 | aTarget->lockHandle() };
|
---|
6624 | /* Only add parent to the lock if it is not null */
|
---|
6625 | if (!aParent.isNull())
|
---|
6626 | pHandles[cHandles++] = aParent->lockHandle();
|
---|
6627 | AutoWriteLock alock(cHandles,
|
---|
6628 | pHandles
|
---|
6629 | COMMA_LOCKVAL_SRC_POS);
|
---|
6630 |
|
---|
6631 | if ( aTarget->m->state != MediumState_NotCreated
|
---|
6632 | && aTarget->m->state != MediumState_Created)
|
---|
6633 | throw aTarget->i_setStateError();
|
---|
6634 |
|
---|
6635 | /* Build the source lock list. */
|
---|
6636 | MediumLockList *pSourceMediumLockList(new MediumLockList());
|
---|
6637 | alock.release();
|
---|
6638 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
6639 | NULL /* pToLockWrite */,
|
---|
6640 | false /* fMediumLockWriteAll */,
|
---|
6641 | NULL,
|
---|
6642 | *pSourceMediumLockList);
|
---|
6643 | alock.acquire();
|
---|
6644 | if (FAILED(rc))
|
---|
6645 | {
|
---|
6646 | delete pSourceMediumLockList;
|
---|
6647 | throw rc;
|
---|
6648 | }
|
---|
6649 |
|
---|
6650 | /* Build the target lock list (including the to-be parent chain). */
|
---|
6651 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
6652 | alock.release();
|
---|
6653 | rc = aTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
6654 | aTarget /* pToLockWrite */,
|
---|
6655 | false /* fMediumLockWriteAll */,
|
---|
6656 | aParent,
|
---|
6657 | *pTargetMediumLockList);
|
---|
6658 | alock.acquire();
|
---|
6659 | if (FAILED(rc))
|
---|
6660 | {
|
---|
6661 | delete pSourceMediumLockList;
|
---|
6662 | delete pTargetMediumLockList;
|
---|
6663 | throw rc;
|
---|
6664 | }
|
---|
6665 |
|
---|
6666 | alock.release();
|
---|
6667 | rc = pSourceMediumLockList->Lock();
|
---|
6668 | alock.acquire();
|
---|
6669 | if (FAILED(rc))
|
---|
6670 | {
|
---|
6671 | delete pSourceMediumLockList;
|
---|
6672 | delete pTargetMediumLockList;
|
---|
6673 | throw setError(rc,
|
---|
6674 | tr("Failed to lock source media '%s'"),
|
---|
6675 | i_getLocationFull().c_str());
|
---|
6676 | }
|
---|
6677 | alock.release();
|
---|
6678 | rc = pTargetMediumLockList->Lock();
|
---|
6679 | alock.acquire();
|
---|
6680 | if (FAILED(rc))
|
---|
6681 | {
|
---|
6682 | delete pSourceMediumLockList;
|
---|
6683 | delete pTargetMediumLockList;
|
---|
6684 | throw setError(rc,
|
---|
6685 | tr("Failed to lock target media '%s'"),
|
---|
6686 | aTarget->i_getLocationFull().c_str());
|
---|
6687 | }
|
---|
6688 |
|
---|
6689 | pProgress.createObject();
|
---|
6690 | rc = pProgress->init(m->pVirtualBox,
|
---|
6691 | static_cast <IMedium *>(this),
|
---|
6692 | BstrFmt(tr("Creating clone medium '%s'"), aTarget->m->strLocationFull.c_str()).raw(),
|
---|
6693 | TRUE /* aCancelable */);
|
---|
6694 | if (FAILED(rc))
|
---|
6695 | {
|
---|
6696 | delete pSourceMediumLockList;
|
---|
6697 | delete pTargetMediumLockList;
|
---|
6698 | throw rc;
|
---|
6699 | }
|
---|
6700 |
|
---|
6701 | /* setup task object to carry out the operation asynchronously */
|
---|
6702 | pTask = new Medium::CloneTask(this, pProgress, aTarget, aVariant,
|
---|
6703 | aParent, idxSrcImageSame,
|
---|
6704 | idxDstImageSame, pSourceMediumLockList,
|
---|
6705 | pTargetMediumLockList, false, false, aNotify);
|
---|
6706 | rc = pTask->rc();
|
---|
6707 | AssertComRC(rc);
|
---|
6708 | if (FAILED(rc))
|
---|
6709 | throw rc;
|
---|
6710 |
|
---|
6711 | if (aTarget->m->state == MediumState_NotCreated)
|
---|
6712 | aTarget->m->state = MediumState_Creating;
|
---|
6713 | }
|
---|
6714 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6715 |
|
---|
6716 | if (SUCCEEDED(rc))
|
---|
6717 | {
|
---|
6718 | rc = pTask->createThread();
|
---|
6719 |
|
---|
6720 | if (SUCCEEDED(rc))
|
---|
6721 | pProgress.queryInterfaceTo(aProgress);
|
---|
6722 | }
|
---|
6723 | else if (pTask != NULL)
|
---|
6724 | delete pTask;
|
---|
6725 |
|
---|
6726 | return rc;
|
---|
6727 | }
|
---|
6728 |
|
---|
6729 | /**
|
---|
6730 | * Returns the key identifier for this medium if encryption is configured.
|
---|
6731 | *
|
---|
6732 | * @returns Key identifier or empty string if no encryption is configured.
|
---|
6733 | */
|
---|
6734 | const Utf8Str& Medium::i_getKeyId()
|
---|
6735 | {
|
---|
6736 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
6737 |
|
---|
6738 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6739 |
|
---|
6740 | settings::StringsMap::const_iterator it = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
6741 | if (it == pBase->m->mapProperties.end())
|
---|
6742 | return Utf8Str::Empty;
|
---|
6743 |
|
---|
6744 | return it->second;
|
---|
6745 | }
|
---|
6746 |
|
---|
6747 |
|
---|
6748 | /**
|
---|
6749 | * Returns all filter related properties.
|
---|
6750 | *
|
---|
6751 | * @returns COM status code.
|
---|
6752 | * @param aReturnNames Where to store the properties names on success.
|
---|
6753 | * @param aReturnValues Where to store the properties values on success.
|
---|
6754 | */
|
---|
6755 | HRESULT Medium::i_getFilterProperties(std::vector<com::Utf8Str> &aReturnNames,
|
---|
6756 | std::vector<com::Utf8Str> &aReturnValues)
|
---|
6757 | {
|
---|
6758 | std::vector<com::Utf8Str> aPropNames;
|
---|
6759 | std::vector<com::Utf8Str> aPropValues;
|
---|
6760 | HRESULT hrc = getProperties(Utf8Str(""), aPropNames, aPropValues);
|
---|
6761 |
|
---|
6762 | if (SUCCEEDED(hrc))
|
---|
6763 | {
|
---|
6764 | unsigned cReturnSize = 0;
|
---|
6765 | aReturnNames.resize(0);
|
---|
6766 | aReturnValues.resize(0);
|
---|
6767 | for (unsigned idx = 0; idx < aPropNames.size(); idx++)
|
---|
6768 | {
|
---|
6769 | if (i_isPropertyForFilter(aPropNames[idx]))
|
---|
6770 | {
|
---|
6771 | aReturnNames.resize(cReturnSize + 1);
|
---|
6772 | aReturnValues.resize(cReturnSize + 1);
|
---|
6773 | aReturnNames[cReturnSize] = aPropNames[idx];
|
---|
6774 | aReturnValues[cReturnSize] = aPropValues[idx];
|
---|
6775 | cReturnSize++;
|
---|
6776 | }
|
---|
6777 | }
|
---|
6778 | }
|
---|
6779 |
|
---|
6780 | return hrc;
|
---|
6781 | }
|
---|
6782 |
|
---|
6783 | /**
|
---|
6784 | * Preparation to move this medium to a new location
|
---|
6785 | *
|
---|
6786 | * @param aLocation Location of the storage unit. If the location is a FS-path,
|
---|
6787 | * then it can be relative to the VirtualBox home directory.
|
---|
6788 | *
|
---|
6789 | * @note Must be called from under this object's write lock.
|
---|
6790 | */
|
---|
6791 | HRESULT Medium::i_preparationForMoving(const Utf8Str &aLocation)
|
---|
6792 | {
|
---|
6793 | HRESULT rc = E_FAIL;
|
---|
6794 |
|
---|
6795 | if (i_getLocationFull() != aLocation)
|
---|
6796 | {
|
---|
6797 | m->strNewLocationFull = aLocation;
|
---|
6798 | m->fMoveThisMedium = true;
|
---|
6799 | rc = S_OK;
|
---|
6800 | }
|
---|
6801 |
|
---|
6802 | return rc;
|
---|
6803 | }
|
---|
6804 |
|
---|
6805 | /**
|
---|
6806 | * Checking whether current operation "moving" or not
|
---|
6807 | */
|
---|
6808 | bool Medium::i_isMoveOperation(const ComObjPtr<Medium> &aTarget) const
|
---|
6809 | {
|
---|
6810 | RT_NOREF(aTarget);
|
---|
6811 | return (m->fMoveThisMedium == true) ? true:false; /** @todo r=bird: this is not an obfuscation contest! */
|
---|
6812 | }
|
---|
6813 |
|
---|
6814 | bool Medium::i_resetMoveOperationData()
|
---|
6815 | {
|
---|
6816 | m->strNewLocationFull.setNull();
|
---|
6817 | m->fMoveThisMedium = false;
|
---|
6818 | return true;
|
---|
6819 | }
|
---|
6820 |
|
---|
6821 | Utf8Str Medium::i_getNewLocationForMoving() const
|
---|
6822 | {
|
---|
6823 | if (m->fMoveThisMedium == true)
|
---|
6824 | return m->strNewLocationFull;
|
---|
6825 | else
|
---|
6826 | return Utf8Str();
|
---|
6827 | }
|
---|
6828 | ////////////////////////////////////////////////////////////////////////////////
|
---|
6829 | //
|
---|
6830 | // Private methods
|
---|
6831 | //
|
---|
6832 | ////////////////////////////////////////////////////////////////////////////////
|
---|
6833 |
|
---|
6834 | /**
|
---|
6835 | * Queries information from the medium.
|
---|
6836 | *
|
---|
6837 | * As a result of this call, the accessibility state and data members such as
|
---|
6838 | * size and description will be updated with the current information.
|
---|
6839 | *
|
---|
6840 | * @note This method may block during a system I/O call that checks storage
|
---|
6841 | * accessibility.
|
---|
6842 | *
|
---|
6843 | * @note Caller MUST NOT hold the media tree or medium lock.
|
---|
6844 | *
|
---|
6845 | * @note Locks m->pParent for reading. Locks this object for writing.
|
---|
6846 | *
|
---|
6847 | * @param fSetImageId Whether to reset the UUID contained in the image file
|
---|
6848 | * to the UUID in the medium instance data (see SetIDs())
|
---|
6849 | * @param fSetParentId Whether to reset the parent UUID contained in the image
|
---|
6850 | * file to the parent UUID in the medium instance data (see
|
---|
6851 | * SetIDs())
|
---|
6852 | * @param autoCaller
|
---|
6853 | * @return
|
---|
6854 | */
|
---|
6855 | HRESULT Medium::i_queryInfo(bool fSetImageId, bool fSetParentId, AutoCaller &autoCaller)
|
---|
6856 | {
|
---|
6857 | Assert(!isWriteLockOnCurrentThread());
|
---|
6858 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6859 |
|
---|
6860 | if ( ( m->state != MediumState_Created
|
---|
6861 | && m->state != MediumState_Inaccessible
|
---|
6862 | && m->state != MediumState_LockedRead)
|
---|
6863 | || m->fClosing)
|
---|
6864 | return E_FAIL;
|
---|
6865 |
|
---|
6866 | HRESULT rc = S_OK;
|
---|
6867 |
|
---|
6868 | int vrc = VINF_SUCCESS;
|
---|
6869 |
|
---|
6870 | /* check if a blocking i_queryInfo() call is in progress on some other thread,
|
---|
6871 | * and wait for it to finish if so instead of querying data ourselves */
|
---|
6872 | if (m->queryInfoRunning)
|
---|
6873 | {
|
---|
6874 | Assert( m->state == MediumState_LockedRead
|
---|
6875 | || m->state == MediumState_LockedWrite);
|
---|
6876 |
|
---|
6877 | while (m->queryInfoRunning)
|
---|
6878 | {
|
---|
6879 | alock.release();
|
---|
6880 | /* must not hold the object lock now */
|
---|
6881 | Assert(!isWriteLockOnCurrentThread());
|
---|
6882 | {
|
---|
6883 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
6884 | }
|
---|
6885 | alock.acquire();
|
---|
6886 | }
|
---|
6887 |
|
---|
6888 | return S_OK;
|
---|
6889 | }
|
---|
6890 |
|
---|
6891 | bool success = false;
|
---|
6892 | Utf8Str lastAccessError;
|
---|
6893 |
|
---|
6894 | /* are we dealing with a new medium constructed using the existing
|
---|
6895 | * location? */
|
---|
6896 | bool isImport = m->id.isZero();
|
---|
6897 | unsigned uOpenFlags = VD_OPEN_FLAGS_INFO;
|
---|
6898 |
|
---|
6899 | /* Note that we don't use VD_OPEN_FLAGS_READONLY when opening new
|
---|
6900 | * media because that would prevent necessary modifications
|
---|
6901 | * when opening media of some third-party formats for the first
|
---|
6902 | * time in VirtualBox (such as VMDK for which VDOpen() needs to
|
---|
6903 | * generate an UUID if it is missing) */
|
---|
6904 | if ( m->hddOpenMode == OpenReadOnly
|
---|
6905 | || m->type == MediumType_Readonly
|
---|
6906 | || (!isImport && !fSetImageId && !fSetParentId)
|
---|
6907 | )
|
---|
6908 | uOpenFlags |= VD_OPEN_FLAGS_READONLY;
|
---|
6909 |
|
---|
6910 | /* Open shareable medium with the appropriate flags */
|
---|
6911 | if (m->type == MediumType_Shareable)
|
---|
6912 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
6913 |
|
---|
6914 | /* Lock the medium, which makes the behavior much more consistent, must be
|
---|
6915 | * done before dropping the object lock and setting queryInfoRunning. */
|
---|
6916 | ComPtr<IToken> pToken;
|
---|
6917 | if (uOpenFlags & (VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SHAREABLE))
|
---|
6918 | rc = LockRead(pToken.asOutParam());
|
---|
6919 | else
|
---|
6920 | rc = LockWrite(pToken.asOutParam());
|
---|
6921 | if (FAILED(rc)) return rc;
|
---|
6922 |
|
---|
6923 | /* Copies of the input state fields which are not read-only,
|
---|
6924 | * as we're dropping the lock. CAUTION: be extremely careful what
|
---|
6925 | * you do with the contents of this medium object, as you will
|
---|
6926 | * create races if there are concurrent changes. */
|
---|
6927 | Utf8Str format(m->strFormat);
|
---|
6928 | Utf8Str location(m->strLocationFull);
|
---|
6929 | ComObjPtr<MediumFormat> formatObj = m->formatObj;
|
---|
6930 |
|
---|
6931 | /* "Output" values which can't be set because the lock isn't held
|
---|
6932 | * at the time the values are determined. */
|
---|
6933 | Guid mediumId = m->id;
|
---|
6934 | uint64_t mediumSize = 0;
|
---|
6935 | uint64_t mediumLogicalSize = 0;
|
---|
6936 |
|
---|
6937 | /* Flag whether a base image has a non-zero parent UUID and thus
|
---|
6938 | * need repairing after it was closed again. */
|
---|
6939 | bool fRepairImageZeroParentUuid = false;
|
---|
6940 |
|
---|
6941 | ComObjPtr<VirtualBox> pVirtualBox = m->pVirtualBox;
|
---|
6942 |
|
---|
6943 | /* must be set before leaving the object lock the first time */
|
---|
6944 | m->queryInfoRunning = true;
|
---|
6945 |
|
---|
6946 | /* must leave object lock now, because a lock from a higher lock class
|
---|
6947 | * is needed and also a lengthy operation is coming */
|
---|
6948 | alock.release();
|
---|
6949 | autoCaller.release();
|
---|
6950 |
|
---|
6951 | /* Note that taking the queryInfoSem after leaving the object lock above
|
---|
6952 | * can lead to short spinning of the loops waiting for i_queryInfo() to
|
---|
6953 | * complete. This is unavoidable since the other order causes a lock order
|
---|
6954 | * violation: here it would be requesting the object lock (at the beginning
|
---|
6955 | * of the method), then queryInfoSem, and below the other way round. */
|
---|
6956 | AutoWriteLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
6957 |
|
---|
6958 | /* take the opportunity to have a media tree lock, released initially */
|
---|
6959 | Assert(!isWriteLockOnCurrentThread());
|
---|
6960 | Assert(!pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
6961 | AutoWriteLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
6962 | treeLock.release();
|
---|
6963 |
|
---|
6964 | /* re-take the caller, but not the object lock, to keep uninit away */
|
---|
6965 | autoCaller.add();
|
---|
6966 | if (FAILED(autoCaller.rc()))
|
---|
6967 | {
|
---|
6968 | m->queryInfoRunning = false;
|
---|
6969 | return autoCaller.rc();
|
---|
6970 | }
|
---|
6971 |
|
---|
6972 | try
|
---|
6973 | {
|
---|
6974 | /* skip accessibility checks for host drives */
|
---|
6975 | if (m->hostDrive)
|
---|
6976 | {
|
---|
6977 | success = true;
|
---|
6978 | throw S_OK;
|
---|
6979 | }
|
---|
6980 |
|
---|
6981 | PVDISK hdd;
|
---|
6982 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
6983 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6984 |
|
---|
6985 | try
|
---|
6986 | {
|
---|
6987 | /** @todo This kind of opening of media is assuming that diff
|
---|
6988 | * media can be opened as base media. Should be documented that
|
---|
6989 | * it must work for all medium format backends. */
|
---|
6990 | vrc = VDOpen(hdd,
|
---|
6991 | format.c_str(),
|
---|
6992 | location.c_str(),
|
---|
6993 | uOpenFlags | m->uOpenFlagsDef,
|
---|
6994 | m->vdImageIfaces);
|
---|
6995 | if (RT_FAILURE(vrc))
|
---|
6996 | {
|
---|
6997 | lastAccessError = Utf8StrFmt(tr("Could not open the medium '%s'%s"),
|
---|
6998 | location.c_str(), i_vdError(vrc).c_str());
|
---|
6999 | throw S_OK;
|
---|
7000 | }
|
---|
7001 |
|
---|
7002 | if (formatObj->i_getCapabilities() & MediumFormatCapabilities_Uuid)
|
---|
7003 | {
|
---|
7004 | /* Modify the UUIDs if necessary. The associated fields are
|
---|
7005 | * not modified by other code, so no need to copy. */
|
---|
7006 | if (fSetImageId)
|
---|
7007 | {
|
---|
7008 | alock.acquire();
|
---|
7009 | vrc = VDSetUuid(hdd, 0, m->uuidImage.raw());
|
---|
7010 | alock.release();
|
---|
7011 | if (RT_FAILURE(vrc))
|
---|
7012 | {
|
---|
7013 | lastAccessError = Utf8StrFmt(tr("Could not update the UUID of medium '%s'%s"),
|
---|
7014 | location.c_str(), i_vdError(vrc).c_str());
|
---|
7015 | throw S_OK;
|
---|
7016 | }
|
---|
7017 | mediumId = m->uuidImage;
|
---|
7018 | }
|
---|
7019 | if (fSetParentId)
|
---|
7020 | {
|
---|
7021 | alock.acquire();
|
---|
7022 | vrc = VDSetParentUuid(hdd, 0, m->uuidParentImage.raw());
|
---|
7023 | alock.release();
|
---|
7024 | if (RT_FAILURE(vrc))
|
---|
7025 | {
|
---|
7026 | lastAccessError = Utf8StrFmt(tr("Could not update the parent UUID of medium '%s'%s"),
|
---|
7027 | location.c_str(), i_vdError(vrc).c_str());
|
---|
7028 | throw S_OK;
|
---|
7029 | }
|
---|
7030 | }
|
---|
7031 | /* zap the information, these are no long-term members */
|
---|
7032 | alock.acquire();
|
---|
7033 | unconst(m->uuidImage).clear();
|
---|
7034 | unconst(m->uuidParentImage).clear();
|
---|
7035 | alock.release();
|
---|
7036 |
|
---|
7037 | /* check the UUID */
|
---|
7038 | RTUUID uuid;
|
---|
7039 | vrc = VDGetUuid(hdd, 0, &uuid);
|
---|
7040 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7041 |
|
---|
7042 | if (isImport)
|
---|
7043 | {
|
---|
7044 | mediumId = uuid;
|
---|
7045 |
|
---|
7046 | if (mediumId.isZero() && (m->hddOpenMode == OpenReadOnly))
|
---|
7047 | // only when importing a VDMK that has no UUID, create one in memory
|
---|
7048 | mediumId.create();
|
---|
7049 | }
|
---|
7050 | else
|
---|
7051 | {
|
---|
7052 | Assert(!mediumId.isZero());
|
---|
7053 |
|
---|
7054 | if (mediumId != uuid)
|
---|
7055 | {
|
---|
7056 | /** @todo r=klaus this always refers to VirtualBox.xml as the medium registry, even for new VMs */
|
---|
7057 | lastAccessError = Utf8StrFmt(
|
---|
7058 | tr("UUID {%RTuuid} of the medium '%s' does not match the value {%RTuuid} stored in the media registry ('%s')"),
|
---|
7059 | &uuid,
|
---|
7060 | location.c_str(),
|
---|
7061 | mediumId.raw(),
|
---|
7062 | pVirtualBox->i_settingsFilePath().c_str());
|
---|
7063 | throw S_OK;
|
---|
7064 | }
|
---|
7065 | }
|
---|
7066 | }
|
---|
7067 | else
|
---|
7068 | {
|
---|
7069 | /* the backend does not support storing UUIDs within the
|
---|
7070 | * underlying storage so use what we store in XML */
|
---|
7071 |
|
---|
7072 | if (fSetImageId)
|
---|
7073 | {
|
---|
7074 | /* set the UUID if an API client wants to change it */
|
---|
7075 | alock.acquire();
|
---|
7076 | mediumId = m->uuidImage;
|
---|
7077 | alock.release();
|
---|
7078 | }
|
---|
7079 | else if (isImport)
|
---|
7080 | {
|
---|
7081 | /* generate an UUID for an imported UUID-less medium */
|
---|
7082 | mediumId.create();
|
---|
7083 | }
|
---|
7084 | }
|
---|
7085 |
|
---|
7086 | /* set the image uuid before the below parent uuid handling code
|
---|
7087 | * might place it somewhere in the media tree, so that the medium
|
---|
7088 | * UUID is valid at this point */
|
---|
7089 | alock.acquire();
|
---|
7090 | if (isImport || fSetImageId)
|
---|
7091 | unconst(m->id) = mediumId;
|
---|
7092 | alock.release();
|
---|
7093 |
|
---|
7094 | /* get the medium variant */
|
---|
7095 | unsigned uImageFlags;
|
---|
7096 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
7097 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7098 | alock.acquire();
|
---|
7099 | m->variant = (MediumVariant_T)uImageFlags;
|
---|
7100 | alock.release();
|
---|
7101 |
|
---|
7102 | /* check/get the parent uuid and update corresponding state */
|
---|
7103 | if (uImageFlags & VD_IMAGE_FLAGS_DIFF)
|
---|
7104 | {
|
---|
7105 | RTUUID parentId;
|
---|
7106 | vrc = VDGetParentUuid(hdd, 0, &parentId);
|
---|
7107 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7108 |
|
---|
7109 | /* streamOptimized VMDK images are only accepted as base
|
---|
7110 | * images, as this allows automatic repair of OVF appliances.
|
---|
7111 | * Since such images don't support random writes they will not
|
---|
7112 | * be created for diff images. Only an overly smart user might
|
---|
7113 | * manually create this case. Too bad for him. */
|
---|
7114 | if ( (isImport || fSetParentId)
|
---|
7115 | && !(uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
|
---|
7116 | {
|
---|
7117 | /* the parent must be known to us. Note that we freely
|
---|
7118 | * call locking methods of mVirtualBox and parent, as all
|
---|
7119 | * relevant locks must be already held. There may be no
|
---|
7120 | * concurrent access to the just opened medium on other
|
---|
7121 | * threads yet (and init() will fail if this method reports
|
---|
7122 | * MediumState_Inaccessible) */
|
---|
7123 |
|
---|
7124 | ComObjPtr<Medium> pParent;
|
---|
7125 | if (RTUuidIsNull(&parentId))
|
---|
7126 | rc = VBOX_E_OBJECT_NOT_FOUND;
|
---|
7127 | else
|
---|
7128 | rc = pVirtualBox->i_findHardDiskById(Guid(parentId), false /* aSetError */, &pParent);
|
---|
7129 | if (FAILED(rc))
|
---|
7130 | {
|
---|
7131 | if (fSetImageId && !fSetParentId)
|
---|
7132 | {
|
---|
7133 | /* If the image UUID gets changed for an existing
|
---|
7134 | * image then the parent UUID can be stale. In such
|
---|
7135 | * cases clear the parent information. The parent
|
---|
7136 | * information may/will be re-set later if the
|
---|
7137 | * API client wants to adjust a complete medium
|
---|
7138 | * hierarchy one by one. */
|
---|
7139 | rc = S_OK;
|
---|
7140 | alock.acquire();
|
---|
7141 | RTUuidClear(&parentId);
|
---|
7142 | vrc = VDSetParentUuid(hdd, 0, &parentId);
|
---|
7143 | alock.release();
|
---|
7144 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7145 | }
|
---|
7146 | else
|
---|
7147 | {
|
---|
7148 | lastAccessError = Utf8StrFmt(tr("Parent medium with UUID {%RTuuid} of the medium '%s' is not found in the media registry ('%s')"),
|
---|
7149 | &parentId, location.c_str(),
|
---|
7150 | pVirtualBox->i_settingsFilePath().c_str());
|
---|
7151 | throw S_OK;
|
---|
7152 | }
|
---|
7153 | }
|
---|
7154 |
|
---|
7155 | /* must drop the caller before taking the tree lock */
|
---|
7156 | autoCaller.release();
|
---|
7157 | /* we set m->pParent & children() */
|
---|
7158 | treeLock.acquire();
|
---|
7159 | autoCaller.add();
|
---|
7160 | if (FAILED(autoCaller.rc()))
|
---|
7161 | throw autoCaller.rc();
|
---|
7162 |
|
---|
7163 | if (m->pParent)
|
---|
7164 | i_deparent();
|
---|
7165 |
|
---|
7166 | if (!pParent.isNull())
|
---|
7167 | if (pParent->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
7168 | {
|
---|
7169 | AutoReadLock plock(pParent COMMA_LOCKVAL_SRC_POS);
|
---|
7170 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7171 | 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"),
|
---|
7172 | pParent->m->strLocationFull.c_str());
|
---|
7173 | }
|
---|
7174 | i_setParent(pParent);
|
---|
7175 |
|
---|
7176 | treeLock.release();
|
---|
7177 | }
|
---|
7178 | else
|
---|
7179 | {
|
---|
7180 | /* must drop the caller before taking the tree lock */
|
---|
7181 | autoCaller.release();
|
---|
7182 | /* we access m->pParent */
|
---|
7183 | treeLock.acquire();
|
---|
7184 | autoCaller.add();
|
---|
7185 | if (FAILED(autoCaller.rc()))
|
---|
7186 | throw autoCaller.rc();
|
---|
7187 |
|
---|
7188 | /* check that parent UUIDs match. Note that there's no need
|
---|
7189 | * for the parent's AutoCaller (our lifetime is bound to
|
---|
7190 | * it) */
|
---|
7191 |
|
---|
7192 | if (m->pParent.isNull())
|
---|
7193 | {
|
---|
7194 | /* Due to a bug in VDCopy() in VirtualBox 3.0.0-3.0.14
|
---|
7195 | * and 3.1.0-3.1.8 there are base images out there
|
---|
7196 | * which have a non-zero parent UUID. No point in
|
---|
7197 | * complaining about them, instead automatically
|
---|
7198 | * repair the problem. Later we can bring back the
|
---|
7199 | * error message, but we should wait until really
|
---|
7200 | * most users have repaired their images, either with
|
---|
7201 | * VBoxFixHdd or this way. */
|
---|
7202 | #if 1
|
---|
7203 | fRepairImageZeroParentUuid = true;
|
---|
7204 | #else /* 0 */
|
---|
7205 | lastAccessError = Utf8StrFmt(
|
---|
7206 | tr("Medium type of '%s' is differencing but it is not associated with any parent medium in the media registry ('%s')"),
|
---|
7207 | location.c_str(),
|
---|
7208 | pVirtualBox->settingsFilePath().c_str());
|
---|
7209 | treeLock.release();
|
---|
7210 | throw S_OK;
|
---|
7211 | #endif /* 0 */
|
---|
7212 | }
|
---|
7213 |
|
---|
7214 | {
|
---|
7215 | autoCaller.release();
|
---|
7216 | AutoReadLock parentLock(m->pParent COMMA_LOCKVAL_SRC_POS);
|
---|
7217 | autoCaller.add();
|
---|
7218 | if (FAILED(autoCaller.rc()))
|
---|
7219 | throw autoCaller.rc();
|
---|
7220 |
|
---|
7221 | if ( !fRepairImageZeroParentUuid
|
---|
7222 | && m->pParent->i_getState() != MediumState_Inaccessible
|
---|
7223 | && m->pParent->i_getId() != parentId)
|
---|
7224 | {
|
---|
7225 | /** @todo r=klaus this always refers to VirtualBox.xml as the medium registry, even for new VMs */
|
---|
7226 | lastAccessError = Utf8StrFmt(
|
---|
7227 | tr("Parent UUID {%RTuuid} of the medium '%s' does not match UUID {%RTuuid} of its parent medium stored in the media registry ('%s')"),
|
---|
7228 | &parentId, location.c_str(),
|
---|
7229 | m->pParent->i_getId().raw(),
|
---|
7230 | pVirtualBox->i_settingsFilePath().c_str());
|
---|
7231 | parentLock.release();
|
---|
7232 | treeLock.release();
|
---|
7233 | throw S_OK;
|
---|
7234 | }
|
---|
7235 | }
|
---|
7236 |
|
---|
7237 | /// @todo NEWMEDIA what to do if the parent is not
|
---|
7238 | /// accessible while the diff is? Probably nothing. The
|
---|
7239 | /// real code will detect the mismatch anyway.
|
---|
7240 |
|
---|
7241 | treeLock.release();
|
---|
7242 | }
|
---|
7243 | }
|
---|
7244 |
|
---|
7245 | mediumSize = VDGetFileSize(hdd, 0);
|
---|
7246 | mediumLogicalSize = VDGetSize(hdd, 0);
|
---|
7247 |
|
---|
7248 | success = true;
|
---|
7249 | }
|
---|
7250 | catch (HRESULT aRC)
|
---|
7251 | {
|
---|
7252 | rc = aRC;
|
---|
7253 | }
|
---|
7254 |
|
---|
7255 | vrc = VDDestroy(hdd);
|
---|
7256 | if (RT_FAILURE(vrc))
|
---|
7257 | {
|
---|
7258 | lastAccessError = Utf8StrFmt(tr("Could not update and close the medium '%s'%s"),
|
---|
7259 | location.c_str(), i_vdError(vrc).c_str());
|
---|
7260 | success = false;
|
---|
7261 | throw S_OK;
|
---|
7262 | }
|
---|
7263 | }
|
---|
7264 | catch (HRESULT aRC)
|
---|
7265 | {
|
---|
7266 | rc = aRC;
|
---|
7267 | }
|
---|
7268 |
|
---|
7269 | autoCaller.release();
|
---|
7270 | treeLock.acquire();
|
---|
7271 | autoCaller.add();
|
---|
7272 | if (FAILED(autoCaller.rc()))
|
---|
7273 | {
|
---|
7274 | m->queryInfoRunning = false;
|
---|
7275 | return autoCaller.rc();
|
---|
7276 | }
|
---|
7277 | alock.acquire();
|
---|
7278 |
|
---|
7279 | if (success)
|
---|
7280 | {
|
---|
7281 | m->size = mediumSize;
|
---|
7282 | m->logicalSize = mediumLogicalSize;
|
---|
7283 | m->strLastAccessError.setNull();
|
---|
7284 | }
|
---|
7285 | else
|
---|
7286 | {
|
---|
7287 | m->strLastAccessError = lastAccessError;
|
---|
7288 | Log1WarningFunc(("'%s' is not accessible (error='%s', rc=%Rhrc, vrc=%Rrc)\n",
|
---|
7289 | location.c_str(), m->strLastAccessError.c_str(), rc, vrc));
|
---|
7290 | }
|
---|
7291 |
|
---|
7292 | /* Set the proper state according to the result of the check */
|
---|
7293 | if (success)
|
---|
7294 | m->preLockState = MediumState_Created;
|
---|
7295 | else
|
---|
7296 | m->preLockState = MediumState_Inaccessible;
|
---|
7297 |
|
---|
7298 | /* unblock anyone waiting for the i_queryInfo results */
|
---|
7299 | qlock.release();
|
---|
7300 | m->queryInfoRunning = false;
|
---|
7301 |
|
---|
7302 | pToken->Abandon();
|
---|
7303 | pToken.setNull();
|
---|
7304 |
|
---|
7305 | if (FAILED(rc))
|
---|
7306 | return rc;
|
---|
7307 |
|
---|
7308 | /* If this is a base image which incorrectly has a parent UUID set,
|
---|
7309 | * repair the image now by zeroing the parent UUID. This is only done
|
---|
7310 | * when we have structural information from a config file, on import
|
---|
7311 | * this is not possible. If someone would accidentally call openMedium
|
---|
7312 | * with a diff image before the base is registered this would destroy
|
---|
7313 | * the diff. Not acceptable. */
|
---|
7314 | do
|
---|
7315 | {
|
---|
7316 | if (fRepairImageZeroParentUuid)
|
---|
7317 | {
|
---|
7318 | rc = LockWrite(pToken.asOutParam());
|
---|
7319 | if (FAILED(rc))
|
---|
7320 | break;
|
---|
7321 |
|
---|
7322 | alock.release();
|
---|
7323 |
|
---|
7324 | try
|
---|
7325 | {
|
---|
7326 | PVDISK hdd;
|
---|
7327 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
7328 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7329 |
|
---|
7330 | try
|
---|
7331 | {
|
---|
7332 | vrc = VDOpen(hdd,
|
---|
7333 | format.c_str(),
|
---|
7334 | location.c_str(),
|
---|
7335 | (uOpenFlags & ~VD_OPEN_FLAGS_READONLY) | m->uOpenFlagsDef,
|
---|
7336 | m->vdImageIfaces);
|
---|
7337 | if (RT_FAILURE(vrc))
|
---|
7338 | throw S_OK;
|
---|
7339 |
|
---|
7340 | RTUUID zeroParentUuid;
|
---|
7341 | RTUuidClear(&zeroParentUuid);
|
---|
7342 | vrc = VDSetParentUuid(hdd, 0, &zeroParentUuid);
|
---|
7343 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7344 | }
|
---|
7345 | catch (HRESULT aRC)
|
---|
7346 | {
|
---|
7347 | rc = aRC;
|
---|
7348 | }
|
---|
7349 |
|
---|
7350 | VDDestroy(hdd);
|
---|
7351 | }
|
---|
7352 | catch (HRESULT aRC)
|
---|
7353 | {
|
---|
7354 | rc = aRC;
|
---|
7355 | }
|
---|
7356 |
|
---|
7357 | pToken->Abandon();
|
---|
7358 | pToken.setNull();
|
---|
7359 | if (FAILED(rc))
|
---|
7360 | break;
|
---|
7361 | }
|
---|
7362 | } while(0);
|
---|
7363 |
|
---|
7364 | return rc;
|
---|
7365 | }
|
---|
7366 |
|
---|
7367 | /**
|
---|
7368 | * Performs extra checks if the medium can be closed and returns S_OK in
|
---|
7369 | * this case. Otherwise, returns a respective error message. Called by
|
---|
7370 | * Close() under the medium tree lock and the medium lock.
|
---|
7371 | *
|
---|
7372 | * @note Also reused by Medium::Reset().
|
---|
7373 | *
|
---|
7374 | * @note Caller must hold the media tree write lock!
|
---|
7375 | */
|
---|
7376 | HRESULT Medium::i_canClose()
|
---|
7377 | {
|
---|
7378 | Assert(m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
7379 |
|
---|
7380 | if (i_getChildren().size() != 0)
|
---|
7381 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
7382 | tr("Cannot close medium '%s' because it has %d child media"),
|
---|
7383 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
7384 |
|
---|
7385 | return S_OK;
|
---|
7386 | }
|
---|
7387 |
|
---|
7388 | /**
|
---|
7389 | * Unregisters this medium with mVirtualBox. Called by close() under the medium tree lock.
|
---|
7390 | *
|
---|
7391 | * @note Caller must have locked the media tree lock for writing!
|
---|
7392 | */
|
---|
7393 | HRESULT Medium::i_unregisterWithVirtualBox()
|
---|
7394 | {
|
---|
7395 | /* Note that we need to de-associate ourselves from the parent to let
|
---|
7396 | * VirtualBox::i_unregisterMedium() properly save the registry */
|
---|
7397 |
|
---|
7398 | /* we modify m->pParent and access children */
|
---|
7399 | Assert(m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
7400 |
|
---|
7401 | Medium *pParentBackup = m->pParent;
|
---|
7402 | AssertReturn(i_getChildren().size() == 0, E_FAIL);
|
---|
7403 | if (m->pParent)
|
---|
7404 | i_deparent();
|
---|
7405 |
|
---|
7406 | HRESULT rc = m->pVirtualBox->i_unregisterMedium(this);
|
---|
7407 | if (FAILED(rc))
|
---|
7408 | {
|
---|
7409 | if (pParentBackup)
|
---|
7410 | {
|
---|
7411 | // re-associate with the parent as we are still relatives in the registry
|
---|
7412 | i_setParent(pParentBackup);
|
---|
7413 | }
|
---|
7414 | }
|
---|
7415 |
|
---|
7416 | return rc;
|
---|
7417 | }
|
---|
7418 |
|
---|
7419 | /**
|
---|
7420 | * Like SetProperty but do not trigger a settings store. Only for internal use!
|
---|
7421 | */
|
---|
7422 | HRESULT Medium::i_setPropertyDirect(const Utf8Str &aName, const Utf8Str &aValue)
|
---|
7423 | {
|
---|
7424 | AutoCaller autoCaller(this);
|
---|
7425 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
7426 |
|
---|
7427 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7428 |
|
---|
7429 | switch (m->state)
|
---|
7430 | {
|
---|
7431 | case MediumState_Created:
|
---|
7432 | case MediumState_Inaccessible:
|
---|
7433 | break;
|
---|
7434 | default:
|
---|
7435 | return i_setStateError();
|
---|
7436 | }
|
---|
7437 |
|
---|
7438 | m->mapProperties[aName] = aValue;
|
---|
7439 |
|
---|
7440 | return S_OK;
|
---|
7441 | }
|
---|
7442 |
|
---|
7443 | /**
|
---|
7444 | * Sets the extended error info according to the current media state.
|
---|
7445 | *
|
---|
7446 | * @note Must be called from under this object's write or read lock.
|
---|
7447 | */
|
---|
7448 | HRESULT Medium::i_setStateError()
|
---|
7449 | {
|
---|
7450 | HRESULT rc = E_FAIL;
|
---|
7451 |
|
---|
7452 | switch (m->state)
|
---|
7453 | {
|
---|
7454 | case MediumState_NotCreated:
|
---|
7455 | {
|
---|
7456 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7457 | tr("Storage for the medium '%s' is not created"),
|
---|
7458 | m->strLocationFull.c_str());
|
---|
7459 | break;
|
---|
7460 | }
|
---|
7461 | case MediumState_Created:
|
---|
7462 | {
|
---|
7463 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7464 | tr("Storage for the medium '%s' is already created"),
|
---|
7465 | m->strLocationFull.c_str());
|
---|
7466 | break;
|
---|
7467 | }
|
---|
7468 | case MediumState_LockedRead:
|
---|
7469 | {
|
---|
7470 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7471 | tr("Medium '%s' is locked for reading by another task"),
|
---|
7472 | m->strLocationFull.c_str());
|
---|
7473 | break;
|
---|
7474 | }
|
---|
7475 | case MediumState_LockedWrite:
|
---|
7476 | {
|
---|
7477 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7478 | tr("Medium '%s' is locked for writing by another task"),
|
---|
7479 | m->strLocationFull.c_str());
|
---|
7480 | break;
|
---|
7481 | }
|
---|
7482 | case MediumState_Inaccessible:
|
---|
7483 | {
|
---|
7484 | /* be in sync with Console::powerUpThread() */
|
---|
7485 | if (!m->strLastAccessError.isEmpty())
|
---|
7486 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7487 | tr("Medium '%s' is not accessible. %s"),
|
---|
7488 | m->strLocationFull.c_str(), m->strLastAccessError.c_str());
|
---|
7489 | else
|
---|
7490 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7491 | tr("Medium '%s' is not accessible"),
|
---|
7492 | m->strLocationFull.c_str());
|
---|
7493 | break;
|
---|
7494 | }
|
---|
7495 | case MediumState_Creating:
|
---|
7496 | {
|
---|
7497 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7498 | tr("Storage for the medium '%s' is being created"),
|
---|
7499 | m->strLocationFull.c_str());
|
---|
7500 | break;
|
---|
7501 | }
|
---|
7502 | case MediumState_Deleting:
|
---|
7503 | {
|
---|
7504 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7505 | tr("Storage for the medium '%s' is being deleted"),
|
---|
7506 | m->strLocationFull.c_str());
|
---|
7507 | break;
|
---|
7508 | }
|
---|
7509 | default:
|
---|
7510 | {
|
---|
7511 | AssertFailed();
|
---|
7512 | break;
|
---|
7513 | }
|
---|
7514 | }
|
---|
7515 |
|
---|
7516 | return rc;
|
---|
7517 | }
|
---|
7518 |
|
---|
7519 | /**
|
---|
7520 | * Sets the value of m->strLocationFull. The given location must be a fully
|
---|
7521 | * qualified path; relative paths are not supported here.
|
---|
7522 | *
|
---|
7523 | * As a special exception, if the specified location is a file path that ends with '/'
|
---|
7524 | * then the file name part will be generated by this method automatically in the format
|
---|
7525 | * '{\<uuid\>}.\<ext\>' where \<uuid\> is a fresh UUID that this method will generate
|
---|
7526 | * and assign to this medium, and \<ext\> is the default extension for this
|
---|
7527 | * medium's storage format. Note that this procedure requires the media state to
|
---|
7528 | * be NotCreated and will return a failure otherwise.
|
---|
7529 | *
|
---|
7530 | * @param aLocation Location of the storage unit. If the location is a FS-path,
|
---|
7531 | * then it can be relative to the VirtualBox home directory.
|
---|
7532 | * @param aFormat Optional fallback format if it is an import and the format
|
---|
7533 | * cannot be determined.
|
---|
7534 | *
|
---|
7535 | * @note Must be called from under this object's write lock.
|
---|
7536 | */
|
---|
7537 | HRESULT Medium::i_setLocation(const Utf8Str &aLocation,
|
---|
7538 | const Utf8Str &aFormat /* = Utf8Str::Empty */)
|
---|
7539 | {
|
---|
7540 | AssertReturn(!aLocation.isEmpty(), E_FAIL);
|
---|
7541 |
|
---|
7542 | AutoCaller autoCaller(this);
|
---|
7543 | AssertComRCReturnRC(autoCaller.rc());
|
---|
7544 |
|
---|
7545 | /* formatObj may be null only when initializing from an existing path and
|
---|
7546 | * no format is known yet */
|
---|
7547 | AssertReturn( (!m->strFormat.isEmpty() && !m->formatObj.isNull())
|
---|
7548 | || ( getObjectState().getState() == ObjectState::InInit
|
---|
7549 | && m->state != MediumState_NotCreated
|
---|
7550 | && m->id.isZero()
|
---|
7551 | && m->strFormat.isEmpty()
|
---|
7552 | && m->formatObj.isNull()),
|
---|
7553 | E_FAIL);
|
---|
7554 |
|
---|
7555 | /* are we dealing with a new medium constructed using the existing
|
---|
7556 | * location? */
|
---|
7557 | bool isImport = m->strFormat.isEmpty();
|
---|
7558 |
|
---|
7559 | if ( isImport
|
---|
7560 | || ( (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
7561 | && !m->hostDrive))
|
---|
7562 | {
|
---|
7563 | Guid id;
|
---|
7564 |
|
---|
7565 | Utf8Str locationFull(aLocation);
|
---|
7566 |
|
---|
7567 | if (m->state == MediumState_NotCreated)
|
---|
7568 | {
|
---|
7569 | /* must be a file (formatObj must be already known) */
|
---|
7570 | Assert(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File);
|
---|
7571 |
|
---|
7572 | if (RTPathFilename(aLocation.c_str()) == NULL)
|
---|
7573 | {
|
---|
7574 | /* no file name is given (either an empty string or ends with a
|
---|
7575 | * slash), generate a new UUID + file name if the state allows
|
---|
7576 | * this */
|
---|
7577 |
|
---|
7578 | ComAssertMsgRet(!m->formatObj->i_getFileExtensions().empty(),
|
---|
7579 | ("Must be at least one extension if it is MediumFormatCapabilities_File\n"),
|
---|
7580 | E_FAIL);
|
---|
7581 |
|
---|
7582 | Utf8Str strExt = m->formatObj->i_getFileExtensions().front();
|
---|
7583 | ComAssertMsgRet(!strExt.isEmpty(),
|
---|
7584 | ("Default extension must not be empty\n"),
|
---|
7585 | E_FAIL);
|
---|
7586 |
|
---|
7587 | id.create();
|
---|
7588 |
|
---|
7589 | locationFull = Utf8StrFmt("%s{%RTuuid}.%s",
|
---|
7590 | aLocation.c_str(), id.raw(), strExt.c_str());
|
---|
7591 | }
|
---|
7592 | }
|
---|
7593 |
|
---|
7594 | // we must always have full paths now (if it refers to a file)
|
---|
7595 | if ( ( m->formatObj.isNull()
|
---|
7596 | || m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
7597 | && !RTPathStartsWithRoot(locationFull.c_str()))
|
---|
7598 | return setError(VBOX_E_FILE_ERROR,
|
---|
7599 | tr("The given path '%s' is not fully qualified"),
|
---|
7600 | locationFull.c_str());
|
---|
7601 |
|
---|
7602 | /* detect the backend from the storage unit if importing */
|
---|
7603 | if (isImport)
|
---|
7604 | {
|
---|
7605 | VDTYPE enmType = VDTYPE_INVALID;
|
---|
7606 | char *backendName = NULL;
|
---|
7607 |
|
---|
7608 | int vrc = VINF_SUCCESS;
|
---|
7609 |
|
---|
7610 | /* is it a file? */
|
---|
7611 | {
|
---|
7612 | RTFILE file;
|
---|
7613 | vrc = RTFileOpen(&file, locationFull.c_str(), RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
7614 | if (RT_SUCCESS(vrc))
|
---|
7615 | RTFileClose(file);
|
---|
7616 | }
|
---|
7617 | if (RT_SUCCESS(vrc))
|
---|
7618 | {
|
---|
7619 | vrc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
|
---|
7620 | locationFull.c_str(), &backendName, &enmType);
|
---|
7621 | }
|
---|
7622 | else if ( vrc != VERR_FILE_NOT_FOUND
|
---|
7623 | && vrc != VERR_PATH_NOT_FOUND
|
---|
7624 | && vrc != VERR_ACCESS_DENIED
|
---|
7625 | && locationFull != aLocation)
|
---|
7626 | {
|
---|
7627 | /* assume it's not a file, restore the original location */
|
---|
7628 | locationFull = aLocation;
|
---|
7629 | vrc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
|
---|
7630 | locationFull.c_str(), &backendName, &enmType);
|
---|
7631 | }
|
---|
7632 |
|
---|
7633 | if (RT_FAILURE(vrc))
|
---|
7634 | {
|
---|
7635 | if (vrc == VERR_ACCESS_DENIED)
|
---|
7636 | return setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
7637 | tr("Permission problem accessing the file for the medium '%s' (%Rrc)"),
|
---|
7638 | locationFull.c_str(), vrc);
|
---|
7639 | else if (vrc == VERR_FILE_NOT_FOUND || vrc == VERR_PATH_NOT_FOUND)
|
---|
7640 | return setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
7641 | tr("Could not find file for the medium '%s' (%Rrc)"),
|
---|
7642 | locationFull.c_str(), vrc);
|
---|
7643 | else if (aFormat.isEmpty())
|
---|
7644 | return setErrorBoth(VBOX_E_IPRT_ERROR, vrc,
|
---|
7645 | tr("Could not get the storage format of the medium '%s' (%Rrc)"),
|
---|
7646 | locationFull.c_str(), vrc);
|
---|
7647 | else
|
---|
7648 | {
|
---|
7649 | HRESULT rc = i_setFormat(aFormat);
|
---|
7650 | /* setFormat() must not fail since we've just used the backend so
|
---|
7651 | * the format object must be there */
|
---|
7652 | AssertComRCReturnRC(rc);
|
---|
7653 | }
|
---|
7654 | }
|
---|
7655 | else if ( enmType == VDTYPE_INVALID
|
---|
7656 | || m->devType != i_convertToDeviceType(enmType))
|
---|
7657 | {
|
---|
7658 | /*
|
---|
7659 | * The user tried to use a image as a device which is not supported
|
---|
7660 | * by the backend.
|
---|
7661 | */
|
---|
7662 | return setError(E_FAIL,
|
---|
7663 | tr("The medium '%s' can't be used as the requested device type"),
|
---|
7664 | locationFull.c_str());
|
---|
7665 | }
|
---|
7666 | else
|
---|
7667 | {
|
---|
7668 | ComAssertRet(backendName != NULL && *backendName != '\0', E_FAIL);
|
---|
7669 |
|
---|
7670 | HRESULT rc = i_setFormat(backendName);
|
---|
7671 | RTStrFree(backendName);
|
---|
7672 |
|
---|
7673 | /* setFormat() must not fail since we've just used the backend so
|
---|
7674 | * the format object must be there */
|
---|
7675 | AssertComRCReturnRC(rc);
|
---|
7676 | }
|
---|
7677 | }
|
---|
7678 |
|
---|
7679 | m->strLocationFull = locationFull;
|
---|
7680 |
|
---|
7681 | /* is it still a file? */
|
---|
7682 | if ( (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
7683 | && (m->state == MediumState_NotCreated)
|
---|
7684 | )
|
---|
7685 | /* assign a new UUID (this UUID will be used when calling
|
---|
7686 | * VDCreateBase/VDCreateDiff as a wanted UUID). Note that we
|
---|
7687 | * also do that if we didn't generate it to make sure it is
|
---|
7688 | * either generated by us or reset to null */
|
---|
7689 | unconst(m->id) = id;
|
---|
7690 | }
|
---|
7691 | else
|
---|
7692 | m->strLocationFull = aLocation;
|
---|
7693 |
|
---|
7694 | return S_OK;
|
---|
7695 | }
|
---|
7696 |
|
---|
7697 | /**
|
---|
7698 | * Checks that the format ID is valid and sets it on success.
|
---|
7699 | *
|
---|
7700 | * Note that this method will caller-reference the format object on success!
|
---|
7701 | * This reference must be released somewhere to let the MediumFormat object be
|
---|
7702 | * uninitialized.
|
---|
7703 | *
|
---|
7704 | * @note Must be called from under this object's write lock.
|
---|
7705 | */
|
---|
7706 | HRESULT Medium::i_setFormat(const Utf8Str &aFormat)
|
---|
7707 | {
|
---|
7708 | /* get the format object first */
|
---|
7709 | {
|
---|
7710 | SystemProperties *pSysProps = m->pVirtualBox->i_getSystemProperties();
|
---|
7711 | AutoReadLock propsLock(pSysProps COMMA_LOCKVAL_SRC_POS);
|
---|
7712 |
|
---|
7713 | unconst(m->formatObj) = pSysProps->i_mediumFormat(aFormat);
|
---|
7714 | if (m->formatObj.isNull())
|
---|
7715 | return setError(E_INVALIDARG,
|
---|
7716 | tr("Invalid medium storage format '%s'"),
|
---|
7717 | aFormat.c_str());
|
---|
7718 |
|
---|
7719 | /* get properties (preinsert them as keys in the map). Note that the
|
---|
7720 | * map doesn't grow over the object life time since the set of
|
---|
7721 | * properties is meant to be constant. */
|
---|
7722 |
|
---|
7723 | Assert(m->mapProperties.empty());
|
---|
7724 |
|
---|
7725 | for (MediumFormat::PropertyArray::const_iterator it = m->formatObj->i_getProperties().begin();
|
---|
7726 | it != m->formatObj->i_getProperties().end();
|
---|
7727 | ++it)
|
---|
7728 | {
|
---|
7729 | m->mapProperties.insert(std::make_pair(it->strName, Utf8Str::Empty));
|
---|
7730 | }
|
---|
7731 | }
|
---|
7732 |
|
---|
7733 | unconst(m->strFormat) = aFormat;
|
---|
7734 |
|
---|
7735 | return S_OK;
|
---|
7736 | }
|
---|
7737 |
|
---|
7738 | /**
|
---|
7739 | * Converts the Medium device type to the VD type.
|
---|
7740 | */
|
---|
7741 | VDTYPE Medium::i_convertDeviceType()
|
---|
7742 | {
|
---|
7743 | VDTYPE enmType;
|
---|
7744 |
|
---|
7745 | switch (m->devType)
|
---|
7746 | {
|
---|
7747 | case DeviceType_HardDisk:
|
---|
7748 | enmType = VDTYPE_HDD;
|
---|
7749 | break;
|
---|
7750 | case DeviceType_DVD:
|
---|
7751 | enmType = VDTYPE_OPTICAL_DISC;
|
---|
7752 | break;
|
---|
7753 | case DeviceType_Floppy:
|
---|
7754 | enmType = VDTYPE_FLOPPY;
|
---|
7755 | break;
|
---|
7756 | default:
|
---|
7757 | ComAssertFailedRet(VDTYPE_INVALID);
|
---|
7758 | }
|
---|
7759 |
|
---|
7760 | return enmType;
|
---|
7761 | }
|
---|
7762 |
|
---|
7763 | /**
|
---|
7764 | * Converts from the VD type to the medium type.
|
---|
7765 | */
|
---|
7766 | DeviceType_T Medium::i_convertToDeviceType(VDTYPE enmType)
|
---|
7767 | {
|
---|
7768 | DeviceType_T devType;
|
---|
7769 |
|
---|
7770 | switch (enmType)
|
---|
7771 | {
|
---|
7772 | case VDTYPE_HDD:
|
---|
7773 | devType = DeviceType_HardDisk;
|
---|
7774 | break;
|
---|
7775 | case VDTYPE_OPTICAL_DISC:
|
---|
7776 | devType = DeviceType_DVD;
|
---|
7777 | break;
|
---|
7778 | case VDTYPE_FLOPPY:
|
---|
7779 | devType = DeviceType_Floppy;
|
---|
7780 | break;
|
---|
7781 | default:
|
---|
7782 | ComAssertFailedRet(DeviceType_Null);
|
---|
7783 | }
|
---|
7784 |
|
---|
7785 | return devType;
|
---|
7786 | }
|
---|
7787 |
|
---|
7788 | /**
|
---|
7789 | * Internal method which checks whether a property name is for a filter plugin.
|
---|
7790 | */
|
---|
7791 | bool Medium::i_isPropertyForFilter(const com::Utf8Str &aName)
|
---|
7792 | {
|
---|
7793 | /* If the name contains "/" use the part before as a filter name and lookup the filter. */
|
---|
7794 | size_t offSlash;
|
---|
7795 | if ((offSlash = aName.find("/", 0)) != aName.npos)
|
---|
7796 | {
|
---|
7797 | com::Utf8Str strFilter;
|
---|
7798 | com::Utf8Str strKey;
|
---|
7799 |
|
---|
7800 | HRESULT rc = strFilter.assignEx(aName, 0, offSlash);
|
---|
7801 | if (FAILED(rc))
|
---|
7802 | return false;
|
---|
7803 |
|
---|
7804 | rc = strKey.assignEx(aName, offSlash + 1, aName.length() - offSlash - 1); /* Skip slash */
|
---|
7805 | if (FAILED(rc))
|
---|
7806 | return false;
|
---|
7807 |
|
---|
7808 | VDFILTERINFO FilterInfo;
|
---|
7809 | int vrc = VDFilterInfoOne(strFilter.c_str(), &FilterInfo);
|
---|
7810 | if (RT_SUCCESS(vrc))
|
---|
7811 | {
|
---|
7812 | /* Check that the property exists. */
|
---|
7813 | PCVDCONFIGINFO paConfig = FilterInfo.paConfigInfo;
|
---|
7814 | while (paConfig->pszKey)
|
---|
7815 | {
|
---|
7816 | if (strKey.equals(paConfig->pszKey))
|
---|
7817 | return true;
|
---|
7818 | paConfig++;
|
---|
7819 | }
|
---|
7820 | }
|
---|
7821 | }
|
---|
7822 |
|
---|
7823 | return false;
|
---|
7824 | }
|
---|
7825 |
|
---|
7826 | /**
|
---|
7827 | * Returns the last error message collected by the i_vdErrorCall callback and
|
---|
7828 | * resets it.
|
---|
7829 | *
|
---|
7830 | * The error message is returned prepended with a dot and a space, like this:
|
---|
7831 | * <code>
|
---|
7832 | * ". <error_text> (%Rrc)"
|
---|
7833 | * </code>
|
---|
7834 | * to make it easily appendable to a more general error message. The @c %Rrc
|
---|
7835 | * format string is given @a aVRC as an argument.
|
---|
7836 | *
|
---|
7837 | * If there is no last error message collected by i_vdErrorCall or if it is a
|
---|
7838 | * null or empty string, then this function returns the following text:
|
---|
7839 | * <code>
|
---|
7840 | * " (%Rrc)"
|
---|
7841 | * </code>
|
---|
7842 | *
|
---|
7843 | * @note Doesn't do any object locking; it is assumed that the caller makes sure
|
---|
7844 | * the callback isn't called by more than one thread at a time.
|
---|
7845 | *
|
---|
7846 | * @param aVRC VBox error code to use when no error message is provided.
|
---|
7847 | */
|
---|
7848 | Utf8Str Medium::i_vdError(int aVRC)
|
---|
7849 | {
|
---|
7850 | Utf8Str error;
|
---|
7851 |
|
---|
7852 | if (m->vdError.isEmpty())
|
---|
7853 | error = Utf8StrFmt(" (%Rrc)", aVRC);
|
---|
7854 | else
|
---|
7855 | error = Utf8StrFmt(".\n%s", m->vdError.c_str());
|
---|
7856 |
|
---|
7857 | m->vdError.setNull();
|
---|
7858 |
|
---|
7859 | return error;
|
---|
7860 | }
|
---|
7861 |
|
---|
7862 | /**
|
---|
7863 | * Error message callback.
|
---|
7864 | *
|
---|
7865 | * Puts the reported error message to the m->vdError field.
|
---|
7866 | *
|
---|
7867 | * @note Doesn't do any object locking; it is assumed that the caller makes sure
|
---|
7868 | * the callback isn't called by more than one thread at a time.
|
---|
7869 | *
|
---|
7870 | * @param pvUser The opaque data passed on container creation.
|
---|
7871 | * @param rc The VBox error code.
|
---|
7872 | * @param SRC_POS Use RT_SRC_POS.
|
---|
7873 | * @param pszFormat Error message format string.
|
---|
7874 | * @param va Error message arguments.
|
---|
7875 | */
|
---|
7876 | /*static*/
|
---|
7877 | DECLCALLBACK(void) Medium::i_vdErrorCall(void *pvUser, int rc, RT_SRC_POS_DECL,
|
---|
7878 | const char *pszFormat, va_list va)
|
---|
7879 | {
|
---|
7880 | NOREF(pszFile); NOREF(iLine); NOREF(pszFunction); /* RT_SRC_POS_DECL */
|
---|
7881 |
|
---|
7882 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
7883 | AssertReturnVoid(that != NULL);
|
---|
7884 |
|
---|
7885 | if (that->m->vdError.isEmpty())
|
---|
7886 | that->m->vdError =
|
---|
7887 | Utf8StrFmt("%s (%Rrc)", Utf8Str(pszFormat, va).c_str(), rc);
|
---|
7888 | else
|
---|
7889 | that->m->vdError =
|
---|
7890 | Utf8StrFmt("%s.\n%s (%Rrc)", that->m->vdError.c_str(),
|
---|
7891 | Utf8Str(pszFormat, va).c_str(), rc);
|
---|
7892 | }
|
---|
7893 |
|
---|
7894 | /* static */
|
---|
7895 | DECLCALLBACK(bool) Medium::i_vdConfigAreKeysValid(void *pvUser,
|
---|
7896 | const char * /* pszzValid */)
|
---|
7897 | {
|
---|
7898 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
7899 | AssertReturn(that != NULL, false);
|
---|
7900 |
|
---|
7901 | /* we always return true since the only keys we have are those found in
|
---|
7902 | * VDBACKENDINFO */
|
---|
7903 | return true;
|
---|
7904 | }
|
---|
7905 |
|
---|
7906 | /* static */
|
---|
7907 | DECLCALLBACK(int) Medium::i_vdConfigQuerySize(void *pvUser,
|
---|
7908 | const char *pszName,
|
---|
7909 | size_t *pcbValue)
|
---|
7910 | {
|
---|
7911 | AssertReturn(VALID_PTR(pcbValue), VERR_INVALID_POINTER);
|
---|
7912 |
|
---|
7913 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
7914 | AssertReturn(that != NULL, VERR_GENERAL_FAILURE);
|
---|
7915 |
|
---|
7916 | settings::StringsMap::const_iterator it = that->m->mapProperties.find(Utf8Str(pszName));
|
---|
7917 | if (it == that->m->mapProperties.end())
|
---|
7918 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7919 |
|
---|
7920 | /* we interpret null values as "no value" in Medium */
|
---|
7921 | if (it->second.isEmpty())
|
---|
7922 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7923 |
|
---|
7924 | *pcbValue = it->second.length() + 1 /* include terminator */;
|
---|
7925 |
|
---|
7926 | return VINF_SUCCESS;
|
---|
7927 | }
|
---|
7928 |
|
---|
7929 | /* static */
|
---|
7930 | DECLCALLBACK(int) Medium::i_vdConfigQuery(void *pvUser,
|
---|
7931 | const char *pszName,
|
---|
7932 | char *pszValue,
|
---|
7933 | size_t cchValue)
|
---|
7934 | {
|
---|
7935 | AssertReturn(VALID_PTR(pszValue), VERR_INVALID_POINTER);
|
---|
7936 |
|
---|
7937 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
7938 | AssertReturn(that != NULL, VERR_GENERAL_FAILURE);
|
---|
7939 |
|
---|
7940 | settings::StringsMap::const_iterator it = that->m->mapProperties.find(Utf8Str(pszName));
|
---|
7941 | if (it == that->m->mapProperties.end())
|
---|
7942 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7943 |
|
---|
7944 | /* we interpret null values as "no value" in Medium */
|
---|
7945 | if (it->second.isEmpty())
|
---|
7946 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7947 |
|
---|
7948 | const Utf8Str &value = it->second;
|
---|
7949 | if (value.length() >= cchValue)
|
---|
7950 | return VERR_CFGM_NOT_ENOUGH_SPACE;
|
---|
7951 |
|
---|
7952 | memcpy(pszValue, value.c_str(), value.length() + 1);
|
---|
7953 |
|
---|
7954 | return VINF_SUCCESS;
|
---|
7955 | }
|
---|
7956 |
|
---|
7957 | DECLCALLBACK(bool) Medium::i_vdCryptoConfigAreKeysValid(void *pvUser, const char *pszzValid)
|
---|
7958 | {
|
---|
7959 | /* Just return always true here. */
|
---|
7960 | NOREF(pvUser);
|
---|
7961 | NOREF(pszzValid);
|
---|
7962 | return true;
|
---|
7963 | }
|
---|
7964 |
|
---|
7965 | DECLCALLBACK(int) Medium::i_vdCryptoConfigQuerySize(void *pvUser, const char *pszName, size_t *pcbValue)
|
---|
7966 | {
|
---|
7967 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
7968 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7969 | AssertReturn(VALID_PTR(pcbValue), VERR_INVALID_POINTER);
|
---|
7970 |
|
---|
7971 | size_t cbValue = 0;
|
---|
7972 | if (!strcmp(pszName, "Algorithm"))
|
---|
7973 | cbValue = strlen(pSettings->pszCipher) + 1;
|
---|
7974 | else if (!strcmp(pszName, "KeyId"))
|
---|
7975 | cbValue = sizeof("irrelevant");
|
---|
7976 | else if (!strcmp(pszName, "KeyStore"))
|
---|
7977 | {
|
---|
7978 | if (!pSettings->pszKeyStoreLoad)
|
---|
7979 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7980 | cbValue = strlen(pSettings->pszKeyStoreLoad) + 1;
|
---|
7981 | }
|
---|
7982 | else if (!strcmp(pszName, "CreateKeyStore"))
|
---|
7983 | cbValue = 2; /* Single digit + terminator. */
|
---|
7984 | else
|
---|
7985 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7986 |
|
---|
7987 | *pcbValue = cbValue + 1 /* include terminator */;
|
---|
7988 |
|
---|
7989 | return VINF_SUCCESS;
|
---|
7990 | }
|
---|
7991 |
|
---|
7992 | DECLCALLBACK(int) Medium::i_vdCryptoConfigQuery(void *pvUser, const char *pszName,
|
---|
7993 | char *pszValue, size_t cchValue)
|
---|
7994 | {
|
---|
7995 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
7996 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7997 | AssertReturn(VALID_PTR(pszValue), VERR_INVALID_POINTER);
|
---|
7998 |
|
---|
7999 | const char *psz = NULL;
|
---|
8000 | if (!strcmp(pszName, "Algorithm"))
|
---|
8001 | psz = pSettings->pszCipher;
|
---|
8002 | else if (!strcmp(pszName, "KeyId"))
|
---|
8003 | psz = "irrelevant";
|
---|
8004 | else if (!strcmp(pszName, "KeyStore"))
|
---|
8005 | psz = pSettings->pszKeyStoreLoad;
|
---|
8006 | else if (!strcmp(pszName, "CreateKeyStore"))
|
---|
8007 | {
|
---|
8008 | if (pSettings->fCreateKeyStore)
|
---|
8009 | psz = "1";
|
---|
8010 | else
|
---|
8011 | psz = "0";
|
---|
8012 | }
|
---|
8013 | else
|
---|
8014 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
8015 |
|
---|
8016 | size_t cch = strlen(psz);
|
---|
8017 | if (cch >= cchValue)
|
---|
8018 | return VERR_CFGM_NOT_ENOUGH_SPACE;
|
---|
8019 |
|
---|
8020 | memcpy(pszValue, psz, cch + 1);
|
---|
8021 | return VINF_SUCCESS;
|
---|
8022 | }
|
---|
8023 |
|
---|
8024 | DECLCALLBACK(int) Medium::i_vdCryptoKeyRetain(void *pvUser, const char *pszId,
|
---|
8025 | const uint8_t **ppbKey, size_t *pcbKey)
|
---|
8026 | {
|
---|
8027 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8028 | NOREF(pszId);
|
---|
8029 | NOREF(ppbKey);
|
---|
8030 | NOREF(pcbKey);
|
---|
8031 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8032 | AssertMsgFailedReturn(("This method should not be called here!\n"), VERR_INVALID_STATE);
|
---|
8033 | }
|
---|
8034 |
|
---|
8035 | DECLCALLBACK(int) Medium::i_vdCryptoKeyRelease(void *pvUser, const char *pszId)
|
---|
8036 | {
|
---|
8037 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8038 | NOREF(pszId);
|
---|
8039 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8040 | AssertMsgFailedReturn(("This method should not be called here!\n"), VERR_INVALID_STATE);
|
---|
8041 | }
|
---|
8042 |
|
---|
8043 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStorePasswordRetain(void *pvUser, const char *pszId, const char **ppszPassword)
|
---|
8044 | {
|
---|
8045 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8046 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8047 |
|
---|
8048 | NOREF(pszId);
|
---|
8049 | *ppszPassword = pSettings->pszPassword;
|
---|
8050 | return VINF_SUCCESS;
|
---|
8051 | }
|
---|
8052 |
|
---|
8053 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStorePasswordRelease(void *pvUser, const char *pszId)
|
---|
8054 | {
|
---|
8055 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8056 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8057 | NOREF(pszId);
|
---|
8058 | return VINF_SUCCESS;
|
---|
8059 | }
|
---|
8060 |
|
---|
8061 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStoreSave(void *pvUser, const void *pvKeyStore, size_t cbKeyStore)
|
---|
8062 | {
|
---|
8063 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8064 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8065 |
|
---|
8066 | pSettings->pszKeyStore = (char *)RTMemAllocZ(cbKeyStore);
|
---|
8067 | if (!pSettings->pszKeyStore)
|
---|
8068 | return VERR_NO_MEMORY;
|
---|
8069 |
|
---|
8070 | memcpy(pSettings->pszKeyStore, pvKeyStore, cbKeyStore);
|
---|
8071 | return VINF_SUCCESS;
|
---|
8072 | }
|
---|
8073 |
|
---|
8074 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStoreReturnParameters(void *pvUser, const char *pszCipher,
|
---|
8075 | const uint8_t *pbDek, size_t cbDek)
|
---|
8076 | {
|
---|
8077 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8078 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8079 |
|
---|
8080 | pSettings->pszCipherReturned = RTStrDup(pszCipher);
|
---|
8081 | pSettings->pbDek = pbDek;
|
---|
8082 | pSettings->cbDek = cbDek;
|
---|
8083 |
|
---|
8084 | return pSettings->pszCipherReturned ? VINF_SUCCESS : VERR_NO_MEMORY;
|
---|
8085 | }
|
---|
8086 |
|
---|
8087 | /**
|
---|
8088 | * Creates a VDISK instance for this medium.
|
---|
8089 | *
|
---|
8090 | * @note Caller should not hold any medium related locks as this method will
|
---|
8091 | * acquire the medium lock for writing and others (VirtualBox).
|
---|
8092 | *
|
---|
8093 | * @returns COM status code.
|
---|
8094 | * @param fWritable Whether to return a writable VDISK instance
|
---|
8095 | * (true) or a read-only one (false).
|
---|
8096 | * @param pKeyStore The key store.
|
---|
8097 | * @param ppHdd Where to return the pointer to the VDISK on
|
---|
8098 | * success.
|
---|
8099 | * @param pMediumLockList The lock list to populate and lock. Caller
|
---|
8100 | * is responsible for calling the destructor or
|
---|
8101 | * MediumLockList::Clear() after destroying
|
---|
8102 | * @a *ppHdd
|
---|
8103 | * @param pCryptoSettings The crypto settings to use for setting up
|
---|
8104 | * decryption/encryption of the VDISK. This object
|
---|
8105 | * must be alive until the VDISK is destroyed!
|
---|
8106 | */
|
---|
8107 | HRESULT Medium::i_openForIO(bool fWritable, SecretKeyStore *pKeyStore, PVDISK *ppHdd, MediumLockList *pMediumLockList,
|
---|
8108 | MediumCryptoFilterSettings *pCryptoSettings)
|
---|
8109 | {
|
---|
8110 | /*
|
---|
8111 | * Create the media lock list and lock the media.
|
---|
8112 | */
|
---|
8113 | HRESULT hrc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
8114 | fWritable ? this : NULL /* pToLockWrite */,
|
---|
8115 | false /* fMediumLockWriteAll */,
|
---|
8116 | NULL,
|
---|
8117 | *pMediumLockList);
|
---|
8118 | if (SUCCEEDED(hrc))
|
---|
8119 | hrc = pMediumLockList->Lock();
|
---|
8120 | if (FAILED(hrc))
|
---|
8121 | return hrc;
|
---|
8122 |
|
---|
8123 | /*
|
---|
8124 | * Get the base medium before write locking this medium.
|
---|
8125 | */
|
---|
8126 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
8127 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8128 |
|
---|
8129 | /*
|
---|
8130 | * Create the VDISK instance.
|
---|
8131 | */
|
---|
8132 | PVDISK pHdd;
|
---|
8133 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pHdd);
|
---|
8134 | AssertRCReturn(vrc, E_FAIL);
|
---|
8135 |
|
---|
8136 | /*
|
---|
8137 | * Goto avoidance using try/catch/throw(HRESULT).
|
---|
8138 | */
|
---|
8139 | try
|
---|
8140 | {
|
---|
8141 | settings::StringsMap::iterator itKeyStore = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
8142 | if (itKeyStore != pBase->m->mapProperties.end())
|
---|
8143 | {
|
---|
8144 | settings::StringsMap::iterator itKeyId = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
8145 |
|
---|
8146 | #ifdef VBOX_WITH_EXTPACK
|
---|
8147 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
8148 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
8149 | {
|
---|
8150 | /* Load the plugin */
|
---|
8151 | Utf8Str strPlugin;
|
---|
8152 | hrc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
8153 | if (SUCCEEDED(hrc))
|
---|
8154 | {
|
---|
8155 | vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
8156 | if (RT_FAILURE(vrc))
|
---|
8157 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
8158 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
8159 | i_vdError(vrc).c_str());
|
---|
8160 | }
|
---|
8161 | else
|
---|
8162 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
8163 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
8164 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
8165 | }
|
---|
8166 | else
|
---|
8167 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
8168 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
8169 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
8170 | #else
|
---|
8171 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
8172 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
8173 | #endif
|
---|
8174 |
|
---|
8175 | if (itKeyId == pBase->m->mapProperties.end())
|
---|
8176 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8177 | tr("Image '%s' is configured for encryption but doesn't has a key identifier set"),
|
---|
8178 | pBase->m->strLocationFull.c_str());
|
---|
8179 |
|
---|
8180 | /* Find the proper secret key in the key store. */
|
---|
8181 | if (!pKeyStore)
|
---|
8182 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8183 | tr("Image '%s' is configured for encryption but there is no key store to retrieve the password from"),
|
---|
8184 | pBase->m->strLocationFull.c_str());
|
---|
8185 |
|
---|
8186 | SecretKey *pKey = NULL;
|
---|
8187 | vrc = pKeyStore->retainSecretKey(itKeyId->second, &pKey);
|
---|
8188 | if (RT_FAILURE(vrc))
|
---|
8189 | throw setErrorBoth(VBOX_E_INVALID_OBJECT_STATE, vrc,
|
---|
8190 | tr("Failed to retrieve the secret key with ID \"%s\" from the store (%Rrc)"),
|
---|
8191 | itKeyId->second.c_str(), vrc);
|
---|
8192 |
|
---|
8193 | i_taskEncryptSettingsSetup(pCryptoSettings, NULL, itKeyStore->second.c_str(), (const char *)pKey->getKeyBuffer(),
|
---|
8194 | false /* fCreateKeyStore */);
|
---|
8195 | vrc = VDFilterAdd(pHdd, "CRYPT", VD_FILTER_FLAGS_DEFAULT, pCryptoSettings->vdFilterIfaces);
|
---|
8196 | pKeyStore->releaseSecretKey(itKeyId->second);
|
---|
8197 | if (vrc == VERR_VD_PASSWORD_INCORRECT)
|
---|
8198 | throw setErrorBoth(VBOX_E_PASSWORD_INCORRECT, vrc, tr("The password to decrypt the image is incorrect"));
|
---|
8199 | if (RT_FAILURE(vrc))
|
---|
8200 | throw setErrorBoth(VBOX_E_INVALID_OBJECT_STATE, vrc, tr("Failed to load the decryption filter: %s"),
|
---|
8201 | i_vdError(vrc).c_str());
|
---|
8202 | }
|
---|
8203 |
|
---|
8204 | /*
|
---|
8205 | * Open all media in the source chain.
|
---|
8206 | */
|
---|
8207 | MediumLockList::Base::const_iterator sourceListBegin = pMediumLockList->GetBegin();
|
---|
8208 | MediumLockList::Base::const_iterator sourceListEnd = pMediumLockList->GetEnd();
|
---|
8209 | MediumLockList::Base::const_iterator mediumListLast = sourceListEnd;
|
---|
8210 | --mediumListLast;
|
---|
8211 |
|
---|
8212 | for (MediumLockList::Base::const_iterator it = sourceListBegin; it != sourceListEnd; ++it)
|
---|
8213 | {
|
---|
8214 | const MediumLock &mediumLock = *it;
|
---|
8215 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8216 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8217 |
|
---|
8218 | /* sanity check */
|
---|
8219 | Assert(pMedium->m->state == (fWritable && it == mediumListLast ? MediumState_LockedWrite : MediumState_LockedRead));
|
---|
8220 |
|
---|
8221 | /* Open all media in read-only mode. */
|
---|
8222 | vrc = VDOpen(pHdd,
|
---|
8223 | pMedium->m->strFormat.c_str(),
|
---|
8224 | pMedium->m->strLocationFull.c_str(),
|
---|
8225 | m->uOpenFlagsDef | (fWritable && it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
8226 | pMedium->m->vdImageIfaces);
|
---|
8227 | if (RT_FAILURE(vrc))
|
---|
8228 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
8229 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8230 | pMedium->m->strLocationFull.c_str(),
|
---|
8231 | i_vdError(vrc).c_str());
|
---|
8232 | }
|
---|
8233 |
|
---|
8234 | Assert(m->state == (fWritable ? MediumState_LockedWrite : MediumState_LockedRead));
|
---|
8235 |
|
---|
8236 | /*
|
---|
8237 | * Done!
|
---|
8238 | */
|
---|
8239 | *ppHdd = pHdd;
|
---|
8240 | return S_OK;
|
---|
8241 | }
|
---|
8242 | catch (HRESULT hrc2)
|
---|
8243 | {
|
---|
8244 | hrc = hrc2;
|
---|
8245 | }
|
---|
8246 |
|
---|
8247 | VDDestroy(pHdd);
|
---|
8248 | return hrc;
|
---|
8249 |
|
---|
8250 | }
|
---|
8251 |
|
---|
8252 | /**
|
---|
8253 | * Implementation code for the "create base" task.
|
---|
8254 | *
|
---|
8255 | * This only gets started from Medium::CreateBaseStorage() and always runs
|
---|
8256 | * asynchronously. As a result, we always save the VirtualBox.xml file when
|
---|
8257 | * we're done here.
|
---|
8258 | *
|
---|
8259 | * @param task
|
---|
8260 | * @return
|
---|
8261 | */
|
---|
8262 | HRESULT Medium::i_taskCreateBaseHandler(Medium::CreateBaseTask &task)
|
---|
8263 | {
|
---|
8264 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
8265 | * to lock order violations, it probably causes lock order issues related
|
---|
8266 | * to the AutoCaller usage. */
|
---|
8267 | HRESULT rc = S_OK;
|
---|
8268 |
|
---|
8269 | /* these parameters we need after creation */
|
---|
8270 | uint64_t size = 0, logicalSize = 0;
|
---|
8271 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
8272 | bool fGenerateUuid = false;
|
---|
8273 |
|
---|
8274 | try
|
---|
8275 | {
|
---|
8276 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8277 |
|
---|
8278 | /* The object may request a specific UUID (through a special form of
|
---|
8279 | * the moveTo() argument). Otherwise we have to generate it */
|
---|
8280 | Guid id = m->id;
|
---|
8281 |
|
---|
8282 | fGenerateUuid = id.isZero();
|
---|
8283 | if (fGenerateUuid)
|
---|
8284 | {
|
---|
8285 | id.create();
|
---|
8286 | /* VirtualBox::i_registerMedium() will need UUID */
|
---|
8287 | unconst(m->id) = id;
|
---|
8288 | }
|
---|
8289 |
|
---|
8290 | Utf8Str format(m->strFormat);
|
---|
8291 | Utf8Str location(m->strLocationFull);
|
---|
8292 | uint64_t capabilities = m->formatObj->i_getCapabilities();
|
---|
8293 | ComAssertThrow(capabilities & ( MediumFormatCapabilities_CreateFixed
|
---|
8294 | | MediumFormatCapabilities_CreateDynamic), E_FAIL);
|
---|
8295 | Assert(m->state == MediumState_Creating);
|
---|
8296 |
|
---|
8297 | PVDISK hdd;
|
---|
8298 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8299 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8300 |
|
---|
8301 | /* unlock before the potentially lengthy operation */
|
---|
8302 | thisLock.release();
|
---|
8303 |
|
---|
8304 | try
|
---|
8305 | {
|
---|
8306 | /* ensure the directory exists */
|
---|
8307 | if (capabilities & MediumFormatCapabilities_File)
|
---|
8308 | {
|
---|
8309 | rc = VirtualBox::i_ensureFilePathExists(location, !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
8310 | if (FAILED(rc))
|
---|
8311 | throw rc;
|
---|
8312 | }
|
---|
8313 |
|
---|
8314 | VDGEOMETRY geo = { 0, 0, 0 }; /* auto-detect */
|
---|
8315 |
|
---|
8316 | vrc = VDCreateBase(hdd,
|
---|
8317 | format.c_str(),
|
---|
8318 | location.c_str(),
|
---|
8319 | task.mSize,
|
---|
8320 | task.mVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted),
|
---|
8321 | NULL,
|
---|
8322 | &geo,
|
---|
8323 | &geo,
|
---|
8324 | id.raw(),
|
---|
8325 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
8326 | m->vdImageIfaces,
|
---|
8327 | task.mVDOperationIfaces);
|
---|
8328 | if (RT_FAILURE(vrc))
|
---|
8329 | {
|
---|
8330 | if (vrc == VERR_VD_INVALID_TYPE)
|
---|
8331 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
8332 | tr("Parameters for creating the medium storage unit '%s' are invalid%s"),
|
---|
8333 | location.c_str(), i_vdError(vrc).c_str());
|
---|
8334 | else
|
---|
8335 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
8336 | tr("Could not create the medium storage unit '%s'%s"),
|
---|
8337 | location.c_str(), i_vdError(vrc).c_str());
|
---|
8338 | }
|
---|
8339 |
|
---|
8340 | if (task.mVariant & MediumVariant_Formatted)
|
---|
8341 | {
|
---|
8342 | RTVFSFILE hVfsFile;
|
---|
8343 | vrc = VDCreateVfsFileFromDisk(hdd, 0 /*fFlags*/, &hVfsFile);
|
---|
8344 | if (RT_FAILURE(vrc))
|
---|
8345 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Opening medium storage unit '%s' failed%s"),
|
---|
8346 | location.c_str(), i_vdError(vrc).c_str());
|
---|
8347 | RTERRINFOSTATIC ErrInfo;
|
---|
8348 | vrc = RTFsFatVolFormat(hVfsFile, 0 /* offVol */, 0 /* cbVol */, RTFSFATVOL_FMT_F_FULL,
|
---|
8349 | 0 /* cbSector */, 0 /* cbSectorPerCluster */, RTFSFATTYPE_INVALID,
|
---|
8350 | 0 /* cHeads */, 0 /* cSectorsPerTrack*/, 0 /* bMedia */,
|
---|
8351 | 0 /* cRootDirEntries */, 0 /* cHiddenSectors */,
|
---|
8352 | RTErrInfoInitStatic(&ErrInfo));
|
---|
8353 | RTVfsFileRelease(hVfsFile);
|
---|
8354 | if (RT_FAILURE(vrc) && RTErrInfoIsSet(&ErrInfo.Core))
|
---|
8355 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Formatting medium storage unit '%s' failed: %s"),
|
---|
8356 | location.c_str(), ErrInfo.Core.pszMsg);
|
---|
8357 | if (RT_FAILURE(vrc))
|
---|
8358 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Formatting medium storage unit '%s' failed%s"),
|
---|
8359 | location.c_str(), i_vdError(vrc).c_str());
|
---|
8360 | }
|
---|
8361 |
|
---|
8362 | size = VDGetFileSize(hdd, 0);
|
---|
8363 | logicalSize = VDGetSize(hdd, 0);
|
---|
8364 | unsigned uImageFlags;
|
---|
8365 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
8366 | if (RT_SUCCESS(vrc))
|
---|
8367 | variant = (MediumVariant_T)uImageFlags;
|
---|
8368 | }
|
---|
8369 | catch (HRESULT aRC) { rc = aRC; }
|
---|
8370 |
|
---|
8371 | VDDestroy(hdd);
|
---|
8372 | }
|
---|
8373 | catch (HRESULT aRC) { rc = aRC; }
|
---|
8374 |
|
---|
8375 | if (SUCCEEDED(rc))
|
---|
8376 | {
|
---|
8377 | /* register with mVirtualBox as the last step and move to
|
---|
8378 | * Created state only on success (leaving an orphan file is
|
---|
8379 | * better than breaking media registry consistency) */
|
---|
8380 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
8381 | ComObjPtr<Medium> pMedium;
|
---|
8382 | rc = m->pVirtualBox->i_registerMedium(this, &pMedium, treeLock);
|
---|
8383 | Assert(pMedium == NULL || this == pMedium);
|
---|
8384 | }
|
---|
8385 |
|
---|
8386 | // re-acquire the lock before changing state
|
---|
8387 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8388 |
|
---|
8389 | if (SUCCEEDED(rc))
|
---|
8390 | {
|
---|
8391 | m->state = MediumState_Created;
|
---|
8392 |
|
---|
8393 | m->size = size;
|
---|
8394 | m->logicalSize = logicalSize;
|
---|
8395 | m->variant = variant;
|
---|
8396 |
|
---|
8397 | thisLock.release();
|
---|
8398 | i_markRegistriesModified();
|
---|
8399 | if (task.isAsync())
|
---|
8400 | {
|
---|
8401 | // in asynchronous mode, save settings now
|
---|
8402 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
8403 | }
|
---|
8404 | }
|
---|
8405 | else
|
---|
8406 | {
|
---|
8407 | /* back to NotCreated on failure */
|
---|
8408 | m->state = MediumState_NotCreated;
|
---|
8409 |
|
---|
8410 | /* reset UUID to prevent it from being reused next time */
|
---|
8411 | if (fGenerateUuid)
|
---|
8412 | unconst(m->id).clear();
|
---|
8413 | }
|
---|
8414 |
|
---|
8415 | if (task.NotifyAboutChanges() && SUCCEEDED(rc))
|
---|
8416 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
8417 |
|
---|
8418 | return rc;
|
---|
8419 | }
|
---|
8420 |
|
---|
8421 | /**
|
---|
8422 | * Implementation code for the "create diff" task.
|
---|
8423 | *
|
---|
8424 | * This task always gets started from Medium::createDiffStorage() and can run
|
---|
8425 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
8426 | * that function. If we run synchronously, the caller expects the medium
|
---|
8427 | * registry modification to be set before returning; otherwise (in asynchronous
|
---|
8428 | * mode), we save the settings ourselves.
|
---|
8429 | *
|
---|
8430 | * @param task
|
---|
8431 | * @return
|
---|
8432 | */
|
---|
8433 | HRESULT Medium::i_taskCreateDiffHandler(Medium::CreateDiffTask &task)
|
---|
8434 | {
|
---|
8435 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
8436 | * to lock order violations, it probably causes lock order issues related
|
---|
8437 | * to the AutoCaller usage. */
|
---|
8438 | HRESULT rcTmp = S_OK;
|
---|
8439 |
|
---|
8440 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
8441 |
|
---|
8442 | uint64_t size = 0, logicalSize = 0;
|
---|
8443 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
8444 | bool fGenerateUuid = false;
|
---|
8445 |
|
---|
8446 | try
|
---|
8447 | {
|
---|
8448 | if (i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
8449 | {
|
---|
8450 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8451 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8452 | 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"),
|
---|
8453 | m->strLocationFull.c_str());
|
---|
8454 | }
|
---|
8455 |
|
---|
8456 | /* Lock both in {parent,child} order. */
|
---|
8457 | AutoMultiWriteLock2 mediaLock(this, pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8458 |
|
---|
8459 | /* The object may request a specific UUID (through a special form of
|
---|
8460 | * the moveTo() argument). Otherwise we have to generate it */
|
---|
8461 | Guid targetId = pTarget->m->id;
|
---|
8462 |
|
---|
8463 | fGenerateUuid = targetId.isZero();
|
---|
8464 | if (fGenerateUuid)
|
---|
8465 | {
|
---|
8466 | targetId.create();
|
---|
8467 | /* VirtualBox::i_registerMedium() will need UUID */
|
---|
8468 | unconst(pTarget->m->id) = targetId;
|
---|
8469 | }
|
---|
8470 |
|
---|
8471 | Guid id = m->id;
|
---|
8472 |
|
---|
8473 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
8474 | Utf8Str targetLocation(pTarget->m->strLocationFull);
|
---|
8475 | uint64_t capabilities = pTarget->m->formatObj->i_getCapabilities();
|
---|
8476 | ComAssertThrow(capabilities & MediumFormatCapabilities_CreateDynamic, E_FAIL);
|
---|
8477 |
|
---|
8478 | Assert(pTarget->m->state == MediumState_Creating);
|
---|
8479 | Assert(m->state == MediumState_LockedRead);
|
---|
8480 |
|
---|
8481 | PVDISK hdd;
|
---|
8482 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8483 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8484 |
|
---|
8485 | /* the two media are now protected by their non-default states;
|
---|
8486 | * unlock the media before the potentially lengthy operation */
|
---|
8487 | mediaLock.release();
|
---|
8488 |
|
---|
8489 | try
|
---|
8490 | {
|
---|
8491 | /* Open all media in the target chain but the last. */
|
---|
8492 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
8493 | task.mpMediumLockList->GetBegin();
|
---|
8494 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
8495 | task.mpMediumLockList->GetEnd();
|
---|
8496 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
8497 | it != targetListEnd;
|
---|
8498 | ++it)
|
---|
8499 | {
|
---|
8500 | const MediumLock &mediumLock = *it;
|
---|
8501 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8502 |
|
---|
8503 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8504 |
|
---|
8505 | /* Skip over the target diff medium */
|
---|
8506 | if (pMedium->m->state == MediumState_Creating)
|
---|
8507 | continue;
|
---|
8508 |
|
---|
8509 | /* sanity check */
|
---|
8510 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
8511 |
|
---|
8512 | /* Open all media in appropriate mode. */
|
---|
8513 | vrc = VDOpen(hdd,
|
---|
8514 | pMedium->m->strFormat.c_str(),
|
---|
8515 | pMedium->m->strLocationFull.c_str(),
|
---|
8516 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
8517 | pMedium->m->vdImageIfaces);
|
---|
8518 | if (RT_FAILURE(vrc))
|
---|
8519 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
8520 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8521 | pMedium->m->strLocationFull.c_str(),
|
---|
8522 | i_vdError(vrc).c_str());
|
---|
8523 | }
|
---|
8524 |
|
---|
8525 | /* ensure the target directory exists */
|
---|
8526 | if (capabilities & MediumFormatCapabilities_File)
|
---|
8527 | {
|
---|
8528 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
8529 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
8530 | if (FAILED(rc))
|
---|
8531 | throw rc;
|
---|
8532 | }
|
---|
8533 |
|
---|
8534 | vrc = VDCreateDiff(hdd,
|
---|
8535 | targetFormat.c_str(),
|
---|
8536 | targetLocation.c_str(),
|
---|
8537 | (task.mVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted | MediumVariant_VmdkESX))
|
---|
8538 | | VD_IMAGE_FLAGS_DIFF,
|
---|
8539 | NULL,
|
---|
8540 | targetId.raw(),
|
---|
8541 | id.raw(),
|
---|
8542 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
8543 | pTarget->m->vdImageIfaces,
|
---|
8544 | task.mVDOperationIfaces);
|
---|
8545 | if (RT_FAILURE(vrc))
|
---|
8546 | {
|
---|
8547 | if (vrc == VERR_VD_INVALID_TYPE)
|
---|
8548 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
8549 | tr("Parameters for creating the differencing medium storage unit '%s' are invalid%s"),
|
---|
8550 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
8551 | else
|
---|
8552 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
8553 | tr("Could not create the differencing medium storage unit '%s'%s"),
|
---|
8554 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
8555 | }
|
---|
8556 |
|
---|
8557 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
8558 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
8559 | unsigned uImageFlags;
|
---|
8560 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
8561 | if (RT_SUCCESS(vrc))
|
---|
8562 | variant = (MediumVariant_T)uImageFlags;
|
---|
8563 | }
|
---|
8564 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8565 |
|
---|
8566 | VDDestroy(hdd);
|
---|
8567 | }
|
---|
8568 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8569 |
|
---|
8570 | MultiResult mrc(rcTmp);
|
---|
8571 |
|
---|
8572 | if (SUCCEEDED(mrc))
|
---|
8573 | {
|
---|
8574 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
8575 |
|
---|
8576 | Assert(pTarget->m->pParent.isNull());
|
---|
8577 |
|
---|
8578 | /* associate child with the parent, maximum depth was checked above */
|
---|
8579 | pTarget->i_setParent(this);
|
---|
8580 |
|
---|
8581 | /* diffs for immutable media are auto-reset by default */
|
---|
8582 | bool fAutoReset;
|
---|
8583 | {
|
---|
8584 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
8585 | AutoReadLock block(pBase COMMA_LOCKVAL_SRC_POS);
|
---|
8586 | fAutoReset = (pBase->m->type == MediumType_Immutable);
|
---|
8587 | }
|
---|
8588 | {
|
---|
8589 | AutoWriteLock tlock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8590 | pTarget->m->autoReset = fAutoReset;
|
---|
8591 | }
|
---|
8592 |
|
---|
8593 | /* register with mVirtualBox as the last step and move to
|
---|
8594 | * Created state only on success (leaving an orphan file is
|
---|
8595 | * better than breaking media registry consistency) */
|
---|
8596 | ComObjPtr<Medium> pMedium;
|
---|
8597 | mrc = m->pVirtualBox->i_registerMedium(pTarget, &pMedium, treeLock);
|
---|
8598 | Assert(pTarget == pMedium);
|
---|
8599 |
|
---|
8600 | if (FAILED(mrc))
|
---|
8601 | /* break the parent association on failure to register */
|
---|
8602 | i_deparent();
|
---|
8603 | }
|
---|
8604 |
|
---|
8605 | AutoMultiWriteLock2 mediaLock(this, pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8606 |
|
---|
8607 | if (SUCCEEDED(mrc))
|
---|
8608 | {
|
---|
8609 | pTarget->m->state = MediumState_Created;
|
---|
8610 |
|
---|
8611 | pTarget->m->size = size;
|
---|
8612 | pTarget->m->logicalSize = logicalSize;
|
---|
8613 | pTarget->m->variant = variant;
|
---|
8614 | }
|
---|
8615 | else
|
---|
8616 | {
|
---|
8617 | /* back to NotCreated on failure */
|
---|
8618 | pTarget->m->state = MediumState_NotCreated;
|
---|
8619 |
|
---|
8620 | pTarget->m->autoReset = false;
|
---|
8621 |
|
---|
8622 | /* reset UUID to prevent it from being reused next time */
|
---|
8623 | if (fGenerateUuid)
|
---|
8624 | unconst(pTarget->m->id).clear();
|
---|
8625 | }
|
---|
8626 |
|
---|
8627 | // deregister the task registered in createDiffStorage()
|
---|
8628 | Assert(m->numCreateDiffTasks != 0);
|
---|
8629 | --m->numCreateDiffTasks;
|
---|
8630 |
|
---|
8631 | mediaLock.release();
|
---|
8632 | i_markRegistriesModified();
|
---|
8633 | if (task.isAsync())
|
---|
8634 | {
|
---|
8635 | // in asynchronous mode, save settings now
|
---|
8636 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
8637 | }
|
---|
8638 |
|
---|
8639 | /* Note that in sync mode, it's the caller's responsibility to
|
---|
8640 | * unlock the medium. */
|
---|
8641 |
|
---|
8642 | if (task.NotifyAboutChanges() && SUCCEEDED(mrc))
|
---|
8643 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
8644 |
|
---|
8645 | return mrc;
|
---|
8646 | }
|
---|
8647 |
|
---|
8648 | /**
|
---|
8649 | * Implementation code for the "merge" task.
|
---|
8650 | *
|
---|
8651 | * This task always gets started from Medium::mergeTo() and can run
|
---|
8652 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
8653 | * that function. If we run synchronously, the caller expects the medium
|
---|
8654 | * registry modification to be set before returning; otherwise (in asynchronous
|
---|
8655 | * mode), we save the settings ourselves.
|
---|
8656 | *
|
---|
8657 | * @param task
|
---|
8658 | * @return
|
---|
8659 | */
|
---|
8660 | HRESULT Medium::i_taskMergeHandler(Medium::MergeTask &task)
|
---|
8661 | {
|
---|
8662 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
8663 | * to lock order violations, it probably causes lock order issues related
|
---|
8664 | * to the AutoCaller usage. */
|
---|
8665 | HRESULT rcTmp = S_OK;
|
---|
8666 |
|
---|
8667 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
8668 |
|
---|
8669 | try
|
---|
8670 | {
|
---|
8671 | if (!task.mParentForTarget.isNull())
|
---|
8672 | if (task.mParentForTarget->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
8673 | {
|
---|
8674 | AutoReadLock plock(task.mParentForTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8675 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8676 | tr("Cannot merge image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
8677 | task.mParentForTarget->m->strLocationFull.c_str());
|
---|
8678 | }
|
---|
8679 |
|
---|
8680 | // Resize target to source size, if possible. Otherwise throw an error.
|
---|
8681 | // It's offline resizing. Online resizing will be called in the
|
---|
8682 | // SessionMachine::onlineMergeMedium.
|
---|
8683 |
|
---|
8684 | uint64_t sourceSize = 0;
|
---|
8685 | Utf8Str sourceName;
|
---|
8686 | {
|
---|
8687 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8688 | sourceSize = i_getLogicalSize();
|
---|
8689 | sourceName = i_getName();
|
---|
8690 | }
|
---|
8691 | uint64_t targetSize = 0;
|
---|
8692 | Utf8Str targetName;
|
---|
8693 | {
|
---|
8694 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8695 | targetSize = pTarget->i_getLogicalSize();
|
---|
8696 | targetName = pTarget->i_getName();
|
---|
8697 | }
|
---|
8698 |
|
---|
8699 | //reducing vm disks are not implemented yet
|
---|
8700 | if (sourceSize > targetSize)
|
---|
8701 | {
|
---|
8702 | if (i_isMediumFormatFile())
|
---|
8703 | {
|
---|
8704 | // Have to make own lock list, because "resize" method resizes only last image
|
---|
8705 | // in the lock chain. The lock chain already in the task.mpMediumLockList, so
|
---|
8706 | // just make new lock list based on it. In fact the own lock list neither makes
|
---|
8707 | // double locking of mediums nor unlocks them during delete, because medium
|
---|
8708 | // already locked by task.mpMediumLockList and own list is used just to specify
|
---|
8709 | // what "resize" method should resize.
|
---|
8710 |
|
---|
8711 | MediumLockList* pMediumLockListForResize = new MediumLockList();
|
---|
8712 |
|
---|
8713 | for (MediumLockList::Base::iterator it = task.mpMediumLockList->GetBegin();
|
---|
8714 | it != task.mpMediumLockList->GetEnd();
|
---|
8715 | ++it)
|
---|
8716 | {
|
---|
8717 | ComObjPtr<Medium> pMedium = it->GetMedium();
|
---|
8718 | pMediumLockListForResize->Append(pMedium, pMedium->m->state == MediumState_LockedWrite);
|
---|
8719 | if (pMedium == pTarget)
|
---|
8720 | break;
|
---|
8721 | }
|
---|
8722 |
|
---|
8723 | // just to switch internal state of the lock list to avoid errors during list deletion,
|
---|
8724 | // because all meduims in the list already locked by task.mpMediumLockList
|
---|
8725 | HRESULT rc = pMediumLockListForResize->Lock(true /* fSkipOverLockedMedia */);
|
---|
8726 | if (FAILED(rc))
|
---|
8727 | {
|
---|
8728 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8729 | rc = setError(rc,
|
---|
8730 | tr("Failed to lock the medium '%s' to resize before merge"),
|
---|
8731 | targetName.c_str());
|
---|
8732 | delete pMediumLockListForResize;
|
---|
8733 | throw rc;
|
---|
8734 | }
|
---|
8735 |
|
---|
8736 | ComObjPtr<Progress> pProgress(task.GetProgressObject());
|
---|
8737 | rc = pTarget->i_resize(sourceSize, pMediumLockListForResize, &pProgress, true, false);
|
---|
8738 | if (FAILED(rc))
|
---|
8739 | {
|
---|
8740 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8741 | rc = setError(rc,
|
---|
8742 | tr("Failed to set size of '%s' to size of '%s'"),
|
---|
8743 | targetName.c_str(), sourceName.c_str());
|
---|
8744 | delete pMediumLockListForResize;
|
---|
8745 | throw rc;
|
---|
8746 | }
|
---|
8747 | delete pMediumLockListForResize;
|
---|
8748 | }
|
---|
8749 | else
|
---|
8750 | {
|
---|
8751 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8752 | HRESULT rc = setError(VBOX_E_NOT_SUPPORTED,
|
---|
8753 | tr("Sizes of '%s' and '%s' are different and medium format does not support resing"),
|
---|
8754 | sourceName.c_str(), targetName.c_str());
|
---|
8755 | throw rc;
|
---|
8756 | }
|
---|
8757 | }
|
---|
8758 |
|
---|
8759 | task.GetProgressObject()->SetNextOperation(BstrFmt(tr("Merging medium '%s' to '%s'"),
|
---|
8760 | i_getName().c_str(),
|
---|
8761 | targetName.c_str()).raw(),
|
---|
8762 | 1);
|
---|
8763 |
|
---|
8764 | PVDISK hdd;
|
---|
8765 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8766 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8767 |
|
---|
8768 | try
|
---|
8769 | {
|
---|
8770 | // Similar code appears in SessionMachine::onlineMergeMedium, so
|
---|
8771 | // if you make any changes below check whether they are applicable
|
---|
8772 | // in that context as well.
|
---|
8773 |
|
---|
8774 | unsigned uTargetIdx = VD_LAST_IMAGE;
|
---|
8775 | unsigned uSourceIdx = VD_LAST_IMAGE;
|
---|
8776 | /* Open all media in the chain. */
|
---|
8777 | MediumLockList::Base::iterator lockListBegin =
|
---|
8778 | task.mpMediumLockList->GetBegin();
|
---|
8779 | MediumLockList::Base::iterator lockListEnd =
|
---|
8780 | task.mpMediumLockList->GetEnd();
|
---|
8781 | unsigned i = 0;
|
---|
8782 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
8783 | it != lockListEnd;
|
---|
8784 | ++it)
|
---|
8785 | {
|
---|
8786 | MediumLock &mediumLock = *it;
|
---|
8787 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8788 |
|
---|
8789 | if (pMedium == this)
|
---|
8790 | uSourceIdx = i;
|
---|
8791 | else if (pMedium == pTarget)
|
---|
8792 | uTargetIdx = i;
|
---|
8793 |
|
---|
8794 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8795 |
|
---|
8796 | /*
|
---|
8797 | * complex sanity (sane complexity)
|
---|
8798 | *
|
---|
8799 | * The current medium must be in the Deleting (medium is merged)
|
---|
8800 | * or LockedRead (parent medium) state if it is not the target.
|
---|
8801 | * If it is the target it must be in the LockedWrite state.
|
---|
8802 | */
|
---|
8803 | Assert( ( pMedium != pTarget
|
---|
8804 | && ( pMedium->m->state == MediumState_Deleting
|
---|
8805 | || pMedium->m->state == MediumState_LockedRead))
|
---|
8806 | || ( pMedium == pTarget
|
---|
8807 | && pMedium->m->state == MediumState_LockedWrite));
|
---|
8808 | /*
|
---|
8809 | * Medium must be the target, in the LockedRead state
|
---|
8810 | * or Deleting state where it is not allowed to be attached
|
---|
8811 | * to a virtual machine.
|
---|
8812 | */
|
---|
8813 | Assert( pMedium == pTarget
|
---|
8814 | || pMedium->m->state == MediumState_LockedRead
|
---|
8815 | || ( pMedium->m->backRefs.size() == 0
|
---|
8816 | && pMedium->m->state == MediumState_Deleting));
|
---|
8817 | /* The source medium must be in Deleting state. */
|
---|
8818 | Assert( pMedium != this
|
---|
8819 | || pMedium->m->state == MediumState_Deleting);
|
---|
8820 |
|
---|
8821 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
8822 |
|
---|
8823 | if ( pMedium->m->state == MediumState_LockedRead
|
---|
8824 | || pMedium->m->state == MediumState_Deleting)
|
---|
8825 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
8826 | if (pMedium->m->type == MediumType_Shareable)
|
---|
8827 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
8828 |
|
---|
8829 | /* Open the medium */
|
---|
8830 | vrc = VDOpen(hdd,
|
---|
8831 | pMedium->m->strFormat.c_str(),
|
---|
8832 | pMedium->m->strLocationFull.c_str(),
|
---|
8833 | uOpenFlags | m->uOpenFlagsDef,
|
---|
8834 | pMedium->m->vdImageIfaces);
|
---|
8835 | if (RT_FAILURE(vrc))
|
---|
8836 | throw vrc;
|
---|
8837 |
|
---|
8838 | i++;
|
---|
8839 | }
|
---|
8840 |
|
---|
8841 | ComAssertThrow( uSourceIdx != VD_LAST_IMAGE
|
---|
8842 | && uTargetIdx != VD_LAST_IMAGE, E_FAIL);
|
---|
8843 |
|
---|
8844 | vrc = VDMerge(hdd, uSourceIdx, uTargetIdx,
|
---|
8845 | task.mVDOperationIfaces);
|
---|
8846 | if (RT_FAILURE(vrc))
|
---|
8847 | throw vrc;
|
---|
8848 |
|
---|
8849 | /* update parent UUIDs */
|
---|
8850 | if (!task.mfMergeForward)
|
---|
8851 | {
|
---|
8852 | /* we need to update UUIDs of all source's children
|
---|
8853 | * which cannot be part of the container at once so
|
---|
8854 | * add each one in there individually */
|
---|
8855 | if (task.mpChildrenToReparent)
|
---|
8856 | {
|
---|
8857 | MediumLockList::Base::iterator childrenBegin = task.mpChildrenToReparent->GetBegin();
|
---|
8858 | MediumLockList::Base::iterator childrenEnd = task.mpChildrenToReparent->GetEnd();
|
---|
8859 | for (MediumLockList::Base::iterator it = childrenBegin;
|
---|
8860 | it != childrenEnd;
|
---|
8861 | ++it)
|
---|
8862 | {
|
---|
8863 | Medium *pMedium = it->GetMedium();
|
---|
8864 | /* VD_OPEN_FLAGS_INFO since UUID is wrong yet */
|
---|
8865 | vrc = VDOpen(hdd,
|
---|
8866 | pMedium->m->strFormat.c_str(),
|
---|
8867 | pMedium->m->strLocationFull.c_str(),
|
---|
8868 | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
8869 | pMedium->m->vdImageIfaces);
|
---|
8870 | if (RT_FAILURE(vrc))
|
---|
8871 | throw vrc;
|
---|
8872 |
|
---|
8873 | vrc = VDSetParentUuid(hdd, VD_LAST_IMAGE,
|
---|
8874 | pTarget->m->id.raw());
|
---|
8875 | if (RT_FAILURE(vrc))
|
---|
8876 | throw vrc;
|
---|
8877 |
|
---|
8878 | vrc = VDClose(hdd, false /* fDelete */);
|
---|
8879 | if (RT_FAILURE(vrc))
|
---|
8880 | throw vrc;
|
---|
8881 | }
|
---|
8882 | }
|
---|
8883 | }
|
---|
8884 | }
|
---|
8885 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8886 | catch (int aVRC)
|
---|
8887 | {
|
---|
8888 | rcTmp = setErrorBoth(VBOX_E_FILE_ERROR, aVRC,
|
---|
8889 | tr("Could not merge the medium '%s' to '%s'%s"),
|
---|
8890 | m->strLocationFull.c_str(),
|
---|
8891 | pTarget->m->strLocationFull.c_str(),
|
---|
8892 | i_vdError(aVRC).c_str());
|
---|
8893 | }
|
---|
8894 |
|
---|
8895 | VDDestroy(hdd);
|
---|
8896 | }
|
---|
8897 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8898 |
|
---|
8899 | ErrorInfoKeeper eik;
|
---|
8900 | MultiResult mrc(rcTmp);
|
---|
8901 | HRESULT rc2;
|
---|
8902 |
|
---|
8903 | std::set<ComObjPtr<Medium> > pMediumsForNotify;
|
---|
8904 | std::map<Guid, DeviceType_T> uIdsForNotify;
|
---|
8905 |
|
---|
8906 | if (SUCCEEDED(mrc))
|
---|
8907 | {
|
---|
8908 | /* all media but the target were successfully deleted by
|
---|
8909 | * VDMerge; reparent the last one and uninitialize deleted media. */
|
---|
8910 |
|
---|
8911 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
8912 |
|
---|
8913 | if (task.mfMergeForward)
|
---|
8914 | {
|
---|
8915 | /* first, unregister the target since it may become a base
|
---|
8916 | * medium which needs re-registration */
|
---|
8917 | rc2 = m->pVirtualBox->i_unregisterMedium(pTarget);
|
---|
8918 | AssertComRC(rc2);
|
---|
8919 |
|
---|
8920 | /* then, reparent it and disconnect the deleted branch at both ends
|
---|
8921 | * (chain->parent() is source's parent). Depth check above. */
|
---|
8922 | pTarget->i_deparent();
|
---|
8923 | pTarget->i_setParent(task.mParentForTarget);
|
---|
8924 | if (task.mParentForTarget)
|
---|
8925 | {
|
---|
8926 | i_deparent();
|
---|
8927 | if (task.NotifyAboutChanges())
|
---|
8928 | pMediumsForNotify.insert(task.mParentForTarget);
|
---|
8929 | }
|
---|
8930 |
|
---|
8931 | /* then, register again */
|
---|
8932 | ComObjPtr<Medium> pMedium;
|
---|
8933 | rc2 = m->pVirtualBox->i_registerMedium(pTarget, &pMedium,
|
---|
8934 | treeLock);
|
---|
8935 | AssertComRC(rc2);
|
---|
8936 | }
|
---|
8937 | else
|
---|
8938 | {
|
---|
8939 | Assert(pTarget->i_getChildren().size() == 1);
|
---|
8940 | Medium *targetChild = pTarget->i_getChildren().front();
|
---|
8941 |
|
---|
8942 | /* disconnect the deleted branch at the elder end */
|
---|
8943 | targetChild->i_deparent();
|
---|
8944 |
|
---|
8945 | /* reparent source's children and disconnect the deleted
|
---|
8946 | * branch at the younger end */
|
---|
8947 | if (task.mpChildrenToReparent)
|
---|
8948 | {
|
---|
8949 | /* obey {parent,child} lock order */
|
---|
8950 | AutoWriteLock sourceLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8951 |
|
---|
8952 | MediumLockList::Base::iterator childrenBegin = task.mpChildrenToReparent->GetBegin();
|
---|
8953 | MediumLockList::Base::iterator childrenEnd = task.mpChildrenToReparent->GetEnd();
|
---|
8954 | for (MediumLockList::Base::iterator it = childrenBegin;
|
---|
8955 | it != childrenEnd;
|
---|
8956 | ++it)
|
---|
8957 | {
|
---|
8958 | Medium *pMedium = it->GetMedium();
|
---|
8959 | AutoWriteLock childLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8960 |
|
---|
8961 | pMedium->i_deparent(); // removes pMedium from source
|
---|
8962 | // no depth check, reduces depth
|
---|
8963 | pMedium->i_setParent(pTarget);
|
---|
8964 |
|
---|
8965 | if (task.NotifyAboutChanges())
|
---|
8966 | pMediumsForNotify.insert(pMedium);
|
---|
8967 | }
|
---|
8968 | }
|
---|
8969 | pMediumsForNotify.insert(pTarget);
|
---|
8970 | }
|
---|
8971 |
|
---|
8972 | /* unregister and uninitialize all media removed by the merge */
|
---|
8973 | MediumLockList::Base::iterator lockListBegin =
|
---|
8974 | task.mpMediumLockList->GetBegin();
|
---|
8975 | MediumLockList::Base::iterator lockListEnd =
|
---|
8976 | task.mpMediumLockList->GetEnd();
|
---|
8977 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
8978 | it != lockListEnd;
|
---|
8979 | )
|
---|
8980 | {
|
---|
8981 | MediumLock &mediumLock = *it;
|
---|
8982 | /* Create a real copy of the medium pointer, as the medium
|
---|
8983 | * lock deletion below would invalidate the referenced object. */
|
---|
8984 | const ComObjPtr<Medium> pMedium = mediumLock.GetMedium();
|
---|
8985 |
|
---|
8986 | /* The target and all media not merged (readonly) are skipped */
|
---|
8987 | if ( pMedium == pTarget
|
---|
8988 | || pMedium->m->state == MediumState_LockedRead)
|
---|
8989 | {
|
---|
8990 | ++it;
|
---|
8991 | continue;
|
---|
8992 | }
|
---|
8993 |
|
---|
8994 | uIdsForNotify[pMedium->i_getId()] = pMedium->i_getDeviceType();
|
---|
8995 | rc2 = pMedium->m->pVirtualBox->i_unregisterMedium(pMedium);
|
---|
8996 | AssertComRC(rc2);
|
---|
8997 |
|
---|
8998 | /* now, uninitialize the deleted medium (note that
|
---|
8999 | * due to the Deleting state, uninit() will not touch
|
---|
9000 | * the parent-child relationship so we need to
|
---|
9001 | * uninitialize each disk individually) */
|
---|
9002 |
|
---|
9003 | /* note that the operation initiator medium (which is
|
---|
9004 | * normally also the source medium) is a special case
|
---|
9005 | * -- there is one more caller added by Task to it which
|
---|
9006 | * we must release. Also, if we are in sync mode, the
|
---|
9007 | * caller may still hold an AutoCaller instance for it
|
---|
9008 | * and therefore we cannot uninit() it (it's therefore
|
---|
9009 | * the caller's responsibility) */
|
---|
9010 | if (pMedium == this)
|
---|
9011 | {
|
---|
9012 | Assert(i_getChildren().size() == 0);
|
---|
9013 | Assert(m->backRefs.size() == 0);
|
---|
9014 | task.mMediumCaller.release();
|
---|
9015 | }
|
---|
9016 |
|
---|
9017 | /* Delete the medium lock list entry, which also releases the
|
---|
9018 | * caller added by MergeChain before uninit() and updates the
|
---|
9019 | * iterator to point to the right place. */
|
---|
9020 | rc2 = task.mpMediumLockList->RemoveByIterator(it);
|
---|
9021 | AssertComRC(rc2);
|
---|
9022 |
|
---|
9023 | if (task.isAsync() || pMedium != this)
|
---|
9024 | {
|
---|
9025 | treeLock.release();
|
---|
9026 | pMedium->uninit();
|
---|
9027 | treeLock.acquire();
|
---|
9028 | }
|
---|
9029 | }
|
---|
9030 | }
|
---|
9031 |
|
---|
9032 | i_markRegistriesModified();
|
---|
9033 | if (task.isAsync())
|
---|
9034 | {
|
---|
9035 | // in asynchronous mode, save settings now
|
---|
9036 | eik.restore();
|
---|
9037 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
9038 | eik.fetch();
|
---|
9039 | }
|
---|
9040 |
|
---|
9041 | if (FAILED(mrc))
|
---|
9042 | {
|
---|
9043 | /* Here we come if either VDMerge() failed (in which case we
|
---|
9044 | * assume that it tried to do everything to make a further
|
---|
9045 | * retry possible -- e.g. not deleted intermediate media
|
---|
9046 | * and so on) or VirtualBox::saveRegistries() failed (where we
|
---|
9047 | * should have the original tree but with intermediate storage
|
---|
9048 | * units deleted by VDMerge()). We have to only restore states
|
---|
9049 | * (through the MergeChain dtor) unless we are run synchronously
|
---|
9050 | * in which case it's the responsibility of the caller as stated
|
---|
9051 | * in the mergeTo() docs. The latter also implies that we
|
---|
9052 | * don't own the merge chain, so release it in this case. */
|
---|
9053 | if (task.isAsync())
|
---|
9054 | i_cancelMergeTo(task.mpChildrenToReparent, task.mpMediumLockList);
|
---|
9055 | }
|
---|
9056 | else if (task.NotifyAboutChanges())
|
---|
9057 | {
|
---|
9058 | for (std::set<ComObjPtr<Medium> >::const_iterator it = pMediumsForNotify.begin();
|
---|
9059 | it != pMediumsForNotify.end();
|
---|
9060 | ++it)
|
---|
9061 | {
|
---|
9062 | if (it->isNotNull())
|
---|
9063 | m->pVirtualBox->i_onMediumConfigChanged(*it);
|
---|
9064 | }
|
---|
9065 | for (std::map<Guid, DeviceType_T>::const_iterator it = uIdsForNotify.begin();
|
---|
9066 | it != uIdsForNotify.end();
|
---|
9067 | ++it)
|
---|
9068 | {
|
---|
9069 | m->pVirtualBox->i_onMediumRegistered(it->first, it->second, FALSE);
|
---|
9070 | }
|
---|
9071 | }
|
---|
9072 |
|
---|
9073 | return mrc;
|
---|
9074 | }
|
---|
9075 |
|
---|
9076 | /**
|
---|
9077 | * Implementation code for the "clone" task.
|
---|
9078 | *
|
---|
9079 | * This only gets started from Medium::CloneTo() and always runs asynchronously.
|
---|
9080 | * As a result, we always save the VirtualBox.xml file when we're done here.
|
---|
9081 | *
|
---|
9082 | * @param task
|
---|
9083 | * @return
|
---|
9084 | */
|
---|
9085 | HRESULT Medium::i_taskCloneHandler(Medium::CloneTask &task)
|
---|
9086 | {
|
---|
9087 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
9088 | * to lock order violations, it probably causes lock order issues related
|
---|
9089 | * to the AutoCaller usage. */
|
---|
9090 | HRESULT rcTmp = S_OK;
|
---|
9091 |
|
---|
9092 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
9093 | const ComObjPtr<Medium> &pParent = task.mParent;
|
---|
9094 |
|
---|
9095 | bool fCreatingTarget = false;
|
---|
9096 |
|
---|
9097 | uint64_t size = 0, logicalSize = 0;
|
---|
9098 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
9099 | bool fGenerateUuid = false;
|
---|
9100 |
|
---|
9101 | try
|
---|
9102 | {
|
---|
9103 | if (!pParent.isNull())
|
---|
9104 | {
|
---|
9105 |
|
---|
9106 | if (pParent->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
9107 | {
|
---|
9108 | AutoReadLock plock(pParent COMMA_LOCKVAL_SRC_POS);
|
---|
9109 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
9110 | tr("Cannot clone image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
9111 | pParent->m->strLocationFull.c_str());
|
---|
9112 | }
|
---|
9113 | }
|
---|
9114 |
|
---|
9115 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
9116 | * signal from the task initiator (which releases it only after
|
---|
9117 | * RTThreadCreate()) that we can start the job. */
|
---|
9118 | AutoMultiWriteLock3 thisLock(this, pTarget, pParent COMMA_LOCKVAL_SRC_POS);
|
---|
9119 |
|
---|
9120 | fCreatingTarget = pTarget->m->state == MediumState_Creating;
|
---|
9121 |
|
---|
9122 | /* The object may request a specific UUID (through a special form of
|
---|
9123 | * the moveTo() argument). Otherwise we have to generate it */
|
---|
9124 | Guid targetId = pTarget->m->id;
|
---|
9125 |
|
---|
9126 | fGenerateUuid = targetId.isZero();
|
---|
9127 | if (fGenerateUuid)
|
---|
9128 | {
|
---|
9129 | targetId.create();
|
---|
9130 | /* VirtualBox::registerMedium() will need UUID */
|
---|
9131 | unconst(pTarget->m->id) = targetId;
|
---|
9132 | }
|
---|
9133 |
|
---|
9134 | PVDISK hdd;
|
---|
9135 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9136 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9137 |
|
---|
9138 | try
|
---|
9139 | {
|
---|
9140 | /* Open all media in the source chain. */
|
---|
9141 | MediumLockList::Base::const_iterator sourceListBegin =
|
---|
9142 | task.mpSourceMediumLockList->GetBegin();
|
---|
9143 | MediumLockList::Base::const_iterator sourceListEnd =
|
---|
9144 | task.mpSourceMediumLockList->GetEnd();
|
---|
9145 | for (MediumLockList::Base::const_iterator it = sourceListBegin;
|
---|
9146 | it != sourceListEnd;
|
---|
9147 | ++it)
|
---|
9148 | {
|
---|
9149 | const MediumLock &mediumLock = *it;
|
---|
9150 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9151 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9152 |
|
---|
9153 | /* sanity check */
|
---|
9154 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
9155 |
|
---|
9156 | /** Open all media in read-only mode. */
|
---|
9157 | vrc = VDOpen(hdd,
|
---|
9158 | pMedium->m->strFormat.c_str(),
|
---|
9159 | pMedium->m->strLocationFull.c_str(),
|
---|
9160 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
9161 | pMedium->m->vdImageIfaces);
|
---|
9162 | if (RT_FAILURE(vrc))
|
---|
9163 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9164 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9165 | pMedium->m->strLocationFull.c_str(),
|
---|
9166 | i_vdError(vrc).c_str());
|
---|
9167 | }
|
---|
9168 |
|
---|
9169 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
9170 | Utf8Str targetLocation(pTarget->m->strLocationFull);
|
---|
9171 | uint64_t capabilities = pTarget->m->formatObj->i_getCapabilities();
|
---|
9172 |
|
---|
9173 | Assert( pTarget->m->state == MediumState_Creating
|
---|
9174 | || pTarget->m->state == MediumState_LockedWrite);
|
---|
9175 | Assert(m->state == MediumState_LockedRead);
|
---|
9176 | Assert( pParent.isNull()
|
---|
9177 | || pParent->m->state == MediumState_LockedRead);
|
---|
9178 |
|
---|
9179 | /* unlock before the potentially lengthy operation */
|
---|
9180 | thisLock.release();
|
---|
9181 |
|
---|
9182 | /* ensure the target directory exists */
|
---|
9183 | if (capabilities & MediumFormatCapabilities_File)
|
---|
9184 | {
|
---|
9185 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
9186 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
9187 | if (FAILED(rc))
|
---|
9188 | throw rc;
|
---|
9189 | }
|
---|
9190 |
|
---|
9191 | PVDISK targetHdd;
|
---|
9192 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &targetHdd);
|
---|
9193 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9194 |
|
---|
9195 | try
|
---|
9196 | {
|
---|
9197 | /* Open all media in the target chain. */
|
---|
9198 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
9199 | task.mpTargetMediumLockList->GetBegin();
|
---|
9200 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
9201 | task.mpTargetMediumLockList->GetEnd();
|
---|
9202 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
9203 | it != targetListEnd;
|
---|
9204 | ++it)
|
---|
9205 | {
|
---|
9206 | const MediumLock &mediumLock = *it;
|
---|
9207 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9208 |
|
---|
9209 | /* If the target medium is not created yet there's no
|
---|
9210 | * reason to open it. */
|
---|
9211 | if (pMedium == pTarget && fCreatingTarget)
|
---|
9212 | continue;
|
---|
9213 |
|
---|
9214 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9215 |
|
---|
9216 | /* sanity check */
|
---|
9217 | Assert( pMedium->m->state == MediumState_LockedRead
|
---|
9218 | || pMedium->m->state == MediumState_LockedWrite);
|
---|
9219 |
|
---|
9220 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
9221 | if (pMedium->m->state != MediumState_LockedWrite)
|
---|
9222 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
9223 | if (pMedium->m->type == MediumType_Shareable)
|
---|
9224 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
9225 |
|
---|
9226 | /* Open all media in appropriate mode. */
|
---|
9227 | vrc = VDOpen(targetHdd,
|
---|
9228 | pMedium->m->strFormat.c_str(),
|
---|
9229 | pMedium->m->strLocationFull.c_str(),
|
---|
9230 | uOpenFlags | m->uOpenFlagsDef,
|
---|
9231 | pMedium->m->vdImageIfaces);
|
---|
9232 | if (RT_FAILURE(vrc))
|
---|
9233 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9234 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9235 | pMedium->m->strLocationFull.c_str(),
|
---|
9236 | i_vdError(vrc).c_str());
|
---|
9237 | }
|
---|
9238 |
|
---|
9239 | /* target isn't locked, but no changing data is accessed */
|
---|
9240 | if (task.midxSrcImageSame == UINT32_MAX)
|
---|
9241 | {
|
---|
9242 | vrc = VDCopy(hdd,
|
---|
9243 | VD_LAST_IMAGE,
|
---|
9244 | targetHdd,
|
---|
9245 | targetFormat.c_str(),
|
---|
9246 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
9247 | false /* fMoveByRename */,
|
---|
9248 | 0 /* cbSize */,
|
---|
9249 | task.mVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted),
|
---|
9250 | targetId.raw(),
|
---|
9251 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
9252 | NULL /* pVDIfsOperation */,
|
---|
9253 | pTarget->m->vdImageIfaces,
|
---|
9254 | task.mVDOperationIfaces);
|
---|
9255 | }
|
---|
9256 | else
|
---|
9257 | {
|
---|
9258 | vrc = VDCopyEx(hdd,
|
---|
9259 | VD_LAST_IMAGE,
|
---|
9260 | targetHdd,
|
---|
9261 | targetFormat.c_str(),
|
---|
9262 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
9263 | false /* fMoveByRename */,
|
---|
9264 | 0 /* cbSize */,
|
---|
9265 | task.midxSrcImageSame,
|
---|
9266 | task.midxDstImageSame,
|
---|
9267 | task.mVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted),
|
---|
9268 | targetId.raw(),
|
---|
9269 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
9270 | NULL /* pVDIfsOperation */,
|
---|
9271 | pTarget->m->vdImageIfaces,
|
---|
9272 | task.mVDOperationIfaces);
|
---|
9273 | }
|
---|
9274 | if (RT_FAILURE(vrc))
|
---|
9275 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9276 | tr("Could not create the clone medium '%s'%s"),
|
---|
9277 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
9278 |
|
---|
9279 | size = VDGetFileSize(targetHdd, VD_LAST_IMAGE);
|
---|
9280 | logicalSize = VDGetSize(targetHdd, VD_LAST_IMAGE);
|
---|
9281 | unsigned uImageFlags;
|
---|
9282 | vrc = VDGetImageFlags(targetHdd, 0, &uImageFlags);
|
---|
9283 | if (RT_SUCCESS(vrc))
|
---|
9284 | variant = (MediumVariant_T)uImageFlags;
|
---|
9285 | }
|
---|
9286 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9287 |
|
---|
9288 | VDDestroy(targetHdd);
|
---|
9289 | }
|
---|
9290 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9291 |
|
---|
9292 | VDDestroy(hdd);
|
---|
9293 | }
|
---|
9294 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9295 |
|
---|
9296 | ErrorInfoKeeper eik;
|
---|
9297 | MultiResult mrc(rcTmp);
|
---|
9298 |
|
---|
9299 | /* Only do the parent changes for newly created media. */
|
---|
9300 | if (SUCCEEDED(mrc) && fCreatingTarget)
|
---|
9301 | {
|
---|
9302 | /* we set m->pParent & children() */
|
---|
9303 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
9304 |
|
---|
9305 | Assert(pTarget->m->pParent.isNull());
|
---|
9306 |
|
---|
9307 | if (pParent)
|
---|
9308 | {
|
---|
9309 | /* Associate the clone with the parent and deassociate
|
---|
9310 | * from VirtualBox. Depth check above. */
|
---|
9311 | pTarget->i_setParent(pParent);
|
---|
9312 |
|
---|
9313 | /* register with mVirtualBox as the last step and move to
|
---|
9314 | * Created state only on success (leaving an orphan file is
|
---|
9315 | * better than breaking media registry consistency) */
|
---|
9316 | eik.restore();
|
---|
9317 | ComObjPtr<Medium> pMedium;
|
---|
9318 | mrc = pParent->m->pVirtualBox->i_registerMedium(pTarget, &pMedium,
|
---|
9319 | treeLock);
|
---|
9320 | Assert( FAILED(mrc)
|
---|
9321 | || pTarget == pMedium);
|
---|
9322 | eik.fetch();
|
---|
9323 |
|
---|
9324 | if (FAILED(mrc))
|
---|
9325 | /* break parent association on failure to register */
|
---|
9326 | pTarget->i_deparent(); // removes target from parent
|
---|
9327 | }
|
---|
9328 | else
|
---|
9329 | {
|
---|
9330 | /* just register */
|
---|
9331 | eik.restore();
|
---|
9332 | ComObjPtr<Medium> pMedium;
|
---|
9333 | mrc = m->pVirtualBox->i_registerMedium(pTarget, &pMedium,
|
---|
9334 | treeLock);
|
---|
9335 | Assert( FAILED(mrc)
|
---|
9336 | || pTarget == pMedium);
|
---|
9337 | eik.fetch();
|
---|
9338 | }
|
---|
9339 | }
|
---|
9340 |
|
---|
9341 | if (fCreatingTarget)
|
---|
9342 | {
|
---|
9343 | AutoWriteLock mLock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
9344 |
|
---|
9345 | if (SUCCEEDED(mrc))
|
---|
9346 | {
|
---|
9347 | pTarget->m->state = MediumState_Created;
|
---|
9348 |
|
---|
9349 | pTarget->m->size = size;
|
---|
9350 | pTarget->m->logicalSize = logicalSize;
|
---|
9351 | pTarget->m->variant = variant;
|
---|
9352 | }
|
---|
9353 | else
|
---|
9354 | {
|
---|
9355 | /* back to NotCreated on failure */
|
---|
9356 | pTarget->m->state = MediumState_NotCreated;
|
---|
9357 |
|
---|
9358 | /* reset UUID to prevent it from being reused next time */
|
---|
9359 | if (fGenerateUuid)
|
---|
9360 | unconst(pTarget->m->id).clear();
|
---|
9361 | }
|
---|
9362 | }
|
---|
9363 |
|
---|
9364 | /* Copy any filter related settings over to the target. */
|
---|
9365 | if (SUCCEEDED(mrc))
|
---|
9366 | {
|
---|
9367 | /* Copy any filter related settings over. */
|
---|
9368 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
9369 | ComObjPtr<Medium> pTargetBase = pTarget->i_getBase();
|
---|
9370 | std::vector<com::Utf8Str> aFilterPropNames;
|
---|
9371 | std::vector<com::Utf8Str> aFilterPropValues;
|
---|
9372 | mrc = pBase->i_getFilterProperties(aFilterPropNames, aFilterPropValues);
|
---|
9373 | if (SUCCEEDED(mrc))
|
---|
9374 | {
|
---|
9375 | /* Go through the properties and add them to the target medium. */
|
---|
9376 | for (unsigned idx = 0; idx < aFilterPropNames.size(); idx++)
|
---|
9377 | {
|
---|
9378 | mrc = pTargetBase->i_setPropertyDirect(aFilterPropNames[idx], aFilterPropValues[idx]);
|
---|
9379 | if (FAILED(mrc)) break;
|
---|
9380 | }
|
---|
9381 |
|
---|
9382 | // now, at the end of this task (always asynchronous), save the settings
|
---|
9383 | if (SUCCEEDED(mrc))
|
---|
9384 | {
|
---|
9385 | // save the settings
|
---|
9386 | i_markRegistriesModified();
|
---|
9387 | /* collect multiple errors */
|
---|
9388 | eik.restore();
|
---|
9389 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
9390 | eik.fetch();
|
---|
9391 |
|
---|
9392 | if (task.NotifyAboutChanges())
|
---|
9393 | {
|
---|
9394 | if (!fCreatingTarget)
|
---|
9395 | {
|
---|
9396 | if (!aFilterPropNames.empty())
|
---|
9397 | m->pVirtualBox->i_onMediumConfigChanged(pTargetBase);
|
---|
9398 | if (pParent)
|
---|
9399 | m->pVirtualBox->i_onMediumConfigChanged(pParent);
|
---|
9400 | }
|
---|
9401 | else
|
---|
9402 | {
|
---|
9403 | m->pVirtualBox->i_onMediumRegistered(pTarget->i_getId(), pTarget->i_getDeviceType(), TRUE);
|
---|
9404 | }
|
---|
9405 | }
|
---|
9406 | }
|
---|
9407 | }
|
---|
9408 | }
|
---|
9409 |
|
---|
9410 | /* Everything is explicitly unlocked when the task exits,
|
---|
9411 | * as the task destruction also destroys the source chain. */
|
---|
9412 |
|
---|
9413 | /* Make sure the source chain is released early. It could happen
|
---|
9414 | * that we get a deadlock in Appliance::Import when Medium::Close
|
---|
9415 | * is called & the source chain is released at the same time. */
|
---|
9416 | task.mpSourceMediumLockList->Clear();
|
---|
9417 |
|
---|
9418 | return mrc;
|
---|
9419 | }
|
---|
9420 |
|
---|
9421 | /**
|
---|
9422 | * Implementation code for the "move" task.
|
---|
9423 | *
|
---|
9424 | * This only gets started from Medium::MoveTo() and always
|
---|
9425 | * runs asynchronously.
|
---|
9426 | *
|
---|
9427 | * @param task
|
---|
9428 | * @return
|
---|
9429 | */
|
---|
9430 | HRESULT Medium::i_taskMoveHandler(Medium::MoveTask &task)
|
---|
9431 | {
|
---|
9432 | LogFlowFuncEnter();
|
---|
9433 | HRESULT rcOut = S_OK;
|
---|
9434 |
|
---|
9435 | /* pTarget is equal "this" in our case */
|
---|
9436 | const ComObjPtr<Medium> &pTarget = task.mMedium;
|
---|
9437 |
|
---|
9438 | uint64_t size = 0; NOREF(size);
|
---|
9439 | uint64_t logicalSize = 0; NOREF(logicalSize);
|
---|
9440 | MediumVariant_T variant = MediumVariant_Standard; NOREF(variant);
|
---|
9441 |
|
---|
9442 | /*
|
---|
9443 | * it's exactly moving, not cloning
|
---|
9444 | */
|
---|
9445 | if (!i_isMoveOperation(pTarget))
|
---|
9446 | {
|
---|
9447 | HRESULT rc = setError(VBOX_E_FILE_ERROR,
|
---|
9448 | tr("Wrong preconditions for moving the medium %s"),
|
---|
9449 | pTarget->m->strLocationFull.c_str());
|
---|
9450 | LogFlowFunc(("LEAVE: rc=%Rhrc (early)\n", rc));
|
---|
9451 | return rc;
|
---|
9452 | }
|
---|
9453 |
|
---|
9454 | try
|
---|
9455 | {
|
---|
9456 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
9457 | * signal from the task initiator (which releases it only after
|
---|
9458 | * RTThreadCreate()) that we can start the job. */
|
---|
9459 |
|
---|
9460 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9461 |
|
---|
9462 | PVDISK hdd;
|
---|
9463 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9464 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9465 |
|
---|
9466 | try
|
---|
9467 | {
|
---|
9468 | /* Open all media in the source chain. */
|
---|
9469 | MediumLockList::Base::const_iterator sourceListBegin =
|
---|
9470 | task.mpMediumLockList->GetBegin();
|
---|
9471 | MediumLockList::Base::const_iterator sourceListEnd =
|
---|
9472 | task.mpMediumLockList->GetEnd();
|
---|
9473 | for (MediumLockList::Base::const_iterator it = sourceListBegin;
|
---|
9474 | it != sourceListEnd;
|
---|
9475 | ++it)
|
---|
9476 | {
|
---|
9477 | const MediumLock &mediumLock = *it;
|
---|
9478 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9479 | AutoWriteLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9480 |
|
---|
9481 | /* sanity check */
|
---|
9482 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
9483 |
|
---|
9484 | vrc = VDOpen(hdd,
|
---|
9485 | pMedium->m->strFormat.c_str(),
|
---|
9486 | pMedium->m->strLocationFull.c_str(),
|
---|
9487 | VD_OPEN_FLAGS_NORMAL,
|
---|
9488 | pMedium->m->vdImageIfaces);
|
---|
9489 | if (RT_FAILURE(vrc))
|
---|
9490 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9491 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9492 | pMedium->m->strLocationFull.c_str(),
|
---|
9493 | i_vdError(vrc).c_str());
|
---|
9494 | }
|
---|
9495 |
|
---|
9496 | /* we can directly use pTarget->m->"variables" but for better reading we use local copies */
|
---|
9497 | Guid targetId = pTarget->m->id;
|
---|
9498 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
9499 | uint64_t targetCapabilities = pTarget->m->formatObj->i_getCapabilities();
|
---|
9500 |
|
---|
9501 | /*
|
---|
9502 | * change target location
|
---|
9503 | * m->strNewLocationFull has been set already together with m->fMoveThisMedium in
|
---|
9504 | * i_preparationForMoving()
|
---|
9505 | */
|
---|
9506 | Utf8Str targetLocation = i_getNewLocationForMoving();
|
---|
9507 |
|
---|
9508 | /* unlock before the potentially lengthy operation */
|
---|
9509 | thisLock.release();
|
---|
9510 |
|
---|
9511 | /* ensure the target directory exists */
|
---|
9512 | if (targetCapabilities & MediumFormatCapabilities_File)
|
---|
9513 | {
|
---|
9514 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
9515 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
9516 | if (FAILED(rc))
|
---|
9517 | throw rc;
|
---|
9518 | }
|
---|
9519 |
|
---|
9520 | try
|
---|
9521 | {
|
---|
9522 | vrc = VDCopy(hdd,
|
---|
9523 | VD_LAST_IMAGE,
|
---|
9524 | hdd,
|
---|
9525 | targetFormat.c_str(),
|
---|
9526 | targetLocation.c_str(),
|
---|
9527 | true /* fMoveByRename */,
|
---|
9528 | 0 /* cbSize */,
|
---|
9529 | VD_IMAGE_FLAGS_NONE,
|
---|
9530 | targetId.raw(),
|
---|
9531 | VD_OPEN_FLAGS_NORMAL,
|
---|
9532 | NULL /* pVDIfsOperation */,
|
---|
9533 | NULL,
|
---|
9534 | NULL);
|
---|
9535 | if (RT_FAILURE(vrc))
|
---|
9536 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9537 | tr("Could not move medium '%s'%s"),
|
---|
9538 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
9539 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
9540 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
9541 | unsigned uImageFlags;
|
---|
9542 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
9543 | if (RT_SUCCESS(vrc))
|
---|
9544 | variant = (MediumVariant_T)uImageFlags;
|
---|
9545 |
|
---|
9546 | /*
|
---|
9547 | * set current location, because VDCopy\VDCopyEx doesn't do it.
|
---|
9548 | * also reset moving flag
|
---|
9549 | */
|
---|
9550 | i_resetMoveOperationData();
|
---|
9551 | m->strLocationFull = targetLocation;
|
---|
9552 |
|
---|
9553 | }
|
---|
9554 | catch (HRESULT aRC) { rcOut = aRC; }
|
---|
9555 |
|
---|
9556 | }
|
---|
9557 | catch (HRESULT aRC) { rcOut = aRC; }
|
---|
9558 |
|
---|
9559 | VDDestroy(hdd);
|
---|
9560 | }
|
---|
9561 | catch (HRESULT aRC) { rcOut = aRC; }
|
---|
9562 |
|
---|
9563 | ErrorInfoKeeper eik;
|
---|
9564 | MultiResult mrc(rcOut);
|
---|
9565 |
|
---|
9566 | // now, at the end of this task (always asynchronous), save the settings
|
---|
9567 | if (SUCCEEDED(mrc))
|
---|
9568 | {
|
---|
9569 | // save the settings
|
---|
9570 | i_markRegistriesModified();
|
---|
9571 | /* collect multiple errors */
|
---|
9572 | eik.restore();
|
---|
9573 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
9574 | eik.fetch();
|
---|
9575 | }
|
---|
9576 |
|
---|
9577 | /* Everything is explicitly unlocked when the task exits,
|
---|
9578 | * as the task destruction also destroys the source chain. */
|
---|
9579 |
|
---|
9580 | task.mpMediumLockList->Clear();
|
---|
9581 |
|
---|
9582 | if (task.NotifyAboutChanges() && SUCCEEDED(mrc))
|
---|
9583 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
9584 |
|
---|
9585 | LogFlowFunc(("LEAVE: mrc=%Rhrc\n", (HRESULT)mrc));
|
---|
9586 | return mrc;
|
---|
9587 | }
|
---|
9588 |
|
---|
9589 | /**
|
---|
9590 | * Implementation code for the "delete" task.
|
---|
9591 | *
|
---|
9592 | * This task always gets started from Medium::deleteStorage() and can run
|
---|
9593 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
9594 | * that function.
|
---|
9595 | *
|
---|
9596 | * @param task
|
---|
9597 | * @return
|
---|
9598 | */
|
---|
9599 | HRESULT Medium::i_taskDeleteHandler(Medium::DeleteTask &task)
|
---|
9600 | {
|
---|
9601 | NOREF(task);
|
---|
9602 | HRESULT rc = S_OK;
|
---|
9603 |
|
---|
9604 | try
|
---|
9605 | {
|
---|
9606 | /* The lock is also used as a signal from the task initiator (which
|
---|
9607 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
9608 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9609 |
|
---|
9610 | PVDISK hdd;
|
---|
9611 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9612 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9613 |
|
---|
9614 | Utf8Str format(m->strFormat);
|
---|
9615 | Utf8Str location(m->strLocationFull);
|
---|
9616 |
|
---|
9617 | /* unlock before the potentially lengthy operation */
|
---|
9618 | Assert(m->state == MediumState_Deleting);
|
---|
9619 | thisLock.release();
|
---|
9620 |
|
---|
9621 | try
|
---|
9622 | {
|
---|
9623 | vrc = VDOpen(hdd,
|
---|
9624 | format.c_str(),
|
---|
9625 | location.c_str(),
|
---|
9626 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
9627 | m->vdImageIfaces);
|
---|
9628 | if (RT_SUCCESS(vrc))
|
---|
9629 | vrc = VDClose(hdd, true /* fDelete */);
|
---|
9630 |
|
---|
9631 | if (RT_FAILURE(vrc) && vrc != VERR_FILE_NOT_FOUND)
|
---|
9632 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9633 | tr("Could not delete the medium storage unit '%s'%s"),
|
---|
9634 | location.c_str(), i_vdError(vrc).c_str());
|
---|
9635 |
|
---|
9636 | }
|
---|
9637 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9638 |
|
---|
9639 | VDDestroy(hdd);
|
---|
9640 | }
|
---|
9641 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9642 |
|
---|
9643 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9644 |
|
---|
9645 | /* go to the NotCreated state even on failure since the storage
|
---|
9646 | * may have been already partially deleted and cannot be used any
|
---|
9647 | * more. One will be able to manually re-open the storage if really
|
---|
9648 | * needed to re-register it. */
|
---|
9649 | m->state = MediumState_NotCreated;
|
---|
9650 |
|
---|
9651 | /* Reset UUID to prevent Create* from reusing it again */
|
---|
9652 | unconst(m->id).clear();
|
---|
9653 |
|
---|
9654 | if (task.NotifyAboutChanges() && SUCCEEDED(rc) && m->pParent.isNotNull())
|
---|
9655 | m->pVirtualBox->i_onMediumConfigChanged(m->pParent);
|
---|
9656 |
|
---|
9657 | return rc;
|
---|
9658 | }
|
---|
9659 |
|
---|
9660 | /**
|
---|
9661 | * Implementation code for the "reset" task.
|
---|
9662 | *
|
---|
9663 | * This always gets started asynchronously from Medium::Reset().
|
---|
9664 | *
|
---|
9665 | * @param task
|
---|
9666 | * @return
|
---|
9667 | */
|
---|
9668 | HRESULT Medium::i_taskResetHandler(Medium::ResetTask &task)
|
---|
9669 | {
|
---|
9670 | HRESULT rc = S_OK;
|
---|
9671 |
|
---|
9672 | uint64_t size = 0, logicalSize = 0;
|
---|
9673 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
9674 |
|
---|
9675 | try
|
---|
9676 | {
|
---|
9677 | /* The lock is also used as a signal from the task initiator (which
|
---|
9678 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
9679 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9680 |
|
---|
9681 | /// @todo Below we use a pair of delete/create operations to reset
|
---|
9682 | /// the diff contents but the most efficient way will of course be
|
---|
9683 | /// to add a VDResetDiff() API call
|
---|
9684 |
|
---|
9685 | PVDISK hdd;
|
---|
9686 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9687 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9688 |
|
---|
9689 | Guid id = m->id;
|
---|
9690 | Utf8Str format(m->strFormat);
|
---|
9691 | Utf8Str location(m->strLocationFull);
|
---|
9692 |
|
---|
9693 | Medium *pParent = m->pParent;
|
---|
9694 | Guid parentId = pParent->m->id;
|
---|
9695 | Utf8Str parentFormat(pParent->m->strFormat);
|
---|
9696 | Utf8Str parentLocation(pParent->m->strLocationFull);
|
---|
9697 |
|
---|
9698 | Assert(m->state == MediumState_LockedWrite);
|
---|
9699 |
|
---|
9700 | /* unlock before the potentially lengthy operation */
|
---|
9701 | thisLock.release();
|
---|
9702 |
|
---|
9703 | try
|
---|
9704 | {
|
---|
9705 | /* Open all media in the target chain but the last. */
|
---|
9706 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
9707 | task.mpMediumLockList->GetBegin();
|
---|
9708 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
9709 | task.mpMediumLockList->GetEnd();
|
---|
9710 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
9711 | it != targetListEnd;
|
---|
9712 | ++it)
|
---|
9713 | {
|
---|
9714 | const MediumLock &mediumLock = *it;
|
---|
9715 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9716 |
|
---|
9717 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9718 |
|
---|
9719 | /* sanity check, "this" is checked above */
|
---|
9720 | Assert( pMedium == this
|
---|
9721 | || pMedium->m->state == MediumState_LockedRead);
|
---|
9722 |
|
---|
9723 | /* Open all media in appropriate mode. */
|
---|
9724 | vrc = VDOpen(hdd,
|
---|
9725 | pMedium->m->strFormat.c_str(),
|
---|
9726 | pMedium->m->strLocationFull.c_str(),
|
---|
9727 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
9728 | pMedium->m->vdImageIfaces);
|
---|
9729 | if (RT_FAILURE(vrc))
|
---|
9730 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9731 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9732 | pMedium->m->strLocationFull.c_str(),
|
---|
9733 | i_vdError(vrc).c_str());
|
---|
9734 |
|
---|
9735 | /* Done when we hit the media which should be reset */
|
---|
9736 | if (pMedium == this)
|
---|
9737 | break;
|
---|
9738 | }
|
---|
9739 |
|
---|
9740 | /* first, delete the storage unit */
|
---|
9741 | vrc = VDClose(hdd, true /* fDelete */);
|
---|
9742 | if (RT_FAILURE(vrc))
|
---|
9743 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9744 | tr("Could not delete the medium storage unit '%s'%s"),
|
---|
9745 | location.c_str(), i_vdError(vrc).c_str());
|
---|
9746 |
|
---|
9747 | /* next, create it again */
|
---|
9748 | vrc = VDOpen(hdd,
|
---|
9749 | parentFormat.c_str(),
|
---|
9750 | parentLocation.c_str(),
|
---|
9751 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
9752 | m->vdImageIfaces);
|
---|
9753 | if (RT_FAILURE(vrc))
|
---|
9754 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9755 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9756 | parentLocation.c_str(), i_vdError(vrc).c_str());
|
---|
9757 |
|
---|
9758 | vrc = VDCreateDiff(hdd,
|
---|
9759 | format.c_str(),
|
---|
9760 | location.c_str(),
|
---|
9761 | /// @todo use the same medium variant as before
|
---|
9762 | VD_IMAGE_FLAGS_NONE,
|
---|
9763 | NULL,
|
---|
9764 | id.raw(),
|
---|
9765 | parentId.raw(),
|
---|
9766 | VD_OPEN_FLAGS_NORMAL,
|
---|
9767 | m->vdImageIfaces,
|
---|
9768 | task.mVDOperationIfaces);
|
---|
9769 | if (RT_FAILURE(vrc))
|
---|
9770 | {
|
---|
9771 | if (vrc == VERR_VD_INVALID_TYPE)
|
---|
9772 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9773 | tr("Parameters for creating the differencing medium storage unit '%s' are invalid%s"),
|
---|
9774 | location.c_str(), i_vdError(vrc).c_str());
|
---|
9775 | else
|
---|
9776 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9777 | tr("Could not create the differencing medium storage unit '%s'%s"),
|
---|
9778 | location.c_str(), i_vdError(vrc).c_str());
|
---|
9779 | }
|
---|
9780 |
|
---|
9781 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
9782 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
9783 | unsigned uImageFlags;
|
---|
9784 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
9785 | if (RT_SUCCESS(vrc))
|
---|
9786 | variant = (MediumVariant_T)uImageFlags;
|
---|
9787 | }
|
---|
9788 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9789 |
|
---|
9790 | VDDestroy(hdd);
|
---|
9791 | }
|
---|
9792 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9793 |
|
---|
9794 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9795 |
|
---|
9796 | m->size = size;
|
---|
9797 | m->logicalSize = logicalSize;
|
---|
9798 | m->variant = variant;
|
---|
9799 |
|
---|
9800 | if (task.NotifyAboutChanges() && SUCCEEDED(rc))
|
---|
9801 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
9802 |
|
---|
9803 | /* Everything is explicitly unlocked when the task exits,
|
---|
9804 | * as the task destruction also destroys the media chain. */
|
---|
9805 |
|
---|
9806 | return rc;
|
---|
9807 | }
|
---|
9808 |
|
---|
9809 | /**
|
---|
9810 | * Implementation code for the "compact" task.
|
---|
9811 | *
|
---|
9812 | * @param task
|
---|
9813 | * @return
|
---|
9814 | */
|
---|
9815 | HRESULT Medium::i_taskCompactHandler(Medium::CompactTask &task)
|
---|
9816 | {
|
---|
9817 | HRESULT rc = S_OK;
|
---|
9818 |
|
---|
9819 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
9820 | * signal from the task initiator (which releases it only after
|
---|
9821 | * RTThreadCreate()) that we can start the job. */
|
---|
9822 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9823 |
|
---|
9824 | try
|
---|
9825 | {
|
---|
9826 | PVDISK hdd;
|
---|
9827 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9828 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9829 |
|
---|
9830 | try
|
---|
9831 | {
|
---|
9832 | /* Open all media in the chain. */
|
---|
9833 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
9834 | task.mpMediumLockList->GetBegin();
|
---|
9835 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
9836 | task.mpMediumLockList->GetEnd();
|
---|
9837 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
9838 | mediumListEnd;
|
---|
9839 | --mediumListLast;
|
---|
9840 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
9841 | it != mediumListEnd;
|
---|
9842 | ++it)
|
---|
9843 | {
|
---|
9844 | const MediumLock &mediumLock = *it;
|
---|
9845 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9846 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9847 |
|
---|
9848 | /* sanity check */
|
---|
9849 | if (it == mediumListLast)
|
---|
9850 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
9851 | else
|
---|
9852 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
9853 |
|
---|
9854 | /* Open all media but last in read-only mode. Do not handle
|
---|
9855 | * shareable media, as compaction and sharing are mutually
|
---|
9856 | * exclusive. */
|
---|
9857 | vrc = VDOpen(hdd,
|
---|
9858 | pMedium->m->strFormat.c_str(),
|
---|
9859 | pMedium->m->strLocationFull.c_str(),
|
---|
9860 | m->uOpenFlagsDef | (it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
9861 | pMedium->m->vdImageIfaces);
|
---|
9862 | if (RT_FAILURE(vrc))
|
---|
9863 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9864 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9865 | pMedium->m->strLocationFull.c_str(),
|
---|
9866 | i_vdError(vrc).c_str());
|
---|
9867 | }
|
---|
9868 |
|
---|
9869 | Assert(m->state == MediumState_LockedWrite);
|
---|
9870 |
|
---|
9871 | Utf8Str location(m->strLocationFull);
|
---|
9872 |
|
---|
9873 | /* unlock before the potentially lengthy operation */
|
---|
9874 | thisLock.release();
|
---|
9875 |
|
---|
9876 | vrc = VDCompact(hdd, VD_LAST_IMAGE, task.mVDOperationIfaces);
|
---|
9877 | if (RT_FAILURE(vrc))
|
---|
9878 | {
|
---|
9879 | if (vrc == VERR_NOT_SUPPORTED)
|
---|
9880 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
9881 | tr("Compacting is not yet supported for medium '%s'"),
|
---|
9882 | location.c_str());
|
---|
9883 | else if (vrc == VERR_NOT_IMPLEMENTED)
|
---|
9884 | throw setErrorBoth(E_NOTIMPL, vrc,
|
---|
9885 | tr("Compacting is not implemented, medium '%s'"),
|
---|
9886 | location.c_str());
|
---|
9887 | else
|
---|
9888 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9889 | tr("Could not compact medium '%s'%s"),
|
---|
9890 | location.c_str(),
|
---|
9891 | i_vdError(vrc).c_str());
|
---|
9892 | }
|
---|
9893 | }
|
---|
9894 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9895 |
|
---|
9896 | VDDestroy(hdd);
|
---|
9897 | }
|
---|
9898 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9899 |
|
---|
9900 | if (task.NotifyAboutChanges() && SUCCEEDED(rc))
|
---|
9901 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
9902 |
|
---|
9903 | /* Everything is explicitly unlocked when the task exits,
|
---|
9904 | * as the task destruction also destroys the media chain. */
|
---|
9905 |
|
---|
9906 | return rc;
|
---|
9907 | }
|
---|
9908 |
|
---|
9909 | /**
|
---|
9910 | * Implementation code for the "resize" task.
|
---|
9911 | *
|
---|
9912 | * @param task
|
---|
9913 | * @return
|
---|
9914 | */
|
---|
9915 | HRESULT Medium::i_taskResizeHandler(Medium::ResizeTask &task)
|
---|
9916 | {
|
---|
9917 | HRESULT rc = S_OK;
|
---|
9918 |
|
---|
9919 | uint64_t size = 0, logicalSize = 0;
|
---|
9920 |
|
---|
9921 | try
|
---|
9922 | {
|
---|
9923 | /* The lock is also used as a signal from the task initiator (which
|
---|
9924 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
9925 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9926 |
|
---|
9927 | PVDISK hdd;
|
---|
9928 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9929 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9930 |
|
---|
9931 | try
|
---|
9932 | {
|
---|
9933 | /* Open all media in the chain. */
|
---|
9934 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
9935 | task.mpMediumLockList->GetBegin();
|
---|
9936 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
9937 | task.mpMediumLockList->GetEnd();
|
---|
9938 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
9939 | mediumListEnd;
|
---|
9940 | --mediumListLast;
|
---|
9941 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
9942 | it != mediumListEnd;
|
---|
9943 | ++it)
|
---|
9944 | {
|
---|
9945 | const MediumLock &mediumLock = *it;
|
---|
9946 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9947 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9948 |
|
---|
9949 | /* sanity check */
|
---|
9950 | if (it == mediumListLast)
|
---|
9951 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
9952 | else
|
---|
9953 | Assert(pMedium->m->state == MediumState_LockedRead ||
|
---|
9954 | // Allow resize the target image during mergeTo in case
|
---|
9955 | // of direction from parent to child because all intermediate
|
---|
9956 | // images are marked to MediumState_Deleting and will be
|
---|
9957 | // destroyed after successful merge
|
---|
9958 | pMedium->m->state == MediumState_Deleting);
|
---|
9959 |
|
---|
9960 | /* Open all media but last in read-only mode. Do not handle
|
---|
9961 | * shareable media, as compaction and sharing are mutually
|
---|
9962 | * exclusive. */
|
---|
9963 | vrc = VDOpen(hdd,
|
---|
9964 | pMedium->m->strFormat.c_str(),
|
---|
9965 | pMedium->m->strLocationFull.c_str(),
|
---|
9966 | m->uOpenFlagsDef | (it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
9967 | pMedium->m->vdImageIfaces);
|
---|
9968 | if (RT_FAILURE(vrc))
|
---|
9969 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9970 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9971 | pMedium->m->strLocationFull.c_str(),
|
---|
9972 | i_vdError(vrc).c_str());
|
---|
9973 | }
|
---|
9974 |
|
---|
9975 | Assert(m->state == MediumState_LockedWrite);
|
---|
9976 |
|
---|
9977 | Utf8Str location(m->strLocationFull);
|
---|
9978 |
|
---|
9979 | /* unlock before the potentially lengthy operation */
|
---|
9980 | thisLock.release();
|
---|
9981 |
|
---|
9982 | VDGEOMETRY geo = {0, 0, 0}; /* auto */
|
---|
9983 | vrc = VDResize(hdd, task.mSize, &geo, &geo, task.mVDOperationIfaces);
|
---|
9984 | if (RT_FAILURE(vrc))
|
---|
9985 | {
|
---|
9986 | if (vrc == VERR_VD_SHRINK_NOT_SUPPORTED)
|
---|
9987 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
9988 | tr("Shrinking is not yet supported for medium '%s'"),
|
---|
9989 | location.c_str());
|
---|
9990 | if (vrc == VERR_NOT_SUPPORTED)
|
---|
9991 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
9992 | tr("Resizing to new size %llu is not yet supported for medium '%s'"),
|
---|
9993 | task.mSize, location.c_str());
|
---|
9994 | else if (vrc == VERR_NOT_IMPLEMENTED)
|
---|
9995 | throw setErrorBoth(E_NOTIMPL, vrc,
|
---|
9996 | tr("Resiting is not implemented, medium '%s'"),
|
---|
9997 | location.c_str());
|
---|
9998 | else
|
---|
9999 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10000 | tr("Could not resize medium '%s'%s"),
|
---|
10001 | location.c_str(),
|
---|
10002 | i_vdError(vrc).c_str());
|
---|
10003 | }
|
---|
10004 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
10005 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
10006 | }
|
---|
10007 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10008 |
|
---|
10009 | VDDestroy(hdd);
|
---|
10010 | }
|
---|
10011 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10012 |
|
---|
10013 | if (SUCCEEDED(rc))
|
---|
10014 | {
|
---|
10015 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
10016 | m->size = size;
|
---|
10017 | m->logicalSize = logicalSize;
|
---|
10018 |
|
---|
10019 | if (task.NotifyAboutChanges())
|
---|
10020 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
10021 | }
|
---|
10022 |
|
---|
10023 | /* Everything is explicitly unlocked when the task exits,
|
---|
10024 | * as the task destruction also destroys the media chain. */
|
---|
10025 |
|
---|
10026 | return rc;
|
---|
10027 | }
|
---|
10028 |
|
---|
10029 | /**
|
---|
10030 | * Implementation code for the "import" task.
|
---|
10031 | *
|
---|
10032 | * This only gets started from Medium::importFile() and always runs
|
---|
10033 | * asynchronously. It potentially touches the media registry, so we
|
---|
10034 | * always save the VirtualBox.xml file when we're done here.
|
---|
10035 | *
|
---|
10036 | * @param task
|
---|
10037 | * @return
|
---|
10038 | */
|
---|
10039 | HRESULT Medium::i_taskImportHandler(Medium::ImportTask &task)
|
---|
10040 | {
|
---|
10041 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
10042 | * to lock order violations, it probably causes lock order issues related
|
---|
10043 | * to the AutoCaller usage. */
|
---|
10044 | HRESULT rcTmp = S_OK;
|
---|
10045 |
|
---|
10046 | const ComObjPtr<Medium> &pParent = task.mParent;
|
---|
10047 |
|
---|
10048 | bool fCreatingTarget = false;
|
---|
10049 |
|
---|
10050 | uint64_t size = 0, logicalSize = 0;
|
---|
10051 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
10052 | bool fGenerateUuid = false;
|
---|
10053 |
|
---|
10054 | try
|
---|
10055 | {
|
---|
10056 | if (!pParent.isNull())
|
---|
10057 | if (pParent->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
10058 | {
|
---|
10059 | AutoReadLock plock(pParent COMMA_LOCKVAL_SRC_POS);
|
---|
10060 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10061 | tr("Cannot import image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
10062 | pParent->m->strLocationFull.c_str());
|
---|
10063 | }
|
---|
10064 |
|
---|
10065 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
10066 | * signal from the task initiator (which releases it only after
|
---|
10067 | * RTThreadCreate()) that we can start the job. */
|
---|
10068 | AutoMultiWriteLock2 thisLock(this, pParent COMMA_LOCKVAL_SRC_POS);
|
---|
10069 |
|
---|
10070 | fCreatingTarget = m->state == MediumState_Creating;
|
---|
10071 |
|
---|
10072 | /* The object may request a specific UUID (through a special form of
|
---|
10073 | * the moveTo() argument). Otherwise we have to generate it */
|
---|
10074 | Guid targetId = m->id;
|
---|
10075 |
|
---|
10076 | fGenerateUuid = targetId.isZero();
|
---|
10077 | if (fGenerateUuid)
|
---|
10078 | {
|
---|
10079 | targetId.create();
|
---|
10080 | /* VirtualBox::i_registerMedium() will need UUID */
|
---|
10081 | unconst(m->id) = targetId;
|
---|
10082 | }
|
---|
10083 |
|
---|
10084 |
|
---|
10085 | PVDISK hdd;
|
---|
10086 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
10087 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
10088 |
|
---|
10089 | try
|
---|
10090 | {
|
---|
10091 | /* Open source medium. */
|
---|
10092 | vrc = VDOpen(hdd,
|
---|
10093 | task.mFormat->i_getId().c_str(),
|
---|
10094 | task.mFilename.c_str(),
|
---|
10095 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SEQUENTIAL | m->uOpenFlagsDef,
|
---|
10096 | task.mVDImageIfaces);
|
---|
10097 | if (RT_FAILURE(vrc))
|
---|
10098 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10099 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
10100 | task.mFilename.c_str(),
|
---|
10101 | i_vdError(vrc).c_str());
|
---|
10102 |
|
---|
10103 | Utf8Str targetFormat(m->strFormat);
|
---|
10104 | Utf8Str targetLocation(m->strLocationFull);
|
---|
10105 | uint64_t capabilities = task.mFormat->i_getCapabilities();
|
---|
10106 |
|
---|
10107 | Assert( m->state == MediumState_Creating
|
---|
10108 | || m->state == MediumState_LockedWrite);
|
---|
10109 | Assert( pParent.isNull()
|
---|
10110 | || pParent->m->state == MediumState_LockedRead);
|
---|
10111 |
|
---|
10112 | /* unlock before the potentially lengthy operation */
|
---|
10113 | thisLock.release();
|
---|
10114 |
|
---|
10115 | /* ensure the target directory exists */
|
---|
10116 | if (capabilities & MediumFormatCapabilities_File)
|
---|
10117 | {
|
---|
10118 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
10119 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
10120 | if (FAILED(rc))
|
---|
10121 | throw rc;
|
---|
10122 | }
|
---|
10123 |
|
---|
10124 | PVDISK targetHdd;
|
---|
10125 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &targetHdd);
|
---|
10126 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
10127 |
|
---|
10128 | try
|
---|
10129 | {
|
---|
10130 | /* Open all media in the target chain. */
|
---|
10131 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
10132 | task.mpTargetMediumLockList->GetBegin();
|
---|
10133 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
10134 | task.mpTargetMediumLockList->GetEnd();
|
---|
10135 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
10136 | it != targetListEnd;
|
---|
10137 | ++it)
|
---|
10138 | {
|
---|
10139 | const MediumLock &mediumLock = *it;
|
---|
10140 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
10141 |
|
---|
10142 | /* If the target medium is not created yet there's no
|
---|
10143 | * reason to open it. */
|
---|
10144 | if (pMedium == this && fCreatingTarget)
|
---|
10145 | continue;
|
---|
10146 |
|
---|
10147 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
10148 |
|
---|
10149 | /* sanity check */
|
---|
10150 | Assert( pMedium->m->state == MediumState_LockedRead
|
---|
10151 | || pMedium->m->state == MediumState_LockedWrite);
|
---|
10152 |
|
---|
10153 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
10154 | if (pMedium->m->state != MediumState_LockedWrite)
|
---|
10155 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
10156 | if (pMedium->m->type == MediumType_Shareable)
|
---|
10157 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
10158 |
|
---|
10159 | /* Open all media in appropriate mode. */
|
---|
10160 | vrc = VDOpen(targetHdd,
|
---|
10161 | pMedium->m->strFormat.c_str(),
|
---|
10162 | pMedium->m->strLocationFull.c_str(),
|
---|
10163 | uOpenFlags | m->uOpenFlagsDef,
|
---|
10164 | pMedium->m->vdImageIfaces);
|
---|
10165 | if (RT_FAILURE(vrc))
|
---|
10166 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10167 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
10168 | pMedium->m->strLocationFull.c_str(),
|
---|
10169 | i_vdError(vrc).c_str());
|
---|
10170 | }
|
---|
10171 |
|
---|
10172 | vrc = VDCopy(hdd,
|
---|
10173 | VD_LAST_IMAGE,
|
---|
10174 | targetHdd,
|
---|
10175 | targetFormat.c_str(),
|
---|
10176 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
10177 | false /* fMoveByRename */,
|
---|
10178 | 0 /* cbSize */,
|
---|
10179 | task.mVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted),
|
---|
10180 | targetId.raw(),
|
---|
10181 | VD_OPEN_FLAGS_NORMAL,
|
---|
10182 | NULL /* pVDIfsOperation */,
|
---|
10183 | m->vdImageIfaces,
|
---|
10184 | task.mVDOperationIfaces);
|
---|
10185 | if (RT_FAILURE(vrc))
|
---|
10186 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10187 | tr("Could not create the imported medium '%s'%s"),
|
---|
10188 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
10189 |
|
---|
10190 | size = VDGetFileSize(targetHdd, VD_LAST_IMAGE);
|
---|
10191 | logicalSize = VDGetSize(targetHdd, VD_LAST_IMAGE);
|
---|
10192 | unsigned uImageFlags;
|
---|
10193 | vrc = VDGetImageFlags(targetHdd, 0, &uImageFlags);
|
---|
10194 | if (RT_SUCCESS(vrc))
|
---|
10195 | variant = (MediumVariant_T)uImageFlags;
|
---|
10196 | }
|
---|
10197 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
10198 |
|
---|
10199 | VDDestroy(targetHdd);
|
---|
10200 | }
|
---|
10201 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
10202 |
|
---|
10203 | VDDestroy(hdd);
|
---|
10204 | }
|
---|
10205 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
10206 |
|
---|
10207 | ErrorInfoKeeper eik;
|
---|
10208 | MultiResult mrc(rcTmp);
|
---|
10209 |
|
---|
10210 | /* Only do the parent changes for newly created media. */
|
---|
10211 | if (SUCCEEDED(mrc) && fCreatingTarget)
|
---|
10212 | {
|
---|
10213 | /* we set m->pParent & children() */
|
---|
10214 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
10215 |
|
---|
10216 | Assert(m->pParent.isNull());
|
---|
10217 |
|
---|
10218 | if (pParent)
|
---|
10219 | {
|
---|
10220 | /* Associate the imported medium with the parent and deassociate
|
---|
10221 | * from VirtualBox. Depth check above. */
|
---|
10222 | i_setParent(pParent);
|
---|
10223 |
|
---|
10224 | /* register with mVirtualBox as the last step and move to
|
---|
10225 | * Created state only on success (leaving an orphan file is
|
---|
10226 | * better than breaking media registry consistency) */
|
---|
10227 | eik.restore();
|
---|
10228 | ComObjPtr<Medium> pMedium;
|
---|
10229 | mrc = pParent->m->pVirtualBox->i_registerMedium(this, &pMedium,
|
---|
10230 | treeLock);
|
---|
10231 | Assert(this == pMedium);
|
---|
10232 | eik.fetch();
|
---|
10233 |
|
---|
10234 | if (FAILED(mrc))
|
---|
10235 | /* break parent association on failure to register */
|
---|
10236 | this->i_deparent(); // removes target from parent
|
---|
10237 | }
|
---|
10238 | else
|
---|
10239 | {
|
---|
10240 | /* just register */
|
---|
10241 | eik.restore();
|
---|
10242 | ComObjPtr<Medium> pMedium;
|
---|
10243 | mrc = m->pVirtualBox->i_registerMedium(this, &pMedium, treeLock);
|
---|
10244 | Assert(this == pMedium);
|
---|
10245 | eik.fetch();
|
---|
10246 | }
|
---|
10247 | }
|
---|
10248 |
|
---|
10249 | if (fCreatingTarget)
|
---|
10250 | {
|
---|
10251 | AutoWriteLock mLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
10252 |
|
---|
10253 | if (SUCCEEDED(mrc))
|
---|
10254 | {
|
---|
10255 | m->state = MediumState_Created;
|
---|
10256 |
|
---|
10257 | m->size = size;
|
---|
10258 | m->logicalSize = logicalSize;
|
---|
10259 | m->variant = variant;
|
---|
10260 | }
|
---|
10261 | else
|
---|
10262 | {
|
---|
10263 | /* back to NotCreated on failure */
|
---|
10264 | m->state = MediumState_NotCreated;
|
---|
10265 |
|
---|
10266 | /* reset UUID to prevent it from being reused next time */
|
---|
10267 | if (fGenerateUuid)
|
---|
10268 | unconst(m->id).clear();
|
---|
10269 | }
|
---|
10270 | }
|
---|
10271 |
|
---|
10272 | // now, at the end of this task (always asynchronous), save the settings
|
---|
10273 | {
|
---|
10274 | // save the settings
|
---|
10275 | i_markRegistriesModified();
|
---|
10276 | /* collect multiple errors */
|
---|
10277 | eik.restore();
|
---|
10278 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
10279 | eik.fetch();
|
---|
10280 | }
|
---|
10281 |
|
---|
10282 | /* Everything is explicitly unlocked when the task exits,
|
---|
10283 | * as the task destruction also destroys the target chain. */
|
---|
10284 |
|
---|
10285 | /* Make sure the target chain is released early, otherwise it can
|
---|
10286 | * lead to deadlocks with concurrent IAppliance activities. */
|
---|
10287 | task.mpTargetMediumLockList->Clear();
|
---|
10288 |
|
---|
10289 | if (task.NotifyAboutChanges() && SUCCEEDED(mrc))
|
---|
10290 | {
|
---|
10291 | if (pParent)
|
---|
10292 | m->pVirtualBox->i_onMediumConfigChanged(pParent);
|
---|
10293 | if (fCreatingTarget)
|
---|
10294 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
10295 | else
|
---|
10296 | m->pVirtualBox->i_onMediumRegistered(m->id, m->devType, TRUE);
|
---|
10297 | }
|
---|
10298 |
|
---|
10299 | return mrc;
|
---|
10300 | }
|
---|
10301 |
|
---|
10302 | /**
|
---|
10303 | * Sets up the encryption settings for a filter.
|
---|
10304 | */
|
---|
10305 | void Medium::i_taskEncryptSettingsSetup(MediumCryptoFilterSettings *pSettings, const char *pszCipher,
|
---|
10306 | const char *pszKeyStore, const char *pszPassword,
|
---|
10307 | bool fCreateKeyStore)
|
---|
10308 | {
|
---|
10309 | pSettings->pszCipher = pszCipher;
|
---|
10310 | pSettings->pszPassword = pszPassword;
|
---|
10311 | pSettings->pszKeyStoreLoad = pszKeyStore;
|
---|
10312 | pSettings->fCreateKeyStore = fCreateKeyStore;
|
---|
10313 | pSettings->pbDek = NULL;
|
---|
10314 | pSettings->cbDek = 0;
|
---|
10315 | pSettings->vdFilterIfaces = NULL;
|
---|
10316 |
|
---|
10317 | pSettings->vdIfCfg.pfnAreKeysValid = i_vdCryptoConfigAreKeysValid;
|
---|
10318 | pSettings->vdIfCfg.pfnQuerySize = i_vdCryptoConfigQuerySize;
|
---|
10319 | pSettings->vdIfCfg.pfnQuery = i_vdCryptoConfigQuery;
|
---|
10320 | pSettings->vdIfCfg.pfnQueryBytes = NULL;
|
---|
10321 |
|
---|
10322 | pSettings->vdIfCrypto.pfnKeyRetain = i_vdCryptoKeyRetain;
|
---|
10323 | pSettings->vdIfCrypto.pfnKeyRelease = i_vdCryptoKeyRelease;
|
---|
10324 | pSettings->vdIfCrypto.pfnKeyStorePasswordRetain = i_vdCryptoKeyStorePasswordRetain;
|
---|
10325 | pSettings->vdIfCrypto.pfnKeyStorePasswordRelease = i_vdCryptoKeyStorePasswordRelease;
|
---|
10326 | pSettings->vdIfCrypto.pfnKeyStoreSave = i_vdCryptoKeyStoreSave;
|
---|
10327 | pSettings->vdIfCrypto.pfnKeyStoreReturnParameters = i_vdCryptoKeyStoreReturnParameters;
|
---|
10328 |
|
---|
10329 | int vrc = VDInterfaceAdd(&pSettings->vdIfCfg.Core,
|
---|
10330 | "Medium::vdInterfaceCfgCrypto",
|
---|
10331 | VDINTERFACETYPE_CONFIG, pSettings,
|
---|
10332 | sizeof(VDINTERFACECONFIG), &pSettings->vdFilterIfaces);
|
---|
10333 | AssertRC(vrc);
|
---|
10334 |
|
---|
10335 | vrc = VDInterfaceAdd(&pSettings->vdIfCrypto.Core,
|
---|
10336 | "Medium::vdInterfaceCrypto",
|
---|
10337 | VDINTERFACETYPE_CRYPTO, pSettings,
|
---|
10338 | sizeof(VDINTERFACECRYPTO), &pSettings->vdFilterIfaces);
|
---|
10339 | AssertRC(vrc);
|
---|
10340 | }
|
---|
10341 |
|
---|
10342 | /**
|
---|
10343 | * Implementation code for the "encrypt" task.
|
---|
10344 | *
|
---|
10345 | * @param task
|
---|
10346 | * @return
|
---|
10347 | */
|
---|
10348 | HRESULT Medium::i_taskEncryptHandler(Medium::EncryptTask &task)
|
---|
10349 | {
|
---|
10350 | # ifndef VBOX_WITH_EXTPACK
|
---|
10351 | RT_NOREF(task);
|
---|
10352 | # endif
|
---|
10353 | HRESULT rc = S_OK;
|
---|
10354 |
|
---|
10355 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
10356 | * signal from the task initiator (which releases it only after
|
---|
10357 | * RTThreadCreate()) that we can start the job. */
|
---|
10358 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
10359 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
10360 |
|
---|
10361 | try
|
---|
10362 | {
|
---|
10363 | # ifdef VBOX_WITH_EXTPACK
|
---|
10364 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
10365 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
10366 | {
|
---|
10367 | /* Load the plugin */
|
---|
10368 | Utf8Str strPlugin;
|
---|
10369 | rc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
10370 | if (SUCCEEDED(rc))
|
---|
10371 | {
|
---|
10372 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
10373 | if (RT_FAILURE(vrc))
|
---|
10374 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
10375 | tr("Encrypting the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
10376 | i_vdError(vrc).c_str());
|
---|
10377 | }
|
---|
10378 | else
|
---|
10379 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
10380 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
10381 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
10382 | }
|
---|
10383 | else
|
---|
10384 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
10385 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
10386 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
10387 |
|
---|
10388 | PVDISK pDisk = NULL;
|
---|
10389 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
10390 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
10391 |
|
---|
10392 | MediumCryptoFilterSettings CryptoSettingsRead;
|
---|
10393 | MediumCryptoFilterSettings CryptoSettingsWrite;
|
---|
10394 |
|
---|
10395 | void *pvBuf = NULL;
|
---|
10396 | const char *pszPasswordNew = NULL;
|
---|
10397 | try
|
---|
10398 | {
|
---|
10399 | /* Set up disk encryption filters. */
|
---|
10400 | if (task.mstrCurrentPassword.isEmpty())
|
---|
10401 | {
|
---|
10402 | /*
|
---|
10403 | * Query whether the medium property indicating that encryption is
|
---|
10404 | * configured is existing.
|
---|
10405 | */
|
---|
10406 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
10407 | if (it != pBase->m->mapProperties.end())
|
---|
10408 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
10409 | tr("The password given for the encrypted image is incorrect"));
|
---|
10410 | }
|
---|
10411 | else
|
---|
10412 | {
|
---|
10413 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
10414 | if (it == pBase->m->mapProperties.end())
|
---|
10415 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10416 | tr("The image is not configured for encryption"));
|
---|
10417 |
|
---|
10418 | i_taskEncryptSettingsSetup(&CryptoSettingsRead, NULL, it->second.c_str(), task.mstrCurrentPassword.c_str(),
|
---|
10419 | false /* fCreateKeyStore */);
|
---|
10420 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ, CryptoSettingsRead.vdFilterIfaces);
|
---|
10421 | if (vrc == VERR_VD_PASSWORD_INCORRECT)
|
---|
10422 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
10423 | tr("The password to decrypt the image is incorrect"));
|
---|
10424 | else if (RT_FAILURE(vrc))
|
---|
10425 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10426 | tr("Failed to load the decryption filter: %s"),
|
---|
10427 | i_vdError(vrc).c_str());
|
---|
10428 | }
|
---|
10429 |
|
---|
10430 | if (task.mstrCipher.isNotEmpty())
|
---|
10431 | {
|
---|
10432 | if ( task.mstrNewPassword.isEmpty()
|
---|
10433 | && task.mstrNewPasswordId.isEmpty()
|
---|
10434 | && task.mstrCurrentPassword.isNotEmpty())
|
---|
10435 | {
|
---|
10436 | /* An empty password and password ID will default to the current password. */
|
---|
10437 | pszPasswordNew = task.mstrCurrentPassword.c_str();
|
---|
10438 | }
|
---|
10439 | else if (task.mstrNewPassword.isEmpty())
|
---|
10440 | throw setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
10441 | tr("A password must be given for the image encryption"));
|
---|
10442 | else if (task.mstrNewPasswordId.isEmpty())
|
---|
10443 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10444 | tr("A valid identifier for the password must be given"));
|
---|
10445 | else
|
---|
10446 | pszPasswordNew = task.mstrNewPassword.c_str();
|
---|
10447 |
|
---|
10448 | i_taskEncryptSettingsSetup(&CryptoSettingsWrite, task.mstrCipher.c_str(), NULL,
|
---|
10449 | pszPasswordNew, true /* fCreateKeyStore */);
|
---|
10450 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_WRITE, CryptoSettingsWrite.vdFilterIfaces);
|
---|
10451 | if (RT_FAILURE(vrc))
|
---|
10452 | throw setErrorBoth(VBOX_E_INVALID_OBJECT_STATE, vrc,
|
---|
10453 | tr("Failed to load the encryption filter: %s"),
|
---|
10454 | i_vdError(vrc).c_str());
|
---|
10455 | }
|
---|
10456 | else if (task.mstrNewPasswordId.isNotEmpty() || task.mstrNewPassword.isNotEmpty())
|
---|
10457 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10458 | tr("The password and password identifier must be empty if the output should be unencrypted"));
|
---|
10459 |
|
---|
10460 | /* Open all media in the chain. */
|
---|
10461 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
10462 | task.mpMediumLockList->GetBegin();
|
---|
10463 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
10464 | task.mpMediumLockList->GetEnd();
|
---|
10465 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
10466 | mediumListEnd;
|
---|
10467 | --mediumListLast;
|
---|
10468 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
10469 | it != mediumListEnd;
|
---|
10470 | ++it)
|
---|
10471 | {
|
---|
10472 | const MediumLock &mediumLock = *it;
|
---|
10473 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
10474 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
10475 |
|
---|
10476 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
10477 |
|
---|
10478 | /* Open all media but last in read-only mode. Do not handle
|
---|
10479 | * shareable media, as compaction and sharing are mutually
|
---|
10480 | * exclusive. */
|
---|
10481 | vrc = VDOpen(pDisk,
|
---|
10482 | pMedium->m->strFormat.c_str(),
|
---|
10483 | pMedium->m->strLocationFull.c_str(),
|
---|
10484 | m->uOpenFlagsDef | (it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
10485 | pMedium->m->vdImageIfaces);
|
---|
10486 | if (RT_FAILURE(vrc))
|
---|
10487 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10488 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
10489 | pMedium->m->strLocationFull.c_str(),
|
---|
10490 | i_vdError(vrc).c_str());
|
---|
10491 | }
|
---|
10492 |
|
---|
10493 | Assert(m->state == MediumState_LockedWrite);
|
---|
10494 |
|
---|
10495 | Utf8Str location(m->strLocationFull);
|
---|
10496 |
|
---|
10497 | /* unlock before the potentially lengthy operation */
|
---|
10498 | thisLock.release();
|
---|
10499 |
|
---|
10500 | vrc = VDPrepareWithFilters(pDisk, task.mVDOperationIfaces);
|
---|
10501 | if (RT_FAILURE(vrc))
|
---|
10502 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10503 | tr("Could not prepare disk images for encryption (%Rrc): %s"),
|
---|
10504 | vrc, i_vdError(vrc).c_str());
|
---|
10505 |
|
---|
10506 | thisLock.acquire();
|
---|
10507 | /* If everything went well set the new key store. */
|
---|
10508 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
10509 | if (it != pBase->m->mapProperties.end())
|
---|
10510 | pBase->m->mapProperties.erase(it);
|
---|
10511 |
|
---|
10512 | /* Delete KeyId if encryption is removed or the password did change. */
|
---|
10513 | if ( task.mstrNewPasswordId.isNotEmpty()
|
---|
10514 | || task.mstrCipher.isEmpty())
|
---|
10515 | {
|
---|
10516 | it = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
10517 | if (it != pBase->m->mapProperties.end())
|
---|
10518 | pBase->m->mapProperties.erase(it);
|
---|
10519 | }
|
---|
10520 |
|
---|
10521 | if (CryptoSettingsWrite.pszKeyStore)
|
---|
10522 | {
|
---|
10523 | pBase->m->mapProperties["CRYPT/KeyStore"] = Utf8Str(CryptoSettingsWrite.pszKeyStore);
|
---|
10524 | if (task.mstrNewPasswordId.isNotEmpty())
|
---|
10525 | pBase->m->mapProperties["CRYPT/KeyId"] = task.mstrNewPasswordId;
|
---|
10526 | }
|
---|
10527 |
|
---|
10528 | if (CryptoSettingsRead.pszCipherReturned)
|
---|
10529 | RTStrFree(CryptoSettingsRead.pszCipherReturned);
|
---|
10530 |
|
---|
10531 | if (CryptoSettingsWrite.pszCipherReturned)
|
---|
10532 | RTStrFree(CryptoSettingsWrite.pszCipherReturned);
|
---|
10533 |
|
---|
10534 | thisLock.release();
|
---|
10535 | pBase->i_markRegistriesModified();
|
---|
10536 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
10537 | }
|
---|
10538 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10539 |
|
---|
10540 | if (pvBuf)
|
---|
10541 | RTMemFree(pvBuf);
|
---|
10542 |
|
---|
10543 | VDDestroy(pDisk);
|
---|
10544 | # else
|
---|
10545 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
10546 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
10547 | # endif
|
---|
10548 | }
|
---|
10549 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10550 |
|
---|
10551 | /* Everything is explicitly unlocked when the task exits,
|
---|
10552 | * as the task destruction also destroys the media chain. */
|
---|
10553 |
|
---|
10554 | return rc;
|
---|
10555 | }
|
---|
10556 |
|
---|
10557 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|