1 | /* $Id: MediumImpl.cpp 76240 2018-12-14 20:34:20Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox COM class implementation
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2017 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 | #include "MediumImpl.h"
|
---|
18 | #include "MediumIOImpl.h"
|
---|
19 | #include "TokenImpl.h"
|
---|
20 | #include "ProgressImpl.h"
|
---|
21 | #include "SystemPropertiesImpl.h"
|
---|
22 | #include "VirtualBoxImpl.h"
|
---|
23 | #include "ExtPackManagerImpl.h"
|
---|
24 |
|
---|
25 | #include "AutoCaller.h"
|
---|
26 | #include "Logging.h"
|
---|
27 | #include "ThreadTask.h"
|
---|
28 | #include "VBox/com/MultiResult.h"
|
---|
29 | #include "VBox/com/ErrorInfo.h"
|
---|
30 |
|
---|
31 | #include <VBox/err.h>
|
---|
32 | #include <VBox/settings.h>
|
---|
33 |
|
---|
34 | #include <iprt/param.h>
|
---|
35 | #include <iprt/path.h>
|
---|
36 | #include <iprt/file.h>
|
---|
37 | #include <iprt/tcp.h>
|
---|
38 | #include <iprt/cpp/utils.h>
|
---|
39 | #include <iprt/memsafer.h>
|
---|
40 | #include <iprt/base64.h>
|
---|
41 | #include <iprt/vfs.h>
|
---|
42 | #include <iprt/fsvfs.h>
|
---|
43 |
|
---|
44 | #include <VBox/vd.h>
|
---|
45 |
|
---|
46 | #include <algorithm>
|
---|
47 | #include <list>
|
---|
48 |
|
---|
49 |
|
---|
50 | typedef std::list<Guid> GuidList;
|
---|
51 |
|
---|
52 |
|
---|
53 | #ifdef VBOX_WITH_EXTPACK
|
---|
54 | static const char g_szVDPlugin[] = "VDPluginCrypt";
|
---|
55 | #endif
|
---|
56 |
|
---|
57 |
|
---|
58 | ////////////////////////////////////////////////////////////////////////////////
|
---|
59 | //
|
---|
60 | // Medium data definition
|
---|
61 | //
|
---|
62 | ////////////////////////////////////////////////////////////////////////////////
|
---|
63 |
|
---|
64 | /** Describes how a machine refers to this medium. */
|
---|
65 | struct BackRef
|
---|
66 | {
|
---|
67 | /** Equality predicate for stdc++. */
|
---|
68 | struct EqualsTo : public std::unary_function <BackRef, bool>
|
---|
69 | {
|
---|
70 | explicit EqualsTo(const Guid &aMachineId) : machineId(aMachineId) {}
|
---|
71 |
|
---|
72 | bool operator()(const argument_type &aThat) const
|
---|
73 | {
|
---|
74 | return aThat.machineId == machineId;
|
---|
75 | }
|
---|
76 |
|
---|
77 | const Guid machineId;
|
---|
78 | };
|
---|
79 |
|
---|
80 | BackRef(const Guid &aMachineId,
|
---|
81 | const Guid &aSnapshotId = Guid::Empty)
|
---|
82 | : machineId(aMachineId),
|
---|
83 | fInCurState(aSnapshotId.isZero())
|
---|
84 | {
|
---|
85 | if (aSnapshotId.isValid() && !aSnapshotId.isZero())
|
---|
86 | llSnapshotIds.push_back(aSnapshotId);
|
---|
87 | }
|
---|
88 |
|
---|
89 | Guid machineId;
|
---|
90 | bool fInCurState : 1;
|
---|
91 | GuidList llSnapshotIds;
|
---|
92 | };
|
---|
93 |
|
---|
94 | typedef std::list<BackRef> BackRefList;
|
---|
95 |
|
---|
96 | struct Medium::Data
|
---|
97 | {
|
---|
98 | Data()
|
---|
99 | : pVirtualBox(NULL),
|
---|
100 | state(MediumState_NotCreated),
|
---|
101 | variant(MediumVariant_Standard),
|
---|
102 | size(0),
|
---|
103 | readers(0),
|
---|
104 | preLockState(MediumState_NotCreated),
|
---|
105 | queryInfoSem(LOCKCLASS_MEDIUMQUERY),
|
---|
106 | queryInfoRunning(false),
|
---|
107 | type(MediumType_Normal),
|
---|
108 | devType(DeviceType_HardDisk),
|
---|
109 | logicalSize(0),
|
---|
110 | hddOpenMode(OpenReadWrite),
|
---|
111 | autoReset(false),
|
---|
112 | hostDrive(false),
|
---|
113 | implicit(false),
|
---|
114 | fClosing(false),
|
---|
115 | uOpenFlagsDef(VD_OPEN_FLAGS_IGNORE_FLUSH),
|
---|
116 | numCreateDiffTasks(0),
|
---|
117 | vdDiskIfaces(NULL),
|
---|
118 | vdImageIfaces(NULL),
|
---|
119 | fMoveThisMedium(false)
|
---|
120 | { }
|
---|
121 |
|
---|
122 | /** weak VirtualBox parent */
|
---|
123 | VirtualBox * const pVirtualBox;
|
---|
124 |
|
---|
125 | // pParent and llChildren are protected by VirtualBox::i_getMediaTreeLockHandle()
|
---|
126 | ComObjPtr<Medium> pParent;
|
---|
127 | MediaList llChildren; // to add a child, just call push_back; to remove
|
---|
128 | // a child, call child->deparent() which does a lookup
|
---|
129 |
|
---|
130 | GuidList llRegistryIDs; // media registries in which this medium is listed
|
---|
131 |
|
---|
132 | const Guid id;
|
---|
133 | Utf8Str strDescription;
|
---|
134 | MediumState_T state;
|
---|
135 | MediumVariant_T variant;
|
---|
136 | Utf8Str strLocationFull;
|
---|
137 | uint64_t size;
|
---|
138 | Utf8Str strLastAccessError;
|
---|
139 |
|
---|
140 | BackRefList backRefs;
|
---|
141 |
|
---|
142 | size_t readers;
|
---|
143 | MediumState_T preLockState;
|
---|
144 |
|
---|
145 | /** Special synchronization for operations which must wait for
|
---|
146 | * Medium::i_queryInfo in another thread to complete. Using a SemRW is
|
---|
147 | * not quite ideal, but at least it is subject to the lock validator,
|
---|
148 | * unlike the SemEventMulti which we had here for many years. Catching
|
---|
149 | * possible deadlocks is more important than a tiny bit of efficiency. */
|
---|
150 | RWLockHandle queryInfoSem;
|
---|
151 | bool queryInfoRunning : 1;
|
---|
152 |
|
---|
153 | const Utf8Str strFormat;
|
---|
154 | ComObjPtr<MediumFormat> formatObj;
|
---|
155 |
|
---|
156 | MediumType_T type;
|
---|
157 | DeviceType_T devType;
|
---|
158 | uint64_t logicalSize;
|
---|
159 |
|
---|
160 | HDDOpenMode hddOpenMode;
|
---|
161 |
|
---|
162 | bool autoReset : 1;
|
---|
163 |
|
---|
164 | /** New UUID to be set on the next Medium::i_queryInfo call. */
|
---|
165 | const Guid uuidImage;
|
---|
166 | /** New parent UUID to be set on the next Medium::i_queryInfo call. */
|
---|
167 | const Guid uuidParentImage;
|
---|
168 |
|
---|
169 | bool hostDrive : 1;
|
---|
170 |
|
---|
171 | settings::StringsMap mapProperties;
|
---|
172 |
|
---|
173 | bool implicit : 1;
|
---|
174 | /** Flag whether the medium is in the process of being closed. */
|
---|
175 | bool fClosing: 1;
|
---|
176 |
|
---|
177 | /** Default flags passed to VDOpen(). */
|
---|
178 | unsigned uOpenFlagsDef;
|
---|
179 |
|
---|
180 | uint32_t numCreateDiffTasks;
|
---|
181 |
|
---|
182 | Utf8Str vdError; /*< Error remembered by the VD error callback. */
|
---|
183 |
|
---|
184 | VDINTERFACEERROR vdIfError;
|
---|
185 |
|
---|
186 | VDINTERFACECONFIG vdIfConfig;
|
---|
187 |
|
---|
188 | VDINTERFACETCPNET vdIfTcpNet;
|
---|
189 |
|
---|
190 | PVDINTERFACE vdDiskIfaces;
|
---|
191 | PVDINTERFACE vdImageIfaces;
|
---|
192 |
|
---|
193 | /** Flag if the medium is going to move to a new
|
---|
194 | * location. */
|
---|
195 | bool fMoveThisMedium;
|
---|
196 | /** new location path */
|
---|
197 | Utf8Str strNewLocationFull;
|
---|
198 | };
|
---|
199 |
|
---|
200 | typedef struct VDSOCKETINT
|
---|
201 | {
|
---|
202 | /** Socket handle. */
|
---|
203 | RTSOCKET hSocket;
|
---|
204 | } VDSOCKETINT, *PVDSOCKETINT;
|
---|
205 |
|
---|
206 | ////////////////////////////////////////////////////////////////////////////////
|
---|
207 | //
|
---|
208 | // Globals
|
---|
209 | //
|
---|
210 | ////////////////////////////////////////////////////////////////////////////////
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Medium::Task class for asynchronous operations.
|
---|
214 | *
|
---|
215 | * @note Instances of this class must be created using new() because the
|
---|
216 | * task thread function will delete them when the task is complete.
|
---|
217 | *
|
---|
218 | * @note The constructor of this class adds a caller on the managed Medium
|
---|
219 | * object which is automatically released upon destruction.
|
---|
220 | */
|
---|
221 | class Medium::Task : public ThreadTask
|
---|
222 | {
|
---|
223 | public:
|
---|
224 | Task(Medium *aMedium, Progress *aProgress)
|
---|
225 | : ThreadTask("Medium::Task"),
|
---|
226 | mVDOperationIfaces(NULL),
|
---|
227 | mMedium(aMedium),
|
---|
228 | mMediumCaller(aMedium),
|
---|
229 | mProgress(aProgress),
|
---|
230 | mVirtualBoxCaller(NULL)
|
---|
231 | {
|
---|
232 | AssertReturnVoidStmt(aMedium, mRC = E_FAIL);
|
---|
233 | mRC = mMediumCaller.rc();
|
---|
234 | if (FAILED(mRC))
|
---|
235 | return;
|
---|
236 |
|
---|
237 | /* Get strong VirtualBox reference, see below. */
|
---|
238 | VirtualBox *pVirtualBox = aMedium->m->pVirtualBox;
|
---|
239 | mVirtualBox = pVirtualBox;
|
---|
240 | mVirtualBoxCaller.attach(pVirtualBox);
|
---|
241 | mRC = mVirtualBoxCaller.rc();
|
---|
242 | if (FAILED(mRC))
|
---|
243 | return;
|
---|
244 |
|
---|
245 | /* Set up a per-operation progress interface, can be used freely (for
|
---|
246 | * binary operations you can use it either on the source or target). */
|
---|
247 | if (mProgress)
|
---|
248 | {
|
---|
249 | mVDIfProgress.pfnProgress = aProgress->i_vdProgressCallback;
|
---|
250 | int vrc = VDInterfaceAdd(&mVDIfProgress.Core,
|
---|
251 | "Medium::Task::vdInterfaceProgress",
|
---|
252 | VDINTERFACETYPE_PROGRESS,
|
---|
253 | mProgress,
|
---|
254 | sizeof(mVDIfProgress),
|
---|
255 | &mVDOperationIfaces);
|
---|
256 | AssertRC(vrc);
|
---|
257 | if (RT_FAILURE(vrc))
|
---|
258 | mRC = E_FAIL;
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | // Make all destructors virtual. Just in case.
|
---|
263 | virtual ~Task()
|
---|
264 | {
|
---|
265 | /* send the notification of completion.*/
|
---|
266 | if ( isAsync()
|
---|
267 | && !mProgress.isNull())
|
---|
268 | mProgress->i_notifyComplete(mRC);
|
---|
269 | }
|
---|
270 |
|
---|
271 | HRESULT rc() const { return mRC; }
|
---|
272 | bool isOk() const { return SUCCEEDED(rc()); }
|
---|
273 |
|
---|
274 | const ComPtr<Progress>& GetProgressObject() const {return mProgress;}
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * Runs Medium::Task::executeTask() on the current thread
|
---|
278 | * instead of creating a new one.
|
---|
279 | */
|
---|
280 | HRESULT runNow()
|
---|
281 | {
|
---|
282 | LogFlowFuncEnter();
|
---|
283 |
|
---|
284 | mRC = executeTask();
|
---|
285 |
|
---|
286 | LogFlowFunc(("rc=%Rhrc\n", mRC));
|
---|
287 | LogFlowFuncLeave();
|
---|
288 | return mRC;
|
---|
289 | }
|
---|
290 |
|
---|
291 | /**
|
---|
292 | * Implementation code for the "create base" task.
|
---|
293 | * Used as function for execution from a standalone thread.
|
---|
294 | */
|
---|
295 | void handler()
|
---|
296 | {
|
---|
297 | LogFlowFuncEnter();
|
---|
298 | try
|
---|
299 | {
|
---|
300 | mRC = executeTask(); /* (destructor picks up mRC, see above) */
|
---|
301 | LogFlowFunc(("rc=%Rhrc\n", mRC));
|
---|
302 | }
|
---|
303 | catch (...)
|
---|
304 | {
|
---|
305 | LogRel(("Some exception in the function Medium::Task:handler()\n"));
|
---|
306 | }
|
---|
307 |
|
---|
308 | LogFlowFuncLeave();
|
---|
309 | }
|
---|
310 |
|
---|
311 | PVDINTERFACE mVDOperationIfaces;
|
---|
312 |
|
---|
313 | const ComObjPtr<Medium> mMedium;
|
---|
314 | AutoCaller mMediumCaller;
|
---|
315 |
|
---|
316 | protected:
|
---|
317 | HRESULT mRC;
|
---|
318 |
|
---|
319 | private:
|
---|
320 | virtual HRESULT executeTask() = 0;
|
---|
321 |
|
---|
322 | const ComObjPtr<Progress> mProgress;
|
---|
323 |
|
---|
324 | VDINTERFACEPROGRESS mVDIfProgress;
|
---|
325 |
|
---|
326 | /* Must have a strong VirtualBox reference during a task otherwise the
|
---|
327 | * reference count might drop to 0 while a task is still running. This
|
---|
328 | * would result in weird behavior, including deadlocks due to uninit and
|
---|
329 | * locking order issues. The deadlock often is not detectable because the
|
---|
330 | * uninit uses event semaphores which sabotages deadlock detection. */
|
---|
331 | ComObjPtr<VirtualBox> mVirtualBox;
|
---|
332 | AutoCaller mVirtualBoxCaller;
|
---|
333 | };
|
---|
334 |
|
---|
335 | HRESULT Medium::Task::executeTask()
|
---|
336 | {
|
---|
337 | return E_NOTIMPL;//ReturnComNotImplemented()
|
---|
338 | }
|
---|
339 |
|
---|
340 | class Medium::CreateBaseTask : public Medium::Task
|
---|
341 | {
|
---|
342 | public:
|
---|
343 | CreateBaseTask(Medium *aMedium,
|
---|
344 | Progress *aProgress,
|
---|
345 | uint64_t aSize,
|
---|
346 | MediumVariant_T aVariant)
|
---|
347 | : Medium::Task(aMedium, aProgress),
|
---|
348 | mSize(aSize),
|
---|
349 | mVariant(aVariant)
|
---|
350 | {
|
---|
351 | m_strTaskName = "createBase";
|
---|
352 | }
|
---|
353 |
|
---|
354 | uint64_t mSize;
|
---|
355 | MediumVariant_T mVariant;
|
---|
356 |
|
---|
357 | private:
|
---|
358 | HRESULT executeTask();
|
---|
359 | };
|
---|
360 |
|
---|
361 | class Medium::CreateDiffTask : public Medium::Task
|
---|
362 | {
|
---|
363 | public:
|
---|
364 | CreateDiffTask(Medium *aMedium,
|
---|
365 | Progress *aProgress,
|
---|
366 | Medium *aTarget,
|
---|
367 | MediumVariant_T aVariant,
|
---|
368 | MediumLockList *aMediumLockList,
|
---|
369 | bool fKeepMediumLockList = false)
|
---|
370 | : Medium::Task(aMedium, aProgress),
|
---|
371 | mpMediumLockList(aMediumLockList),
|
---|
372 | mTarget(aTarget),
|
---|
373 | mVariant(aVariant),
|
---|
374 | mTargetCaller(aTarget),
|
---|
375 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
376 | {
|
---|
377 | AssertReturnVoidStmt(aTarget != NULL, mRC = E_FAIL);
|
---|
378 | mRC = mTargetCaller.rc();
|
---|
379 | if (FAILED(mRC))
|
---|
380 | return;
|
---|
381 | m_strTaskName = "createDiff";
|
---|
382 | }
|
---|
383 |
|
---|
384 | ~CreateDiffTask()
|
---|
385 | {
|
---|
386 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
387 | delete mpMediumLockList;
|
---|
388 | }
|
---|
389 |
|
---|
390 | MediumLockList *mpMediumLockList;
|
---|
391 |
|
---|
392 | const ComObjPtr<Medium> mTarget;
|
---|
393 | MediumVariant_T mVariant;
|
---|
394 |
|
---|
395 | private:
|
---|
396 | HRESULT executeTask();
|
---|
397 | AutoCaller mTargetCaller;
|
---|
398 | bool mfKeepMediumLockList;
|
---|
399 | };
|
---|
400 |
|
---|
401 | class Medium::CloneTask : public Medium::Task
|
---|
402 | {
|
---|
403 | public:
|
---|
404 | CloneTask(Medium *aMedium,
|
---|
405 | Progress *aProgress,
|
---|
406 | Medium *aTarget,
|
---|
407 | MediumVariant_T aVariant,
|
---|
408 | Medium *aParent,
|
---|
409 | uint32_t idxSrcImageSame,
|
---|
410 | uint32_t idxDstImageSame,
|
---|
411 | MediumLockList *aSourceMediumLockList,
|
---|
412 | MediumLockList *aTargetMediumLockList,
|
---|
413 | bool fKeepSourceMediumLockList = false,
|
---|
414 | bool fKeepTargetMediumLockList = false)
|
---|
415 | : Medium::Task(aMedium, aProgress),
|
---|
416 | mTarget(aTarget),
|
---|
417 | mParent(aParent),
|
---|
418 | mpSourceMediumLockList(aSourceMediumLockList),
|
---|
419 | mpTargetMediumLockList(aTargetMediumLockList),
|
---|
420 | mVariant(aVariant),
|
---|
421 | midxSrcImageSame(idxSrcImageSame),
|
---|
422 | midxDstImageSame(idxDstImageSame),
|
---|
423 | mTargetCaller(aTarget),
|
---|
424 | mParentCaller(aParent),
|
---|
425 | mfKeepSourceMediumLockList(fKeepSourceMediumLockList),
|
---|
426 | mfKeepTargetMediumLockList(fKeepTargetMediumLockList)
|
---|
427 | {
|
---|
428 | AssertReturnVoidStmt(aTarget != NULL, mRC = E_FAIL);
|
---|
429 | mRC = mTargetCaller.rc();
|
---|
430 | if (FAILED(mRC))
|
---|
431 | return;
|
---|
432 | /* aParent may be NULL */
|
---|
433 | mRC = mParentCaller.rc();
|
---|
434 | if (FAILED(mRC))
|
---|
435 | return;
|
---|
436 | AssertReturnVoidStmt(aSourceMediumLockList != NULL, mRC = E_FAIL);
|
---|
437 | AssertReturnVoidStmt(aTargetMediumLockList != NULL, mRC = E_FAIL);
|
---|
438 | m_strTaskName = "createClone";
|
---|
439 | }
|
---|
440 |
|
---|
441 | ~CloneTask()
|
---|
442 | {
|
---|
443 | if (!mfKeepSourceMediumLockList && mpSourceMediumLockList)
|
---|
444 | delete mpSourceMediumLockList;
|
---|
445 | if (!mfKeepTargetMediumLockList && mpTargetMediumLockList)
|
---|
446 | delete mpTargetMediumLockList;
|
---|
447 | }
|
---|
448 |
|
---|
449 | const ComObjPtr<Medium> mTarget;
|
---|
450 | const ComObjPtr<Medium> mParent;
|
---|
451 | MediumLockList *mpSourceMediumLockList;
|
---|
452 | MediumLockList *mpTargetMediumLockList;
|
---|
453 | MediumVariant_T mVariant;
|
---|
454 | uint32_t midxSrcImageSame;
|
---|
455 | uint32_t midxDstImageSame;
|
---|
456 |
|
---|
457 | private:
|
---|
458 | HRESULT executeTask();
|
---|
459 | AutoCaller mTargetCaller;
|
---|
460 | AutoCaller mParentCaller;
|
---|
461 | bool mfKeepSourceMediumLockList;
|
---|
462 | bool mfKeepTargetMediumLockList;
|
---|
463 | };
|
---|
464 |
|
---|
465 | class Medium::MoveTask : public Medium::Task
|
---|
466 | {
|
---|
467 | public:
|
---|
468 | MoveTask(Medium *aMedium,
|
---|
469 | Progress *aProgress,
|
---|
470 | MediumVariant_T aVariant,
|
---|
471 | MediumLockList *aMediumLockList,
|
---|
472 | bool fKeepMediumLockList = false)
|
---|
473 | : Medium::Task(aMedium, aProgress),
|
---|
474 | mpMediumLockList(aMediumLockList),
|
---|
475 | mVariant(aVariant),
|
---|
476 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
477 | {
|
---|
478 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
479 | m_strTaskName = "createMove";
|
---|
480 | }
|
---|
481 |
|
---|
482 | ~MoveTask()
|
---|
483 | {
|
---|
484 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
485 | delete mpMediumLockList;
|
---|
486 | }
|
---|
487 |
|
---|
488 | MediumLockList *mpMediumLockList;
|
---|
489 | MediumVariant_T mVariant;
|
---|
490 |
|
---|
491 | private:
|
---|
492 | HRESULT executeTask();
|
---|
493 | bool mfKeepMediumLockList;
|
---|
494 | };
|
---|
495 |
|
---|
496 | class Medium::CompactTask : public Medium::Task
|
---|
497 | {
|
---|
498 | public:
|
---|
499 | CompactTask(Medium *aMedium,
|
---|
500 | Progress *aProgress,
|
---|
501 | MediumLockList *aMediumLockList,
|
---|
502 | bool fKeepMediumLockList = false)
|
---|
503 | : Medium::Task(aMedium, aProgress),
|
---|
504 | mpMediumLockList(aMediumLockList),
|
---|
505 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
506 | {
|
---|
507 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
508 | m_strTaskName = "createCompact";
|
---|
509 | }
|
---|
510 |
|
---|
511 | ~CompactTask()
|
---|
512 | {
|
---|
513 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
514 | delete mpMediumLockList;
|
---|
515 | }
|
---|
516 |
|
---|
517 | MediumLockList *mpMediumLockList;
|
---|
518 |
|
---|
519 | private:
|
---|
520 | HRESULT executeTask();
|
---|
521 | bool mfKeepMediumLockList;
|
---|
522 | };
|
---|
523 |
|
---|
524 | class Medium::ResizeTask : public Medium::Task
|
---|
525 | {
|
---|
526 | public:
|
---|
527 | ResizeTask(Medium *aMedium,
|
---|
528 | uint64_t aSize,
|
---|
529 | Progress *aProgress,
|
---|
530 | MediumLockList *aMediumLockList,
|
---|
531 | bool fKeepMediumLockList = false)
|
---|
532 | : Medium::Task(aMedium, aProgress),
|
---|
533 | mSize(aSize),
|
---|
534 | mpMediumLockList(aMediumLockList),
|
---|
535 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
536 | {
|
---|
537 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
538 | m_strTaskName = "createResize";
|
---|
539 | }
|
---|
540 |
|
---|
541 | ~ResizeTask()
|
---|
542 | {
|
---|
543 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
544 | delete mpMediumLockList;
|
---|
545 | }
|
---|
546 |
|
---|
547 | uint64_t mSize;
|
---|
548 | MediumLockList *mpMediumLockList;
|
---|
549 |
|
---|
550 | private:
|
---|
551 | HRESULT executeTask();
|
---|
552 | bool mfKeepMediumLockList;
|
---|
553 | };
|
---|
554 |
|
---|
555 | class Medium::ResetTask : public Medium::Task
|
---|
556 | {
|
---|
557 | public:
|
---|
558 | ResetTask(Medium *aMedium,
|
---|
559 | Progress *aProgress,
|
---|
560 | MediumLockList *aMediumLockList,
|
---|
561 | bool fKeepMediumLockList = false)
|
---|
562 | : Medium::Task(aMedium, aProgress),
|
---|
563 | mpMediumLockList(aMediumLockList),
|
---|
564 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
565 | {
|
---|
566 | m_strTaskName = "createReset";
|
---|
567 | }
|
---|
568 |
|
---|
569 | ~ResetTask()
|
---|
570 | {
|
---|
571 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
572 | delete mpMediumLockList;
|
---|
573 | }
|
---|
574 |
|
---|
575 | MediumLockList *mpMediumLockList;
|
---|
576 |
|
---|
577 | private:
|
---|
578 | HRESULT executeTask();
|
---|
579 | bool mfKeepMediumLockList;
|
---|
580 | };
|
---|
581 |
|
---|
582 | class Medium::DeleteTask : public Medium::Task
|
---|
583 | {
|
---|
584 | public:
|
---|
585 | DeleteTask(Medium *aMedium,
|
---|
586 | Progress *aProgress,
|
---|
587 | MediumLockList *aMediumLockList,
|
---|
588 | bool fKeepMediumLockList = false)
|
---|
589 | : Medium::Task(aMedium, aProgress),
|
---|
590 | mpMediumLockList(aMediumLockList),
|
---|
591 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
592 | {
|
---|
593 | m_strTaskName = "createDelete";
|
---|
594 | }
|
---|
595 |
|
---|
596 | ~DeleteTask()
|
---|
597 | {
|
---|
598 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
599 | delete mpMediumLockList;
|
---|
600 | }
|
---|
601 |
|
---|
602 | MediumLockList *mpMediumLockList;
|
---|
603 |
|
---|
604 | private:
|
---|
605 | HRESULT executeTask();
|
---|
606 | bool mfKeepMediumLockList;
|
---|
607 | };
|
---|
608 |
|
---|
609 | class Medium::MergeTask : public Medium::Task
|
---|
610 | {
|
---|
611 | public:
|
---|
612 | MergeTask(Medium *aMedium,
|
---|
613 | Medium *aTarget,
|
---|
614 | bool fMergeForward,
|
---|
615 | Medium *aParentForTarget,
|
---|
616 | MediumLockList *aChildrenToReparent,
|
---|
617 | Progress *aProgress,
|
---|
618 | MediumLockList *aMediumLockList,
|
---|
619 | bool fKeepMediumLockList = false)
|
---|
620 | : Medium::Task(aMedium, aProgress),
|
---|
621 | mTarget(aTarget),
|
---|
622 | mfMergeForward(fMergeForward),
|
---|
623 | mParentForTarget(aParentForTarget),
|
---|
624 | mpChildrenToReparent(aChildrenToReparent),
|
---|
625 | mpMediumLockList(aMediumLockList),
|
---|
626 | mTargetCaller(aTarget),
|
---|
627 | mParentForTargetCaller(aParentForTarget),
|
---|
628 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
629 | {
|
---|
630 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
631 | m_strTaskName = "createMerge";
|
---|
632 | }
|
---|
633 |
|
---|
634 | ~MergeTask()
|
---|
635 | {
|
---|
636 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
637 | delete mpMediumLockList;
|
---|
638 | if (mpChildrenToReparent)
|
---|
639 | delete mpChildrenToReparent;
|
---|
640 | }
|
---|
641 |
|
---|
642 | const ComObjPtr<Medium> mTarget;
|
---|
643 | bool mfMergeForward;
|
---|
644 | /* When mpChildrenToReparent is null then mParentForTarget is non-null and
|
---|
645 | * vice versa. In other words: they are used in different cases. */
|
---|
646 | const ComObjPtr<Medium> mParentForTarget;
|
---|
647 | MediumLockList *mpChildrenToReparent;
|
---|
648 | MediumLockList *mpMediumLockList;
|
---|
649 |
|
---|
650 | private:
|
---|
651 | HRESULT executeTask();
|
---|
652 | AutoCaller mTargetCaller;
|
---|
653 | AutoCaller mParentForTargetCaller;
|
---|
654 | bool mfKeepMediumLockList;
|
---|
655 | };
|
---|
656 |
|
---|
657 | class Medium::ImportTask : public Medium::Task
|
---|
658 | {
|
---|
659 | public:
|
---|
660 | ImportTask(Medium *aMedium,
|
---|
661 | Progress *aProgress,
|
---|
662 | const char *aFilename,
|
---|
663 | MediumFormat *aFormat,
|
---|
664 | MediumVariant_T aVariant,
|
---|
665 | RTVFSIOSTREAM aVfsIosSrc,
|
---|
666 | Medium *aParent,
|
---|
667 | MediumLockList *aTargetMediumLockList,
|
---|
668 | bool fKeepTargetMediumLockList = false)
|
---|
669 | : Medium::Task(aMedium, aProgress),
|
---|
670 | mFilename(aFilename),
|
---|
671 | mFormat(aFormat),
|
---|
672 | mVariant(aVariant),
|
---|
673 | mParent(aParent),
|
---|
674 | mpTargetMediumLockList(aTargetMediumLockList),
|
---|
675 | mpVfsIoIf(NULL),
|
---|
676 | mParentCaller(aParent),
|
---|
677 | mfKeepTargetMediumLockList(fKeepTargetMediumLockList)
|
---|
678 | {
|
---|
679 | AssertReturnVoidStmt(aTargetMediumLockList != NULL, mRC = E_FAIL);
|
---|
680 | /* aParent may be NULL */
|
---|
681 | mRC = mParentCaller.rc();
|
---|
682 | if (FAILED(mRC))
|
---|
683 | return;
|
---|
684 |
|
---|
685 | mVDImageIfaces = aMedium->m->vdImageIfaces;
|
---|
686 |
|
---|
687 | int vrc = VDIfCreateFromVfsStream(aVfsIosSrc, RTFILE_O_READ, &mpVfsIoIf);
|
---|
688 | AssertRCReturnVoidStmt(vrc, mRC = E_FAIL);
|
---|
689 |
|
---|
690 | vrc = VDInterfaceAdd(&mpVfsIoIf->Core, "Medium::ImportTaskVfsIos",
|
---|
691 | VDINTERFACETYPE_IO, mpVfsIoIf,
|
---|
692 | sizeof(VDINTERFACEIO), &mVDImageIfaces);
|
---|
693 | AssertRCReturnVoidStmt(vrc, mRC = E_FAIL);
|
---|
694 | m_strTaskName = "createImport";
|
---|
695 | }
|
---|
696 |
|
---|
697 | ~ImportTask()
|
---|
698 | {
|
---|
699 | if (!mfKeepTargetMediumLockList && mpTargetMediumLockList)
|
---|
700 | delete mpTargetMediumLockList;
|
---|
701 | if (mpVfsIoIf)
|
---|
702 | {
|
---|
703 | VDIfDestroyFromVfsStream(mpVfsIoIf);
|
---|
704 | mpVfsIoIf = NULL;
|
---|
705 | }
|
---|
706 | }
|
---|
707 |
|
---|
708 | Utf8Str mFilename;
|
---|
709 | ComObjPtr<MediumFormat> mFormat;
|
---|
710 | MediumVariant_T mVariant;
|
---|
711 | const ComObjPtr<Medium> mParent;
|
---|
712 | MediumLockList *mpTargetMediumLockList;
|
---|
713 | PVDINTERFACE mVDImageIfaces;
|
---|
714 | PVDINTERFACEIO mpVfsIoIf; /**< Pointer to the VFS I/O stream to VD I/O interface wrapper. */
|
---|
715 |
|
---|
716 | private:
|
---|
717 | HRESULT executeTask();
|
---|
718 | AutoCaller mParentCaller;
|
---|
719 | bool mfKeepTargetMediumLockList;
|
---|
720 | };
|
---|
721 |
|
---|
722 | class Medium::EncryptTask : public Medium::Task
|
---|
723 | {
|
---|
724 | public:
|
---|
725 | EncryptTask(Medium *aMedium,
|
---|
726 | const com::Utf8Str &strNewPassword,
|
---|
727 | const com::Utf8Str &strCurrentPassword,
|
---|
728 | const com::Utf8Str &strCipher,
|
---|
729 | const com::Utf8Str &strNewPasswordId,
|
---|
730 | Progress *aProgress,
|
---|
731 | MediumLockList *aMediumLockList)
|
---|
732 | : Medium::Task(aMedium, aProgress),
|
---|
733 | mstrNewPassword(strNewPassword),
|
---|
734 | mstrCurrentPassword(strCurrentPassword),
|
---|
735 | mstrCipher(strCipher),
|
---|
736 | mstrNewPasswordId(strNewPasswordId),
|
---|
737 | mpMediumLockList(aMediumLockList)
|
---|
738 | {
|
---|
739 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
740 | /* aParent may be NULL */
|
---|
741 | mRC = mParentCaller.rc();
|
---|
742 | if (FAILED(mRC))
|
---|
743 | return;
|
---|
744 |
|
---|
745 | mVDImageIfaces = aMedium->m->vdImageIfaces;
|
---|
746 | m_strTaskName = "createEncrypt";
|
---|
747 | }
|
---|
748 |
|
---|
749 | ~EncryptTask()
|
---|
750 | {
|
---|
751 | if (mstrNewPassword.length())
|
---|
752 | RTMemWipeThoroughly(mstrNewPassword.mutableRaw(), mstrNewPassword.length(), 10 /* cPasses */);
|
---|
753 | if (mstrCurrentPassword.length())
|
---|
754 | RTMemWipeThoroughly(mstrCurrentPassword.mutableRaw(), mstrCurrentPassword.length(), 10 /* cPasses */);
|
---|
755 |
|
---|
756 | /* Keep any errors which might be set when deleting the lock list. */
|
---|
757 | ErrorInfoKeeper eik;
|
---|
758 | delete mpMediumLockList;
|
---|
759 | }
|
---|
760 |
|
---|
761 | Utf8Str mstrNewPassword;
|
---|
762 | Utf8Str mstrCurrentPassword;
|
---|
763 | Utf8Str mstrCipher;
|
---|
764 | Utf8Str mstrNewPasswordId;
|
---|
765 | MediumLockList *mpMediumLockList;
|
---|
766 | PVDINTERFACE mVDImageIfaces;
|
---|
767 |
|
---|
768 | private:
|
---|
769 | HRESULT executeTask();
|
---|
770 | AutoCaller mParentCaller;
|
---|
771 | };
|
---|
772 |
|
---|
773 | /**
|
---|
774 | * Implementation code for the "create base" task.
|
---|
775 | */
|
---|
776 | HRESULT Medium::CreateBaseTask::executeTask()
|
---|
777 | {
|
---|
778 | return mMedium->i_taskCreateBaseHandler(*this);
|
---|
779 | }
|
---|
780 |
|
---|
781 | /**
|
---|
782 | * Implementation code for the "create diff" task.
|
---|
783 | */
|
---|
784 | HRESULT Medium::CreateDiffTask::executeTask()
|
---|
785 | {
|
---|
786 | return mMedium->i_taskCreateDiffHandler(*this);
|
---|
787 | }
|
---|
788 |
|
---|
789 | /**
|
---|
790 | * Implementation code for the "clone" task.
|
---|
791 | */
|
---|
792 | HRESULT Medium::CloneTask::executeTask()
|
---|
793 | {
|
---|
794 | return mMedium->i_taskCloneHandler(*this);
|
---|
795 | }
|
---|
796 |
|
---|
797 | /**
|
---|
798 | * Implementation code for the "move" task.
|
---|
799 | */
|
---|
800 | HRESULT Medium::MoveTask::executeTask()
|
---|
801 | {
|
---|
802 | return mMedium->i_taskMoveHandler(*this);
|
---|
803 | }
|
---|
804 |
|
---|
805 | /**
|
---|
806 | * Implementation code for the "compact" task.
|
---|
807 | */
|
---|
808 | HRESULT Medium::CompactTask::executeTask()
|
---|
809 | {
|
---|
810 | return mMedium->i_taskCompactHandler(*this);
|
---|
811 | }
|
---|
812 |
|
---|
813 | /**
|
---|
814 | * Implementation code for the "resize" task.
|
---|
815 | */
|
---|
816 | HRESULT Medium::ResizeTask::executeTask()
|
---|
817 | {
|
---|
818 | return mMedium->i_taskResizeHandler(*this);
|
---|
819 | }
|
---|
820 |
|
---|
821 |
|
---|
822 | /**
|
---|
823 | * Implementation code for the "reset" task.
|
---|
824 | */
|
---|
825 | HRESULT Medium::ResetTask::executeTask()
|
---|
826 | {
|
---|
827 | return mMedium->i_taskResetHandler(*this);
|
---|
828 | }
|
---|
829 |
|
---|
830 | /**
|
---|
831 | * Implementation code for the "delete" task.
|
---|
832 | */
|
---|
833 | HRESULT Medium::DeleteTask::executeTask()
|
---|
834 | {
|
---|
835 | return mMedium->i_taskDeleteHandler(*this);
|
---|
836 | }
|
---|
837 |
|
---|
838 | /**
|
---|
839 | * Implementation code for the "merge" task.
|
---|
840 | */
|
---|
841 | HRESULT Medium::MergeTask::executeTask()
|
---|
842 | {
|
---|
843 | return mMedium->i_taskMergeHandler(*this);
|
---|
844 | }
|
---|
845 |
|
---|
846 | /**
|
---|
847 | * Implementation code for the "import" task.
|
---|
848 | */
|
---|
849 | HRESULT Medium::ImportTask::executeTask()
|
---|
850 | {
|
---|
851 | return mMedium->i_taskImportHandler(*this);
|
---|
852 | }
|
---|
853 |
|
---|
854 | /**
|
---|
855 | * Implementation code for the "encrypt" task.
|
---|
856 | */
|
---|
857 | HRESULT Medium::EncryptTask::executeTask()
|
---|
858 | {
|
---|
859 | return mMedium->i_taskEncryptHandler(*this);
|
---|
860 | }
|
---|
861 |
|
---|
862 | ////////////////////////////////////////////////////////////////////////////////
|
---|
863 | //
|
---|
864 | // Medium constructor / destructor
|
---|
865 | //
|
---|
866 | ////////////////////////////////////////////////////////////////////////////////
|
---|
867 |
|
---|
868 | DEFINE_EMPTY_CTOR_DTOR(Medium)
|
---|
869 |
|
---|
870 | HRESULT Medium::FinalConstruct()
|
---|
871 | {
|
---|
872 | m = new Data;
|
---|
873 |
|
---|
874 | /* Initialize the callbacks of the VD error interface */
|
---|
875 | m->vdIfError.pfnError = i_vdErrorCall;
|
---|
876 | m->vdIfError.pfnMessage = NULL;
|
---|
877 |
|
---|
878 | /* Initialize the callbacks of the VD config interface */
|
---|
879 | m->vdIfConfig.pfnAreKeysValid = i_vdConfigAreKeysValid;
|
---|
880 | m->vdIfConfig.pfnQuerySize = i_vdConfigQuerySize;
|
---|
881 | m->vdIfConfig.pfnQuery = i_vdConfigQuery;
|
---|
882 | m->vdIfConfig.pfnQueryBytes = NULL;
|
---|
883 |
|
---|
884 | /* Initialize the callbacks of the VD TCP interface (we always use the host
|
---|
885 | * IP stack for now) */
|
---|
886 | m->vdIfTcpNet.pfnSocketCreate = i_vdTcpSocketCreate;
|
---|
887 | m->vdIfTcpNet.pfnSocketDestroy = i_vdTcpSocketDestroy;
|
---|
888 | m->vdIfTcpNet.pfnClientConnect = i_vdTcpClientConnect;
|
---|
889 | m->vdIfTcpNet.pfnClientClose = i_vdTcpClientClose;
|
---|
890 | m->vdIfTcpNet.pfnIsClientConnected = i_vdTcpIsClientConnected;
|
---|
891 | m->vdIfTcpNet.pfnSelectOne = i_vdTcpSelectOne;
|
---|
892 | m->vdIfTcpNet.pfnRead = i_vdTcpRead;
|
---|
893 | m->vdIfTcpNet.pfnWrite = i_vdTcpWrite;
|
---|
894 | m->vdIfTcpNet.pfnSgWrite = i_vdTcpSgWrite;
|
---|
895 | m->vdIfTcpNet.pfnFlush = i_vdTcpFlush;
|
---|
896 | m->vdIfTcpNet.pfnSetSendCoalescing = i_vdTcpSetSendCoalescing;
|
---|
897 | m->vdIfTcpNet.pfnGetLocalAddress = i_vdTcpGetLocalAddress;
|
---|
898 | m->vdIfTcpNet.pfnGetPeerAddress = i_vdTcpGetPeerAddress;
|
---|
899 | m->vdIfTcpNet.pfnSelectOneEx = NULL;
|
---|
900 | m->vdIfTcpNet.pfnPoke = NULL;
|
---|
901 |
|
---|
902 | /* Initialize the per-disk interface chain (could be done more globally,
|
---|
903 | * but it's not wasting much time or space so it's not worth it). */
|
---|
904 | int vrc;
|
---|
905 | vrc = VDInterfaceAdd(&m->vdIfError.Core,
|
---|
906 | "Medium::vdInterfaceError",
|
---|
907 | VDINTERFACETYPE_ERROR, this,
|
---|
908 | sizeof(VDINTERFACEERROR), &m->vdDiskIfaces);
|
---|
909 | AssertRCReturn(vrc, E_FAIL);
|
---|
910 |
|
---|
911 | /* Initialize the per-image interface chain */
|
---|
912 | vrc = VDInterfaceAdd(&m->vdIfConfig.Core,
|
---|
913 | "Medium::vdInterfaceConfig",
|
---|
914 | VDINTERFACETYPE_CONFIG, this,
|
---|
915 | sizeof(VDINTERFACECONFIG), &m->vdImageIfaces);
|
---|
916 | AssertRCReturn(vrc, E_FAIL);
|
---|
917 |
|
---|
918 | vrc = VDInterfaceAdd(&m->vdIfTcpNet.Core,
|
---|
919 | "Medium::vdInterfaceTcpNet",
|
---|
920 | VDINTERFACETYPE_TCPNET, this,
|
---|
921 | sizeof(VDINTERFACETCPNET), &m->vdImageIfaces);
|
---|
922 | AssertRCReturn(vrc, E_FAIL);
|
---|
923 |
|
---|
924 | return BaseFinalConstruct();
|
---|
925 | }
|
---|
926 |
|
---|
927 | void Medium::FinalRelease()
|
---|
928 | {
|
---|
929 | uninit();
|
---|
930 |
|
---|
931 | delete m;
|
---|
932 |
|
---|
933 | BaseFinalRelease();
|
---|
934 | }
|
---|
935 |
|
---|
936 | /**
|
---|
937 | * Initializes an empty hard disk object without creating or opening an associated
|
---|
938 | * storage unit.
|
---|
939 | *
|
---|
940 | * This gets called by VirtualBox::CreateMedium() in which case uuidMachineRegistry
|
---|
941 | * is empty since starting with VirtualBox 4.0, we no longer add opened media to a
|
---|
942 | * registry automatically (this is deferred until the medium is attached to a machine).
|
---|
943 | *
|
---|
944 | * This also gets called when VirtualBox creates diff images; in this case uuidMachineRegistry
|
---|
945 | * is set to the registry of the parent image to make sure they all end up in the same
|
---|
946 | * file.
|
---|
947 | *
|
---|
948 | * For hard disks that don't have the MediumFormatCapabilities_CreateFixed or
|
---|
949 | * MediumFormatCapabilities_CreateDynamic capability (and therefore cannot be created or deleted
|
---|
950 | * with the means of VirtualBox) the associated storage unit is assumed to be
|
---|
951 | * ready for use so the state of the hard disk object will be set to Created.
|
---|
952 | *
|
---|
953 | * @param aVirtualBox VirtualBox object.
|
---|
954 | * @param aFormat
|
---|
955 | * @param aLocation Storage unit location.
|
---|
956 | * @param uuidMachineRegistry The registry to which this medium should be added
|
---|
957 | * (global registry UUID or machine UUID or empty if none).
|
---|
958 | * @param aDeviceType Device Type.
|
---|
959 | */
|
---|
960 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
961 | const Utf8Str &aFormat,
|
---|
962 | const Utf8Str &aLocation,
|
---|
963 | const Guid &uuidMachineRegistry,
|
---|
964 | const DeviceType_T aDeviceType)
|
---|
965 | {
|
---|
966 | AssertReturn(aVirtualBox != NULL, E_FAIL);
|
---|
967 | AssertReturn(!aFormat.isEmpty(), E_FAIL);
|
---|
968 |
|
---|
969 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
970 | AutoInitSpan autoInitSpan(this);
|
---|
971 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
972 |
|
---|
973 | HRESULT rc = S_OK;
|
---|
974 |
|
---|
975 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
976 |
|
---|
977 | if (uuidMachineRegistry.isValid() && !uuidMachineRegistry.isZero())
|
---|
978 | m->llRegistryIDs.push_back(uuidMachineRegistry);
|
---|
979 |
|
---|
980 | /* no storage yet */
|
---|
981 | m->state = MediumState_NotCreated;
|
---|
982 |
|
---|
983 | /* cannot be a host drive */
|
---|
984 | m->hostDrive = false;
|
---|
985 |
|
---|
986 | m->devType = aDeviceType;
|
---|
987 |
|
---|
988 | /* No storage unit is created yet, no need to call Medium::i_queryInfo */
|
---|
989 |
|
---|
990 | rc = i_setFormat(aFormat);
|
---|
991 | if (FAILED(rc)) return rc;
|
---|
992 |
|
---|
993 | rc = i_setLocation(aLocation);
|
---|
994 | if (FAILED(rc)) return rc;
|
---|
995 |
|
---|
996 | if (!(m->formatObj->i_getCapabilities() & ( MediumFormatCapabilities_CreateFixed
|
---|
997 | | MediumFormatCapabilities_CreateDynamic))
|
---|
998 | )
|
---|
999 | {
|
---|
1000 | /* Storage for mediums of this format can neither be explicitly
|
---|
1001 | * created by VirtualBox nor deleted, so we place the medium to
|
---|
1002 | * Inaccessible state here and also add it to the registry. The
|
---|
1003 | * state means that one has to use RefreshState() to update the
|
---|
1004 | * medium format specific fields. */
|
---|
1005 | m->state = MediumState_Inaccessible;
|
---|
1006 | // create new UUID
|
---|
1007 | unconst(m->id).create();
|
---|
1008 |
|
---|
1009 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1010 | ComObjPtr<Medium> pMedium;
|
---|
1011 |
|
---|
1012 | /*
|
---|
1013 | * Check whether the UUID is taken already and create a new one
|
---|
1014 | * if required.
|
---|
1015 | * Try this only a limited amount of times in case the PRNG is broken
|
---|
1016 | * in some way to prevent an endless loop.
|
---|
1017 | */
|
---|
1018 | for (unsigned i = 0; i < 5; i++)
|
---|
1019 | {
|
---|
1020 | bool fInUse;
|
---|
1021 |
|
---|
1022 | fInUse = m->pVirtualBox->i_isMediaUuidInUse(m->id, aDeviceType);
|
---|
1023 | if (fInUse)
|
---|
1024 | {
|
---|
1025 | // create new UUID
|
---|
1026 | unconst(m->id).create();
|
---|
1027 | }
|
---|
1028 | else
|
---|
1029 | break;
|
---|
1030 | }
|
---|
1031 |
|
---|
1032 | rc = m->pVirtualBox->i_registerMedium(this, &pMedium, treeLock);
|
---|
1033 | Assert(this == pMedium || FAILED(rc));
|
---|
1034 | }
|
---|
1035 |
|
---|
1036 | /* Confirm a successful initialization when it's the case */
|
---|
1037 | if (SUCCEEDED(rc))
|
---|
1038 | autoInitSpan.setSucceeded();
|
---|
1039 |
|
---|
1040 | return rc;
|
---|
1041 | }
|
---|
1042 |
|
---|
1043 | /**
|
---|
1044 | * Initializes the medium object by opening the storage unit at the specified
|
---|
1045 | * location. The enOpenMode parameter defines whether the medium will be opened
|
---|
1046 | * read/write or read-only.
|
---|
1047 | *
|
---|
1048 | * This gets called by VirtualBox::OpenMedium() and also by
|
---|
1049 | * Machine::AttachDevice() and createImplicitDiffs() when new diff
|
---|
1050 | * images are created.
|
---|
1051 | *
|
---|
1052 | * There is no registry for this case since starting with VirtualBox 4.0, we
|
---|
1053 | * no longer add opened media to a registry automatically (this is deferred
|
---|
1054 | * until the medium is attached to a machine).
|
---|
1055 | *
|
---|
1056 | * For hard disks, the UUID, format and the parent of this medium will be
|
---|
1057 | * determined when reading the medium storage unit. For DVD and floppy images,
|
---|
1058 | * which have no UUIDs in their storage units, new UUIDs are created.
|
---|
1059 | * If the detected or set parent is not known to VirtualBox, then this method
|
---|
1060 | * will fail.
|
---|
1061 | *
|
---|
1062 | * @param aVirtualBox VirtualBox object.
|
---|
1063 | * @param aLocation Storage unit location.
|
---|
1064 | * @param enOpenMode Whether to open the medium read/write or read-only.
|
---|
1065 | * @param fForceNewUuid Whether a new UUID should be set to avoid duplicates.
|
---|
1066 | * @param aDeviceType Device type of medium.
|
---|
1067 | */
|
---|
1068 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1069 | const Utf8Str &aLocation,
|
---|
1070 | HDDOpenMode enOpenMode,
|
---|
1071 | bool fForceNewUuid,
|
---|
1072 | DeviceType_T aDeviceType)
|
---|
1073 | {
|
---|
1074 | AssertReturn(aVirtualBox, E_INVALIDARG);
|
---|
1075 | AssertReturn(!aLocation.isEmpty(), E_INVALIDARG);
|
---|
1076 |
|
---|
1077 | HRESULT rc = S_OK;
|
---|
1078 |
|
---|
1079 | {
|
---|
1080 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1081 | AutoInitSpan autoInitSpan(this);
|
---|
1082 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1083 |
|
---|
1084 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1085 |
|
---|
1086 | /* there must be a storage unit */
|
---|
1087 | m->state = MediumState_Created;
|
---|
1088 |
|
---|
1089 | /* remember device type for correct unregistering later */
|
---|
1090 | m->devType = aDeviceType;
|
---|
1091 |
|
---|
1092 | /* cannot be a host drive */
|
---|
1093 | m->hostDrive = false;
|
---|
1094 |
|
---|
1095 | /* remember the open mode (defaults to ReadWrite) */
|
---|
1096 | m->hddOpenMode = enOpenMode;
|
---|
1097 |
|
---|
1098 | if (aDeviceType == DeviceType_DVD)
|
---|
1099 | m->type = MediumType_Readonly;
|
---|
1100 | else if (aDeviceType == DeviceType_Floppy)
|
---|
1101 | m->type = MediumType_Writethrough;
|
---|
1102 |
|
---|
1103 | rc = i_setLocation(aLocation);
|
---|
1104 | if (FAILED(rc)) return rc;
|
---|
1105 |
|
---|
1106 | /* get all the information about the medium from the storage unit */
|
---|
1107 | if (fForceNewUuid)
|
---|
1108 | unconst(m->uuidImage).create();
|
---|
1109 |
|
---|
1110 | m->state = MediumState_Inaccessible;
|
---|
1111 | m->strLastAccessError = tr("Accessibility check was not yet performed");
|
---|
1112 |
|
---|
1113 | /* Confirm a successful initialization before the call to i_queryInfo.
|
---|
1114 | * Otherwise we can end up with a AutoCaller deadlock because the
|
---|
1115 | * medium becomes visible but is not marked as initialized. Causes
|
---|
1116 | * locking trouble (e.g. trying to save media registries) which is
|
---|
1117 | * hard to solve. */
|
---|
1118 | autoInitSpan.setSucceeded();
|
---|
1119 | }
|
---|
1120 |
|
---|
1121 | /* we're normal code from now on, no longer init */
|
---|
1122 | AutoCaller autoCaller(this);
|
---|
1123 | if (FAILED(autoCaller.rc()))
|
---|
1124 | return autoCaller.rc();
|
---|
1125 |
|
---|
1126 | /* need to call i_queryInfo immediately to correctly place the medium in
|
---|
1127 | * the respective media tree and update other information such as uuid */
|
---|
1128 | rc = i_queryInfo(fForceNewUuid /* fSetImageId */, false /* fSetParentId */,
|
---|
1129 | autoCaller);
|
---|
1130 | if (SUCCEEDED(rc))
|
---|
1131 | {
|
---|
1132 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1133 |
|
---|
1134 | /* if the storage unit is not accessible, it's not acceptable for the
|
---|
1135 | * newly opened media so convert this into an error */
|
---|
1136 | if (m->state == MediumState_Inaccessible)
|
---|
1137 | {
|
---|
1138 | Assert(!m->strLastAccessError.isEmpty());
|
---|
1139 | rc = setError(E_FAIL, "%s", m->strLastAccessError.c_str());
|
---|
1140 | alock.release();
|
---|
1141 | autoCaller.release();
|
---|
1142 | uninit();
|
---|
1143 | }
|
---|
1144 | else
|
---|
1145 | {
|
---|
1146 | AssertStmt(!m->id.isZero(),
|
---|
1147 | alock.release(); autoCaller.release(); uninit(); return E_FAIL);
|
---|
1148 |
|
---|
1149 | /* storage format must be detected by Medium::i_queryInfo if the
|
---|
1150 | * medium is accessible */
|
---|
1151 | AssertStmt(!m->strFormat.isEmpty(),
|
---|
1152 | alock.release(); autoCaller.release(); uninit(); return E_FAIL);
|
---|
1153 | }
|
---|
1154 | }
|
---|
1155 | else
|
---|
1156 | {
|
---|
1157 | /* opening this image failed, mark the object as dead */
|
---|
1158 | autoCaller.release();
|
---|
1159 | uninit();
|
---|
1160 | }
|
---|
1161 |
|
---|
1162 | return rc;
|
---|
1163 | }
|
---|
1164 |
|
---|
1165 | /**
|
---|
1166 | * Initializes the medium object by loading its data from the given settings
|
---|
1167 | * node. The medium will always be opened read/write.
|
---|
1168 | *
|
---|
1169 | * In this case, since we're loading from a registry, uuidMachineRegistry is
|
---|
1170 | * always set: it's either the global registry UUID or a machine UUID when
|
---|
1171 | * loading from a per-machine registry.
|
---|
1172 | *
|
---|
1173 | * @param aParent Parent medium disk or NULL for a root (base) medium.
|
---|
1174 | * @param aDeviceType Device type of the medium.
|
---|
1175 | * @param uuidMachineRegistry The registry to which this medium should be
|
---|
1176 | * added (global registry UUID or machine UUID).
|
---|
1177 | * @param data Configuration settings.
|
---|
1178 | * @param strMachineFolder The machine folder with which to resolve relative paths;
|
---|
1179 | * if empty, then we use the VirtualBox home directory
|
---|
1180 | *
|
---|
1181 | * @note Locks the medium tree for writing.
|
---|
1182 | */
|
---|
1183 | HRESULT Medium::initOne(Medium *aParent,
|
---|
1184 | DeviceType_T aDeviceType,
|
---|
1185 | const Guid &uuidMachineRegistry,
|
---|
1186 | const settings::Medium &data,
|
---|
1187 | const Utf8Str &strMachineFolder)
|
---|
1188 | {
|
---|
1189 | HRESULT rc;
|
---|
1190 |
|
---|
1191 | if (uuidMachineRegistry.isValid() && !uuidMachineRegistry.isZero())
|
---|
1192 | m->llRegistryIDs.push_back(uuidMachineRegistry);
|
---|
1193 |
|
---|
1194 | /* register with VirtualBox/parent early, since uninit() will
|
---|
1195 | * unconditionally unregister on failure */
|
---|
1196 | if (aParent)
|
---|
1197 | {
|
---|
1198 | // differencing medium: add to parent
|
---|
1199 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1200 | // no need to check maximum depth as settings reading did it
|
---|
1201 | i_setParent(aParent);
|
---|
1202 | }
|
---|
1203 |
|
---|
1204 | /* see below why we don't call Medium::i_queryInfo (and therefore treat
|
---|
1205 | * the medium as inaccessible for now */
|
---|
1206 | m->state = MediumState_Inaccessible;
|
---|
1207 | m->strLastAccessError = tr("Accessibility check was not yet performed");
|
---|
1208 |
|
---|
1209 | /* required */
|
---|
1210 | unconst(m->id) = data.uuid;
|
---|
1211 |
|
---|
1212 | /* assume not a host drive */
|
---|
1213 | m->hostDrive = false;
|
---|
1214 |
|
---|
1215 | /* optional */
|
---|
1216 | m->strDescription = data.strDescription;
|
---|
1217 |
|
---|
1218 | /* required */
|
---|
1219 | if (aDeviceType == DeviceType_HardDisk)
|
---|
1220 | {
|
---|
1221 | AssertReturn(!data.strFormat.isEmpty(), E_FAIL);
|
---|
1222 | rc = i_setFormat(data.strFormat);
|
---|
1223 | if (FAILED(rc)) return rc;
|
---|
1224 | }
|
---|
1225 | else
|
---|
1226 | {
|
---|
1227 | /// @todo handle host drive settings here as well?
|
---|
1228 | if (!data.strFormat.isEmpty())
|
---|
1229 | rc = i_setFormat(data.strFormat);
|
---|
1230 | else
|
---|
1231 | rc = i_setFormat("RAW");
|
---|
1232 | if (FAILED(rc)) return rc;
|
---|
1233 | }
|
---|
1234 |
|
---|
1235 | /* optional, only for diffs, default is false; we can only auto-reset
|
---|
1236 | * diff media so they must have a parent */
|
---|
1237 | if (aParent != NULL)
|
---|
1238 | m->autoReset = data.fAutoReset;
|
---|
1239 | else
|
---|
1240 | m->autoReset = false;
|
---|
1241 |
|
---|
1242 | /* properties (after setting the format as it populates the map). Note that
|
---|
1243 | * if some properties are not supported but present in the settings file,
|
---|
1244 | * they will still be read and accessible (for possible backward
|
---|
1245 | * compatibility; we can also clean them up from the XML upon next
|
---|
1246 | * XML format version change if we wish) */
|
---|
1247 | for (settings::StringsMap::const_iterator it = data.properties.begin();
|
---|
1248 | it != data.properties.end();
|
---|
1249 | ++it)
|
---|
1250 | {
|
---|
1251 | const Utf8Str &name = it->first;
|
---|
1252 | const Utf8Str &value = it->second;
|
---|
1253 | m->mapProperties[name] = value;
|
---|
1254 | }
|
---|
1255 |
|
---|
1256 | /* try to decrypt an optional iSCSI initiator secret */
|
---|
1257 | settings::StringsMap::const_iterator itCph = data.properties.find("InitiatorSecretEncrypted");
|
---|
1258 | if ( itCph != data.properties.end()
|
---|
1259 | && !itCph->second.isEmpty())
|
---|
1260 | {
|
---|
1261 | Utf8Str strPlaintext;
|
---|
1262 | int vrc = m->pVirtualBox->i_decryptSetting(&strPlaintext, itCph->second);
|
---|
1263 | if (RT_SUCCESS(vrc))
|
---|
1264 | m->mapProperties["InitiatorSecret"] = strPlaintext;
|
---|
1265 | }
|
---|
1266 |
|
---|
1267 | Utf8Str strFull;
|
---|
1268 | if (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
1269 | {
|
---|
1270 | // compose full path of the medium, if it's not fully qualified...
|
---|
1271 | // slightly convoluted logic here. If the caller has given us a
|
---|
1272 | // machine folder, then a relative path will be relative to that:
|
---|
1273 | if ( !strMachineFolder.isEmpty()
|
---|
1274 | && !RTPathStartsWithRoot(data.strLocation.c_str())
|
---|
1275 | )
|
---|
1276 | {
|
---|
1277 | strFull = strMachineFolder;
|
---|
1278 | strFull += RTPATH_SLASH;
|
---|
1279 | strFull += data.strLocation;
|
---|
1280 | }
|
---|
1281 | else
|
---|
1282 | {
|
---|
1283 | // Otherwise use the old VirtualBox "make absolute path" logic:
|
---|
1284 | rc = m->pVirtualBox->i_calculateFullPath(data.strLocation, strFull);
|
---|
1285 | if (FAILED(rc)) return rc;
|
---|
1286 | }
|
---|
1287 | }
|
---|
1288 | else
|
---|
1289 | strFull = data.strLocation;
|
---|
1290 |
|
---|
1291 | rc = i_setLocation(strFull);
|
---|
1292 | if (FAILED(rc)) return rc;
|
---|
1293 |
|
---|
1294 | if (aDeviceType == DeviceType_HardDisk)
|
---|
1295 | {
|
---|
1296 | /* type is only for base hard disks */
|
---|
1297 | if (m->pParent.isNull())
|
---|
1298 | m->type = data.hdType;
|
---|
1299 | }
|
---|
1300 | else if (aDeviceType == DeviceType_DVD)
|
---|
1301 | m->type = MediumType_Readonly;
|
---|
1302 | else
|
---|
1303 | m->type = MediumType_Writethrough;
|
---|
1304 |
|
---|
1305 | /* remember device type for correct unregistering later */
|
---|
1306 | m->devType = aDeviceType;
|
---|
1307 |
|
---|
1308 | LogFlowThisFunc(("m->strLocationFull='%s', m->strFormat=%s, m->id={%RTuuid}\n",
|
---|
1309 | m->strLocationFull.c_str(), m->strFormat.c_str(), m->id.raw()));
|
---|
1310 |
|
---|
1311 | return S_OK;
|
---|
1312 | }
|
---|
1313 |
|
---|
1314 | /**
|
---|
1315 | * Initializes the medium object and its children by loading its data from the
|
---|
1316 | * given settings node. The medium will always be opened read/write.
|
---|
1317 | *
|
---|
1318 | * In this case, since we're loading from a registry, uuidMachineRegistry is
|
---|
1319 | * always set: it's either the global registry UUID or a machine UUID when
|
---|
1320 | * loading from a per-machine registry.
|
---|
1321 | *
|
---|
1322 | * @param aVirtualBox VirtualBox object.
|
---|
1323 | * @param aParent Parent medium disk or NULL for a root (base) medium.
|
---|
1324 | * @param aDeviceType Device type of the medium.
|
---|
1325 | * @param uuidMachineRegistry The registry to which this medium should be added
|
---|
1326 | * (global registry UUID or machine UUID).
|
---|
1327 | * @param data Configuration settings.
|
---|
1328 | * @param strMachineFolder The machine folder with which to resolve relative
|
---|
1329 | * paths; if empty, then we use the VirtualBox home directory
|
---|
1330 | * @param mediaTreeLock Autolock.
|
---|
1331 | *
|
---|
1332 | * @note Locks the medium tree for writing.
|
---|
1333 | */
|
---|
1334 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1335 | Medium *aParent,
|
---|
1336 | DeviceType_T aDeviceType,
|
---|
1337 | const Guid &uuidMachineRegistry,
|
---|
1338 | const settings::Medium &data,
|
---|
1339 | const Utf8Str &strMachineFolder,
|
---|
1340 | AutoWriteLock &mediaTreeLock)
|
---|
1341 | {
|
---|
1342 | using namespace settings;
|
---|
1343 |
|
---|
1344 | Assert(aVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
1345 | AssertReturn(aVirtualBox, E_INVALIDARG);
|
---|
1346 |
|
---|
1347 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1348 | AutoInitSpan autoInitSpan(this);
|
---|
1349 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1350 |
|
---|
1351 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1352 |
|
---|
1353 | // Do not inline this method call, as the purpose of having this separate
|
---|
1354 | // is to save on stack size. Less local variables are the key for reaching
|
---|
1355 | // deep recursion levels with small stack (XPCOM/g++ without optimization).
|
---|
1356 | HRESULT rc = initOne(aParent, aDeviceType, uuidMachineRegistry, data, strMachineFolder);
|
---|
1357 |
|
---|
1358 |
|
---|
1359 | /* Don't call Medium::i_queryInfo for registered media to prevent the calling
|
---|
1360 | * thread (i.e. the VirtualBox server startup thread) from an unexpected
|
---|
1361 | * freeze but mark it as initially inaccessible instead. The vital UUID,
|
---|
1362 | * location and format properties are read from the registry file above; to
|
---|
1363 | * get the actual state and the rest of the data, the user will have to call
|
---|
1364 | * COMGETTER(State). */
|
---|
1365 |
|
---|
1366 | /* load all children */
|
---|
1367 | for (settings::MediaList::const_iterator it = data.llChildren.begin();
|
---|
1368 | it != data.llChildren.end();
|
---|
1369 | ++it)
|
---|
1370 | {
|
---|
1371 | const settings::Medium &med = *it;
|
---|
1372 |
|
---|
1373 | ComObjPtr<Medium> pMedium;
|
---|
1374 | pMedium.createObject();
|
---|
1375 | rc = pMedium->init(aVirtualBox,
|
---|
1376 | this, // parent
|
---|
1377 | aDeviceType,
|
---|
1378 | uuidMachineRegistry,
|
---|
1379 | med, // child data
|
---|
1380 | strMachineFolder,
|
---|
1381 | mediaTreeLock);
|
---|
1382 | if (FAILED(rc)) break;
|
---|
1383 |
|
---|
1384 | rc = m->pVirtualBox->i_registerMedium(pMedium, &pMedium, mediaTreeLock);
|
---|
1385 | if (FAILED(rc)) break;
|
---|
1386 | }
|
---|
1387 |
|
---|
1388 | /* Confirm a successful initialization when it's the case */
|
---|
1389 | if (SUCCEEDED(rc))
|
---|
1390 | autoInitSpan.setSucceeded();
|
---|
1391 |
|
---|
1392 | return rc;
|
---|
1393 | }
|
---|
1394 |
|
---|
1395 | /**
|
---|
1396 | * Initializes the medium object by providing the host drive information.
|
---|
1397 | * Not used for anything but the host floppy/host DVD case.
|
---|
1398 | *
|
---|
1399 | * There is no registry for this case.
|
---|
1400 | *
|
---|
1401 | * @param aVirtualBox VirtualBox object.
|
---|
1402 | * @param aDeviceType Device type of the medium.
|
---|
1403 | * @param aLocation Location of the host drive.
|
---|
1404 | * @param aDescription Comment for this host drive.
|
---|
1405 | *
|
---|
1406 | * @note Locks VirtualBox lock for writing.
|
---|
1407 | */
|
---|
1408 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1409 | DeviceType_T aDeviceType,
|
---|
1410 | const Utf8Str &aLocation,
|
---|
1411 | const Utf8Str &aDescription /* = Utf8Str::Empty */)
|
---|
1412 | {
|
---|
1413 | ComAssertRet(aDeviceType == DeviceType_DVD || aDeviceType == DeviceType_Floppy, E_INVALIDARG);
|
---|
1414 | ComAssertRet(!aLocation.isEmpty(), E_INVALIDARG);
|
---|
1415 |
|
---|
1416 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1417 | AutoInitSpan autoInitSpan(this);
|
---|
1418 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1419 |
|
---|
1420 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1421 |
|
---|
1422 | // We do not store host drives in VirtualBox.xml or anywhere else, so if we want
|
---|
1423 | // host drives to be identifiable by UUID and not give the drive a different UUID
|
---|
1424 | // every time VirtualBox starts, we need to fake a reproducible UUID here:
|
---|
1425 | RTUUID uuid;
|
---|
1426 | RTUuidClear(&uuid);
|
---|
1427 | if (aDeviceType == DeviceType_DVD)
|
---|
1428 | memcpy(&uuid.au8[0], "DVD", 3);
|
---|
1429 | else
|
---|
1430 | memcpy(&uuid.au8[0], "FD", 2);
|
---|
1431 | /* use device name, adjusted to the end of uuid, shortened if necessary */
|
---|
1432 | size_t lenLocation = aLocation.length();
|
---|
1433 | if (lenLocation > 12)
|
---|
1434 | memcpy(&uuid.au8[4], aLocation.c_str() + (lenLocation - 12), 12);
|
---|
1435 | else
|
---|
1436 | memcpy(&uuid.au8[4 + 12 - lenLocation], aLocation.c_str(), lenLocation);
|
---|
1437 | unconst(m->id) = uuid;
|
---|
1438 |
|
---|
1439 | if (aDeviceType == DeviceType_DVD)
|
---|
1440 | m->type = MediumType_Readonly;
|
---|
1441 | else
|
---|
1442 | m->type = MediumType_Writethrough;
|
---|
1443 | m->devType = aDeviceType;
|
---|
1444 | m->state = MediumState_Created;
|
---|
1445 | m->hostDrive = true;
|
---|
1446 | HRESULT rc = i_setFormat("RAW");
|
---|
1447 | if (FAILED(rc)) return rc;
|
---|
1448 | rc = i_setLocation(aLocation);
|
---|
1449 | if (FAILED(rc)) return rc;
|
---|
1450 | m->strDescription = aDescription;
|
---|
1451 |
|
---|
1452 | autoInitSpan.setSucceeded();
|
---|
1453 | return S_OK;
|
---|
1454 | }
|
---|
1455 |
|
---|
1456 | /**
|
---|
1457 | * Uninitializes the instance.
|
---|
1458 | *
|
---|
1459 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
1460 | *
|
---|
1461 | * @note All children of this medium get uninitialized by calling their
|
---|
1462 | * uninit() methods.
|
---|
1463 | */
|
---|
1464 | void Medium::uninit()
|
---|
1465 | {
|
---|
1466 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1467 | * the pVirtualBox reference, and in this case we don't need to continue.
|
---|
1468 | * Normally this would be handled through the AutoUninitSpan magic, however
|
---|
1469 | * this cannot be done at this point as the media tree must be locked
|
---|
1470 | * before reaching the AutoUninitSpan, otherwise deadlocks can happen.
|
---|
1471 | *
|
---|
1472 | * NOTE: The tree lock is higher priority than the medium caller and medium
|
---|
1473 | * object locks, i.e. the medium caller may have to be released and be
|
---|
1474 | * re-acquired in the right place later. See Medium::getParent() for sample
|
---|
1475 | * code how to do this safely. */
|
---|
1476 | VirtualBox *pVirtualBox = m->pVirtualBox;
|
---|
1477 | if (!pVirtualBox)
|
---|
1478 | return;
|
---|
1479 |
|
---|
1480 | /* Caller must not hold the object or media tree lock over uninit(). */
|
---|
1481 | Assert(!isWriteLockOnCurrentThread());
|
---|
1482 | Assert(!pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
1483 |
|
---|
1484 | AutoWriteLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1485 |
|
---|
1486 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
1487 | AutoUninitSpan autoUninitSpan(this);
|
---|
1488 | if (autoUninitSpan.uninitDone())
|
---|
1489 | return;
|
---|
1490 |
|
---|
1491 | if (!m->formatObj.isNull())
|
---|
1492 | m->formatObj.setNull();
|
---|
1493 |
|
---|
1494 | if (m->state == MediumState_Deleting)
|
---|
1495 | {
|
---|
1496 | /* This medium has been already deleted (directly or as part of a
|
---|
1497 | * merge). Reparenting has already been done. */
|
---|
1498 | Assert(m->pParent.isNull());
|
---|
1499 | }
|
---|
1500 | else
|
---|
1501 | {
|
---|
1502 | MediaList llChildren(m->llChildren);
|
---|
1503 | m->llChildren.clear();
|
---|
1504 | autoUninitSpan.setSucceeded();
|
---|
1505 |
|
---|
1506 | while (!llChildren.empty())
|
---|
1507 | {
|
---|
1508 | ComObjPtr<Medium> pChild = llChildren.front();
|
---|
1509 | llChildren.pop_front();
|
---|
1510 | pChild->m->pParent.setNull();
|
---|
1511 | treeLock.release();
|
---|
1512 | pChild->uninit();
|
---|
1513 | treeLock.acquire();
|
---|
1514 | }
|
---|
1515 |
|
---|
1516 | if (m->pParent)
|
---|
1517 | {
|
---|
1518 | // this is a differencing disk: then remove it from the parent's children list
|
---|
1519 | i_deparent();
|
---|
1520 | }
|
---|
1521 | }
|
---|
1522 |
|
---|
1523 | unconst(m->pVirtualBox) = NULL;
|
---|
1524 | }
|
---|
1525 |
|
---|
1526 | /**
|
---|
1527 | * Internal helper that removes "this" from the list of children of its
|
---|
1528 | * parent. Used in uninit() and other places when reparenting is necessary.
|
---|
1529 | *
|
---|
1530 | * The caller must hold the medium tree lock!
|
---|
1531 | */
|
---|
1532 | void Medium::i_deparent()
|
---|
1533 | {
|
---|
1534 | MediaList &llParent = m->pParent->m->llChildren;
|
---|
1535 | for (MediaList::iterator it = llParent.begin();
|
---|
1536 | it != llParent.end();
|
---|
1537 | ++it)
|
---|
1538 | {
|
---|
1539 | Medium *pParentsChild = *it;
|
---|
1540 | if (this == pParentsChild)
|
---|
1541 | {
|
---|
1542 | llParent.erase(it);
|
---|
1543 | break;
|
---|
1544 | }
|
---|
1545 | }
|
---|
1546 | m->pParent.setNull();
|
---|
1547 | }
|
---|
1548 |
|
---|
1549 | /**
|
---|
1550 | * Internal helper that removes "this" from the list of children of its
|
---|
1551 | * parent. Used in uninit() and other places when reparenting is necessary.
|
---|
1552 | *
|
---|
1553 | * The caller must hold the medium tree lock!
|
---|
1554 | */
|
---|
1555 | void Medium::i_setParent(const ComObjPtr<Medium> &pParent)
|
---|
1556 | {
|
---|
1557 | m->pParent = pParent;
|
---|
1558 | if (pParent)
|
---|
1559 | pParent->m->llChildren.push_back(this);
|
---|
1560 | }
|
---|
1561 |
|
---|
1562 |
|
---|
1563 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1564 | //
|
---|
1565 | // IMedium public methods
|
---|
1566 | //
|
---|
1567 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1568 |
|
---|
1569 | HRESULT Medium::getId(com::Guid &aId)
|
---|
1570 | {
|
---|
1571 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1572 |
|
---|
1573 | aId = m->id;
|
---|
1574 |
|
---|
1575 | return S_OK;
|
---|
1576 | }
|
---|
1577 |
|
---|
1578 | HRESULT Medium::getDescription(AutoCaller &autoCaller, com::Utf8Str &aDescription)
|
---|
1579 | {
|
---|
1580 | NOREF(autoCaller);
|
---|
1581 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1582 |
|
---|
1583 | aDescription = m->strDescription;
|
---|
1584 |
|
---|
1585 | return S_OK;
|
---|
1586 | }
|
---|
1587 |
|
---|
1588 | HRESULT Medium::setDescription(AutoCaller &autoCaller, const com::Utf8Str &aDescription)
|
---|
1589 | {
|
---|
1590 | /// @todo update m->strDescription and save the global registry (and local
|
---|
1591 | /// registries of portable VMs referring to this medium), this will also
|
---|
1592 | /// require to add the mRegistered flag to data
|
---|
1593 |
|
---|
1594 | HRESULT rc = S_OK;
|
---|
1595 |
|
---|
1596 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
1597 |
|
---|
1598 | try
|
---|
1599 | {
|
---|
1600 | autoCaller.release();
|
---|
1601 |
|
---|
1602 | // to avoid redundant locking, which just takes a time, just call required functions.
|
---|
1603 | // the error will be just stored and will be reported after locks will be acquired again
|
---|
1604 |
|
---|
1605 | const char *pszError = NULL;
|
---|
1606 |
|
---|
1607 |
|
---|
1608 | /* Build the lock list. */
|
---|
1609 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
1610 | this /* pToLockWrite */,
|
---|
1611 | true /* fMediumLockWriteAll */,
|
---|
1612 | NULL,
|
---|
1613 | *pMediumLockList);
|
---|
1614 | if (FAILED(rc))
|
---|
1615 | {
|
---|
1616 | pszError = tr("Failed to create medium lock list for '%s'");
|
---|
1617 | }
|
---|
1618 | else
|
---|
1619 | {
|
---|
1620 | rc = pMediumLockList->Lock();
|
---|
1621 | if (FAILED(rc))
|
---|
1622 | pszError = tr("Failed to lock media '%s'");
|
---|
1623 | }
|
---|
1624 |
|
---|
1625 | // locking: we need the tree lock first because we access parent pointers
|
---|
1626 | // and we need to write-lock the media involved
|
---|
1627 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1628 |
|
---|
1629 | autoCaller.add();
|
---|
1630 | AssertComRCThrowRC(autoCaller.rc());
|
---|
1631 |
|
---|
1632 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1633 |
|
---|
1634 | if (FAILED(rc))
|
---|
1635 | throw setError(rc, pszError, i_getLocationFull().c_str());
|
---|
1636 |
|
---|
1637 | /* Set a new description */
|
---|
1638 | m->strDescription = aDescription;
|
---|
1639 |
|
---|
1640 | // save the settings
|
---|
1641 | alock.release();
|
---|
1642 | autoCaller.release();
|
---|
1643 | treeLock.release();
|
---|
1644 | i_markRegistriesModified();
|
---|
1645 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
1646 | }
|
---|
1647 | catch (HRESULT aRC) { rc = aRC; }
|
---|
1648 |
|
---|
1649 | delete pMediumLockList;
|
---|
1650 |
|
---|
1651 | return rc;
|
---|
1652 | }
|
---|
1653 |
|
---|
1654 | HRESULT Medium::getState(MediumState_T *aState)
|
---|
1655 | {
|
---|
1656 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1657 | *aState = m->state;
|
---|
1658 |
|
---|
1659 | return S_OK;
|
---|
1660 | }
|
---|
1661 |
|
---|
1662 | HRESULT Medium::getVariant(std::vector<MediumVariant_T> &aVariant)
|
---|
1663 | {
|
---|
1664 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1665 |
|
---|
1666 | const size_t cBits = sizeof(MediumVariant_T) * 8;
|
---|
1667 | aVariant.resize(cBits);
|
---|
1668 | for (size_t i = 0; i < cBits; ++i)
|
---|
1669 | aVariant[i] = (MediumVariant_T)(m->variant & RT_BIT(i));
|
---|
1670 |
|
---|
1671 | return S_OK;
|
---|
1672 | }
|
---|
1673 |
|
---|
1674 | HRESULT Medium::getLocation(com::Utf8Str &aLocation)
|
---|
1675 | {
|
---|
1676 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1677 |
|
---|
1678 | aLocation = m->strLocationFull;
|
---|
1679 |
|
---|
1680 | return S_OK;
|
---|
1681 | }
|
---|
1682 |
|
---|
1683 | HRESULT Medium::getName(com::Utf8Str &aName)
|
---|
1684 | {
|
---|
1685 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1686 |
|
---|
1687 | aName = i_getName();
|
---|
1688 |
|
---|
1689 | return S_OK;
|
---|
1690 | }
|
---|
1691 |
|
---|
1692 | HRESULT Medium::getDeviceType(DeviceType_T *aDeviceType)
|
---|
1693 | {
|
---|
1694 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1695 |
|
---|
1696 | *aDeviceType = m->devType;
|
---|
1697 |
|
---|
1698 | return S_OK;
|
---|
1699 | }
|
---|
1700 |
|
---|
1701 | HRESULT Medium::getHostDrive(BOOL *aHostDrive)
|
---|
1702 | {
|
---|
1703 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1704 |
|
---|
1705 | *aHostDrive = m->hostDrive;
|
---|
1706 |
|
---|
1707 | return S_OK;
|
---|
1708 | }
|
---|
1709 |
|
---|
1710 | HRESULT Medium::getSize(LONG64 *aSize)
|
---|
1711 | {
|
---|
1712 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1713 |
|
---|
1714 | *aSize = m->size;
|
---|
1715 |
|
---|
1716 | return S_OK;
|
---|
1717 | }
|
---|
1718 |
|
---|
1719 | HRESULT Medium::getFormat(com::Utf8Str &aFormat)
|
---|
1720 | {
|
---|
1721 | /* no need to lock, m->strFormat is const */
|
---|
1722 |
|
---|
1723 | aFormat = m->strFormat;
|
---|
1724 | return S_OK;
|
---|
1725 | }
|
---|
1726 |
|
---|
1727 | HRESULT Medium::getMediumFormat(ComPtr<IMediumFormat> &aMediumFormat)
|
---|
1728 | {
|
---|
1729 | /* no need to lock, m->formatObj is const */
|
---|
1730 | m->formatObj.queryInterfaceTo(aMediumFormat.asOutParam());
|
---|
1731 |
|
---|
1732 | return S_OK;
|
---|
1733 | }
|
---|
1734 |
|
---|
1735 | HRESULT Medium::getType(AutoCaller &autoCaller, MediumType_T *aType)
|
---|
1736 | {
|
---|
1737 | NOREF(autoCaller);
|
---|
1738 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1739 |
|
---|
1740 | *aType = m->type;
|
---|
1741 |
|
---|
1742 | return S_OK;
|
---|
1743 | }
|
---|
1744 |
|
---|
1745 | HRESULT Medium::setType(AutoCaller &autoCaller, MediumType_T aType)
|
---|
1746 | {
|
---|
1747 | autoCaller.release();
|
---|
1748 |
|
---|
1749 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1750 | * the pVirtualBox reference, see #uninit(). */
|
---|
1751 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
1752 |
|
---|
1753 | // we access m->pParent
|
---|
1754 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
1755 |
|
---|
1756 | autoCaller.add();
|
---|
1757 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1758 |
|
---|
1759 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1760 |
|
---|
1761 | switch (m->state)
|
---|
1762 | {
|
---|
1763 | case MediumState_Created:
|
---|
1764 | case MediumState_Inaccessible:
|
---|
1765 | break;
|
---|
1766 | default:
|
---|
1767 | return i_setStateError();
|
---|
1768 | }
|
---|
1769 |
|
---|
1770 | if (m->type == aType)
|
---|
1771 | {
|
---|
1772 | /* Nothing to do */
|
---|
1773 | return S_OK;
|
---|
1774 | }
|
---|
1775 |
|
---|
1776 | DeviceType_T devType = i_getDeviceType();
|
---|
1777 | // DVD media can only be readonly.
|
---|
1778 | if (devType == DeviceType_DVD && aType != MediumType_Readonly)
|
---|
1779 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1780 | tr("Cannot change the type of DVD medium '%s'"),
|
---|
1781 | m->strLocationFull.c_str());
|
---|
1782 | // Floppy media can only be writethrough or readonly.
|
---|
1783 | if ( devType == DeviceType_Floppy
|
---|
1784 | && aType != MediumType_Writethrough
|
---|
1785 | && aType != MediumType_Readonly)
|
---|
1786 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1787 | tr("Cannot change the type of floppy medium '%s'"),
|
---|
1788 | m->strLocationFull.c_str());
|
---|
1789 |
|
---|
1790 | /* cannot change the type of a differencing medium */
|
---|
1791 | if (m->pParent)
|
---|
1792 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1793 | tr("Cannot change the type of medium '%s' because it is a differencing medium"),
|
---|
1794 | m->strLocationFull.c_str());
|
---|
1795 |
|
---|
1796 | /* Cannot change the type of a medium being in use by more than one VM.
|
---|
1797 | * If the change is to Immutable or MultiAttach then it must not be
|
---|
1798 | * directly attached to any VM, otherwise the assumptions about indirect
|
---|
1799 | * attachment elsewhere are violated and the VM becomes inaccessible.
|
---|
1800 | * Attaching an immutable medium triggers the diff creation, and this is
|
---|
1801 | * vital for the correct operation. */
|
---|
1802 | if ( m->backRefs.size() > 1
|
---|
1803 | || ( ( aType == MediumType_Immutable
|
---|
1804 | || aType == MediumType_MultiAttach)
|
---|
1805 | && m->backRefs.size() > 0))
|
---|
1806 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1807 | tr("Cannot change the type of medium '%s' because it is attached to %d virtual machines"),
|
---|
1808 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
1809 |
|
---|
1810 | switch (aType)
|
---|
1811 | {
|
---|
1812 | case MediumType_Normal:
|
---|
1813 | case MediumType_Immutable:
|
---|
1814 | case MediumType_MultiAttach:
|
---|
1815 | {
|
---|
1816 | /* normal can be easily converted to immutable and vice versa even
|
---|
1817 | * if they have children as long as they are not attached to any
|
---|
1818 | * machine themselves */
|
---|
1819 | break;
|
---|
1820 | }
|
---|
1821 | case MediumType_Writethrough:
|
---|
1822 | case MediumType_Shareable:
|
---|
1823 | case MediumType_Readonly:
|
---|
1824 | {
|
---|
1825 | /* cannot change to writethrough, shareable or readonly
|
---|
1826 | * if there are children */
|
---|
1827 | if (i_getChildren().size() != 0)
|
---|
1828 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
1829 | tr("Cannot change type for medium '%s' since it has %d child media"),
|
---|
1830 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
1831 | if (aType == MediumType_Shareable)
|
---|
1832 | {
|
---|
1833 | MediumVariant_T variant = i_getVariant();
|
---|
1834 | if (!(variant & MediumVariant_Fixed))
|
---|
1835 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1836 | tr("Cannot change type for medium '%s' to 'Shareable' since it is a dynamic medium storage unit"),
|
---|
1837 | m->strLocationFull.c_str());
|
---|
1838 | }
|
---|
1839 | else if (aType == MediumType_Readonly && devType == DeviceType_HardDisk)
|
---|
1840 | {
|
---|
1841 | // Readonly hard disks are not allowed, this medium type is reserved for
|
---|
1842 | // DVDs and floppy images at the moment. Later we might allow readonly hard
|
---|
1843 | // disks, but that's extremely unusual and many guest OSes will have trouble.
|
---|
1844 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1845 | tr("Cannot change type for medium '%s' to 'Readonly' since it is a hard disk"),
|
---|
1846 | m->strLocationFull.c_str());
|
---|
1847 | }
|
---|
1848 | break;
|
---|
1849 | }
|
---|
1850 | default:
|
---|
1851 | AssertFailedReturn(E_FAIL);
|
---|
1852 | }
|
---|
1853 |
|
---|
1854 | if (aType == MediumType_MultiAttach)
|
---|
1855 | {
|
---|
1856 | // This type is new with VirtualBox 4.0 and therefore requires settings
|
---|
1857 | // version 1.11 in the settings backend. Unfortunately it is not enough to do
|
---|
1858 | // the usual routine in MachineConfigFile::bumpSettingsVersionIfNeeded() for
|
---|
1859 | // two reasons: The medium type is a property of the media registry tree, which
|
---|
1860 | // can reside in the global config file (for pre-4.0 media); we would therefore
|
---|
1861 | // possibly need to bump the global config version. We don't want to do that though
|
---|
1862 | // because that might make downgrading to pre-4.0 impossible.
|
---|
1863 | // As a result, we can only use these two new types if the medium is NOT in the
|
---|
1864 | // global registry:
|
---|
1865 | const Guid &uuidGlobalRegistry = m->pVirtualBox->i_getGlobalRegistryId();
|
---|
1866 | if (i_isInRegistry(uuidGlobalRegistry))
|
---|
1867 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1868 | tr("Cannot change type for medium '%s': the media type 'MultiAttach' can only be used "
|
---|
1869 | "on media registered with a machine that was created with VirtualBox 4.0 or later"),
|
---|
1870 | m->strLocationFull.c_str());
|
---|
1871 | }
|
---|
1872 |
|
---|
1873 | m->type = aType;
|
---|
1874 |
|
---|
1875 | // save the settings
|
---|
1876 | mlock.release();
|
---|
1877 | autoCaller.release();
|
---|
1878 | treeLock.release();
|
---|
1879 | i_markRegistriesModified();
|
---|
1880 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
1881 |
|
---|
1882 | return S_OK;
|
---|
1883 | }
|
---|
1884 |
|
---|
1885 | HRESULT Medium::getAllowedTypes(std::vector<MediumType_T> &aAllowedTypes)
|
---|
1886 | {
|
---|
1887 | NOREF(aAllowedTypes);
|
---|
1888 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1889 |
|
---|
1890 | ReturnComNotImplemented();
|
---|
1891 | }
|
---|
1892 |
|
---|
1893 | HRESULT Medium::getParent(AutoCaller &autoCaller, ComPtr<IMedium> &aParent)
|
---|
1894 | {
|
---|
1895 | autoCaller.release();
|
---|
1896 |
|
---|
1897 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1898 | * the pVirtualBox reference, see #uninit(). */
|
---|
1899 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
1900 |
|
---|
1901 | /* we access m->pParent */
|
---|
1902 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
1903 |
|
---|
1904 | autoCaller.add();
|
---|
1905 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1906 |
|
---|
1907 | m->pParent.queryInterfaceTo(aParent.asOutParam());
|
---|
1908 |
|
---|
1909 | return S_OK;
|
---|
1910 | }
|
---|
1911 |
|
---|
1912 | HRESULT Medium::getChildren(AutoCaller &autoCaller, std::vector<ComPtr<IMedium> > &aChildren)
|
---|
1913 | {
|
---|
1914 | autoCaller.release();
|
---|
1915 |
|
---|
1916 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1917 | * the pVirtualBox reference, see #uninit(). */
|
---|
1918 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
1919 |
|
---|
1920 | /* we access children */
|
---|
1921 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
1922 |
|
---|
1923 | autoCaller.add();
|
---|
1924 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1925 |
|
---|
1926 | MediaList children(this->i_getChildren());
|
---|
1927 | aChildren.resize(children.size());
|
---|
1928 | size_t i = 0;
|
---|
1929 | for (MediaList::const_iterator it = children.begin(); it != children.end(); ++it, ++i)
|
---|
1930 | (*it).queryInterfaceTo(aChildren[i].asOutParam());
|
---|
1931 | return S_OK;
|
---|
1932 | }
|
---|
1933 |
|
---|
1934 | HRESULT Medium::getBase(AutoCaller &autoCaller, ComPtr<IMedium> &aBase)
|
---|
1935 | {
|
---|
1936 | autoCaller.release();
|
---|
1937 |
|
---|
1938 | /* i_getBase() will do callers/locking */
|
---|
1939 | i_getBase().queryInterfaceTo(aBase.asOutParam());
|
---|
1940 |
|
---|
1941 | return S_OK;
|
---|
1942 | }
|
---|
1943 |
|
---|
1944 | HRESULT Medium::getReadOnly(AutoCaller &autoCaller, BOOL *aReadOnly)
|
---|
1945 | {
|
---|
1946 | autoCaller.release();
|
---|
1947 |
|
---|
1948 | /* isReadOnly() will do locking */
|
---|
1949 | *aReadOnly = i_isReadOnly();
|
---|
1950 |
|
---|
1951 | return S_OK;
|
---|
1952 | }
|
---|
1953 |
|
---|
1954 | HRESULT Medium::getLogicalSize(LONG64 *aLogicalSize)
|
---|
1955 | {
|
---|
1956 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1957 |
|
---|
1958 | *aLogicalSize = m->logicalSize;
|
---|
1959 |
|
---|
1960 | return S_OK;
|
---|
1961 | }
|
---|
1962 |
|
---|
1963 | HRESULT Medium::getAutoReset(BOOL *aAutoReset)
|
---|
1964 | {
|
---|
1965 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1966 |
|
---|
1967 | if (m->pParent.isNull())
|
---|
1968 | *aAutoReset = FALSE;
|
---|
1969 | else
|
---|
1970 | *aAutoReset = m->autoReset;
|
---|
1971 |
|
---|
1972 | return S_OK;
|
---|
1973 | }
|
---|
1974 |
|
---|
1975 | HRESULT Medium::setAutoReset(BOOL aAutoReset)
|
---|
1976 | {
|
---|
1977 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1978 |
|
---|
1979 | if (m->pParent.isNull())
|
---|
1980 | return setError(VBOX_E_NOT_SUPPORTED,
|
---|
1981 | tr("Medium '%s' is not differencing"),
|
---|
1982 | m->strLocationFull.c_str());
|
---|
1983 |
|
---|
1984 | if (m->autoReset != !!aAutoReset)
|
---|
1985 | {
|
---|
1986 | m->autoReset = !!aAutoReset;
|
---|
1987 |
|
---|
1988 | // save the settings
|
---|
1989 | mlock.release();
|
---|
1990 | i_markRegistriesModified();
|
---|
1991 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
1992 | }
|
---|
1993 |
|
---|
1994 | return S_OK;
|
---|
1995 | }
|
---|
1996 |
|
---|
1997 | HRESULT Medium::getLastAccessError(com::Utf8Str &aLastAccessError)
|
---|
1998 | {
|
---|
1999 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2000 |
|
---|
2001 | aLastAccessError = m->strLastAccessError;
|
---|
2002 |
|
---|
2003 | return S_OK;
|
---|
2004 | }
|
---|
2005 |
|
---|
2006 | HRESULT Medium::getMachineIds(std::vector<com::Guid> &aMachineIds)
|
---|
2007 | {
|
---|
2008 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2009 |
|
---|
2010 | if (m->backRefs.size() != 0)
|
---|
2011 | {
|
---|
2012 | BackRefList brlist(m->backRefs);
|
---|
2013 | aMachineIds.resize(brlist.size());
|
---|
2014 | size_t i = 0;
|
---|
2015 | for (BackRefList::const_iterator it = brlist.begin(); it != brlist.end(); ++it, ++i)
|
---|
2016 | aMachineIds[i] = it->machineId;
|
---|
2017 | }
|
---|
2018 |
|
---|
2019 | return S_OK;
|
---|
2020 | }
|
---|
2021 |
|
---|
2022 | HRESULT Medium::setIds(AutoCaller &autoCaller,
|
---|
2023 | BOOL aSetImageId,
|
---|
2024 | const com::Guid &aImageId,
|
---|
2025 | BOOL aSetParentId,
|
---|
2026 | const com::Guid &aParentId)
|
---|
2027 | {
|
---|
2028 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2029 |
|
---|
2030 | switch (m->state)
|
---|
2031 | {
|
---|
2032 | case MediumState_Created:
|
---|
2033 | break;
|
---|
2034 | default:
|
---|
2035 | return i_setStateError();
|
---|
2036 | }
|
---|
2037 |
|
---|
2038 | Guid imageId, parentId;
|
---|
2039 | if (aSetImageId)
|
---|
2040 | {
|
---|
2041 | if (aImageId.isZero())
|
---|
2042 | imageId.create();
|
---|
2043 | else
|
---|
2044 | {
|
---|
2045 | imageId = aImageId;
|
---|
2046 | if (!imageId.isValid())
|
---|
2047 | return setError(E_INVALIDARG, tr("Argument %s is invalid"), "aImageId");
|
---|
2048 | }
|
---|
2049 | }
|
---|
2050 | if (aSetParentId)
|
---|
2051 | {
|
---|
2052 | if (aParentId.isZero())
|
---|
2053 | parentId.create();
|
---|
2054 | else
|
---|
2055 | parentId = aParentId;
|
---|
2056 | }
|
---|
2057 |
|
---|
2058 | unconst(m->uuidImage) = imageId;
|
---|
2059 | unconst(m->uuidParentImage) = parentId;
|
---|
2060 |
|
---|
2061 | // must not hold any locks before calling Medium::i_queryInfo
|
---|
2062 | alock.release();
|
---|
2063 |
|
---|
2064 | HRESULT rc = i_queryInfo(!!aSetImageId /* fSetImageId */,
|
---|
2065 | !!aSetParentId /* fSetParentId */,
|
---|
2066 | autoCaller);
|
---|
2067 |
|
---|
2068 | return rc;
|
---|
2069 | }
|
---|
2070 |
|
---|
2071 | HRESULT Medium::refreshState(AutoCaller &autoCaller, MediumState_T *aState)
|
---|
2072 | {
|
---|
2073 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2074 |
|
---|
2075 | HRESULT rc = S_OK;
|
---|
2076 |
|
---|
2077 | switch (m->state)
|
---|
2078 | {
|
---|
2079 | case MediumState_Created:
|
---|
2080 | case MediumState_Inaccessible:
|
---|
2081 | case MediumState_LockedRead:
|
---|
2082 | {
|
---|
2083 | // must not hold any locks before calling Medium::i_queryInfo
|
---|
2084 | alock.release();
|
---|
2085 |
|
---|
2086 | rc = i_queryInfo(false /* fSetImageId */, false /* fSetParentId */,
|
---|
2087 | autoCaller);
|
---|
2088 |
|
---|
2089 | alock.acquire();
|
---|
2090 | break;
|
---|
2091 | }
|
---|
2092 | default:
|
---|
2093 | break;
|
---|
2094 | }
|
---|
2095 |
|
---|
2096 | *aState = m->state;
|
---|
2097 |
|
---|
2098 | return rc;
|
---|
2099 | }
|
---|
2100 |
|
---|
2101 | HRESULT Medium::getSnapshotIds(const com::Guid &aMachineId,
|
---|
2102 | std::vector<com::Guid> &aSnapshotIds)
|
---|
2103 | {
|
---|
2104 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2105 |
|
---|
2106 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
2107 | it != m->backRefs.end(); ++it)
|
---|
2108 | {
|
---|
2109 | if (it->machineId == aMachineId)
|
---|
2110 | {
|
---|
2111 | size_t size = it->llSnapshotIds.size();
|
---|
2112 |
|
---|
2113 | /* if the medium is attached to the machine in the current state, we
|
---|
2114 | * return its ID as the first element of the array */
|
---|
2115 | if (it->fInCurState)
|
---|
2116 | ++size;
|
---|
2117 |
|
---|
2118 | if (size > 0)
|
---|
2119 | {
|
---|
2120 | aSnapshotIds.resize(size);
|
---|
2121 |
|
---|
2122 | size_t j = 0;
|
---|
2123 | if (it->fInCurState)
|
---|
2124 | aSnapshotIds[j++] = it->machineId.toUtf16();
|
---|
2125 |
|
---|
2126 | for(GuidList::const_iterator jt = it->llSnapshotIds.begin(); jt != it->llSnapshotIds.end(); ++jt, ++j)
|
---|
2127 | aSnapshotIds[j] = (*jt);
|
---|
2128 | }
|
---|
2129 |
|
---|
2130 | break;
|
---|
2131 | }
|
---|
2132 | }
|
---|
2133 |
|
---|
2134 | return S_OK;
|
---|
2135 | }
|
---|
2136 |
|
---|
2137 | HRESULT Medium::lockRead(ComPtr<IToken> &aToken)
|
---|
2138 | {
|
---|
2139 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2140 |
|
---|
2141 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
2142 | if (m->queryInfoRunning)
|
---|
2143 | {
|
---|
2144 | /* Must not hold the media tree lock, as Medium::i_queryInfo needs this
|
---|
2145 | * lock and thus we would run into a deadlock here. */
|
---|
2146 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
2147 | while (m->queryInfoRunning)
|
---|
2148 | {
|
---|
2149 | alock.release();
|
---|
2150 | /* must not hold the object lock now */
|
---|
2151 | Assert(!isWriteLockOnCurrentThread());
|
---|
2152 | {
|
---|
2153 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
2154 | }
|
---|
2155 | alock.acquire();
|
---|
2156 | }
|
---|
2157 | }
|
---|
2158 |
|
---|
2159 | HRESULT rc = S_OK;
|
---|
2160 |
|
---|
2161 | switch (m->state)
|
---|
2162 | {
|
---|
2163 | case MediumState_Created:
|
---|
2164 | case MediumState_Inaccessible:
|
---|
2165 | case MediumState_LockedRead:
|
---|
2166 | {
|
---|
2167 | ++m->readers;
|
---|
2168 |
|
---|
2169 | ComAssertMsgBreak(m->readers != 0, ("Counter overflow"), rc = E_FAIL);
|
---|
2170 |
|
---|
2171 | /* Remember pre-lock state */
|
---|
2172 | if (m->state != MediumState_LockedRead)
|
---|
2173 | m->preLockState = m->state;
|
---|
2174 |
|
---|
2175 | LogFlowThisFunc(("Okay - prev state=%d readers=%d\n", m->state, m->readers));
|
---|
2176 | m->state = MediumState_LockedRead;
|
---|
2177 |
|
---|
2178 | ComObjPtr<MediumLockToken> pToken;
|
---|
2179 | rc = pToken.createObject();
|
---|
2180 | if (SUCCEEDED(rc))
|
---|
2181 | rc = pToken->init(this, false /* fWrite */);
|
---|
2182 | if (FAILED(rc))
|
---|
2183 | {
|
---|
2184 | --m->readers;
|
---|
2185 | if (m->readers == 0)
|
---|
2186 | m->state = m->preLockState;
|
---|
2187 | return rc;
|
---|
2188 | }
|
---|
2189 |
|
---|
2190 | pToken.queryInterfaceTo(aToken.asOutParam());
|
---|
2191 | break;
|
---|
2192 | }
|
---|
2193 | default:
|
---|
2194 | {
|
---|
2195 | LogFlowThisFunc(("Failing - state=%d\n", m->state));
|
---|
2196 | rc = i_setStateError();
|
---|
2197 | break;
|
---|
2198 | }
|
---|
2199 | }
|
---|
2200 |
|
---|
2201 | return rc;
|
---|
2202 | }
|
---|
2203 |
|
---|
2204 | /**
|
---|
2205 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
2206 | * in-process calls).
|
---|
2207 | */
|
---|
2208 | HRESULT Medium::i_unlockRead(MediumState_T *aState)
|
---|
2209 | {
|
---|
2210 | AutoCaller autoCaller(this);
|
---|
2211 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2212 |
|
---|
2213 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2214 |
|
---|
2215 | HRESULT rc = S_OK;
|
---|
2216 |
|
---|
2217 | switch (m->state)
|
---|
2218 | {
|
---|
2219 | case MediumState_LockedRead:
|
---|
2220 | {
|
---|
2221 | ComAssertMsgBreak(m->readers != 0, ("Counter underflow"), rc = E_FAIL);
|
---|
2222 | --m->readers;
|
---|
2223 |
|
---|
2224 | /* Reset the state after the last reader */
|
---|
2225 | if (m->readers == 0)
|
---|
2226 | {
|
---|
2227 | m->state = m->preLockState;
|
---|
2228 | /* There are cases where we inject the deleting state into
|
---|
2229 | * a medium locked for reading. Make sure #unmarkForDeletion()
|
---|
2230 | * gets the right state afterwards. */
|
---|
2231 | if (m->preLockState == MediumState_Deleting)
|
---|
2232 | m->preLockState = MediumState_Created;
|
---|
2233 | }
|
---|
2234 |
|
---|
2235 | LogFlowThisFunc(("new state=%d\n", m->state));
|
---|
2236 | break;
|
---|
2237 | }
|
---|
2238 | default:
|
---|
2239 | {
|
---|
2240 | LogFlowThisFunc(("Failing - state=%d\n", m->state));
|
---|
2241 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2242 | tr("Medium '%s' is not locked for reading"),
|
---|
2243 | m->strLocationFull.c_str());
|
---|
2244 | break;
|
---|
2245 | }
|
---|
2246 | }
|
---|
2247 |
|
---|
2248 | /* return the current state after */
|
---|
2249 | if (aState)
|
---|
2250 | *aState = m->state;
|
---|
2251 |
|
---|
2252 | return rc;
|
---|
2253 | }
|
---|
2254 | HRESULT Medium::lockWrite(ComPtr<IToken> &aToken)
|
---|
2255 | {
|
---|
2256 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2257 |
|
---|
2258 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
2259 | if (m->queryInfoRunning)
|
---|
2260 | {
|
---|
2261 | /* Must not hold the media tree lock, as Medium::i_queryInfo needs this
|
---|
2262 | * lock and thus we would run into a deadlock here. */
|
---|
2263 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
2264 | while (m->queryInfoRunning)
|
---|
2265 | {
|
---|
2266 | alock.release();
|
---|
2267 | /* must not hold the object lock now */
|
---|
2268 | Assert(!isWriteLockOnCurrentThread());
|
---|
2269 | {
|
---|
2270 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
2271 | }
|
---|
2272 | alock.acquire();
|
---|
2273 | }
|
---|
2274 | }
|
---|
2275 |
|
---|
2276 | HRESULT rc = S_OK;
|
---|
2277 |
|
---|
2278 | switch (m->state)
|
---|
2279 | {
|
---|
2280 | case MediumState_Created:
|
---|
2281 | case MediumState_Inaccessible:
|
---|
2282 | {
|
---|
2283 | m->preLockState = m->state;
|
---|
2284 |
|
---|
2285 | LogFlowThisFunc(("Okay - prev state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2286 | m->state = MediumState_LockedWrite;
|
---|
2287 |
|
---|
2288 | ComObjPtr<MediumLockToken> pToken;
|
---|
2289 | rc = pToken.createObject();
|
---|
2290 | if (SUCCEEDED(rc))
|
---|
2291 | rc = pToken->init(this, true /* fWrite */);
|
---|
2292 | if (FAILED(rc))
|
---|
2293 | {
|
---|
2294 | m->state = m->preLockState;
|
---|
2295 | return rc;
|
---|
2296 | }
|
---|
2297 |
|
---|
2298 | pToken.queryInterfaceTo(aToken.asOutParam());
|
---|
2299 | break;
|
---|
2300 | }
|
---|
2301 | default:
|
---|
2302 | {
|
---|
2303 | LogFlowThisFunc(("Failing - state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2304 | rc = i_setStateError();
|
---|
2305 | break;
|
---|
2306 | }
|
---|
2307 | }
|
---|
2308 |
|
---|
2309 | return rc;
|
---|
2310 | }
|
---|
2311 |
|
---|
2312 | /**
|
---|
2313 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
2314 | * in-process calls).
|
---|
2315 | */
|
---|
2316 | HRESULT Medium::i_unlockWrite(MediumState_T *aState)
|
---|
2317 | {
|
---|
2318 | AutoCaller autoCaller(this);
|
---|
2319 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2320 |
|
---|
2321 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2322 |
|
---|
2323 | HRESULT rc = S_OK;
|
---|
2324 |
|
---|
2325 | switch (m->state)
|
---|
2326 | {
|
---|
2327 | case MediumState_LockedWrite:
|
---|
2328 | {
|
---|
2329 | m->state = m->preLockState;
|
---|
2330 | /* There are cases where we inject the deleting state into
|
---|
2331 | * a medium locked for writing. Make sure #unmarkForDeletion()
|
---|
2332 | * gets the right state afterwards. */
|
---|
2333 | if (m->preLockState == MediumState_Deleting)
|
---|
2334 | m->preLockState = MediumState_Created;
|
---|
2335 | LogFlowThisFunc(("new state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2336 | break;
|
---|
2337 | }
|
---|
2338 | default:
|
---|
2339 | {
|
---|
2340 | LogFlowThisFunc(("Failing - state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2341 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2342 | tr("Medium '%s' is not locked for writing"),
|
---|
2343 | m->strLocationFull.c_str());
|
---|
2344 | break;
|
---|
2345 | }
|
---|
2346 | }
|
---|
2347 |
|
---|
2348 | /* return the current state after */
|
---|
2349 | if (aState)
|
---|
2350 | *aState = m->state;
|
---|
2351 |
|
---|
2352 | return rc;
|
---|
2353 | }
|
---|
2354 |
|
---|
2355 | HRESULT Medium::close(AutoCaller &aAutoCaller)
|
---|
2356 | {
|
---|
2357 | // make a copy of VirtualBox pointer which gets nulled by uninit()
|
---|
2358 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
2359 |
|
---|
2360 | MultiResult mrc = i_close(aAutoCaller);
|
---|
2361 |
|
---|
2362 | pVirtualBox->i_saveModifiedRegistries();
|
---|
2363 |
|
---|
2364 | return mrc;
|
---|
2365 | }
|
---|
2366 |
|
---|
2367 | HRESULT Medium::getProperty(const com::Utf8Str &aName,
|
---|
2368 | com::Utf8Str &aValue)
|
---|
2369 | {
|
---|
2370 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2371 |
|
---|
2372 | settings::StringsMap::const_iterator it = m->mapProperties.find(aName);
|
---|
2373 | if (it == m->mapProperties.end())
|
---|
2374 | {
|
---|
2375 | if (!aName.startsWith("Special/"))
|
---|
2376 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2377 | tr("Property '%s' does not exist"), aName.c_str());
|
---|
2378 | else
|
---|
2379 | /* be more silent here */
|
---|
2380 | return VBOX_E_OBJECT_NOT_FOUND;
|
---|
2381 | }
|
---|
2382 |
|
---|
2383 | aValue = it->second;
|
---|
2384 |
|
---|
2385 | return S_OK;
|
---|
2386 | }
|
---|
2387 |
|
---|
2388 | HRESULT Medium::setProperty(const com::Utf8Str &aName,
|
---|
2389 | const com::Utf8Str &aValue)
|
---|
2390 | {
|
---|
2391 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2392 |
|
---|
2393 | switch (m->state)
|
---|
2394 | {
|
---|
2395 | case MediumState_Created:
|
---|
2396 | case MediumState_Inaccessible:
|
---|
2397 | break;
|
---|
2398 | default:
|
---|
2399 | return i_setStateError();
|
---|
2400 | }
|
---|
2401 |
|
---|
2402 | settings::StringsMap::iterator it = m->mapProperties.find(aName);
|
---|
2403 | if ( !aName.startsWith("Special/")
|
---|
2404 | && !i_isPropertyForFilter(aName))
|
---|
2405 | {
|
---|
2406 | if (it == m->mapProperties.end())
|
---|
2407 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2408 | tr("Property '%s' does not exist"),
|
---|
2409 | aName.c_str());
|
---|
2410 | it->second = aValue;
|
---|
2411 | }
|
---|
2412 | else
|
---|
2413 | {
|
---|
2414 | if (it == m->mapProperties.end())
|
---|
2415 | {
|
---|
2416 | if (!aValue.isEmpty())
|
---|
2417 | m->mapProperties[aName] = aValue;
|
---|
2418 | }
|
---|
2419 | else
|
---|
2420 | {
|
---|
2421 | if (!aValue.isEmpty())
|
---|
2422 | it->second = aValue;
|
---|
2423 | else
|
---|
2424 | m->mapProperties.erase(it);
|
---|
2425 | }
|
---|
2426 | }
|
---|
2427 |
|
---|
2428 | // save the settings
|
---|
2429 | mlock.release();
|
---|
2430 | i_markRegistriesModified();
|
---|
2431 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2432 |
|
---|
2433 | return S_OK;
|
---|
2434 | }
|
---|
2435 |
|
---|
2436 | HRESULT Medium::getProperties(const com::Utf8Str &aNames,
|
---|
2437 | std::vector<com::Utf8Str> &aReturnNames,
|
---|
2438 | std::vector<com::Utf8Str> &aReturnValues)
|
---|
2439 | {
|
---|
2440 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2441 |
|
---|
2442 | /// @todo make use of aNames according to the documentation
|
---|
2443 | NOREF(aNames);
|
---|
2444 |
|
---|
2445 | aReturnNames.resize(m->mapProperties.size());
|
---|
2446 | aReturnValues.resize(m->mapProperties.size());
|
---|
2447 | size_t i = 0;
|
---|
2448 | for (settings::StringsMap::const_iterator it = m->mapProperties.begin();
|
---|
2449 | it != m->mapProperties.end();
|
---|
2450 | ++it, ++i)
|
---|
2451 | {
|
---|
2452 | aReturnNames[i] = it->first;
|
---|
2453 | aReturnValues[i] = it->second;
|
---|
2454 | }
|
---|
2455 | return S_OK;
|
---|
2456 | }
|
---|
2457 |
|
---|
2458 | HRESULT Medium::setProperties(const std::vector<com::Utf8Str> &aNames,
|
---|
2459 | const std::vector<com::Utf8Str> &aValues)
|
---|
2460 | {
|
---|
2461 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2462 |
|
---|
2463 | /* first pass: validate names */
|
---|
2464 | for (size_t i = 0;
|
---|
2465 | i < aNames.size();
|
---|
2466 | ++i)
|
---|
2467 | {
|
---|
2468 | Utf8Str strName(aNames[i]);
|
---|
2469 | if ( !strName.startsWith("Special/")
|
---|
2470 | && !i_isPropertyForFilter(strName)
|
---|
2471 | && m->mapProperties.find(strName) == m->mapProperties.end())
|
---|
2472 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2473 | tr("Property '%s' does not exist"), strName.c_str());
|
---|
2474 | }
|
---|
2475 |
|
---|
2476 | /* second pass: assign */
|
---|
2477 | for (size_t i = 0;
|
---|
2478 | i < aNames.size();
|
---|
2479 | ++i)
|
---|
2480 | {
|
---|
2481 | Utf8Str strName(aNames[i]);
|
---|
2482 | Utf8Str strValue(aValues[i]);
|
---|
2483 | settings::StringsMap::iterator it = m->mapProperties.find(strName);
|
---|
2484 | if ( !strName.startsWith("Special/")
|
---|
2485 | && !i_isPropertyForFilter(strName))
|
---|
2486 | {
|
---|
2487 | AssertReturn(it != m->mapProperties.end(), E_FAIL);
|
---|
2488 | it->second = strValue;
|
---|
2489 | }
|
---|
2490 | else
|
---|
2491 | {
|
---|
2492 | if (it == m->mapProperties.end())
|
---|
2493 | {
|
---|
2494 | if (!strValue.isEmpty())
|
---|
2495 | m->mapProperties[strName] = strValue;
|
---|
2496 | }
|
---|
2497 | else
|
---|
2498 | {
|
---|
2499 | if (!strValue.isEmpty())
|
---|
2500 | it->second = strValue;
|
---|
2501 | else
|
---|
2502 | m->mapProperties.erase(it);
|
---|
2503 | }
|
---|
2504 | }
|
---|
2505 | }
|
---|
2506 |
|
---|
2507 | // save the settings
|
---|
2508 | mlock.release();
|
---|
2509 | i_markRegistriesModified();
|
---|
2510 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2511 |
|
---|
2512 | return S_OK;
|
---|
2513 | }
|
---|
2514 | HRESULT Medium::createBaseStorage(LONG64 aLogicalSize,
|
---|
2515 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2516 | ComPtr<IProgress> &aProgress)
|
---|
2517 | {
|
---|
2518 | if (aLogicalSize < 0)
|
---|
2519 | return setError(E_INVALIDARG, tr("The medium size argument (%lld) is negative"), aLogicalSize);
|
---|
2520 |
|
---|
2521 | HRESULT rc = S_OK;
|
---|
2522 | ComObjPtr<Progress> pProgress;
|
---|
2523 | Medium::Task *pTask = NULL;
|
---|
2524 |
|
---|
2525 | try
|
---|
2526 | {
|
---|
2527 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2528 |
|
---|
2529 | ULONG mediumVariantFlags = 0;
|
---|
2530 |
|
---|
2531 | if (aVariant.size())
|
---|
2532 | {
|
---|
2533 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
2534 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
2535 | }
|
---|
2536 |
|
---|
2537 | mediumVariantFlags &= ((unsigned)~MediumVariant_Diff);
|
---|
2538 |
|
---|
2539 | if ( !(mediumVariantFlags & MediumVariant_Fixed)
|
---|
2540 | && !(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_CreateDynamic))
|
---|
2541 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2542 | tr("Medium format '%s' does not support dynamic storage creation"),
|
---|
2543 | m->strFormat.c_str());
|
---|
2544 |
|
---|
2545 | if ( (mediumVariantFlags & MediumVariant_Fixed)
|
---|
2546 | && !(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_CreateFixed))
|
---|
2547 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2548 | tr("Medium format '%s' does not support fixed storage creation"),
|
---|
2549 | m->strFormat.c_str());
|
---|
2550 |
|
---|
2551 | if ( (mediumVariantFlags & MediumVariant_Formatted)
|
---|
2552 | && i_getDeviceType() != DeviceType_Floppy)
|
---|
2553 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2554 | tr("Medium variant 'formatted' applies to floppy images only"));
|
---|
2555 |
|
---|
2556 | if (m->state != MediumState_NotCreated)
|
---|
2557 | throw i_setStateError();
|
---|
2558 |
|
---|
2559 | pProgress.createObject();
|
---|
2560 | rc = pProgress->init(m->pVirtualBox,
|
---|
2561 | static_cast<IMedium*>(this),
|
---|
2562 | (mediumVariantFlags & MediumVariant_Fixed)
|
---|
2563 | ? BstrFmt(tr("Creating fixed medium storage unit '%s'"), m->strLocationFull.c_str()).raw()
|
---|
2564 | : BstrFmt(tr("Creating dynamic medium storage unit '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
2565 | TRUE /* aCancelable */);
|
---|
2566 | if (FAILED(rc))
|
---|
2567 | throw rc;
|
---|
2568 |
|
---|
2569 | /* setup task object to carry out the operation asynchronously */
|
---|
2570 | pTask = new Medium::CreateBaseTask(this, pProgress, aLogicalSize,
|
---|
2571 | (MediumVariant_T)mediumVariantFlags);
|
---|
2572 | rc = pTask->rc();
|
---|
2573 | AssertComRC(rc);
|
---|
2574 | if (FAILED(rc))
|
---|
2575 | throw rc;
|
---|
2576 |
|
---|
2577 | m->state = MediumState_Creating;
|
---|
2578 | }
|
---|
2579 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2580 |
|
---|
2581 | if (SUCCEEDED(rc))
|
---|
2582 | {
|
---|
2583 | rc = pTask->createThread();
|
---|
2584 |
|
---|
2585 | if (SUCCEEDED(rc))
|
---|
2586 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2587 | }
|
---|
2588 | else if (pTask != NULL)
|
---|
2589 | delete pTask;
|
---|
2590 |
|
---|
2591 | return rc;
|
---|
2592 | }
|
---|
2593 |
|
---|
2594 | HRESULT Medium::deleteStorage(ComPtr<IProgress> &aProgress)
|
---|
2595 | {
|
---|
2596 | ComObjPtr<Progress> pProgress;
|
---|
2597 |
|
---|
2598 | MultiResult mrc = i_deleteStorage(&pProgress,
|
---|
2599 | false /* aWait */);
|
---|
2600 | /* Must save the registries in any case, since an entry was removed. */
|
---|
2601 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2602 |
|
---|
2603 | if (SUCCEEDED(mrc))
|
---|
2604 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2605 |
|
---|
2606 | return mrc;
|
---|
2607 | }
|
---|
2608 |
|
---|
2609 | HRESULT Medium::createDiffStorage(AutoCaller &autoCaller,
|
---|
2610 | const ComPtr<IMedium> &aTarget,
|
---|
2611 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2612 | ComPtr<IProgress> &aProgress)
|
---|
2613 | {
|
---|
2614 | IMedium *aT = aTarget;
|
---|
2615 | ComObjPtr<Medium> diff = static_cast<Medium*>(aT);
|
---|
2616 |
|
---|
2617 | autoCaller.release();
|
---|
2618 |
|
---|
2619 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
2620 | * the pVirtualBox reference, see #uninit(). */
|
---|
2621 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
2622 |
|
---|
2623 | // we access m->pParent
|
---|
2624 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
2625 |
|
---|
2626 | autoCaller.add();
|
---|
2627 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2628 |
|
---|
2629 | AutoMultiWriteLock2 alock(this, diff COMMA_LOCKVAL_SRC_POS);
|
---|
2630 |
|
---|
2631 | if (m->type == MediumType_Writethrough)
|
---|
2632 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2633 | tr("Medium type of '%s' is Writethrough"),
|
---|
2634 | m->strLocationFull.c_str());
|
---|
2635 | else if (m->type == MediumType_Shareable)
|
---|
2636 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2637 | tr("Medium type of '%s' is Shareable"),
|
---|
2638 | m->strLocationFull.c_str());
|
---|
2639 | else if (m->type == MediumType_Readonly)
|
---|
2640 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2641 | tr("Medium type of '%s' is Readonly"),
|
---|
2642 | m->strLocationFull.c_str());
|
---|
2643 |
|
---|
2644 | /* Apply the normal locking logic to the entire chain. */
|
---|
2645 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
2646 | alock.release();
|
---|
2647 | autoCaller.release();
|
---|
2648 | treeLock.release();
|
---|
2649 | HRESULT rc = diff->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2650 | diff /* pToLockWrite */,
|
---|
2651 | false /* fMediumLockWriteAll */,
|
---|
2652 | this,
|
---|
2653 | *pMediumLockList);
|
---|
2654 | treeLock.acquire();
|
---|
2655 | autoCaller.add();
|
---|
2656 | if (FAILED(autoCaller.rc()))
|
---|
2657 | rc = autoCaller.rc();
|
---|
2658 | alock.acquire();
|
---|
2659 | if (FAILED(rc))
|
---|
2660 | {
|
---|
2661 | delete pMediumLockList;
|
---|
2662 | return rc;
|
---|
2663 | }
|
---|
2664 |
|
---|
2665 | alock.release();
|
---|
2666 | autoCaller.release();
|
---|
2667 | treeLock.release();
|
---|
2668 | rc = pMediumLockList->Lock();
|
---|
2669 | treeLock.acquire();
|
---|
2670 | autoCaller.add();
|
---|
2671 | if (FAILED(autoCaller.rc()))
|
---|
2672 | rc = autoCaller.rc();
|
---|
2673 | alock.acquire();
|
---|
2674 | if (FAILED(rc))
|
---|
2675 | {
|
---|
2676 | delete pMediumLockList;
|
---|
2677 |
|
---|
2678 | return setError(rc, tr("Could not lock medium when creating diff '%s'"),
|
---|
2679 | diff->i_getLocationFull().c_str());
|
---|
2680 | }
|
---|
2681 |
|
---|
2682 | Guid parentMachineRegistry;
|
---|
2683 | if (i_getFirstRegistryMachineId(parentMachineRegistry))
|
---|
2684 | {
|
---|
2685 | /* since this medium has been just created it isn't associated yet */
|
---|
2686 | diff->m->llRegistryIDs.push_back(parentMachineRegistry);
|
---|
2687 | alock.release();
|
---|
2688 | autoCaller.release();
|
---|
2689 | treeLock.release();
|
---|
2690 | diff->i_markRegistriesModified();
|
---|
2691 | treeLock.acquire();
|
---|
2692 | autoCaller.add();
|
---|
2693 | alock.acquire();
|
---|
2694 | }
|
---|
2695 |
|
---|
2696 | alock.release();
|
---|
2697 | autoCaller.release();
|
---|
2698 | treeLock.release();
|
---|
2699 |
|
---|
2700 | ComObjPtr<Progress> pProgress;
|
---|
2701 |
|
---|
2702 | ULONG mediumVariantFlags = 0;
|
---|
2703 |
|
---|
2704 | if (aVariant.size())
|
---|
2705 | {
|
---|
2706 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
2707 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
2708 | }
|
---|
2709 |
|
---|
2710 | if (mediumVariantFlags & MediumVariant_Formatted)
|
---|
2711 | {
|
---|
2712 | delete pMediumLockList;
|
---|
2713 | return setError(VBOX_E_NOT_SUPPORTED,
|
---|
2714 | tr("Medium variant 'formatted' applies to floppy images only"));
|
---|
2715 | }
|
---|
2716 |
|
---|
2717 | rc = i_createDiffStorage(diff, (MediumVariant_T)mediumVariantFlags, pMediumLockList,
|
---|
2718 | &pProgress, false /* aWait */);
|
---|
2719 | if (FAILED(rc))
|
---|
2720 | delete pMediumLockList;
|
---|
2721 | else
|
---|
2722 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2723 |
|
---|
2724 | return rc;
|
---|
2725 | }
|
---|
2726 |
|
---|
2727 | HRESULT Medium::mergeTo(const ComPtr<IMedium> &aTarget,
|
---|
2728 | ComPtr<IProgress> &aProgress)
|
---|
2729 | {
|
---|
2730 | IMedium *aT = aTarget;
|
---|
2731 |
|
---|
2732 | ComAssertRet(aT != this, E_INVALIDARG);
|
---|
2733 |
|
---|
2734 | ComObjPtr<Medium> pTarget = static_cast<Medium*>(aT);
|
---|
2735 |
|
---|
2736 | bool fMergeForward = false;
|
---|
2737 | ComObjPtr<Medium> pParentForTarget;
|
---|
2738 | MediumLockList *pChildrenToReparent = NULL;
|
---|
2739 | MediumLockList *pMediumLockList = NULL;
|
---|
2740 |
|
---|
2741 | HRESULT rc = S_OK;
|
---|
2742 |
|
---|
2743 | rc = i_prepareMergeTo(pTarget, NULL, NULL, true, fMergeForward,
|
---|
2744 | pParentForTarget, pChildrenToReparent, pMediumLockList);
|
---|
2745 | if (FAILED(rc)) return rc;
|
---|
2746 |
|
---|
2747 | ComObjPtr<Progress> pProgress;
|
---|
2748 |
|
---|
2749 | rc = i_mergeTo(pTarget, fMergeForward, pParentForTarget, pChildrenToReparent,
|
---|
2750 | pMediumLockList, &pProgress, false /* aWait */);
|
---|
2751 | if (FAILED(rc))
|
---|
2752 | i_cancelMergeTo(pChildrenToReparent, pMediumLockList);
|
---|
2753 | else
|
---|
2754 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2755 |
|
---|
2756 | return rc;
|
---|
2757 | }
|
---|
2758 |
|
---|
2759 | HRESULT Medium::cloneToBase(const ComPtr<IMedium> &aTarget,
|
---|
2760 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2761 | ComPtr<IProgress> &aProgress)
|
---|
2762 | {
|
---|
2763 | int rc = S_OK;
|
---|
2764 |
|
---|
2765 | rc = cloneTo(aTarget, aVariant, NULL, aProgress);
|
---|
2766 | return rc;
|
---|
2767 | }
|
---|
2768 |
|
---|
2769 | HRESULT Medium::cloneTo(const ComPtr<IMedium> &aTarget,
|
---|
2770 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2771 | const ComPtr<IMedium> &aParent,
|
---|
2772 | ComPtr<IProgress> &aProgress)
|
---|
2773 | {
|
---|
2774 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
2775 | * to lock order violations, it probably causes lock order issues related
|
---|
2776 | * to the AutoCaller usage. */
|
---|
2777 | ComAssertRet(aTarget != this, E_INVALIDARG);
|
---|
2778 |
|
---|
2779 | IMedium *aT = aTarget;
|
---|
2780 | ComObjPtr<Medium> pTarget = static_cast<Medium*>(aT);
|
---|
2781 | ComObjPtr<Medium> pParent;
|
---|
2782 | if (aParent)
|
---|
2783 | {
|
---|
2784 | IMedium *aP = aParent;
|
---|
2785 | pParent = static_cast<Medium*>(aP);
|
---|
2786 | }
|
---|
2787 |
|
---|
2788 | HRESULT rc = S_OK;
|
---|
2789 | ComObjPtr<Progress> pProgress;
|
---|
2790 | Medium::Task *pTask = NULL;
|
---|
2791 |
|
---|
2792 | try
|
---|
2793 | {
|
---|
2794 | // locking: we need the tree lock first because we access parent pointers
|
---|
2795 | // and we need to write-lock the media involved
|
---|
2796 | uint32_t cHandles = 3;
|
---|
2797 | LockHandle* pHandles[4] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
2798 | this->lockHandle(),
|
---|
2799 | pTarget->lockHandle() };
|
---|
2800 | /* Only add parent to the lock if it is not null */
|
---|
2801 | if (!pParent.isNull())
|
---|
2802 | pHandles[cHandles++] = pParent->lockHandle();
|
---|
2803 | AutoWriteLock alock(cHandles,
|
---|
2804 | pHandles
|
---|
2805 | COMMA_LOCKVAL_SRC_POS);
|
---|
2806 |
|
---|
2807 | if ( pTarget->m->state != MediumState_NotCreated
|
---|
2808 | && pTarget->m->state != MediumState_Created)
|
---|
2809 | throw pTarget->i_setStateError();
|
---|
2810 |
|
---|
2811 | /* Build the source lock list. */
|
---|
2812 | MediumLockList *pSourceMediumLockList(new MediumLockList());
|
---|
2813 | alock.release();
|
---|
2814 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2815 | NULL /* pToLockWrite */,
|
---|
2816 | false /* fMediumLockWriteAll */,
|
---|
2817 | NULL,
|
---|
2818 | *pSourceMediumLockList);
|
---|
2819 | alock.acquire();
|
---|
2820 | if (FAILED(rc))
|
---|
2821 | {
|
---|
2822 | delete pSourceMediumLockList;
|
---|
2823 | throw rc;
|
---|
2824 | }
|
---|
2825 |
|
---|
2826 | /* Build the target lock list (including the to-be parent chain). */
|
---|
2827 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
2828 | alock.release();
|
---|
2829 | rc = pTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2830 | pTarget /* pToLockWrite */,
|
---|
2831 | false /* fMediumLockWriteAll */,
|
---|
2832 | pParent,
|
---|
2833 | *pTargetMediumLockList);
|
---|
2834 | alock.acquire();
|
---|
2835 | if (FAILED(rc))
|
---|
2836 | {
|
---|
2837 | delete pSourceMediumLockList;
|
---|
2838 | delete pTargetMediumLockList;
|
---|
2839 | throw rc;
|
---|
2840 | }
|
---|
2841 |
|
---|
2842 | alock.release();
|
---|
2843 | rc = pSourceMediumLockList->Lock();
|
---|
2844 | alock.acquire();
|
---|
2845 | if (FAILED(rc))
|
---|
2846 | {
|
---|
2847 | delete pSourceMediumLockList;
|
---|
2848 | delete pTargetMediumLockList;
|
---|
2849 | throw setError(rc,
|
---|
2850 | tr("Failed to lock source media '%s'"),
|
---|
2851 | i_getLocationFull().c_str());
|
---|
2852 | }
|
---|
2853 | alock.release();
|
---|
2854 | rc = pTargetMediumLockList->Lock();
|
---|
2855 | alock.acquire();
|
---|
2856 | if (FAILED(rc))
|
---|
2857 | {
|
---|
2858 | delete pSourceMediumLockList;
|
---|
2859 | delete pTargetMediumLockList;
|
---|
2860 | throw setError(rc,
|
---|
2861 | tr("Failed to lock target media '%s'"),
|
---|
2862 | pTarget->i_getLocationFull().c_str());
|
---|
2863 | }
|
---|
2864 |
|
---|
2865 | pProgress.createObject();
|
---|
2866 | rc = pProgress->init(m->pVirtualBox,
|
---|
2867 | static_cast <IMedium *>(this),
|
---|
2868 | BstrFmt(tr("Creating clone medium '%s'"), pTarget->m->strLocationFull.c_str()).raw(),
|
---|
2869 | TRUE /* aCancelable */);
|
---|
2870 | if (FAILED(rc))
|
---|
2871 | {
|
---|
2872 | delete pSourceMediumLockList;
|
---|
2873 | delete pTargetMediumLockList;
|
---|
2874 | throw rc;
|
---|
2875 | }
|
---|
2876 |
|
---|
2877 | ULONG mediumVariantFlags = 0;
|
---|
2878 |
|
---|
2879 | if (aVariant.size())
|
---|
2880 | {
|
---|
2881 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
2882 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
2883 | }
|
---|
2884 |
|
---|
2885 | if (mediumVariantFlags & MediumVariant_Formatted)
|
---|
2886 | {
|
---|
2887 | delete pSourceMediumLockList;
|
---|
2888 | delete pTargetMediumLockList;
|
---|
2889 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2890 | tr("Medium variant 'formatted' applies to floppy images only"));
|
---|
2891 | }
|
---|
2892 |
|
---|
2893 | /* setup task object to carry out the operation asynchronously */
|
---|
2894 | pTask = new Medium::CloneTask(this, pProgress, pTarget,
|
---|
2895 | (MediumVariant_T)mediumVariantFlags,
|
---|
2896 | pParent, UINT32_MAX, UINT32_MAX,
|
---|
2897 | pSourceMediumLockList, pTargetMediumLockList);
|
---|
2898 | rc = pTask->rc();
|
---|
2899 | AssertComRC(rc);
|
---|
2900 | if (FAILED(rc))
|
---|
2901 | throw rc;
|
---|
2902 |
|
---|
2903 | if (pTarget->m->state == MediumState_NotCreated)
|
---|
2904 | pTarget->m->state = MediumState_Creating;
|
---|
2905 | }
|
---|
2906 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2907 |
|
---|
2908 | if (SUCCEEDED(rc))
|
---|
2909 | {
|
---|
2910 | rc = pTask->createThread();
|
---|
2911 |
|
---|
2912 | if (SUCCEEDED(rc))
|
---|
2913 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2914 | }
|
---|
2915 | else if (pTask != NULL)
|
---|
2916 | delete pTask;
|
---|
2917 |
|
---|
2918 | return rc;
|
---|
2919 | }
|
---|
2920 |
|
---|
2921 | HRESULT Medium::moveTo(AutoCaller &autoCaller, const com::Utf8Str &aLocation, ComPtr<IProgress> &aProgress)
|
---|
2922 | {
|
---|
2923 | ComObjPtr<Medium> pParent;
|
---|
2924 | ComObjPtr<Progress> pProgress;
|
---|
2925 | HRESULT rc = S_OK;
|
---|
2926 | Medium::Task *pTask = NULL;
|
---|
2927 |
|
---|
2928 | try
|
---|
2929 | {
|
---|
2930 | /// @todo NEWMEDIA for file names, add the default extension if no extension
|
---|
2931 | /// is present (using the information from the VD backend which also implies
|
---|
2932 | /// that one more parameter should be passed to moveTo() requesting
|
---|
2933 | /// that functionality since it is only allowed when called from this method
|
---|
2934 |
|
---|
2935 | /// @todo NEWMEDIA rename the file and set m->location on success, then save
|
---|
2936 | /// the global registry (and local registries of portable VMs referring to
|
---|
2937 | /// this medium), this will also require to add the mRegistered flag to data
|
---|
2938 |
|
---|
2939 | autoCaller.release();
|
---|
2940 |
|
---|
2941 | // locking: we need the tree lock first because we access parent pointers
|
---|
2942 | // and we need to write-lock the media involved
|
---|
2943 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
2944 |
|
---|
2945 | autoCaller.add();
|
---|
2946 | AssertComRCThrowRC(autoCaller.rc());
|
---|
2947 |
|
---|
2948 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2949 |
|
---|
2950 | /* play with locations */
|
---|
2951 | {
|
---|
2952 | /* get source path and filename */
|
---|
2953 | Utf8Str sourcePath = i_getLocationFull();
|
---|
2954 | Utf8Str sourceFName = i_getName();
|
---|
2955 |
|
---|
2956 | if (aLocation.isEmpty())
|
---|
2957 | {
|
---|
2958 | rc = setError(VERR_PATH_ZERO_LENGTH,
|
---|
2959 | tr("Medium '%s' can't be moved. Destination path is empty."),
|
---|
2960 | i_getLocationFull().c_str());
|
---|
2961 | throw rc;
|
---|
2962 | }
|
---|
2963 |
|
---|
2964 | /* extract destination path and filename */
|
---|
2965 | Utf8Str destPath(aLocation);
|
---|
2966 | Utf8Str destFName(destPath);
|
---|
2967 | destFName.stripPath();
|
---|
2968 |
|
---|
2969 | Utf8Str suffix(destFName);
|
---|
2970 | suffix.stripSuffix();
|
---|
2971 |
|
---|
2972 | if (suffix.equals(destFName) && !destFName.isEmpty())
|
---|
2973 | {
|
---|
2974 | /*
|
---|
2975 | * The target path has no filename: Either "/path/to/new/location" or
|
---|
2976 | * just "newname" (no trailing backslash or there is no filename with
|
---|
2977 | * extension(suffix)).
|
---|
2978 | */
|
---|
2979 | if (destPath.equals(destFName))
|
---|
2980 | {
|
---|
2981 | /* new path contains only "newname", no path, no extension */
|
---|
2982 | destFName.append(RTPathSuffix(sourceFName.c_str()));
|
---|
2983 | destPath = destFName;
|
---|
2984 | }
|
---|
2985 | else
|
---|
2986 | {
|
---|
2987 | /* new path looks like "/path/to/new/location" */
|
---|
2988 | destFName.setNull();
|
---|
2989 | destPath.append(RTPATH_SLASH);
|
---|
2990 | }
|
---|
2991 | }
|
---|
2992 |
|
---|
2993 | if (destFName.isEmpty())
|
---|
2994 | {
|
---|
2995 | /* No target name */
|
---|
2996 | destPath.append(sourceFName);
|
---|
2997 | }
|
---|
2998 | else
|
---|
2999 | {
|
---|
3000 | if (destPath.equals(destFName))
|
---|
3001 | {
|
---|
3002 | /*
|
---|
3003 | * The target path contains of only a filename without a directory.
|
---|
3004 | * Move the medium within the source directory to the new name
|
---|
3005 | * (actually rename operation).
|
---|
3006 | * Scratches sourcePath!
|
---|
3007 | */
|
---|
3008 | destPath = sourcePath.stripFilename().append(RTPATH_SLASH).append(destFName);
|
---|
3009 | }
|
---|
3010 | suffix = i_getFormat();
|
---|
3011 | if (suffix.compare("RAW", Utf8Str::CaseInsensitive) == 0)
|
---|
3012 | {
|
---|
3013 | DeviceType_T devType = i_getDeviceType();
|
---|
3014 | switch (devType)
|
---|
3015 | {
|
---|
3016 | case DeviceType_DVD:
|
---|
3017 | suffix = "iso";
|
---|
3018 | break;
|
---|
3019 | case DeviceType_Floppy:
|
---|
3020 | suffix = "img";
|
---|
3021 | break;
|
---|
3022 | default:
|
---|
3023 | rc = setError(VERR_NOT_A_FILE,
|
---|
3024 | tr("Medium '%s' has RAW type. \"Move\" operation isn't supported for this type."),
|
---|
3025 | i_getLocationFull().c_str());
|
---|
3026 | throw rc;
|
---|
3027 | }
|
---|
3028 | }
|
---|
3029 | else if (suffix.compare("Parallels", Utf8Str::CaseInsensitive) == 0)
|
---|
3030 | {
|
---|
3031 | suffix = "hdd";
|
---|
3032 | }
|
---|
3033 |
|
---|
3034 | /* Set the target extension like on the source. Any conversions are prohibited */
|
---|
3035 | suffix.toLower();
|
---|
3036 | destPath.stripSuffix().append('.').append(suffix);
|
---|
3037 | }
|
---|
3038 |
|
---|
3039 | /* Simple check for existence */
|
---|
3040 | if (RTFileExists(destPath.c_str()))
|
---|
3041 | {
|
---|
3042 | rc = setError(VBOX_E_FILE_ERROR,
|
---|
3043 | tr("The given path '%s' is an existing file. Delete or rename this file."),
|
---|
3044 | destPath.c_str());
|
---|
3045 | throw rc;
|
---|
3046 | }
|
---|
3047 |
|
---|
3048 | if (!i_isMediumFormatFile())
|
---|
3049 | {
|
---|
3050 | rc = setError(VERR_NOT_A_FILE,
|
---|
3051 | tr("Medium '%s' isn't a file object. \"Move\" operation isn't supported."),
|
---|
3052 | i_getLocationFull().c_str());
|
---|
3053 | throw rc;
|
---|
3054 | }
|
---|
3055 | /* Path must be absolute */
|
---|
3056 | if (!RTPathStartsWithRoot(destPath.c_str()))
|
---|
3057 | {
|
---|
3058 | rc = setError(VBOX_E_FILE_ERROR,
|
---|
3059 | tr("The given path '%s' is not fully qualified"),
|
---|
3060 | destPath.c_str());
|
---|
3061 | throw rc;
|
---|
3062 | }
|
---|
3063 | /* Check path for a new file object */
|
---|
3064 | rc = VirtualBox::i_ensureFilePathExists(destPath, true);
|
---|
3065 | if (FAILED(rc))
|
---|
3066 | throw rc;
|
---|
3067 |
|
---|
3068 | /* Set needed variables for "moving" procedure. It'll be used later in separate thread task */
|
---|
3069 | rc = i_preparationForMoving(destPath);
|
---|
3070 | if (FAILED(rc))
|
---|
3071 | {
|
---|
3072 | rc = setError(VERR_NO_CHANGE,
|
---|
3073 | tr("Medium '%s' is already in the correct location"),
|
---|
3074 | i_getLocationFull().c_str());
|
---|
3075 | throw rc;
|
---|
3076 | }
|
---|
3077 | }
|
---|
3078 |
|
---|
3079 | /* Check VMs which have this medium attached to*/
|
---|
3080 | std::vector<com::Guid> aMachineIds;
|
---|
3081 | rc = getMachineIds(aMachineIds);
|
---|
3082 | std::vector<com::Guid>::const_iterator currMachineID = aMachineIds.begin();
|
---|
3083 | std::vector<com::Guid>::const_iterator lastMachineID = aMachineIds.end();
|
---|
3084 |
|
---|
3085 | while (currMachineID != lastMachineID)
|
---|
3086 | {
|
---|
3087 | Guid id(*currMachineID);
|
---|
3088 | ComObjPtr<Machine> aMachine;
|
---|
3089 |
|
---|
3090 | alock.release();
|
---|
3091 | autoCaller.release();
|
---|
3092 | treeLock.release();
|
---|
3093 | rc = m->pVirtualBox->i_findMachine(id, false, true, &aMachine);
|
---|
3094 | treeLock.acquire();
|
---|
3095 | autoCaller.add();
|
---|
3096 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3097 | alock.acquire();
|
---|
3098 |
|
---|
3099 | if (SUCCEEDED(rc))
|
---|
3100 | {
|
---|
3101 | ComObjPtr<SessionMachine> sm;
|
---|
3102 | ComPtr<IInternalSessionControl> ctl;
|
---|
3103 |
|
---|
3104 | alock.release();
|
---|
3105 | autoCaller.release();
|
---|
3106 | treeLock.release();
|
---|
3107 | bool ses = aMachine->i_isSessionOpenVM(sm, &ctl);
|
---|
3108 | treeLock.acquire();
|
---|
3109 | autoCaller.add();
|
---|
3110 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3111 | alock.acquire();
|
---|
3112 |
|
---|
3113 | if (ses)
|
---|
3114 | {
|
---|
3115 | rc = setError(VERR_VM_UNEXPECTED_VM_STATE,
|
---|
3116 | tr("At least the VM '%s' to whom this medium '%s' attached has currently an opened session. Stop all VMs before relocating this medium"),
|
---|
3117 | id.toString().c_str(),
|
---|
3118 | i_getLocationFull().c_str());
|
---|
3119 | throw rc;
|
---|
3120 | }
|
---|
3121 | }
|
---|
3122 | ++currMachineID;
|
---|
3123 | }
|
---|
3124 |
|
---|
3125 | /* Build the source lock list. */
|
---|
3126 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3127 | alock.release();
|
---|
3128 | autoCaller.release();
|
---|
3129 | treeLock.release();
|
---|
3130 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
3131 | this /* pToLockWrite */,
|
---|
3132 | true /* fMediumLockWriteAll */,
|
---|
3133 | NULL,
|
---|
3134 | *pMediumLockList);
|
---|
3135 | treeLock.acquire();
|
---|
3136 | autoCaller.add();
|
---|
3137 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3138 | alock.acquire();
|
---|
3139 | if (FAILED(rc))
|
---|
3140 | {
|
---|
3141 | delete pMediumLockList;
|
---|
3142 | throw setError(rc,
|
---|
3143 | tr("Failed to create medium lock list for '%s'"),
|
---|
3144 | i_getLocationFull().c_str());
|
---|
3145 | }
|
---|
3146 | alock.release();
|
---|
3147 | autoCaller.release();
|
---|
3148 | treeLock.release();
|
---|
3149 | rc = pMediumLockList->Lock();
|
---|
3150 | treeLock.acquire();
|
---|
3151 | autoCaller.add();
|
---|
3152 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3153 | alock.acquire();
|
---|
3154 | if (FAILED(rc))
|
---|
3155 | {
|
---|
3156 | delete pMediumLockList;
|
---|
3157 | throw setError(rc,
|
---|
3158 | tr("Failed to lock media '%s'"),
|
---|
3159 | i_getLocationFull().c_str());
|
---|
3160 | }
|
---|
3161 |
|
---|
3162 | pProgress.createObject();
|
---|
3163 | rc = pProgress->init(m->pVirtualBox,
|
---|
3164 | static_cast <IMedium *>(this),
|
---|
3165 | BstrFmt(tr("Moving medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3166 | TRUE /* aCancelable */);
|
---|
3167 |
|
---|
3168 | /* Do the disk moving. */
|
---|
3169 | if (SUCCEEDED(rc))
|
---|
3170 | {
|
---|
3171 | ULONG mediumVariantFlags = i_getVariant();
|
---|
3172 |
|
---|
3173 | /* setup task object to carry out the operation asynchronously */
|
---|
3174 | pTask = new Medium::MoveTask(this, pProgress,
|
---|
3175 | (MediumVariant_T)mediumVariantFlags,
|
---|
3176 | pMediumLockList);
|
---|
3177 | rc = pTask->rc();
|
---|
3178 | AssertComRC(rc);
|
---|
3179 | if (FAILED(rc))
|
---|
3180 | throw rc;
|
---|
3181 | }
|
---|
3182 |
|
---|
3183 | }
|
---|
3184 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3185 |
|
---|
3186 | if (SUCCEEDED(rc))
|
---|
3187 | {
|
---|
3188 | rc = pTask->createThread();
|
---|
3189 |
|
---|
3190 | if (SUCCEEDED(rc))
|
---|
3191 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3192 | }
|
---|
3193 | else
|
---|
3194 | {
|
---|
3195 | if (pTask)
|
---|
3196 | delete pTask;
|
---|
3197 | }
|
---|
3198 |
|
---|
3199 | return rc;
|
---|
3200 | }
|
---|
3201 |
|
---|
3202 | HRESULT Medium::setLocation(const com::Utf8Str &aLocation)
|
---|
3203 | {
|
---|
3204 | HRESULT rc = S_OK;
|
---|
3205 |
|
---|
3206 | try
|
---|
3207 | {
|
---|
3208 | // locking: we need the tree lock first because we access parent pointers
|
---|
3209 | // and we need to write-lock the media involved
|
---|
3210 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3211 |
|
---|
3212 | AutoCaller autoCaller(this);
|
---|
3213 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3214 |
|
---|
3215 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3216 |
|
---|
3217 | Utf8Str destPath(aLocation);
|
---|
3218 |
|
---|
3219 | // some check for file based medium
|
---|
3220 | if (i_isMediumFormatFile())
|
---|
3221 | {
|
---|
3222 | /* Path must be absolute */
|
---|
3223 | if (!RTPathStartsWithRoot(destPath.c_str()))
|
---|
3224 | {
|
---|
3225 | rc = setError(VBOX_E_FILE_ERROR,
|
---|
3226 | tr("The given path '%s' is not fully qualified"),
|
---|
3227 | destPath.c_str());
|
---|
3228 | throw rc;
|
---|
3229 | }
|
---|
3230 |
|
---|
3231 | /* Simple check for existence */
|
---|
3232 | if (!RTFileExists(destPath.c_str()))
|
---|
3233 | {
|
---|
3234 | rc = setError(VBOX_E_FILE_ERROR,
|
---|
3235 | tr("The given path '%s' is not an existing file. New location is invalid."),
|
---|
3236 | destPath.c_str());
|
---|
3237 | throw rc;
|
---|
3238 | }
|
---|
3239 | }
|
---|
3240 |
|
---|
3241 | /* Check VMs which have this medium attached to*/
|
---|
3242 | std::vector<com::Guid> aMachineIds;
|
---|
3243 | rc = getMachineIds(aMachineIds);
|
---|
3244 |
|
---|
3245 | // switch locks only if there are machines with this medium attached
|
---|
3246 | if (!aMachineIds.empty())
|
---|
3247 | {
|
---|
3248 | std::vector<com::Guid>::const_iterator currMachineID = aMachineIds.begin();
|
---|
3249 | std::vector<com::Guid>::const_iterator lastMachineID = aMachineIds.end();
|
---|
3250 |
|
---|
3251 | alock.release();
|
---|
3252 | autoCaller.release();
|
---|
3253 | treeLock.release();
|
---|
3254 |
|
---|
3255 | while (currMachineID != lastMachineID)
|
---|
3256 | {
|
---|
3257 | Guid id(*currMachineID);
|
---|
3258 | ComObjPtr<Machine> aMachine;
|
---|
3259 | rc = m->pVirtualBox->i_findMachine(id, false, true, &aMachine);
|
---|
3260 | if (SUCCEEDED(rc))
|
---|
3261 | {
|
---|
3262 | ComObjPtr<SessionMachine> sm;
|
---|
3263 | ComPtr<IInternalSessionControl> ctl;
|
---|
3264 |
|
---|
3265 | bool ses = aMachine->i_isSessionOpenVM(sm, &ctl);
|
---|
3266 | if (ses)
|
---|
3267 | {
|
---|
3268 | treeLock.acquire();
|
---|
3269 | autoCaller.add();
|
---|
3270 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3271 | alock.acquire();
|
---|
3272 |
|
---|
3273 | rc = setError(VERR_VM_UNEXPECTED_VM_STATE,
|
---|
3274 | tr("At least the VM '%s' to whom this medium '%s' attached has currently an opened session. Stop all VMs before set location for this medium"),
|
---|
3275 | id.toString().c_str(),
|
---|
3276 | i_getLocationFull().c_str());
|
---|
3277 | throw rc;
|
---|
3278 | }
|
---|
3279 | }
|
---|
3280 | ++currMachineID;
|
---|
3281 | }
|
---|
3282 |
|
---|
3283 | treeLock.acquire();
|
---|
3284 | autoCaller.add();
|
---|
3285 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3286 | alock.acquire();
|
---|
3287 | }
|
---|
3288 |
|
---|
3289 | m->strLocationFull = destPath;
|
---|
3290 |
|
---|
3291 | // save the settings
|
---|
3292 | alock.release();
|
---|
3293 | autoCaller.release();
|
---|
3294 | treeLock.release();
|
---|
3295 |
|
---|
3296 | i_markRegistriesModified();
|
---|
3297 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
3298 |
|
---|
3299 | MediumState_T mediumState;
|
---|
3300 | refreshState(autoCaller, &mediumState);
|
---|
3301 | }
|
---|
3302 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3303 |
|
---|
3304 | return rc;
|
---|
3305 | }
|
---|
3306 |
|
---|
3307 | HRESULT Medium::compact(ComPtr<IProgress> &aProgress)
|
---|
3308 | {
|
---|
3309 | HRESULT rc = S_OK;
|
---|
3310 | ComObjPtr<Progress> pProgress;
|
---|
3311 | Medium::Task *pTask = NULL;
|
---|
3312 |
|
---|
3313 | try
|
---|
3314 | {
|
---|
3315 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3316 |
|
---|
3317 | /* Build the medium lock list. */
|
---|
3318 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3319 | alock.release();
|
---|
3320 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
3321 | this /* pToLockWrite */,
|
---|
3322 | false /* fMediumLockWriteAll */,
|
---|
3323 | NULL,
|
---|
3324 | *pMediumLockList);
|
---|
3325 | alock.acquire();
|
---|
3326 | if (FAILED(rc))
|
---|
3327 | {
|
---|
3328 | delete pMediumLockList;
|
---|
3329 | throw rc;
|
---|
3330 | }
|
---|
3331 |
|
---|
3332 | alock.release();
|
---|
3333 | rc = pMediumLockList->Lock();
|
---|
3334 | alock.acquire();
|
---|
3335 | if (FAILED(rc))
|
---|
3336 | {
|
---|
3337 | delete pMediumLockList;
|
---|
3338 | throw setError(rc,
|
---|
3339 | tr("Failed to lock media when compacting '%s'"),
|
---|
3340 | i_getLocationFull().c_str());
|
---|
3341 | }
|
---|
3342 |
|
---|
3343 | pProgress.createObject();
|
---|
3344 | rc = pProgress->init(m->pVirtualBox,
|
---|
3345 | static_cast <IMedium *>(this),
|
---|
3346 | BstrFmt(tr("Compacting medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3347 | TRUE /* aCancelable */);
|
---|
3348 | if (FAILED(rc))
|
---|
3349 | {
|
---|
3350 | delete pMediumLockList;
|
---|
3351 | throw rc;
|
---|
3352 | }
|
---|
3353 |
|
---|
3354 | /* setup task object to carry out the operation asynchronously */
|
---|
3355 | pTask = new Medium::CompactTask(this, pProgress, pMediumLockList);
|
---|
3356 | rc = pTask->rc();
|
---|
3357 | AssertComRC(rc);
|
---|
3358 | if (FAILED(rc))
|
---|
3359 | throw rc;
|
---|
3360 | }
|
---|
3361 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3362 |
|
---|
3363 | if (SUCCEEDED(rc))
|
---|
3364 | {
|
---|
3365 | rc = pTask->createThread();
|
---|
3366 |
|
---|
3367 | if (SUCCEEDED(rc))
|
---|
3368 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3369 | }
|
---|
3370 | else if (pTask != NULL)
|
---|
3371 | delete pTask;
|
---|
3372 |
|
---|
3373 | return rc;
|
---|
3374 | }
|
---|
3375 |
|
---|
3376 | HRESULT Medium::resize(LONG64 aLogicalSize,
|
---|
3377 | ComPtr<IProgress> &aProgress)
|
---|
3378 | {
|
---|
3379 | HRESULT rc = S_OK;
|
---|
3380 | ComObjPtr<Progress> pProgress;
|
---|
3381 |
|
---|
3382 | /* Build the medium lock list. */
|
---|
3383 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3384 |
|
---|
3385 | try
|
---|
3386 | {
|
---|
3387 | const char *pszError = NULL;
|
---|
3388 |
|
---|
3389 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
3390 | this /* pToLockWrite */,
|
---|
3391 | false /* fMediumLockWriteAll */,
|
---|
3392 | NULL,
|
---|
3393 | *pMediumLockList);
|
---|
3394 | if (FAILED(rc))
|
---|
3395 | {
|
---|
3396 | pszError = tr("Failed to create medium lock list when resize '%s'");
|
---|
3397 | }
|
---|
3398 | else
|
---|
3399 | {
|
---|
3400 | rc = pMediumLockList->Lock();
|
---|
3401 | if (FAILED(rc))
|
---|
3402 | pszError = tr("Failed to lock media when compacting '%s'");
|
---|
3403 | }
|
---|
3404 |
|
---|
3405 |
|
---|
3406 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3407 |
|
---|
3408 | if (FAILED(rc))
|
---|
3409 | {
|
---|
3410 | delete pMediumLockList;
|
---|
3411 | throw setError(rc, pszError, i_getLocationFull().c_str());
|
---|
3412 | }
|
---|
3413 |
|
---|
3414 | pProgress.createObject();
|
---|
3415 | rc = pProgress->init(m->pVirtualBox,
|
---|
3416 | static_cast <IMedium *>(this),
|
---|
3417 | BstrFmt(tr("Resizing medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3418 | TRUE /* aCancelable */);
|
---|
3419 | if (FAILED(rc))
|
---|
3420 | {
|
---|
3421 | delete pMediumLockList;
|
---|
3422 | throw rc;
|
---|
3423 | }
|
---|
3424 | }
|
---|
3425 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3426 |
|
---|
3427 | if (SUCCEEDED(rc))
|
---|
3428 | rc = i_resize(aLogicalSize, pMediumLockList, &pProgress, false /* aWait */);
|
---|
3429 |
|
---|
3430 | if (SUCCEEDED(rc))
|
---|
3431 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3432 | else
|
---|
3433 | delete pMediumLockList;
|
---|
3434 |
|
---|
3435 | return rc;
|
---|
3436 | }
|
---|
3437 |
|
---|
3438 | HRESULT Medium::reset(AutoCaller &autoCaller, ComPtr<IProgress> &aProgress)
|
---|
3439 | {
|
---|
3440 | HRESULT rc = S_OK;
|
---|
3441 | ComObjPtr<Progress> pProgress;
|
---|
3442 | Medium::Task *pTask = NULL;
|
---|
3443 |
|
---|
3444 | try
|
---|
3445 | {
|
---|
3446 | autoCaller.release();
|
---|
3447 |
|
---|
3448 | /* It is possible that some previous/concurrent uninit has already
|
---|
3449 | * cleared the pVirtualBox reference, see #uninit(). */
|
---|
3450 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
3451 |
|
---|
3452 | /* canClose() needs the tree lock */
|
---|
3453 | AutoMultiWriteLock2 multilock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL,
|
---|
3454 | this->lockHandle()
|
---|
3455 | COMMA_LOCKVAL_SRC_POS);
|
---|
3456 |
|
---|
3457 | autoCaller.add();
|
---|
3458 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
3459 |
|
---|
3460 | LogFlowThisFunc(("ENTER for medium %s\n", m->strLocationFull.c_str()));
|
---|
3461 |
|
---|
3462 | if (m->pParent.isNull())
|
---|
3463 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3464 | tr("Medium type of '%s' is not differencing"),
|
---|
3465 | m->strLocationFull.c_str());
|
---|
3466 |
|
---|
3467 | rc = i_canClose();
|
---|
3468 | if (FAILED(rc))
|
---|
3469 | throw rc;
|
---|
3470 |
|
---|
3471 | /* Build the medium lock list. */
|
---|
3472 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3473 | multilock.release();
|
---|
3474 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
3475 | this /* pToLockWrite */,
|
---|
3476 | false /* fMediumLockWriteAll */,
|
---|
3477 | NULL,
|
---|
3478 | *pMediumLockList);
|
---|
3479 | multilock.acquire();
|
---|
3480 | if (FAILED(rc))
|
---|
3481 | {
|
---|
3482 | delete pMediumLockList;
|
---|
3483 | throw rc;
|
---|
3484 | }
|
---|
3485 |
|
---|
3486 | multilock.release();
|
---|
3487 | rc = pMediumLockList->Lock();
|
---|
3488 | multilock.acquire();
|
---|
3489 | if (FAILED(rc))
|
---|
3490 | {
|
---|
3491 | delete pMediumLockList;
|
---|
3492 | throw setError(rc,
|
---|
3493 | tr("Failed to lock media when resetting '%s'"),
|
---|
3494 | i_getLocationFull().c_str());
|
---|
3495 | }
|
---|
3496 |
|
---|
3497 | pProgress.createObject();
|
---|
3498 | rc = pProgress->init(m->pVirtualBox,
|
---|
3499 | static_cast<IMedium*>(this),
|
---|
3500 | BstrFmt(tr("Resetting differencing medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3501 | FALSE /* aCancelable */);
|
---|
3502 | if (FAILED(rc))
|
---|
3503 | throw rc;
|
---|
3504 |
|
---|
3505 | /* setup task object to carry out the operation asynchronously */
|
---|
3506 | pTask = new Medium::ResetTask(this, pProgress, pMediumLockList);
|
---|
3507 | rc = pTask->rc();
|
---|
3508 | AssertComRC(rc);
|
---|
3509 | if (FAILED(rc))
|
---|
3510 | throw rc;
|
---|
3511 | }
|
---|
3512 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3513 |
|
---|
3514 | if (SUCCEEDED(rc))
|
---|
3515 | {
|
---|
3516 | rc = pTask->createThread();
|
---|
3517 |
|
---|
3518 | if (SUCCEEDED(rc))
|
---|
3519 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3520 | }
|
---|
3521 | else if (pTask != NULL)
|
---|
3522 | delete pTask;
|
---|
3523 |
|
---|
3524 | LogFlowThisFunc(("LEAVE, rc=%Rhrc\n", rc));
|
---|
3525 |
|
---|
3526 | return rc;
|
---|
3527 | }
|
---|
3528 |
|
---|
3529 | HRESULT Medium::changeEncryption(const com::Utf8Str &aCurrentPassword, const com::Utf8Str &aCipher,
|
---|
3530 | const com::Utf8Str &aNewPassword, const com::Utf8Str &aNewPasswordId,
|
---|
3531 | ComPtr<IProgress> &aProgress)
|
---|
3532 | {
|
---|
3533 | HRESULT rc = S_OK;
|
---|
3534 | ComObjPtr<Progress> pProgress;
|
---|
3535 | Medium::Task *pTask = NULL;
|
---|
3536 |
|
---|
3537 | try
|
---|
3538 | {
|
---|
3539 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3540 |
|
---|
3541 | DeviceType_T devType = i_getDeviceType();
|
---|
3542 | /* Cannot encrypt DVD or floppy images so far. */
|
---|
3543 | if ( devType == DeviceType_DVD
|
---|
3544 | || devType == DeviceType_Floppy)
|
---|
3545 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3546 | tr("Cannot encrypt DVD or Floppy medium '%s'"),
|
---|
3547 | m->strLocationFull.c_str());
|
---|
3548 |
|
---|
3549 | /* Cannot encrypt media which are attached to more than one virtual machine. */
|
---|
3550 | if (m->backRefs.size() > 1)
|
---|
3551 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3552 | tr("Cannot encrypt medium '%s' because it is attached to %d virtual machines"),
|
---|
3553 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
3554 |
|
---|
3555 | if (i_getChildren().size() != 0)
|
---|
3556 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3557 | tr("Cannot encrypt medium '%s' because it has %d children"),
|
---|
3558 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
3559 |
|
---|
3560 | /* Build the medium lock list. */
|
---|
3561 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3562 | alock.release();
|
---|
3563 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
3564 | this /* pToLockWrite */,
|
---|
3565 | true /* fMediumLockAllWrite */,
|
---|
3566 | NULL,
|
---|
3567 | *pMediumLockList);
|
---|
3568 | alock.acquire();
|
---|
3569 | if (FAILED(rc))
|
---|
3570 | {
|
---|
3571 | delete pMediumLockList;
|
---|
3572 | throw rc;
|
---|
3573 | }
|
---|
3574 |
|
---|
3575 | alock.release();
|
---|
3576 | rc = pMediumLockList->Lock();
|
---|
3577 | alock.acquire();
|
---|
3578 | if (FAILED(rc))
|
---|
3579 | {
|
---|
3580 | delete pMediumLockList;
|
---|
3581 | throw setError(rc,
|
---|
3582 | tr("Failed to lock media for encryption '%s'"),
|
---|
3583 | i_getLocationFull().c_str());
|
---|
3584 | }
|
---|
3585 |
|
---|
3586 | /*
|
---|
3587 | * Check all media in the chain to not contain any branches or references to
|
---|
3588 | * other virtual machines, we support encrypting only a list of differencing media at the moment.
|
---|
3589 | */
|
---|
3590 | MediumLockList::Base::const_iterator mediumListBegin = pMediumLockList->GetBegin();
|
---|
3591 | MediumLockList::Base::const_iterator mediumListEnd = pMediumLockList->GetEnd();
|
---|
3592 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
3593 | it != mediumListEnd;
|
---|
3594 | ++it)
|
---|
3595 | {
|
---|
3596 | const MediumLock &mediumLock = *it;
|
---|
3597 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
3598 | AutoReadLock mediumReadLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
3599 |
|
---|
3600 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
3601 |
|
---|
3602 | if (pMedium->m->backRefs.size() > 1)
|
---|
3603 | {
|
---|
3604 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3605 | tr("Cannot encrypt medium '%s' because it is attached to %d virtual machines"),
|
---|
3606 | pMedium->m->strLocationFull.c_str(), pMedium->m->backRefs.size());
|
---|
3607 | break;
|
---|
3608 | }
|
---|
3609 | else if (pMedium->i_getChildren().size() > 1)
|
---|
3610 | {
|
---|
3611 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3612 | tr("Cannot encrypt medium '%s' because it has %d children"),
|
---|
3613 | pMedium->m->strLocationFull.c_str(), pMedium->i_getChildren().size());
|
---|
3614 | break;
|
---|
3615 | }
|
---|
3616 | }
|
---|
3617 |
|
---|
3618 | if (FAILED(rc))
|
---|
3619 | {
|
---|
3620 | delete pMediumLockList;
|
---|
3621 | throw rc;
|
---|
3622 | }
|
---|
3623 |
|
---|
3624 | const char *pszAction = "Encrypting";
|
---|
3625 | if ( aCurrentPassword.isNotEmpty()
|
---|
3626 | && aCipher.isEmpty())
|
---|
3627 | pszAction = "Decrypting";
|
---|
3628 |
|
---|
3629 | pProgress.createObject();
|
---|
3630 | rc = pProgress->init(m->pVirtualBox,
|
---|
3631 | static_cast <IMedium *>(this),
|
---|
3632 | BstrFmt(tr("%s medium '%s'"), pszAction, m->strLocationFull.c_str()).raw(),
|
---|
3633 | TRUE /* aCancelable */);
|
---|
3634 | if (FAILED(rc))
|
---|
3635 | {
|
---|
3636 | delete pMediumLockList;
|
---|
3637 | throw rc;
|
---|
3638 | }
|
---|
3639 |
|
---|
3640 | /* setup task object to carry out the operation asynchronously */
|
---|
3641 | pTask = new Medium::EncryptTask(this, aNewPassword, aCurrentPassword,
|
---|
3642 | aCipher, aNewPasswordId, pProgress, pMediumLockList);
|
---|
3643 | rc = pTask->rc();
|
---|
3644 | AssertComRC(rc);
|
---|
3645 | if (FAILED(rc))
|
---|
3646 | throw rc;
|
---|
3647 | }
|
---|
3648 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3649 |
|
---|
3650 | if (SUCCEEDED(rc))
|
---|
3651 | {
|
---|
3652 | rc = pTask->createThread();
|
---|
3653 |
|
---|
3654 | if (SUCCEEDED(rc))
|
---|
3655 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3656 | }
|
---|
3657 | else if (pTask != NULL)
|
---|
3658 | delete pTask;
|
---|
3659 |
|
---|
3660 | return rc;
|
---|
3661 | }
|
---|
3662 |
|
---|
3663 | HRESULT Medium::getEncryptionSettings(AutoCaller &autoCaller, com::Utf8Str &aCipher, com::Utf8Str &aPasswordId)
|
---|
3664 | {
|
---|
3665 | #ifndef VBOX_WITH_EXTPACK
|
---|
3666 | RT_NOREF(aCipher, aPasswordId);
|
---|
3667 | #endif
|
---|
3668 | HRESULT rc = S_OK;
|
---|
3669 |
|
---|
3670 | try
|
---|
3671 | {
|
---|
3672 | autoCaller.release();
|
---|
3673 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
3674 | autoCaller.add();
|
---|
3675 | if (FAILED(autoCaller.rc()))
|
---|
3676 | throw rc;
|
---|
3677 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3678 |
|
---|
3679 | /* Check whether encryption is configured for this medium. */
|
---|
3680 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
3681 | if (it == pBase->m->mapProperties.end())
|
---|
3682 | throw VBOX_E_NOT_SUPPORTED;
|
---|
3683 |
|
---|
3684 | # ifdef VBOX_WITH_EXTPACK
|
---|
3685 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
3686 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
3687 | {
|
---|
3688 | /* Load the plugin */
|
---|
3689 | Utf8Str strPlugin;
|
---|
3690 | rc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
3691 | if (SUCCEEDED(rc))
|
---|
3692 | {
|
---|
3693 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
3694 | if (RT_FAILURE(vrc))
|
---|
3695 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
3696 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
3697 | i_vdError(vrc).c_str());
|
---|
3698 | }
|
---|
3699 | else
|
---|
3700 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3701 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
3702 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
3703 | }
|
---|
3704 | else
|
---|
3705 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3706 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
3707 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
3708 |
|
---|
3709 | PVDISK pDisk = NULL;
|
---|
3710 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
3711 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
3712 |
|
---|
3713 | MediumCryptoFilterSettings CryptoSettings;
|
---|
3714 |
|
---|
3715 | i_taskEncryptSettingsSetup(&CryptoSettings, NULL, it->second.c_str(), NULL, false /* fCreateKeyStore */);
|
---|
3716 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ | VD_FILTER_FLAGS_INFO, CryptoSettings.vdFilterIfaces);
|
---|
3717 | if (RT_FAILURE(vrc))
|
---|
3718 | throw setErrorBoth(VBOX_E_INVALID_OBJECT_STATE, vrc,
|
---|
3719 | tr("Failed to load the encryption filter: %s"),
|
---|
3720 | i_vdError(vrc).c_str());
|
---|
3721 |
|
---|
3722 | it = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
3723 | if (it == pBase->m->mapProperties.end())
|
---|
3724 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3725 | tr("Image is configured for encryption but doesn't has a KeyId set"));
|
---|
3726 |
|
---|
3727 | aPasswordId = it->second.c_str();
|
---|
3728 | aCipher = CryptoSettings.pszCipherReturned;
|
---|
3729 | RTStrFree(CryptoSettings.pszCipherReturned);
|
---|
3730 |
|
---|
3731 | VDDestroy(pDisk);
|
---|
3732 | # else
|
---|
3733 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3734 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
3735 | # endif
|
---|
3736 | }
|
---|
3737 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3738 |
|
---|
3739 | return rc;
|
---|
3740 | }
|
---|
3741 |
|
---|
3742 | HRESULT Medium::checkEncryptionPassword(const com::Utf8Str &aPassword)
|
---|
3743 | {
|
---|
3744 | HRESULT rc = S_OK;
|
---|
3745 |
|
---|
3746 | try
|
---|
3747 | {
|
---|
3748 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
3749 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3750 |
|
---|
3751 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
3752 | if (it == pBase->m->mapProperties.end())
|
---|
3753 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3754 | tr("The image is not configured for encryption"));
|
---|
3755 |
|
---|
3756 | if (aPassword.isEmpty())
|
---|
3757 | throw setError(E_INVALIDARG,
|
---|
3758 | tr("The given password must not be empty"));
|
---|
3759 |
|
---|
3760 | # ifdef VBOX_WITH_EXTPACK
|
---|
3761 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
3762 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
3763 | {
|
---|
3764 | /* Load the plugin */
|
---|
3765 | Utf8Str strPlugin;
|
---|
3766 | rc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
3767 | if (SUCCEEDED(rc))
|
---|
3768 | {
|
---|
3769 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
3770 | if (RT_FAILURE(vrc))
|
---|
3771 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
3772 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
3773 | i_vdError(vrc).c_str());
|
---|
3774 | }
|
---|
3775 | else
|
---|
3776 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3777 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
3778 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
3779 | }
|
---|
3780 | else
|
---|
3781 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3782 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
3783 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
3784 |
|
---|
3785 | PVDISK pDisk = NULL;
|
---|
3786 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
3787 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
3788 |
|
---|
3789 | MediumCryptoFilterSettings CryptoSettings;
|
---|
3790 |
|
---|
3791 | i_taskEncryptSettingsSetup(&CryptoSettings, NULL, it->second.c_str(), aPassword.c_str(),
|
---|
3792 | false /* fCreateKeyStore */);
|
---|
3793 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ, CryptoSettings.vdFilterIfaces);
|
---|
3794 | if (vrc == VERR_VD_PASSWORD_INCORRECT)
|
---|
3795 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
3796 | tr("The given password is incorrect"));
|
---|
3797 | else if (RT_FAILURE(vrc))
|
---|
3798 | throw setErrorBoth(VBOX_E_INVALID_OBJECT_STATE, vrc,
|
---|
3799 | tr("Failed to load the encryption filter: %s"),
|
---|
3800 | i_vdError(vrc).c_str());
|
---|
3801 |
|
---|
3802 | VDDestroy(pDisk);
|
---|
3803 | # else
|
---|
3804 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3805 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
3806 | # endif
|
---|
3807 | }
|
---|
3808 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3809 |
|
---|
3810 | return rc;
|
---|
3811 | }
|
---|
3812 |
|
---|
3813 | HRESULT Medium::openForIO(BOOL aWritable, com::Utf8Str const &aPassword, ComPtr<IMediumIO> &aMediumIO)
|
---|
3814 | {
|
---|
3815 | /*
|
---|
3816 | * Input validation.
|
---|
3817 | */
|
---|
3818 | if (aWritable && i_isReadOnly())
|
---|
3819 | return setError(E_ACCESSDENIED, tr("Write access denied: read-only"));
|
---|
3820 |
|
---|
3821 | com::Utf8Str const strKeyId = i_getKeyId();
|
---|
3822 | if (strKeyId.isEmpty() && aPassword.isNotEmpty())
|
---|
3823 | return setError(E_INVALIDARG, tr("Password given for unencrypted medium"));
|
---|
3824 | if (strKeyId.isNotEmpty() && aPassword.isEmpty())
|
---|
3825 | return setError(E_INVALIDARG, tr("Password needed for encrypted medium"));
|
---|
3826 |
|
---|
3827 | /*
|
---|
3828 | * Create IO object and return it.
|
---|
3829 | */
|
---|
3830 | ComObjPtr<MediumIO> ptrIO;
|
---|
3831 | HRESULT hrc = ptrIO.createObject();
|
---|
3832 | if (SUCCEEDED(hrc))
|
---|
3833 | {
|
---|
3834 | hrc = ptrIO->initForMedium(this, m->pVirtualBox, aWritable != FALSE, strKeyId, aPassword);
|
---|
3835 | if (SUCCEEDED(hrc))
|
---|
3836 | ptrIO.queryInterfaceTo(aMediumIO.asOutParam());
|
---|
3837 | }
|
---|
3838 | return hrc;
|
---|
3839 | }
|
---|
3840 |
|
---|
3841 |
|
---|
3842 | ////////////////////////////////////////////////////////////////////////////////
|
---|
3843 | //
|
---|
3844 | // Medium public internal methods
|
---|
3845 | //
|
---|
3846 | ////////////////////////////////////////////////////////////////////////////////
|
---|
3847 |
|
---|
3848 | /**
|
---|
3849 | * Internal method to return the medium's parent medium. Must have caller + locking!
|
---|
3850 | * @return
|
---|
3851 | */
|
---|
3852 | const ComObjPtr<Medium>& Medium::i_getParent() const
|
---|
3853 | {
|
---|
3854 | return m->pParent;
|
---|
3855 | }
|
---|
3856 |
|
---|
3857 | /**
|
---|
3858 | * Internal method to return the medium's list of child media. Must have caller + locking!
|
---|
3859 | * @return
|
---|
3860 | */
|
---|
3861 | const MediaList& Medium::i_getChildren() const
|
---|
3862 | {
|
---|
3863 | return m->llChildren;
|
---|
3864 | }
|
---|
3865 |
|
---|
3866 | /**
|
---|
3867 | * Internal method to return the medium's GUID. Must have caller + locking!
|
---|
3868 | * @return
|
---|
3869 | */
|
---|
3870 | const Guid& Medium::i_getId() const
|
---|
3871 | {
|
---|
3872 | return m->id;
|
---|
3873 | }
|
---|
3874 |
|
---|
3875 | /**
|
---|
3876 | * Internal method to return the medium's state. Must have caller + locking!
|
---|
3877 | * @return
|
---|
3878 | */
|
---|
3879 | MediumState_T Medium::i_getState() const
|
---|
3880 | {
|
---|
3881 | return m->state;
|
---|
3882 | }
|
---|
3883 |
|
---|
3884 | /**
|
---|
3885 | * Internal method to return the medium's variant. Must have caller + locking!
|
---|
3886 | * @return
|
---|
3887 | */
|
---|
3888 | MediumVariant_T Medium::i_getVariant() const
|
---|
3889 | {
|
---|
3890 | return m->variant;
|
---|
3891 | }
|
---|
3892 |
|
---|
3893 | /**
|
---|
3894 | * Internal method which returns true if this medium represents a host drive.
|
---|
3895 | * @return
|
---|
3896 | */
|
---|
3897 | bool Medium::i_isHostDrive() const
|
---|
3898 | {
|
---|
3899 | return m->hostDrive;
|
---|
3900 | }
|
---|
3901 |
|
---|
3902 | /**
|
---|
3903 | * Internal method to return the medium's full location. Must have caller + locking!
|
---|
3904 | * @return
|
---|
3905 | */
|
---|
3906 | const Utf8Str& Medium::i_getLocationFull() const
|
---|
3907 | {
|
---|
3908 | return m->strLocationFull;
|
---|
3909 | }
|
---|
3910 |
|
---|
3911 | /**
|
---|
3912 | * Internal method to return the medium's format string. Must have caller + locking!
|
---|
3913 | * @return
|
---|
3914 | */
|
---|
3915 | const Utf8Str& Medium::i_getFormat() const
|
---|
3916 | {
|
---|
3917 | return m->strFormat;
|
---|
3918 | }
|
---|
3919 |
|
---|
3920 | /**
|
---|
3921 | * Internal method to return the medium's format object. Must have caller + locking!
|
---|
3922 | * @return
|
---|
3923 | */
|
---|
3924 | const ComObjPtr<MediumFormat>& Medium::i_getMediumFormat() const
|
---|
3925 | {
|
---|
3926 | return m->formatObj;
|
---|
3927 | }
|
---|
3928 |
|
---|
3929 | /**
|
---|
3930 | * Internal method that returns true if the medium is represented by a file on the host disk
|
---|
3931 | * (and not iSCSI or something).
|
---|
3932 | * @return
|
---|
3933 | */
|
---|
3934 | bool Medium::i_isMediumFormatFile() const
|
---|
3935 | {
|
---|
3936 | if ( m->formatObj
|
---|
3937 | && (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
3938 | )
|
---|
3939 | return true;
|
---|
3940 | return false;
|
---|
3941 | }
|
---|
3942 |
|
---|
3943 | /**
|
---|
3944 | * Internal method to return the medium's size. Must have caller + locking!
|
---|
3945 | * @return
|
---|
3946 | */
|
---|
3947 | uint64_t Medium::i_getSize() const
|
---|
3948 | {
|
---|
3949 | return m->size;
|
---|
3950 | }
|
---|
3951 |
|
---|
3952 | /**
|
---|
3953 | * Internal method to return the medium's size. Must have caller + locking!
|
---|
3954 | * @return
|
---|
3955 | */
|
---|
3956 | uint64_t Medium::i_getLogicalSize() const
|
---|
3957 | {
|
---|
3958 | return m->logicalSize;
|
---|
3959 | }
|
---|
3960 |
|
---|
3961 | /**
|
---|
3962 | * Returns the medium device type. Must have caller + locking!
|
---|
3963 | * @return
|
---|
3964 | */
|
---|
3965 | DeviceType_T Medium::i_getDeviceType() const
|
---|
3966 | {
|
---|
3967 | return m->devType;
|
---|
3968 | }
|
---|
3969 |
|
---|
3970 | /**
|
---|
3971 | * Returns the medium type. Must have caller + locking!
|
---|
3972 | * @return
|
---|
3973 | */
|
---|
3974 | MediumType_T Medium::i_getType() const
|
---|
3975 | {
|
---|
3976 | return m->type;
|
---|
3977 | }
|
---|
3978 |
|
---|
3979 | /**
|
---|
3980 | * Returns a short version of the location attribute.
|
---|
3981 | *
|
---|
3982 | * @note Must be called from under this object's read or write lock.
|
---|
3983 | */
|
---|
3984 | Utf8Str Medium::i_getName()
|
---|
3985 | {
|
---|
3986 | Utf8Str name = RTPathFilename(m->strLocationFull.c_str());
|
---|
3987 | return name;
|
---|
3988 | }
|
---|
3989 |
|
---|
3990 | /**
|
---|
3991 | * This adds the given UUID to the list of media registries in which this
|
---|
3992 | * medium should be registered. The UUID can either be a machine UUID,
|
---|
3993 | * to add a machine registry, or the global registry UUID as returned by
|
---|
3994 | * VirtualBox::getGlobalRegistryId().
|
---|
3995 | *
|
---|
3996 | * Note that for hard disks, this method does nothing if the medium is
|
---|
3997 | * already in another registry to avoid having hard disks in more than
|
---|
3998 | * one registry, which causes trouble with keeping diff images in sync.
|
---|
3999 | * See getFirstRegistryMachineId() for details.
|
---|
4000 | *
|
---|
4001 | * @param id
|
---|
4002 | * @return true if the registry was added; false if the given id was already on the list.
|
---|
4003 | */
|
---|
4004 | bool Medium::i_addRegistry(const Guid& id)
|
---|
4005 | {
|
---|
4006 | AutoCaller autoCaller(this);
|
---|
4007 | if (FAILED(autoCaller.rc()))
|
---|
4008 | return false;
|
---|
4009 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4010 |
|
---|
4011 | bool fAdd = true;
|
---|
4012 |
|
---|
4013 | // hard disks cannot be in more than one registry
|
---|
4014 | if ( m->devType == DeviceType_HardDisk
|
---|
4015 | && m->llRegistryIDs.size() > 0)
|
---|
4016 | fAdd = false;
|
---|
4017 |
|
---|
4018 | // no need to add the UUID twice
|
---|
4019 | if (fAdd)
|
---|
4020 | {
|
---|
4021 | for (GuidList::const_iterator it = m->llRegistryIDs.begin();
|
---|
4022 | it != m->llRegistryIDs.end();
|
---|
4023 | ++it)
|
---|
4024 | {
|
---|
4025 | if ((*it) == id)
|
---|
4026 | {
|
---|
4027 | fAdd = false;
|
---|
4028 | break;
|
---|
4029 | }
|
---|
4030 | }
|
---|
4031 | }
|
---|
4032 |
|
---|
4033 | if (fAdd)
|
---|
4034 | m->llRegistryIDs.push_back(id);
|
---|
4035 |
|
---|
4036 | return fAdd;
|
---|
4037 | }
|
---|
4038 |
|
---|
4039 | /**
|
---|
4040 | * This adds the given UUID to the list of media registries in which this
|
---|
4041 | * medium should be registered. The UUID can either be a machine UUID,
|
---|
4042 | * to add a machine registry, or the global registry UUID as returned by
|
---|
4043 | * VirtualBox::getGlobalRegistryId(). This recurses over all children.
|
---|
4044 | *
|
---|
4045 | * Note that for hard disks, this method does nothing if the medium is
|
---|
4046 | * already in another registry to avoid having hard disks in more than
|
---|
4047 | * one registry, which causes trouble with keeping diff images in sync.
|
---|
4048 | * See getFirstRegistryMachineId() for details.
|
---|
4049 | *
|
---|
4050 | * @note the caller must hold the media tree lock for reading.
|
---|
4051 | *
|
---|
4052 | * @param id
|
---|
4053 | * @return true if the registry was added; false if the given id was already on the list.
|
---|
4054 | */
|
---|
4055 | bool Medium::i_addRegistryRecursive(const Guid &id)
|
---|
4056 | {
|
---|
4057 | AutoCaller autoCaller(this);
|
---|
4058 | if (FAILED(autoCaller.rc()))
|
---|
4059 | return false;
|
---|
4060 |
|
---|
4061 | bool fAdd = i_addRegistry(id);
|
---|
4062 |
|
---|
4063 | // protected by the medium tree lock held by our original caller
|
---|
4064 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
4065 | it != i_getChildren().end();
|
---|
4066 | ++it)
|
---|
4067 | {
|
---|
4068 | Medium *pChild = *it;
|
---|
4069 | fAdd |= pChild->i_addRegistryRecursive(id);
|
---|
4070 | }
|
---|
4071 |
|
---|
4072 | return fAdd;
|
---|
4073 | }
|
---|
4074 |
|
---|
4075 | /**
|
---|
4076 | * Removes the given UUID from the list of media registry UUIDs of this medium.
|
---|
4077 | *
|
---|
4078 | * @param id
|
---|
4079 | * @return true if the UUID was found or false if not.
|
---|
4080 | */
|
---|
4081 | bool Medium::i_removeRegistry(const Guid &id)
|
---|
4082 | {
|
---|
4083 | AutoCaller autoCaller(this);
|
---|
4084 | if (FAILED(autoCaller.rc()))
|
---|
4085 | return false;
|
---|
4086 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4087 |
|
---|
4088 | bool fRemove = false;
|
---|
4089 |
|
---|
4090 | /// @todo r=klaus eliminate this code, replace it by using find.
|
---|
4091 | for (GuidList::iterator it = m->llRegistryIDs.begin();
|
---|
4092 | it != m->llRegistryIDs.end();
|
---|
4093 | ++it)
|
---|
4094 | {
|
---|
4095 | if ((*it) == id)
|
---|
4096 | {
|
---|
4097 | // getting away with this as the iterator isn't used after
|
---|
4098 | m->llRegistryIDs.erase(it);
|
---|
4099 | fRemove = true;
|
---|
4100 | break;
|
---|
4101 | }
|
---|
4102 | }
|
---|
4103 |
|
---|
4104 | return fRemove;
|
---|
4105 | }
|
---|
4106 |
|
---|
4107 | /**
|
---|
4108 | * Removes the given UUID from the list of media registry UUIDs, for this
|
---|
4109 | * medium and all its children recursively.
|
---|
4110 | *
|
---|
4111 | * @note the caller must hold the media tree lock for reading.
|
---|
4112 | *
|
---|
4113 | * @param id
|
---|
4114 | * @return true if the UUID was found or false if not.
|
---|
4115 | */
|
---|
4116 | bool Medium::i_removeRegistryRecursive(const Guid &id)
|
---|
4117 | {
|
---|
4118 | AutoCaller autoCaller(this);
|
---|
4119 | if (FAILED(autoCaller.rc()))
|
---|
4120 | return false;
|
---|
4121 |
|
---|
4122 | bool fRemove = i_removeRegistry(id);
|
---|
4123 |
|
---|
4124 | // protected by the medium tree lock held by our original caller
|
---|
4125 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
4126 | it != i_getChildren().end();
|
---|
4127 | ++it)
|
---|
4128 | {
|
---|
4129 | Medium *pChild = *it;
|
---|
4130 | fRemove |= pChild->i_removeRegistryRecursive(id);
|
---|
4131 | }
|
---|
4132 |
|
---|
4133 | return fRemove;
|
---|
4134 | }
|
---|
4135 |
|
---|
4136 | /**
|
---|
4137 | * Returns true if id is in the list of media registries for this medium.
|
---|
4138 | *
|
---|
4139 | * Must have caller + read locking!
|
---|
4140 | *
|
---|
4141 | * @param id
|
---|
4142 | * @return
|
---|
4143 | */
|
---|
4144 | bool Medium::i_isInRegistry(const Guid &id)
|
---|
4145 | {
|
---|
4146 | /// @todo r=klaus eliminate this code, replace it by using find.
|
---|
4147 | for (GuidList::const_iterator it = m->llRegistryIDs.begin();
|
---|
4148 | it != m->llRegistryIDs.end();
|
---|
4149 | ++it)
|
---|
4150 | {
|
---|
4151 | if (*it == id)
|
---|
4152 | return true;
|
---|
4153 | }
|
---|
4154 |
|
---|
4155 | return false;
|
---|
4156 | }
|
---|
4157 |
|
---|
4158 | /**
|
---|
4159 | * Internal method to return the medium's first registry machine (i.e. the machine in whose
|
---|
4160 | * machine XML this medium is listed).
|
---|
4161 | *
|
---|
4162 | * Every attached medium must now (4.0) reside in at least one media registry, which is identified
|
---|
4163 | * by a UUID. This is either a machine UUID if the machine is from 4.0 or newer, in which case
|
---|
4164 | * machines have their own media registries, or it is the pseudo-UUID of the VirtualBox
|
---|
4165 | * object if the machine is old and still needs the global registry in VirtualBox.xml.
|
---|
4166 | *
|
---|
4167 | * By definition, hard disks may only be in one media registry, in which all its children
|
---|
4168 | * will be stored as well. Otherwise we run into problems with having keep multiple registries
|
---|
4169 | * in sync. (This is the "cloned VM" case in which VM1 may link to the disks of VM2; in this
|
---|
4170 | * case, only VM2's registry is used for the disk in question.)
|
---|
4171 | *
|
---|
4172 | * If there is no medium registry, particularly if the medium has not been attached yet, this
|
---|
4173 | * does not modify uuid and returns false.
|
---|
4174 | *
|
---|
4175 | * ISOs and RAWs, by contrast, can be in more than one repository to make things easier for
|
---|
4176 | * the user.
|
---|
4177 | *
|
---|
4178 | * Must have caller + locking!
|
---|
4179 | *
|
---|
4180 | * @param uuid Receives first registry machine UUID, if available.
|
---|
4181 | * @return true if uuid was set.
|
---|
4182 | */
|
---|
4183 | bool Medium::i_getFirstRegistryMachineId(Guid &uuid) const
|
---|
4184 | {
|
---|
4185 | if (m->llRegistryIDs.size())
|
---|
4186 | {
|
---|
4187 | uuid = m->llRegistryIDs.front();
|
---|
4188 | return true;
|
---|
4189 | }
|
---|
4190 | return false;
|
---|
4191 | }
|
---|
4192 |
|
---|
4193 | /**
|
---|
4194 | * Marks all the registries in which this medium is registered as modified.
|
---|
4195 | */
|
---|
4196 | void Medium::i_markRegistriesModified()
|
---|
4197 | {
|
---|
4198 | AutoCaller autoCaller(this);
|
---|
4199 | if (FAILED(autoCaller.rc())) return;
|
---|
4200 |
|
---|
4201 | // Get local copy, as keeping the lock over VirtualBox::markRegistryModified
|
---|
4202 | // causes trouble with the lock order
|
---|
4203 | GuidList llRegistryIDs;
|
---|
4204 | {
|
---|
4205 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4206 | llRegistryIDs = m->llRegistryIDs;
|
---|
4207 | }
|
---|
4208 |
|
---|
4209 | autoCaller.release();
|
---|
4210 |
|
---|
4211 | /* Save the error information now, the implicit restore when this goes
|
---|
4212 | * out of scope will throw away spurious additional errors created below. */
|
---|
4213 | ErrorInfoKeeper eik;
|
---|
4214 | for (GuidList::const_iterator it = llRegistryIDs.begin();
|
---|
4215 | it != llRegistryIDs.end();
|
---|
4216 | ++it)
|
---|
4217 | {
|
---|
4218 | m->pVirtualBox->i_markRegistryModified(*it);
|
---|
4219 | }
|
---|
4220 | }
|
---|
4221 |
|
---|
4222 | /**
|
---|
4223 | * Adds the given machine and optionally the snapshot to the list of the objects
|
---|
4224 | * this medium is attached to.
|
---|
4225 | *
|
---|
4226 | * @param aMachineId Machine ID.
|
---|
4227 | * @param aSnapshotId Snapshot ID; when non-empty, adds a snapshot attachment.
|
---|
4228 | */
|
---|
4229 | HRESULT Medium::i_addBackReference(const Guid &aMachineId,
|
---|
4230 | const Guid &aSnapshotId /*= Guid::Empty*/)
|
---|
4231 | {
|
---|
4232 | AssertReturn(aMachineId.isValid(), E_FAIL);
|
---|
4233 |
|
---|
4234 | LogFlowThisFunc(("ENTER, aMachineId: {%RTuuid}, aSnapshotId: {%RTuuid}\n", aMachineId.raw(), aSnapshotId.raw()));
|
---|
4235 |
|
---|
4236 | AutoCaller autoCaller(this);
|
---|
4237 | AssertComRCReturnRC(autoCaller.rc());
|
---|
4238 |
|
---|
4239 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4240 |
|
---|
4241 | switch (m->state)
|
---|
4242 | {
|
---|
4243 | case MediumState_Created:
|
---|
4244 | case MediumState_Inaccessible:
|
---|
4245 | case MediumState_LockedRead:
|
---|
4246 | case MediumState_LockedWrite:
|
---|
4247 | break;
|
---|
4248 |
|
---|
4249 | default:
|
---|
4250 | return i_setStateError();
|
---|
4251 | }
|
---|
4252 |
|
---|
4253 | if (m->numCreateDiffTasks > 0)
|
---|
4254 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4255 | tr("Cannot attach medium '%s' {%RTuuid}: %u differencing child media are being created"),
|
---|
4256 | m->strLocationFull.c_str(),
|
---|
4257 | m->id.raw(),
|
---|
4258 | m->numCreateDiffTasks);
|
---|
4259 |
|
---|
4260 | BackRefList::iterator it = std::find_if(m->backRefs.begin(),
|
---|
4261 | m->backRefs.end(),
|
---|
4262 | BackRef::EqualsTo(aMachineId));
|
---|
4263 | if (it == m->backRefs.end())
|
---|
4264 | {
|
---|
4265 | BackRef ref(aMachineId, aSnapshotId);
|
---|
4266 | m->backRefs.push_back(ref);
|
---|
4267 |
|
---|
4268 | return S_OK;
|
---|
4269 | }
|
---|
4270 |
|
---|
4271 | // if the caller has not supplied a snapshot ID, then we're attaching
|
---|
4272 | // to a machine a medium which represents the machine's current state,
|
---|
4273 | // so set the flag
|
---|
4274 |
|
---|
4275 | if (aSnapshotId.isZero())
|
---|
4276 | {
|
---|
4277 | /* sanity: no duplicate attachments */
|
---|
4278 | if (it->fInCurState)
|
---|
4279 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4280 | tr("Cannot attach medium '%s' {%RTuuid}: medium is already associated with the current state of machine uuid {%RTuuid}!"),
|
---|
4281 | m->strLocationFull.c_str(),
|
---|
4282 | m->id.raw(),
|
---|
4283 | aMachineId.raw());
|
---|
4284 | it->fInCurState = true;
|
---|
4285 |
|
---|
4286 | return S_OK;
|
---|
4287 | }
|
---|
4288 |
|
---|
4289 | // otherwise: a snapshot medium is being attached
|
---|
4290 |
|
---|
4291 | /* sanity: no duplicate attachments */
|
---|
4292 | for (GuidList::const_iterator jt = it->llSnapshotIds.begin();
|
---|
4293 | jt != it->llSnapshotIds.end();
|
---|
4294 | ++jt)
|
---|
4295 | {
|
---|
4296 | const Guid &idOldSnapshot = *jt;
|
---|
4297 |
|
---|
4298 | if (idOldSnapshot == aSnapshotId)
|
---|
4299 | {
|
---|
4300 | #ifdef DEBUG
|
---|
4301 | i_dumpBackRefs();
|
---|
4302 | #endif
|
---|
4303 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4304 | tr("Cannot attach medium '%s' {%RTuuid} from snapshot '%RTuuid': medium is already in use by this snapshot!"),
|
---|
4305 | m->strLocationFull.c_str(),
|
---|
4306 | m->id.raw(),
|
---|
4307 | aSnapshotId.raw());
|
---|
4308 | }
|
---|
4309 | }
|
---|
4310 |
|
---|
4311 | it->llSnapshotIds.push_back(aSnapshotId);
|
---|
4312 | // Do not touch fInCurState, as the image may be attached to the current
|
---|
4313 | // state *and* a snapshot, otherwise we lose the current state association!
|
---|
4314 |
|
---|
4315 | LogFlowThisFuncLeave();
|
---|
4316 |
|
---|
4317 | return S_OK;
|
---|
4318 | }
|
---|
4319 |
|
---|
4320 | /**
|
---|
4321 | * Removes the given machine and optionally the snapshot from the list of the
|
---|
4322 | * objects this medium is attached to.
|
---|
4323 | *
|
---|
4324 | * @param aMachineId Machine ID.
|
---|
4325 | * @param aSnapshotId Snapshot ID; when non-empty, removes the snapshot
|
---|
4326 | * attachment.
|
---|
4327 | */
|
---|
4328 | HRESULT Medium::i_removeBackReference(const Guid &aMachineId,
|
---|
4329 | const Guid &aSnapshotId /*= Guid::Empty*/)
|
---|
4330 | {
|
---|
4331 | AssertReturn(aMachineId.isValid(), E_FAIL);
|
---|
4332 |
|
---|
4333 | AutoCaller autoCaller(this);
|
---|
4334 | AssertComRCReturnRC(autoCaller.rc());
|
---|
4335 |
|
---|
4336 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4337 |
|
---|
4338 | BackRefList::iterator it =
|
---|
4339 | std::find_if(m->backRefs.begin(), m->backRefs.end(),
|
---|
4340 | BackRef::EqualsTo(aMachineId));
|
---|
4341 | AssertReturn(it != m->backRefs.end(), E_FAIL);
|
---|
4342 |
|
---|
4343 | if (aSnapshotId.isZero())
|
---|
4344 | {
|
---|
4345 | /* remove the current state attachment */
|
---|
4346 | it->fInCurState = false;
|
---|
4347 | }
|
---|
4348 | else
|
---|
4349 | {
|
---|
4350 | /* remove the snapshot attachment */
|
---|
4351 | GuidList::iterator jt = std::find(it->llSnapshotIds.begin(),
|
---|
4352 | it->llSnapshotIds.end(),
|
---|
4353 | aSnapshotId);
|
---|
4354 |
|
---|
4355 | AssertReturn(jt != it->llSnapshotIds.end(), E_FAIL);
|
---|
4356 | it->llSnapshotIds.erase(jt);
|
---|
4357 | }
|
---|
4358 |
|
---|
4359 | /* if the backref becomes empty, remove it */
|
---|
4360 | if (it->fInCurState == false && it->llSnapshotIds.size() == 0)
|
---|
4361 | m->backRefs.erase(it);
|
---|
4362 |
|
---|
4363 | return S_OK;
|
---|
4364 | }
|
---|
4365 |
|
---|
4366 | /**
|
---|
4367 | * Internal method to return the medium's list of backrefs. Must have caller + locking!
|
---|
4368 | * @return
|
---|
4369 | */
|
---|
4370 | const Guid* Medium::i_getFirstMachineBackrefId() const
|
---|
4371 | {
|
---|
4372 | if (!m->backRefs.size())
|
---|
4373 | return NULL;
|
---|
4374 |
|
---|
4375 | return &m->backRefs.front().machineId;
|
---|
4376 | }
|
---|
4377 |
|
---|
4378 | /**
|
---|
4379 | * Internal method which returns a machine that either this medium or one of its children
|
---|
4380 | * is attached to. This is used for finding a replacement media registry when an existing
|
---|
4381 | * media registry is about to be deleted in VirtualBox::unregisterMachine().
|
---|
4382 | *
|
---|
4383 | * Must have caller + locking, *and* caller must hold the media tree lock!
|
---|
4384 | * @return
|
---|
4385 | */
|
---|
4386 | const Guid* Medium::i_getAnyMachineBackref() const
|
---|
4387 | {
|
---|
4388 | if (m->backRefs.size())
|
---|
4389 | return &m->backRefs.front().machineId;
|
---|
4390 |
|
---|
4391 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
4392 | it != i_getChildren().end();
|
---|
4393 | ++it)
|
---|
4394 | {
|
---|
4395 | Medium *pChild = *it;
|
---|
4396 | // recurse for this child
|
---|
4397 | const Guid* puuid;
|
---|
4398 | if ((puuid = pChild->i_getAnyMachineBackref()))
|
---|
4399 | return puuid;
|
---|
4400 | }
|
---|
4401 |
|
---|
4402 | return NULL;
|
---|
4403 | }
|
---|
4404 |
|
---|
4405 | const Guid* Medium::i_getFirstMachineBackrefSnapshotId() const
|
---|
4406 | {
|
---|
4407 | if (!m->backRefs.size())
|
---|
4408 | return NULL;
|
---|
4409 |
|
---|
4410 | const BackRef &ref = m->backRefs.front();
|
---|
4411 | if (ref.llSnapshotIds.empty())
|
---|
4412 | return NULL;
|
---|
4413 |
|
---|
4414 | return &ref.llSnapshotIds.front();
|
---|
4415 | }
|
---|
4416 |
|
---|
4417 | size_t Medium::i_getMachineBackRefCount() const
|
---|
4418 | {
|
---|
4419 | return m->backRefs.size();
|
---|
4420 | }
|
---|
4421 |
|
---|
4422 | #ifdef DEBUG
|
---|
4423 | /**
|
---|
4424 | * Debugging helper that gets called after VirtualBox initialization that writes all
|
---|
4425 | * machine backreferences to the debug log.
|
---|
4426 | */
|
---|
4427 | void Medium::i_dumpBackRefs()
|
---|
4428 | {
|
---|
4429 | AutoCaller autoCaller(this);
|
---|
4430 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4431 |
|
---|
4432 | LogFlowThisFunc(("Dumping backrefs for medium '%s':\n", m->strLocationFull.c_str()));
|
---|
4433 |
|
---|
4434 | for (BackRefList::iterator it2 = m->backRefs.begin();
|
---|
4435 | it2 != m->backRefs.end();
|
---|
4436 | ++it2)
|
---|
4437 | {
|
---|
4438 | const BackRef &ref = *it2;
|
---|
4439 | LogFlowThisFunc((" Backref from machine {%RTuuid} (fInCurState: %d)\n", ref.machineId.raw(), ref.fInCurState));
|
---|
4440 |
|
---|
4441 | for (GuidList::const_iterator jt2 = it2->llSnapshotIds.begin();
|
---|
4442 | jt2 != it2->llSnapshotIds.end();
|
---|
4443 | ++jt2)
|
---|
4444 | {
|
---|
4445 | const Guid &id = *jt2;
|
---|
4446 | LogFlowThisFunc((" Backref from snapshot {%RTuuid}\n", id.raw()));
|
---|
4447 | }
|
---|
4448 | }
|
---|
4449 | }
|
---|
4450 | #endif
|
---|
4451 |
|
---|
4452 | /**
|
---|
4453 | * Checks if the given change of \a aOldPath to \a aNewPath affects the location
|
---|
4454 | * of this media and updates it if necessary to reflect the new location.
|
---|
4455 | *
|
---|
4456 | * @param strOldPath Old path (full).
|
---|
4457 | * @param strNewPath New path (full).
|
---|
4458 | *
|
---|
4459 | * @note Locks this object for writing.
|
---|
4460 | */
|
---|
4461 | HRESULT Medium::i_updatePath(const Utf8Str &strOldPath, const Utf8Str &strNewPath)
|
---|
4462 | {
|
---|
4463 | AssertReturn(!strOldPath.isEmpty(), E_FAIL);
|
---|
4464 | AssertReturn(!strNewPath.isEmpty(), E_FAIL);
|
---|
4465 |
|
---|
4466 | AutoCaller autoCaller(this);
|
---|
4467 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4468 |
|
---|
4469 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4470 |
|
---|
4471 | LogFlowThisFunc(("locationFull.before='%s'\n", m->strLocationFull.c_str()));
|
---|
4472 |
|
---|
4473 | const char *pcszMediumPath = m->strLocationFull.c_str();
|
---|
4474 |
|
---|
4475 | if (RTPathStartsWith(pcszMediumPath, strOldPath.c_str()))
|
---|
4476 | {
|
---|
4477 | Utf8Str newPath(strNewPath);
|
---|
4478 | newPath.append(pcszMediumPath + strOldPath.length());
|
---|
4479 | unconst(m->strLocationFull) = newPath;
|
---|
4480 |
|
---|
4481 | LogFlowThisFunc(("locationFull.after='%s'\n", m->strLocationFull.c_str()));
|
---|
4482 | // we changed something
|
---|
4483 | return S_OK;
|
---|
4484 | }
|
---|
4485 |
|
---|
4486 | // no change was necessary, signal error which the caller needs to interpret
|
---|
4487 | return VBOX_E_FILE_ERROR;
|
---|
4488 | }
|
---|
4489 |
|
---|
4490 | /**
|
---|
4491 | * Returns the base medium of the media chain this medium is part of.
|
---|
4492 | *
|
---|
4493 | * The base medium is found by walking up the parent-child relationship axis.
|
---|
4494 | * If the medium doesn't have a parent (i.e. it's a base medium), it
|
---|
4495 | * returns itself in response to this method.
|
---|
4496 | *
|
---|
4497 | * @param aLevel Where to store the number of ancestors of this medium
|
---|
4498 | * (zero for the base), may be @c NULL.
|
---|
4499 | *
|
---|
4500 | * @note Locks medium tree for reading.
|
---|
4501 | */
|
---|
4502 | ComObjPtr<Medium> Medium::i_getBase(uint32_t *aLevel /*= NULL*/)
|
---|
4503 | {
|
---|
4504 | ComObjPtr<Medium> pBase;
|
---|
4505 |
|
---|
4506 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
4507 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
4508 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
4509 | if (!pVirtualBox)
|
---|
4510 | return pBase;
|
---|
4511 |
|
---|
4512 | /* we access m->pParent */
|
---|
4513 | AutoReadLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4514 |
|
---|
4515 | AutoCaller autoCaller(this);
|
---|
4516 | AssertReturn(autoCaller.isOk(), pBase);
|
---|
4517 |
|
---|
4518 | pBase = this;
|
---|
4519 | uint32_t level = 0;
|
---|
4520 |
|
---|
4521 | if (m->pParent)
|
---|
4522 | {
|
---|
4523 | for (;;)
|
---|
4524 | {
|
---|
4525 | AutoCaller baseCaller(pBase);
|
---|
4526 | AssertReturn(baseCaller.isOk(), pBase);
|
---|
4527 |
|
---|
4528 | if (pBase->m->pParent.isNull())
|
---|
4529 | break;
|
---|
4530 |
|
---|
4531 | pBase = pBase->m->pParent;
|
---|
4532 | ++level;
|
---|
4533 | }
|
---|
4534 | }
|
---|
4535 |
|
---|
4536 | if (aLevel != NULL)
|
---|
4537 | *aLevel = level;
|
---|
4538 |
|
---|
4539 | return pBase;
|
---|
4540 | }
|
---|
4541 |
|
---|
4542 | /**
|
---|
4543 | * Returns the depth of this medium in the media chain.
|
---|
4544 | *
|
---|
4545 | * @note Locks medium tree for reading.
|
---|
4546 | */
|
---|
4547 | uint32_t Medium::i_getDepth()
|
---|
4548 | {
|
---|
4549 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
4550 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
4551 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
4552 | if (!pVirtualBox)
|
---|
4553 | return 1;
|
---|
4554 |
|
---|
4555 | /* we access m->pParent */
|
---|
4556 | AutoReadLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4557 |
|
---|
4558 | uint32_t cDepth = 0;
|
---|
4559 | ComObjPtr<Medium> pMedium(this);
|
---|
4560 | while (!pMedium.isNull())
|
---|
4561 | {
|
---|
4562 | AutoCaller autoCaller(this);
|
---|
4563 | AssertReturn(autoCaller.isOk(), cDepth + 1);
|
---|
4564 |
|
---|
4565 | pMedium = pMedium->m->pParent;
|
---|
4566 | cDepth++;
|
---|
4567 | }
|
---|
4568 |
|
---|
4569 | return cDepth;
|
---|
4570 | }
|
---|
4571 |
|
---|
4572 | /**
|
---|
4573 | * Returns @c true if this medium cannot be modified because it has
|
---|
4574 | * dependents (children) or is part of the snapshot. Related to the medium
|
---|
4575 | * type and posterity, not to the current media state.
|
---|
4576 | *
|
---|
4577 | * @note Locks this object and medium tree for reading.
|
---|
4578 | */
|
---|
4579 | bool Medium::i_isReadOnly()
|
---|
4580 | {
|
---|
4581 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
4582 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
4583 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
4584 | if (!pVirtualBox)
|
---|
4585 | return false;
|
---|
4586 |
|
---|
4587 | /* we access children */
|
---|
4588 | AutoReadLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4589 |
|
---|
4590 | AutoCaller autoCaller(this);
|
---|
4591 | AssertComRCReturn(autoCaller.rc(), false);
|
---|
4592 |
|
---|
4593 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4594 |
|
---|
4595 | switch (m->type)
|
---|
4596 | {
|
---|
4597 | case MediumType_Normal:
|
---|
4598 | {
|
---|
4599 | if (i_getChildren().size() != 0)
|
---|
4600 | return true;
|
---|
4601 |
|
---|
4602 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
4603 | it != m->backRefs.end(); ++it)
|
---|
4604 | if (it->llSnapshotIds.size() != 0)
|
---|
4605 | return true;
|
---|
4606 |
|
---|
4607 | if (m->variant & MediumVariant_VmdkStreamOptimized)
|
---|
4608 | return true;
|
---|
4609 |
|
---|
4610 | return false;
|
---|
4611 | }
|
---|
4612 | case MediumType_Immutable:
|
---|
4613 | case MediumType_MultiAttach:
|
---|
4614 | return true;
|
---|
4615 | case MediumType_Writethrough:
|
---|
4616 | case MediumType_Shareable:
|
---|
4617 | case MediumType_Readonly: /* explicit readonly media has no diffs */
|
---|
4618 | return false;
|
---|
4619 | default:
|
---|
4620 | break;
|
---|
4621 | }
|
---|
4622 |
|
---|
4623 | AssertFailedReturn(false);
|
---|
4624 | }
|
---|
4625 |
|
---|
4626 | /**
|
---|
4627 | * Internal method to return the medium's size. Must have caller + locking!
|
---|
4628 | * @return
|
---|
4629 | */
|
---|
4630 | void Medium::i_updateId(const Guid &id)
|
---|
4631 | {
|
---|
4632 | unconst(m->id) = id;
|
---|
4633 | }
|
---|
4634 |
|
---|
4635 | /**
|
---|
4636 | * Saves the settings of one medium.
|
---|
4637 | *
|
---|
4638 | * @note Caller MUST take care of the medium tree lock and caller.
|
---|
4639 | *
|
---|
4640 | * @param data Settings struct to be updated.
|
---|
4641 | * @param strHardDiskFolder Folder for which paths should be relative.
|
---|
4642 | */
|
---|
4643 | void Medium::i_saveSettingsOne(settings::Medium &data, const Utf8Str &strHardDiskFolder)
|
---|
4644 | {
|
---|
4645 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4646 |
|
---|
4647 | data.uuid = m->id;
|
---|
4648 |
|
---|
4649 | // make path relative if needed
|
---|
4650 | if ( !strHardDiskFolder.isEmpty()
|
---|
4651 | && RTPathStartsWith(m->strLocationFull.c_str(), strHardDiskFolder.c_str())
|
---|
4652 | )
|
---|
4653 | data.strLocation = m->strLocationFull.substr(strHardDiskFolder.length() + 1);
|
---|
4654 | else
|
---|
4655 | data.strLocation = m->strLocationFull;
|
---|
4656 | data.strFormat = m->strFormat;
|
---|
4657 |
|
---|
4658 | /* optional, only for diffs, default is false */
|
---|
4659 | if (m->pParent)
|
---|
4660 | data.fAutoReset = m->autoReset;
|
---|
4661 | else
|
---|
4662 | data.fAutoReset = false;
|
---|
4663 |
|
---|
4664 | /* optional */
|
---|
4665 | data.strDescription = m->strDescription;
|
---|
4666 |
|
---|
4667 | /* optional properties */
|
---|
4668 | data.properties.clear();
|
---|
4669 |
|
---|
4670 | /* handle iSCSI initiator secrets transparently */
|
---|
4671 | bool fHaveInitiatorSecretEncrypted = false;
|
---|
4672 | Utf8Str strCiphertext;
|
---|
4673 | settings::StringsMap::const_iterator itPln = m->mapProperties.find("InitiatorSecret");
|
---|
4674 | if ( itPln != m->mapProperties.end()
|
---|
4675 | && !itPln->second.isEmpty())
|
---|
4676 | {
|
---|
4677 | /* Encrypt the plain secret. If that does not work (i.e. no or wrong settings key
|
---|
4678 | * specified), just use the encrypted secret (if there is any). */
|
---|
4679 | int rc = m->pVirtualBox->i_encryptSetting(itPln->second, &strCiphertext);
|
---|
4680 | if (RT_SUCCESS(rc))
|
---|
4681 | fHaveInitiatorSecretEncrypted = true;
|
---|
4682 | }
|
---|
4683 | for (settings::StringsMap::const_iterator it = m->mapProperties.begin();
|
---|
4684 | it != m->mapProperties.end();
|
---|
4685 | ++it)
|
---|
4686 | {
|
---|
4687 | /* only save properties that have non-default values */
|
---|
4688 | if (!it->second.isEmpty())
|
---|
4689 | {
|
---|
4690 | const Utf8Str &name = it->first;
|
---|
4691 | const Utf8Str &value = it->second;
|
---|
4692 | /* do NOT store the plain InitiatorSecret */
|
---|
4693 | if ( !fHaveInitiatorSecretEncrypted
|
---|
4694 | || !name.equals("InitiatorSecret"))
|
---|
4695 | data.properties[name] = value;
|
---|
4696 | }
|
---|
4697 | }
|
---|
4698 | if (fHaveInitiatorSecretEncrypted)
|
---|
4699 | data.properties["InitiatorSecretEncrypted"] = strCiphertext;
|
---|
4700 |
|
---|
4701 | /* only for base media */
|
---|
4702 | if (m->pParent.isNull())
|
---|
4703 | data.hdType = m->type;
|
---|
4704 | }
|
---|
4705 |
|
---|
4706 | /**
|
---|
4707 | * Saves medium data by putting it into the provided data structure.
|
---|
4708 | * Recurses over all children to save their settings, too.
|
---|
4709 | *
|
---|
4710 | * @param data Settings struct to be updated.
|
---|
4711 | * @param strHardDiskFolder Folder for which paths should be relative.
|
---|
4712 | *
|
---|
4713 | * @note Locks this object, medium tree and children for reading.
|
---|
4714 | */
|
---|
4715 | HRESULT Medium::i_saveSettings(settings::Medium &data,
|
---|
4716 | const Utf8Str &strHardDiskFolder)
|
---|
4717 | {
|
---|
4718 | /* we access m->pParent */
|
---|
4719 | AutoReadLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4720 |
|
---|
4721 | AutoCaller autoCaller(this);
|
---|
4722 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4723 |
|
---|
4724 | i_saveSettingsOne(data, strHardDiskFolder);
|
---|
4725 |
|
---|
4726 | /* save all children */
|
---|
4727 | settings::MediaList &llSettingsChildren = data.llChildren;
|
---|
4728 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
4729 | it != i_getChildren().end();
|
---|
4730 | ++it)
|
---|
4731 | {
|
---|
4732 | // Use the element straight in the list to reduce both unnecessary
|
---|
4733 | // deep copying (when unwinding the recursion the entire medium
|
---|
4734 | // settings sub-tree is copied) and the stack footprint (the settings
|
---|
4735 | // need almost 1K, and there can be VMs with long image chains.
|
---|
4736 | llSettingsChildren.push_back(settings::Medium::Empty);
|
---|
4737 | HRESULT rc = (*it)->i_saveSettings(llSettingsChildren.back(), strHardDiskFolder);
|
---|
4738 | if (FAILED(rc))
|
---|
4739 | {
|
---|
4740 | llSettingsChildren.pop_back();
|
---|
4741 | return rc;
|
---|
4742 | }
|
---|
4743 | }
|
---|
4744 |
|
---|
4745 | return S_OK;
|
---|
4746 | }
|
---|
4747 |
|
---|
4748 | /**
|
---|
4749 | * Constructs a medium lock list for this medium. The lock is not taken.
|
---|
4750 | *
|
---|
4751 | * @note Caller MUST NOT hold the media tree or medium lock.
|
---|
4752 | *
|
---|
4753 | * @param fFailIfInaccessible If true, this fails with an error if a medium is inaccessible. If false,
|
---|
4754 | * inaccessible media are silently skipped and not locked (i.e. their state remains "Inaccessible");
|
---|
4755 | * this is necessary for a VM's removable media VM startup for which we do not want to fail.
|
---|
4756 | * @param pToLockWrite If not NULL, associate a write lock with this medium object.
|
---|
4757 | * @param fMediumLockWriteAll Whether to associate a write lock to all other media too.
|
---|
4758 | * @param pToBeParent Medium which will become the parent of this medium.
|
---|
4759 | * @param mediumLockList Where to store the resulting list.
|
---|
4760 | */
|
---|
4761 | HRESULT Medium::i_createMediumLockList(bool fFailIfInaccessible,
|
---|
4762 | Medium *pToLockWrite,
|
---|
4763 | bool fMediumLockWriteAll,
|
---|
4764 | Medium *pToBeParent,
|
---|
4765 | MediumLockList &mediumLockList)
|
---|
4766 | {
|
---|
4767 | /** @todo r=klaus this needs to be reworked, as the code below uses
|
---|
4768 | * i_getParent without holding the tree lock, and changing this is
|
---|
4769 | * a significant amount of effort. */
|
---|
4770 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
4771 | Assert(!isWriteLockOnCurrentThread());
|
---|
4772 |
|
---|
4773 | AutoCaller autoCaller(this);
|
---|
4774 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4775 |
|
---|
4776 | HRESULT rc = S_OK;
|
---|
4777 |
|
---|
4778 | /* paranoid sanity checking if the medium has a to-be parent medium */
|
---|
4779 | if (pToBeParent)
|
---|
4780 | {
|
---|
4781 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4782 | ComAssertRet(i_getParent().isNull(), E_FAIL);
|
---|
4783 | ComAssertRet(i_getChildren().size() == 0, E_FAIL);
|
---|
4784 | }
|
---|
4785 |
|
---|
4786 | ErrorInfoKeeper eik;
|
---|
4787 | MultiResult mrc(S_OK);
|
---|
4788 |
|
---|
4789 | ComObjPtr<Medium> pMedium = this;
|
---|
4790 | while (!pMedium.isNull())
|
---|
4791 | {
|
---|
4792 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
4793 |
|
---|
4794 | /* Accessibility check must be first, otherwise locking interferes
|
---|
4795 | * with getting the medium state. Lock lists are not created for
|
---|
4796 | * fun, and thus getting the medium status is no luxury. */
|
---|
4797 | MediumState_T mediumState = pMedium->i_getState();
|
---|
4798 | if (mediumState == MediumState_Inaccessible)
|
---|
4799 | {
|
---|
4800 | alock.release();
|
---|
4801 | rc = pMedium->i_queryInfo(false /* fSetImageId */, false /* fSetParentId */,
|
---|
4802 | autoCaller);
|
---|
4803 | alock.acquire();
|
---|
4804 | if (FAILED(rc)) return rc;
|
---|
4805 |
|
---|
4806 | mediumState = pMedium->i_getState();
|
---|
4807 | if (mediumState == MediumState_Inaccessible)
|
---|
4808 | {
|
---|
4809 | // ignore inaccessible ISO media and silently return S_OK,
|
---|
4810 | // otherwise VM startup (esp. restore) may fail without good reason
|
---|
4811 | if (!fFailIfInaccessible)
|
---|
4812 | return S_OK;
|
---|
4813 |
|
---|
4814 | // otherwise report an error
|
---|
4815 | Bstr error;
|
---|
4816 | rc = pMedium->COMGETTER(LastAccessError)(error.asOutParam());
|
---|
4817 | if (FAILED(rc)) return rc;
|
---|
4818 |
|
---|
4819 | /* collect multiple errors */
|
---|
4820 | eik.restore();
|
---|
4821 | Assert(!error.isEmpty());
|
---|
4822 | mrc = setError(E_FAIL,
|
---|
4823 | "%ls",
|
---|
4824 | error.raw());
|
---|
4825 | // error message will be something like
|
---|
4826 | // "Could not open the medium ... VD: error VERR_FILE_NOT_FOUND opening image file ... (VERR_FILE_NOT_FOUND).
|
---|
4827 | eik.fetch();
|
---|
4828 | }
|
---|
4829 | }
|
---|
4830 |
|
---|
4831 | if (pMedium == pToLockWrite)
|
---|
4832 | mediumLockList.Prepend(pMedium, true);
|
---|
4833 | else
|
---|
4834 | mediumLockList.Prepend(pMedium, fMediumLockWriteAll);
|
---|
4835 |
|
---|
4836 | pMedium = pMedium->i_getParent();
|
---|
4837 | if (pMedium.isNull() && pToBeParent)
|
---|
4838 | {
|
---|
4839 | pMedium = pToBeParent;
|
---|
4840 | pToBeParent = NULL;
|
---|
4841 | }
|
---|
4842 | }
|
---|
4843 |
|
---|
4844 | return mrc;
|
---|
4845 | }
|
---|
4846 |
|
---|
4847 | /**
|
---|
4848 | * Creates a new differencing storage unit using the format of the given target
|
---|
4849 | * medium and the location. Note that @c aTarget must be NotCreated.
|
---|
4850 | *
|
---|
4851 | * The @a aMediumLockList parameter contains the associated medium lock list,
|
---|
4852 | * which must be in locked state. If @a aWait is @c true then the caller is
|
---|
4853 | * responsible for unlocking.
|
---|
4854 | *
|
---|
4855 | * If @a aProgress is not NULL but the object it points to is @c null then a
|
---|
4856 | * new progress object will be created and assigned to @a *aProgress on
|
---|
4857 | * success, otherwise the existing progress object is used. If @a aProgress is
|
---|
4858 | * NULL, then no progress object is created/used at all.
|
---|
4859 | *
|
---|
4860 | * When @a aWait is @c false, this method will create a thread to perform the
|
---|
4861 | * create operation asynchronously and will return immediately. Otherwise, it
|
---|
4862 | * will perform the operation on the calling thread and will not return to the
|
---|
4863 | * caller until the operation is completed. Note that @a aProgress cannot be
|
---|
4864 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
4865 | *
|
---|
4866 | * @param aTarget Target medium.
|
---|
4867 | * @param aVariant Precise medium variant to create.
|
---|
4868 | * @param aMediumLockList List of media which should be locked.
|
---|
4869 | * @param aProgress Where to find/store a Progress object to track
|
---|
4870 | * operation completion.
|
---|
4871 | * @param aWait @c true if this method should block instead of
|
---|
4872 | * creating an asynchronous thread.
|
---|
4873 | *
|
---|
4874 | * @note Locks this object and @a aTarget for writing.
|
---|
4875 | */
|
---|
4876 | HRESULT Medium::i_createDiffStorage(ComObjPtr<Medium> &aTarget,
|
---|
4877 | MediumVariant_T aVariant,
|
---|
4878 | MediumLockList *aMediumLockList,
|
---|
4879 | ComObjPtr<Progress> *aProgress,
|
---|
4880 | bool aWait)
|
---|
4881 | {
|
---|
4882 | AssertReturn(!aTarget.isNull(), E_FAIL);
|
---|
4883 | AssertReturn(aMediumLockList, E_FAIL);
|
---|
4884 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
4885 |
|
---|
4886 | AutoCaller autoCaller(this);
|
---|
4887 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4888 |
|
---|
4889 | AutoCaller targetCaller(aTarget);
|
---|
4890 | if (FAILED(targetCaller.rc())) return targetCaller.rc();
|
---|
4891 |
|
---|
4892 | HRESULT rc = S_OK;
|
---|
4893 | ComObjPtr<Progress> pProgress;
|
---|
4894 | Medium::Task *pTask = NULL;
|
---|
4895 |
|
---|
4896 | try
|
---|
4897 | {
|
---|
4898 | AutoMultiWriteLock2 alock(this, aTarget COMMA_LOCKVAL_SRC_POS);
|
---|
4899 |
|
---|
4900 | ComAssertThrow( m->type != MediumType_Writethrough
|
---|
4901 | && m->type != MediumType_Shareable
|
---|
4902 | && m->type != MediumType_Readonly, E_FAIL);
|
---|
4903 | ComAssertThrow(m->state == MediumState_LockedRead, E_FAIL);
|
---|
4904 |
|
---|
4905 | if (aTarget->m->state != MediumState_NotCreated)
|
---|
4906 | throw aTarget->i_setStateError();
|
---|
4907 |
|
---|
4908 | /* Check that the medium is not attached to the current state of
|
---|
4909 | * any VM referring to it. */
|
---|
4910 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
4911 | it != m->backRefs.end();
|
---|
4912 | ++it)
|
---|
4913 | {
|
---|
4914 | if (it->fInCurState)
|
---|
4915 | {
|
---|
4916 | /* Note: when a VM snapshot is being taken, all normal media
|
---|
4917 | * attached to the VM in the current state will be, as an
|
---|
4918 | * exception, also associated with the snapshot which is about
|
---|
4919 | * to create (see SnapshotMachine::init()) before deassociating
|
---|
4920 | * them from the current state (which takes place only on
|
---|
4921 | * success in Machine::fixupHardDisks()), so that the size of
|
---|
4922 | * snapshotIds will be 1 in this case. The extra condition is
|
---|
4923 | * used to filter out this legal situation. */
|
---|
4924 | if (it->llSnapshotIds.size() == 0)
|
---|
4925 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4926 | 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"),
|
---|
4927 | m->strLocationFull.c_str(), it->machineId.raw());
|
---|
4928 |
|
---|
4929 | Assert(it->llSnapshotIds.size() == 1);
|
---|
4930 | }
|
---|
4931 | }
|
---|
4932 |
|
---|
4933 | if (aProgress != NULL)
|
---|
4934 | {
|
---|
4935 | /* use the existing progress object... */
|
---|
4936 | pProgress = *aProgress;
|
---|
4937 |
|
---|
4938 | /* ...but create a new one if it is null */
|
---|
4939 | if (pProgress.isNull())
|
---|
4940 | {
|
---|
4941 | pProgress.createObject();
|
---|
4942 | rc = pProgress->init(m->pVirtualBox,
|
---|
4943 | static_cast<IMedium*>(this),
|
---|
4944 | BstrFmt(tr("Creating differencing medium storage unit '%s'"),
|
---|
4945 | aTarget->m->strLocationFull.c_str()).raw(),
|
---|
4946 | TRUE /* aCancelable */);
|
---|
4947 | if (FAILED(rc))
|
---|
4948 | throw rc;
|
---|
4949 | }
|
---|
4950 | }
|
---|
4951 |
|
---|
4952 | /* setup task object to carry out the operation sync/async */
|
---|
4953 | pTask = new Medium::CreateDiffTask(this, pProgress, aTarget, aVariant,
|
---|
4954 | aMediumLockList,
|
---|
4955 | aWait /* fKeepMediumLockList */);
|
---|
4956 | rc = pTask->rc();
|
---|
4957 | AssertComRC(rc);
|
---|
4958 | if (FAILED(rc))
|
---|
4959 | throw rc;
|
---|
4960 |
|
---|
4961 | /* register a task (it will deregister itself when done) */
|
---|
4962 | ++m->numCreateDiffTasks;
|
---|
4963 | Assert(m->numCreateDiffTasks != 0); /* overflow? */
|
---|
4964 |
|
---|
4965 | aTarget->m->state = MediumState_Creating;
|
---|
4966 | }
|
---|
4967 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4968 |
|
---|
4969 | if (SUCCEEDED(rc))
|
---|
4970 | {
|
---|
4971 | if (aWait)
|
---|
4972 | {
|
---|
4973 | rc = pTask->runNow();
|
---|
4974 |
|
---|
4975 | delete pTask;
|
---|
4976 | }
|
---|
4977 | else
|
---|
4978 | rc = pTask->createThread();
|
---|
4979 |
|
---|
4980 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
4981 | *aProgress = pProgress;
|
---|
4982 | }
|
---|
4983 | else if (pTask != NULL)
|
---|
4984 | delete pTask;
|
---|
4985 |
|
---|
4986 | return rc;
|
---|
4987 | }
|
---|
4988 |
|
---|
4989 | /**
|
---|
4990 | * Returns a preferred format for differencing media.
|
---|
4991 | */
|
---|
4992 | Utf8Str Medium::i_getPreferredDiffFormat()
|
---|
4993 | {
|
---|
4994 | AutoCaller autoCaller(this);
|
---|
4995 | AssertComRCReturn(autoCaller.rc(), Utf8Str::Empty);
|
---|
4996 |
|
---|
4997 | /* check that our own format supports diffs */
|
---|
4998 | if (!(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_Differencing))
|
---|
4999 | {
|
---|
5000 | /* use the default format if not */
|
---|
5001 | Utf8Str tmp;
|
---|
5002 | m->pVirtualBox->i_getDefaultHardDiskFormat(tmp);
|
---|
5003 | return tmp;
|
---|
5004 | }
|
---|
5005 |
|
---|
5006 | /* m->strFormat is const, no need to lock */
|
---|
5007 | return m->strFormat;
|
---|
5008 | }
|
---|
5009 |
|
---|
5010 | /**
|
---|
5011 | * Returns a preferred variant for differencing media.
|
---|
5012 | */
|
---|
5013 | MediumVariant_T Medium::i_getPreferredDiffVariant()
|
---|
5014 | {
|
---|
5015 | AutoCaller autoCaller(this);
|
---|
5016 | AssertComRCReturn(autoCaller.rc(), MediumVariant_Standard);
|
---|
5017 |
|
---|
5018 | /* check that our own format supports diffs */
|
---|
5019 | if (!(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_Differencing))
|
---|
5020 | return MediumVariant_Standard;
|
---|
5021 |
|
---|
5022 | /* m->variant is const, no need to lock */
|
---|
5023 | ULONG mediumVariantFlags = (ULONG)m->variant;
|
---|
5024 | mediumVariantFlags &= ~(MediumVariant_Fixed | MediumVariant_VmdkStreamOptimized);
|
---|
5025 | mediumVariantFlags |= MediumVariant_Diff;
|
---|
5026 | return (MediumVariant_T)mediumVariantFlags;
|
---|
5027 | }
|
---|
5028 |
|
---|
5029 | /**
|
---|
5030 | * Implementation for the public Medium::Close() with the exception of calling
|
---|
5031 | * VirtualBox::saveRegistries(), in case someone wants to call this for several
|
---|
5032 | * media.
|
---|
5033 | *
|
---|
5034 | * After this returns with success, uninit() has been called on the medium, and
|
---|
5035 | * the object is no longer usable ("not ready" state).
|
---|
5036 | *
|
---|
5037 | * @param autoCaller AutoCaller instance which must have been created on the caller's
|
---|
5038 | * stack for this medium. This gets released hereupon
|
---|
5039 | * which the Medium instance gets uninitialized.
|
---|
5040 | * @return
|
---|
5041 | */
|
---|
5042 | HRESULT Medium::i_close(AutoCaller &autoCaller)
|
---|
5043 | {
|
---|
5044 | // must temporarily drop the caller, need the tree lock first
|
---|
5045 | autoCaller.release();
|
---|
5046 |
|
---|
5047 | // we're accessing parent/child and backrefs, so lock the tree first, then ourselves
|
---|
5048 | AutoMultiWriteLock2 multilock(&m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
5049 | this->lockHandle()
|
---|
5050 | COMMA_LOCKVAL_SRC_POS);
|
---|
5051 |
|
---|
5052 | autoCaller.add();
|
---|
5053 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
5054 |
|
---|
5055 | LogFlowFunc(("ENTER for %s\n", i_getLocationFull().c_str()));
|
---|
5056 |
|
---|
5057 | bool wasCreated = true;
|
---|
5058 |
|
---|
5059 | switch (m->state)
|
---|
5060 | {
|
---|
5061 | case MediumState_NotCreated:
|
---|
5062 | wasCreated = false;
|
---|
5063 | break;
|
---|
5064 | case MediumState_Created:
|
---|
5065 | case MediumState_Inaccessible:
|
---|
5066 | break;
|
---|
5067 | default:
|
---|
5068 | return i_setStateError();
|
---|
5069 | }
|
---|
5070 |
|
---|
5071 | if (m->backRefs.size() != 0)
|
---|
5072 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
5073 | tr("Medium '%s' cannot be closed because it is still attached to %d virtual machines"),
|
---|
5074 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
5075 |
|
---|
5076 | // perform extra media-dependent close checks
|
---|
5077 | HRESULT rc = i_canClose();
|
---|
5078 | if (FAILED(rc)) return rc;
|
---|
5079 |
|
---|
5080 | m->fClosing = true;
|
---|
5081 |
|
---|
5082 | if (wasCreated)
|
---|
5083 | {
|
---|
5084 | // remove from the list of known media before performing actual
|
---|
5085 | // uninitialization (to keep the media registry consistent on
|
---|
5086 | // failure to do so)
|
---|
5087 | rc = i_unregisterWithVirtualBox();
|
---|
5088 | if (FAILED(rc)) return rc;
|
---|
5089 |
|
---|
5090 | multilock.release();
|
---|
5091 | // Release the AutoCaller now, as otherwise uninit() will simply hang.
|
---|
5092 | // Needs to be done before mark the registries as modified and saving
|
---|
5093 | // the registry, as otherwise there may be a deadlock with someone else
|
---|
5094 | // closing this object while we're in i_saveModifiedRegistries(), which
|
---|
5095 | // needs the media tree lock, which the other thread holds until after
|
---|
5096 | // uninit() below.
|
---|
5097 | autoCaller.release();
|
---|
5098 | i_markRegistriesModified();
|
---|
5099 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
5100 | }
|
---|
5101 | else
|
---|
5102 | {
|
---|
5103 | multilock.release();
|
---|
5104 | // release the AutoCaller, as otherwise uninit() will simply hang
|
---|
5105 | autoCaller.release();
|
---|
5106 | }
|
---|
5107 |
|
---|
5108 | // Keep the locks held until after uninit, as otherwise the consistency
|
---|
5109 | // of the medium tree cannot be guaranteed.
|
---|
5110 | uninit();
|
---|
5111 |
|
---|
5112 | LogFlowFuncLeave();
|
---|
5113 |
|
---|
5114 | return rc;
|
---|
5115 | }
|
---|
5116 |
|
---|
5117 | /**
|
---|
5118 | * Deletes the medium storage unit.
|
---|
5119 | *
|
---|
5120 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
5121 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
5122 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
5123 | * progress object is created/used at all.
|
---|
5124 | *
|
---|
5125 | * When @a aWait is @c false, this method will create a thread to perform the
|
---|
5126 | * delete operation asynchronously and will return immediately. Otherwise, it
|
---|
5127 | * will perform the operation on the calling thread and will not return to the
|
---|
5128 | * caller until the operation is completed. Note that @a aProgress cannot be
|
---|
5129 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
5130 | *
|
---|
5131 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
5132 | * completion.
|
---|
5133 | * @param aWait @c true if this method should block instead of creating
|
---|
5134 | * an asynchronous thread.
|
---|
5135 | *
|
---|
5136 | * @note Locks mVirtualBox and this object for writing. Locks medium tree for
|
---|
5137 | * writing.
|
---|
5138 | */
|
---|
5139 | HRESULT Medium::i_deleteStorage(ComObjPtr<Progress> *aProgress,
|
---|
5140 | bool aWait)
|
---|
5141 | {
|
---|
5142 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
5143 |
|
---|
5144 | HRESULT rc = S_OK;
|
---|
5145 | ComObjPtr<Progress> pProgress;
|
---|
5146 | Medium::Task *pTask = NULL;
|
---|
5147 |
|
---|
5148 | try
|
---|
5149 | {
|
---|
5150 | /* we're accessing the media tree, and canClose() needs it too */
|
---|
5151 | AutoWriteLock treelock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5152 |
|
---|
5153 | AutoCaller autoCaller(this);
|
---|
5154 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5155 |
|
---|
5156 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5157 |
|
---|
5158 | LogFlowThisFunc(("aWait=%RTbool locationFull=%s\n", aWait, i_getLocationFull().c_str() ));
|
---|
5159 |
|
---|
5160 | if ( !(m->formatObj->i_getCapabilities() & ( MediumFormatCapabilities_CreateDynamic
|
---|
5161 | | MediumFormatCapabilities_CreateFixed)))
|
---|
5162 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
5163 | tr("Medium format '%s' does not support storage deletion"),
|
---|
5164 | m->strFormat.c_str());
|
---|
5165 |
|
---|
5166 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
5167 | /** @todo r=klaus would be great if this could be moved to the async
|
---|
5168 | * part of the operation as it can take quite a while */
|
---|
5169 | if (m->queryInfoRunning)
|
---|
5170 | {
|
---|
5171 | while (m->queryInfoRunning)
|
---|
5172 | {
|
---|
5173 | alock.release();
|
---|
5174 | autoCaller.release();
|
---|
5175 | treelock.release();
|
---|
5176 | /* Must not hold the media tree lock or the object lock, as
|
---|
5177 | * Medium::i_queryInfo needs this lock and thus we would run
|
---|
5178 | * into a deadlock here. */
|
---|
5179 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
5180 | Assert(!isWriteLockOnCurrentThread());
|
---|
5181 | {
|
---|
5182 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
5183 | }
|
---|
5184 | treelock.acquire();
|
---|
5185 | autoCaller.add();
|
---|
5186 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5187 | alock.acquire();
|
---|
5188 | }
|
---|
5189 | }
|
---|
5190 |
|
---|
5191 | /* Note that we are fine with Inaccessible state too: a) for symmetry
|
---|
5192 | * with create calls and b) because it doesn't really harm to try, if
|
---|
5193 | * it is really inaccessible, the delete operation will fail anyway.
|
---|
5194 | * Accepting Inaccessible state is especially important because all
|
---|
5195 | * registered media are initially Inaccessible upon VBoxSVC startup
|
---|
5196 | * until COMGETTER(RefreshState) is called. Accept Deleting state
|
---|
5197 | * because some callers need to put the medium in this state early
|
---|
5198 | * to prevent races. */
|
---|
5199 | switch (m->state)
|
---|
5200 | {
|
---|
5201 | case MediumState_Created:
|
---|
5202 | case MediumState_Deleting:
|
---|
5203 | case MediumState_Inaccessible:
|
---|
5204 | break;
|
---|
5205 | default:
|
---|
5206 | throw i_setStateError();
|
---|
5207 | }
|
---|
5208 |
|
---|
5209 | if (m->backRefs.size() != 0)
|
---|
5210 | {
|
---|
5211 | Utf8Str strMachines;
|
---|
5212 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
5213 | it != m->backRefs.end();
|
---|
5214 | ++it)
|
---|
5215 | {
|
---|
5216 | const BackRef &b = *it;
|
---|
5217 | if (strMachines.length())
|
---|
5218 | strMachines.append(", ");
|
---|
5219 | strMachines.append(b.machineId.toString().c_str());
|
---|
5220 | }
|
---|
5221 | #ifdef DEBUG
|
---|
5222 | i_dumpBackRefs();
|
---|
5223 | #endif
|
---|
5224 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5225 | tr("Cannot delete storage: medium '%s' is still attached to the following %d virtual machine(s): %s"),
|
---|
5226 | m->strLocationFull.c_str(),
|
---|
5227 | m->backRefs.size(),
|
---|
5228 | strMachines.c_str());
|
---|
5229 | }
|
---|
5230 |
|
---|
5231 | rc = i_canClose();
|
---|
5232 | if (FAILED(rc))
|
---|
5233 | throw rc;
|
---|
5234 |
|
---|
5235 | /* go to Deleting state, so that the medium is not actually locked */
|
---|
5236 | if (m->state != MediumState_Deleting)
|
---|
5237 | {
|
---|
5238 | rc = i_markForDeletion();
|
---|
5239 | if (FAILED(rc))
|
---|
5240 | throw rc;
|
---|
5241 | }
|
---|
5242 |
|
---|
5243 | /* Build the medium lock list. */
|
---|
5244 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
5245 | alock.release();
|
---|
5246 | autoCaller.release();
|
---|
5247 | treelock.release();
|
---|
5248 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5249 | this /* pToLockWrite */,
|
---|
5250 | false /* fMediumLockWriteAll */,
|
---|
5251 | NULL,
|
---|
5252 | *pMediumLockList);
|
---|
5253 | treelock.acquire();
|
---|
5254 | autoCaller.add();
|
---|
5255 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5256 | alock.acquire();
|
---|
5257 | if (FAILED(rc))
|
---|
5258 | {
|
---|
5259 | delete pMediumLockList;
|
---|
5260 | throw rc;
|
---|
5261 | }
|
---|
5262 |
|
---|
5263 | alock.release();
|
---|
5264 | autoCaller.release();
|
---|
5265 | treelock.release();
|
---|
5266 | rc = pMediumLockList->Lock();
|
---|
5267 | treelock.acquire();
|
---|
5268 | autoCaller.add();
|
---|
5269 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5270 | alock.acquire();
|
---|
5271 | if (FAILED(rc))
|
---|
5272 | {
|
---|
5273 | delete pMediumLockList;
|
---|
5274 | throw setError(rc,
|
---|
5275 | tr("Failed to lock media when deleting '%s'"),
|
---|
5276 | i_getLocationFull().c_str());
|
---|
5277 | }
|
---|
5278 |
|
---|
5279 | /* try to remove from the list of known media before performing
|
---|
5280 | * actual deletion (we favor the consistency of the media registry
|
---|
5281 | * which would have been broken if unregisterWithVirtualBox() failed
|
---|
5282 | * after we successfully deleted the storage) */
|
---|
5283 | rc = i_unregisterWithVirtualBox();
|
---|
5284 | if (FAILED(rc))
|
---|
5285 | throw rc;
|
---|
5286 | // no longer need lock
|
---|
5287 | alock.release();
|
---|
5288 | autoCaller.release();
|
---|
5289 | treelock.release();
|
---|
5290 | i_markRegistriesModified();
|
---|
5291 |
|
---|
5292 | if (aProgress != NULL)
|
---|
5293 | {
|
---|
5294 | /* use the existing progress object... */
|
---|
5295 | pProgress = *aProgress;
|
---|
5296 |
|
---|
5297 | /* ...but create a new one if it is null */
|
---|
5298 | if (pProgress.isNull())
|
---|
5299 | {
|
---|
5300 | pProgress.createObject();
|
---|
5301 | rc = pProgress->init(m->pVirtualBox,
|
---|
5302 | static_cast<IMedium*>(this),
|
---|
5303 | BstrFmt(tr("Deleting medium storage unit '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
5304 | FALSE /* aCancelable */);
|
---|
5305 | if (FAILED(rc))
|
---|
5306 | throw rc;
|
---|
5307 | }
|
---|
5308 | }
|
---|
5309 |
|
---|
5310 | /* setup task object to carry out the operation sync/async */
|
---|
5311 | pTask = new Medium::DeleteTask(this, pProgress, pMediumLockList);
|
---|
5312 | rc = pTask->rc();
|
---|
5313 | AssertComRC(rc);
|
---|
5314 | if (FAILED(rc))
|
---|
5315 | throw rc;
|
---|
5316 | }
|
---|
5317 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5318 |
|
---|
5319 | if (SUCCEEDED(rc))
|
---|
5320 | {
|
---|
5321 | if (aWait)
|
---|
5322 | {
|
---|
5323 | rc = pTask->runNow();
|
---|
5324 |
|
---|
5325 | delete pTask;
|
---|
5326 | }
|
---|
5327 | else
|
---|
5328 | rc = pTask->createThread();
|
---|
5329 |
|
---|
5330 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
5331 | *aProgress = pProgress;
|
---|
5332 |
|
---|
5333 | }
|
---|
5334 | else
|
---|
5335 | {
|
---|
5336 | if (pTask)
|
---|
5337 | delete pTask;
|
---|
5338 |
|
---|
5339 | /* Undo deleting state if necessary. */
|
---|
5340 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5341 | /* Make sure that any error signalled by unmarkForDeletion() is not
|
---|
5342 | * ending up in the error list (if the caller uses MultiResult). It
|
---|
5343 | * usually is spurious, as in most cases the medium hasn't been marked
|
---|
5344 | * for deletion when the error was thrown above. */
|
---|
5345 | ErrorInfoKeeper eik;
|
---|
5346 | i_unmarkForDeletion();
|
---|
5347 | }
|
---|
5348 |
|
---|
5349 | return rc;
|
---|
5350 | }
|
---|
5351 |
|
---|
5352 | /**
|
---|
5353 | * Mark a medium for deletion.
|
---|
5354 | *
|
---|
5355 | * @note Caller must hold the write lock on this medium!
|
---|
5356 | */
|
---|
5357 | HRESULT Medium::i_markForDeletion()
|
---|
5358 | {
|
---|
5359 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5360 | switch (m->state)
|
---|
5361 | {
|
---|
5362 | case MediumState_Created:
|
---|
5363 | case MediumState_Inaccessible:
|
---|
5364 | m->preLockState = m->state;
|
---|
5365 | m->state = MediumState_Deleting;
|
---|
5366 | return S_OK;
|
---|
5367 | default:
|
---|
5368 | return i_setStateError();
|
---|
5369 | }
|
---|
5370 | }
|
---|
5371 |
|
---|
5372 | /**
|
---|
5373 | * Removes the "mark for deletion".
|
---|
5374 | *
|
---|
5375 | * @note Caller must hold the write lock on this medium!
|
---|
5376 | */
|
---|
5377 | HRESULT Medium::i_unmarkForDeletion()
|
---|
5378 | {
|
---|
5379 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5380 | switch (m->state)
|
---|
5381 | {
|
---|
5382 | case MediumState_Deleting:
|
---|
5383 | m->state = m->preLockState;
|
---|
5384 | return S_OK;
|
---|
5385 | default:
|
---|
5386 | return i_setStateError();
|
---|
5387 | }
|
---|
5388 | }
|
---|
5389 |
|
---|
5390 | /**
|
---|
5391 | * Mark a medium for deletion which is in locked state.
|
---|
5392 | *
|
---|
5393 | * @note Caller must hold the write lock on this medium!
|
---|
5394 | */
|
---|
5395 | HRESULT Medium::i_markLockedForDeletion()
|
---|
5396 | {
|
---|
5397 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5398 | if ( ( m->state == MediumState_LockedRead
|
---|
5399 | || m->state == MediumState_LockedWrite)
|
---|
5400 | && m->preLockState == MediumState_Created)
|
---|
5401 | {
|
---|
5402 | m->preLockState = MediumState_Deleting;
|
---|
5403 | return S_OK;
|
---|
5404 | }
|
---|
5405 | else
|
---|
5406 | return i_setStateError();
|
---|
5407 | }
|
---|
5408 |
|
---|
5409 | /**
|
---|
5410 | * Removes the "mark for deletion" for a medium in locked state.
|
---|
5411 | *
|
---|
5412 | * @note Caller must hold the write lock on this medium!
|
---|
5413 | */
|
---|
5414 | HRESULT Medium::i_unmarkLockedForDeletion()
|
---|
5415 | {
|
---|
5416 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5417 | if ( ( m->state == MediumState_LockedRead
|
---|
5418 | || m->state == MediumState_LockedWrite)
|
---|
5419 | && m->preLockState == MediumState_Deleting)
|
---|
5420 | {
|
---|
5421 | m->preLockState = MediumState_Created;
|
---|
5422 | return S_OK;
|
---|
5423 | }
|
---|
5424 | else
|
---|
5425 | return i_setStateError();
|
---|
5426 | }
|
---|
5427 |
|
---|
5428 | /**
|
---|
5429 | * Queries the preferred merge direction from this to the other medium, i.e.
|
---|
5430 | * the one which requires the least amount of I/O and therefore time and
|
---|
5431 | * disk consumption.
|
---|
5432 | *
|
---|
5433 | * @returns Status code.
|
---|
5434 | * @retval E_FAIL in case determining the merge direction fails for some reason,
|
---|
5435 | * for example if getting the size of the media fails. There is no
|
---|
5436 | * error set though and the caller is free to continue to find out
|
---|
5437 | * what was going wrong later. Leaves fMergeForward unset.
|
---|
5438 | * @retval VBOX_E_INVALID_OBJECT_STATE if both media are not related to each other
|
---|
5439 | * An error is set.
|
---|
5440 | * @param pOther The other medium to merge with.
|
---|
5441 | * @param fMergeForward Resulting preferred merge direction (out).
|
---|
5442 | */
|
---|
5443 | HRESULT Medium::i_queryPreferredMergeDirection(const ComObjPtr<Medium> &pOther,
|
---|
5444 | bool &fMergeForward)
|
---|
5445 | {
|
---|
5446 | AssertReturn(pOther != NULL, E_FAIL);
|
---|
5447 | AssertReturn(pOther != this, E_FAIL);
|
---|
5448 |
|
---|
5449 | HRESULT rc = S_OK;
|
---|
5450 | bool fThisParent = false; /**<< Flag whether this medium is the parent of pOther. */
|
---|
5451 |
|
---|
5452 | try
|
---|
5453 | {
|
---|
5454 | // locking: we need the tree lock first because we access parent pointers
|
---|
5455 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5456 |
|
---|
5457 | AutoCaller autoCaller(this);
|
---|
5458 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5459 |
|
---|
5460 | AutoCaller otherCaller(pOther);
|
---|
5461 | AssertComRCThrowRC(otherCaller.rc());
|
---|
5462 |
|
---|
5463 | /* more sanity checking and figuring out the current merge direction */
|
---|
5464 | ComObjPtr<Medium> pMedium = i_getParent();
|
---|
5465 | while (!pMedium.isNull() && pMedium != pOther)
|
---|
5466 | pMedium = pMedium->i_getParent();
|
---|
5467 | if (pMedium == pOther)
|
---|
5468 | fThisParent = false;
|
---|
5469 | else
|
---|
5470 | {
|
---|
5471 | pMedium = pOther->i_getParent();
|
---|
5472 | while (!pMedium.isNull() && pMedium != this)
|
---|
5473 | pMedium = pMedium->i_getParent();
|
---|
5474 | if (pMedium == this)
|
---|
5475 | fThisParent = true;
|
---|
5476 | else
|
---|
5477 | {
|
---|
5478 | Utf8Str tgtLoc;
|
---|
5479 | {
|
---|
5480 | AutoReadLock alock(pOther COMMA_LOCKVAL_SRC_POS);
|
---|
5481 | tgtLoc = pOther->i_getLocationFull();
|
---|
5482 | }
|
---|
5483 |
|
---|
5484 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5485 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5486 | tr("Media '%s' and '%s' are unrelated"),
|
---|
5487 | m->strLocationFull.c_str(), tgtLoc.c_str());
|
---|
5488 | }
|
---|
5489 | }
|
---|
5490 |
|
---|
5491 | /*
|
---|
5492 | * Figure out the preferred merge direction. The current way is to
|
---|
5493 | * get the current sizes of file based images and select the merge
|
---|
5494 | * direction depending on the size.
|
---|
5495 | *
|
---|
5496 | * Can't use the VD API to get current size here as the media might
|
---|
5497 | * be write locked by a running VM. Resort to RTFileQuerySize().
|
---|
5498 | */
|
---|
5499 | int vrc = VINF_SUCCESS;
|
---|
5500 | uint64_t cbMediumThis = 0;
|
---|
5501 | uint64_t cbMediumOther = 0;
|
---|
5502 |
|
---|
5503 | if (i_isMediumFormatFile() && pOther->i_isMediumFormatFile())
|
---|
5504 | {
|
---|
5505 | vrc = RTFileQuerySize(this->i_getLocationFull().c_str(), &cbMediumThis);
|
---|
5506 | if (RT_SUCCESS(vrc))
|
---|
5507 | {
|
---|
5508 | vrc = RTFileQuerySize(pOther->i_getLocationFull().c_str(),
|
---|
5509 | &cbMediumOther);
|
---|
5510 | }
|
---|
5511 |
|
---|
5512 | if (RT_FAILURE(vrc))
|
---|
5513 | rc = E_FAIL;
|
---|
5514 | else
|
---|
5515 | {
|
---|
5516 | /*
|
---|
5517 | * Check which merge direction might be more optimal.
|
---|
5518 | * This method is not bullet proof of course as there might
|
---|
5519 | * be overlapping blocks in the images so the file size is
|
---|
5520 | * not the best indicator but it is good enough for our purpose
|
---|
5521 | * and everything else is too complicated, especially when the
|
---|
5522 | * media are used by a running VM.
|
---|
5523 | */
|
---|
5524 |
|
---|
5525 | uint32_t mediumVariants = MediumVariant_Fixed | MediumVariant_VmdkStreamOptimized;
|
---|
5526 | uint32_t mediumCaps = MediumFormatCapabilities_CreateDynamic | MediumFormatCapabilities_File;
|
---|
5527 |
|
---|
5528 | bool fDynamicOther = pOther->i_getMediumFormat()->i_getCapabilities() & mediumCaps
|
---|
5529 | && pOther->i_getVariant() & ~mediumVariants;
|
---|
5530 | bool fDynamicThis = i_getMediumFormat()->i_getCapabilities() & mediumCaps
|
---|
5531 | && i_getVariant() & ~mediumVariants;
|
---|
5532 | bool fMergeIntoThis = (fDynamicThis && !fDynamicOther)
|
---|
5533 | || (fDynamicThis == fDynamicOther && cbMediumThis > cbMediumOther);
|
---|
5534 | fMergeForward = fMergeIntoThis != fThisParent;
|
---|
5535 | }
|
---|
5536 | }
|
---|
5537 | }
|
---|
5538 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5539 |
|
---|
5540 | return rc;
|
---|
5541 | }
|
---|
5542 |
|
---|
5543 | /**
|
---|
5544 | * Prepares this (source) medium, target medium and all intermediate media
|
---|
5545 | * for the merge operation.
|
---|
5546 | *
|
---|
5547 | * This method is to be called prior to calling the #mergeTo() to perform
|
---|
5548 | * necessary consistency checks and place involved media to appropriate
|
---|
5549 | * states. If #mergeTo() is not called or fails, the state modifications
|
---|
5550 | * performed by this method must be undone by #i_cancelMergeTo().
|
---|
5551 | *
|
---|
5552 | * See #mergeTo() for more information about merging.
|
---|
5553 | *
|
---|
5554 | * @param pTarget Target medium.
|
---|
5555 | * @param aMachineId Allowed machine attachment. NULL means do not check.
|
---|
5556 | * @param aSnapshotId Allowed snapshot attachment. NULL or empty UUID means
|
---|
5557 | * do not check.
|
---|
5558 | * @param fLockMedia Flag whether to lock the medium lock list or not.
|
---|
5559 | * If set to false and the medium lock list locking fails
|
---|
5560 | * later you must call #i_cancelMergeTo().
|
---|
5561 | * @param fMergeForward Resulting merge direction (out).
|
---|
5562 | * @param pParentForTarget New parent for target medium after merge (out).
|
---|
5563 | * @param aChildrenToReparent Medium lock list containing all children of the
|
---|
5564 | * source which will have to be reparented to the target
|
---|
5565 | * after merge (out).
|
---|
5566 | * @param aMediumLockList Medium locking information (out).
|
---|
5567 | *
|
---|
5568 | * @note Locks medium tree for reading. Locks this object, aTarget and all
|
---|
5569 | * intermediate media for writing.
|
---|
5570 | */
|
---|
5571 | HRESULT Medium::i_prepareMergeTo(const ComObjPtr<Medium> &pTarget,
|
---|
5572 | const Guid *aMachineId,
|
---|
5573 | const Guid *aSnapshotId,
|
---|
5574 | bool fLockMedia,
|
---|
5575 | bool &fMergeForward,
|
---|
5576 | ComObjPtr<Medium> &pParentForTarget,
|
---|
5577 | MediumLockList * &aChildrenToReparent,
|
---|
5578 | MediumLockList * &aMediumLockList)
|
---|
5579 | {
|
---|
5580 | AssertReturn(pTarget != NULL, E_FAIL);
|
---|
5581 | AssertReturn(pTarget != this, E_FAIL);
|
---|
5582 |
|
---|
5583 | HRESULT rc = S_OK;
|
---|
5584 | fMergeForward = false;
|
---|
5585 | pParentForTarget.setNull();
|
---|
5586 | Assert(aChildrenToReparent == NULL);
|
---|
5587 | aChildrenToReparent = NULL;
|
---|
5588 | Assert(aMediumLockList == NULL);
|
---|
5589 | aMediumLockList = NULL;
|
---|
5590 |
|
---|
5591 | try
|
---|
5592 | {
|
---|
5593 | // locking: we need the tree lock first because we access parent pointers
|
---|
5594 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5595 |
|
---|
5596 | AutoCaller autoCaller(this);
|
---|
5597 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5598 |
|
---|
5599 | AutoCaller targetCaller(pTarget);
|
---|
5600 | AssertComRCThrowRC(targetCaller.rc());
|
---|
5601 |
|
---|
5602 | /* more sanity checking and figuring out the merge direction */
|
---|
5603 | ComObjPtr<Medium> pMedium = i_getParent();
|
---|
5604 | while (!pMedium.isNull() && pMedium != pTarget)
|
---|
5605 | pMedium = pMedium->i_getParent();
|
---|
5606 | if (pMedium == pTarget)
|
---|
5607 | fMergeForward = false;
|
---|
5608 | else
|
---|
5609 | {
|
---|
5610 | pMedium = pTarget->i_getParent();
|
---|
5611 | while (!pMedium.isNull() && pMedium != this)
|
---|
5612 | pMedium = pMedium->i_getParent();
|
---|
5613 | if (pMedium == this)
|
---|
5614 | fMergeForward = true;
|
---|
5615 | else
|
---|
5616 | {
|
---|
5617 | Utf8Str tgtLoc;
|
---|
5618 | {
|
---|
5619 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5620 | tgtLoc = pTarget->i_getLocationFull();
|
---|
5621 | }
|
---|
5622 |
|
---|
5623 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5624 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5625 | tr("Media '%s' and '%s' are unrelated"),
|
---|
5626 | m->strLocationFull.c_str(), tgtLoc.c_str());
|
---|
5627 | }
|
---|
5628 | }
|
---|
5629 |
|
---|
5630 | /* Build the lock list. */
|
---|
5631 | aMediumLockList = new MediumLockList();
|
---|
5632 | targetCaller.release();
|
---|
5633 | autoCaller.release();
|
---|
5634 | treeLock.release();
|
---|
5635 | if (fMergeForward)
|
---|
5636 | rc = pTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5637 | pTarget /* pToLockWrite */,
|
---|
5638 | false /* fMediumLockWriteAll */,
|
---|
5639 | NULL,
|
---|
5640 | *aMediumLockList);
|
---|
5641 | else
|
---|
5642 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5643 | pTarget /* pToLockWrite */,
|
---|
5644 | false /* fMediumLockWriteAll */,
|
---|
5645 | NULL,
|
---|
5646 | *aMediumLockList);
|
---|
5647 | treeLock.acquire();
|
---|
5648 | autoCaller.add();
|
---|
5649 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5650 | targetCaller.add();
|
---|
5651 | AssertComRCThrowRC(targetCaller.rc());
|
---|
5652 | if (FAILED(rc))
|
---|
5653 | throw rc;
|
---|
5654 |
|
---|
5655 | /* Sanity checking, must be after lock list creation as it depends on
|
---|
5656 | * valid medium states. The medium objects must be accessible. Only
|
---|
5657 | * do this if immediate locking is requested, otherwise it fails when
|
---|
5658 | * we construct a medium lock list for an already running VM. Snapshot
|
---|
5659 | * deletion uses this to simplify its life. */
|
---|
5660 | if (fLockMedia)
|
---|
5661 | {
|
---|
5662 | {
|
---|
5663 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5664 | if (m->state != MediumState_Created)
|
---|
5665 | throw i_setStateError();
|
---|
5666 | }
|
---|
5667 | {
|
---|
5668 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5669 | if (pTarget->m->state != MediumState_Created)
|
---|
5670 | throw pTarget->i_setStateError();
|
---|
5671 | }
|
---|
5672 | }
|
---|
5673 |
|
---|
5674 | /* check medium attachment and other sanity conditions */
|
---|
5675 | if (fMergeForward)
|
---|
5676 | {
|
---|
5677 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5678 | if (i_getChildren().size() > 1)
|
---|
5679 | {
|
---|
5680 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5681 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
5682 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
5683 | }
|
---|
5684 | /* One backreference is only allowed if the machine ID is not empty
|
---|
5685 | * and it matches the machine the medium is attached to (including
|
---|
5686 | * the snapshot ID if not empty). */
|
---|
5687 | if ( m->backRefs.size() != 0
|
---|
5688 | && ( !aMachineId
|
---|
5689 | || m->backRefs.size() != 1
|
---|
5690 | || aMachineId->isZero()
|
---|
5691 | || *i_getFirstMachineBackrefId() != *aMachineId
|
---|
5692 | || ( (!aSnapshotId || !aSnapshotId->isZero())
|
---|
5693 | && *i_getFirstMachineBackrefSnapshotId() != *aSnapshotId)))
|
---|
5694 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5695 | tr("Medium '%s' is attached to %d virtual machines"),
|
---|
5696 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
5697 | if (m->type == MediumType_Immutable)
|
---|
5698 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5699 | tr("Medium '%s' is immutable"),
|
---|
5700 | m->strLocationFull.c_str());
|
---|
5701 | if (m->type == MediumType_MultiAttach)
|
---|
5702 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5703 | tr("Medium '%s' is multi-attach"),
|
---|
5704 | m->strLocationFull.c_str());
|
---|
5705 | }
|
---|
5706 | else
|
---|
5707 | {
|
---|
5708 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5709 | if (pTarget->i_getChildren().size() > 1)
|
---|
5710 | {
|
---|
5711 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5712 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
5713 | pTarget->m->strLocationFull.c_str(),
|
---|
5714 | pTarget->i_getChildren().size());
|
---|
5715 | }
|
---|
5716 | if (pTarget->m->type == MediumType_Immutable)
|
---|
5717 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5718 | tr("Medium '%s' is immutable"),
|
---|
5719 | pTarget->m->strLocationFull.c_str());
|
---|
5720 | if (pTarget->m->type == MediumType_MultiAttach)
|
---|
5721 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5722 | tr("Medium '%s' is multi-attach"),
|
---|
5723 | pTarget->m->strLocationFull.c_str());
|
---|
5724 | }
|
---|
5725 | ComObjPtr<Medium> pLast(fMergeForward ? (Medium *)pTarget : this);
|
---|
5726 | ComObjPtr<Medium> pLastIntermediate = pLast->i_getParent();
|
---|
5727 | for (pLast = pLastIntermediate;
|
---|
5728 | !pLast.isNull() && pLast != pTarget && pLast != this;
|
---|
5729 | pLast = pLast->i_getParent())
|
---|
5730 | {
|
---|
5731 | AutoReadLock alock(pLast COMMA_LOCKVAL_SRC_POS);
|
---|
5732 | if (pLast->i_getChildren().size() > 1)
|
---|
5733 | {
|
---|
5734 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5735 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
5736 | pLast->m->strLocationFull.c_str(),
|
---|
5737 | pLast->i_getChildren().size());
|
---|
5738 | }
|
---|
5739 | if (pLast->m->backRefs.size() != 0)
|
---|
5740 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5741 | tr("Medium '%s' is attached to %d virtual machines"),
|
---|
5742 | pLast->m->strLocationFull.c_str(),
|
---|
5743 | pLast->m->backRefs.size());
|
---|
5744 |
|
---|
5745 | }
|
---|
5746 |
|
---|
5747 | /* Update medium states appropriately */
|
---|
5748 | {
|
---|
5749 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5750 |
|
---|
5751 | if (m->state == MediumState_Created)
|
---|
5752 | {
|
---|
5753 | rc = i_markForDeletion();
|
---|
5754 | if (FAILED(rc))
|
---|
5755 | throw rc;
|
---|
5756 | }
|
---|
5757 | else
|
---|
5758 | {
|
---|
5759 | if (fLockMedia)
|
---|
5760 | throw i_setStateError();
|
---|
5761 | else if ( m->state == MediumState_LockedWrite
|
---|
5762 | || m->state == MediumState_LockedRead)
|
---|
5763 | {
|
---|
5764 | /* Either mark it for deletion in locked state or allow
|
---|
5765 | * others to have done so. */
|
---|
5766 | if (m->preLockState == MediumState_Created)
|
---|
5767 | i_markLockedForDeletion();
|
---|
5768 | else if (m->preLockState != MediumState_Deleting)
|
---|
5769 | throw i_setStateError();
|
---|
5770 | }
|
---|
5771 | else
|
---|
5772 | throw i_setStateError();
|
---|
5773 | }
|
---|
5774 | }
|
---|
5775 |
|
---|
5776 | if (fMergeForward)
|
---|
5777 | {
|
---|
5778 | /* we will need parent to reparent target */
|
---|
5779 | pParentForTarget = i_getParent();
|
---|
5780 | }
|
---|
5781 | else
|
---|
5782 | {
|
---|
5783 | /* we will need to reparent children of the source */
|
---|
5784 | aChildrenToReparent = new MediumLockList();
|
---|
5785 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
5786 | it != i_getChildren().end();
|
---|
5787 | ++it)
|
---|
5788 | {
|
---|
5789 | pMedium = *it;
|
---|
5790 | aChildrenToReparent->Append(pMedium, true /* fLockWrite */);
|
---|
5791 | }
|
---|
5792 | if (fLockMedia && aChildrenToReparent)
|
---|
5793 | {
|
---|
5794 | targetCaller.release();
|
---|
5795 | autoCaller.release();
|
---|
5796 | treeLock.release();
|
---|
5797 | rc = aChildrenToReparent->Lock();
|
---|
5798 | treeLock.acquire();
|
---|
5799 | autoCaller.add();
|
---|
5800 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5801 | targetCaller.add();
|
---|
5802 | AssertComRCThrowRC(targetCaller.rc());
|
---|
5803 | if (FAILED(rc))
|
---|
5804 | throw rc;
|
---|
5805 | }
|
---|
5806 | }
|
---|
5807 | for (pLast = pLastIntermediate;
|
---|
5808 | !pLast.isNull() && pLast != pTarget && pLast != this;
|
---|
5809 | pLast = pLast->i_getParent())
|
---|
5810 | {
|
---|
5811 | AutoWriteLock alock(pLast COMMA_LOCKVAL_SRC_POS);
|
---|
5812 | if (pLast->m->state == MediumState_Created)
|
---|
5813 | {
|
---|
5814 | rc = pLast->i_markForDeletion();
|
---|
5815 | if (FAILED(rc))
|
---|
5816 | throw rc;
|
---|
5817 | }
|
---|
5818 | else
|
---|
5819 | throw pLast->i_setStateError();
|
---|
5820 | }
|
---|
5821 |
|
---|
5822 | /* Tweak the lock list in the backward merge case, as the target
|
---|
5823 | * isn't marked to be locked for writing yet. */
|
---|
5824 | if (!fMergeForward)
|
---|
5825 | {
|
---|
5826 | MediumLockList::Base::iterator lockListBegin =
|
---|
5827 | aMediumLockList->GetBegin();
|
---|
5828 | MediumLockList::Base::iterator lockListEnd =
|
---|
5829 | aMediumLockList->GetEnd();
|
---|
5830 | ++lockListEnd;
|
---|
5831 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
5832 | it != lockListEnd;
|
---|
5833 | ++it)
|
---|
5834 | {
|
---|
5835 | MediumLock &mediumLock = *it;
|
---|
5836 | if (mediumLock.GetMedium() == pTarget)
|
---|
5837 | {
|
---|
5838 | HRESULT rc2 = mediumLock.UpdateLock(true);
|
---|
5839 | AssertComRC(rc2);
|
---|
5840 | break;
|
---|
5841 | }
|
---|
5842 | }
|
---|
5843 | }
|
---|
5844 |
|
---|
5845 | if (fLockMedia)
|
---|
5846 | {
|
---|
5847 | targetCaller.release();
|
---|
5848 | autoCaller.release();
|
---|
5849 | treeLock.release();
|
---|
5850 | rc = aMediumLockList->Lock();
|
---|
5851 | treeLock.acquire();
|
---|
5852 | autoCaller.add();
|
---|
5853 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5854 | targetCaller.add();
|
---|
5855 | AssertComRCThrowRC(targetCaller.rc());
|
---|
5856 | if (FAILED(rc))
|
---|
5857 | {
|
---|
5858 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5859 | throw setError(rc,
|
---|
5860 | tr("Failed to lock media when merging to '%s'"),
|
---|
5861 | pTarget->i_getLocationFull().c_str());
|
---|
5862 | }
|
---|
5863 | }
|
---|
5864 | }
|
---|
5865 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5866 |
|
---|
5867 | if (FAILED(rc))
|
---|
5868 | {
|
---|
5869 | if (aMediumLockList)
|
---|
5870 | {
|
---|
5871 | delete aMediumLockList;
|
---|
5872 | aMediumLockList = NULL;
|
---|
5873 | }
|
---|
5874 | if (aChildrenToReparent)
|
---|
5875 | {
|
---|
5876 | delete aChildrenToReparent;
|
---|
5877 | aChildrenToReparent = NULL;
|
---|
5878 | }
|
---|
5879 | }
|
---|
5880 |
|
---|
5881 | return rc;
|
---|
5882 | }
|
---|
5883 |
|
---|
5884 | /**
|
---|
5885 | * Merges this medium to the specified medium which must be either its
|
---|
5886 | * direct ancestor or descendant.
|
---|
5887 | *
|
---|
5888 | * Given this medium is SOURCE and the specified medium is TARGET, we will
|
---|
5889 | * get two variants of the merge operation:
|
---|
5890 | *
|
---|
5891 | * forward merge
|
---|
5892 | * ------------------------->
|
---|
5893 | * [Extra] <- SOURCE <- Intermediate <- TARGET
|
---|
5894 | * Any Del Del LockWr
|
---|
5895 | *
|
---|
5896 | *
|
---|
5897 | * backward merge
|
---|
5898 | * <-------------------------
|
---|
5899 | * TARGET <- Intermediate <- SOURCE <- [Extra]
|
---|
5900 | * LockWr Del Del LockWr
|
---|
5901 | *
|
---|
5902 | * Each diagram shows the involved media on the media chain where
|
---|
5903 | * SOURCE and TARGET belong. Under each medium there is a state value which
|
---|
5904 | * the medium must have at a time of the mergeTo() call.
|
---|
5905 | *
|
---|
5906 | * The media in the square braces may be absent (e.g. when the forward
|
---|
5907 | * operation takes place and SOURCE is the base medium, or when the backward
|
---|
5908 | * merge operation takes place and TARGET is the last child in the chain) but if
|
---|
5909 | * they present they are involved too as shown.
|
---|
5910 | *
|
---|
5911 | * Neither the source medium nor intermediate media may be attached to
|
---|
5912 | * any VM directly or in the snapshot, otherwise this method will assert.
|
---|
5913 | *
|
---|
5914 | * The #i_prepareMergeTo() method must be called prior to this method to place
|
---|
5915 | * all involved to necessary states and perform other consistency checks.
|
---|
5916 | *
|
---|
5917 | * If @a aWait is @c true then this method will perform the operation on the
|
---|
5918 | * calling thread and will not return to the caller until the operation is
|
---|
5919 | * completed. When this method succeeds, all intermediate medium objects in
|
---|
5920 | * the chain will be uninitialized, the state of the target medium (and all
|
---|
5921 | * involved extra media) will be restored. @a aMediumLockList will not be
|
---|
5922 | * deleted, whether the operation is successful or not. The caller has to do
|
---|
5923 | * this if appropriate. Note that this (source) medium is not uninitialized
|
---|
5924 | * because of possible AutoCaller instances held by the caller of this method
|
---|
5925 | * on the current thread. It's therefore the responsibility of the caller to
|
---|
5926 | * call Medium::uninit() after releasing all callers.
|
---|
5927 | *
|
---|
5928 | * If @a aWait is @c false then this method will create a thread to perform the
|
---|
5929 | * operation asynchronously and will return immediately. If the operation
|
---|
5930 | * succeeds, the thread will uninitialize the source medium object and all
|
---|
5931 | * intermediate medium objects in the chain, reset the state of the target
|
---|
5932 | * medium (and all involved extra media) and delete @a aMediumLockList.
|
---|
5933 | * If the operation fails, the thread will only reset the states of all
|
---|
5934 | * involved media and delete @a aMediumLockList.
|
---|
5935 | *
|
---|
5936 | * When this method fails (regardless of the @a aWait mode), it is a caller's
|
---|
5937 | * responsibility to undo state changes and delete @a aMediumLockList using
|
---|
5938 | * #i_cancelMergeTo().
|
---|
5939 | *
|
---|
5940 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
5941 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
5942 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
5943 | * progress object is created/used at all. Note that @a aProgress cannot be
|
---|
5944 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
5945 | *
|
---|
5946 | * @param pTarget Target medium.
|
---|
5947 | * @param fMergeForward Merge direction.
|
---|
5948 | * @param pParentForTarget New parent for target medium after merge.
|
---|
5949 | * @param aChildrenToReparent List of children of the source which will have
|
---|
5950 | * to be reparented to the target after merge.
|
---|
5951 | * @param aMediumLockList Medium locking information.
|
---|
5952 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
5953 | * completion.
|
---|
5954 | * @param aWait @c true if this method should block instead of creating
|
---|
5955 | * an asynchronous thread.
|
---|
5956 | *
|
---|
5957 | * @note Locks the tree lock for writing. Locks the media from the chain
|
---|
5958 | * for writing.
|
---|
5959 | */
|
---|
5960 | HRESULT Medium::i_mergeTo(const ComObjPtr<Medium> &pTarget,
|
---|
5961 | bool fMergeForward,
|
---|
5962 | const ComObjPtr<Medium> &pParentForTarget,
|
---|
5963 | MediumLockList *aChildrenToReparent,
|
---|
5964 | MediumLockList *aMediumLockList,
|
---|
5965 | ComObjPtr<Progress> *aProgress,
|
---|
5966 | bool aWait)
|
---|
5967 | {
|
---|
5968 | AssertReturn(pTarget != NULL, E_FAIL);
|
---|
5969 | AssertReturn(pTarget != this, E_FAIL);
|
---|
5970 | AssertReturn(aMediumLockList != NULL, E_FAIL);
|
---|
5971 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
5972 |
|
---|
5973 | AutoCaller autoCaller(this);
|
---|
5974 | AssertComRCReturnRC(autoCaller.rc());
|
---|
5975 |
|
---|
5976 | AutoCaller targetCaller(pTarget);
|
---|
5977 | AssertComRCReturnRC(targetCaller.rc());
|
---|
5978 |
|
---|
5979 | HRESULT rc = S_OK;
|
---|
5980 | ComObjPtr<Progress> pProgress;
|
---|
5981 | Medium::Task *pTask = NULL;
|
---|
5982 |
|
---|
5983 | try
|
---|
5984 | {
|
---|
5985 | if (aProgress != NULL)
|
---|
5986 | {
|
---|
5987 | /* use the existing progress object... */
|
---|
5988 | pProgress = *aProgress;
|
---|
5989 |
|
---|
5990 | /* ...but create a new one if it is null */
|
---|
5991 | if (pProgress.isNull())
|
---|
5992 | {
|
---|
5993 | Utf8Str tgtName;
|
---|
5994 | {
|
---|
5995 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5996 | tgtName = pTarget->i_getName();
|
---|
5997 | }
|
---|
5998 |
|
---|
5999 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6000 |
|
---|
6001 | pProgress.createObject();
|
---|
6002 | rc = pProgress->init(m->pVirtualBox,
|
---|
6003 | static_cast<IMedium*>(this),
|
---|
6004 | BstrFmt(tr("Merging medium '%s' to '%s'"),
|
---|
6005 | i_getName().c_str(),
|
---|
6006 | tgtName.c_str()).raw(),
|
---|
6007 | TRUE, /* aCancelable */
|
---|
6008 | 2, /* Number of opearations */
|
---|
6009 | BstrFmt(tr("Resizing medium '%s' before merge"),
|
---|
6010 | tgtName.c_str()).raw()
|
---|
6011 | );
|
---|
6012 | if (FAILED(rc))
|
---|
6013 | throw rc;
|
---|
6014 | }
|
---|
6015 | }
|
---|
6016 |
|
---|
6017 | /* setup task object to carry out the operation sync/async */
|
---|
6018 | pTask = new Medium::MergeTask(this, pTarget, fMergeForward,
|
---|
6019 | pParentForTarget, aChildrenToReparent,
|
---|
6020 | pProgress, aMediumLockList,
|
---|
6021 | aWait /* fKeepMediumLockList */);
|
---|
6022 | rc = pTask->rc();
|
---|
6023 | AssertComRC(rc);
|
---|
6024 | if (FAILED(rc))
|
---|
6025 | throw rc;
|
---|
6026 | }
|
---|
6027 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6028 |
|
---|
6029 | if (SUCCEEDED(rc))
|
---|
6030 | {
|
---|
6031 | if (aWait)
|
---|
6032 | {
|
---|
6033 | rc = pTask->runNow();
|
---|
6034 |
|
---|
6035 | delete pTask;
|
---|
6036 | }
|
---|
6037 | else
|
---|
6038 | rc = pTask->createThread();
|
---|
6039 |
|
---|
6040 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
6041 | *aProgress = pProgress;
|
---|
6042 | }
|
---|
6043 | else if (pTask != NULL)
|
---|
6044 | delete pTask;
|
---|
6045 |
|
---|
6046 | return rc;
|
---|
6047 | }
|
---|
6048 |
|
---|
6049 | /**
|
---|
6050 | * Undoes what #i_prepareMergeTo() did. Must be called if #mergeTo() is not
|
---|
6051 | * called or fails. Frees memory occupied by @a aMediumLockList and unlocks
|
---|
6052 | * the medium objects in @a aChildrenToReparent.
|
---|
6053 | *
|
---|
6054 | * @param aChildrenToReparent List of children of the source which will have
|
---|
6055 | * to be reparented to the target after merge.
|
---|
6056 | * @param aMediumLockList Medium locking information.
|
---|
6057 | *
|
---|
6058 | * @note Locks the tree lock for writing. Locks the media from the chain
|
---|
6059 | * for writing.
|
---|
6060 | */
|
---|
6061 | void Medium::i_cancelMergeTo(MediumLockList *aChildrenToReparent,
|
---|
6062 | MediumLockList *aMediumLockList)
|
---|
6063 | {
|
---|
6064 | AutoCaller autoCaller(this);
|
---|
6065 | AssertComRCReturnVoid(autoCaller.rc());
|
---|
6066 |
|
---|
6067 | AssertReturnVoid(aMediumLockList != NULL);
|
---|
6068 |
|
---|
6069 | /* Revert media marked for deletion to previous state. */
|
---|
6070 | HRESULT rc;
|
---|
6071 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
6072 | aMediumLockList->GetBegin();
|
---|
6073 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
6074 | aMediumLockList->GetEnd();
|
---|
6075 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
6076 | it != mediumListEnd;
|
---|
6077 | ++it)
|
---|
6078 | {
|
---|
6079 | const MediumLock &mediumLock = *it;
|
---|
6080 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
6081 | AutoWriteLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
6082 |
|
---|
6083 | if (pMedium->m->state == MediumState_Deleting)
|
---|
6084 | {
|
---|
6085 | rc = pMedium->i_unmarkForDeletion();
|
---|
6086 | AssertComRC(rc);
|
---|
6087 | }
|
---|
6088 | else if ( ( pMedium->m->state == MediumState_LockedWrite
|
---|
6089 | || pMedium->m->state == MediumState_LockedRead)
|
---|
6090 | && pMedium->m->preLockState == MediumState_Deleting)
|
---|
6091 | {
|
---|
6092 | rc = pMedium->i_unmarkLockedForDeletion();
|
---|
6093 | AssertComRC(rc);
|
---|
6094 | }
|
---|
6095 | }
|
---|
6096 |
|
---|
6097 | /* the destructor will do the work */
|
---|
6098 | delete aMediumLockList;
|
---|
6099 |
|
---|
6100 | /* unlock the children which had to be reparented, the destructor will do
|
---|
6101 | * the work */
|
---|
6102 | if (aChildrenToReparent)
|
---|
6103 | delete aChildrenToReparent;
|
---|
6104 | }
|
---|
6105 |
|
---|
6106 | /**
|
---|
6107 | * Resizes the media.
|
---|
6108 | *
|
---|
6109 | * If @a aWait is @c true then this method will perform the operation on the
|
---|
6110 | * calling thread and will not return to the caller until the operation is
|
---|
6111 | * completed. When this method succeeds, the state of the target medium (and all
|
---|
6112 | * involved extra media) will be restored. @a aMediumLockList will not be
|
---|
6113 | * deleted, whether the operation is successful or not. The caller has to do
|
---|
6114 | * this if appropriate.
|
---|
6115 | *
|
---|
6116 | * If @a aWait is @c false then this method will create a thread to perform the
|
---|
6117 | * operation asynchronously and will return immediately. The thread will reset
|
---|
6118 | * the state of the target medium (and all involved extra media) and delete
|
---|
6119 | * @a aMediumLockList.
|
---|
6120 | *
|
---|
6121 | * When this method fails (regardless of the @a aWait mode), it is a caller's
|
---|
6122 | * responsibility to undo state changes and delete @a aMediumLockList.
|
---|
6123 | *
|
---|
6124 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
6125 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
6126 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
6127 | * progress object is created/used at all. Note that @a aProgress cannot be
|
---|
6128 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
6129 | *
|
---|
6130 | * @param aLogicalSize New nominal capacity of the medium in bytes.
|
---|
6131 | * @param aMediumLockList Medium locking information.
|
---|
6132 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
6133 | * completion.
|
---|
6134 | * @param aWait @c true if this method should block instead of creating
|
---|
6135 | * an asynchronous thread.
|
---|
6136 | *
|
---|
6137 | * @note Locks the media from the chain for writing.
|
---|
6138 | */
|
---|
6139 |
|
---|
6140 | HRESULT Medium::i_resize(LONG64 aLogicalSize,
|
---|
6141 | MediumLockList *aMediumLockList,
|
---|
6142 | ComObjPtr<Progress> *aProgress,
|
---|
6143 | bool aWait)
|
---|
6144 | {
|
---|
6145 | AssertReturn(aMediumLockList != NULL, E_FAIL);
|
---|
6146 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
6147 |
|
---|
6148 | AutoCaller autoCaller(this);
|
---|
6149 | AssertComRCReturnRC(autoCaller.rc());
|
---|
6150 |
|
---|
6151 | HRESULT rc = S_OK;
|
---|
6152 | ComObjPtr<Progress> pProgress;
|
---|
6153 | Medium::Task *pTask = NULL;
|
---|
6154 |
|
---|
6155 | try
|
---|
6156 | {
|
---|
6157 | if (aProgress != NULL)
|
---|
6158 | {
|
---|
6159 | /* use the existing progress object... */
|
---|
6160 | pProgress = *aProgress;
|
---|
6161 |
|
---|
6162 | /* ...but create a new one if it is null */
|
---|
6163 | if (pProgress.isNull())
|
---|
6164 | {
|
---|
6165 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6166 |
|
---|
6167 | pProgress.createObject();
|
---|
6168 | rc = pProgress->init(m->pVirtualBox,
|
---|
6169 | static_cast <IMedium *>(this),
|
---|
6170 | BstrFmt(tr("Resizing medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
6171 | TRUE /* aCancelable */);
|
---|
6172 | if (FAILED(rc))
|
---|
6173 | throw rc;
|
---|
6174 | }
|
---|
6175 | }
|
---|
6176 |
|
---|
6177 | /* setup task object to carry out the operation asynchronously */
|
---|
6178 | pTask = new Medium::ResizeTask(this,
|
---|
6179 | aLogicalSize,
|
---|
6180 | pProgress,
|
---|
6181 | aMediumLockList,
|
---|
6182 | aWait /* fKeepMediumLockList */);
|
---|
6183 | rc = pTask->rc();
|
---|
6184 | AssertComRC(rc);
|
---|
6185 | if (FAILED(rc))
|
---|
6186 | throw rc;
|
---|
6187 | }
|
---|
6188 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6189 |
|
---|
6190 | if (SUCCEEDED(rc))
|
---|
6191 | {
|
---|
6192 | if (aWait)
|
---|
6193 | {
|
---|
6194 | rc = pTask->runNow();
|
---|
6195 | delete pTask;
|
---|
6196 | }
|
---|
6197 | else
|
---|
6198 | rc = pTask->createThread();
|
---|
6199 |
|
---|
6200 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
6201 | *aProgress = pProgress;
|
---|
6202 | }
|
---|
6203 | else if (pTask != NULL)
|
---|
6204 | delete pTask;
|
---|
6205 |
|
---|
6206 | return rc;
|
---|
6207 | }
|
---|
6208 |
|
---|
6209 | /**
|
---|
6210 | * Fix the parent UUID of all children to point to this medium as their
|
---|
6211 | * parent.
|
---|
6212 | */
|
---|
6213 | HRESULT Medium::i_fixParentUuidOfChildren(MediumLockList *pChildrenToReparent)
|
---|
6214 | {
|
---|
6215 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
6216 | * to lock order violations, it probably causes lock order issues related
|
---|
6217 | * to the AutoCaller usage. Likewise the code using this method seems
|
---|
6218 | * problematic. */
|
---|
6219 | Assert(!isWriteLockOnCurrentThread());
|
---|
6220 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
6221 | MediumLockList mediumLockList;
|
---|
6222 | HRESULT rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
6223 | NULL /* pToLockWrite */,
|
---|
6224 | false /* fMediumLockWriteAll */,
|
---|
6225 | this,
|
---|
6226 | mediumLockList);
|
---|
6227 | AssertComRCReturnRC(rc);
|
---|
6228 |
|
---|
6229 | try
|
---|
6230 | {
|
---|
6231 | PVDISK hdd;
|
---|
6232 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
6233 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6234 |
|
---|
6235 | try
|
---|
6236 | {
|
---|
6237 | MediumLockList::Base::iterator lockListBegin =
|
---|
6238 | mediumLockList.GetBegin();
|
---|
6239 | MediumLockList::Base::iterator lockListEnd =
|
---|
6240 | mediumLockList.GetEnd();
|
---|
6241 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
6242 | it != lockListEnd;
|
---|
6243 | ++it)
|
---|
6244 | {
|
---|
6245 | MediumLock &mediumLock = *it;
|
---|
6246 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
6247 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
6248 |
|
---|
6249 | // open the medium
|
---|
6250 | vrc = VDOpen(hdd,
|
---|
6251 | pMedium->m->strFormat.c_str(),
|
---|
6252 | pMedium->m->strLocationFull.c_str(),
|
---|
6253 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
6254 | pMedium->m->vdImageIfaces);
|
---|
6255 | if (RT_FAILURE(vrc))
|
---|
6256 | throw vrc;
|
---|
6257 | }
|
---|
6258 |
|
---|
6259 | MediumLockList::Base::iterator childrenBegin = pChildrenToReparent->GetBegin();
|
---|
6260 | MediumLockList::Base::iterator childrenEnd = pChildrenToReparent->GetEnd();
|
---|
6261 | for (MediumLockList::Base::iterator it = childrenBegin;
|
---|
6262 | it != childrenEnd;
|
---|
6263 | ++it)
|
---|
6264 | {
|
---|
6265 | Medium *pMedium = it->GetMedium();
|
---|
6266 | /* VD_OPEN_FLAGS_INFO since UUID is wrong yet */
|
---|
6267 | vrc = VDOpen(hdd,
|
---|
6268 | pMedium->m->strFormat.c_str(),
|
---|
6269 | pMedium->m->strLocationFull.c_str(),
|
---|
6270 | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
6271 | pMedium->m->vdImageIfaces);
|
---|
6272 | if (RT_FAILURE(vrc))
|
---|
6273 | throw vrc;
|
---|
6274 |
|
---|
6275 | vrc = VDSetParentUuid(hdd, VD_LAST_IMAGE, m->id.raw());
|
---|
6276 | if (RT_FAILURE(vrc))
|
---|
6277 | throw vrc;
|
---|
6278 |
|
---|
6279 | vrc = VDClose(hdd, false /* fDelete */);
|
---|
6280 | if (RT_FAILURE(vrc))
|
---|
6281 | throw vrc;
|
---|
6282 | }
|
---|
6283 | }
|
---|
6284 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6285 | catch (int aVRC)
|
---|
6286 | {
|
---|
6287 | rc = setErrorBoth(E_FAIL, aVRC,
|
---|
6288 | tr("Could not update medium UUID references to parent '%s' (%s)"),
|
---|
6289 | m->strLocationFull.c_str(),
|
---|
6290 | i_vdError(aVRC).c_str());
|
---|
6291 | }
|
---|
6292 |
|
---|
6293 | VDDestroy(hdd);
|
---|
6294 | }
|
---|
6295 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6296 |
|
---|
6297 | return rc;
|
---|
6298 | }
|
---|
6299 |
|
---|
6300 | /**
|
---|
6301 | *
|
---|
6302 | * @note Similar code exists in i_taskExportHandler.
|
---|
6303 | */
|
---|
6304 | HRESULT Medium::i_addRawToFss(const char *aFilename, SecretKeyStore *pKeyStore, RTVFSFSSTREAM hVfsFssDst,
|
---|
6305 | const ComObjPtr<Progress> &aProgress, bool fSparse)
|
---|
6306 | {
|
---|
6307 | AutoCaller autoCaller(this);
|
---|
6308 | HRESULT hrc = autoCaller.rc();
|
---|
6309 | if (SUCCEEDED(hrc))
|
---|
6310 | {
|
---|
6311 | /*
|
---|
6312 | * Get a readonly hdd for this medium.
|
---|
6313 | */
|
---|
6314 | MediumCryptoFilterSettings CryptoSettingsRead;
|
---|
6315 | MediumLockList SourceMediumLockList;
|
---|
6316 | PVDISK pHdd;
|
---|
6317 | hrc = i_openForIO(false /*fWritable*/, pKeyStore, &pHdd, &SourceMediumLockList, &CryptoSettingsRead);
|
---|
6318 | if (SUCCEEDED(hrc))
|
---|
6319 | {
|
---|
6320 | /*
|
---|
6321 | * Create a VFS file interface to the HDD and attach a progress wrapper
|
---|
6322 | * that monitors the progress reading of the raw image. The image will
|
---|
6323 | * be read twice if hVfsFssDst does sparse processing.
|
---|
6324 | */
|
---|
6325 | RTVFSFILE hVfsFileDisk = NIL_RTVFSFILE;
|
---|
6326 | int vrc = VDCreateVfsFileFromDisk(pHdd, 0 /*fFlags*/, &hVfsFileDisk);
|
---|
6327 | if (RT_SUCCESS(vrc))
|
---|
6328 | {
|
---|
6329 | RTVFSFILE hVfsFileProgress = NIL_RTVFSFILE;
|
---|
6330 | vrc = RTVfsCreateProgressForFile(hVfsFileDisk, aProgress->i_iprtProgressCallback, &*aProgress,
|
---|
6331 | RTVFSPROGRESS_F_CANCELABLE | RTVFSPROGRESS_F_FORWARD_SEEK_AS_READ,
|
---|
6332 | VDGetSize(pHdd, VD_LAST_IMAGE) * (fSparse ? 2 : 1) /*cbExpectedRead*/,
|
---|
6333 | 0 /*cbExpectedWritten*/, &hVfsFileProgress);
|
---|
6334 | RTVfsFileRelease(hVfsFileDisk);
|
---|
6335 | if (RT_SUCCESS(vrc))
|
---|
6336 | {
|
---|
6337 | RTVFSOBJ hVfsObj = RTVfsObjFromFile(hVfsFileProgress);
|
---|
6338 | RTVfsFileRelease(hVfsFileProgress);
|
---|
6339 |
|
---|
6340 | vrc = RTVfsFsStrmAdd(hVfsFssDst, aFilename, hVfsObj, 0 /*fFlags*/);
|
---|
6341 | RTVfsObjRelease(hVfsObj);
|
---|
6342 | if (RT_FAILURE(vrc))
|
---|
6343 | hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Failed to add '%s' to output (%Rrc)"), aFilename, vrc);
|
---|
6344 | }
|
---|
6345 | else
|
---|
6346 | hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
6347 | tr("RTVfsCreateProgressForFile failed when processing '%s' (%Rrc)"), aFilename, vrc);
|
---|
6348 | }
|
---|
6349 | else
|
---|
6350 | hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("VDCreateVfsFileFromDisk failed for '%s' (%Rrc)"), aFilename, vrc);
|
---|
6351 | VDDestroy(pHdd);
|
---|
6352 | }
|
---|
6353 | }
|
---|
6354 | return hrc;
|
---|
6355 | }
|
---|
6356 |
|
---|
6357 | /**
|
---|
6358 | * Used by IAppliance to export disk images.
|
---|
6359 | *
|
---|
6360 | * @param aFilename Filename to create (UTF8).
|
---|
6361 | * @param aFormat Medium format for creating @a aFilename.
|
---|
6362 | * @param aVariant Which exact image format variant to use for the
|
---|
6363 | * destination image.
|
---|
6364 | * @param pKeyStore The optional key store for decrypting the data for
|
---|
6365 | * encrypted media during the export.
|
---|
6366 | * @param hVfsIosDst The destination I/O stream object.
|
---|
6367 | * @param aProgress Progress object to use.
|
---|
6368 | * @return
|
---|
6369 | *
|
---|
6370 | * @note The source format is defined by the Medium instance.
|
---|
6371 | */
|
---|
6372 | HRESULT Medium::i_exportFile(const char *aFilename,
|
---|
6373 | const ComObjPtr<MediumFormat> &aFormat,
|
---|
6374 | MediumVariant_T aVariant,
|
---|
6375 | SecretKeyStore *pKeyStore,
|
---|
6376 | RTVFSIOSTREAM hVfsIosDst,
|
---|
6377 | const ComObjPtr<Progress> &aProgress)
|
---|
6378 | {
|
---|
6379 | AssertPtrReturn(aFilename, E_INVALIDARG);
|
---|
6380 | AssertReturn(aFormat.isNotNull(), E_INVALIDARG);
|
---|
6381 | AssertReturn(aProgress.isNotNull(), E_INVALIDARG);
|
---|
6382 |
|
---|
6383 | AutoCaller autoCaller(this);
|
---|
6384 | HRESULT hrc = autoCaller.rc();
|
---|
6385 | if (SUCCEEDED(hrc))
|
---|
6386 | {
|
---|
6387 | /*
|
---|
6388 | * Setup VD interfaces.
|
---|
6389 | */
|
---|
6390 | PVDINTERFACE pVDImageIfaces = m->vdImageIfaces;
|
---|
6391 | PVDINTERFACEIO pVfsIoIf;
|
---|
6392 | int vrc = VDIfCreateFromVfsStream(hVfsIosDst, RTFILE_O_WRITE, &pVfsIoIf);
|
---|
6393 | if (RT_SUCCESS(vrc))
|
---|
6394 | {
|
---|
6395 | vrc = VDInterfaceAdd(&pVfsIoIf->Core, "Medium::ExportTaskVfsIos", VDINTERFACETYPE_IO,
|
---|
6396 | pVfsIoIf, sizeof(VDINTERFACEIO), &pVDImageIfaces);
|
---|
6397 | if (RT_SUCCESS(vrc))
|
---|
6398 | {
|
---|
6399 | /*
|
---|
6400 | * Get a readonly hdd for this medium (source).
|
---|
6401 | */
|
---|
6402 | MediumCryptoFilterSettings CryptoSettingsRead;
|
---|
6403 | MediumLockList SourceMediumLockList;
|
---|
6404 | PVDISK pSrcHdd;
|
---|
6405 | hrc = i_openForIO(false /*fWritable*/, pKeyStore, &pSrcHdd, &SourceMediumLockList, &CryptoSettingsRead);
|
---|
6406 | if (SUCCEEDED(hrc))
|
---|
6407 | {
|
---|
6408 | /*
|
---|
6409 | * Create the target medium.
|
---|
6410 | */
|
---|
6411 | Utf8Str strDstFormat(aFormat->i_getId());
|
---|
6412 |
|
---|
6413 | /* ensure the target directory exists */
|
---|
6414 | uint64_t fDstCapabilities = aFormat->i_getCapabilities();
|
---|
6415 | if (fDstCapabilities & MediumFormatCapabilities_File)
|
---|
6416 | {
|
---|
6417 | Utf8Str strDstLocation(aFilename);
|
---|
6418 | hrc = VirtualBox::i_ensureFilePathExists(strDstLocation.c_str(),
|
---|
6419 | !(aVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
6420 | }
|
---|
6421 | if (SUCCEEDED(hrc))
|
---|
6422 | {
|
---|
6423 | PVDISK pDstHdd;
|
---|
6424 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDstHdd);
|
---|
6425 | if (RT_SUCCESS(vrc))
|
---|
6426 | {
|
---|
6427 | /*
|
---|
6428 | * Create an interface for getting progress callbacks.
|
---|
6429 | */
|
---|
6430 | VDINTERFACEPROGRESS ProgressIf = VDINTERFACEPROGRESS_INITALIZER(aProgress->i_vdProgressCallback);
|
---|
6431 | PVDINTERFACE pProgress = NULL;
|
---|
6432 | vrc = VDInterfaceAdd(&ProgressIf.Core, "export-progress", VDINTERFACETYPE_PROGRESS,
|
---|
6433 | &*aProgress, sizeof(ProgressIf), &pProgress);
|
---|
6434 | AssertRC(vrc);
|
---|
6435 |
|
---|
6436 | /*
|
---|
6437 | * Do the exporting.
|
---|
6438 | */
|
---|
6439 | vrc = VDCopy(pSrcHdd,
|
---|
6440 | VD_LAST_IMAGE,
|
---|
6441 | pDstHdd,
|
---|
6442 | strDstFormat.c_str(),
|
---|
6443 | aFilename,
|
---|
6444 | false /* fMoveByRename */,
|
---|
6445 | 0 /* cbSize */,
|
---|
6446 | aVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted),
|
---|
6447 | NULL /* pDstUuid */,
|
---|
6448 | VD_OPEN_FLAGS_NORMAL | VD_OPEN_FLAGS_SEQUENTIAL,
|
---|
6449 | pProgress,
|
---|
6450 | pVDImageIfaces,
|
---|
6451 | NULL);
|
---|
6452 | if (RT_SUCCESS(vrc))
|
---|
6453 | hrc = S_OK;
|
---|
6454 | else
|
---|
6455 | hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Could not create the exported medium '%s'%s"),
|
---|
6456 | aFilename, i_vdError(vrc).c_str());
|
---|
6457 | VDDestroy(pDstHdd);
|
---|
6458 | }
|
---|
6459 | else
|
---|
6460 | hrc = setErrorVrc(vrc);
|
---|
6461 | }
|
---|
6462 | }
|
---|
6463 | VDDestroy(pSrcHdd);
|
---|
6464 | }
|
---|
6465 | else
|
---|
6466 | hrc = setErrorVrc(vrc, "VDInterfaceAdd -> %Rrc", vrc);
|
---|
6467 | VDIfDestroyFromVfsStream(pVfsIoIf);
|
---|
6468 | }
|
---|
6469 | else
|
---|
6470 | hrc = setErrorVrc(vrc, "VDIfCreateFromVfsStream -> %Rrc", vrc);
|
---|
6471 | }
|
---|
6472 | return hrc;
|
---|
6473 | }
|
---|
6474 |
|
---|
6475 | /**
|
---|
6476 | * Used by IAppliance to import disk images.
|
---|
6477 | *
|
---|
6478 | * @param aFilename Filename to read (UTF8).
|
---|
6479 | * @param aFormat Medium format for reading @a aFilename.
|
---|
6480 | * @param aVariant Which exact image format variant to use
|
---|
6481 | * for the destination image.
|
---|
6482 | * @param aVfsIosSrc Handle to the source I/O stream.
|
---|
6483 | * @param aParent Parent medium. May be NULL.
|
---|
6484 | * @param aProgress Progress object to use.
|
---|
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 | {
|
---|
6500 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
6501 | * to lock order violations, it probably causes lock order issues related
|
---|
6502 | * to the AutoCaller usage. */
|
---|
6503 | AssertPtrReturn(aFilename, E_INVALIDARG);
|
---|
6504 | AssertReturn(!aFormat.isNull(), E_INVALIDARG);
|
---|
6505 | AssertReturn(!aProgress.isNull(), E_INVALIDARG);
|
---|
6506 |
|
---|
6507 | AutoCaller autoCaller(this);
|
---|
6508 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
6509 |
|
---|
6510 | HRESULT rc = S_OK;
|
---|
6511 | Medium::Task *pTask = NULL;
|
---|
6512 |
|
---|
6513 | try
|
---|
6514 | {
|
---|
6515 | // locking: we need the tree lock first because we access parent pointers
|
---|
6516 | // and we need to write-lock the media involved
|
---|
6517 | uint32_t cHandles = 2;
|
---|
6518 | LockHandle* pHandles[3] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
6519 | this->lockHandle() };
|
---|
6520 | /* Only add parent to the lock if it is not null */
|
---|
6521 | if (!aParent.isNull())
|
---|
6522 | pHandles[cHandles++] = aParent->lockHandle();
|
---|
6523 | AutoWriteLock alock(cHandles,
|
---|
6524 | pHandles
|
---|
6525 | COMMA_LOCKVAL_SRC_POS);
|
---|
6526 |
|
---|
6527 | if ( m->state != MediumState_NotCreated
|
---|
6528 | && m->state != MediumState_Created)
|
---|
6529 | throw i_setStateError();
|
---|
6530 |
|
---|
6531 | /* Build the target lock list. */
|
---|
6532 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
6533 | alock.release();
|
---|
6534 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
6535 | this /* pToLockWrite */,
|
---|
6536 | false /* fMediumLockWriteAll */,
|
---|
6537 | aParent,
|
---|
6538 | *pTargetMediumLockList);
|
---|
6539 | alock.acquire();
|
---|
6540 | if (FAILED(rc))
|
---|
6541 | {
|
---|
6542 | delete pTargetMediumLockList;
|
---|
6543 | throw rc;
|
---|
6544 | }
|
---|
6545 |
|
---|
6546 | alock.release();
|
---|
6547 | rc = pTargetMediumLockList->Lock();
|
---|
6548 | alock.acquire();
|
---|
6549 | if (FAILED(rc))
|
---|
6550 | {
|
---|
6551 | delete pTargetMediumLockList;
|
---|
6552 | throw setError(rc,
|
---|
6553 | tr("Failed to lock target media '%s'"),
|
---|
6554 | i_getLocationFull().c_str());
|
---|
6555 | }
|
---|
6556 |
|
---|
6557 | /* setup task object to carry out the operation asynchronously */
|
---|
6558 | pTask = new Medium::ImportTask(this, aProgress, aFilename, aFormat, aVariant,
|
---|
6559 | aVfsIosSrc, aParent, pTargetMediumLockList);
|
---|
6560 | rc = pTask->rc();
|
---|
6561 | AssertComRC(rc);
|
---|
6562 | if (FAILED(rc))
|
---|
6563 | throw rc;
|
---|
6564 |
|
---|
6565 | if (m->state == MediumState_NotCreated)
|
---|
6566 | m->state = MediumState_Creating;
|
---|
6567 | }
|
---|
6568 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6569 |
|
---|
6570 | if (SUCCEEDED(rc))
|
---|
6571 | rc = pTask->createThread();
|
---|
6572 | else if (pTask != NULL)
|
---|
6573 | delete pTask;
|
---|
6574 |
|
---|
6575 | return rc;
|
---|
6576 | }
|
---|
6577 |
|
---|
6578 | /**
|
---|
6579 | * Internal version of the public CloneTo API which allows to enable certain
|
---|
6580 | * optimizations to improve speed during VM cloning.
|
---|
6581 | *
|
---|
6582 | * @param aTarget Target medium
|
---|
6583 | * @param aVariant Which exact image format variant to use
|
---|
6584 | * for the destination image.
|
---|
6585 | * @param aParent Parent medium. May be NULL.
|
---|
6586 | * @param aProgress Progress object to use.
|
---|
6587 | * @param idxSrcImageSame The last image in the source chain which has the
|
---|
6588 | * same content as the given image in the destination
|
---|
6589 | * chain. Use UINT32_MAX to disable this optimization.
|
---|
6590 | * @param idxDstImageSame The last image in the destination chain which has the
|
---|
6591 | * same content as the given image in the source chain.
|
---|
6592 | * Use UINT32_MAX to disable this optimization.
|
---|
6593 | * @return
|
---|
6594 | */
|
---|
6595 | HRESULT Medium::i_cloneToEx(const ComObjPtr<Medium> &aTarget, MediumVariant_T aVariant,
|
---|
6596 | const ComObjPtr<Medium> &aParent, IProgress **aProgress,
|
---|
6597 | uint32_t idxSrcImageSame, uint32_t idxDstImageSame)
|
---|
6598 | {
|
---|
6599 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
6600 | * to lock order violations, it probably causes lock order issues related
|
---|
6601 | * to the AutoCaller usage. */
|
---|
6602 | CheckComArgNotNull(aTarget);
|
---|
6603 | CheckComArgOutPointerValid(aProgress);
|
---|
6604 | ComAssertRet(aTarget != this, E_INVALIDARG);
|
---|
6605 |
|
---|
6606 | AutoCaller autoCaller(this);
|
---|
6607 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
6608 |
|
---|
6609 | HRESULT rc = S_OK;
|
---|
6610 | ComObjPtr<Progress> pProgress;
|
---|
6611 | Medium::Task *pTask = NULL;
|
---|
6612 |
|
---|
6613 | try
|
---|
6614 | {
|
---|
6615 | // locking: we need the tree lock first because we access parent pointers
|
---|
6616 | // and we need to write-lock the media involved
|
---|
6617 | uint32_t cHandles = 3;
|
---|
6618 | LockHandle* pHandles[4] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
6619 | this->lockHandle(),
|
---|
6620 | aTarget->lockHandle() };
|
---|
6621 | /* Only add parent to the lock if it is not null */
|
---|
6622 | if (!aParent.isNull())
|
---|
6623 | pHandles[cHandles++] = aParent->lockHandle();
|
---|
6624 | AutoWriteLock alock(cHandles,
|
---|
6625 | pHandles
|
---|
6626 | COMMA_LOCKVAL_SRC_POS);
|
---|
6627 |
|
---|
6628 | if ( aTarget->m->state != MediumState_NotCreated
|
---|
6629 | && aTarget->m->state != MediumState_Created)
|
---|
6630 | throw aTarget->i_setStateError();
|
---|
6631 |
|
---|
6632 | /* Build the source lock list. */
|
---|
6633 | MediumLockList *pSourceMediumLockList(new MediumLockList());
|
---|
6634 | alock.release();
|
---|
6635 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
6636 | NULL /* pToLockWrite */,
|
---|
6637 | false /* fMediumLockWriteAll */,
|
---|
6638 | NULL,
|
---|
6639 | *pSourceMediumLockList);
|
---|
6640 | alock.acquire();
|
---|
6641 | if (FAILED(rc))
|
---|
6642 | {
|
---|
6643 | delete pSourceMediumLockList;
|
---|
6644 | throw rc;
|
---|
6645 | }
|
---|
6646 |
|
---|
6647 | /* Build the target lock list (including the to-be parent chain). */
|
---|
6648 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
6649 | alock.release();
|
---|
6650 | rc = aTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
6651 | aTarget /* pToLockWrite */,
|
---|
6652 | false /* fMediumLockWriteAll */,
|
---|
6653 | aParent,
|
---|
6654 | *pTargetMediumLockList);
|
---|
6655 | alock.acquire();
|
---|
6656 | if (FAILED(rc))
|
---|
6657 | {
|
---|
6658 | delete pSourceMediumLockList;
|
---|
6659 | delete pTargetMediumLockList;
|
---|
6660 | throw rc;
|
---|
6661 | }
|
---|
6662 |
|
---|
6663 | alock.release();
|
---|
6664 | rc = pSourceMediumLockList->Lock();
|
---|
6665 | alock.acquire();
|
---|
6666 | if (FAILED(rc))
|
---|
6667 | {
|
---|
6668 | delete pSourceMediumLockList;
|
---|
6669 | delete pTargetMediumLockList;
|
---|
6670 | throw setError(rc,
|
---|
6671 | tr("Failed to lock source media '%s'"),
|
---|
6672 | i_getLocationFull().c_str());
|
---|
6673 | }
|
---|
6674 | alock.release();
|
---|
6675 | rc = pTargetMediumLockList->Lock();
|
---|
6676 | alock.acquire();
|
---|
6677 | if (FAILED(rc))
|
---|
6678 | {
|
---|
6679 | delete pSourceMediumLockList;
|
---|
6680 | delete pTargetMediumLockList;
|
---|
6681 | throw setError(rc,
|
---|
6682 | tr("Failed to lock target media '%s'"),
|
---|
6683 | aTarget->i_getLocationFull().c_str());
|
---|
6684 | }
|
---|
6685 |
|
---|
6686 | pProgress.createObject();
|
---|
6687 | rc = pProgress->init(m->pVirtualBox,
|
---|
6688 | static_cast <IMedium *>(this),
|
---|
6689 | BstrFmt(tr("Creating clone medium '%s'"), aTarget->m->strLocationFull.c_str()).raw(),
|
---|
6690 | TRUE /* aCancelable */);
|
---|
6691 | if (FAILED(rc))
|
---|
6692 | {
|
---|
6693 | delete pSourceMediumLockList;
|
---|
6694 | delete pTargetMediumLockList;
|
---|
6695 | throw rc;
|
---|
6696 | }
|
---|
6697 |
|
---|
6698 | /* setup task object to carry out the operation asynchronously */
|
---|
6699 | pTask = new Medium::CloneTask(this, pProgress, aTarget, aVariant,
|
---|
6700 | aParent, idxSrcImageSame,
|
---|
6701 | idxDstImageSame, pSourceMediumLockList,
|
---|
6702 | pTargetMediumLockList);
|
---|
6703 | rc = pTask->rc();
|
---|
6704 | AssertComRC(rc);
|
---|
6705 | if (FAILED(rc))
|
---|
6706 | throw rc;
|
---|
6707 |
|
---|
6708 | if (aTarget->m->state == MediumState_NotCreated)
|
---|
6709 | aTarget->m->state = MediumState_Creating;
|
---|
6710 | }
|
---|
6711 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6712 |
|
---|
6713 | if (SUCCEEDED(rc))
|
---|
6714 | {
|
---|
6715 | rc = pTask->createThread();
|
---|
6716 |
|
---|
6717 | if (SUCCEEDED(rc))
|
---|
6718 | pProgress.queryInterfaceTo(aProgress);
|
---|
6719 | }
|
---|
6720 | else if (pTask != NULL)
|
---|
6721 | delete pTask;
|
---|
6722 |
|
---|
6723 | return rc;
|
---|
6724 | }
|
---|
6725 |
|
---|
6726 | /**
|
---|
6727 | * Returns the key identifier for this medium if encryption is configured.
|
---|
6728 | *
|
---|
6729 | * @returns Key identifier or empty string if no encryption is configured.
|
---|
6730 | */
|
---|
6731 | const Utf8Str& Medium::i_getKeyId()
|
---|
6732 | {
|
---|
6733 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
6734 |
|
---|
6735 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6736 |
|
---|
6737 | settings::StringsMap::const_iterator it = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
6738 | if (it == pBase->m->mapProperties.end())
|
---|
6739 | return Utf8Str::Empty;
|
---|
6740 |
|
---|
6741 | return it->second;
|
---|
6742 | }
|
---|
6743 |
|
---|
6744 |
|
---|
6745 | /**
|
---|
6746 | * Returns all filter related properties.
|
---|
6747 | *
|
---|
6748 | * @returns COM status code.
|
---|
6749 | * @param aReturnNames Where to store the properties names on success.
|
---|
6750 | * @param aReturnValues Where to store the properties values on success.
|
---|
6751 | */
|
---|
6752 | HRESULT Medium::i_getFilterProperties(std::vector<com::Utf8Str> &aReturnNames,
|
---|
6753 | std::vector<com::Utf8Str> &aReturnValues)
|
---|
6754 | {
|
---|
6755 | std::vector<com::Utf8Str> aPropNames;
|
---|
6756 | std::vector<com::Utf8Str> aPropValues;
|
---|
6757 | HRESULT hrc = getProperties(Utf8Str(""), aPropNames, aPropValues);
|
---|
6758 |
|
---|
6759 | if (SUCCEEDED(hrc))
|
---|
6760 | {
|
---|
6761 | unsigned cReturnSize = 0;
|
---|
6762 | aReturnNames.resize(0);
|
---|
6763 | aReturnValues.resize(0);
|
---|
6764 | for (unsigned idx = 0; idx < aPropNames.size(); idx++)
|
---|
6765 | {
|
---|
6766 | if (i_isPropertyForFilter(aPropNames[idx]))
|
---|
6767 | {
|
---|
6768 | aReturnNames.resize(cReturnSize + 1);
|
---|
6769 | aReturnValues.resize(cReturnSize + 1);
|
---|
6770 | aReturnNames[cReturnSize] = aPropNames[idx];
|
---|
6771 | aReturnValues[cReturnSize] = aPropValues[idx];
|
---|
6772 | cReturnSize++;
|
---|
6773 | }
|
---|
6774 | }
|
---|
6775 | }
|
---|
6776 |
|
---|
6777 | return hrc;
|
---|
6778 | }
|
---|
6779 |
|
---|
6780 | /**
|
---|
6781 | * Preparation to move this medium to a new location
|
---|
6782 | *
|
---|
6783 | * @param aLocation Location of the storage unit. If the location is a FS-path,
|
---|
6784 | * then it can be relative to the VirtualBox home directory.
|
---|
6785 | *
|
---|
6786 | * @note Must be called from under this object's write lock.
|
---|
6787 | */
|
---|
6788 | HRESULT Medium::i_preparationForMoving(const Utf8Str &aLocation)
|
---|
6789 | {
|
---|
6790 | HRESULT rc = E_FAIL;
|
---|
6791 |
|
---|
6792 | if (i_getLocationFull() != aLocation)
|
---|
6793 | {
|
---|
6794 | m->strNewLocationFull = aLocation;
|
---|
6795 | m->fMoveThisMedium = true;
|
---|
6796 | rc = S_OK;
|
---|
6797 | }
|
---|
6798 |
|
---|
6799 | return rc;
|
---|
6800 | }
|
---|
6801 |
|
---|
6802 | /**
|
---|
6803 | * Checking whether current operation "moving" or not
|
---|
6804 | */
|
---|
6805 | bool Medium::i_isMoveOperation(const ComObjPtr<Medium> &aTarget) const
|
---|
6806 | {
|
---|
6807 | RT_NOREF(aTarget);
|
---|
6808 | return (m->fMoveThisMedium == true) ? true:false;
|
---|
6809 | }
|
---|
6810 |
|
---|
6811 | bool Medium::i_resetMoveOperationData()
|
---|
6812 | {
|
---|
6813 | m->strNewLocationFull.setNull();
|
---|
6814 | m->fMoveThisMedium = false;
|
---|
6815 | return true;
|
---|
6816 | }
|
---|
6817 |
|
---|
6818 | Utf8Str Medium::i_getNewLocationForMoving() const
|
---|
6819 | {
|
---|
6820 | if (m->fMoveThisMedium == true)
|
---|
6821 | return m->strNewLocationFull;
|
---|
6822 | else
|
---|
6823 | return Utf8Str();
|
---|
6824 | }
|
---|
6825 | ////////////////////////////////////////////////////////////////////////////////
|
---|
6826 | //
|
---|
6827 | // Private methods
|
---|
6828 | //
|
---|
6829 | ////////////////////////////////////////////////////////////////////////////////
|
---|
6830 |
|
---|
6831 | /**
|
---|
6832 | * Queries information from the medium.
|
---|
6833 | *
|
---|
6834 | * As a result of this call, the accessibility state and data members such as
|
---|
6835 | * size and description will be updated with the current information.
|
---|
6836 | *
|
---|
6837 | * @note This method may block during a system I/O call that checks storage
|
---|
6838 | * accessibility.
|
---|
6839 | *
|
---|
6840 | * @note Caller MUST NOT hold the media tree or medium lock.
|
---|
6841 | *
|
---|
6842 | * @note Locks m->pParent for reading. Locks this object for writing.
|
---|
6843 | *
|
---|
6844 | * @param fSetImageId Whether to reset the UUID contained in the image file
|
---|
6845 | * to the UUID in the medium instance data (see SetIDs())
|
---|
6846 | * @param fSetParentId Whether to reset the parent UUID contained in the image
|
---|
6847 | * file to the parent UUID in the medium instance data (see
|
---|
6848 | * SetIDs())
|
---|
6849 | * @param autoCaller
|
---|
6850 | * @return
|
---|
6851 | */
|
---|
6852 | HRESULT Medium::i_queryInfo(bool fSetImageId, bool fSetParentId, AutoCaller &autoCaller)
|
---|
6853 | {
|
---|
6854 | Assert(!isWriteLockOnCurrentThread());
|
---|
6855 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6856 |
|
---|
6857 | if ( ( m->state != MediumState_Created
|
---|
6858 | && m->state != MediumState_Inaccessible
|
---|
6859 | && m->state != MediumState_LockedRead)
|
---|
6860 | || m->fClosing)
|
---|
6861 | return E_FAIL;
|
---|
6862 |
|
---|
6863 | HRESULT rc = S_OK;
|
---|
6864 |
|
---|
6865 | int vrc = VINF_SUCCESS;
|
---|
6866 |
|
---|
6867 | /* check if a blocking i_queryInfo() call is in progress on some other thread,
|
---|
6868 | * and wait for it to finish if so instead of querying data ourselves */
|
---|
6869 | if (m->queryInfoRunning)
|
---|
6870 | {
|
---|
6871 | Assert( m->state == MediumState_LockedRead
|
---|
6872 | || m->state == MediumState_LockedWrite);
|
---|
6873 |
|
---|
6874 | while (m->queryInfoRunning)
|
---|
6875 | {
|
---|
6876 | alock.release();
|
---|
6877 | /* must not hold the object lock now */
|
---|
6878 | Assert(!isWriteLockOnCurrentThread());
|
---|
6879 | {
|
---|
6880 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
6881 | }
|
---|
6882 | alock.acquire();
|
---|
6883 | }
|
---|
6884 |
|
---|
6885 | return S_OK;
|
---|
6886 | }
|
---|
6887 |
|
---|
6888 | bool success = false;
|
---|
6889 | Utf8Str lastAccessError;
|
---|
6890 |
|
---|
6891 | /* are we dealing with a new medium constructed using the existing
|
---|
6892 | * location? */
|
---|
6893 | bool isImport = m->id.isZero();
|
---|
6894 | unsigned uOpenFlags = VD_OPEN_FLAGS_INFO;
|
---|
6895 |
|
---|
6896 | /* Note that we don't use VD_OPEN_FLAGS_READONLY when opening new
|
---|
6897 | * media because that would prevent necessary modifications
|
---|
6898 | * when opening media of some third-party formats for the first
|
---|
6899 | * time in VirtualBox (such as VMDK for which VDOpen() needs to
|
---|
6900 | * generate an UUID if it is missing) */
|
---|
6901 | if ( m->hddOpenMode == OpenReadOnly
|
---|
6902 | || m->type == MediumType_Readonly
|
---|
6903 | || (!isImport && !fSetImageId && !fSetParentId)
|
---|
6904 | )
|
---|
6905 | uOpenFlags |= VD_OPEN_FLAGS_READONLY;
|
---|
6906 |
|
---|
6907 | /* Open shareable medium with the appropriate flags */
|
---|
6908 | if (m->type == MediumType_Shareable)
|
---|
6909 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
6910 |
|
---|
6911 | /* Lock the medium, which makes the behavior much more consistent, must be
|
---|
6912 | * done before dropping the object lock and setting queryInfoRunning. */
|
---|
6913 | ComPtr<IToken> pToken;
|
---|
6914 | if (uOpenFlags & (VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SHAREABLE))
|
---|
6915 | rc = LockRead(pToken.asOutParam());
|
---|
6916 | else
|
---|
6917 | rc = LockWrite(pToken.asOutParam());
|
---|
6918 | if (FAILED(rc)) return rc;
|
---|
6919 |
|
---|
6920 | /* Copies of the input state fields which are not read-only,
|
---|
6921 | * as we're dropping the lock. CAUTION: be extremely careful what
|
---|
6922 | * you do with the contents of this medium object, as you will
|
---|
6923 | * create races if there are concurrent changes. */
|
---|
6924 | Utf8Str format(m->strFormat);
|
---|
6925 | Utf8Str location(m->strLocationFull);
|
---|
6926 | ComObjPtr<MediumFormat> formatObj = m->formatObj;
|
---|
6927 |
|
---|
6928 | /* "Output" values which can't be set because the lock isn't held
|
---|
6929 | * at the time the values are determined. */
|
---|
6930 | Guid mediumId = m->id;
|
---|
6931 | uint64_t mediumSize = 0;
|
---|
6932 | uint64_t mediumLogicalSize = 0;
|
---|
6933 |
|
---|
6934 | /* Flag whether a base image has a non-zero parent UUID and thus
|
---|
6935 | * need repairing after it was closed again. */
|
---|
6936 | bool fRepairImageZeroParentUuid = false;
|
---|
6937 |
|
---|
6938 | ComObjPtr<VirtualBox> pVirtualBox = m->pVirtualBox;
|
---|
6939 |
|
---|
6940 | /* must be set before leaving the object lock the first time */
|
---|
6941 | m->queryInfoRunning = true;
|
---|
6942 |
|
---|
6943 | /* must leave object lock now, because a lock from a higher lock class
|
---|
6944 | * is needed and also a lengthy operation is coming */
|
---|
6945 | alock.release();
|
---|
6946 | autoCaller.release();
|
---|
6947 |
|
---|
6948 | /* Note that taking the queryInfoSem after leaving the object lock above
|
---|
6949 | * can lead to short spinning of the loops waiting for i_queryInfo() to
|
---|
6950 | * complete. This is unavoidable since the other order causes a lock order
|
---|
6951 | * violation: here it would be requesting the object lock (at the beginning
|
---|
6952 | * of the method), then queryInfoSem, and below the other way round. */
|
---|
6953 | AutoWriteLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
6954 |
|
---|
6955 | /* take the opportunity to have a media tree lock, released initially */
|
---|
6956 | Assert(!isWriteLockOnCurrentThread());
|
---|
6957 | Assert(!pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
6958 | AutoWriteLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
6959 | treeLock.release();
|
---|
6960 |
|
---|
6961 | /* re-take the caller, but not the object lock, to keep uninit away */
|
---|
6962 | autoCaller.add();
|
---|
6963 | if (FAILED(autoCaller.rc()))
|
---|
6964 | {
|
---|
6965 | m->queryInfoRunning = false;
|
---|
6966 | return autoCaller.rc();
|
---|
6967 | }
|
---|
6968 |
|
---|
6969 | try
|
---|
6970 | {
|
---|
6971 | /* skip accessibility checks for host drives */
|
---|
6972 | if (m->hostDrive)
|
---|
6973 | {
|
---|
6974 | success = true;
|
---|
6975 | throw S_OK;
|
---|
6976 | }
|
---|
6977 |
|
---|
6978 | PVDISK hdd;
|
---|
6979 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
6980 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6981 |
|
---|
6982 | try
|
---|
6983 | {
|
---|
6984 | /** @todo This kind of opening of media is assuming that diff
|
---|
6985 | * media can be opened as base media. Should be documented that
|
---|
6986 | * it must work for all medium format backends. */
|
---|
6987 | vrc = VDOpen(hdd,
|
---|
6988 | format.c_str(),
|
---|
6989 | location.c_str(),
|
---|
6990 | uOpenFlags | m->uOpenFlagsDef,
|
---|
6991 | m->vdImageIfaces);
|
---|
6992 | if (RT_FAILURE(vrc))
|
---|
6993 | {
|
---|
6994 | lastAccessError = Utf8StrFmt(tr("Could not open the medium '%s'%s"),
|
---|
6995 | location.c_str(), i_vdError(vrc).c_str());
|
---|
6996 | throw S_OK;
|
---|
6997 | }
|
---|
6998 |
|
---|
6999 | if (formatObj->i_getCapabilities() & MediumFormatCapabilities_Uuid)
|
---|
7000 | {
|
---|
7001 | /* Modify the UUIDs if necessary. The associated fields are
|
---|
7002 | * not modified by other code, so no need to copy. */
|
---|
7003 | if (fSetImageId)
|
---|
7004 | {
|
---|
7005 | alock.acquire();
|
---|
7006 | vrc = VDSetUuid(hdd, 0, m->uuidImage.raw());
|
---|
7007 | alock.release();
|
---|
7008 | if (RT_FAILURE(vrc))
|
---|
7009 | {
|
---|
7010 | lastAccessError = Utf8StrFmt(tr("Could not update the UUID of medium '%s'%s"),
|
---|
7011 | location.c_str(), i_vdError(vrc).c_str());
|
---|
7012 | throw S_OK;
|
---|
7013 | }
|
---|
7014 | mediumId = m->uuidImage;
|
---|
7015 | }
|
---|
7016 | if (fSetParentId)
|
---|
7017 | {
|
---|
7018 | alock.acquire();
|
---|
7019 | vrc = VDSetParentUuid(hdd, 0, m->uuidParentImage.raw());
|
---|
7020 | alock.release();
|
---|
7021 | if (RT_FAILURE(vrc))
|
---|
7022 | {
|
---|
7023 | lastAccessError = Utf8StrFmt(tr("Could not update the parent UUID of medium '%s'%s"),
|
---|
7024 | location.c_str(), i_vdError(vrc).c_str());
|
---|
7025 | throw S_OK;
|
---|
7026 | }
|
---|
7027 | }
|
---|
7028 | /* zap the information, these are no long-term members */
|
---|
7029 | alock.acquire();
|
---|
7030 | unconst(m->uuidImage).clear();
|
---|
7031 | unconst(m->uuidParentImage).clear();
|
---|
7032 | alock.release();
|
---|
7033 |
|
---|
7034 | /* check the UUID */
|
---|
7035 | RTUUID uuid;
|
---|
7036 | vrc = VDGetUuid(hdd, 0, &uuid);
|
---|
7037 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7038 |
|
---|
7039 | if (isImport)
|
---|
7040 | {
|
---|
7041 | mediumId = uuid;
|
---|
7042 |
|
---|
7043 | if (mediumId.isZero() && (m->hddOpenMode == OpenReadOnly))
|
---|
7044 | // only when importing a VDMK that has no UUID, create one in memory
|
---|
7045 | mediumId.create();
|
---|
7046 | }
|
---|
7047 | else
|
---|
7048 | {
|
---|
7049 | Assert(!mediumId.isZero());
|
---|
7050 |
|
---|
7051 | if (mediumId != uuid)
|
---|
7052 | {
|
---|
7053 | /** @todo r=klaus this always refers to VirtualBox.xml as the medium registry, even for new VMs */
|
---|
7054 | lastAccessError = Utf8StrFmt(
|
---|
7055 | tr("UUID {%RTuuid} of the medium '%s' does not match the value {%RTuuid} stored in the media registry ('%s')"),
|
---|
7056 | &uuid,
|
---|
7057 | location.c_str(),
|
---|
7058 | mediumId.raw(),
|
---|
7059 | pVirtualBox->i_settingsFilePath().c_str());
|
---|
7060 | throw S_OK;
|
---|
7061 | }
|
---|
7062 | }
|
---|
7063 | }
|
---|
7064 | else
|
---|
7065 | {
|
---|
7066 | /* the backend does not support storing UUIDs within the
|
---|
7067 | * underlying storage so use what we store in XML */
|
---|
7068 |
|
---|
7069 | if (fSetImageId)
|
---|
7070 | {
|
---|
7071 | /* set the UUID if an API client wants to change it */
|
---|
7072 | alock.acquire();
|
---|
7073 | mediumId = m->uuidImage;
|
---|
7074 | alock.release();
|
---|
7075 | }
|
---|
7076 | else if (isImport)
|
---|
7077 | {
|
---|
7078 | /* generate an UUID for an imported UUID-less medium */
|
---|
7079 | mediumId.create();
|
---|
7080 | }
|
---|
7081 | }
|
---|
7082 |
|
---|
7083 | /* set the image uuid before the below parent uuid handling code
|
---|
7084 | * might place it somewhere in the media tree, so that the medium
|
---|
7085 | * UUID is valid at this point */
|
---|
7086 | alock.acquire();
|
---|
7087 | if (isImport || fSetImageId)
|
---|
7088 | unconst(m->id) = mediumId;
|
---|
7089 | alock.release();
|
---|
7090 |
|
---|
7091 | /* get the medium variant */
|
---|
7092 | unsigned uImageFlags;
|
---|
7093 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
7094 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7095 | alock.acquire();
|
---|
7096 | m->variant = (MediumVariant_T)uImageFlags;
|
---|
7097 | alock.release();
|
---|
7098 |
|
---|
7099 | /* check/get the parent uuid and update corresponding state */
|
---|
7100 | if (uImageFlags & VD_IMAGE_FLAGS_DIFF)
|
---|
7101 | {
|
---|
7102 | RTUUID parentId;
|
---|
7103 | vrc = VDGetParentUuid(hdd, 0, &parentId);
|
---|
7104 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7105 |
|
---|
7106 | /* streamOptimized VMDK images are only accepted as base
|
---|
7107 | * images, as this allows automatic repair of OVF appliances.
|
---|
7108 | * Since such images don't support random writes they will not
|
---|
7109 | * be created for diff images. Only an overly smart user might
|
---|
7110 | * manually create this case. Too bad for him. */
|
---|
7111 | if ( (isImport || fSetParentId)
|
---|
7112 | && !(uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
|
---|
7113 | {
|
---|
7114 | /* the parent must be known to us. Note that we freely
|
---|
7115 | * call locking methods of mVirtualBox and parent, as all
|
---|
7116 | * relevant locks must be already held. There may be no
|
---|
7117 | * concurrent access to the just opened medium on other
|
---|
7118 | * threads yet (and init() will fail if this method reports
|
---|
7119 | * MediumState_Inaccessible) */
|
---|
7120 |
|
---|
7121 | ComObjPtr<Medium> pParent;
|
---|
7122 | if (RTUuidIsNull(&parentId))
|
---|
7123 | rc = VBOX_E_OBJECT_NOT_FOUND;
|
---|
7124 | else
|
---|
7125 | rc = pVirtualBox->i_findHardDiskById(Guid(parentId), false /* aSetError */, &pParent);
|
---|
7126 | if (FAILED(rc))
|
---|
7127 | {
|
---|
7128 | if (fSetImageId && !fSetParentId)
|
---|
7129 | {
|
---|
7130 | /* If the image UUID gets changed for an existing
|
---|
7131 | * image then the parent UUID can be stale. In such
|
---|
7132 | * cases clear the parent information. The parent
|
---|
7133 | * information may/will be re-set later if the
|
---|
7134 | * API client wants to adjust a complete medium
|
---|
7135 | * hierarchy one by one. */
|
---|
7136 | rc = S_OK;
|
---|
7137 | alock.acquire();
|
---|
7138 | RTUuidClear(&parentId);
|
---|
7139 | vrc = VDSetParentUuid(hdd, 0, &parentId);
|
---|
7140 | alock.release();
|
---|
7141 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7142 | }
|
---|
7143 | else
|
---|
7144 | {
|
---|
7145 | lastAccessError = Utf8StrFmt(tr("Parent medium with UUID {%RTuuid} of the medium '%s' is not found in the media registry ('%s')"),
|
---|
7146 | &parentId, location.c_str(),
|
---|
7147 | pVirtualBox->i_settingsFilePath().c_str());
|
---|
7148 | throw S_OK;
|
---|
7149 | }
|
---|
7150 | }
|
---|
7151 |
|
---|
7152 | /* must drop the caller before taking the tree lock */
|
---|
7153 | autoCaller.release();
|
---|
7154 | /* we set m->pParent & children() */
|
---|
7155 | treeLock.acquire();
|
---|
7156 | autoCaller.add();
|
---|
7157 | if (FAILED(autoCaller.rc()))
|
---|
7158 | throw autoCaller.rc();
|
---|
7159 |
|
---|
7160 | if (m->pParent)
|
---|
7161 | i_deparent();
|
---|
7162 |
|
---|
7163 | if (!pParent.isNull())
|
---|
7164 | if (pParent->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
7165 | {
|
---|
7166 | AutoReadLock plock(pParent COMMA_LOCKVAL_SRC_POS);
|
---|
7167 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7168 | 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"),
|
---|
7169 | pParent->m->strLocationFull.c_str());
|
---|
7170 | }
|
---|
7171 | i_setParent(pParent);
|
---|
7172 |
|
---|
7173 | treeLock.release();
|
---|
7174 | }
|
---|
7175 | else
|
---|
7176 | {
|
---|
7177 | /* must drop the caller before taking the tree lock */
|
---|
7178 | autoCaller.release();
|
---|
7179 | /* we access m->pParent */
|
---|
7180 | treeLock.acquire();
|
---|
7181 | autoCaller.add();
|
---|
7182 | if (FAILED(autoCaller.rc()))
|
---|
7183 | throw autoCaller.rc();
|
---|
7184 |
|
---|
7185 | /* check that parent UUIDs match. Note that there's no need
|
---|
7186 | * for the parent's AutoCaller (our lifetime is bound to
|
---|
7187 | * it) */
|
---|
7188 |
|
---|
7189 | if (m->pParent.isNull())
|
---|
7190 | {
|
---|
7191 | /* Due to a bug in VDCopy() in VirtualBox 3.0.0-3.0.14
|
---|
7192 | * and 3.1.0-3.1.8 there are base images out there
|
---|
7193 | * which have a non-zero parent UUID. No point in
|
---|
7194 | * complaining about them, instead automatically
|
---|
7195 | * repair the problem. Later we can bring back the
|
---|
7196 | * error message, but we should wait until really
|
---|
7197 | * most users have repaired their images, either with
|
---|
7198 | * VBoxFixHdd or this way. */
|
---|
7199 | #if 1
|
---|
7200 | fRepairImageZeroParentUuid = true;
|
---|
7201 | #else /* 0 */
|
---|
7202 | lastAccessError = Utf8StrFmt(
|
---|
7203 | tr("Medium type of '%s' is differencing but it is not associated with any parent medium in the media registry ('%s')"),
|
---|
7204 | location.c_str(),
|
---|
7205 | pVirtualBox->settingsFilePath().c_str());
|
---|
7206 | treeLock.release();
|
---|
7207 | throw S_OK;
|
---|
7208 | #endif /* 0 */
|
---|
7209 | }
|
---|
7210 |
|
---|
7211 | {
|
---|
7212 | autoCaller.release();
|
---|
7213 | AutoReadLock parentLock(m->pParent COMMA_LOCKVAL_SRC_POS);
|
---|
7214 | autoCaller.add();
|
---|
7215 | if (FAILED(autoCaller.rc()))
|
---|
7216 | throw autoCaller.rc();
|
---|
7217 |
|
---|
7218 | if ( !fRepairImageZeroParentUuid
|
---|
7219 | && m->pParent->i_getState() != MediumState_Inaccessible
|
---|
7220 | && m->pParent->i_getId() != parentId)
|
---|
7221 | {
|
---|
7222 | /** @todo r=klaus this always refers to VirtualBox.xml as the medium registry, even for new VMs */
|
---|
7223 | lastAccessError = Utf8StrFmt(
|
---|
7224 | tr("Parent UUID {%RTuuid} of the medium '%s' does not match UUID {%RTuuid} of its parent medium stored in the media registry ('%s')"),
|
---|
7225 | &parentId, location.c_str(),
|
---|
7226 | m->pParent->i_getId().raw(),
|
---|
7227 | pVirtualBox->i_settingsFilePath().c_str());
|
---|
7228 | parentLock.release();
|
---|
7229 | treeLock.release();
|
---|
7230 | throw S_OK;
|
---|
7231 | }
|
---|
7232 | }
|
---|
7233 |
|
---|
7234 | /// @todo NEWMEDIA what to do if the parent is not
|
---|
7235 | /// accessible while the diff is? Probably nothing. The
|
---|
7236 | /// real code will detect the mismatch anyway.
|
---|
7237 |
|
---|
7238 | treeLock.release();
|
---|
7239 | }
|
---|
7240 | }
|
---|
7241 |
|
---|
7242 | mediumSize = VDGetFileSize(hdd, 0);
|
---|
7243 | mediumLogicalSize = VDGetSize(hdd, 0);
|
---|
7244 |
|
---|
7245 | success = true;
|
---|
7246 | }
|
---|
7247 | catch (HRESULT aRC)
|
---|
7248 | {
|
---|
7249 | rc = aRC;
|
---|
7250 | }
|
---|
7251 |
|
---|
7252 | vrc = VDDestroy(hdd);
|
---|
7253 | if (RT_FAILURE(vrc))
|
---|
7254 | {
|
---|
7255 | lastAccessError = Utf8StrFmt(tr("Could not update and close the medium '%s'%s"),
|
---|
7256 | location.c_str(), i_vdError(vrc).c_str());
|
---|
7257 | success = false;
|
---|
7258 | throw S_OK;
|
---|
7259 | }
|
---|
7260 | }
|
---|
7261 | catch (HRESULT aRC)
|
---|
7262 | {
|
---|
7263 | rc = aRC;
|
---|
7264 | }
|
---|
7265 |
|
---|
7266 | autoCaller.release();
|
---|
7267 | treeLock.acquire();
|
---|
7268 | autoCaller.add();
|
---|
7269 | if (FAILED(autoCaller.rc()))
|
---|
7270 | {
|
---|
7271 | m->queryInfoRunning = false;
|
---|
7272 | return autoCaller.rc();
|
---|
7273 | }
|
---|
7274 | alock.acquire();
|
---|
7275 |
|
---|
7276 | if (success)
|
---|
7277 | {
|
---|
7278 | m->size = mediumSize;
|
---|
7279 | m->logicalSize = mediumLogicalSize;
|
---|
7280 | m->strLastAccessError.setNull();
|
---|
7281 | }
|
---|
7282 | else
|
---|
7283 | {
|
---|
7284 | m->strLastAccessError = lastAccessError;
|
---|
7285 | Log1WarningFunc(("'%s' is not accessible (error='%s', rc=%Rhrc, vrc=%Rrc)\n",
|
---|
7286 | location.c_str(), m->strLastAccessError.c_str(), rc, vrc));
|
---|
7287 | }
|
---|
7288 |
|
---|
7289 | /* Set the proper state according to the result of the check */
|
---|
7290 | if (success)
|
---|
7291 | m->preLockState = MediumState_Created;
|
---|
7292 | else
|
---|
7293 | m->preLockState = MediumState_Inaccessible;
|
---|
7294 |
|
---|
7295 | /* unblock anyone waiting for the i_queryInfo results */
|
---|
7296 | qlock.release();
|
---|
7297 | m->queryInfoRunning = false;
|
---|
7298 |
|
---|
7299 | pToken->Abandon();
|
---|
7300 | pToken.setNull();
|
---|
7301 |
|
---|
7302 | if (FAILED(rc)) return rc;
|
---|
7303 |
|
---|
7304 | /* If this is a base image which incorrectly has a parent UUID set,
|
---|
7305 | * repair the image now by zeroing the parent UUID. This is only done
|
---|
7306 | * when we have structural information from a config file, on import
|
---|
7307 | * this is not possible. If someone would accidentally call openMedium
|
---|
7308 | * with a diff image before the base is registered this would destroy
|
---|
7309 | * the diff. Not acceptable. */
|
---|
7310 | if (fRepairImageZeroParentUuid)
|
---|
7311 | {
|
---|
7312 | rc = LockWrite(pToken.asOutParam());
|
---|
7313 | if (FAILED(rc)) return rc;
|
---|
7314 |
|
---|
7315 | alock.release();
|
---|
7316 |
|
---|
7317 | try
|
---|
7318 | {
|
---|
7319 | PVDISK hdd;
|
---|
7320 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
7321 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7322 |
|
---|
7323 | try
|
---|
7324 | {
|
---|
7325 | vrc = VDOpen(hdd,
|
---|
7326 | format.c_str(),
|
---|
7327 | location.c_str(),
|
---|
7328 | (uOpenFlags & ~VD_OPEN_FLAGS_READONLY) | m->uOpenFlagsDef,
|
---|
7329 | m->vdImageIfaces);
|
---|
7330 | if (RT_FAILURE(vrc))
|
---|
7331 | throw S_OK;
|
---|
7332 |
|
---|
7333 | RTUUID zeroParentUuid;
|
---|
7334 | RTUuidClear(&zeroParentUuid);
|
---|
7335 | vrc = VDSetParentUuid(hdd, 0, &zeroParentUuid);
|
---|
7336 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7337 | }
|
---|
7338 | catch (HRESULT aRC)
|
---|
7339 | {
|
---|
7340 | rc = aRC;
|
---|
7341 | }
|
---|
7342 |
|
---|
7343 | VDDestroy(hdd);
|
---|
7344 | }
|
---|
7345 | catch (HRESULT aRC)
|
---|
7346 | {
|
---|
7347 | rc = aRC;
|
---|
7348 | }
|
---|
7349 |
|
---|
7350 | pToken->Abandon();
|
---|
7351 | pToken.setNull();
|
---|
7352 | if (FAILED(rc)) return rc;
|
---|
7353 | }
|
---|
7354 |
|
---|
7355 | return rc;
|
---|
7356 | }
|
---|
7357 |
|
---|
7358 | /**
|
---|
7359 | * Performs extra checks if the medium can be closed and returns S_OK in
|
---|
7360 | * this case. Otherwise, returns a respective error message. Called by
|
---|
7361 | * Close() under the medium tree lock and the medium lock.
|
---|
7362 | *
|
---|
7363 | * @note Also reused by Medium::Reset().
|
---|
7364 | *
|
---|
7365 | * @note Caller must hold the media tree write lock!
|
---|
7366 | */
|
---|
7367 | HRESULT Medium::i_canClose()
|
---|
7368 | {
|
---|
7369 | Assert(m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
7370 |
|
---|
7371 | if (i_getChildren().size() != 0)
|
---|
7372 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
7373 | tr("Cannot close medium '%s' because it has %d child media"),
|
---|
7374 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
7375 |
|
---|
7376 | return S_OK;
|
---|
7377 | }
|
---|
7378 |
|
---|
7379 | /**
|
---|
7380 | * Unregisters this medium with mVirtualBox. Called by close() under the medium tree lock.
|
---|
7381 | *
|
---|
7382 | * @note Caller must have locked the media tree lock for writing!
|
---|
7383 | */
|
---|
7384 | HRESULT Medium::i_unregisterWithVirtualBox()
|
---|
7385 | {
|
---|
7386 | /* Note that we need to de-associate ourselves from the parent to let
|
---|
7387 | * VirtualBox::i_unregisterMedium() properly save the registry */
|
---|
7388 |
|
---|
7389 | /* we modify m->pParent and access children */
|
---|
7390 | Assert(m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
7391 |
|
---|
7392 | Medium *pParentBackup = m->pParent;
|
---|
7393 | AssertReturn(i_getChildren().size() == 0, E_FAIL);
|
---|
7394 | if (m->pParent)
|
---|
7395 | i_deparent();
|
---|
7396 |
|
---|
7397 | HRESULT rc = m->pVirtualBox->i_unregisterMedium(this);
|
---|
7398 | if (FAILED(rc))
|
---|
7399 | {
|
---|
7400 | if (pParentBackup)
|
---|
7401 | {
|
---|
7402 | // re-associate with the parent as we are still relatives in the registry
|
---|
7403 | i_setParent(pParentBackup);
|
---|
7404 | }
|
---|
7405 | }
|
---|
7406 |
|
---|
7407 | return rc;
|
---|
7408 | }
|
---|
7409 |
|
---|
7410 | /**
|
---|
7411 | * Like SetProperty but do not trigger a settings store. Only for internal use!
|
---|
7412 | */
|
---|
7413 | HRESULT Medium::i_setPropertyDirect(const Utf8Str &aName, const Utf8Str &aValue)
|
---|
7414 | {
|
---|
7415 | AutoCaller autoCaller(this);
|
---|
7416 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
7417 |
|
---|
7418 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7419 |
|
---|
7420 | switch (m->state)
|
---|
7421 | {
|
---|
7422 | case MediumState_Created:
|
---|
7423 | case MediumState_Inaccessible:
|
---|
7424 | break;
|
---|
7425 | default:
|
---|
7426 | return i_setStateError();
|
---|
7427 | }
|
---|
7428 |
|
---|
7429 | m->mapProperties[aName] = aValue;
|
---|
7430 |
|
---|
7431 | return S_OK;
|
---|
7432 | }
|
---|
7433 |
|
---|
7434 | /**
|
---|
7435 | * Sets the extended error info according to the current media state.
|
---|
7436 | *
|
---|
7437 | * @note Must be called from under this object's write or read lock.
|
---|
7438 | */
|
---|
7439 | HRESULT Medium::i_setStateError()
|
---|
7440 | {
|
---|
7441 | HRESULT rc = E_FAIL;
|
---|
7442 |
|
---|
7443 | switch (m->state)
|
---|
7444 | {
|
---|
7445 | case MediumState_NotCreated:
|
---|
7446 | {
|
---|
7447 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7448 | tr("Storage for the medium '%s' is not created"),
|
---|
7449 | m->strLocationFull.c_str());
|
---|
7450 | break;
|
---|
7451 | }
|
---|
7452 | case MediumState_Created:
|
---|
7453 | {
|
---|
7454 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7455 | tr("Storage for the medium '%s' is already created"),
|
---|
7456 | m->strLocationFull.c_str());
|
---|
7457 | break;
|
---|
7458 | }
|
---|
7459 | case MediumState_LockedRead:
|
---|
7460 | {
|
---|
7461 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7462 | tr("Medium '%s' is locked for reading by another task"),
|
---|
7463 | m->strLocationFull.c_str());
|
---|
7464 | break;
|
---|
7465 | }
|
---|
7466 | case MediumState_LockedWrite:
|
---|
7467 | {
|
---|
7468 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7469 | tr("Medium '%s' is locked for writing by another task"),
|
---|
7470 | m->strLocationFull.c_str());
|
---|
7471 | break;
|
---|
7472 | }
|
---|
7473 | case MediumState_Inaccessible:
|
---|
7474 | {
|
---|
7475 | /* be in sync with Console::powerUpThread() */
|
---|
7476 | if (!m->strLastAccessError.isEmpty())
|
---|
7477 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7478 | tr("Medium '%s' is not accessible. %s"),
|
---|
7479 | m->strLocationFull.c_str(), m->strLastAccessError.c_str());
|
---|
7480 | else
|
---|
7481 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7482 | tr("Medium '%s' is not accessible"),
|
---|
7483 | m->strLocationFull.c_str());
|
---|
7484 | break;
|
---|
7485 | }
|
---|
7486 | case MediumState_Creating:
|
---|
7487 | {
|
---|
7488 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7489 | tr("Storage for the medium '%s' is being created"),
|
---|
7490 | m->strLocationFull.c_str());
|
---|
7491 | break;
|
---|
7492 | }
|
---|
7493 | case MediumState_Deleting:
|
---|
7494 | {
|
---|
7495 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7496 | tr("Storage for the medium '%s' is being deleted"),
|
---|
7497 | m->strLocationFull.c_str());
|
---|
7498 | break;
|
---|
7499 | }
|
---|
7500 | default:
|
---|
7501 | {
|
---|
7502 | AssertFailed();
|
---|
7503 | break;
|
---|
7504 | }
|
---|
7505 | }
|
---|
7506 |
|
---|
7507 | return rc;
|
---|
7508 | }
|
---|
7509 |
|
---|
7510 | /**
|
---|
7511 | * Sets the value of m->strLocationFull. The given location must be a fully
|
---|
7512 | * qualified path; relative paths are not supported here.
|
---|
7513 | *
|
---|
7514 | * As a special exception, if the specified location is a file path that ends with '/'
|
---|
7515 | * then the file name part will be generated by this method automatically in the format
|
---|
7516 | * '{\<uuid\>}.\<ext\>' where \<uuid\> is a fresh UUID that this method will generate
|
---|
7517 | * and assign to this medium, and \<ext\> is the default extension for this
|
---|
7518 | * medium's storage format. Note that this procedure requires the media state to
|
---|
7519 | * be NotCreated and will return a failure otherwise.
|
---|
7520 | *
|
---|
7521 | * @param aLocation Location of the storage unit. If the location is a FS-path,
|
---|
7522 | * then it can be relative to the VirtualBox home directory.
|
---|
7523 | * @param aFormat Optional fallback format if it is an import and the format
|
---|
7524 | * cannot be determined.
|
---|
7525 | *
|
---|
7526 | * @note Must be called from under this object's write lock.
|
---|
7527 | */
|
---|
7528 | HRESULT Medium::i_setLocation(const Utf8Str &aLocation,
|
---|
7529 | const Utf8Str &aFormat /* = Utf8Str::Empty */)
|
---|
7530 | {
|
---|
7531 | AssertReturn(!aLocation.isEmpty(), E_FAIL);
|
---|
7532 |
|
---|
7533 | AutoCaller autoCaller(this);
|
---|
7534 | AssertComRCReturnRC(autoCaller.rc());
|
---|
7535 |
|
---|
7536 | /* formatObj may be null only when initializing from an existing path and
|
---|
7537 | * no format is known yet */
|
---|
7538 | AssertReturn( (!m->strFormat.isEmpty() && !m->formatObj.isNull())
|
---|
7539 | || ( getObjectState().getState() == ObjectState::InInit
|
---|
7540 | && m->state != MediumState_NotCreated
|
---|
7541 | && m->id.isZero()
|
---|
7542 | && m->strFormat.isEmpty()
|
---|
7543 | && m->formatObj.isNull()),
|
---|
7544 | E_FAIL);
|
---|
7545 |
|
---|
7546 | /* are we dealing with a new medium constructed using the existing
|
---|
7547 | * location? */
|
---|
7548 | bool isImport = m->strFormat.isEmpty();
|
---|
7549 |
|
---|
7550 | if ( isImport
|
---|
7551 | || ( (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
7552 | && !m->hostDrive))
|
---|
7553 | {
|
---|
7554 | Guid id;
|
---|
7555 |
|
---|
7556 | Utf8Str locationFull(aLocation);
|
---|
7557 |
|
---|
7558 | if (m->state == MediumState_NotCreated)
|
---|
7559 | {
|
---|
7560 | /* must be a file (formatObj must be already known) */
|
---|
7561 | Assert(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File);
|
---|
7562 |
|
---|
7563 | if (RTPathFilename(aLocation.c_str()) == NULL)
|
---|
7564 | {
|
---|
7565 | /* no file name is given (either an empty string or ends with a
|
---|
7566 | * slash), generate a new UUID + file name if the state allows
|
---|
7567 | * this */
|
---|
7568 |
|
---|
7569 | ComAssertMsgRet(!m->formatObj->i_getFileExtensions().empty(),
|
---|
7570 | ("Must be at least one extension if it is MediumFormatCapabilities_File\n"),
|
---|
7571 | E_FAIL);
|
---|
7572 |
|
---|
7573 | Utf8Str strExt = m->formatObj->i_getFileExtensions().front();
|
---|
7574 | ComAssertMsgRet(!strExt.isEmpty(),
|
---|
7575 | ("Default extension must not be empty\n"),
|
---|
7576 | E_FAIL);
|
---|
7577 |
|
---|
7578 | id.create();
|
---|
7579 |
|
---|
7580 | locationFull = Utf8StrFmt("%s{%RTuuid}.%s",
|
---|
7581 | aLocation.c_str(), id.raw(), strExt.c_str());
|
---|
7582 | }
|
---|
7583 | }
|
---|
7584 |
|
---|
7585 | // we must always have full paths now (if it refers to a file)
|
---|
7586 | if ( ( m->formatObj.isNull()
|
---|
7587 | || m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
7588 | && !RTPathStartsWithRoot(locationFull.c_str()))
|
---|
7589 | return setError(VBOX_E_FILE_ERROR,
|
---|
7590 | tr("The given path '%s' is not fully qualified"),
|
---|
7591 | locationFull.c_str());
|
---|
7592 |
|
---|
7593 | /* detect the backend from the storage unit if importing */
|
---|
7594 | if (isImport)
|
---|
7595 | {
|
---|
7596 | VDTYPE enmType = VDTYPE_INVALID;
|
---|
7597 | char *backendName = NULL;
|
---|
7598 |
|
---|
7599 | int vrc = VINF_SUCCESS;
|
---|
7600 |
|
---|
7601 | /* is it a file? */
|
---|
7602 | {
|
---|
7603 | RTFILE file;
|
---|
7604 | vrc = RTFileOpen(&file, locationFull.c_str(), RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
7605 | if (RT_SUCCESS(vrc))
|
---|
7606 | RTFileClose(file);
|
---|
7607 | }
|
---|
7608 | if (RT_SUCCESS(vrc))
|
---|
7609 | {
|
---|
7610 | vrc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
|
---|
7611 | locationFull.c_str(), &backendName, &enmType);
|
---|
7612 | }
|
---|
7613 | else if ( vrc != VERR_FILE_NOT_FOUND
|
---|
7614 | && vrc != VERR_PATH_NOT_FOUND
|
---|
7615 | && vrc != VERR_ACCESS_DENIED
|
---|
7616 | && locationFull != aLocation)
|
---|
7617 | {
|
---|
7618 | /* assume it's not a file, restore the original location */
|
---|
7619 | locationFull = aLocation;
|
---|
7620 | vrc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
|
---|
7621 | locationFull.c_str(), &backendName, &enmType);
|
---|
7622 | }
|
---|
7623 |
|
---|
7624 | if (RT_FAILURE(vrc))
|
---|
7625 | {
|
---|
7626 | if (vrc == VERR_ACCESS_DENIED)
|
---|
7627 | return setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
7628 | tr("Permission problem accessing the file for the medium '%s' (%Rrc)"),
|
---|
7629 | locationFull.c_str(), vrc);
|
---|
7630 | else if (vrc == VERR_FILE_NOT_FOUND || vrc == VERR_PATH_NOT_FOUND)
|
---|
7631 | return setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
7632 | tr("Could not find file for the medium '%s' (%Rrc)"),
|
---|
7633 | locationFull.c_str(), vrc);
|
---|
7634 | else if (aFormat.isEmpty())
|
---|
7635 | return setErrorBoth(VBOX_E_IPRT_ERROR, vrc,
|
---|
7636 | tr("Could not get the storage format of the medium '%s' (%Rrc)"),
|
---|
7637 | locationFull.c_str(), vrc);
|
---|
7638 | else
|
---|
7639 | {
|
---|
7640 | HRESULT rc = i_setFormat(aFormat);
|
---|
7641 | /* setFormat() must not fail since we've just used the backend so
|
---|
7642 | * the format object must be there */
|
---|
7643 | AssertComRCReturnRC(rc);
|
---|
7644 | }
|
---|
7645 | }
|
---|
7646 | else if ( enmType == VDTYPE_INVALID
|
---|
7647 | || m->devType != i_convertToDeviceType(enmType))
|
---|
7648 | {
|
---|
7649 | /*
|
---|
7650 | * The user tried to use a image as a device which is not supported
|
---|
7651 | * by the backend.
|
---|
7652 | */
|
---|
7653 | return setError(E_FAIL,
|
---|
7654 | tr("The medium '%s' can't be used as the requested device type"),
|
---|
7655 | locationFull.c_str());
|
---|
7656 | }
|
---|
7657 | else
|
---|
7658 | {
|
---|
7659 | ComAssertRet(backendName != NULL && *backendName != '\0', E_FAIL);
|
---|
7660 |
|
---|
7661 | HRESULT rc = i_setFormat(backendName);
|
---|
7662 | RTStrFree(backendName);
|
---|
7663 |
|
---|
7664 | /* setFormat() must not fail since we've just used the backend so
|
---|
7665 | * the format object must be there */
|
---|
7666 | AssertComRCReturnRC(rc);
|
---|
7667 | }
|
---|
7668 | }
|
---|
7669 |
|
---|
7670 | m->strLocationFull = locationFull;
|
---|
7671 |
|
---|
7672 | /* is it still a file? */
|
---|
7673 | if ( (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
7674 | && (m->state == MediumState_NotCreated)
|
---|
7675 | )
|
---|
7676 | /* assign a new UUID (this UUID will be used when calling
|
---|
7677 | * VDCreateBase/VDCreateDiff as a wanted UUID). Note that we
|
---|
7678 | * also do that if we didn't generate it to make sure it is
|
---|
7679 | * either generated by us or reset to null */
|
---|
7680 | unconst(m->id) = id;
|
---|
7681 | }
|
---|
7682 | else
|
---|
7683 | m->strLocationFull = aLocation;
|
---|
7684 |
|
---|
7685 | return S_OK;
|
---|
7686 | }
|
---|
7687 |
|
---|
7688 | /**
|
---|
7689 | * Checks that the format ID is valid and sets it on success.
|
---|
7690 | *
|
---|
7691 | * Note that this method will caller-reference the format object on success!
|
---|
7692 | * This reference must be released somewhere to let the MediumFormat object be
|
---|
7693 | * uninitialized.
|
---|
7694 | *
|
---|
7695 | * @note Must be called from under this object's write lock.
|
---|
7696 | */
|
---|
7697 | HRESULT Medium::i_setFormat(const Utf8Str &aFormat)
|
---|
7698 | {
|
---|
7699 | /* get the format object first */
|
---|
7700 | {
|
---|
7701 | SystemProperties *pSysProps = m->pVirtualBox->i_getSystemProperties();
|
---|
7702 | AutoReadLock propsLock(pSysProps COMMA_LOCKVAL_SRC_POS);
|
---|
7703 |
|
---|
7704 | unconst(m->formatObj) = pSysProps->i_mediumFormat(aFormat);
|
---|
7705 | if (m->formatObj.isNull())
|
---|
7706 | return setError(E_INVALIDARG,
|
---|
7707 | tr("Invalid medium storage format '%s'"),
|
---|
7708 | aFormat.c_str());
|
---|
7709 |
|
---|
7710 | /* get properties (preinsert them as keys in the map). Note that the
|
---|
7711 | * map doesn't grow over the object life time since the set of
|
---|
7712 | * properties is meant to be constant. */
|
---|
7713 |
|
---|
7714 | Assert(m->mapProperties.empty());
|
---|
7715 |
|
---|
7716 | for (MediumFormat::PropertyArray::const_iterator it = m->formatObj->i_getProperties().begin();
|
---|
7717 | it != m->formatObj->i_getProperties().end();
|
---|
7718 | ++it)
|
---|
7719 | {
|
---|
7720 | m->mapProperties.insert(std::make_pair(it->strName, Utf8Str::Empty));
|
---|
7721 | }
|
---|
7722 | }
|
---|
7723 |
|
---|
7724 | unconst(m->strFormat) = aFormat;
|
---|
7725 |
|
---|
7726 | return S_OK;
|
---|
7727 | }
|
---|
7728 |
|
---|
7729 | /**
|
---|
7730 | * Converts the Medium device type to the VD type.
|
---|
7731 | */
|
---|
7732 | VDTYPE Medium::i_convertDeviceType()
|
---|
7733 | {
|
---|
7734 | VDTYPE enmType;
|
---|
7735 |
|
---|
7736 | switch (m->devType)
|
---|
7737 | {
|
---|
7738 | case DeviceType_HardDisk:
|
---|
7739 | enmType = VDTYPE_HDD;
|
---|
7740 | break;
|
---|
7741 | case DeviceType_DVD:
|
---|
7742 | enmType = VDTYPE_OPTICAL_DISC;
|
---|
7743 | break;
|
---|
7744 | case DeviceType_Floppy:
|
---|
7745 | enmType = VDTYPE_FLOPPY;
|
---|
7746 | break;
|
---|
7747 | default:
|
---|
7748 | ComAssertFailedRet(VDTYPE_INVALID);
|
---|
7749 | }
|
---|
7750 |
|
---|
7751 | return enmType;
|
---|
7752 | }
|
---|
7753 |
|
---|
7754 | /**
|
---|
7755 | * Converts from the VD type to the medium type.
|
---|
7756 | */
|
---|
7757 | DeviceType_T Medium::i_convertToDeviceType(VDTYPE enmType)
|
---|
7758 | {
|
---|
7759 | DeviceType_T devType;
|
---|
7760 |
|
---|
7761 | switch (enmType)
|
---|
7762 | {
|
---|
7763 | case VDTYPE_HDD:
|
---|
7764 | devType = DeviceType_HardDisk;
|
---|
7765 | break;
|
---|
7766 | case VDTYPE_OPTICAL_DISC:
|
---|
7767 | devType = DeviceType_DVD;
|
---|
7768 | break;
|
---|
7769 | case VDTYPE_FLOPPY:
|
---|
7770 | devType = DeviceType_Floppy;
|
---|
7771 | break;
|
---|
7772 | default:
|
---|
7773 | ComAssertFailedRet(DeviceType_Null);
|
---|
7774 | }
|
---|
7775 |
|
---|
7776 | return devType;
|
---|
7777 | }
|
---|
7778 |
|
---|
7779 | /**
|
---|
7780 | * Internal method which checks whether a property name is for a filter plugin.
|
---|
7781 | */
|
---|
7782 | bool Medium::i_isPropertyForFilter(const com::Utf8Str &aName)
|
---|
7783 | {
|
---|
7784 | /* If the name contains "/" use the part before as a filter name and lookup the filter. */
|
---|
7785 | size_t offSlash;
|
---|
7786 | if ((offSlash = aName.find("/", 0)) != aName.npos)
|
---|
7787 | {
|
---|
7788 | com::Utf8Str strFilter;
|
---|
7789 | com::Utf8Str strKey;
|
---|
7790 |
|
---|
7791 | HRESULT rc = strFilter.assignEx(aName, 0, offSlash);
|
---|
7792 | if (FAILED(rc))
|
---|
7793 | return false;
|
---|
7794 |
|
---|
7795 | rc = strKey.assignEx(aName, offSlash + 1, aName.length() - offSlash - 1); /* Skip slash */
|
---|
7796 | if (FAILED(rc))
|
---|
7797 | return false;
|
---|
7798 |
|
---|
7799 | VDFILTERINFO FilterInfo;
|
---|
7800 | int vrc = VDFilterInfoOne(strFilter.c_str(), &FilterInfo);
|
---|
7801 | if (RT_SUCCESS(vrc))
|
---|
7802 | {
|
---|
7803 | /* Check that the property exists. */
|
---|
7804 | PCVDCONFIGINFO paConfig = FilterInfo.paConfigInfo;
|
---|
7805 | while (paConfig->pszKey)
|
---|
7806 | {
|
---|
7807 | if (strKey.equals(paConfig->pszKey))
|
---|
7808 | return true;
|
---|
7809 | paConfig++;
|
---|
7810 | }
|
---|
7811 | }
|
---|
7812 | }
|
---|
7813 |
|
---|
7814 | return false;
|
---|
7815 | }
|
---|
7816 |
|
---|
7817 | /**
|
---|
7818 | * Returns the last error message collected by the i_vdErrorCall callback and
|
---|
7819 | * resets it.
|
---|
7820 | *
|
---|
7821 | * The error message is returned prepended with a dot and a space, like this:
|
---|
7822 | * <code>
|
---|
7823 | * ". <error_text> (%Rrc)"
|
---|
7824 | * </code>
|
---|
7825 | * to make it easily appendable to a more general error message. The @c %Rrc
|
---|
7826 | * format string is given @a aVRC as an argument.
|
---|
7827 | *
|
---|
7828 | * If there is no last error message collected by i_vdErrorCall or if it is a
|
---|
7829 | * null or empty string, then this function returns the following text:
|
---|
7830 | * <code>
|
---|
7831 | * " (%Rrc)"
|
---|
7832 | * </code>
|
---|
7833 | *
|
---|
7834 | * @note Doesn't do any object locking; it is assumed that the caller makes sure
|
---|
7835 | * the callback isn't called by more than one thread at a time.
|
---|
7836 | *
|
---|
7837 | * @param aVRC VBox error code to use when no error message is provided.
|
---|
7838 | */
|
---|
7839 | Utf8Str Medium::i_vdError(int aVRC)
|
---|
7840 | {
|
---|
7841 | Utf8Str error;
|
---|
7842 |
|
---|
7843 | if (m->vdError.isEmpty())
|
---|
7844 | error = Utf8StrFmt(" (%Rrc)", aVRC);
|
---|
7845 | else
|
---|
7846 | error = Utf8StrFmt(".\n%s", m->vdError.c_str());
|
---|
7847 |
|
---|
7848 | m->vdError.setNull();
|
---|
7849 |
|
---|
7850 | return error;
|
---|
7851 | }
|
---|
7852 |
|
---|
7853 | /**
|
---|
7854 | * Error message callback.
|
---|
7855 | *
|
---|
7856 | * Puts the reported error message to the m->vdError field.
|
---|
7857 | *
|
---|
7858 | * @note Doesn't do any object locking; it is assumed that the caller makes sure
|
---|
7859 | * the callback isn't called by more than one thread at a time.
|
---|
7860 | *
|
---|
7861 | * @param pvUser The opaque data passed on container creation.
|
---|
7862 | * @param rc The VBox error code.
|
---|
7863 | * @param SRC_POS Use RT_SRC_POS.
|
---|
7864 | * @param pszFormat Error message format string.
|
---|
7865 | * @param va Error message arguments.
|
---|
7866 | */
|
---|
7867 | /*static*/
|
---|
7868 | DECLCALLBACK(void) Medium::i_vdErrorCall(void *pvUser, int rc, RT_SRC_POS_DECL,
|
---|
7869 | const char *pszFormat, va_list va)
|
---|
7870 | {
|
---|
7871 | NOREF(pszFile); NOREF(iLine); NOREF(pszFunction); /* RT_SRC_POS_DECL */
|
---|
7872 |
|
---|
7873 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
7874 | AssertReturnVoid(that != NULL);
|
---|
7875 |
|
---|
7876 | if (that->m->vdError.isEmpty())
|
---|
7877 | that->m->vdError =
|
---|
7878 | Utf8StrFmt("%s (%Rrc)", Utf8Str(pszFormat, va).c_str(), rc);
|
---|
7879 | else
|
---|
7880 | that->m->vdError =
|
---|
7881 | Utf8StrFmt("%s.\n%s (%Rrc)", that->m->vdError.c_str(),
|
---|
7882 | Utf8Str(pszFormat, va).c_str(), rc);
|
---|
7883 | }
|
---|
7884 |
|
---|
7885 | /* static */
|
---|
7886 | DECLCALLBACK(bool) Medium::i_vdConfigAreKeysValid(void *pvUser,
|
---|
7887 | const char * /* pszzValid */)
|
---|
7888 | {
|
---|
7889 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
7890 | AssertReturn(that != NULL, false);
|
---|
7891 |
|
---|
7892 | /* we always return true since the only keys we have are those found in
|
---|
7893 | * VDBACKENDINFO */
|
---|
7894 | return true;
|
---|
7895 | }
|
---|
7896 |
|
---|
7897 | /* static */
|
---|
7898 | DECLCALLBACK(int) Medium::i_vdConfigQuerySize(void *pvUser,
|
---|
7899 | const char *pszName,
|
---|
7900 | size_t *pcbValue)
|
---|
7901 | {
|
---|
7902 | AssertReturn(VALID_PTR(pcbValue), VERR_INVALID_POINTER);
|
---|
7903 |
|
---|
7904 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
7905 | AssertReturn(that != NULL, VERR_GENERAL_FAILURE);
|
---|
7906 |
|
---|
7907 | settings::StringsMap::const_iterator it = that->m->mapProperties.find(Utf8Str(pszName));
|
---|
7908 | if (it == that->m->mapProperties.end())
|
---|
7909 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7910 |
|
---|
7911 | /* we interpret null values as "no value" in Medium */
|
---|
7912 | if (it->second.isEmpty())
|
---|
7913 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7914 |
|
---|
7915 | *pcbValue = it->second.length() + 1 /* include terminator */;
|
---|
7916 |
|
---|
7917 | return VINF_SUCCESS;
|
---|
7918 | }
|
---|
7919 |
|
---|
7920 | /* static */
|
---|
7921 | DECLCALLBACK(int) Medium::i_vdConfigQuery(void *pvUser,
|
---|
7922 | const char *pszName,
|
---|
7923 | char *pszValue,
|
---|
7924 | size_t cchValue)
|
---|
7925 | {
|
---|
7926 | AssertReturn(VALID_PTR(pszValue), VERR_INVALID_POINTER);
|
---|
7927 |
|
---|
7928 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
7929 | AssertReturn(that != NULL, VERR_GENERAL_FAILURE);
|
---|
7930 |
|
---|
7931 | settings::StringsMap::const_iterator it = that->m->mapProperties.find(Utf8Str(pszName));
|
---|
7932 | if (it == that->m->mapProperties.end())
|
---|
7933 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7934 |
|
---|
7935 | /* we interpret null values as "no value" in Medium */
|
---|
7936 | if (it->second.isEmpty())
|
---|
7937 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7938 |
|
---|
7939 | const Utf8Str &value = it->second;
|
---|
7940 | if (value.length() >= cchValue)
|
---|
7941 | return VERR_CFGM_NOT_ENOUGH_SPACE;
|
---|
7942 |
|
---|
7943 | memcpy(pszValue, value.c_str(), value.length() + 1);
|
---|
7944 |
|
---|
7945 | return VINF_SUCCESS;
|
---|
7946 | }
|
---|
7947 |
|
---|
7948 | DECLCALLBACK(int) Medium::i_vdTcpSocketCreate(uint32_t fFlags, PVDSOCKET pSock)
|
---|
7949 | {
|
---|
7950 | PVDSOCKETINT pSocketInt = NULL;
|
---|
7951 |
|
---|
7952 | if ((fFlags & VD_INTERFACETCPNET_CONNECT_EXTENDED_SELECT) != 0)
|
---|
7953 | return VERR_NOT_SUPPORTED;
|
---|
7954 |
|
---|
7955 | pSocketInt = (PVDSOCKETINT)RTMemAllocZ(sizeof(VDSOCKETINT));
|
---|
7956 | if (!pSocketInt)
|
---|
7957 | return VERR_NO_MEMORY;
|
---|
7958 |
|
---|
7959 | pSocketInt->hSocket = NIL_RTSOCKET;
|
---|
7960 | *pSock = pSocketInt;
|
---|
7961 | return VINF_SUCCESS;
|
---|
7962 | }
|
---|
7963 |
|
---|
7964 | DECLCALLBACK(int) Medium::i_vdTcpSocketDestroy(VDSOCKET Sock)
|
---|
7965 | {
|
---|
7966 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7967 |
|
---|
7968 | if (pSocketInt->hSocket != NIL_RTSOCKET)
|
---|
7969 | RTTcpClientCloseEx(pSocketInt->hSocket, false /*fGracefulShutdown*/);
|
---|
7970 |
|
---|
7971 | RTMemFree(pSocketInt);
|
---|
7972 |
|
---|
7973 | return VINF_SUCCESS;
|
---|
7974 | }
|
---|
7975 |
|
---|
7976 | DECLCALLBACK(int) Medium::i_vdTcpClientConnect(VDSOCKET Sock, const char *pszAddress, uint32_t uPort,
|
---|
7977 | RTMSINTERVAL cMillies)
|
---|
7978 | {
|
---|
7979 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7980 |
|
---|
7981 | return RTTcpClientConnectEx(pszAddress, uPort, &pSocketInt->hSocket, cMillies, NULL);
|
---|
7982 | }
|
---|
7983 |
|
---|
7984 | DECLCALLBACK(int) Medium::i_vdTcpClientClose(VDSOCKET Sock)
|
---|
7985 | {
|
---|
7986 | int rc = VINF_SUCCESS;
|
---|
7987 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7988 |
|
---|
7989 | rc = RTTcpClientCloseEx(pSocketInt->hSocket, false /*fGracefulShutdown*/);
|
---|
7990 | pSocketInt->hSocket = NIL_RTSOCKET;
|
---|
7991 | return rc;
|
---|
7992 | }
|
---|
7993 |
|
---|
7994 | DECLCALLBACK(bool) Medium::i_vdTcpIsClientConnected(VDSOCKET Sock)
|
---|
7995 | {
|
---|
7996 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7997 | return pSocketInt->hSocket != NIL_RTSOCKET;
|
---|
7998 | }
|
---|
7999 |
|
---|
8000 | DECLCALLBACK(int) Medium::i_vdTcpSelectOne(VDSOCKET Sock, RTMSINTERVAL cMillies)
|
---|
8001 | {
|
---|
8002 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
8003 | return RTTcpSelectOne(pSocketInt->hSocket, cMillies);
|
---|
8004 | }
|
---|
8005 |
|
---|
8006 | DECLCALLBACK(int) Medium::i_vdTcpRead(VDSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead)
|
---|
8007 | {
|
---|
8008 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
8009 | return RTTcpRead(pSocketInt->hSocket, pvBuffer, cbBuffer, pcbRead);
|
---|
8010 | }
|
---|
8011 |
|
---|
8012 | DECLCALLBACK(int) Medium::i_vdTcpWrite(VDSOCKET Sock, const void *pvBuffer, size_t cbBuffer)
|
---|
8013 | {
|
---|
8014 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
8015 | return RTTcpWrite(pSocketInt->hSocket, pvBuffer, cbBuffer);
|
---|
8016 | }
|
---|
8017 |
|
---|
8018 | DECLCALLBACK(int) Medium::i_vdTcpSgWrite(VDSOCKET Sock, PCRTSGBUF pSgBuf)
|
---|
8019 | {
|
---|
8020 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
8021 | return RTTcpSgWrite(pSocketInt->hSocket, pSgBuf);
|
---|
8022 | }
|
---|
8023 |
|
---|
8024 | DECLCALLBACK(int) Medium::i_vdTcpFlush(VDSOCKET Sock)
|
---|
8025 | {
|
---|
8026 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
8027 | return RTTcpFlush(pSocketInt->hSocket);
|
---|
8028 | }
|
---|
8029 |
|
---|
8030 | DECLCALLBACK(int) Medium::i_vdTcpSetSendCoalescing(VDSOCKET Sock, bool fEnable)
|
---|
8031 | {
|
---|
8032 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
8033 | return RTTcpSetSendCoalescing(pSocketInt->hSocket, fEnable);
|
---|
8034 | }
|
---|
8035 |
|
---|
8036 | DECLCALLBACK(int) Medium::i_vdTcpGetLocalAddress(VDSOCKET Sock, PRTNETADDR pAddr)
|
---|
8037 | {
|
---|
8038 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
8039 | return RTTcpGetLocalAddress(pSocketInt->hSocket, pAddr);
|
---|
8040 | }
|
---|
8041 |
|
---|
8042 | DECLCALLBACK(int) Medium::i_vdTcpGetPeerAddress(VDSOCKET Sock, PRTNETADDR pAddr)
|
---|
8043 | {
|
---|
8044 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
8045 | return RTTcpGetPeerAddress(pSocketInt->hSocket, pAddr);
|
---|
8046 | }
|
---|
8047 |
|
---|
8048 | DECLCALLBACK(bool) Medium::i_vdCryptoConfigAreKeysValid(void *pvUser, const char *pszzValid)
|
---|
8049 | {
|
---|
8050 | /* Just return always true here. */
|
---|
8051 | NOREF(pvUser);
|
---|
8052 | NOREF(pszzValid);
|
---|
8053 | return true;
|
---|
8054 | }
|
---|
8055 |
|
---|
8056 | DECLCALLBACK(int) Medium::i_vdCryptoConfigQuerySize(void *pvUser, const char *pszName, size_t *pcbValue)
|
---|
8057 | {
|
---|
8058 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8059 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8060 | AssertReturn(VALID_PTR(pcbValue), VERR_INVALID_POINTER);
|
---|
8061 |
|
---|
8062 | size_t cbValue = 0;
|
---|
8063 | if (!strcmp(pszName, "Algorithm"))
|
---|
8064 | cbValue = strlen(pSettings->pszCipher) + 1;
|
---|
8065 | else if (!strcmp(pszName, "KeyId"))
|
---|
8066 | cbValue = sizeof("irrelevant");
|
---|
8067 | else if (!strcmp(pszName, "KeyStore"))
|
---|
8068 | {
|
---|
8069 | if (!pSettings->pszKeyStoreLoad)
|
---|
8070 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
8071 | cbValue = strlen(pSettings->pszKeyStoreLoad) + 1;
|
---|
8072 | }
|
---|
8073 | else if (!strcmp(pszName, "CreateKeyStore"))
|
---|
8074 | cbValue = 2; /* Single digit + terminator. */
|
---|
8075 | else
|
---|
8076 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
8077 |
|
---|
8078 | *pcbValue = cbValue + 1 /* include terminator */;
|
---|
8079 |
|
---|
8080 | return VINF_SUCCESS;
|
---|
8081 | }
|
---|
8082 |
|
---|
8083 | DECLCALLBACK(int) Medium::i_vdCryptoConfigQuery(void *pvUser, const char *pszName,
|
---|
8084 | char *pszValue, size_t cchValue)
|
---|
8085 | {
|
---|
8086 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8087 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8088 | AssertReturn(VALID_PTR(pszValue), VERR_INVALID_POINTER);
|
---|
8089 |
|
---|
8090 | const char *psz = NULL;
|
---|
8091 | if (!strcmp(pszName, "Algorithm"))
|
---|
8092 | psz = pSettings->pszCipher;
|
---|
8093 | else if (!strcmp(pszName, "KeyId"))
|
---|
8094 | psz = "irrelevant";
|
---|
8095 | else if (!strcmp(pszName, "KeyStore"))
|
---|
8096 | psz = pSettings->pszKeyStoreLoad;
|
---|
8097 | else if (!strcmp(pszName, "CreateKeyStore"))
|
---|
8098 | {
|
---|
8099 | if (pSettings->fCreateKeyStore)
|
---|
8100 | psz = "1";
|
---|
8101 | else
|
---|
8102 | psz = "0";
|
---|
8103 | }
|
---|
8104 | else
|
---|
8105 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
8106 |
|
---|
8107 | size_t cch = strlen(psz);
|
---|
8108 | if (cch >= cchValue)
|
---|
8109 | return VERR_CFGM_NOT_ENOUGH_SPACE;
|
---|
8110 |
|
---|
8111 | memcpy(pszValue, psz, cch + 1);
|
---|
8112 | return VINF_SUCCESS;
|
---|
8113 | }
|
---|
8114 |
|
---|
8115 | DECLCALLBACK(int) Medium::i_vdCryptoKeyRetain(void *pvUser, const char *pszId,
|
---|
8116 | const uint8_t **ppbKey, size_t *pcbKey)
|
---|
8117 | {
|
---|
8118 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8119 | NOREF(pszId);
|
---|
8120 | NOREF(ppbKey);
|
---|
8121 | NOREF(pcbKey);
|
---|
8122 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8123 | AssertMsgFailedReturn(("This method should not be called here!\n"), VERR_INVALID_STATE);
|
---|
8124 | }
|
---|
8125 |
|
---|
8126 | DECLCALLBACK(int) Medium::i_vdCryptoKeyRelease(void *pvUser, const char *pszId)
|
---|
8127 | {
|
---|
8128 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8129 | NOREF(pszId);
|
---|
8130 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8131 | AssertMsgFailedReturn(("This method should not be called here!\n"), VERR_INVALID_STATE);
|
---|
8132 | }
|
---|
8133 |
|
---|
8134 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStorePasswordRetain(void *pvUser, const char *pszId, const char **ppszPassword)
|
---|
8135 | {
|
---|
8136 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8137 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8138 |
|
---|
8139 | NOREF(pszId);
|
---|
8140 | *ppszPassword = pSettings->pszPassword;
|
---|
8141 | return VINF_SUCCESS;
|
---|
8142 | }
|
---|
8143 |
|
---|
8144 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStorePasswordRelease(void *pvUser, const char *pszId)
|
---|
8145 | {
|
---|
8146 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8147 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8148 | NOREF(pszId);
|
---|
8149 | return VINF_SUCCESS;
|
---|
8150 | }
|
---|
8151 |
|
---|
8152 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStoreSave(void *pvUser, const void *pvKeyStore, size_t cbKeyStore)
|
---|
8153 | {
|
---|
8154 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8155 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8156 |
|
---|
8157 | pSettings->pszKeyStore = (char *)RTMemAllocZ(cbKeyStore);
|
---|
8158 | if (!pSettings->pszKeyStore)
|
---|
8159 | return VERR_NO_MEMORY;
|
---|
8160 |
|
---|
8161 | memcpy(pSettings->pszKeyStore, pvKeyStore, cbKeyStore);
|
---|
8162 | return VINF_SUCCESS;
|
---|
8163 | }
|
---|
8164 |
|
---|
8165 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStoreReturnParameters(void *pvUser, const char *pszCipher,
|
---|
8166 | const uint8_t *pbDek, size_t cbDek)
|
---|
8167 | {
|
---|
8168 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8169 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8170 |
|
---|
8171 | pSettings->pszCipherReturned = RTStrDup(pszCipher);
|
---|
8172 | pSettings->pbDek = pbDek;
|
---|
8173 | pSettings->cbDek = cbDek;
|
---|
8174 |
|
---|
8175 | return pSettings->pszCipherReturned ? VINF_SUCCESS : VERR_NO_MEMORY;
|
---|
8176 | }
|
---|
8177 |
|
---|
8178 | /**
|
---|
8179 | * Creates a VDISK instance for this medium.
|
---|
8180 | *
|
---|
8181 | * @note Caller should not hold any medium related locks as this method will
|
---|
8182 | * acquire the medium lock for writing and others (VirtualBox).
|
---|
8183 | *
|
---|
8184 | * @returns COM status code.
|
---|
8185 | * @param fWritable Whether to return a writable VDISK instance
|
---|
8186 | * (true) or a read-only one (false).
|
---|
8187 | * @param pKeyStore The key store.
|
---|
8188 | * @param ppHdd Where to return the pointer to the VDISK on
|
---|
8189 | * success.
|
---|
8190 | * @param pMediumLockList The lock list to populate and lock. Caller
|
---|
8191 | * is responsible for calling the destructor or
|
---|
8192 | * MediumLockList::Clear() after destroying
|
---|
8193 | * @a *ppHdd
|
---|
8194 | * @param pCryptoSettings The crypto settings to use for setting up
|
---|
8195 | * decryption/encryption of the VDISK. This object
|
---|
8196 | * must be alive until the VDISK is destroyed!
|
---|
8197 | */
|
---|
8198 | HRESULT Medium::i_openForIO(bool fWritable, SecretKeyStore *pKeyStore, PVDISK *ppHdd, MediumLockList *pMediumLockList,
|
---|
8199 | MediumCryptoFilterSettings *pCryptoSettings)
|
---|
8200 | {
|
---|
8201 | /*
|
---|
8202 | * Create the media lock list and lock the media.
|
---|
8203 | */
|
---|
8204 | HRESULT hrc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
8205 | fWritable ? this : NULL /* pToLockWrite */,
|
---|
8206 | false /* fMediumLockWriteAll */,
|
---|
8207 | NULL,
|
---|
8208 | *pMediumLockList);
|
---|
8209 | if (SUCCEEDED(hrc))
|
---|
8210 | hrc = pMediumLockList->Lock();
|
---|
8211 | if (FAILED(hrc))
|
---|
8212 | return hrc;
|
---|
8213 |
|
---|
8214 | /*
|
---|
8215 | * Get the base medium before write locking this medium.
|
---|
8216 | */
|
---|
8217 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
8218 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8219 |
|
---|
8220 | /*
|
---|
8221 | * Create the VDISK instance.
|
---|
8222 | */
|
---|
8223 | PVDISK pHdd;
|
---|
8224 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pHdd);
|
---|
8225 | AssertRCReturn(vrc, E_FAIL);
|
---|
8226 |
|
---|
8227 | /*
|
---|
8228 | * Goto avoidance using try/catch/throw(HRESULT).
|
---|
8229 | */
|
---|
8230 | try
|
---|
8231 | {
|
---|
8232 | settings::StringsMap::iterator itKeyStore = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
8233 | if (itKeyStore != pBase->m->mapProperties.end())
|
---|
8234 | {
|
---|
8235 | settings::StringsMap::iterator itKeyId = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
8236 |
|
---|
8237 | #ifdef VBOX_WITH_EXTPACK
|
---|
8238 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
8239 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
8240 | {
|
---|
8241 | /* Load the plugin */
|
---|
8242 | Utf8Str strPlugin;
|
---|
8243 | hrc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
8244 | if (SUCCEEDED(hrc))
|
---|
8245 | {
|
---|
8246 | vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
8247 | if (RT_FAILURE(vrc))
|
---|
8248 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
8249 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
8250 | i_vdError(vrc).c_str());
|
---|
8251 | }
|
---|
8252 | else
|
---|
8253 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
8254 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
8255 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
8256 | }
|
---|
8257 | else
|
---|
8258 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
8259 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
8260 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
8261 | #else
|
---|
8262 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
8263 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
8264 | #endif
|
---|
8265 |
|
---|
8266 | if (itKeyId == pBase->m->mapProperties.end())
|
---|
8267 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8268 | tr("Image '%s' is configured for encryption but doesn't has a key identifier set"),
|
---|
8269 | pBase->m->strLocationFull.c_str());
|
---|
8270 |
|
---|
8271 | /* Find the proper secret key in the key store. */
|
---|
8272 | if (!pKeyStore)
|
---|
8273 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8274 | tr("Image '%s' is configured for encryption but there is no key store to retrieve the password from"),
|
---|
8275 | pBase->m->strLocationFull.c_str());
|
---|
8276 |
|
---|
8277 | SecretKey *pKey = NULL;
|
---|
8278 | vrc = pKeyStore->retainSecretKey(itKeyId->second, &pKey);
|
---|
8279 | if (RT_FAILURE(vrc))
|
---|
8280 | throw setErrorBoth(VBOX_E_INVALID_OBJECT_STATE, vrc,
|
---|
8281 | tr("Failed to retrieve the secret key with ID \"%s\" from the store (%Rrc)"),
|
---|
8282 | itKeyId->second.c_str(), vrc);
|
---|
8283 |
|
---|
8284 | i_taskEncryptSettingsSetup(pCryptoSettings, NULL, itKeyStore->second.c_str(), (const char *)pKey->getKeyBuffer(),
|
---|
8285 | false /* fCreateKeyStore */);
|
---|
8286 | vrc = VDFilterAdd(pHdd, "CRYPT", VD_FILTER_FLAGS_DEFAULT, pCryptoSettings->vdFilterIfaces);
|
---|
8287 | pKeyStore->releaseSecretKey(itKeyId->second);
|
---|
8288 | if (vrc == VERR_VD_PASSWORD_INCORRECT)
|
---|
8289 | throw setErrorBoth(VBOX_E_PASSWORD_INCORRECT, vrc, tr("The password to decrypt the image is incorrect"));
|
---|
8290 | if (RT_FAILURE(vrc))
|
---|
8291 | throw setErrorBoth(VBOX_E_INVALID_OBJECT_STATE, vrc, tr("Failed to load the decryption filter: %s"),
|
---|
8292 | i_vdError(vrc).c_str());
|
---|
8293 | }
|
---|
8294 |
|
---|
8295 | /*
|
---|
8296 | * Open all media in the source chain.
|
---|
8297 | */
|
---|
8298 | MediumLockList::Base::const_iterator sourceListBegin = pMediumLockList->GetBegin();
|
---|
8299 | MediumLockList::Base::const_iterator sourceListEnd = pMediumLockList->GetEnd();
|
---|
8300 | MediumLockList::Base::const_iterator mediumListLast = sourceListEnd;
|
---|
8301 | --mediumListLast;
|
---|
8302 |
|
---|
8303 | for (MediumLockList::Base::const_iterator it = sourceListBegin; it != sourceListEnd; ++it)
|
---|
8304 | {
|
---|
8305 | const MediumLock &mediumLock = *it;
|
---|
8306 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8307 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8308 |
|
---|
8309 | /* sanity check */
|
---|
8310 | Assert(pMedium->m->state == (fWritable && it == mediumListLast ? MediumState_LockedWrite : MediumState_LockedRead));
|
---|
8311 |
|
---|
8312 | /* Open all media in read-only mode. */
|
---|
8313 | vrc = VDOpen(pHdd,
|
---|
8314 | pMedium->m->strFormat.c_str(),
|
---|
8315 | pMedium->m->strLocationFull.c_str(),
|
---|
8316 | m->uOpenFlagsDef | (fWritable && it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
8317 | pMedium->m->vdImageIfaces);
|
---|
8318 | if (RT_FAILURE(vrc))
|
---|
8319 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
8320 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8321 | pMedium->m->strLocationFull.c_str(),
|
---|
8322 | i_vdError(vrc).c_str());
|
---|
8323 | }
|
---|
8324 |
|
---|
8325 | Assert(m->state == (fWritable ? MediumState_LockedWrite : MediumState_LockedRead));
|
---|
8326 |
|
---|
8327 | /*
|
---|
8328 | * Done!
|
---|
8329 | */
|
---|
8330 | *ppHdd = pHdd;
|
---|
8331 | return S_OK;
|
---|
8332 | }
|
---|
8333 | catch (HRESULT hrc2)
|
---|
8334 | {
|
---|
8335 | hrc = hrc2;
|
---|
8336 | }
|
---|
8337 |
|
---|
8338 | VDDestroy(pHdd);
|
---|
8339 | return hrc;
|
---|
8340 |
|
---|
8341 | }
|
---|
8342 |
|
---|
8343 | /**
|
---|
8344 | * Implementation code for the "create base" task.
|
---|
8345 | *
|
---|
8346 | * This only gets started from Medium::CreateBaseStorage() and always runs
|
---|
8347 | * asynchronously. As a result, we always save the VirtualBox.xml file when
|
---|
8348 | * we're done here.
|
---|
8349 | *
|
---|
8350 | * @param task
|
---|
8351 | * @return
|
---|
8352 | */
|
---|
8353 | HRESULT Medium::i_taskCreateBaseHandler(Medium::CreateBaseTask &task)
|
---|
8354 | {
|
---|
8355 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
8356 | * to lock order violations, it probably causes lock order issues related
|
---|
8357 | * to the AutoCaller usage. */
|
---|
8358 | HRESULT rc = S_OK;
|
---|
8359 |
|
---|
8360 | /* these parameters we need after creation */
|
---|
8361 | uint64_t size = 0, logicalSize = 0;
|
---|
8362 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
8363 | bool fGenerateUuid = false;
|
---|
8364 |
|
---|
8365 | try
|
---|
8366 | {
|
---|
8367 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8368 |
|
---|
8369 | /* The object may request a specific UUID (through a special form of
|
---|
8370 | * the moveTo() argument). Otherwise we have to generate it */
|
---|
8371 | Guid id = m->id;
|
---|
8372 |
|
---|
8373 | fGenerateUuid = id.isZero();
|
---|
8374 | if (fGenerateUuid)
|
---|
8375 | {
|
---|
8376 | id.create();
|
---|
8377 | /* VirtualBox::i_registerMedium() will need UUID */
|
---|
8378 | unconst(m->id) = id;
|
---|
8379 | }
|
---|
8380 |
|
---|
8381 | Utf8Str format(m->strFormat);
|
---|
8382 | Utf8Str location(m->strLocationFull);
|
---|
8383 | uint64_t capabilities = m->formatObj->i_getCapabilities();
|
---|
8384 | ComAssertThrow(capabilities & ( MediumFormatCapabilities_CreateFixed
|
---|
8385 | | MediumFormatCapabilities_CreateDynamic), E_FAIL);
|
---|
8386 | Assert(m->state == MediumState_Creating);
|
---|
8387 |
|
---|
8388 | PVDISK hdd;
|
---|
8389 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8390 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8391 |
|
---|
8392 | /* unlock before the potentially lengthy operation */
|
---|
8393 | thisLock.release();
|
---|
8394 |
|
---|
8395 | try
|
---|
8396 | {
|
---|
8397 | /* ensure the directory exists */
|
---|
8398 | if (capabilities & MediumFormatCapabilities_File)
|
---|
8399 | {
|
---|
8400 | rc = VirtualBox::i_ensureFilePathExists(location, !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
8401 | if (FAILED(rc))
|
---|
8402 | throw rc;
|
---|
8403 | }
|
---|
8404 |
|
---|
8405 | VDGEOMETRY geo = { 0, 0, 0 }; /* auto-detect */
|
---|
8406 |
|
---|
8407 | vrc = VDCreateBase(hdd,
|
---|
8408 | format.c_str(),
|
---|
8409 | location.c_str(),
|
---|
8410 | task.mSize,
|
---|
8411 | task.mVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted),
|
---|
8412 | NULL,
|
---|
8413 | &geo,
|
---|
8414 | &geo,
|
---|
8415 | id.raw(),
|
---|
8416 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
8417 | m->vdImageIfaces,
|
---|
8418 | task.mVDOperationIfaces);
|
---|
8419 | if (RT_FAILURE(vrc))
|
---|
8420 | {
|
---|
8421 | if (vrc == VERR_VD_INVALID_TYPE)
|
---|
8422 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
8423 | tr("Parameters for creating the medium storage unit '%s' are invalid%s"),
|
---|
8424 | location.c_str(), i_vdError(vrc).c_str());
|
---|
8425 | else
|
---|
8426 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
8427 | tr("Could not create the medium storage unit '%s'%s"),
|
---|
8428 | location.c_str(), i_vdError(vrc).c_str());
|
---|
8429 | }
|
---|
8430 |
|
---|
8431 | if (task.mVariant & MediumVariant_Formatted)
|
---|
8432 | {
|
---|
8433 | RTVFSFILE hVfsFile;
|
---|
8434 | vrc = VDCreateVfsFileFromDisk(hdd, 0 /*fFlags*/, &hVfsFile);
|
---|
8435 | if (RT_FAILURE(vrc))
|
---|
8436 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Opening medium storage unit '%s' failed%s"),
|
---|
8437 | location.c_str(), i_vdError(vrc).c_str());
|
---|
8438 | RTERRINFOSTATIC ErrInfo;
|
---|
8439 | vrc = RTFsFatVolFormat(hVfsFile, 0 /* offVol */, 0 /* cbVol */, RTFSFATVOL_FMT_F_FULL,
|
---|
8440 | 0 /* cbSector */, 0 /* cbSectorPerCluster */, RTFSFATTYPE_INVALID,
|
---|
8441 | 0 /* cHeads */, 0 /* cSectorsPerTrack*/, 0 /* bMedia */,
|
---|
8442 | 0 /* cRootDirEntries */, 0 /* cHiddenSectors */,
|
---|
8443 | RTErrInfoInitStatic(&ErrInfo));
|
---|
8444 | RTVfsFileRelease(hVfsFile);
|
---|
8445 | if (RT_FAILURE(vrc) && RTErrInfoIsSet(&ErrInfo.Core))
|
---|
8446 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Formatting medium storage unit '%s' failed: %s"),
|
---|
8447 | location.c_str(), ErrInfo.Core.pszMsg);
|
---|
8448 | if (RT_FAILURE(vrc))
|
---|
8449 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Formatting medium storage unit '%s' failed%s"),
|
---|
8450 | location.c_str(), i_vdError(vrc).c_str());
|
---|
8451 | }
|
---|
8452 |
|
---|
8453 | size = VDGetFileSize(hdd, 0);
|
---|
8454 | logicalSize = VDGetSize(hdd, 0);
|
---|
8455 | unsigned uImageFlags;
|
---|
8456 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
8457 | if (RT_SUCCESS(vrc))
|
---|
8458 | variant = (MediumVariant_T)uImageFlags;
|
---|
8459 | }
|
---|
8460 | catch (HRESULT aRC) { rc = aRC; }
|
---|
8461 |
|
---|
8462 | VDDestroy(hdd);
|
---|
8463 | }
|
---|
8464 | catch (HRESULT aRC) { rc = aRC; }
|
---|
8465 |
|
---|
8466 | if (SUCCEEDED(rc))
|
---|
8467 | {
|
---|
8468 | /* register with mVirtualBox as the last step and move to
|
---|
8469 | * Created state only on success (leaving an orphan file is
|
---|
8470 | * better than breaking media registry consistency) */
|
---|
8471 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
8472 | ComObjPtr<Medium> pMedium;
|
---|
8473 | rc = m->pVirtualBox->i_registerMedium(this, &pMedium, treeLock);
|
---|
8474 | Assert(pMedium == NULL || this == pMedium);
|
---|
8475 | }
|
---|
8476 |
|
---|
8477 | // re-acquire the lock before changing state
|
---|
8478 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8479 |
|
---|
8480 | if (SUCCEEDED(rc))
|
---|
8481 | {
|
---|
8482 | m->state = MediumState_Created;
|
---|
8483 |
|
---|
8484 | m->size = size;
|
---|
8485 | m->logicalSize = logicalSize;
|
---|
8486 | m->variant = variant;
|
---|
8487 |
|
---|
8488 | thisLock.release();
|
---|
8489 | i_markRegistriesModified();
|
---|
8490 | if (task.isAsync())
|
---|
8491 | {
|
---|
8492 | // in asynchronous mode, save settings now
|
---|
8493 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
8494 | }
|
---|
8495 | }
|
---|
8496 | else
|
---|
8497 | {
|
---|
8498 | /* back to NotCreated on failure */
|
---|
8499 | m->state = MediumState_NotCreated;
|
---|
8500 |
|
---|
8501 | /* reset UUID to prevent it from being reused next time */
|
---|
8502 | if (fGenerateUuid)
|
---|
8503 | unconst(m->id).clear();
|
---|
8504 | }
|
---|
8505 |
|
---|
8506 | return rc;
|
---|
8507 | }
|
---|
8508 |
|
---|
8509 | /**
|
---|
8510 | * Implementation code for the "create diff" task.
|
---|
8511 | *
|
---|
8512 | * This task always gets started from Medium::createDiffStorage() and can run
|
---|
8513 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
8514 | * that function. If we run synchronously, the caller expects the medium
|
---|
8515 | * registry modification to be set before returning; otherwise (in asynchronous
|
---|
8516 | * mode), we save the settings ourselves.
|
---|
8517 | *
|
---|
8518 | * @param task
|
---|
8519 | * @return
|
---|
8520 | */
|
---|
8521 | HRESULT Medium::i_taskCreateDiffHandler(Medium::CreateDiffTask &task)
|
---|
8522 | {
|
---|
8523 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
8524 | * to lock order violations, it probably causes lock order issues related
|
---|
8525 | * to the AutoCaller usage. */
|
---|
8526 | HRESULT rcTmp = S_OK;
|
---|
8527 |
|
---|
8528 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
8529 |
|
---|
8530 | uint64_t size = 0, logicalSize = 0;
|
---|
8531 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
8532 | bool fGenerateUuid = false;
|
---|
8533 |
|
---|
8534 | try
|
---|
8535 | {
|
---|
8536 | if (i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
8537 | {
|
---|
8538 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8539 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8540 | 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"),
|
---|
8541 | m->strLocationFull.c_str());
|
---|
8542 | }
|
---|
8543 |
|
---|
8544 | /* Lock both in {parent,child} order. */
|
---|
8545 | AutoMultiWriteLock2 mediaLock(this, pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8546 |
|
---|
8547 | /* The object may request a specific UUID (through a special form of
|
---|
8548 | * the moveTo() argument). Otherwise we have to generate it */
|
---|
8549 | Guid targetId = pTarget->m->id;
|
---|
8550 |
|
---|
8551 | fGenerateUuid = targetId.isZero();
|
---|
8552 | if (fGenerateUuid)
|
---|
8553 | {
|
---|
8554 | targetId.create();
|
---|
8555 | /* VirtualBox::i_registerMedium() will need UUID */
|
---|
8556 | unconst(pTarget->m->id) = targetId;
|
---|
8557 | }
|
---|
8558 |
|
---|
8559 | Guid id = m->id;
|
---|
8560 |
|
---|
8561 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
8562 | Utf8Str targetLocation(pTarget->m->strLocationFull);
|
---|
8563 | uint64_t capabilities = pTarget->m->formatObj->i_getCapabilities();
|
---|
8564 | ComAssertThrow(capabilities & MediumFormatCapabilities_CreateDynamic, E_FAIL);
|
---|
8565 |
|
---|
8566 | Assert(pTarget->m->state == MediumState_Creating);
|
---|
8567 | Assert(m->state == MediumState_LockedRead);
|
---|
8568 |
|
---|
8569 | PVDISK hdd;
|
---|
8570 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8571 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8572 |
|
---|
8573 | /* the two media are now protected by their non-default states;
|
---|
8574 | * unlock the media before the potentially lengthy operation */
|
---|
8575 | mediaLock.release();
|
---|
8576 |
|
---|
8577 | try
|
---|
8578 | {
|
---|
8579 | /* Open all media in the target chain but the last. */
|
---|
8580 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
8581 | task.mpMediumLockList->GetBegin();
|
---|
8582 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
8583 | task.mpMediumLockList->GetEnd();
|
---|
8584 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
8585 | it != targetListEnd;
|
---|
8586 | ++it)
|
---|
8587 | {
|
---|
8588 | const MediumLock &mediumLock = *it;
|
---|
8589 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8590 |
|
---|
8591 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8592 |
|
---|
8593 | /* Skip over the target diff medium */
|
---|
8594 | if (pMedium->m->state == MediumState_Creating)
|
---|
8595 | continue;
|
---|
8596 |
|
---|
8597 | /* sanity check */
|
---|
8598 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
8599 |
|
---|
8600 | /* Open all media in appropriate mode. */
|
---|
8601 | vrc = VDOpen(hdd,
|
---|
8602 | pMedium->m->strFormat.c_str(),
|
---|
8603 | pMedium->m->strLocationFull.c_str(),
|
---|
8604 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
8605 | pMedium->m->vdImageIfaces);
|
---|
8606 | if (RT_FAILURE(vrc))
|
---|
8607 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
8608 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8609 | pMedium->m->strLocationFull.c_str(),
|
---|
8610 | i_vdError(vrc).c_str());
|
---|
8611 | }
|
---|
8612 |
|
---|
8613 | /* ensure the target directory exists */
|
---|
8614 | if (capabilities & MediumFormatCapabilities_File)
|
---|
8615 | {
|
---|
8616 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
8617 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
8618 | if (FAILED(rc))
|
---|
8619 | throw rc;
|
---|
8620 | }
|
---|
8621 |
|
---|
8622 | vrc = VDCreateDiff(hdd,
|
---|
8623 | targetFormat.c_str(),
|
---|
8624 | targetLocation.c_str(),
|
---|
8625 | (task.mVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted | MediumVariant_VmdkESX))
|
---|
8626 | | VD_IMAGE_FLAGS_DIFF,
|
---|
8627 | NULL,
|
---|
8628 | targetId.raw(),
|
---|
8629 | id.raw(),
|
---|
8630 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
8631 | pTarget->m->vdImageIfaces,
|
---|
8632 | task.mVDOperationIfaces);
|
---|
8633 | if (RT_FAILURE(vrc))
|
---|
8634 | {
|
---|
8635 | if (vrc == VERR_VD_INVALID_TYPE)
|
---|
8636 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
8637 | tr("Parameters for creating the differencing medium storage unit '%s' are invalid%s"),
|
---|
8638 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
8639 | else
|
---|
8640 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
8641 | tr("Could not create the differencing medium storage unit '%s'%s"),
|
---|
8642 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
8643 | }
|
---|
8644 |
|
---|
8645 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
8646 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
8647 | unsigned uImageFlags;
|
---|
8648 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
8649 | if (RT_SUCCESS(vrc))
|
---|
8650 | variant = (MediumVariant_T)uImageFlags;
|
---|
8651 | }
|
---|
8652 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8653 |
|
---|
8654 | VDDestroy(hdd);
|
---|
8655 | }
|
---|
8656 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8657 |
|
---|
8658 | MultiResult mrc(rcTmp);
|
---|
8659 |
|
---|
8660 | if (SUCCEEDED(mrc))
|
---|
8661 | {
|
---|
8662 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
8663 |
|
---|
8664 | Assert(pTarget->m->pParent.isNull());
|
---|
8665 |
|
---|
8666 | /* associate child with the parent, maximum depth was checked above */
|
---|
8667 | pTarget->i_setParent(this);
|
---|
8668 |
|
---|
8669 | /* diffs for immutable media are auto-reset by default */
|
---|
8670 | bool fAutoReset;
|
---|
8671 | {
|
---|
8672 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
8673 | AutoReadLock block(pBase COMMA_LOCKVAL_SRC_POS);
|
---|
8674 | fAutoReset = (pBase->m->type == MediumType_Immutable);
|
---|
8675 | }
|
---|
8676 | {
|
---|
8677 | AutoWriteLock tlock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8678 | pTarget->m->autoReset = fAutoReset;
|
---|
8679 | }
|
---|
8680 |
|
---|
8681 | /* register with mVirtualBox as the last step and move to
|
---|
8682 | * Created state only on success (leaving an orphan file is
|
---|
8683 | * better than breaking media registry consistency) */
|
---|
8684 | ComObjPtr<Medium> pMedium;
|
---|
8685 | mrc = m->pVirtualBox->i_registerMedium(pTarget, &pMedium, treeLock);
|
---|
8686 | Assert(pTarget == pMedium);
|
---|
8687 |
|
---|
8688 | if (FAILED(mrc))
|
---|
8689 | /* break the parent association on failure to register */
|
---|
8690 | i_deparent();
|
---|
8691 | }
|
---|
8692 |
|
---|
8693 | AutoMultiWriteLock2 mediaLock(this, pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8694 |
|
---|
8695 | if (SUCCEEDED(mrc))
|
---|
8696 | {
|
---|
8697 | pTarget->m->state = MediumState_Created;
|
---|
8698 |
|
---|
8699 | pTarget->m->size = size;
|
---|
8700 | pTarget->m->logicalSize = logicalSize;
|
---|
8701 | pTarget->m->variant = variant;
|
---|
8702 | }
|
---|
8703 | else
|
---|
8704 | {
|
---|
8705 | /* back to NotCreated on failure */
|
---|
8706 | pTarget->m->state = MediumState_NotCreated;
|
---|
8707 |
|
---|
8708 | pTarget->m->autoReset = false;
|
---|
8709 |
|
---|
8710 | /* reset UUID to prevent it from being reused next time */
|
---|
8711 | if (fGenerateUuid)
|
---|
8712 | unconst(pTarget->m->id).clear();
|
---|
8713 | }
|
---|
8714 |
|
---|
8715 | // deregister the task registered in createDiffStorage()
|
---|
8716 | Assert(m->numCreateDiffTasks != 0);
|
---|
8717 | --m->numCreateDiffTasks;
|
---|
8718 |
|
---|
8719 | mediaLock.release();
|
---|
8720 | i_markRegistriesModified();
|
---|
8721 | if (task.isAsync())
|
---|
8722 | {
|
---|
8723 | // in asynchronous mode, save settings now
|
---|
8724 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
8725 | }
|
---|
8726 |
|
---|
8727 | /* Note that in sync mode, it's the caller's responsibility to
|
---|
8728 | * unlock the medium. */
|
---|
8729 |
|
---|
8730 | return mrc;
|
---|
8731 | }
|
---|
8732 |
|
---|
8733 | /**
|
---|
8734 | * Implementation code for the "merge" task.
|
---|
8735 | *
|
---|
8736 | * This task always gets started from Medium::mergeTo() and can run
|
---|
8737 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
8738 | * that function. If we run synchronously, the caller expects the medium
|
---|
8739 | * registry modification to be set before returning; otherwise (in asynchronous
|
---|
8740 | * mode), we save the settings ourselves.
|
---|
8741 | *
|
---|
8742 | * @param task
|
---|
8743 | * @return
|
---|
8744 | */
|
---|
8745 | HRESULT Medium::i_taskMergeHandler(Medium::MergeTask &task)
|
---|
8746 | {
|
---|
8747 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
8748 | * to lock order violations, it probably causes lock order issues related
|
---|
8749 | * to the AutoCaller usage. */
|
---|
8750 | HRESULT rcTmp = S_OK;
|
---|
8751 |
|
---|
8752 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
8753 |
|
---|
8754 | try
|
---|
8755 | {
|
---|
8756 | if (!task.mParentForTarget.isNull())
|
---|
8757 | if (task.mParentForTarget->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
8758 | {
|
---|
8759 | AutoReadLock plock(task.mParentForTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8760 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8761 | tr("Cannot merge image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
8762 | task.mParentForTarget->m->strLocationFull.c_str());
|
---|
8763 | }
|
---|
8764 |
|
---|
8765 | // Resize target to source size, if possible. Otherwise throw an error.
|
---|
8766 | // It's offline resizing. Online resizing will be called in the
|
---|
8767 | // SessionMachine::onlineMergeMedium.
|
---|
8768 |
|
---|
8769 | uint64_t sourceSize = 0;
|
---|
8770 | Utf8Str sourceName;
|
---|
8771 | {
|
---|
8772 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8773 | sourceSize = i_getLogicalSize();
|
---|
8774 | sourceName = i_getName();
|
---|
8775 | }
|
---|
8776 | uint64_t targetSize = 0;
|
---|
8777 | Utf8Str targetName;
|
---|
8778 | {
|
---|
8779 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8780 | targetSize = pTarget->i_getLogicalSize();
|
---|
8781 | targetName = pTarget->i_getName();
|
---|
8782 | }
|
---|
8783 |
|
---|
8784 | //reducing vm disks are not implemented yet
|
---|
8785 | if (sourceSize > targetSize)
|
---|
8786 | {
|
---|
8787 | if (i_isMediumFormatFile())
|
---|
8788 | {
|
---|
8789 | // Have to make own lock list, because "resize" method resizes only last image
|
---|
8790 | // in the lock chain. The lock chain already in the task.mpMediumLockList, so
|
---|
8791 | // just make new lock list based on it. In fact the own lock list neither makes
|
---|
8792 | // double locking of mediums nor unlocks them during delete, because medium
|
---|
8793 | // already locked by task.mpMediumLockList and own list is used just to specify
|
---|
8794 | // what "resize" method should resize.
|
---|
8795 |
|
---|
8796 | MediumLockList* pMediumLockListForResize = new MediumLockList();
|
---|
8797 |
|
---|
8798 | for (MediumLockList::Base::iterator it = task.mpMediumLockList->GetBegin();
|
---|
8799 | it != task.mpMediumLockList->GetEnd();
|
---|
8800 | ++it)
|
---|
8801 | {
|
---|
8802 | ComObjPtr<Medium> pMedium = it->GetMedium();
|
---|
8803 | pMediumLockListForResize->Append(pMedium, pMedium->m->state == MediumState_LockedWrite);
|
---|
8804 | if (pMedium == pTarget)
|
---|
8805 | break;
|
---|
8806 | }
|
---|
8807 |
|
---|
8808 | // just to switch internal state of the lock list to avoid errors during list deletion,
|
---|
8809 | // because all meduims in the list already locked by task.mpMediumLockList
|
---|
8810 | HRESULT rc = pMediumLockListForResize->Lock(true /* fSkipOverLockedMedia */);
|
---|
8811 | if (FAILED(rc))
|
---|
8812 | {
|
---|
8813 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8814 | rc = setError(rc,
|
---|
8815 | tr("Failed to lock the medium '%s' to resize before merge"),
|
---|
8816 | targetName.c_str());
|
---|
8817 | delete pMediumLockListForResize;
|
---|
8818 | throw rc;
|
---|
8819 | }
|
---|
8820 |
|
---|
8821 | ComObjPtr<Progress> pProgress(task.GetProgressObject());
|
---|
8822 | rc = pTarget->i_resize(sourceSize, pMediumLockListForResize, &pProgress, true);
|
---|
8823 | if (FAILED(rc))
|
---|
8824 | {
|
---|
8825 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8826 | rc = setError(rc,
|
---|
8827 | tr("Failed to set size of '%s' to size of '%s'"),
|
---|
8828 | targetName.c_str(), sourceName.c_str());
|
---|
8829 | delete pMediumLockListForResize;
|
---|
8830 | throw rc;
|
---|
8831 | }
|
---|
8832 | delete pMediumLockListForResize;
|
---|
8833 | }
|
---|
8834 | else
|
---|
8835 | {
|
---|
8836 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8837 | HRESULT rc = setError(VBOX_E_NOT_SUPPORTED,
|
---|
8838 | tr("Sizes of '%s' and '%s' are different and medium format does not support resing"),
|
---|
8839 | sourceName.c_str(), targetName.c_str());
|
---|
8840 | throw rc;
|
---|
8841 | }
|
---|
8842 | }
|
---|
8843 |
|
---|
8844 | task.GetProgressObject()->SetNextOperation(BstrFmt(tr("Merging medium '%s' to '%s'"),
|
---|
8845 | i_getName().c_str(),
|
---|
8846 | targetName.c_str()).raw(),
|
---|
8847 | 1);
|
---|
8848 |
|
---|
8849 | PVDISK hdd;
|
---|
8850 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8851 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8852 |
|
---|
8853 | try
|
---|
8854 | {
|
---|
8855 | // Similar code appears in SessionMachine::onlineMergeMedium, so
|
---|
8856 | // if you make any changes below check whether they are applicable
|
---|
8857 | // in that context as well.
|
---|
8858 |
|
---|
8859 | unsigned uTargetIdx = VD_LAST_IMAGE;
|
---|
8860 | unsigned uSourceIdx = VD_LAST_IMAGE;
|
---|
8861 | /* Open all media in the chain. */
|
---|
8862 | MediumLockList::Base::iterator lockListBegin =
|
---|
8863 | task.mpMediumLockList->GetBegin();
|
---|
8864 | MediumLockList::Base::iterator lockListEnd =
|
---|
8865 | task.mpMediumLockList->GetEnd();
|
---|
8866 | unsigned i = 0;
|
---|
8867 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
8868 | it != lockListEnd;
|
---|
8869 | ++it)
|
---|
8870 | {
|
---|
8871 | MediumLock &mediumLock = *it;
|
---|
8872 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8873 |
|
---|
8874 | if (pMedium == this)
|
---|
8875 | uSourceIdx = i;
|
---|
8876 | else if (pMedium == pTarget)
|
---|
8877 | uTargetIdx = i;
|
---|
8878 |
|
---|
8879 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8880 |
|
---|
8881 | /*
|
---|
8882 | * complex sanity (sane complexity)
|
---|
8883 | *
|
---|
8884 | * The current medium must be in the Deleting (medium is merged)
|
---|
8885 | * or LockedRead (parent medium) state if it is not the target.
|
---|
8886 | * If it is the target it must be in the LockedWrite state.
|
---|
8887 | */
|
---|
8888 | Assert( ( pMedium != pTarget
|
---|
8889 | && ( pMedium->m->state == MediumState_Deleting
|
---|
8890 | || pMedium->m->state == MediumState_LockedRead))
|
---|
8891 | || ( pMedium == pTarget
|
---|
8892 | && pMedium->m->state == MediumState_LockedWrite));
|
---|
8893 | /*
|
---|
8894 | * Medium must be the target, in the LockedRead state
|
---|
8895 | * or Deleting state where it is not allowed to be attached
|
---|
8896 | * to a virtual machine.
|
---|
8897 | */
|
---|
8898 | Assert( pMedium == pTarget
|
---|
8899 | || pMedium->m->state == MediumState_LockedRead
|
---|
8900 | || ( pMedium->m->backRefs.size() == 0
|
---|
8901 | && pMedium->m->state == MediumState_Deleting));
|
---|
8902 | /* The source medium must be in Deleting state. */
|
---|
8903 | Assert( pMedium != this
|
---|
8904 | || pMedium->m->state == MediumState_Deleting);
|
---|
8905 |
|
---|
8906 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
8907 |
|
---|
8908 | if ( pMedium->m->state == MediumState_LockedRead
|
---|
8909 | || pMedium->m->state == MediumState_Deleting)
|
---|
8910 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
8911 | if (pMedium->m->type == MediumType_Shareable)
|
---|
8912 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
8913 |
|
---|
8914 | /* Open the medium */
|
---|
8915 | vrc = VDOpen(hdd,
|
---|
8916 | pMedium->m->strFormat.c_str(),
|
---|
8917 | pMedium->m->strLocationFull.c_str(),
|
---|
8918 | uOpenFlags | m->uOpenFlagsDef,
|
---|
8919 | pMedium->m->vdImageIfaces);
|
---|
8920 | if (RT_FAILURE(vrc))
|
---|
8921 | throw vrc;
|
---|
8922 |
|
---|
8923 | i++;
|
---|
8924 | }
|
---|
8925 |
|
---|
8926 | ComAssertThrow( uSourceIdx != VD_LAST_IMAGE
|
---|
8927 | && uTargetIdx != VD_LAST_IMAGE, E_FAIL);
|
---|
8928 |
|
---|
8929 | vrc = VDMerge(hdd, uSourceIdx, uTargetIdx,
|
---|
8930 | task.mVDOperationIfaces);
|
---|
8931 | if (RT_FAILURE(vrc))
|
---|
8932 | throw vrc;
|
---|
8933 |
|
---|
8934 | /* update parent UUIDs */
|
---|
8935 | if (!task.mfMergeForward)
|
---|
8936 | {
|
---|
8937 | /* we need to update UUIDs of all source's children
|
---|
8938 | * which cannot be part of the container at once so
|
---|
8939 | * add each one in there individually */
|
---|
8940 | if (task.mpChildrenToReparent)
|
---|
8941 | {
|
---|
8942 | MediumLockList::Base::iterator childrenBegin = task.mpChildrenToReparent->GetBegin();
|
---|
8943 | MediumLockList::Base::iterator childrenEnd = task.mpChildrenToReparent->GetEnd();
|
---|
8944 | for (MediumLockList::Base::iterator it = childrenBegin;
|
---|
8945 | it != childrenEnd;
|
---|
8946 | ++it)
|
---|
8947 | {
|
---|
8948 | Medium *pMedium = it->GetMedium();
|
---|
8949 | /* VD_OPEN_FLAGS_INFO since UUID is wrong yet */
|
---|
8950 | vrc = VDOpen(hdd,
|
---|
8951 | pMedium->m->strFormat.c_str(),
|
---|
8952 | pMedium->m->strLocationFull.c_str(),
|
---|
8953 | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
8954 | pMedium->m->vdImageIfaces);
|
---|
8955 | if (RT_FAILURE(vrc))
|
---|
8956 | throw vrc;
|
---|
8957 |
|
---|
8958 | vrc = VDSetParentUuid(hdd, VD_LAST_IMAGE,
|
---|
8959 | pTarget->m->id.raw());
|
---|
8960 | if (RT_FAILURE(vrc))
|
---|
8961 | throw vrc;
|
---|
8962 |
|
---|
8963 | vrc = VDClose(hdd, false /* fDelete */);
|
---|
8964 | if (RT_FAILURE(vrc))
|
---|
8965 | throw vrc;
|
---|
8966 | }
|
---|
8967 | }
|
---|
8968 | }
|
---|
8969 | }
|
---|
8970 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8971 | catch (int aVRC)
|
---|
8972 | {
|
---|
8973 | rcTmp = setErrorBoth(VBOX_E_FILE_ERROR, aVRC,
|
---|
8974 | tr("Could not merge the medium '%s' to '%s'%s"),
|
---|
8975 | m->strLocationFull.c_str(),
|
---|
8976 | pTarget->m->strLocationFull.c_str(),
|
---|
8977 | i_vdError(aVRC).c_str());
|
---|
8978 | }
|
---|
8979 |
|
---|
8980 | VDDestroy(hdd);
|
---|
8981 | }
|
---|
8982 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8983 |
|
---|
8984 | ErrorInfoKeeper eik;
|
---|
8985 | MultiResult mrc(rcTmp);
|
---|
8986 | HRESULT rc2;
|
---|
8987 |
|
---|
8988 | if (SUCCEEDED(mrc))
|
---|
8989 | {
|
---|
8990 | /* all media but the target were successfully deleted by
|
---|
8991 | * VDMerge; reparent the last one and uninitialize deleted media. */
|
---|
8992 |
|
---|
8993 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
8994 |
|
---|
8995 | if (task.mfMergeForward)
|
---|
8996 | {
|
---|
8997 | /* first, unregister the target since it may become a base
|
---|
8998 | * medium which needs re-registration */
|
---|
8999 | rc2 = m->pVirtualBox->i_unregisterMedium(pTarget);
|
---|
9000 | AssertComRC(rc2);
|
---|
9001 |
|
---|
9002 | /* then, reparent it and disconnect the deleted branch at both ends
|
---|
9003 | * (chain->parent() is source's parent). Depth check above. */
|
---|
9004 | pTarget->i_deparent();
|
---|
9005 | pTarget->i_setParent(task.mParentForTarget);
|
---|
9006 | if (task.mParentForTarget)
|
---|
9007 | i_deparent();
|
---|
9008 |
|
---|
9009 | /* then, register again */
|
---|
9010 | ComObjPtr<Medium> pMedium;
|
---|
9011 | rc2 = m->pVirtualBox->i_registerMedium(pTarget, &pMedium,
|
---|
9012 | treeLock);
|
---|
9013 | AssertComRC(rc2);
|
---|
9014 | }
|
---|
9015 | else
|
---|
9016 | {
|
---|
9017 | Assert(pTarget->i_getChildren().size() == 1);
|
---|
9018 | Medium *targetChild = pTarget->i_getChildren().front();
|
---|
9019 |
|
---|
9020 | /* disconnect the deleted branch at the elder end */
|
---|
9021 | targetChild->i_deparent();
|
---|
9022 |
|
---|
9023 | /* reparent source's children and disconnect the deleted
|
---|
9024 | * branch at the younger end */
|
---|
9025 | if (task.mpChildrenToReparent)
|
---|
9026 | {
|
---|
9027 | /* obey {parent,child} lock order */
|
---|
9028 | AutoWriteLock sourceLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9029 |
|
---|
9030 | MediumLockList::Base::iterator childrenBegin = task.mpChildrenToReparent->GetBegin();
|
---|
9031 | MediumLockList::Base::iterator childrenEnd = task.mpChildrenToReparent->GetEnd();
|
---|
9032 | for (MediumLockList::Base::iterator it = childrenBegin;
|
---|
9033 | it != childrenEnd;
|
---|
9034 | ++it)
|
---|
9035 | {
|
---|
9036 | Medium *pMedium = it->GetMedium();
|
---|
9037 | AutoWriteLock childLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9038 |
|
---|
9039 | pMedium->i_deparent(); // removes pMedium from source
|
---|
9040 | // no depth check, reduces depth
|
---|
9041 | pMedium->i_setParent(pTarget);
|
---|
9042 | }
|
---|
9043 | }
|
---|
9044 | }
|
---|
9045 |
|
---|
9046 | /* unregister and uninitialize all media removed by the merge */
|
---|
9047 | MediumLockList::Base::iterator lockListBegin =
|
---|
9048 | task.mpMediumLockList->GetBegin();
|
---|
9049 | MediumLockList::Base::iterator lockListEnd =
|
---|
9050 | task.mpMediumLockList->GetEnd();
|
---|
9051 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
9052 | it != lockListEnd;
|
---|
9053 | )
|
---|
9054 | {
|
---|
9055 | MediumLock &mediumLock = *it;
|
---|
9056 | /* Create a real copy of the medium pointer, as the medium
|
---|
9057 | * lock deletion below would invalidate the referenced object. */
|
---|
9058 | const ComObjPtr<Medium> pMedium = mediumLock.GetMedium();
|
---|
9059 |
|
---|
9060 | /* The target and all media not merged (readonly) are skipped */
|
---|
9061 | if ( pMedium == pTarget
|
---|
9062 | || pMedium->m->state == MediumState_LockedRead)
|
---|
9063 | {
|
---|
9064 | ++it;
|
---|
9065 | continue;
|
---|
9066 | }
|
---|
9067 |
|
---|
9068 | rc2 = pMedium->m->pVirtualBox->i_unregisterMedium(pMedium);
|
---|
9069 | AssertComRC(rc2);
|
---|
9070 |
|
---|
9071 | /* now, uninitialize the deleted medium (note that
|
---|
9072 | * due to the Deleting state, uninit() will not touch
|
---|
9073 | * the parent-child relationship so we need to
|
---|
9074 | * uninitialize each disk individually) */
|
---|
9075 |
|
---|
9076 | /* note that the operation initiator medium (which is
|
---|
9077 | * normally also the source medium) is a special case
|
---|
9078 | * -- there is one more caller added by Task to it which
|
---|
9079 | * we must release. Also, if we are in sync mode, the
|
---|
9080 | * caller may still hold an AutoCaller instance for it
|
---|
9081 | * and therefore we cannot uninit() it (it's therefore
|
---|
9082 | * the caller's responsibility) */
|
---|
9083 | if (pMedium == this)
|
---|
9084 | {
|
---|
9085 | Assert(i_getChildren().size() == 0);
|
---|
9086 | Assert(m->backRefs.size() == 0);
|
---|
9087 | task.mMediumCaller.release();
|
---|
9088 | }
|
---|
9089 |
|
---|
9090 | /* Delete the medium lock list entry, which also releases the
|
---|
9091 | * caller added by MergeChain before uninit() and updates the
|
---|
9092 | * iterator to point to the right place. */
|
---|
9093 | rc2 = task.mpMediumLockList->RemoveByIterator(it);
|
---|
9094 | AssertComRC(rc2);
|
---|
9095 |
|
---|
9096 | if (task.isAsync() || pMedium != this)
|
---|
9097 | {
|
---|
9098 | treeLock.release();
|
---|
9099 | pMedium->uninit();
|
---|
9100 | treeLock.acquire();
|
---|
9101 | }
|
---|
9102 | }
|
---|
9103 | }
|
---|
9104 |
|
---|
9105 | i_markRegistriesModified();
|
---|
9106 | if (task.isAsync())
|
---|
9107 | {
|
---|
9108 | // in asynchronous mode, save settings now
|
---|
9109 | eik.restore();
|
---|
9110 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
9111 | eik.fetch();
|
---|
9112 | }
|
---|
9113 |
|
---|
9114 | if (FAILED(mrc))
|
---|
9115 | {
|
---|
9116 | /* Here we come if either VDMerge() failed (in which case we
|
---|
9117 | * assume that it tried to do everything to make a further
|
---|
9118 | * retry possible -- e.g. not deleted intermediate media
|
---|
9119 | * and so on) or VirtualBox::saveRegistries() failed (where we
|
---|
9120 | * should have the original tree but with intermediate storage
|
---|
9121 | * units deleted by VDMerge()). We have to only restore states
|
---|
9122 | * (through the MergeChain dtor) unless we are run synchronously
|
---|
9123 | * in which case it's the responsibility of the caller as stated
|
---|
9124 | * in the mergeTo() docs. The latter also implies that we
|
---|
9125 | * don't own the merge chain, so release it in this case. */
|
---|
9126 | if (task.isAsync())
|
---|
9127 | i_cancelMergeTo(task.mpChildrenToReparent, task.mpMediumLockList);
|
---|
9128 | }
|
---|
9129 |
|
---|
9130 | return mrc;
|
---|
9131 | }
|
---|
9132 |
|
---|
9133 | /**
|
---|
9134 | * Implementation code for the "clone" task.
|
---|
9135 | *
|
---|
9136 | * This only gets started from Medium::CloneTo() and always runs asynchronously.
|
---|
9137 | * As a result, we always save the VirtualBox.xml file when we're done here.
|
---|
9138 | *
|
---|
9139 | * @param task
|
---|
9140 | * @return
|
---|
9141 | */
|
---|
9142 | HRESULT Medium::i_taskCloneHandler(Medium::CloneTask &task)
|
---|
9143 | {
|
---|
9144 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
9145 | * to lock order violations, it probably causes lock order issues related
|
---|
9146 | * to the AutoCaller usage. */
|
---|
9147 | HRESULT rcTmp = S_OK;
|
---|
9148 |
|
---|
9149 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
9150 | const ComObjPtr<Medium> &pParent = task.mParent;
|
---|
9151 |
|
---|
9152 | bool fCreatingTarget = false;
|
---|
9153 |
|
---|
9154 | uint64_t size = 0, logicalSize = 0;
|
---|
9155 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
9156 | bool fGenerateUuid = false;
|
---|
9157 |
|
---|
9158 | try
|
---|
9159 | {
|
---|
9160 | if (!pParent.isNull())
|
---|
9161 | {
|
---|
9162 |
|
---|
9163 | if (pParent->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
9164 | {
|
---|
9165 | AutoReadLock plock(pParent COMMA_LOCKVAL_SRC_POS);
|
---|
9166 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
9167 | tr("Cannot clone image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
9168 | pParent->m->strLocationFull.c_str());
|
---|
9169 | }
|
---|
9170 | }
|
---|
9171 |
|
---|
9172 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
9173 | * signal from the task initiator (which releases it only after
|
---|
9174 | * RTThreadCreate()) that we can start the job. */
|
---|
9175 | AutoMultiWriteLock3 thisLock(this, pTarget, pParent COMMA_LOCKVAL_SRC_POS);
|
---|
9176 |
|
---|
9177 | fCreatingTarget = pTarget->m->state == MediumState_Creating;
|
---|
9178 |
|
---|
9179 | /* The object may request a specific UUID (through a special form of
|
---|
9180 | * the moveTo() argument). Otherwise we have to generate it */
|
---|
9181 | Guid targetId = pTarget->m->id;
|
---|
9182 |
|
---|
9183 | fGenerateUuid = targetId.isZero();
|
---|
9184 | if (fGenerateUuid)
|
---|
9185 | {
|
---|
9186 | targetId.create();
|
---|
9187 | /* VirtualBox::registerMedium() will need UUID */
|
---|
9188 | unconst(pTarget->m->id) = targetId;
|
---|
9189 | }
|
---|
9190 |
|
---|
9191 | PVDISK hdd;
|
---|
9192 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9193 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9194 |
|
---|
9195 | try
|
---|
9196 | {
|
---|
9197 | /* Open all media in the source chain. */
|
---|
9198 | MediumLockList::Base::const_iterator sourceListBegin =
|
---|
9199 | task.mpSourceMediumLockList->GetBegin();
|
---|
9200 | MediumLockList::Base::const_iterator sourceListEnd =
|
---|
9201 | task.mpSourceMediumLockList->GetEnd();
|
---|
9202 | for (MediumLockList::Base::const_iterator it = sourceListBegin;
|
---|
9203 | it != sourceListEnd;
|
---|
9204 | ++it)
|
---|
9205 | {
|
---|
9206 | const MediumLock &mediumLock = *it;
|
---|
9207 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9208 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9209 |
|
---|
9210 | /* sanity check */
|
---|
9211 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
9212 |
|
---|
9213 | /** Open all media in read-only mode. */
|
---|
9214 | vrc = VDOpen(hdd,
|
---|
9215 | pMedium->m->strFormat.c_str(),
|
---|
9216 | pMedium->m->strLocationFull.c_str(),
|
---|
9217 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
9218 | pMedium->m->vdImageIfaces);
|
---|
9219 | if (RT_FAILURE(vrc))
|
---|
9220 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9221 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9222 | pMedium->m->strLocationFull.c_str(),
|
---|
9223 | i_vdError(vrc).c_str());
|
---|
9224 | }
|
---|
9225 |
|
---|
9226 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
9227 | Utf8Str targetLocation(pTarget->m->strLocationFull);
|
---|
9228 | uint64_t capabilities = pTarget->m->formatObj->i_getCapabilities();
|
---|
9229 |
|
---|
9230 | Assert( pTarget->m->state == MediumState_Creating
|
---|
9231 | || pTarget->m->state == MediumState_LockedWrite);
|
---|
9232 | Assert(m->state == MediumState_LockedRead);
|
---|
9233 | Assert( pParent.isNull()
|
---|
9234 | || pParent->m->state == MediumState_LockedRead);
|
---|
9235 |
|
---|
9236 | /* unlock before the potentially lengthy operation */
|
---|
9237 | thisLock.release();
|
---|
9238 |
|
---|
9239 | /* ensure the target directory exists */
|
---|
9240 | if (capabilities & MediumFormatCapabilities_File)
|
---|
9241 | {
|
---|
9242 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
9243 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
9244 | if (FAILED(rc))
|
---|
9245 | throw rc;
|
---|
9246 | }
|
---|
9247 |
|
---|
9248 | PVDISK targetHdd;
|
---|
9249 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &targetHdd);
|
---|
9250 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9251 |
|
---|
9252 | try
|
---|
9253 | {
|
---|
9254 | /* Open all media in the target chain. */
|
---|
9255 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
9256 | task.mpTargetMediumLockList->GetBegin();
|
---|
9257 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
9258 | task.mpTargetMediumLockList->GetEnd();
|
---|
9259 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
9260 | it != targetListEnd;
|
---|
9261 | ++it)
|
---|
9262 | {
|
---|
9263 | const MediumLock &mediumLock = *it;
|
---|
9264 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9265 |
|
---|
9266 | /* If the target medium is not created yet there's no
|
---|
9267 | * reason to open it. */
|
---|
9268 | if (pMedium == pTarget && fCreatingTarget)
|
---|
9269 | continue;
|
---|
9270 |
|
---|
9271 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9272 |
|
---|
9273 | /* sanity check */
|
---|
9274 | Assert( pMedium->m->state == MediumState_LockedRead
|
---|
9275 | || pMedium->m->state == MediumState_LockedWrite);
|
---|
9276 |
|
---|
9277 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
9278 | if (pMedium->m->state != MediumState_LockedWrite)
|
---|
9279 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
9280 | if (pMedium->m->type == MediumType_Shareable)
|
---|
9281 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
9282 |
|
---|
9283 | /* Open all media in appropriate mode. */
|
---|
9284 | vrc = VDOpen(targetHdd,
|
---|
9285 | pMedium->m->strFormat.c_str(),
|
---|
9286 | pMedium->m->strLocationFull.c_str(),
|
---|
9287 | uOpenFlags | m->uOpenFlagsDef,
|
---|
9288 | pMedium->m->vdImageIfaces);
|
---|
9289 | if (RT_FAILURE(vrc))
|
---|
9290 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9291 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9292 | pMedium->m->strLocationFull.c_str(),
|
---|
9293 | i_vdError(vrc).c_str());
|
---|
9294 | }
|
---|
9295 |
|
---|
9296 | /* target isn't locked, but no changing data is accessed */
|
---|
9297 | if (task.midxSrcImageSame == UINT32_MAX)
|
---|
9298 | {
|
---|
9299 | vrc = VDCopy(hdd,
|
---|
9300 | VD_LAST_IMAGE,
|
---|
9301 | targetHdd,
|
---|
9302 | targetFormat.c_str(),
|
---|
9303 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
9304 | false /* fMoveByRename */,
|
---|
9305 | 0 /* cbSize */,
|
---|
9306 | task.mVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted),
|
---|
9307 | targetId.raw(),
|
---|
9308 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
9309 | NULL /* pVDIfsOperation */,
|
---|
9310 | pTarget->m->vdImageIfaces,
|
---|
9311 | task.mVDOperationIfaces);
|
---|
9312 | }
|
---|
9313 | else
|
---|
9314 | {
|
---|
9315 | vrc = VDCopyEx(hdd,
|
---|
9316 | VD_LAST_IMAGE,
|
---|
9317 | targetHdd,
|
---|
9318 | targetFormat.c_str(),
|
---|
9319 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
9320 | false /* fMoveByRename */,
|
---|
9321 | 0 /* cbSize */,
|
---|
9322 | task.midxSrcImageSame,
|
---|
9323 | task.midxDstImageSame,
|
---|
9324 | task.mVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted),
|
---|
9325 | targetId.raw(),
|
---|
9326 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
9327 | NULL /* pVDIfsOperation */,
|
---|
9328 | pTarget->m->vdImageIfaces,
|
---|
9329 | task.mVDOperationIfaces);
|
---|
9330 | }
|
---|
9331 | if (RT_FAILURE(vrc))
|
---|
9332 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9333 | tr("Could not create the clone medium '%s'%s"),
|
---|
9334 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
9335 |
|
---|
9336 | size = VDGetFileSize(targetHdd, VD_LAST_IMAGE);
|
---|
9337 | logicalSize = VDGetSize(targetHdd, VD_LAST_IMAGE);
|
---|
9338 | unsigned uImageFlags;
|
---|
9339 | vrc = VDGetImageFlags(targetHdd, 0, &uImageFlags);
|
---|
9340 | if (RT_SUCCESS(vrc))
|
---|
9341 | variant = (MediumVariant_T)uImageFlags;
|
---|
9342 | }
|
---|
9343 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9344 |
|
---|
9345 | VDDestroy(targetHdd);
|
---|
9346 | }
|
---|
9347 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9348 |
|
---|
9349 | VDDestroy(hdd);
|
---|
9350 | }
|
---|
9351 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9352 |
|
---|
9353 | ErrorInfoKeeper eik;
|
---|
9354 | MultiResult mrc(rcTmp);
|
---|
9355 |
|
---|
9356 | /* Only do the parent changes for newly created media. */
|
---|
9357 | if (SUCCEEDED(mrc) && fCreatingTarget)
|
---|
9358 | {
|
---|
9359 | /* we set m->pParent & children() */
|
---|
9360 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
9361 |
|
---|
9362 | Assert(pTarget->m->pParent.isNull());
|
---|
9363 |
|
---|
9364 | if (pParent)
|
---|
9365 | {
|
---|
9366 | /* Associate the clone with the parent and deassociate
|
---|
9367 | * from VirtualBox. Depth check above. */
|
---|
9368 | pTarget->i_setParent(pParent);
|
---|
9369 |
|
---|
9370 | /* register with mVirtualBox as the last step and move to
|
---|
9371 | * Created state only on success (leaving an orphan file is
|
---|
9372 | * better than breaking media registry consistency) */
|
---|
9373 | eik.restore();
|
---|
9374 | ComObjPtr<Medium> pMedium;
|
---|
9375 | mrc = pParent->m->pVirtualBox->i_registerMedium(pTarget, &pMedium,
|
---|
9376 | treeLock);
|
---|
9377 | Assert( FAILED(mrc)
|
---|
9378 | || pTarget == pMedium);
|
---|
9379 | eik.fetch();
|
---|
9380 |
|
---|
9381 | if (FAILED(mrc))
|
---|
9382 | /* break parent association on failure to register */
|
---|
9383 | pTarget->i_deparent(); // removes target from parent
|
---|
9384 | }
|
---|
9385 | else
|
---|
9386 | {
|
---|
9387 | /* just register */
|
---|
9388 | eik.restore();
|
---|
9389 | ComObjPtr<Medium> pMedium;
|
---|
9390 | mrc = m->pVirtualBox->i_registerMedium(pTarget, &pMedium,
|
---|
9391 | treeLock);
|
---|
9392 | Assert( FAILED(mrc)
|
---|
9393 | || pTarget == pMedium);
|
---|
9394 | eik.fetch();
|
---|
9395 | }
|
---|
9396 | }
|
---|
9397 |
|
---|
9398 | if (fCreatingTarget)
|
---|
9399 | {
|
---|
9400 | AutoWriteLock mLock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
9401 |
|
---|
9402 | if (SUCCEEDED(mrc))
|
---|
9403 | {
|
---|
9404 | pTarget->m->state = MediumState_Created;
|
---|
9405 |
|
---|
9406 | pTarget->m->size = size;
|
---|
9407 | pTarget->m->logicalSize = logicalSize;
|
---|
9408 | pTarget->m->variant = variant;
|
---|
9409 | }
|
---|
9410 | else
|
---|
9411 | {
|
---|
9412 | /* back to NotCreated on failure */
|
---|
9413 | pTarget->m->state = MediumState_NotCreated;
|
---|
9414 |
|
---|
9415 | /* reset UUID to prevent it from being reused next time */
|
---|
9416 | if (fGenerateUuid)
|
---|
9417 | unconst(pTarget->m->id).clear();
|
---|
9418 | }
|
---|
9419 | }
|
---|
9420 |
|
---|
9421 | /* Copy any filter related settings over to the target. */
|
---|
9422 | if (SUCCEEDED(mrc))
|
---|
9423 | {
|
---|
9424 | /* Copy any filter related settings over. */
|
---|
9425 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
9426 | ComObjPtr<Medium> pTargetBase = pTarget->i_getBase();
|
---|
9427 | std::vector<com::Utf8Str> aFilterPropNames;
|
---|
9428 | std::vector<com::Utf8Str> aFilterPropValues;
|
---|
9429 | mrc = pBase->i_getFilterProperties(aFilterPropNames, aFilterPropValues);
|
---|
9430 | if (SUCCEEDED(mrc))
|
---|
9431 | {
|
---|
9432 | /* Go through the properties and add them to the target medium. */
|
---|
9433 | for (unsigned idx = 0; idx < aFilterPropNames.size(); idx++)
|
---|
9434 | {
|
---|
9435 | mrc = pTargetBase->i_setPropertyDirect(aFilterPropNames[idx], aFilterPropValues[idx]);
|
---|
9436 | if (FAILED(mrc)) break;
|
---|
9437 | }
|
---|
9438 |
|
---|
9439 | // now, at the end of this task (always asynchronous), save the settings
|
---|
9440 | if (SUCCEEDED(mrc))
|
---|
9441 | {
|
---|
9442 | // save the settings
|
---|
9443 | i_markRegistriesModified();
|
---|
9444 | /* collect multiple errors */
|
---|
9445 | eik.restore();
|
---|
9446 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
9447 | eik.fetch();
|
---|
9448 | }
|
---|
9449 | }
|
---|
9450 | }
|
---|
9451 |
|
---|
9452 | /* Everything is explicitly unlocked when the task exits,
|
---|
9453 | * as the task destruction also destroys the source chain. */
|
---|
9454 |
|
---|
9455 | /* Make sure the source chain is released early. It could happen
|
---|
9456 | * that we get a deadlock in Appliance::Import when Medium::Close
|
---|
9457 | * is called & the source chain is released at the same time. */
|
---|
9458 | task.mpSourceMediumLockList->Clear();
|
---|
9459 |
|
---|
9460 | return mrc;
|
---|
9461 | }
|
---|
9462 |
|
---|
9463 | /**
|
---|
9464 | * Implementation code for the "move" task.
|
---|
9465 | *
|
---|
9466 | * This only gets started from Medium::MoveTo() and always
|
---|
9467 | * runs asynchronously.
|
---|
9468 | *
|
---|
9469 | * @param task
|
---|
9470 | * @return
|
---|
9471 | */
|
---|
9472 | HRESULT Medium::i_taskMoveHandler(Medium::MoveTask &task)
|
---|
9473 | {
|
---|
9474 |
|
---|
9475 | HRESULT rcOut = S_OK;
|
---|
9476 |
|
---|
9477 | /* pTarget is equal "this" in our case */
|
---|
9478 | const ComObjPtr<Medium> &pTarget = task.mMedium;
|
---|
9479 |
|
---|
9480 | uint64_t size = 0; NOREF(size);
|
---|
9481 | uint64_t logicalSize = 0; NOREF(logicalSize);
|
---|
9482 | MediumVariant_T variant = MediumVariant_Standard; NOREF(variant);
|
---|
9483 |
|
---|
9484 | /*
|
---|
9485 | * it's exactly moving, not cloning
|
---|
9486 | */
|
---|
9487 | if (!i_isMoveOperation(pTarget))
|
---|
9488 | {
|
---|
9489 | HRESULT rc = setError(VBOX_E_FILE_ERROR,
|
---|
9490 | tr("Wrong preconditions for moving the medium %s"),
|
---|
9491 | pTarget->m->strLocationFull.c_str());
|
---|
9492 | return rc;
|
---|
9493 | }
|
---|
9494 |
|
---|
9495 | try
|
---|
9496 | {
|
---|
9497 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
9498 | * signal from the task initiator (which releases it only after
|
---|
9499 | * RTThreadCreate()) that we can start the job. */
|
---|
9500 |
|
---|
9501 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9502 |
|
---|
9503 | PVDISK hdd;
|
---|
9504 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9505 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9506 |
|
---|
9507 | try
|
---|
9508 | {
|
---|
9509 | /* Open all media in the source chain. */
|
---|
9510 | MediumLockList::Base::const_iterator sourceListBegin =
|
---|
9511 | task.mpMediumLockList->GetBegin();
|
---|
9512 | MediumLockList::Base::const_iterator sourceListEnd =
|
---|
9513 | task.mpMediumLockList->GetEnd();
|
---|
9514 | for (MediumLockList::Base::const_iterator it = sourceListBegin;
|
---|
9515 | it != sourceListEnd;
|
---|
9516 | ++it)
|
---|
9517 | {
|
---|
9518 | const MediumLock &mediumLock = *it;
|
---|
9519 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9520 | AutoWriteLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9521 |
|
---|
9522 | /* sanity check */
|
---|
9523 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
9524 |
|
---|
9525 | vrc = VDOpen(hdd,
|
---|
9526 | pMedium->m->strFormat.c_str(),
|
---|
9527 | pMedium->m->strLocationFull.c_str(),
|
---|
9528 | VD_OPEN_FLAGS_NORMAL,
|
---|
9529 | pMedium->m->vdImageIfaces);
|
---|
9530 | if (RT_FAILURE(vrc))
|
---|
9531 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9532 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9533 | pMedium->m->strLocationFull.c_str(),
|
---|
9534 | i_vdError(vrc).c_str());
|
---|
9535 | }
|
---|
9536 |
|
---|
9537 | /* we can directly use pTarget->m->"variables" but for better reading we use local copies */
|
---|
9538 | Guid targetId = pTarget->m->id;
|
---|
9539 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
9540 | uint64_t targetCapabilities = pTarget->m->formatObj->i_getCapabilities();
|
---|
9541 |
|
---|
9542 | /*
|
---|
9543 | * change target location
|
---|
9544 | * m->strNewLocationFull has been set already together with m->fMoveThisMedium in
|
---|
9545 | * i_preparationForMoving()
|
---|
9546 | */
|
---|
9547 | Utf8Str targetLocation = i_getNewLocationForMoving();
|
---|
9548 |
|
---|
9549 | /* unlock before the potentially lengthy operation */
|
---|
9550 | thisLock.release();
|
---|
9551 |
|
---|
9552 | /* ensure the target directory exists */
|
---|
9553 | if (targetCapabilities & MediumFormatCapabilities_File)
|
---|
9554 | {
|
---|
9555 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
9556 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
9557 | if (FAILED(rc))
|
---|
9558 | throw rc;
|
---|
9559 | }
|
---|
9560 |
|
---|
9561 | try
|
---|
9562 | {
|
---|
9563 | vrc = VDCopy(hdd,
|
---|
9564 | VD_LAST_IMAGE,
|
---|
9565 | hdd,
|
---|
9566 | targetFormat.c_str(),
|
---|
9567 | targetLocation.c_str(),
|
---|
9568 | true /* fMoveByRename */,
|
---|
9569 | 0 /* cbSize */,
|
---|
9570 | VD_IMAGE_FLAGS_NONE,
|
---|
9571 | targetId.raw(),
|
---|
9572 | VD_OPEN_FLAGS_NORMAL,
|
---|
9573 | NULL /* pVDIfsOperation */,
|
---|
9574 | NULL,
|
---|
9575 | NULL);
|
---|
9576 | if (RT_FAILURE(vrc))
|
---|
9577 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9578 | tr("Could not move medium '%s'%s"),
|
---|
9579 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
9580 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
9581 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
9582 | unsigned uImageFlags;
|
---|
9583 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
9584 | if (RT_SUCCESS(vrc))
|
---|
9585 | variant = (MediumVariant_T)uImageFlags;
|
---|
9586 |
|
---|
9587 | /*
|
---|
9588 | * set current location, because VDCopy\VDCopyEx doesn't do it.
|
---|
9589 | * also reset moving flag
|
---|
9590 | */
|
---|
9591 | i_resetMoveOperationData();
|
---|
9592 | m->strLocationFull = targetLocation;
|
---|
9593 |
|
---|
9594 | }
|
---|
9595 | catch (HRESULT aRC) { rcOut = aRC; }
|
---|
9596 |
|
---|
9597 | }
|
---|
9598 | catch (HRESULT aRC) { rcOut = aRC; }
|
---|
9599 |
|
---|
9600 | VDDestroy(hdd);
|
---|
9601 | }
|
---|
9602 | catch (HRESULT aRC) { rcOut = aRC; }
|
---|
9603 |
|
---|
9604 | ErrorInfoKeeper eik;
|
---|
9605 | MultiResult mrc(rcOut);
|
---|
9606 |
|
---|
9607 | // now, at the end of this task (always asynchronous), save the settings
|
---|
9608 | if (SUCCEEDED(mrc))
|
---|
9609 | {
|
---|
9610 | // save the settings
|
---|
9611 | i_markRegistriesModified();
|
---|
9612 | /* collect multiple errors */
|
---|
9613 | eik.restore();
|
---|
9614 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
9615 | eik.fetch();
|
---|
9616 | }
|
---|
9617 |
|
---|
9618 | /* Everything is explicitly unlocked when the task exits,
|
---|
9619 | * as the task destruction also destroys the source chain. */
|
---|
9620 |
|
---|
9621 | task.mpMediumLockList->Clear();
|
---|
9622 |
|
---|
9623 | return mrc;
|
---|
9624 | }
|
---|
9625 |
|
---|
9626 | /**
|
---|
9627 | * Implementation code for the "delete" task.
|
---|
9628 | *
|
---|
9629 | * This task always gets started from Medium::deleteStorage() and can run
|
---|
9630 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
9631 | * that function.
|
---|
9632 | *
|
---|
9633 | * @param task
|
---|
9634 | * @return
|
---|
9635 | */
|
---|
9636 | HRESULT Medium::i_taskDeleteHandler(Medium::DeleteTask &task)
|
---|
9637 | {
|
---|
9638 | NOREF(task);
|
---|
9639 | HRESULT rc = S_OK;
|
---|
9640 |
|
---|
9641 | try
|
---|
9642 | {
|
---|
9643 | /* The lock is also used as a signal from the task initiator (which
|
---|
9644 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
9645 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9646 |
|
---|
9647 | PVDISK hdd;
|
---|
9648 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9649 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9650 |
|
---|
9651 | Utf8Str format(m->strFormat);
|
---|
9652 | Utf8Str location(m->strLocationFull);
|
---|
9653 |
|
---|
9654 | /* unlock before the potentially lengthy operation */
|
---|
9655 | Assert(m->state == MediumState_Deleting);
|
---|
9656 | thisLock.release();
|
---|
9657 |
|
---|
9658 | try
|
---|
9659 | {
|
---|
9660 | vrc = VDOpen(hdd,
|
---|
9661 | format.c_str(),
|
---|
9662 | location.c_str(),
|
---|
9663 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
9664 | m->vdImageIfaces);
|
---|
9665 | if (RT_SUCCESS(vrc))
|
---|
9666 | vrc = VDClose(hdd, true /* fDelete */);
|
---|
9667 |
|
---|
9668 | if (RT_FAILURE(vrc) && vrc != VERR_FILE_NOT_FOUND)
|
---|
9669 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9670 | tr("Could not delete the medium storage unit '%s'%s"),
|
---|
9671 | location.c_str(), i_vdError(vrc).c_str());
|
---|
9672 |
|
---|
9673 | }
|
---|
9674 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9675 |
|
---|
9676 | VDDestroy(hdd);
|
---|
9677 | }
|
---|
9678 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9679 |
|
---|
9680 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9681 |
|
---|
9682 | /* go to the NotCreated state even on failure since the storage
|
---|
9683 | * may have been already partially deleted and cannot be used any
|
---|
9684 | * more. One will be able to manually re-open the storage if really
|
---|
9685 | * needed to re-register it. */
|
---|
9686 | m->state = MediumState_NotCreated;
|
---|
9687 |
|
---|
9688 | /* Reset UUID to prevent Create* from reusing it again */
|
---|
9689 | unconst(m->id).clear();
|
---|
9690 |
|
---|
9691 | return rc;
|
---|
9692 | }
|
---|
9693 |
|
---|
9694 | /**
|
---|
9695 | * Implementation code for the "reset" task.
|
---|
9696 | *
|
---|
9697 | * This always gets started asynchronously from Medium::Reset().
|
---|
9698 | *
|
---|
9699 | * @param task
|
---|
9700 | * @return
|
---|
9701 | */
|
---|
9702 | HRESULT Medium::i_taskResetHandler(Medium::ResetTask &task)
|
---|
9703 | {
|
---|
9704 | HRESULT rc = S_OK;
|
---|
9705 |
|
---|
9706 | uint64_t size = 0, logicalSize = 0;
|
---|
9707 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
9708 |
|
---|
9709 | try
|
---|
9710 | {
|
---|
9711 | /* The lock is also used as a signal from the task initiator (which
|
---|
9712 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
9713 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9714 |
|
---|
9715 | /// @todo Below we use a pair of delete/create operations to reset
|
---|
9716 | /// the diff contents but the most efficient way will of course be
|
---|
9717 | /// to add a VDResetDiff() API call
|
---|
9718 |
|
---|
9719 | PVDISK hdd;
|
---|
9720 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9721 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9722 |
|
---|
9723 | Guid id = m->id;
|
---|
9724 | Utf8Str format(m->strFormat);
|
---|
9725 | Utf8Str location(m->strLocationFull);
|
---|
9726 |
|
---|
9727 | Medium *pParent = m->pParent;
|
---|
9728 | Guid parentId = pParent->m->id;
|
---|
9729 | Utf8Str parentFormat(pParent->m->strFormat);
|
---|
9730 | Utf8Str parentLocation(pParent->m->strLocationFull);
|
---|
9731 |
|
---|
9732 | Assert(m->state == MediumState_LockedWrite);
|
---|
9733 |
|
---|
9734 | /* unlock before the potentially lengthy operation */
|
---|
9735 | thisLock.release();
|
---|
9736 |
|
---|
9737 | try
|
---|
9738 | {
|
---|
9739 | /* Open all media in the target chain but the last. */
|
---|
9740 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
9741 | task.mpMediumLockList->GetBegin();
|
---|
9742 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
9743 | task.mpMediumLockList->GetEnd();
|
---|
9744 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
9745 | it != targetListEnd;
|
---|
9746 | ++it)
|
---|
9747 | {
|
---|
9748 | const MediumLock &mediumLock = *it;
|
---|
9749 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9750 |
|
---|
9751 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9752 |
|
---|
9753 | /* sanity check, "this" is checked above */
|
---|
9754 | Assert( pMedium == this
|
---|
9755 | || pMedium->m->state == MediumState_LockedRead);
|
---|
9756 |
|
---|
9757 | /* Open all media in appropriate mode. */
|
---|
9758 | vrc = VDOpen(hdd,
|
---|
9759 | pMedium->m->strFormat.c_str(),
|
---|
9760 | pMedium->m->strLocationFull.c_str(),
|
---|
9761 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
9762 | pMedium->m->vdImageIfaces);
|
---|
9763 | if (RT_FAILURE(vrc))
|
---|
9764 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9765 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9766 | pMedium->m->strLocationFull.c_str(),
|
---|
9767 | i_vdError(vrc).c_str());
|
---|
9768 |
|
---|
9769 | /* Done when we hit the media which should be reset */
|
---|
9770 | if (pMedium == this)
|
---|
9771 | break;
|
---|
9772 | }
|
---|
9773 |
|
---|
9774 | /* first, delete the storage unit */
|
---|
9775 | vrc = VDClose(hdd, true /* fDelete */);
|
---|
9776 | if (RT_FAILURE(vrc))
|
---|
9777 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9778 | tr("Could not delete the medium storage unit '%s'%s"),
|
---|
9779 | location.c_str(), i_vdError(vrc).c_str());
|
---|
9780 |
|
---|
9781 | /* next, create it again */
|
---|
9782 | vrc = VDOpen(hdd,
|
---|
9783 | parentFormat.c_str(),
|
---|
9784 | parentLocation.c_str(),
|
---|
9785 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
9786 | m->vdImageIfaces);
|
---|
9787 | if (RT_FAILURE(vrc))
|
---|
9788 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9789 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9790 | parentLocation.c_str(), i_vdError(vrc).c_str());
|
---|
9791 |
|
---|
9792 | vrc = VDCreateDiff(hdd,
|
---|
9793 | format.c_str(),
|
---|
9794 | location.c_str(),
|
---|
9795 | /// @todo use the same medium variant as before
|
---|
9796 | VD_IMAGE_FLAGS_NONE,
|
---|
9797 | NULL,
|
---|
9798 | id.raw(),
|
---|
9799 | parentId.raw(),
|
---|
9800 | VD_OPEN_FLAGS_NORMAL,
|
---|
9801 | m->vdImageIfaces,
|
---|
9802 | task.mVDOperationIfaces);
|
---|
9803 | if (RT_FAILURE(vrc))
|
---|
9804 | {
|
---|
9805 | if (vrc == VERR_VD_INVALID_TYPE)
|
---|
9806 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9807 | tr("Parameters for creating the differencing medium storage unit '%s' are invalid%s"),
|
---|
9808 | location.c_str(), i_vdError(vrc).c_str());
|
---|
9809 | else
|
---|
9810 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9811 | tr("Could not create the differencing medium storage unit '%s'%s"),
|
---|
9812 | location.c_str(), i_vdError(vrc).c_str());
|
---|
9813 | }
|
---|
9814 |
|
---|
9815 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
9816 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
9817 | unsigned uImageFlags;
|
---|
9818 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
9819 | if (RT_SUCCESS(vrc))
|
---|
9820 | variant = (MediumVariant_T)uImageFlags;
|
---|
9821 | }
|
---|
9822 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9823 |
|
---|
9824 | VDDestroy(hdd);
|
---|
9825 | }
|
---|
9826 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9827 |
|
---|
9828 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9829 |
|
---|
9830 | m->size = size;
|
---|
9831 | m->logicalSize = logicalSize;
|
---|
9832 | m->variant = variant;
|
---|
9833 |
|
---|
9834 | /* Everything is explicitly unlocked when the task exits,
|
---|
9835 | * as the task destruction also destroys the media chain. */
|
---|
9836 |
|
---|
9837 | return rc;
|
---|
9838 | }
|
---|
9839 |
|
---|
9840 | /**
|
---|
9841 | * Implementation code for the "compact" task.
|
---|
9842 | *
|
---|
9843 | * @param task
|
---|
9844 | * @return
|
---|
9845 | */
|
---|
9846 | HRESULT Medium::i_taskCompactHandler(Medium::CompactTask &task)
|
---|
9847 | {
|
---|
9848 | HRESULT rc = S_OK;
|
---|
9849 |
|
---|
9850 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
9851 | * signal from the task initiator (which releases it only after
|
---|
9852 | * RTThreadCreate()) that we can start the job. */
|
---|
9853 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9854 |
|
---|
9855 | try
|
---|
9856 | {
|
---|
9857 | PVDISK hdd;
|
---|
9858 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9859 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9860 |
|
---|
9861 | try
|
---|
9862 | {
|
---|
9863 | /* Open all media in the chain. */
|
---|
9864 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
9865 | task.mpMediumLockList->GetBegin();
|
---|
9866 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
9867 | task.mpMediumLockList->GetEnd();
|
---|
9868 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
9869 | mediumListEnd;
|
---|
9870 | --mediumListLast;
|
---|
9871 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
9872 | it != mediumListEnd;
|
---|
9873 | ++it)
|
---|
9874 | {
|
---|
9875 | const MediumLock &mediumLock = *it;
|
---|
9876 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9877 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9878 |
|
---|
9879 | /* sanity check */
|
---|
9880 | if (it == mediumListLast)
|
---|
9881 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
9882 | else
|
---|
9883 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
9884 |
|
---|
9885 | /* Open all media but last in read-only mode. Do not handle
|
---|
9886 | * shareable media, as compaction and sharing are mutually
|
---|
9887 | * exclusive. */
|
---|
9888 | vrc = VDOpen(hdd,
|
---|
9889 | pMedium->m->strFormat.c_str(),
|
---|
9890 | pMedium->m->strLocationFull.c_str(),
|
---|
9891 | m->uOpenFlagsDef | (it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
9892 | pMedium->m->vdImageIfaces);
|
---|
9893 | if (RT_FAILURE(vrc))
|
---|
9894 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9895 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9896 | pMedium->m->strLocationFull.c_str(),
|
---|
9897 | i_vdError(vrc).c_str());
|
---|
9898 | }
|
---|
9899 |
|
---|
9900 | Assert(m->state == MediumState_LockedWrite);
|
---|
9901 |
|
---|
9902 | Utf8Str location(m->strLocationFull);
|
---|
9903 |
|
---|
9904 | /* unlock before the potentially lengthy operation */
|
---|
9905 | thisLock.release();
|
---|
9906 |
|
---|
9907 | vrc = VDCompact(hdd, VD_LAST_IMAGE, task.mVDOperationIfaces);
|
---|
9908 | if (RT_FAILURE(vrc))
|
---|
9909 | {
|
---|
9910 | if (vrc == VERR_NOT_SUPPORTED)
|
---|
9911 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
9912 | tr("Compacting is not yet supported for medium '%s'"),
|
---|
9913 | location.c_str());
|
---|
9914 | else if (vrc == VERR_NOT_IMPLEMENTED)
|
---|
9915 | throw setErrorBoth(E_NOTIMPL, vrc,
|
---|
9916 | tr("Compacting is not implemented, medium '%s'"),
|
---|
9917 | location.c_str());
|
---|
9918 | else
|
---|
9919 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9920 | tr("Could not compact medium '%s'%s"),
|
---|
9921 | location.c_str(),
|
---|
9922 | i_vdError(vrc).c_str());
|
---|
9923 | }
|
---|
9924 | }
|
---|
9925 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9926 |
|
---|
9927 | VDDestroy(hdd);
|
---|
9928 | }
|
---|
9929 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9930 |
|
---|
9931 | /* Everything is explicitly unlocked when the task exits,
|
---|
9932 | * as the task destruction also destroys the media chain. */
|
---|
9933 |
|
---|
9934 | return rc;
|
---|
9935 | }
|
---|
9936 |
|
---|
9937 | /**
|
---|
9938 | * Implementation code for the "resize" task.
|
---|
9939 | *
|
---|
9940 | * @param task
|
---|
9941 | * @return
|
---|
9942 | */
|
---|
9943 | HRESULT Medium::i_taskResizeHandler(Medium::ResizeTask &task)
|
---|
9944 | {
|
---|
9945 | HRESULT rc = S_OK;
|
---|
9946 |
|
---|
9947 | uint64_t size = 0, logicalSize = 0;
|
---|
9948 |
|
---|
9949 | try
|
---|
9950 | {
|
---|
9951 | /* The lock is also used as a signal from the task initiator (which
|
---|
9952 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
9953 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9954 |
|
---|
9955 | PVDISK hdd;
|
---|
9956 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9957 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9958 |
|
---|
9959 | try
|
---|
9960 | {
|
---|
9961 | /* Open all media in the chain. */
|
---|
9962 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
9963 | task.mpMediumLockList->GetBegin();
|
---|
9964 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
9965 | task.mpMediumLockList->GetEnd();
|
---|
9966 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
9967 | mediumListEnd;
|
---|
9968 | --mediumListLast;
|
---|
9969 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
9970 | it != mediumListEnd;
|
---|
9971 | ++it)
|
---|
9972 | {
|
---|
9973 | const MediumLock &mediumLock = *it;
|
---|
9974 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9975 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9976 |
|
---|
9977 | /* sanity check */
|
---|
9978 | if (it == mediumListLast)
|
---|
9979 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
9980 | else
|
---|
9981 | Assert(pMedium->m->state == MediumState_LockedRead ||
|
---|
9982 | // Allow resize the target image during mergeTo in case
|
---|
9983 | // of direction from parent to child because all intermediate
|
---|
9984 | // images are marked to MediumState_Deleting and will be
|
---|
9985 | // destroyed after successful merge
|
---|
9986 | pMedium->m->state == MediumState_Deleting);
|
---|
9987 |
|
---|
9988 | /* Open all media but last in read-only mode. Do not handle
|
---|
9989 | * shareable media, as compaction and sharing are mutually
|
---|
9990 | * exclusive. */
|
---|
9991 | vrc = VDOpen(hdd,
|
---|
9992 | pMedium->m->strFormat.c_str(),
|
---|
9993 | pMedium->m->strLocationFull.c_str(),
|
---|
9994 | m->uOpenFlagsDef | (it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
9995 | pMedium->m->vdImageIfaces);
|
---|
9996 | if (RT_FAILURE(vrc))
|
---|
9997 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9998 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9999 | pMedium->m->strLocationFull.c_str(),
|
---|
10000 | i_vdError(vrc).c_str());
|
---|
10001 | }
|
---|
10002 |
|
---|
10003 | Assert(m->state == MediumState_LockedWrite);
|
---|
10004 |
|
---|
10005 | Utf8Str location(m->strLocationFull);
|
---|
10006 |
|
---|
10007 | /* unlock before the potentially lengthy operation */
|
---|
10008 | thisLock.release();
|
---|
10009 |
|
---|
10010 | VDGEOMETRY geo = {0, 0, 0}; /* auto */
|
---|
10011 | vrc = VDResize(hdd, task.mSize, &geo, &geo, task.mVDOperationIfaces);
|
---|
10012 | if (RT_FAILURE(vrc))
|
---|
10013 | {
|
---|
10014 | if (vrc == VERR_VD_SHRINK_NOT_SUPPORTED)
|
---|
10015 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
10016 | tr("Shrinking is not yet supported for medium '%s'"),
|
---|
10017 | location.c_str());
|
---|
10018 | if (vrc == VERR_NOT_SUPPORTED)
|
---|
10019 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
10020 | tr("Resizing to new size %llu is not yet supported for medium '%s'"),
|
---|
10021 | task.mSize, location.c_str());
|
---|
10022 | else if (vrc == VERR_NOT_IMPLEMENTED)
|
---|
10023 | throw setErrorBoth(E_NOTIMPL, vrc,
|
---|
10024 | tr("Resiting is not implemented, medium '%s'"),
|
---|
10025 | location.c_str());
|
---|
10026 | else
|
---|
10027 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10028 | tr("Could not resize medium '%s'%s"),
|
---|
10029 | location.c_str(),
|
---|
10030 | i_vdError(vrc).c_str());
|
---|
10031 | }
|
---|
10032 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
10033 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
10034 | }
|
---|
10035 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10036 |
|
---|
10037 | VDDestroy(hdd);
|
---|
10038 | }
|
---|
10039 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10040 |
|
---|
10041 | if (SUCCEEDED(rc))
|
---|
10042 | {
|
---|
10043 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
10044 | m->size = size;
|
---|
10045 | m->logicalSize = logicalSize;
|
---|
10046 | }
|
---|
10047 |
|
---|
10048 | /* Everything is explicitly unlocked when the task exits,
|
---|
10049 | * as the task destruction also destroys the media chain. */
|
---|
10050 |
|
---|
10051 | return rc;
|
---|
10052 | }
|
---|
10053 |
|
---|
10054 | /**
|
---|
10055 | * Implementation code for the "import" task.
|
---|
10056 | *
|
---|
10057 | * This only gets started from Medium::importFile() and always runs
|
---|
10058 | * asynchronously. It potentially touches the media registry, so we
|
---|
10059 | * always save the VirtualBox.xml file when we're done here.
|
---|
10060 | *
|
---|
10061 | * @param task
|
---|
10062 | * @return
|
---|
10063 | */
|
---|
10064 | HRESULT Medium::i_taskImportHandler(Medium::ImportTask &task)
|
---|
10065 | {
|
---|
10066 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
10067 | * to lock order violations, it probably causes lock order issues related
|
---|
10068 | * to the AutoCaller usage. */
|
---|
10069 | HRESULT rcTmp = S_OK;
|
---|
10070 |
|
---|
10071 | const ComObjPtr<Medium> &pParent = task.mParent;
|
---|
10072 |
|
---|
10073 | bool fCreatingTarget = false;
|
---|
10074 |
|
---|
10075 | uint64_t size = 0, logicalSize = 0;
|
---|
10076 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
10077 | bool fGenerateUuid = false;
|
---|
10078 |
|
---|
10079 | try
|
---|
10080 | {
|
---|
10081 | if (!pParent.isNull())
|
---|
10082 | if (pParent->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
10083 | {
|
---|
10084 | AutoReadLock plock(pParent COMMA_LOCKVAL_SRC_POS);
|
---|
10085 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10086 | tr("Cannot import image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
10087 | pParent->m->strLocationFull.c_str());
|
---|
10088 | }
|
---|
10089 |
|
---|
10090 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
10091 | * signal from the task initiator (which releases it only after
|
---|
10092 | * RTThreadCreate()) that we can start the job. */
|
---|
10093 | AutoMultiWriteLock2 thisLock(this, pParent COMMA_LOCKVAL_SRC_POS);
|
---|
10094 |
|
---|
10095 | fCreatingTarget = m->state == MediumState_Creating;
|
---|
10096 |
|
---|
10097 | /* The object may request a specific UUID (through a special form of
|
---|
10098 | * the moveTo() argument). Otherwise we have to generate it */
|
---|
10099 | Guid targetId = m->id;
|
---|
10100 |
|
---|
10101 | fGenerateUuid = targetId.isZero();
|
---|
10102 | if (fGenerateUuid)
|
---|
10103 | {
|
---|
10104 | targetId.create();
|
---|
10105 | /* VirtualBox::i_registerMedium() will need UUID */
|
---|
10106 | unconst(m->id) = targetId;
|
---|
10107 | }
|
---|
10108 |
|
---|
10109 |
|
---|
10110 | PVDISK hdd;
|
---|
10111 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
10112 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
10113 |
|
---|
10114 | try
|
---|
10115 | {
|
---|
10116 | /* Open source medium. */
|
---|
10117 | vrc = VDOpen(hdd,
|
---|
10118 | task.mFormat->i_getId().c_str(),
|
---|
10119 | task.mFilename.c_str(),
|
---|
10120 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SEQUENTIAL | m->uOpenFlagsDef,
|
---|
10121 | task.mVDImageIfaces);
|
---|
10122 | if (RT_FAILURE(vrc))
|
---|
10123 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10124 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
10125 | task.mFilename.c_str(),
|
---|
10126 | i_vdError(vrc).c_str());
|
---|
10127 |
|
---|
10128 | Utf8Str targetFormat(m->strFormat);
|
---|
10129 | Utf8Str targetLocation(m->strLocationFull);
|
---|
10130 | uint64_t capabilities = task.mFormat->i_getCapabilities();
|
---|
10131 |
|
---|
10132 | Assert( m->state == MediumState_Creating
|
---|
10133 | || m->state == MediumState_LockedWrite);
|
---|
10134 | Assert( pParent.isNull()
|
---|
10135 | || pParent->m->state == MediumState_LockedRead);
|
---|
10136 |
|
---|
10137 | /* unlock before the potentially lengthy operation */
|
---|
10138 | thisLock.release();
|
---|
10139 |
|
---|
10140 | /* ensure the target directory exists */
|
---|
10141 | if (capabilities & MediumFormatCapabilities_File)
|
---|
10142 | {
|
---|
10143 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
10144 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
10145 | if (FAILED(rc))
|
---|
10146 | throw rc;
|
---|
10147 | }
|
---|
10148 |
|
---|
10149 | PVDISK targetHdd;
|
---|
10150 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &targetHdd);
|
---|
10151 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
10152 |
|
---|
10153 | try
|
---|
10154 | {
|
---|
10155 | /* Open all media in the target chain. */
|
---|
10156 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
10157 | task.mpTargetMediumLockList->GetBegin();
|
---|
10158 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
10159 | task.mpTargetMediumLockList->GetEnd();
|
---|
10160 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
10161 | it != targetListEnd;
|
---|
10162 | ++it)
|
---|
10163 | {
|
---|
10164 | const MediumLock &mediumLock = *it;
|
---|
10165 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
10166 |
|
---|
10167 | /* If the target medium is not created yet there's no
|
---|
10168 | * reason to open it. */
|
---|
10169 | if (pMedium == this && fCreatingTarget)
|
---|
10170 | continue;
|
---|
10171 |
|
---|
10172 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
10173 |
|
---|
10174 | /* sanity check */
|
---|
10175 | Assert( pMedium->m->state == MediumState_LockedRead
|
---|
10176 | || pMedium->m->state == MediumState_LockedWrite);
|
---|
10177 |
|
---|
10178 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
10179 | if (pMedium->m->state != MediumState_LockedWrite)
|
---|
10180 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
10181 | if (pMedium->m->type == MediumType_Shareable)
|
---|
10182 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
10183 |
|
---|
10184 | /* Open all media in appropriate mode. */
|
---|
10185 | vrc = VDOpen(targetHdd,
|
---|
10186 | pMedium->m->strFormat.c_str(),
|
---|
10187 | pMedium->m->strLocationFull.c_str(),
|
---|
10188 | uOpenFlags | m->uOpenFlagsDef,
|
---|
10189 | pMedium->m->vdImageIfaces);
|
---|
10190 | if (RT_FAILURE(vrc))
|
---|
10191 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10192 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
10193 | pMedium->m->strLocationFull.c_str(),
|
---|
10194 | i_vdError(vrc).c_str());
|
---|
10195 | }
|
---|
10196 |
|
---|
10197 | vrc = VDCopy(hdd,
|
---|
10198 | VD_LAST_IMAGE,
|
---|
10199 | targetHdd,
|
---|
10200 | targetFormat.c_str(),
|
---|
10201 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
10202 | false /* fMoveByRename */,
|
---|
10203 | 0 /* cbSize */,
|
---|
10204 | task.mVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted),
|
---|
10205 | targetId.raw(),
|
---|
10206 | VD_OPEN_FLAGS_NORMAL,
|
---|
10207 | NULL /* pVDIfsOperation */,
|
---|
10208 | m->vdImageIfaces,
|
---|
10209 | task.mVDOperationIfaces);
|
---|
10210 | if (RT_FAILURE(vrc))
|
---|
10211 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10212 | tr("Could not create the imported medium '%s'%s"),
|
---|
10213 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
10214 |
|
---|
10215 | size = VDGetFileSize(targetHdd, VD_LAST_IMAGE);
|
---|
10216 | logicalSize = VDGetSize(targetHdd, VD_LAST_IMAGE);
|
---|
10217 | unsigned uImageFlags;
|
---|
10218 | vrc = VDGetImageFlags(targetHdd, 0, &uImageFlags);
|
---|
10219 | if (RT_SUCCESS(vrc))
|
---|
10220 | variant = (MediumVariant_T)uImageFlags;
|
---|
10221 | }
|
---|
10222 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
10223 |
|
---|
10224 | VDDestroy(targetHdd);
|
---|
10225 | }
|
---|
10226 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
10227 |
|
---|
10228 | VDDestroy(hdd);
|
---|
10229 | }
|
---|
10230 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
10231 |
|
---|
10232 | ErrorInfoKeeper eik;
|
---|
10233 | MultiResult mrc(rcTmp);
|
---|
10234 |
|
---|
10235 | /* Only do the parent changes for newly created media. */
|
---|
10236 | if (SUCCEEDED(mrc) && fCreatingTarget)
|
---|
10237 | {
|
---|
10238 | /* we set m->pParent & children() */
|
---|
10239 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
10240 |
|
---|
10241 | Assert(m->pParent.isNull());
|
---|
10242 |
|
---|
10243 | if (pParent)
|
---|
10244 | {
|
---|
10245 | /* Associate the imported medium with the parent and deassociate
|
---|
10246 | * from VirtualBox. Depth check above. */
|
---|
10247 | i_setParent(pParent);
|
---|
10248 |
|
---|
10249 | /* register with mVirtualBox as the last step and move to
|
---|
10250 | * Created state only on success (leaving an orphan file is
|
---|
10251 | * better than breaking media registry consistency) */
|
---|
10252 | eik.restore();
|
---|
10253 | ComObjPtr<Medium> pMedium;
|
---|
10254 | mrc = pParent->m->pVirtualBox->i_registerMedium(this, &pMedium,
|
---|
10255 | treeLock);
|
---|
10256 | Assert(this == pMedium);
|
---|
10257 | eik.fetch();
|
---|
10258 |
|
---|
10259 | if (FAILED(mrc))
|
---|
10260 | /* break parent association on failure to register */
|
---|
10261 | this->i_deparent(); // removes target from parent
|
---|
10262 | }
|
---|
10263 | else
|
---|
10264 | {
|
---|
10265 | /* just register */
|
---|
10266 | eik.restore();
|
---|
10267 | ComObjPtr<Medium> pMedium;
|
---|
10268 | mrc = m->pVirtualBox->i_registerMedium(this, &pMedium, treeLock);
|
---|
10269 | Assert(this == pMedium);
|
---|
10270 | eik.fetch();
|
---|
10271 | }
|
---|
10272 | }
|
---|
10273 |
|
---|
10274 | if (fCreatingTarget)
|
---|
10275 | {
|
---|
10276 | AutoWriteLock mLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
10277 |
|
---|
10278 | if (SUCCEEDED(mrc))
|
---|
10279 | {
|
---|
10280 | m->state = MediumState_Created;
|
---|
10281 |
|
---|
10282 | m->size = size;
|
---|
10283 | m->logicalSize = logicalSize;
|
---|
10284 | m->variant = variant;
|
---|
10285 | }
|
---|
10286 | else
|
---|
10287 | {
|
---|
10288 | /* back to NotCreated on failure */
|
---|
10289 | m->state = MediumState_NotCreated;
|
---|
10290 |
|
---|
10291 | /* reset UUID to prevent it from being reused next time */
|
---|
10292 | if (fGenerateUuid)
|
---|
10293 | unconst(m->id).clear();
|
---|
10294 | }
|
---|
10295 | }
|
---|
10296 |
|
---|
10297 | // now, at the end of this task (always asynchronous), save the settings
|
---|
10298 | {
|
---|
10299 | // save the settings
|
---|
10300 | i_markRegistriesModified();
|
---|
10301 | /* collect multiple errors */
|
---|
10302 | eik.restore();
|
---|
10303 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
10304 | eik.fetch();
|
---|
10305 | }
|
---|
10306 |
|
---|
10307 | /* Everything is explicitly unlocked when the task exits,
|
---|
10308 | * as the task destruction also destroys the target chain. */
|
---|
10309 |
|
---|
10310 | /* Make sure the target chain is released early, otherwise it can
|
---|
10311 | * lead to deadlocks with concurrent IAppliance activities. */
|
---|
10312 | task.mpTargetMediumLockList->Clear();
|
---|
10313 |
|
---|
10314 | return mrc;
|
---|
10315 | }
|
---|
10316 |
|
---|
10317 | /**
|
---|
10318 | * Sets up the encryption settings for a filter.
|
---|
10319 | */
|
---|
10320 | void Medium::i_taskEncryptSettingsSetup(MediumCryptoFilterSettings *pSettings, const char *pszCipher,
|
---|
10321 | const char *pszKeyStore, const char *pszPassword,
|
---|
10322 | bool fCreateKeyStore)
|
---|
10323 | {
|
---|
10324 | pSettings->pszCipher = pszCipher;
|
---|
10325 | pSettings->pszPassword = pszPassword;
|
---|
10326 | pSettings->pszKeyStoreLoad = pszKeyStore;
|
---|
10327 | pSettings->fCreateKeyStore = fCreateKeyStore;
|
---|
10328 | pSettings->pbDek = NULL;
|
---|
10329 | pSettings->cbDek = 0;
|
---|
10330 | pSettings->vdFilterIfaces = NULL;
|
---|
10331 |
|
---|
10332 | pSettings->vdIfCfg.pfnAreKeysValid = i_vdCryptoConfigAreKeysValid;
|
---|
10333 | pSettings->vdIfCfg.pfnQuerySize = i_vdCryptoConfigQuerySize;
|
---|
10334 | pSettings->vdIfCfg.pfnQuery = i_vdCryptoConfigQuery;
|
---|
10335 | pSettings->vdIfCfg.pfnQueryBytes = NULL;
|
---|
10336 |
|
---|
10337 | pSettings->vdIfCrypto.pfnKeyRetain = i_vdCryptoKeyRetain;
|
---|
10338 | pSettings->vdIfCrypto.pfnKeyRelease = i_vdCryptoKeyRelease;
|
---|
10339 | pSettings->vdIfCrypto.pfnKeyStorePasswordRetain = i_vdCryptoKeyStorePasswordRetain;
|
---|
10340 | pSettings->vdIfCrypto.pfnKeyStorePasswordRelease = i_vdCryptoKeyStorePasswordRelease;
|
---|
10341 | pSettings->vdIfCrypto.pfnKeyStoreSave = i_vdCryptoKeyStoreSave;
|
---|
10342 | pSettings->vdIfCrypto.pfnKeyStoreReturnParameters = i_vdCryptoKeyStoreReturnParameters;
|
---|
10343 |
|
---|
10344 | int vrc = VDInterfaceAdd(&pSettings->vdIfCfg.Core,
|
---|
10345 | "Medium::vdInterfaceCfgCrypto",
|
---|
10346 | VDINTERFACETYPE_CONFIG, pSettings,
|
---|
10347 | sizeof(VDINTERFACECONFIG), &pSettings->vdFilterIfaces);
|
---|
10348 | AssertRC(vrc);
|
---|
10349 |
|
---|
10350 | vrc = VDInterfaceAdd(&pSettings->vdIfCrypto.Core,
|
---|
10351 | "Medium::vdInterfaceCrypto",
|
---|
10352 | VDINTERFACETYPE_CRYPTO, pSettings,
|
---|
10353 | sizeof(VDINTERFACECRYPTO), &pSettings->vdFilterIfaces);
|
---|
10354 | AssertRC(vrc);
|
---|
10355 | }
|
---|
10356 |
|
---|
10357 | /**
|
---|
10358 | * Implementation code for the "encrypt" task.
|
---|
10359 | *
|
---|
10360 | * @param task
|
---|
10361 | * @return
|
---|
10362 | */
|
---|
10363 | HRESULT Medium::i_taskEncryptHandler(Medium::EncryptTask &task)
|
---|
10364 | {
|
---|
10365 | # ifndef VBOX_WITH_EXTPACK
|
---|
10366 | RT_NOREF(task);
|
---|
10367 | # endif
|
---|
10368 | HRESULT rc = S_OK;
|
---|
10369 |
|
---|
10370 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
10371 | * signal from the task initiator (which releases it only after
|
---|
10372 | * RTThreadCreate()) that we can start the job. */
|
---|
10373 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
10374 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
10375 |
|
---|
10376 | try
|
---|
10377 | {
|
---|
10378 | # ifdef VBOX_WITH_EXTPACK
|
---|
10379 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
10380 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
10381 | {
|
---|
10382 | /* Load the plugin */
|
---|
10383 | Utf8Str strPlugin;
|
---|
10384 | rc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
10385 | if (SUCCEEDED(rc))
|
---|
10386 | {
|
---|
10387 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
10388 | if (RT_FAILURE(vrc))
|
---|
10389 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
10390 | tr("Encrypting the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
10391 | i_vdError(vrc).c_str());
|
---|
10392 | }
|
---|
10393 | else
|
---|
10394 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
10395 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
10396 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
10397 | }
|
---|
10398 | else
|
---|
10399 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
10400 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
10401 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
10402 |
|
---|
10403 | PVDISK pDisk = NULL;
|
---|
10404 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
10405 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
10406 |
|
---|
10407 | MediumCryptoFilterSettings CryptoSettingsRead;
|
---|
10408 | MediumCryptoFilterSettings CryptoSettingsWrite;
|
---|
10409 |
|
---|
10410 | void *pvBuf = NULL;
|
---|
10411 | const char *pszPasswordNew = NULL;
|
---|
10412 | try
|
---|
10413 | {
|
---|
10414 | /* Set up disk encryption filters. */
|
---|
10415 | if (task.mstrCurrentPassword.isEmpty())
|
---|
10416 | {
|
---|
10417 | /*
|
---|
10418 | * Query whether the medium property indicating that encryption is
|
---|
10419 | * configured is existing.
|
---|
10420 | */
|
---|
10421 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
10422 | if (it != pBase->m->mapProperties.end())
|
---|
10423 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
10424 | tr("The password given for the encrypted image is incorrect"));
|
---|
10425 | }
|
---|
10426 | else
|
---|
10427 | {
|
---|
10428 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
10429 | if (it == pBase->m->mapProperties.end())
|
---|
10430 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10431 | tr("The image is not configured for encryption"));
|
---|
10432 |
|
---|
10433 | i_taskEncryptSettingsSetup(&CryptoSettingsRead, NULL, it->second.c_str(), task.mstrCurrentPassword.c_str(),
|
---|
10434 | false /* fCreateKeyStore */);
|
---|
10435 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ, CryptoSettingsRead.vdFilterIfaces);
|
---|
10436 | if (vrc == VERR_VD_PASSWORD_INCORRECT)
|
---|
10437 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
10438 | tr("The password to decrypt the image is incorrect"));
|
---|
10439 | else if (RT_FAILURE(vrc))
|
---|
10440 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10441 | tr("Failed to load the decryption filter: %s"),
|
---|
10442 | i_vdError(vrc).c_str());
|
---|
10443 | }
|
---|
10444 |
|
---|
10445 | if (task.mstrCipher.isNotEmpty())
|
---|
10446 | {
|
---|
10447 | if ( task.mstrNewPassword.isEmpty()
|
---|
10448 | && task.mstrNewPasswordId.isEmpty()
|
---|
10449 | && task.mstrCurrentPassword.isNotEmpty())
|
---|
10450 | {
|
---|
10451 | /* An empty password and password ID will default to the current password. */
|
---|
10452 | pszPasswordNew = task.mstrCurrentPassword.c_str();
|
---|
10453 | }
|
---|
10454 | else if (task.mstrNewPassword.isEmpty())
|
---|
10455 | throw setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
10456 | tr("A password must be given for the image encryption"));
|
---|
10457 | else if (task.mstrNewPasswordId.isEmpty())
|
---|
10458 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10459 | tr("A valid identifier for the password must be given"));
|
---|
10460 | else
|
---|
10461 | pszPasswordNew = task.mstrNewPassword.c_str();
|
---|
10462 |
|
---|
10463 | i_taskEncryptSettingsSetup(&CryptoSettingsWrite, task.mstrCipher.c_str(), NULL,
|
---|
10464 | pszPasswordNew, true /* fCreateKeyStore */);
|
---|
10465 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_WRITE, CryptoSettingsWrite.vdFilterIfaces);
|
---|
10466 | if (RT_FAILURE(vrc))
|
---|
10467 | throw setErrorBoth(VBOX_E_INVALID_OBJECT_STATE, vrc,
|
---|
10468 | tr("Failed to load the encryption filter: %s"),
|
---|
10469 | i_vdError(vrc).c_str());
|
---|
10470 | }
|
---|
10471 | else if (task.mstrNewPasswordId.isNotEmpty() || task.mstrNewPassword.isNotEmpty())
|
---|
10472 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10473 | tr("The password and password identifier must be empty if the output should be unencrypted"));
|
---|
10474 |
|
---|
10475 | /* Open all media in the chain. */
|
---|
10476 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
10477 | task.mpMediumLockList->GetBegin();
|
---|
10478 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
10479 | task.mpMediumLockList->GetEnd();
|
---|
10480 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
10481 | mediumListEnd;
|
---|
10482 | --mediumListLast;
|
---|
10483 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
10484 | it != mediumListEnd;
|
---|
10485 | ++it)
|
---|
10486 | {
|
---|
10487 | const MediumLock &mediumLock = *it;
|
---|
10488 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
10489 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
10490 |
|
---|
10491 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
10492 |
|
---|
10493 | /* Open all media but last in read-only mode. Do not handle
|
---|
10494 | * shareable media, as compaction and sharing are mutually
|
---|
10495 | * exclusive. */
|
---|
10496 | vrc = VDOpen(pDisk,
|
---|
10497 | pMedium->m->strFormat.c_str(),
|
---|
10498 | pMedium->m->strLocationFull.c_str(),
|
---|
10499 | m->uOpenFlagsDef | (it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
10500 | pMedium->m->vdImageIfaces);
|
---|
10501 | if (RT_FAILURE(vrc))
|
---|
10502 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10503 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
10504 | pMedium->m->strLocationFull.c_str(),
|
---|
10505 | i_vdError(vrc).c_str());
|
---|
10506 | }
|
---|
10507 |
|
---|
10508 | Assert(m->state == MediumState_LockedWrite);
|
---|
10509 |
|
---|
10510 | Utf8Str location(m->strLocationFull);
|
---|
10511 |
|
---|
10512 | /* unlock before the potentially lengthy operation */
|
---|
10513 | thisLock.release();
|
---|
10514 |
|
---|
10515 | vrc = VDPrepareWithFilters(pDisk, task.mVDOperationIfaces);
|
---|
10516 | if (RT_FAILURE(vrc))
|
---|
10517 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10518 | tr("Could not prepare disk images for encryption (%Rrc): %s"),
|
---|
10519 | vrc, i_vdError(vrc).c_str());
|
---|
10520 |
|
---|
10521 | thisLock.acquire();
|
---|
10522 | /* If everything went well set the new key store. */
|
---|
10523 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
10524 | if (it != pBase->m->mapProperties.end())
|
---|
10525 | pBase->m->mapProperties.erase(it);
|
---|
10526 |
|
---|
10527 | /* Delete KeyId if encryption is removed or the password did change. */
|
---|
10528 | if ( task.mstrNewPasswordId.isNotEmpty()
|
---|
10529 | || task.mstrCipher.isEmpty())
|
---|
10530 | {
|
---|
10531 | it = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
10532 | if (it != pBase->m->mapProperties.end())
|
---|
10533 | pBase->m->mapProperties.erase(it);
|
---|
10534 | }
|
---|
10535 |
|
---|
10536 | if (CryptoSettingsWrite.pszKeyStore)
|
---|
10537 | {
|
---|
10538 | pBase->m->mapProperties["CRYPT/KeyStore"] = Utf8Str(CryptoSettingsWrite.pszKeyStore);
|
---|
10539 | if (task.mstrNewPasswordId.isNotEmpty())
|
---|
10540 | pBase->m->mapProperties["CRYPT/KeyId"] = task.mstrNewPasswordId;
|
---|
10541 | }
|
---|
10542 |
|
---|
10543 | if (CryptoSettingsRead.pszCipherReturned)
|
---|
10544 | RTStrFree(CryptoSettingsRead.pszCipherReturned);
|
---|
10545 |
|
---|
10546 | if (CryptoSettingsWrite.pszCipherReturned)
|
---|
10547 | RTStrFree(CryptoSettingsWrite.pszCipherReturned);
|
---|
10548 |
|
---|
10549 | thisLock.release();
|
---|
10550 | pBase->i_markRegistriesModified();
|
---|
10551 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
10552 | }
|
---|
10553 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10554 |
|
---|
10555 | if (pvBuf)
|
---|
10556 | RTMemFree(pvBuf);
|
---|
10557 |
|
---|
10558 | VDDestroy(pDisk);
|
---|
10559 | # else
|
---|
10560 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
10561 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
10562 | # endif
|
---|
10563 | }
|
---|
10564 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10565 |
|
---|
10566 | /* Everything is explicitly unlocked when the task exits,
|
---|
10567 | * as the task destruction also destroys the media chain. */
|
---|
10568 |
|
---|
10569 | return rc;
|
---|
10570 | }
|
---|
10571 |
|
---|
10572 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|