1 | /* $Id: MediumImpl.cpp 98263 2023-01-24 01:52:49Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox COM class implementation
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #define LOG_GROUP LOG_GROUP_MAIN_MEDIUM
|
---|
29 | #include "MediumImpl.h"
|
---|
30 | #include "MediumIOImpl.h"
|
---|
31 | #include "TokenImpl.h"
|
---|
32 | #include "ProgressImpl.h"
|
---|
33 | #include "SystemPropertiesImpl.h"
|
---|
34 | #include "VirtualBoxImpl.h"
|
---|
35 | #include "ExtPackManagerImpl.h"
|
---|
36 |
|
---|
37 | #include "AutoCaller.h"
|
---|
38 | #include "Global.h"
|
---|
39 | #include "LoggingNew.h"
|
---|
40 | #include "ThreadTask.h"
|
---|
41 | #include "VBox/com/MultiResult.h"
|
---|
42 | #include "VBox/com/ErrorInfo.h"
|
---|
43 |
|
---|
44 | #include <VBox/err.h>
|
---|
45 | #include <VBox/settings.h>
|
---|
46 |
|
---|
47 | #include <iprt/param.h>
|
---|
48 | #include <iprt/path.h>
|
---|
49 | #include <iprt/file.h>
|
---|
50 | #include <iprt/cpp/utils.h>
|
---|
51 | #include <iprt/memsafer.h>
|
---|
52 | #include <iprt/base64.h>
|
---|
53 | #include <iprt/vfs.h>
|
---|
54 | #include <iprt/fsvfs.h>
|
---|
55 |
|
---|
56 | #include <VBox/vd.h>
|
---|
57 |
|
---|
58 | #include <algorithm>
|
---|
59 | #include <list>
|
---|
60 | #include <set>
|
---|
61 | #include <map>
|
---|
62 |
|
---|
63 |
|
---|
64 | typedef std::list<Guid> GuidList;
|
---|
65 |
|
---|
66 |
|
---|
67 | #ifdef VBOX_WITH_EXTPACK
|
---|
68 | static const char g_szVDPlugin[] = "VDPluginCrypt";
|
---|
69 | #endif
|
---|
70 |
|
---|
71 |
|
---|
72 | ////////////////////////////////////////////////////////////////////////////////
|
---|
73 | //
|
---|
74 | // Medium data definition
|
---|
75 | //
|
---|
76 | ////////////////////////////////////////////////////////////////////////////////
|
---|
77 | #if __cplusplus < 201700 && RT_GNUC_PREREQ(11,0) /* gcc/libstdc++ 12.1.1 errors out here because unary_function is deprecated */
|
---|
78 | # pragma GCC diagnostic push
|
---|
79 | # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
---|
80 | #endif
|
---|
81 |
|
---|
82 | struct SnapshotRef
|
---|
83 | {
|
---|
84 | /** Equality predicate for stdc++. */
|
---|
85 | struct EqualsTo
|
---|
86 | #if __cplusplus < 201700 /* deprecated in C++11, removed in C++17. */
|
---|
87 | : public std::unary_function <SnapshotRef, bool>
|
---|
88 | #endif
|
---|
89 | {
|
---|
90 | explicit EqualsTo(const Guid &aSnapshotId) : snapshotId(aSnapshotId) {}
|
---|
91 |
|
---|
92 | #if __cplusplus < 201700 /* deprecated in C++11, removed in C++17. */
|
---|
93 | bool operator()(const argument_type &aThat) const
|
---|
94 | #else
|
---|
95 | bool operator()(const SnapshotRef &aThat) const
|
---|
96 | #endif
|
---|
97 | {
|
---|
98 | return aThat.snapshotId == snapshotId;
|
---|
99 | }
|
---|
100 |
|
---|
101 | const Guid snapshotId;
|
---|
102 | };
|
---|
103 |
|
---|
104 | SnapshotRef(const Guid &aSnapshotId,
|
---|
105 | const int &aRefCnt = 1)
|
---|
106 | : snapshotId(aSnapshotId),
|
---|
107 | iRefCnt(aRefCnt) {}
|
---|
108 |
|
---|
109 | Guid snapshotId;
|
---|
110 | /*
|
---|
111 | * The number of attachments of the medium in the same snapshot.
|
---|
112 | * Used for MediumType_Readonly. It is always equal to 1 for other types.
|
---|
113 | * Usual int is used because any changes in the BackRef are guarded by
|
---|
114 | * AutoWriteLock.
|
---|
115 | */
|
---|
116 | int iRefCnt;
|
---|
117 | };
|
---|
118 |
|
---|
119 | /** Describes how a machine refers to this medium. */
|
---|
120 | struct BackRef
|
---|
121 | {
|
---|
122 | /** Equality predicate for stdc++. */
|
---|
123 | struct EqualsTo
|
---|
124 | #if __cplusplus < 201700 /* deprecated in C++11, removed in C++17. */
|
---|
125 | : public std::unary_function <BackRef, bool>
|
---|
126 | #endif
|
---|
127 | {
|
---|
128 | explicit EqualsTo(const Guid &aMachineId) : machineId(aMachineId) {}
|
---|
129 |
|
---|
130 | #if __cplusplus < 201700 /* deprecated in C++11, removed in C++17. */
|
---|
131 | bool operator()(const argument_type &aThat) const
|
---|
132 | #else
|
---|
133 | bool operator()(const BackRef &aThat) const
|
---|
134 | #endif
|
---|
135 | {
|
---|
136 | return aThat.machineId == machineId;
|
---|
137 | }
|
---|
138 |
|
---|
139 | const Guid machineId;
|
---|
140 | };
|
---|
141 |
|
---|
142 | BackRef(const Guid &aMachineId,
|
---|
143 | const Guid &aSnapshotId = Guid::Empty)
|
---|
144 | : machineId(aMachineId),
|
---|
145 | iRefCnt(1),
|
---|
146 | fInCurState(aSnapshotId.isZero())
|
---|
147 | {
|
---|
148 | if (aSnapshotId.isValid() && !aSnapshotId.isZero())
|
---|
149 | llSnapshotIds.push_back(SnapshotRef(aSnapshotId));
|
---|
150 | }
|
---|
151 |
|
---|
152 | Guid machineId;
|
---|
153 | /*
|
---|
154 | * The number of attachments of the medium in the same machine.
|
---|
155 | * Used for MediumType_Readonly. It is always equal to 1 for other types.
|
---|
156 | * Usual int is used because any changes in the BackRef are guarded by
|
---|
157 | * AutoWriteLock.
|
---|
158 | */
|
---|
159 | int iRefCnt;
|
---|
160 | bool fInCurState : 1;
|
---|
161 | std::list<SnapshotRef> llSnapshotIds;
|
---|
162 | };
|
---|
163 |
|
---|
164 | typedef std::list<BackRef> BackRefList;
|
---|
165 |
|
---|
166 | #if __cplusplus < 201700 && RT_GNUC_PREREQ(11,0)
|
---|
167 | # pragma GCC diagnostic pop
|
---|
168 | #endif
|
---|
169 |
|
---|
170 |
|
---|
171 | struct Medium::Data
|
---|
172 | {
|
---|
173 | Data()
|
---|
174 | : pVirtualBox(NULL),
|
---|
175 | state(MediumState_NotCreated),
|
---|
176 | variant(MediumVariant_Standard),
|
---|
177 | size(0),
|
---|
178 | readers(0),
|
---|
179 | preLockState(MediumState_NotCreated),
|
---|
180 | queryInfoSem(LOCKCLASS_MEDIUMQUERY),
|
---|
181 | queryInfoRunning(false),
|
---|
182 | type(MediumType_Normal),
|
---|
183 | devType(DeviceType_HardDisk),
|
---|
184 | logicalSize(0),
|
---|
185 | hddOpenMode(OpenReadWrite),
|
---|
186 | autoReset(false),
|
---|
187 | hostDrive(false),
|
---|
188 | implicit(false),
|
---|
189 | fClosing(false),
|
---|
190 | uOpenFlagsDef(VD_OPEN_FLAGS_IGNORE_FLUSH),
|
---|
191 | numCreateDiffTasks(0),
|
---|
192 | vdDiskIfaces(NULL),
|
---|
193 | vdImageIfaces(NULL),
|
---|
194 | fMoveThisMedium(false)
|
---|
195 | { }
|
---|
196 |
|
---|
197 | /** weak VirtualBox parent */
|
---|
198 | VirtualBox * const pVirtualBox;
|
---|
199 |
|
---|
200 | // pParent and llChildren are protected by VirtualBox::i_getMediaTreeLockHandle()
|
---|
201 | ComObjPtr<Medium> pParent;
|
---|
202 | MediaList llChildren; // to add a child, just call push_back; to remove
|
---|
203 | // a child, call child->deparent() which does a lookup
|
---|
204 |
|
---|
205 | GuidList llRegistryIDs; // media registries in which this medium is listed
|
---|
206 |
|
---|
207 | const Guid id;
|
---|
208 | Utf8Str strDescription;
|
---|
209 | MediumState_T state;
|
---|
210 | MediumVariant_T variant;
|
---|
211 | Utf8Str strLocationFull;
|
---|
212 | uint64_t size;
|
---|
213 | Utf8Str strLastAccessError;
|
---|
214 |
|
---|
215 | BackRefList backRefs;
|
---|
216 |
|
---|
217 | size_t readers;
|
---|
218 | MediumState_T preLockState;
|
---|
219 |
|
---|
220 | /** Special synchronization for operations which must wait for
|
---|
221 | * Medium::i_queryInfo in another thread to complete. Using a SemRW is
|
---|
222 | * not quite ideal, but at least it is subject to the lock validator,
|
---|
223 | * unlike the SemEventMulti which we had here for many years. Catching
|
---|
224 | * possible deadlocks is more important than a tiny bit of efficiency. */
|
---|
225 | RWLockHandle queryInfoSem;
|
---|
226 | bool queryInfoRunning : 1;
|
---|
227 |
|
---|
228 | const Utf8Str strFormat;
|
---|
229 | ComObjPtr<MediumFormat> formatObj;
|
---|
230 |
|
---|
231 | MediumType_T type;
|
---|
232 | DeviceType_T devType;
|
---|
233 | uint64_t logicalSize;
|
---|
234 |
|
---|
235 | HDDOpenMode hddOpenMode;
|
---|
236 |
|
---|
237 | bool autoReset : 1;
|
---|
238 |
|
---|
239 | /** New UUID to be set on the next Medium::i_queryInfo call. */
|
---|
240 | const Guid uuidImage;
|
---|
241 | /** New parent UUID to be set on the next Medium::i_queryInfo call. */
|
---|
242 | const Guid uuidParentImage;
|
---|
243 |
|
---|
244 | /** @todo r=bird: the boolean bitfields are pointless if they're not grouped! */
|
---|
245 | bool hostDrive : 1;
|
---|
246 |
|
---|
247 | settings::StringsMap mapProperties;
|
---|
248 |
|
---|
249 | bool implicit : 1;
|
---|
250 | /** Flag whether the medium is in the process of being closed. */
|
---|
251 | bool fClosing: 1;
|
---|
252 |
|
---|
253 | /** Default flags passed to VDOpen(). */
|
---|
254 | unsigned uOpenFlagsDef;
|
---|
255 |
|
---|
256 | uint32_t numCreateDiffTasks;
|
---|
257 |
|
---|
258 | Utf8Str vdError; /*< Error remembered by the VD error callback. */
|
---|
259 |
|
---|
260 | VDINTERFACEERROR vdIfError;
|
---|
261 |
|
---|
262 | VDINTERFACECONFIG vdIfConfig;
|
---|
263 |
|
---|
264 | /** The handle to the default VD TCP/IP interface. */
|
---|
265 | VDIFINST hTcpNetInst;
|
---|
266 |
|
---|
267 | PVDINTERFACE vdDiskIfaces;
|
---|
268 | PVDINTERFACE vdImageIfaces;
|
---|
269 |
|
---|
270 | /** Flag if the medium is going to move to a new
|
---|
271 | * location. */
|
---|
272 | bool fMoveThisMedium;
|
---|
273 | /** new location path */
|
---|
274 | Utf8Str strNewLocationFull;
|
---|
275 | };
|
---|
276 |
|
---|
277 | typedef struct VDSOCKETINT
|
---|
278 | {
|
---|
279 | /** Socket handle. */
|
---|
280 | RTSOCKET hSocket;
|
---|
281 | } VDSOCKETINT, *PVDSOCKETINT;
|
---|
282 |
|
---|
283 | ////////////////////////////////////////////////////////////////////////////////
|
---|
284 | //
|
---|
285 | // Globals
|
---|
286 | //
|
---|
287 | ////////////////////////////////////////////////////////////////////////////////
|
---|
288 |
|
---|
289 | /**
|
---|
290 | * Medium::Task class for asynchronous operations.
|
---|
291 | *
|
---|
292 | * @note Instances of this class must be created using new() because the
|
---|
293 | * task thread function will delete them when the task is complete.
|
---|
294 | *
|
---|
295 | * @note The constructor of this class adds a caller on the managed Medium
|
---|
296 | * object which is automatically released upon destruction.
|
---|
297 | */
|
---|
298 | class Medium::Task : public ThreadTask
|
---|
299 | {
|
---|
300 | public:
|
---|
301 | Task(Medium *aMedium, Progress *aProgress, bool fNotifyAboutChanges = true)
|
---|
302 | : ThreadTask("Medium::Task"),
|
---|
303 | mVDOperationIfaces(NULL),
|
---|
304 | mMedium(aMedium),
|
---|
305 | mMediumCaller(aMedium),
|
---|
306 | mProgress(aProgress),
|
---|
307 | mVirtualBoxCaller(NULL),
|
---|
308 | mNotifyAboutChanges(fNotifyAboutChanges)
|
---|
309 | {
|
---|
310 | AssertReturnVoidStmt(aMedium, mRC = E_FAIL);
|
---|
311 | mRC = mMediumCaller.hrc();
|
---|
312 | if (FAILED(mRC))
|
---|
313 | return;
|
---|
314 |
|
---|
315 | /* Get strong VirtualBox reference, see below. */
|
---|
316 | VirtualBox *pVirtualBox = aMedium->m->pVirtualBox;
|
---|
317 | mVirtualBox = pVirtualBox;
|
---|
318 | mVirtualBoxCaller.attach(pVirtualBox);
|
---|
319 | mRC = mVirtualBoxCaller.hrc();
|
---|
320 | if (FAILED(mRC))
|
---|
321 | return;
|
---|
322 |
|
---|
323 | /* Set up a per-operation progress interface, can be used freely (for
|
---|
324 | * binary operations you can use it either on the source or target). */
|
---|
325 | if (mProgress)
|
---|
326 | {
|
---|
327 | mVDIfProgress.pfnProgress = aProgress->i_vdProgressCallback;
|
---|
328 | int vrc = VDInterfaceAdd(&mVDIfProgress.Core,
|
---|
329 | "Medium::Task::vdInterfaceProgress",
|
---|
330 | VDINTERFACETYPE_PROGRESS,
|
---|
331 | mProgress,
|
---|
332 | sizeof(mVDIfProgress),
|
---|
333 | &mVDOperationIfaces);
|
---|
334 | AssertRC(vrc);
|
---|
335 | if (RT_FAILURE(vrc))
|
---|
336 | mRC = E_FAIL;
|
---|
337 | }
|
---|
338 | }
|
---|
339 |
|
---|
340 | // Make all destructors virtual. Just in case.
|
---|
341 | virtual ~Task()
|
---|
342 | {
|
---|
343 | /* send the notification of completion.*/
|
---|
344 | if ( isAsync()
|
---|
345 | && !mProgress.isNull())
|
---|
346 | mProgress->i_notifyComplete(mRC);
|
---|
347 | }
|
---|
348 |
|
---|
349 | HRESULT hrc() const { return mRC; }
|
---|
350 | bool isOk() const { return SUCCEEDED(hrc()); }
|
---|
351 | bool NotifyAboutChanges() const { return mNotifyAboutChanges; }
|
---|
352 |
|
---|
353 | const ComPtr<Progress>& GetProgressObject() const {return mProgress;}
|
---|
354 |
|
---|
355 | /**
|
---|
356 | * Runs Medium::Task::executeTask() on the current thread
|
---|
357 | * instead of creating a new one.
|
---|
358 | */
|
---|
359 | HRESULT runNow()
|
---|
360 | {
|
---|
361 | LogFlowFuncEnter();
|
---|
362 |
|
---|
363 | mRC = executeTask();
|
---|
364 |
|
---|
365 | LogFlowFunc(("rc=%Rhrc\n", mRC));
|
---|
366 | LogFlowFuncLeave();
|
---|
367 | return mRC;
|
---|
368 | }
|
---|
369 |
|
---|
370 | /**
|
---|
371 | * Implementation code for the "create base" task.
|
---|
372 | * Used as function for execution from a standalone thread.
|
---|
373 | */
|
---|
374 | void handler()
|
---|
375 | {
|
---|
376 | LogFlowFuncEnter();
|
---|
377 | try
|
---|
378 | {
|
---|
379 | mRC = executeTask(); /* (destructor picks up mRC, see above) */
|
---|
380 | LogFlowFunc(("rc=%Rhrc\n", mRC));
|
---|
381 | }
|
---|
382 | catch (...)
|
---|
383 | {
|
---|
384 | LogRel(("Some exception in the function Medium::Task:handler()\n"));
|
---|
385 | }
|
---|
386 |
|
---|
387 | LogFlowFuncLeave();
|
---|
388 | }
|
---|
389 |
|
---|
390 | PVDINTERFACE mVDOperationIfaces;
|
---|
391 |
|
---|
392 | const ComObjPtr<Medium> mMedium;
|
---|
393 | AutoCaller mMediumCaller;
|
---|
394 |
|
---|
395 | protected:
|
---|
396 | HRESULT mRC;
|
---|
397 |
|
---|
398 | private:
|
---|
399 | virtual HRESULT executeTask() = 0;
|
---|
400 |
|
---|
401 | const ComObjPtr<Progress> mProgress;
|
---|
402 |
|
---|
403 | VDINTERFACEPROGRESS mVDIfProgress;
|
---|
404 |
|
---|
405 | /* Must have a strong VirtualBox reference during a task otherwise the
|
---|
406 | * reference count might drop to 0 while a task is still running. This
|
---|
407 | * would result in weird behavior, including deadlocks due to uninit and
|
---|
408 | * locking order issues. The deadlock often is not detectable because the
|
---|
409 | * uninit uses event semaphores which sabotages deadlock detection. */
|
---|
410 | ComObjPtr<VirtualBox> mVirtualBox;
|
---|
411 | AutoCaller mVirtualBoxCaller;
|
---|
412 | bool mNotifyAboutChanges;
|
---|
413 | };
|
---|
414 |
|
---|
415 | class Medium::CreateBaseTask : public Medium::Task
|
---|
416 | {
|
---|
417 | public:
|
---|
418 | CreateBaseTask(Medium *aMedium,
|
---|
419 | Progress *aProgress,
|
---|
420 | uint64_t aSize,
|
---|
421 | MediumVariant_T aVariant,
|
---|
422 | bool fNotifyAboutChanges = true)
|
---|
423 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
424 | mSize(aSize),
|
---|
425 | mVariant(aVariant)
|
---|
426 | {
|
---|
427 | m_strTaskName = "createBase";
|
---|
428 | }
|
---|
429 |
|
---|
430 | uint64_t mSize;
|
---|
431 | MediumVariant_T mVariant;
|
---|
432 |
|
---|
433 | private:
|
---|
434 | HRESULT executeTask()
|
---|
435 | {
|
---|
436 | return mMedium->i_taskCreateBaseHandler(*this);
|
---|
437 | }
|
---|
438 | };
|
---|
439 |
|
---|
440 | class Medium::CreateDiffTask : public Medium::Task
|
---|
441 | {
|
---|
442 | public:
|
---|
443 | CreateDiffTask(Medium *aMedium,
|
---|
444 | Progress *aProgress,
|
---|
445 | Medium *aTarget,
|
---|
446 | MediumVariant_T aVariant,
|
---|
447 | MediumLockList *aMediumLockList,
|
---|
448 | bool fKeepMediumLockList = false,
|
---|
449 | bool fNotifyAboutChanges = true)
|
---|
450 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
451 | mpMediumLockList(aMediumLockList),
|
---|
452 | mTarget(aTarget),
|
---|
453 | mVariant(aVariant),
|
---|
454 | mTargetCaller(aTarget),
|
---|
455 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
456 | {
|
---|
457 | AssertReturnVoidStmt(aTarget != NULL, mRC = E_FAIL);
|
---|
458 | mRC = mTargetCaller.hrc();
|
---|
459 | if (FAILED(mRC))
|
---|
460 | return;
|
---|
461 | m_strTaskName = "createDiff";
|
---|
462 | }
|
---|
463 |
|
---|
464 | ~CreateDiffTask()
|
---|
465 | {
|
---|
466 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
467 | delete mpMediumLockList;
|
---|
468 | }
|
---|
469 |
|
---|
470 | MediumLockList *mpMediumLockList;
|
---|
471 |
|
---|
472 | const ComObjPtr<Medium> mTarget;
|
---|
473 | MediumVariant_T mVariant;
|
---|
474 |
|
---|
475 | private:
|
---|
476 | HRESULT executeTask()
|
---|
477 | {
|
---|
478 | return mMedium->i_taskCreateDiffHandler(*this);
|
---|
479 | }
|
---|
480 |
|
---|
481 | AutoCaller mTargetCaller;
|
---|
482 | bool mfKeepMediumLockList;
|
---|
483 | };
|
---|
484 |
|
---|
485 | class Medium::CloneTask : public Medium::Task
|
---|
486 | {
|
---|
487 | public:
|
---|
488 | CloneTask(Medium *aMedium,
|
---|
489 | Progress *aProgress,
|
---|
490 | Medium *aTarget,
|
---|
491 | MediumVariant_T aVariant,
|
---|
492 | Medium *aParent,
|
---|
493 | uint32_t idxSrcImageSame,
|
---|
494 | uint32_t idxDstImageSame,
|
---|
495 | MediumLockList *aSourceMediumLockList,
|
---|
496 | MediumLockList *aTargetMediumLockList,
|
---|
497 | bool fKeepSourceMediumLockList = false,
|
---|
498 | bool fKeepTargetMediumLockList = false,
|
---|
499 | bool fNotifyAboutChanges = true,
|
---|
500 | uint64_t aTargetLogicalSize = 0)
|
---|
501 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
502 | mTarget(aTarget),
|
---|
503 | mParent(aParent),
|
---|
504 | mTargetLogicalSize(aTargetLogicalSize),
|
---|
505 | mpSourceMediumLockList(aSourceMediumLockList),
|
---|
506 | mpTargetMediumLockList(aTargetMediumLockList),
|
---|
507 | mVariant(aVariant),
|
---|
508 | midxSrcImageSame(idxSrcImageSame),
|
---|
509 | midxDstImageSame(idxDstImageSame),
|
---|
510 | mTargetCaller(aTarget),
|
---|
511 | mParentCaller(aParent),
|
---|
512 | mfKeepSourceMediumLockList(fKeepSourceMediumLockList),
|
---|
513 | mfKeepTargetMediumLockList(fKeepTargetMediumLockList)
|
---|
514 | {
|
---|
515 | AssertReturnVoidStmt(aTarget != NULL, mRC = E_FAIL);
|
---|
516 | mRC = mTargetCaller.hrc();
|
---|
517 | if (FAILED(mRC))
|
---|
518 | return;
|
---|
519 | /* aParent may be NULL */
|
---|
520 | mRC = mParentCaller.hrc();
|
---|
521 | if (FAILED(mRC))
|
---|
522 | return;
|
---|
523 | AssertReturnVoidStmt(aSourceMediumLockList != NULL, mRC = E_FAIL);
|
---|
524 | AssertReturnVoidStmt(aTargetMediumLockList != NULL, mRC = E_FAIL);
|
---|
525 | m_strTaskName = "createClone";
|
---|
526 | }
|
---|
527 |
|
---|
528 | ~CloneTask()
|
---|
529 | {
|
---|
530 | if (!mfKeepSourceMediumLockList && mpSourceMediumLockList)
|
---|
531 | delete mpSourceMediumLockList;
|
---|
532 | if (!mfKeepTargetMediumLockList && mpTargetMediumLockList)
|
---|
533 | delete mpTargetMediumLockList;
|
---|
534 | }
|
---|
535 |
|
---|
536 | const ComObjPtr<Medium> mTarget;
|
---|
537 | const ComObjPtr<Medium> mParent;
|
---|
538 | uint64_t mTargetLogicalSize;
|
---|
539 | MediumLockList *mpSourceMediumLockList;
|
---|
540 | MediumLockList *mpTargetMediumLockList;
|
---|
541 | MediumVariant_T mVariant;
|
---|
542 | uint32_t midxSrcImageSame;
|
---|
543 | uint32_t midxDstImageSame;
|
---|
544 |
|
---|
545 | private:
|
---|
546 | HRESULT executeTask()
|
---|
547 | {
|
---|
548 | return mMedium->i_taskCloneHandler(*this);
|
---|
549 | }
|
---|
550 |
|
---|
551 | AutoCaller mTargetCaller;
|
---|
552 | AutoCaller mParentCaller;
|
---|
553 | bool mfKeepSourceMediumLockList;
|
---|
554 | bool mfKeepTargetMediumLockList;
|
---|
555 | };
|
---|
556 |
|
---|
557 | class Medium::MoveTask : public Medium::Task
|
---|
558 | {
|
---|
559 | public:
|
---|
560 | MoveTask(Medium *aMedium,
|
---|
561 | Progress *aProgress,
|
---|
562 | MediumVariant_T aVariant,
|
---|
563 | MediumLockList *aMediumLockList,
|
---|
564 | bool fKeepMediumLockList = false,
|
---|
565 | bool fNotifyAboutChanges = true)
|
---|
566 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
567 | mpMediumLockList(aMediumLockList),
|
---|
568 | mVariant(aVariant),
|
---|
569 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
570 | {
|
---|
571 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
572 | m_strTaskName = "createMove";
|
---|
573 | }
|
---|
574 |
|
---|
575 | ~MoveTask()
|
---|
576 | {
|
---|
577 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
578 | delete mpMediumLockList;
|
---|
579 | }
|
---|
580 |
|
---|
581 | MediumLockList *mpMediumLockList;
|
---|
582 | MediumVariant_T mVariant;
|
---|
583 |
|
---|
584 | private:
|
---|
585 | HRESULT executeTask()
|
---|
586 | {
|
---|
587 | return mMedium->i_taskMoveHandler(*this);
|
---|
588 | }
|
---|
589 |
|
---|
590 | bool mfKeepMediumLockList;
|
---|
591 | };
|
---|
592 |
|
---|
593 | class Medium::CompactTask : public Medium::Task
|
---|
594 | {
|
---|
595 | public:
|
---|
596 | CompactTask(Medium *aMedium,
|
---|
597 | Progress *aProgress,
|
---|
598 | MediumLockList *aMediumLockList,
|
---|
599 | bool fKeepMediumLockList = false,
|
---|
600 | bool fNotifyAboutChanges = true)
|
---|
601 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
602 | mpMediumLockList(aMediumLockList),
|
---|
603 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
604 | {
|
---|
605 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
606 | m_strTaskName = "createCompact";
|
---|
607 | }
|
---|
608 |
|
---|
609 | ~CompactTask()
|
---|
610 | {
|
---|
611 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
612 | delete mpMediumLockList;
|
---|
613 | }
|
---|
614 |
|
---|
615 | MediumLockList *mpMediumLockList;
|
---|
616 |
|
---|
617 | private:
|
---|
618 | HRESULT executeTask()
|
---|
619 | {
|
---|
620 | return mMedium->i_taskCompactHandler(*this);
|
---|
621 | }
|
---|
622 |
|
---|
623 | bool mfKeepMediumLockList;
|
---|
624 | };
|
---|
625 |
|
---|
626 | class Medium::ResizeTask : public Medium::Task
|
---|
627 | {
|
---|
628 | public:
|
---|
629 | ResizeTask(Medium *aMedium,
|
---|
630 | uint64_t aSize,
|
---|
631 | Progress *aProgress,
|
---|
632 | MediumLockList *aMediumLockList,
|
---|
633 | bool fKeepMediumLockList = false,
|
---|
634 | bool fNotifyAboutChanges = true)
|
---|
635 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
636 | mSize(aSize),
|
---|
637 | mpMediumLockList(aMediumLockList),
|
---|
638 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
639 | {
|
---|
640 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
641 | m_strTaskName = "createResize";
|
---|
642 | }
|
---|
643 |
|
---|
644 | ~ResizeTask()
|
---|
645 | {
|
---|
646 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
647 | delete mpMediumLockList;
|
---|
648 | }
|
---|
649 |
|
---|
650 | uint64_t mSize;
|
---|
651 | MediumLockList *mpMediumLockList;
|
---|
652 |
|
---|
653 | private:
|
---|
654 | HRESULT executeTask()
|
---|
655 | {
|
---|
656 | return mMedium->i_taskResizeHandler(*this);
|
---|
657 | }
|
---|
658 |
|
---|
659 | bool mfKeepMediumLockList;
|
---|
660 | };
|
---|
661 |
|
---|
662 | class Medium::ResetTask : public Medium::Task
|
---|
663 | {
|
---|
664 | public:
|
---|
665 | ResetTask(Medium *aMedium,
|
---|
666 | Progress *aProgress,
|
---|
667 | MediumLockList *aMediumLockList,
|
---|
668 | bool fKeepMediumLockList = false,
|
---|
669 | bool fNotifyAboutChanges = true)
|
---|
670 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
671 | mpMediumLockList(aMediumLockList),
|
---|
672 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
673 | {
|
---|
674 | m_strTaskName = "createReset";
|
---|
675 | }
|
---|
676 |
|
---|
677 | ~ResetTask()
|
---|
678 | {
|
---|
679 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
680 | delete mpMediumLockList;
|
---|
681 | }
|
---|
682 |
|
---|
683 | MediumLockList *mpMediumLockList;
|
---|
684 |
|
---|
685 | private:
|
---|
686 | HRESULT executeTask()
|
---|
687 | {
|
---|
688 | return mMedium->i_taskResetHandler(*this);
|
---|
689 | }
|
---|
690 |
|
---|
691 | bool mfKeepMediumLockList;
|
---|
692 | };
|
---|
693 |
|
---|
694 | class Medium::DeleteTask : public Medium::Task
|
---|
695 | {
|
---|
696 | public:
|
---|
697 | DeleteTask(Medium *aMedium,
|
---|
698 | Progress *aProgress,
|
---|
699 | MediumLockList *aMediumLockList,
|
---|
700 | bool fKeepMediumLockList = false,
|
---|
701 | bool fNotifyAboutChanges = true)
|
---|
702 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
703 | mpMediumLockList(aMediumLockList),
|
---|
704 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
705 | {
|
---|
706 | m_strTaskName = "createDelete";
|
---|
707 | }
|
---|
708 |
|
---|
709 | ~DeleteTask()
|
---|
710 | {
|
---|
711 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
712 | delete mpMediumLockList;
|
---|
713 | }
|
---|
714 |
|
---|
715 | MediumLockList *mpMediumLockList;
|
---|
716 |
|
---|
717 | private:
|
---|
718 | HRESULT executeTask()
|
---|
719 | {
|
---|
720 | return mMedium->i_taskDeleteHandler(*this);
|
---|
721 | }
|
---|
722 |
|
---|
723 | bool mfKeepMediumLockList;
|
---|
724 | };
|
---|
725 |
|
---|
726 | class Medium::MergeTask : public Medium::Task
|
---|
727 | {
|
---|
728 | public:
|
---|
729 | MergeTask(Medium *aMedium,
|
---|
730 | Medium *aTarget,
|
---|
731 | bool fMergeForward,
|
---|
732 | Medium *aParentForTarget,
|
---|
733 | MediumLockList *aChildrenToReparent,
|
---|
734 | Progress *aProgress,
|
---|
735 | MediumLockList *aMediumLockList,
|
---|
736 | bool fKeepMediumLockList = false,
|
---|
737 | bool fNotifyAboutChanges = true)
|
---|
738 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
739 | mTarget(aTarget),
|
---|
740 | mfMergeForward(fMergeForward),
|
---|
741 | mParentForTarget(aParentForTarget),
|
---|
742 | mpChildrenToReparent(aChildrenToReparent),
|
---|
743 | mpMediumLockList(aMediumLockList),
|
---|
744 | mTargetCaller(aTarget),
|
---|
745 | mParentForTargetCaller(aParentForTarget),
|
---|
746 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
747 | {
|
---|
748 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
749 | m_strTaskName = "createMerge";
|
---|
750 | }
|
---|
751 |
|
---|
752 | ~MergeTask()
|
---|
753 | {
|
---|
754 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
755 | delete mpMediumLockList;
|
---|
756 | if (mpChildrenToReparent)
|
---|
757 | delete mpChildrenToReparent;
|
---|
758 | }
|
---|
759 |
|
---|
760 | const ComObjPtr<Medium> mTarget;
|
---|
761 | bool mfMergeForward;
|
---|
762 | /* When mpChildrenToReparent is null then mParentForTarget is non-null and
|
---|
763 | * vice versa. In other words: they are used in different cases. */
|
---|
764 | const ComObjPtr<Medium> mParentForTarget;
|
---|
765 | MediumLockList *mpChildrenToReparent;
|
---|
766 | MediumLockList *mpMediumLockList;
|
---|
767 |
|
---|
768 | private:
|
---|
769 | HRESULT executeTask()
|
---|
770 | {
|
---|
771 | return mMedium->i_taskMergeHandler(*this);
|
---|
772 | }
|
---|
773 |
|
---|
774 | AutoCaller mTargetCaller;
|
---|
775 | AutoCaller mParentForTargetCaller;
|
---|
776 | bool mfKeepMediumLockList;
|
---|
777 | };
|
---|
778 |
|
---|
779 | class Medium::ImportTask : public Medium::Task
|
---|
780 | {
|
---|
781 | public:
|
---|
782 | ImportTask(Medium *aMedium,
|
---|
783 | Progress *aProgress,
|
---|
784 | const char *aFilename,
|
---|
785 | MediumFormat *aFormat,
|
---|
786 | MediumVariant_T aVariant,
|
---|
787 | RTVFSIOSTREAM aVfsIosSrc,
|
---|
788 | Medium *aParent,
|
---|
789 | MediumLockList *aTargetMediumLockList,
|
---|
790 | bool fKeepTargetMediumLockList = false,
|
---|
791 | bool fNotifyAboutChanges = true)
|
---|
792 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
793 | mFilename(aFilename),
|
---|
794 | mFormat(aFormat),
|
---|
795 | mVariant(aVariant),
|
---|
796 | mParent(aParent),
|
---|
797 | mpTargetMediumLockList(aTargetMediumLockList),
|
---|
798 | mpVfsIoIf(NULL),
|
---|
799 | mParentCaller(aParent),
|
---|
800 | mfKeepTargetMediumLockList(fKeepTargetMediumLockList)
|
---|
801 | {
|
---|
802 | AssertReturnVoidStmt(aTargetMediumLockList != NULL, mRC = E_FAIL);
|
---|
803 | /* aParent may be NULL */
|
---|
804 | mRC = mParentCaller.hrc();
|
---|
805 | if (FAILED(mRC))
|
---|
806 | return;
|
---|
807 |
|
---|
808 | mVDImageIfaces = aMedium->m->vdImageIfaces;
|
---|
809 |
|
---|
810 | int vrc = VDIfCreateFromVfsStream(aVfsIosSrc, RTFILE_O_READ, &mpVfsIoIf);
|
---|
811 | AssertRCReturnVoidStmt(vrc, mRC = E_FAIL);
|
---|
812 |
|
---|
813 | vrc = VDInterfaceAdd(&mpVfsIoIf->Core, "Medium::ImportTaskVfsIos",
|
---|
814 | VDINTERFACETYPE_IO, mpVfsIoIf,
|
---|
815 | sizeof(VDINTERFACEIO), &mVDImageIfaces);
|
---|
816 | AssertRCReturnVoidStmt(vrc, mRC = E_FAIL);
|
---|
817 | m_strTaskName = "createImport";
|
---|
818 | }
|
---|
819 |
|
---|
820 | ~ImportTask()
|
---|
821 | {
|
---|
822 | if (!mfKeepTargetMediumLockList && mpTargetMediumLockList)
|
---|
823 | delete mpTargetMediumLockList;
|
---|
824 | if (mpVfsIoIf)
|
---|
825 | {
|
---|
826 | VDIfDestroyFromVfsStream(mpVfsIoIf);
|
---|
827 | mpVfsIoIf = NULL;
|
---|
828 | }
|
---|
829 | }
|
---|
830 |
|
---|
831 | Utf8Str mFilename;
|
---|
832 | ComObjPtr<MediumFormat> mFormat;
|
---|
833 | MediumVariant_T mVariant;
|
---|
834 | const ComObjPtr<Medium> mParent;
|
---|
835 | MediumLockList *mpTargetMediumLockList;
|
---|
836 | PVDINTERFACE mVDImageIfaces;
|
---|
837 | PVDINTERFACEIO mpVfsIoIf; /**< Pointer to the VFS I/O stream to VD I/O interface wrapper. */
|
---|
838 |
|
---|
839 | private:
|
---|
840 | HRESULT executeTask()
|
---|
841 | {
|
---|
842 | return mMedium->i_taskImportHandler(*this);
|
---|
843 | }
|
---|
844 |
|
---|
845 | AutoCaller mParentCaller;
|
---|
846 | bool mfKeepTargetMediumLockList;
|
---|
847 | };
|
---|
848 |
|
---|
849 | class Medium::EncryptTask : public Medium::Task
|
---|
850 | {
|
---|
851 | public:
|
---|
852 | EncryptTask(Medium *aMedium,
|
---|
853 | const com::Utf8Str &strNewPassword,
|
---|
854 | const com::Utf8Str &strCurrentPassword,
|
---|
855 | const com::Utf8Str &strCipher,
|
---|
856 | const com::Utf8Str &strNewPasswordId,
|
---|
857 | Progress *aProgress,
|
---|
858 | MediumLockList *aMediumLockList)
|
---|
859 | : Medium::Task(aMedium, aProgress, false),
|
---|
860 | mstrNewPassword(strNewPassword),
|
---|
861 | mstrCurrentPassword(strCurrentPassword),
|
---|
862 | mstrCipher(strCipher),
|
---|
863 | mstrNewPasswordId(strNewPasswordId),
|
---|
864 | mpMediumLockList(aMediumLockList)
|
---|
865 | {
|
---|
866 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
867 | /* aParent may be NULL */
|
---|
868 | mRC = mParentCaller.hrc();
|
---|
869 | if (FAILED(mRC))
|
---|
870 | return;
|
---|
871 |
|
---|
872 | mVDImageIfaces = aMedium->m->vdImageIfaces;
|
---|
873 | m_strTaskName = "createEncrypt";
|
---|
874 | }
|
---|
875 |
|
---|
876 | ~EncryptTask()
|
---|
877 | {
|
---|
878 | if (mstrNewPassword.length())
|
---|
879 | RTMemWipeThoroughly(mstrNewPassword.mutableRaw(), mstrNewPassword.length(), 10 /* cPasses */);
|
---|
880 | if (mstrCurrentPassword.length())
|
---|
881 | RTMemWipeThoroughly(mstrCurrentPassword.mutableRaw(), mstrCurrentPassword.length(), 10 /* cPasses */);
|
---|
882 |
|
---|
883 | /* Keep any errors which might be set when deleting the lock list. */
|
---|
884 | ErrorInfoKeeper eik;
|
---|
885 | delete mpMediumLockList;
|
---|
886 | }
|
---|
887 |
|
---|
888 | Utf8Str mstrNewPassword;
|
---|
889 | Utf8Str mstrCurrentPassword;
|
---|
890 | Utf8Str mstrCipher;
|
---|
891 | Utf8Str mstrNewPasswordId;
|
---|
892 | MediumLockList *mpMediumLockList;
|
---|
893 | PVDINTERFACE mVDImageIfaces;
|
---|
894 |
|
---|
895 | private:
|
---|
896 | HRESULT executeTask()
|
---|
897 | {
|
---|
898 | return mMedium->i_taskEncryptHandler(*this);
|
---|
899 | }
|
---|
900 |
|
---|
901 | AutoCaller mParentCaller;
|
---|
902 | };
|
---|
903 |
|
---|
904 |
|
---|
905 |
|
---|
906 | /**
|
---|
907 | * Converts the Medium device type to the VD type.
|
---|
908 | */
|
---|
909 | static const char *getVDTypeName(VDTYPE enmType)
|
---|
910 | {
|
---|
911 | switch (enmType)
|
---|
912 | {
|
---|
913 | case VDTYPE_HDD: return "HDD";
|
---|
914 | case VDTYPE_OPTICAL_DISC: return "DVD";
|
---|
915 | case VDTYPE_FLOPPY: return "floppy";
|
---|
916 | case VDTYPE_INVALID: return "invalid";
|
---|
917 | default:
|
---|
918 | AssertFailedReturn("unknown");
|
---|
919 | }
|
---|
920 | }
|
---|
921 |
|
---|
922 | /**
|
---|
923 | * Converts the Medium device type to the VD type.
|
---|
924 | */
|
---|
925 | static const char *getDeviceTypeName(DeviceType_T enmType)
|
---|
926 | {
|
---|
927 | switch (enmType)
|
---|
928 | {
|
---|
929 | case DeviceType_HardDisk: return "HDD";
|
---|
930 | case DeviceType_DVD: return "DVD";
|
---|
931 | case DeviceType_Floppy: return "floppy";
|
---|
932 | case DeviceType_Null: return "null";
|
---|
933 | case DeviceType_Network: return "network";
|
---|
934 | case DeviceType_USB: return "USB";
|
---|
935 | case DeviceType_SharedFolder: return "shared folder";
|
---|
936 | case DeviceType_Graphics3D: return "graphics 3d";
|
---|
937 | default:
|
---|
938 | AssertFailedReturn("unknown");
|
---|
939 | }
|
---|
940 | }
|
---|
941 |
|
---|
942 |
|
---|
943 |
|
---|
944 | ////////////////////////////////////////////////////////////////////////////////
|
---|
945 | //
|
---|
946 | // Medium constructor / destructor
|
---|
947 | //
|
---|
948 | ////////////////////////////////////////////////////////////////////////////////
|
---|
949 |
|
---|
950 | DEFINE_EMPTY_CTOR_DTOR(Medium)
|
---|
951 |
|
---|
952 | HRESULT Medium::FinalConstruct()
|
---|
953 | {
|
---|
954 | m = new Data;
|
---|
955 |
|
---|
956 | /* Initialize the callbacks of the VD error interface */
|
---|
957 | m->vdIfError.pfnError = i_vdErrorCall;
|
---|
958 | m->vdIfError.pfnMessage = NULL;
|
---|
959 |
|
---|
960 | /* Initialize the callbacks of the VD config interface */
|
---|
961 | m->vdIfConfig.pfnAreKeysValid = i_vdConfigAreKeysValid;
|
---|
962 | m->vdIfConfig.pfnQuerySize = i_vdConfigQuerySize;
|
---|
963 | m->vdIfConfig.pfnQuery = i_vdConfigQuery;
|
---|
964 | m->vdIfConfig.pfnUpdate = i_vdConfigUpdate;
|
---|
965 | m->vdIfConfig.pfnQueryBytes = NULL;
|
---|
966 |
|
---|
967 | /* Initialize the per-disk interface chain (could be done more globally,
|
---|
968 | * but it's not wasting much time or space so it's not worth it). */
|
---|
969 | int vrc;
|
---|
970 | vrc = VDInterfaceAdd(&m->vdIfError.Core,
|
---|
971 | "Medium::vdInterfaceError",
|
---|
972 | VDINTERFACETYPE_ERROR, this,
|
---|
973 | sizeof(VDINTERFACEERROR), &m->vdDiskIfaces);
|
---|
974 | AssertRCReturn(vrc, E_FAIL);
|
---|
975 |
|
---|
976 | /* Initialize the per-image interface chain */
|
---|
977 | vrc = VDInterfaceAdd(&m->vdIfConfig.Core,
|
---|
978 | "Medium::vdInterfaceConfig",
|
---|
979 | VDINTERFACETYPE_CONFIG, this,
|
---|
980 | sizeof(VDINTERFACECONFIG), &m->vdImageIfaces);
|
---|
981 | AssertRCReturn(vrc, E_FAIL);
|
---|
982 |
|
---|
983 | /* Initialize the callbacks of the VD TCP interface (we always use the host
|
---|
984 | * IP stack for now) */
|
---|
985 | vrc = VDIfTcpNetInstDefaultCreate(&m->hTcpNetInst, &m->vdImageIfaces);
|
---|
986 | AssertRCReturn(vrc, E_FAIL);
|
---|
987 |
|
---|
988 | return BaseFinalConstruct();
|
---|
989 | }
|
---|
990 |
|
---|
991 | void Medium::FinalRelease()
|
---|
992 | {
|
---|
993 | uninit();
|
---|
994 |
|
---|
995 | VDIfTcpNetInstDefaultDestroy(m->hTcpNetInst);
|
---|
996 | delete m;
|
---|
997 |
|
---|
998 | BaseFinalRelease();
|
---|
999 | }
|
---|
1000 |
|
---|
1001 | /**
|
---|
1002 | * Initializes an empty hard disk object without creating or opening an associated
|
---|
1003 | * storage unit.
|
---|
1004 | *
|
---|
1005 | * This gets called by VirtualBox::CreateMedium() in which case uuidMachineRegistry
|
---|
1006 | * is empty since starting with VirtualBox 4.0, we no longer add opened media to a
|
---|
1007 | * registry automatically (this is deferred until the medium is attached to a machine).
|
---|
1008 | *
|
---|
1009 | * This also gets called when VirtualBox creates diff images; in this case uuidMachineRegistry
|
---|
1010 | * is set to the registry of the parent image to make sure they all end up in the same
|
---|
1011 | * file.
|
---|
1012 | *
|
---|
1013 | * For hard disks that don't have the MediumFormatCapabilities_CreateFixed or
|
---|
1014 | * MediumFormatCapabilities_CreateDynamic capability (and therefore cannot be created or deleted
|
---|
1015 | * with the means of VirtualBox) the associated storage unit is assumed to be
|
---|
1016 | * ready for use so the state of the hard disk object will be set to Created.
|
---|
1017 | *
|
---|
1018 | * @param aVirtualBox VirtualBox object.
|
---|
1019 | * @param aFormat
|
---|
1020 | * @param aLocation Storage unit location.
|
---|
1021 | * @param uuidMachineRegistry The registry to which this medium should be added
|
---|
1022 | * (global registry UUID or machine UUID or empty if none).
|
---|
1023 | * @param aDeviceType Device Type.
|
---|
1024 | */
|
---|
1025 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1026 | const Utf8Str &aFormat,
|
---|
1027 | const Utf8Str &aLocation,
|
---|
1028 | const Guid &uuidMachineRegistry,
|
---|
1029 | const DeviceType_T aDeviceType)
|
---|
1030 | {
|
---|
1031 | AssertReturn(aVirtualBox != NULL, E_FAIL);
|
---|
1032 | AssertReturn(!aFormat.isEmpty(), E_FAIL);
|
---|
1033 |
|
---|
1034 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1035 | AutoInitSpan autoInitSpan(this);
|
---|
1036 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1037 |
|
---|
1038 | HRESULT rc = S_OK;
|
---|
1039 |
|
---|
1040 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1041 |
|
---|
1042 | if (uuidMachineRegistry.isValid() && !uuidMachineRegistry.isZero())
|
---|
1043 | m->llRegistryIDs.push_back(uuidMachineRegistry);
|
---|
1044 |
|
---|
1045 | /* no storage yet */
|
---|
1046 | m->state = MediumState_NotCreated;
|
---|
1047 |
|
---|
1048 | /* cannot be a host drive */
|
---|
1049 | m->hostDrive = false;
|
---|
1050 |
|
---|
1051 | m->devType = aDeviceType;
|
---|
1052 |
|
---|
1053 | /* No storage unit is created yet, no need to call Medium::i_queryInfo */
|
---|
1054 |
|
---|
1055 | rc = i_setFormat(aFormat);
|
---|
1056 | if (FAILED(rc)) return rc;
|
---|
1057 |
|
---|
1058 | rc = i_setLocation(aLocation);
|
---|
1059 | if (FAILED(rc)) return rc;
|
---|
1060 |
|
---|
1061 | if (!(m->formatObj->i_getCapabilities() & ( MediumFormatCapabilities_CreateFixed
|
---|
1062 | | MediumFormatCapabilities_CreateDynamic
|
---|
1063 | | MediumFormatCapabilities_File))
|
---|
1064 | )
|
---|
1065 | {
|
---|
1066 | /* Storage for mediums of this format can neither be explicitly
|
---|
1067 | * created by VirtualBox nor deleted, so we place the medium to
|
---|
1068 | * Inaccessible state here and also add it to the registry. The
|
---|
1069 | * state means that one has to use RefreshState() to update the
|
---|
1070 | * medium format specific fields. */
|
---|
1071 | m->state = MediumState_Inaccessible;
|
---|
1072 | // create new UUID
|
---|
1073 | unconst(m->id).create();
|
---|
1074 |
|
---|
1075 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1076 | ComObjPtr<Medium> pMedium;
|
---|
1077 |
|
---|
1078 | /*
|
---|
1079 | * Check whether the UUID is taken already and create a new one
|
---|
1080 | * if required.
|
---|
1081 | * Try this only a limited amount of times in case the PRNG is broken
|
---|
1082 | * in some way to prevent an endless loop.
|
---|
1083 | */
|
---|
1084 | for (unsigned i = 0; i < 5; i++)
|
---|
1085 | {
|
---|
1086 | bool fInUse;
|
---|
1087 |
|
---|
1088 | fInUse = m->pVirtualBox->i_isMediaUuidInUse(m->id, aDeviceType);
|
---|
1089 | if (fInUse)
|
---|
1090 | {
|
---|
1091 | // create new UUID
|
---|
1092 | unconst(m->id).create();
|
---|
1093 | }
|
---|
1094 | else
|
---|
1095 | break;
|
---|
1096 | }
|
---|
1097 |
|
---|
1098 | rc = m->pVirtualBox->i_registerMedium(this, &pMedium, treeLock);
|
---|
1099 | Assert(this == pMedium || FAILED(rc));
|
---|
1100 | }
|
---|
1101 |
|
---|
1102 | /* Confirm a successful initialization when it's the case */
|
---|
1103 | if (SUCCEEDED(rc))
|
---|
1104 | autoInitSpan.setSucceeded();
|
---|
1105 |
|
---|
1106 | return rc;
|
---|
1107 | }
|
---|
1108 |
|
---|
1109 | /**
|
---|
1110 | * Initializes the medium object by opening the storage unit at the specified
|
---|
1111 | * location. The enOpenMode parameter defines whether the medium will be opened
|
---|
1112 | * read/write or read-only.
|
---|
1113 | *
|
---|
1114 | * This gets called by VirtualBox::OpenMedium() and also by
|
---|
1115 | * Machine::AttachDevice() and createImplicitDiffs() when new diff
|
---|
1116 | * images are created.
|
---|
1117 | *
|
---|
1118 | * There is no registry for this case since starting with VirtualBox 4.0, we
|
---|
1119 | * no longer add opened media to a registry automatically (this is deferred
|
---|
1120 | * until the medium is attached to a machine).
|
---|
1121 | *
|
---|
1122 | * For hard disks, the UUID, format and the parent of this medium will be
|
---|
1123 | * determined when reading the medium storage unit. For DVD and floppy images,
|
---|
1124 | * which have no UUIDs in their storage units, new UUIDs are created.
|
---|
1125 | * If the detected or set parent is not known to VirtualBox, then this method
|
---|
1126 | * will fail.
|
---|
1127 | *
|
---|
1128 | * @param aVirtualBox VirtualBox object.
|
---|
1129 | * @param aLocation Storage unit location.
|
---|
1130 | * @param enOpenMode Whether to open the medium read/write or read-only.
|
---|
1131 | * @param fForceNewUuid Whether a new UUID should be set to avoid duplicates.
|
---|
1132 | * @param aDeviceType Device type of medium.
|
---|
1133 | */
|
---|
1134 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1135 | const Utf8Str &aLocation,
|
---|
1136 | HDDOpenMode enOpenMode,
|
---|
1137 | bool fForceNewUuid,
|
---|
1138 | DeviceType_T aDeviceType)
|
---|
1139 | {
|
---|
1140 | AssertReturn(aVirtualBox, E_INVALIDARG);
|
---|
1141 | AssertReturn(!aLocation.isEmpty(), E_INVALIDARG);
|
---|
1142 |
|
---|
1143 | HRESULT rc = S_OK;
|
---|
1144 |
|
---|
1145 | {
|
---|
1146 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1147 | AutoInitSpan autoInitSpan(this);
|
---|
1148 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1149 |
|
---|
1150 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1151 |
|
---|
1152 | /* there must be a storage unit */
|
---|
1153 | m->state = MediumState_Created;
|
---|
1154 |
|
---|
1155 | /* remember device type for correct unregistering later */
|
---|
1156 | m->devType = aDeviceType;
|
---|
1157 |
|
---|
1158 | /* cannot be a host drive */
|
---|
1159 | m->hostDrive = false;
|
---|
1160 |
|
---|
1161 | /* remember the open mode (defaults to ReadWrite) */
|
---|
1162 | m->hddOpenMode = enOpenMode;
|
---|
1163 |
|
---|
1164 | if (aDeviceType == DeviceType_DVD)
|
---|
1165 | m->type = MediumType_Readonly;
|
---|
1166 | else if (aDeviceType == DeviceType_Floppy)
|
---|
1167 | m->type = MediumType_Writethrough;
|
---|
1168 |
|
---|
1169 | rc = i_setLocation(aLocation);
|
---|
1170 | if (FAILED(rc)) return rc;
|
---|
1171 |
|
---|
1172 | /* get all the information about the medium from the storage unit */
|
---|
1173 | if (fForceNewUuid)
|
---|
1174 | unconst(m->uuidImage).create();
|
---|
1175 |
|
---|
1176 | m->state = MediumState_Inaccessible;
|
---|
1177 | m->strLastAccessError = tr("Accessibility check was not yet performed");
|
---|
1178 |
|
---|
1179 | /* Confirm a successful initialization before the call to i_queryInfo.
|
---|
1180 | * Otherwise we can end up with a AutoCaller deadlock because the
|
---|
1181 | * medium becomes visible but is not marked as initialized. Causes
|
---|
1182 | * locking trouble (e.g. trying to save media registries) which is
|
---|
1183 | * hard to solve. */
|
---|
1184 | autoInitSpan.setSucceeded();
|
---|
1185 | }
|
---|
1186 |
|
---|
1187 | /* we're normal code from now on, no longer init */
|
---|
1188 | AutoCaller autoCaller(this);
|
---|
1189 | if (FAILED(autoCaller.hrc()))
|
---|
1190 | return autoCaller.hrc();
|
---|
1191 |
|
---|
1192 | /* need to call i_queryInfo immediately to correctly place the medium in
|
---|
1193 | * the respective media tree and update other information such as uuid */
|
---|
1194 | rc = i_queryInfo(fForceNewUuid /* fSetImageId */, false /* fSetParentId */,
|
---|
1195 | autoCaller);
|
---|
1196 | if (SUCCEEDED(rc))
|
---|
1197 | {
|
---|
1198 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1199 |
|
---|
1200 | /* if the storage unit is not accessible, it's not acceptable for the
|
---|
1201 | * newly opened media so convert this into an error */
|
---|
1202 | if (m->state == MediumState_Inaccessible)
|
---|
1203 | {
|
---|
1204 | Assert(!m->strLastAccessError.isEmpty());
|
---|
1205 | rc = setError(E_FAIL, "%s", m->strLastAccessError.c_str());
|
---|
1206 | alock.release();
|
---|
1207 | autoCaller.release();
|
---|
1208 | uninit();
|
---|
1209 | }
|
---|
1210 | else
|
---|
1211 | {
|
---|
1212 | AssertStmt(!m->id.isZero(),
|
---|
1213 | alock.release(); autoCaller.release(); uninit(); return E_FAIL);
|
---|
1214 |
|
---|
1215 | /* storage format must be detected by Medium::i_queryInfo if the
|
---|
1216 | * medium is accessible */
|
---|
1217 | AssertStmt(!m->strFormat.isEmpty(),
|
---|
1218 | alock.release(); autoCaller.release(); uninit(); return E_FAIL);
|
---|
1219 | }
|
---|
1220 | }
|
---|
1221 | else
|
---|
1222 | {
|
---|
1223 | /* opening this image failed, mark the object as dead */
|
---|
1224 | autoCaller.release();
|
---|
1225 | uninit();
|
---|
1226 | }
|
---|
1227 |
|
---|
1228 | return rc;
|
---|
1229 | }
|
---|
1230 |
|
---|
1231 | /**
|
---|
1232 | * Initializes the medium object by loading its data from the given settings
|
---|
1233 | * node. The medium will always be opened read/write.
|
---|
1234 | *
|
---|
1235 | * In this case, since we're loading from a registry, uuidMachineRegistry is
|
---|
1236 | * always set: it's either the global registry UUID or a machine UUID when
|
---|
1237 | * loading from a per-machine registry.
|
---|
1238 | *
|
---|
1239 | * @param aParent Parent medium disk or NULL for a root (base) medium.
|
---|
1240 | * @param aDeviceType Device type of the medium.
|
---|
1241 | * @param uuidMachineRegistry The registry to which this medium should be
|
---|
1242 | * added (global registry UUID or machine UUID).
|
---|
1243 | * @param data Configuration settings.
|
---|
1244 | * @param strMachineFolder The machine folder with which to resolve relative paths;
|
---|
1245 | * if empty, then we use the VirtualBox home directory
|
---|
1246 | *
|
---|
1247 | * @note Locks the medium tree for writing.
|
---|
1248 | */
|
---|
1249 | HRESULT Medium::initOne(Medium *aParent,
|
---|
1250 | DeviceType_T aDeviceType,
|
---|
1251 | const Guid &uuidMachineRegistry,
|
---|
1252 | const Utf8Str &strMachineFolder,
|
---|
1253 | const settings::Medium &data)
|
---|
1254 | {
|
---|
1255 | HRESULT rc;
|
---|
1256 |
|
---|
1257 | if (uuidMachineRegistry.isValid() && !uuidMachineRegistry.isZero())
|
---|
1258 | m->llRegistryIDs.push_back(uuidMachineRegistry);
|
---|
1259 |
|
---|
1260 | /* register with VirtualBox/parent early, since uninit() will
|
---|
1261 | * unconditionally unregister on failure */
|
---|
1262 | if (aParent)
|
---|
1263 | {
|
---|
1264 | // differencing medium: add to parent
|
---|
1265 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1266 | // no need to check maximum depth as settings reading did it
|
---|
1267 | i_setParent(aParent);
|
---|
1268 | }
|
---|
1269 |
|
---|
1270 | /* see below why we don't call Medium::i_queryInfo (and therefore treat
|
---|
1271 | * the medium as inaccessible for now */
|
---|
1272 | m->state = MediumState_Inaccessible;
|
---|
1273 | m->strLastAccessError = tr("Accessibility check was not yet performed");
|
---|
1274 |
|
---|
1275 | /* required */
|
---|
1276 | unconst(m->id) = data.uuid;
|
---|
1277 |
|
---|
1278 | /* assume not a host drive */
|
---|
1279 | m->hostDrive = false;
|
---|
1280 |
|
---|
1281 | /* optional */
|
---|
1282 | m->strDescription = data.strDescription;
|
---|
1283 |
|
---|
1284 | /* required */
|
---|
1285 | if (aDeviceType == DeviceType_HardDisk)
|
---|
1286 | {
|
---|
1287 | AssertReturn(!data.strFormat.isEmpty(), E_FAIL);
|
---|
1288 | rc = i_setFormat(data.strFormat);
|
---|
1289 | if (FAILED(rc)) return rc;
|
---|
1290 | }
|
---|
1291 | else
|
---|
1292 | {
|
---|
1293 | /// @todo handle host drive settings here as well?
|
---|
1294 | if (!data.strFormat.isEmpty())
|
---|
1295 | rc = i_setFormat(data.strFormat);
|
---|
1296 | else
|
---|
1297 | rc = i_setFormat("RAW");
|
---|
1298 | if (FAILED(rc)) return rc;
|
---|
1299 | }
|
---|
1300 |
|
---|
1301 | /* optional, only for diffs, default is false; we can only auto-reset
|
---|
1302 | * diff media so they must have a parent */
|
---|
1303 | if (aParent != NULL)
|
---|
1304 | m->autoReset = data.fAutoReset;
|
---|
1305 | else
|
---|
1306 | m->autoReset = false;
|
---|
1307 |
|
---|
1308 | /* properties (after setting the format as it populates the map). Note that
|
---|
1309 | * if some properties are not supported but present in the settings file,
|
---|
1310 | * they will still be read and accessible (for possible backward
|
---|
1311 | * compatibility; we can also clean them up from the XML upon next
|
---|
1312 | * XML format version change if we wish) */
|
---|
1313 | for (settings::StringsMap::const_iterator it = data.properties.begin();
|
---|
1314 | it != data.properties.end();
|
---|
1315 | ++it)
|
---|
1316 | {
|
---|
1317 | const Utf8Str &name = it->first;
|
---|
1318 | const Utf8Str &value = it->second;
|
---|
1319 | m->mapProperties[name] = value;
|
---|
1320 | }
|
---|
1321 |
|
---|
1322 | /* try to decrypt an optional iSCSI initiator secret */
|
---|
1323 | settings::StringsMap::const_iterator itCph = data.properties.find("InitiatorSecretEncrypted");
|
---|
1324 | if ( itCph != data.properties.end()
|
---|
1325 | && !itCph->second.isEmpty())
|
---|
1326 | {
|
---|
1327 | Utf8Str strPlaintext;
|
---|
1328 | int vrc = m->pVirtualBox->i_decryptSetting(&strPlaintext, itCph->second);
|
---|
1329 | if (RT_SUCCESS(vrc))
|
---|
1330 | m->mapProperties["InitiatorSecret"] = strPlaintext;
|
---|
1331 | }
|
---|
1332 |
|
---|
1333 | Utf8Str strFull;
|
---|
1334 | if (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
1335 | {
|
---|
1336 | // compose full path of the medium, if it's not fully qualified...
|
---|
1337 | // slightly convoluted logic here. If the caller has given us a
|
---|
1338 | // machine folder, then a relative path will be relative to that:
|
---|
1339 | if ( !strMachineFolder.isEmpty()
|
---|
1340 | && !RTPathStartsWithRoot(data.strLocation.c_str())
|
---|
1341 | )
|
---|
1342 | {
|
---|
1343 | strFull = strMachineFolder;
|
---|
1344 | strFull += RTPATH_SLASH;
|
---|
1345 | strFull += data.strLocation;
|
---|
1346 | }
|
---|
1347 | else
|
---|
1348 | {
|
---|
1349 | // Otherwise use the old VirtualBox "make absolute path" logic:
|
---|
1350 | int vrc = m->pVirtualBox->i_calculateFullPath(data.strLocation, strFull);
|
---|
1351 | if (RT_FAILURE(vrc))
|
---|
1352 | return Global::vboxStatusCodeToCOM(vrc);
|
---|
1353 | }
|
---|
1354 | }
|
---|
1355 | else
|
---|
1356 | strFull = data.strLocation;
|
---|
1357 |
|
---|
1358 | rc = i_setLocation(strFull);
|
---|
1359 | if (FAILED(rc)) return rc;
|
---|
1360 |
|
---|
1361 | if (aDeviceType == DeviceType_HardDisk)
|
---|
1362 | {
|
---|
1363 | /* type is only for base hard disks */
|
---|
1364 | if (m->pParent.isNull())
|
---|
1365 | m->type = data.hdType;
|
---|
1366 | }
|
---|
1367 | else if (aDeviceType == DeviceType_DVD)
|
---|
1368 | m->type = MediumType_Readonly;
|
---|
1369 | else
|
---|
1370 | m->type = MediumType_Writethrough;
|
---|
1371 |
|
---|
1372 | /* remember device type for correct unregistering later */
|
---|
1373 | m->devType = aDeviceType;
|
---|
1374 |
|
---|
1375 | LogFlowThisFunc(("m->strLocationFull='%s', m->strFormat=%s, m->id={%RTuuid}\n",
|
---|
1376 | m->strLocationFull.c_str(), m->strFormat.c_str(), m->id.raw()));
|
---|
1377 |
|
---|
1378 | return S_OK;
|
---|
1379 | }
|
---|
1380 |
|
---|
1381 | /**
|
---|
1382 | * Initializes and registers the medium object and its children by loading its
|
---|
1383 | * data from the given settings node. The medium will always be opened
|
---|
1384 | * read/write.
|
---|
1385 | *
|
---|
1386 | * @todo r=bird: What's that stuff about 'always be opened read/write'?
|
---|
1387 | *
|
---|
1388 | * In this case, since we're loading from a registry, uuidMachineRegistry is
|
---|
1389 | * always set: it's either the global registry UUID or a machine UUID when
|
---|
1390 | * loading from a per-machine registry.
|
---|
1391 | *
|
---|
1392 | * The only caller is currently VirtualBox::initMedia().
|
---|
1393 | *
|
---|
1394 | * @param aVirtualBox VirtualBox object.
|
---|
1395 | * @param aDeviceType Device type of the medium.
|
---|
1396 | * @param uuidMachineRegistry The registry to which this medium should be added
|
---|
1397 | * (global registry UUID or machine UUID).
|
---|
1398 | * @param strMachineFolder The machine folder with which to resolve relative
|
---|
1399 | * paths; if empty, then we use the VirtualBox home directory
|
---|
1400 | * @param data Configuration settings.
|
---|
1401 | * @param mediaTreeLock Autolock.
|
---|
1402 | * @param uIdsForNotify List to be updated with newly registered media.
|
---|
1403 | *
|
---|
1404 | * @note Assumes that the medium tree lock is held for writing. May release
|
---|
1405 | * and lock it again. At the end it is always held.
|
---|
1406 | */
|
---|
1407 | /* static */
|
---|
1408 | HRESULT Medium::initFromSettings(VirtualBox *aVirtualBox,
|
---|
1409 | DeviceType_T aDeviceType,
|
---|
1410 | const Guid &uuidMachineRegistry,
|
---|
1411 | const Utf8Str &strMachineFolder,
|
---|
1412 | const settings::Medium &data,
|
---|
1413 | AutoWriteLock &mediaTreeLock,
|
---|
1414 | std::list<std::pair<Guid, DeviceType_T> > &uIdsForNotify)
|
---|
1415 | {
|
---|
1416 | Assert(aVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
1417 | AssertReturn(aVirtualBox, E_INVALIDARG);
|
---|
1418 |
|
---|
1419 | HRESULT rc = S_OK;
|
---|
1420 |
|
---|
1421 | MediaList llMediaTocleanup;
|
---|
1422 |
|
---|
1423 | std::list<const settings::Medium *> llSettingsTodo;
|
---|
1424 | llSettingsTodo.push_back(&data);
|
---|
1425 | MediaList llParentsTodo;
|
---|
1426 | llParentsTodo.push_back(NULL);
|
---|
1427 |
|
---|
1428 | while (!llSettingsTodo.empty())
|
---|
1429 | {
|
---|
1430 | const settings::Medium *current = llSettingsTodo.front();
|
---|
1431 | llSettingsTodo.pop_front();
|
---|
1432 | ComObjPtr<Medium> pParent = llParentsTodo.front();
|
---|
1433 | llParentsTodo.pop_front();
|
---|
1434 |
|
---|
1435 | bool fReleasedMediaTreeLock = false;
|
---|
1436 | ComObjPtr<Medium> pMedium;
|
---|
1437 | rc = pMedium.createObject();
|
---|
1438 | if (FAILED(rc))
|
---|
1439 | break;
|
---|
1440 | ComObjPtr<Medium> pActualMedium(pMedium);
|
---|
1441 |
|
---|
1442 | {
|
---|
1443 | AutoInitSpan autoInitSpan(pMedium);
|
---|
1444 | AssertBreakStmt(autoInitSpan.isOk(), rc = E_FAIL);
|
---|
1445 |
|
---|
1446 | unconst(pMedium->m->pVirtualBox) = aVirtualBox;
|
---|
1447 | rc = pMedium->initOne(pParent, aDeviceType, uuidMachineRegistry, strMachineFolder, *current);
|
---|
1448 | if (FAILED(rc))
|
---|
1449 | break;
|
---|
1450 | rc = aVirtualBox->i_registerMedium(pActualMedium, &pActualMedium, mediaTreeLock, true /*fCalledFromMediumInit*/);
|
---|
1451 | if (FAILED(rc))
|
---|
1452 | break;
|
---|
1453 |
|
---|
1454 | if (pActualMedium == pMedium)
|
---|
1455 | {
|
---|
1456 | /* It is a truly new medium, remember details for cleanup. */
|
---|
1457 | autoInitSpan.setSucceeded();
|
---|
1458 | llMediaTocleanup.push_front(pMedium);
|
---|
1459 | }
|
---|
1460 | else
|
---|
1461 | {
|
---|
1462 | /* Since the newly created medium was replaced by an already
|
---|
1463 | * known one when merging medium trees, we can immediately mark
|
---|
1464 | * it as failed. */
|
---|
1465 | autoInitSpan.setFailed();
|
---|
1466 | mediaTreeLock.release();
|
---|
1467 | fReleasedMediaTreeLock = true;
|
---|
1468 | }
|
---|
1469 | }
|
---|
1470 | if (fReleasedMediaTreeLock)
|
---|
1471 | {
|
---|
1472 | /* With the InitSpan out of the way it's safe to let the refcount
|
---|
1473 | * drop to 0 without causing uninit trouble. */
|
---|
1474 | pMedium.setNull();
|
---|
1475 | mediaTreeLock.acquire();
|
---|
1476 | }
|
---|
1477 |
|
---|
1478 | /* create all children */
|
---|
1479 | std::list<settings::Medium>::const_iterator itBegin = current->llChildren.begin();
|
---|
1480 | std::list<settings::Medium>::const_iterator itEnd = current->llChildren.end();
|
---|
1481 | for (std::list<settings::Medium>::const_iterator it = itBegin; it != itEnd; ++it)
|
---|
1482 | {
|
---|
1483 | llSettingsTodo.push_back(&*it);
|
---|
1484 | llParentsTodo.push_back(pActualMedium);
|
---|
1485 | }
|
---|
1486 | }
|
---|
1487 |
|
---|
1488 | if (SUCCEEDED(rc))
|
---|
1489 | {
|
---|
1490 | /* Check for consistency. */
|
---|
1491 | Assert(llSettingsTodo.size() == 0);
|
---|
1492 | Assert(llParentsTodo.size() == 0);
|
---|
1493 | /* Create the list of notifications, parent first. */
|
---|
1494 | MediaList::const_reverse_iterator itBegin = llMediaTocleanup.rbegin();
|
---|
1495 | MediaList::const_reverse_iterator itEnd = llMediaTocleanup.rend();
|
---|
1496 | for (MediaList::const_reverse_iterator it = itBegin; it != itEnd; --it)
|
---|
1497 | {
|
---|
1498 | ComObjPtr<Medium> pMedium = *it;
|
---|
1499 | AutoCaller mediumCaller(pMedium);
|
---|
1500 | if (FAILED(mediumCaller.hrc())) continue;
|
---|
1501 | const Guid &id = pMedium->i_getId();
|
---|
1502 | uIdsForNotify.push_back(std::pair<Guid, DeviceType_T>(id, aDeviceType));
|
---|
1503 | }
|
---|
1504 | }
|
---|
1505 | else
|
---|
1506 | {
|
---|
1507 | /* Forget state of the settings processing. */
|
---|
1508 | llSettingsTodo.clear();
|
---|
1509 | llParentsTodo.clear();
|
---|
1510 | /* Unregister all accumulated medium objects in the right order (last
|
---|
1511 | * created to first created, avoiding config leftovers). */
|
---|
1512 | MediaList::const_iterator itBegin = llMediaTocleanup.begin();
|
---|
1513 | MediaList::const_iterator itEnd = llMediaTocleanup.end();
|
---|
1514 | for (MediaList::const_iterator it = itBegin; it != itEnd; ++it)
|
---|
1515 | {
|
---|
1516 | ComObjPtr<Medium> pMedium = *it;
|
---|
1517 | pMedium->i_unregisterWithVirtualBox();
|
---|
1518 | }
|
---|
1519 | /* Forget the only references to all newly created medium objects,
|
---|
1520 | * triggering freeing (uninit happened in unregistering above). */
|
---|
1521 | mediaTreeLock.release();
|
---|
1522 | llMediaTocleanup.clear();
|
---|
1523 | mediaTreeLock.acquire();
|
---|
1524 | }
|
---|
1525 |
|
---|
1526 | return rc;
|
---|
1527 | }
|
---|
1528 |
|
---|
1529 | /**
|
---|
1530 | * Initializes the medium object by providing the host drive information.
|
---|
1531 | * Not used for anything but the host floppy/host DVD case.
|
---|
1532 | *
|
---|
1533 | * There is no registry for this case.
|
---|
1534 | *
|
---|
1535 | * @param aVirtualBox VirtualBox object.
|
---|
1536 | * @param aDeviceType Device type of the medium.
|
---|
1537 | * @param aLocation Location of the host drive.
|
---|
1538 | * @param aDescription Comment for this host drive.
|
---|
1539 | *
|
---|
1540 | * @note Locks VirtualBox lock for writing.
|
---|
1541 | */
|
---|
1542 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1543 | DeviceType_T aDeviceType,
|
---|
1544 | const Utf8Str &aLocation,
|
---|
1545 | const Utf8Str &aDescription /* = Utf8Str::Empty */)
|
---|
1546 | {
|
---|
1547 | ComAssertRet(aDeviceType == DeviceType_DVD || aDeviceType == DeviceType_Floppy, E_INVALIDARG);
|
---|
1548 | ComAssertRet(!aLocation.isEmpty(), E_INVALIDARG);
|
---|
1549 |
|
---|
1550 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1551 | AutoInitSpan autoInitSpan(this);
|
---|
1552 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1553 |
|
---|
1554 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1555 |
|
---|
1556 | // We do not store host drives in VirtualBox.xml or anywhere else, so if we want
|
---|
1557 | // host drives to be identifiable by UUID and not give the drive a different UUID
|
---|
1558 | // every time VirtualBox starts, we need to fake a reproducible UUID here:
|
---|
1559 | RTUUID uuid;
|
---|
1560 | RTUuidClear(&uuid);
|
---|
1561 | if (aDeviceType == DeviceType_DVD)
|
---|
1562 | memcpy(&uuid.au8[0], "DVD", 3);
|
---|
1563 | else
|
---|
1564 | memcpy(&uuid.au8[0], "FD", 2);
|
---|
1565 | /* use device name, adjusted to the end of uuid, shortened if necessary */
|
---|
1566 | size_t lenLocation = aLocation.length();
|
---|
1567 | if (lenLocation > 12)
|
---|
1568 | memcpy(&uuid.au8[4], aLocation.c_str() + (lenLocation - 12), 12);
|
---|
1569 | else
|
---|
1570 | memcpy(&uuid.au8[4 + 12 - lenLocation], aLocation.c_str(), lenLocation);
|
---|
1571 | unconst(m->id) = uuid;
|
---|
1572 |
|
---|
1573 | if (aDeviceType == DeviceType_DVD)
|
---|
1574 | m->type = MediumType_Readonly;
|
---|
1575 | else
|
---|
1576 | m->type = MediumType_Writethrough;
|
---|
1577 | m->devType = aDeviceType;
|
---|
1578 | m->state = MediumState_Created;
|
---|
1579 | m->hostDrive = true;
|
---|
1580 | HRESULT rc = i_setFormat("RAW");
|
---|
1581 | if (FAILED(rc)) return rc;
|
---|
1582 | rc = i_setLocation(aLocation);
|
---|
1583 | if (FAILED(rc)) return rc;
|
---|
1584 | m->strDescription = aDescription;
|
---|
1585 |
|
---|
1586 | autoInitSpan.setSucceeded();
|
---|
1587 | return S_OK;
|
---|
1588 | }
|
---|
1589 |
|
---|
1590 | /**
|
---|
1591 | * Uninitializes the instance.
|
---|
1592 | *
|
---|
1593 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
1594 | *
|
---|
1595 | * @note All children of this medium get uninitialized, too, in a stack
|
---|
1596 | * friendly manner.
|
---|
1597 | */
|
---|
1598 | void Medium::uninit()
|
---|
1599 | {
|
---|
1600 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1601 | * the pVirtualBox reference, and in this case we don't need to continue.
|
---|
1602 | * Normally this would be handled through the AutoUninitSpan magic, however
|
---|
1603 | * this cannot be done at this point as the media tree must be locked
|
---|
1604 | * before reaching the AutoUninitSpan, otherwise deadlocks can happen.
|
---|
1605 | *
|
---|
1606 | * NOTE: The tree lock is higher priority than the medium caller and medium
|
---|
1607 | * object locks, i.e. the medium caller may have to be released and be
|
---|
1608 | * re-acquired in the right place later. See Medium::getParent() for sample
|
---|
1609 | * code how to do this safely. */
|
---|
1610 | VirtualBox *pVirtualBox = m->pVirtualBox;
|
---|
1611 | if (!pVirtualBox)
|
---|
1612 | return;
|
---|
1613 |
|
---|
1614 | /* Caller must not hold the object (checked below) or media tree lock. */
|
---|
1615 | Assert(!pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
1616 |
|
---|
1617 | AutoWriteLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1618 |
|
---|
1619 | /* Must use a list without refcounting help since "this" might already have
|
---|
1620 | * reached 0, and then the refcount must not be increased again since it
|
---|
1621 | * would otherwise trigger a double free. For all other list entries this
|
---|
1622 | * needs manual refcount updating, to make sure the refcount for children
|
---|
1623 | * does not drop to 0 too early. */
|
---|
1624 | std::list<Medium *> llMediaTodo;
|
---|
1625 | llMediaTodo.push_back(this);
|
---|
1626 |
|
---|
1627 | while (!llMediaTodo.empty())
|
---|
1628 | {
|
---|
1629 | Medium *pMedium = llMediaTodo.front();
|
---|
1630 | llMediaTodo.pop_front();
|
---|
1631 |
|
---|
1632 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
1633 | AutoUninitSpan autoUninitSpan(pMedium);
|
---|
1634 | if (autoUninitSpan.uninitDone())
|
---|
1635 | {
|
---|
1636 | if (pMedium != this)
|
---|
1637 | pMedium->Release();
|
---|
1638 | continue;
|
---|
1639 | }
|
---|
1640 |
|
---|
1641 | Assert(!pMedium->isWriteLockOnCurrentThread());
|
---|
1642 | #ifdef DEBUG
|
---|
1643 | if (!pMedium->m->backRefs.empty())
|
---|
1644 | pMedium->i_dumpBackRefs();
|
---|
1645 | #endif
|
---|
1646 | Assert(pMedium->m->backRefs.empty());
|
---|
1647 |
|
---|
1648 | pMedium->m->formatObj.setNull();
|
---|
1649 |
|
---|
1650 | if (pMedium->m->state == MediumState_Deleting)
|
---|
1651 | {
|
---|
1652 | /* This medium has been already deleted (directly or as part of a
|
---|
1653 | * merge). Reparenting has already been done. */
|
---|
1654 | Assert(pMedium->m->pParent.isNull());
|
---|
1655 | Assert(pMedium->m->llChildren.empty());
|
---|
1656 | if (pMedium != this)
|
---|
1657 | pMedium->Release();
|
---|
1658 | continue;
|
---|
1659 | }
|
---|
1660 |
|
---|
1661 | //Assert(!pMedium->m->pParent);
|
---|
1662 | /** @todo r=klaus Should not be necessary, since the caller should be
|
---|
1663 | * doing the deparenting. No time right now to test everything. */
|
---|
1664 | if (pMedium == this && pMedium->m->pParent)
|
---|
1665 | pMedium->i_deparent();
|
---|
1666 |
|
---|
1667 | /* Process all children */
|
---|
1668 | MediaList::const_iterator itBegin = pMedium->m->llChildren.begin();
|
---|
1669 | MediaList::const_iterator itEnd = pMedium->m->llChildren.end();
|
---|
1670 | for (MediaList::const_iterator it = itBegin; it != itEnd; ++it)
|
---|
1671 | {
|
---|
1672 | Medium *pChild = *it;
|
---|
1673 | pChild->m->pParent.setNull();
|
---|
1674 | pChild->AddRef();
|
---|
1675 | llMediaTodo.push_back(pChild);
|
---|
1676 | }
|
---|
1677 |
|
---|
1678 | /* Children information obsolete, will be processed anyway. */
|
---|
1679 | pMedium->m->llChildren.clear();
|
---|
1680 |
|
---|
1681 | unconst(pMedium->m->pVirtualBox) = NULL;
|
---|
1682 |
|
---|
1683 | if (pMedium != this)
|
---|
1684 | pMedium->Release();
|
---|
1685 |
|
---|
1686 | autoUninitSpan.setSucceeded();
|
---|
1687 | }
|
---|
1688 | }
|
---|
1689 |
|
---|
1690 | /**
|
---|
1691 | * Internal helper that removes "this" from the list of children of its
|
---|
1692 | * parent. Used in uninit() and other places when reparenting is necessary.
|
---|
1693 | *
|
---|
1694 | * The caller must hold the medium tree lock!
|
---|
1695 | */
|
---|
1696 | void Medium::i_deparent()
|
---|
1697 | {
|
---|
1698 | MediaList &llParent = m->pParent->m->llChildren;
|
---|
1699 | for (MediaList::iterator it = llParent.begin();
|
---|
1700 | it != llParent.end();
|
---|
1701 | ++it)
|
---|
1702 | {
|
---|
1703 | Medium *pParentsChild = *it;
|
---|
1704 | if (this == pParentsChild)
|
---|
1705 | {
|
---|
1706 | llParent.erase(it);
|
---|
1707 | break;
|
---|
1708 | }
|
---|
1709 | }
|
---|
1710 | m->pParent.setNull();
|
---|
1711 | }
|
---|
1712 |
|
---|
1713 | /**
|
---|
1714 | * Internal helper that removes "this" from the list of children of its
|
---|
1715 | * parent. Used in uninit() and other places when reparenting is necessary.
|
---|
1716 | *
|
---|
1717 | * The caller must hold the medium tree lock!
|
---|
1718 | */
|
---|
1719 | void Medium::i_setParent(const ComObjPtr<Medium> &pParent)
|
---|
1720 | {
|
---|
1721 | m->pParent = pParent;
|
---|
1722 | if (pParent)
|
---|
1723 | pParent->m->llChildren.push_back(this);
|
---|
1724 | }
|
---|
1725 |
|
---|
1726 |
|
---|
1727 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1728 | //
|
---|
1729 | // IMedium public methods
|
---|
1730 | //
|
---|
1731 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1732 |
|
---|
1733 | HRESULT Medium::getId(com::Guid &aId)
|
---|
1734 | {
|
---|
1735 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1736 |
|
---|
1737 | aId = m->id;
|
---|
1738 |
|
---|
1739 | return S_OK;
|
---|
1740 | }
|
---|
1741 |
|
---|
1742 | HRESULT Medium::getDescription(AutoCaller &autoCaller, com::Utf8Str &aDescription)
|
---|
1743 | {
|
---|
1744 | NOREF(autoCaller);
|
---|
1745 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1746 |
|
---|
1747 | aDescription = m->strDescription;
|
---|
1748 |
|
---|
1749 | return S_OK;
|
---|
1750 | }
|
---|
1751 |
|
---|
1752 | HRESULT Medium::setDescription(AutoCaller &autoCaller, const com::Utf8Str &aDescription)
|
---|
1753 | {
|
---|
1754 | /// @todo update m->strDescription and save the global registry (and local
|
---|
1755 | /// registries of portable VMs referring to this medium), this will also
|
---|
1756 | /// require to add the mRegistered flag to data
|
---|
1757 |
|
---|
1758 | HRESULT rc = S_OK;
|
---|
1759 |
|
---|
1760 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
1761 |
|
---|
1762 | try
|
---|
1763 | {
|
---|
1764 | autoCaller.release();
|
---|
1765 |
|
---|
1766 | // to avoid redundant locking, which just takes a time, just call required functions.
|
---|
1767 | // the error will be just stored and will be reported after locks will be acquired again
|
---|
1768 |
|
---|
1769 | const char *pszError = NULL;
|
---|
1770 |
|
---|
1771 |
|
---|
1772 | /* Build the lock list. */
|
---|
1773 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
1774 | this /* pToLockWrite */,
|
---|
1775 | true /* fMediumLockWriteAll */,
|
---|
1776 | NULL,
|
---|
1777 | *pMediumLockList);
|
---|
1778 | if (FAILED(rc))
|
---|
1779 | {
|
---|
1780 | pszError = tr("Failed to create medium lock list for '%s'");
|
---|
1781 | }
|
---|
1782 | else
|
---|
1783 | {
|
---|
1784 | rc = pMediumLockList->Lock();
|
---|
1785 | if (FAILED(rc))
|
---|
1786 | pszError = tr("Failed to lock media '%s'");
|
---|
1787 | }
|
---|
1788 |
|
---|
1789 | // locking: we need the tree lock first because we access parent pointers
|
---|
1790 | // and we need to write-lock the media involved
|
---|
1791 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1792 |
|
---|
1793 | autoCaller.add();
|
---|
1794 | AssertComRCThrowRC(autoCaller.hrc());
|
---|
1795 |
|
---|
1796 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1797 |
|
---|
1798 | if (FAILED(rc))
|
---|
1799 | throw setError(rc, pszError, i_getLocationFull().c_str());
|
---|
1800 |
|
---|
1801 | /* Set a new description */
|
---|
1802 | m->strDescription = aDescription;
|
---|
1803 |
|
---|
1804 | // save the settings
|
---|
1805 | alock.release();
|
---|
1806 | autoCaller.release();
|
---|
1807 | treeLock.release();
|
---|
1808 | i_markRegistriesModified();
|
---|
1809 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
1810 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
1811 | }
|
---|
1812 | catch (HRESULT aRC) { rc = aRC; }
|
---|
1813 |
|
---|
1814 | delete pMediumLockList;
|
---|
1815 |
|
---|
1816 | return rc;
|
---|
1817 | }
|
---|
1818 |
|
---|
1819 | HRESULT Medium::getState(MediumState_T *aState)
|
---|
1820 | {
|
---|
1821 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1822 | *aState = m->state;
|
---|
1823 |
|
---|
1824 | return S_OK;
|
---|
1825 | }
|
---|
1826 |
|
---|
1827 | HRESULT Medium::getVariant(std::vector<MediumVariant_T> &aVariant)
|
---|
1828 | {
|
---|
1829 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1830 |
|
---|
1831 | const size_t cBits = sizeof(MediumVariant_T) * 8;
|
---|
1832 | aVariant.resize(cBits);
|
---|
1833 | for (size_t i = 0; i < cBits; ++i)
|
---|
1834 | aVariant[i] = (MediumVariant_T)(m->variant & RT_BIT(i));
|
---|
1835 |
|
---|
1836 | return S_OK;
|
---|
1837 | }
|
---|
1838 |
|
---|
1839 | HRESULT Medium::getLocation(com::Utf8Str &aLocation)
|
---|
1840 | {
|
---|
1841 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1842 |
|
---|
1843 | aLocation = m->strLocationFull;
|
---|
1844 |
|
---|
1845 | return S_OK;
|
---|
1846 | }
|
---|
1847 |
|
---|
1848 | HRESULT Medium::getName(com::Utf8Str &aName)
|
---|
1849 | {
|
---|
1850 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1851 |
|
---|
1852 | aName = i_getName();
|
---|
1853 |
|
---|
1854 | return S_OK;
|
---|
1855 | }
|
---|
1856 |
|
---|
1857 | HRESULT Medium::getDeviceType(DeviceType_T *aDeviceType)
|
---|
1858 | {
|
---|
1859 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1860 |
|
---|
1861 | *aDeviceType = m->devType;
|
---|
1862 |
|
---|
1863 | return S_OK;
|
---|
1864 | }
|
---|
1865 |
|
---|
1866 | HRESULT Medium::getHostDrive(BOOL *aHostDrive)
|
---|
1867 | {
|
---|
1868 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1869 |
|
---|
1870 | *aHostDrive = m->hostDrive;
|
---|
1871 |
|
---|
1872 | return S_OK;
|
---|
1873 | }
|
---|
1874 |
|
---|
1875 | HRESULT Medium::getSize(LONG64 *aSize)
|
---|
1876 | {
|
---|
1877 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1878 |
|
---|
1879 | *aSize = (LONG64)m->size;
|
---|
1880 |
|
---|
1881 | return S_OK;
|
---|
1882 | }
|
---|
1883 |
|
---|
1884 | HRESULT Medium::getFormat(com::Utf8Str &aFormat)
|
---|
1885 | {
|
---|
1886 | /* no need to lock, m->strFormat is const */
|
---|
1887 |
|
---|
1888 | aFormat = m->strFormat;
|
---|
1889 | return S_OK;
|
---|
1890 | }
|
---|
1891 |
|
---|
1892 | HRESULT Medium::getMediumFormat(ComPtr<IMediumFormat> &aMediumFormat)
|
---|
1893 | {
|
---|
1894 | /* no need to lock, m->formatObj is const */
|
---|
1895 | m->formatObj.queryInterfaceTo(aMediumFormat.asOutParam());
|
---|
1896 |
|
---|
1897 | return S_OK;
|
---|
1898 | }
|
---|
1899 |
|
---|
1900 | HRESULT Medium::getType(AutoCaller &autoCaller, MediumType_T *aType)
|
---|
1901 | {
|
---|
1902 | NOREF(autoCaller);
|
---|
1903 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1904 |
|
---|
1905 | *aType = m->type;
|
---|
1906 |
|
---|
1907 | return S_OK;
|
---|
1908 | }
|
---|
1909 |
|
---|
1910 | HRESULT Medium::setType(AutoCaller &autoCaller, MediumType_T aType)
|
---|
1911 | {
|
---|
1912 | autoCaller.release();
|
---|
1913 |
|
---|
1914 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1915 | * the pVirtualBox reference, see #uninit(). */
|
---|
1916 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
1917 |
|
---|
1918 | // we access m->pParent
|
---|
1919 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
1920 |
|
---|
1921 | autoCaller.add();
|
---|
1922 | if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
|
---|
1923 |
|
---|
1924 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1925 |
|
---|
1926 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
1927 | while (m->queryInfoRunning)
|
---|
1928 | {
|
---|
1929 | mlock.release();
|
---|
1930 | autoCaller.release();
|
---|
1931 | treeLock.release();
|
---|
1932 | /* Must not hold the media tree lock, as Medium::i_queryInfo needs
|
---|
1933 | * this lock and thus we would run into a deadlock here. */
|
---|
1934 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
1935 | /* must not hold the object lock now */
|
---|
1936 | Assert(!isWriteLockOnCurrentThread());
|
---|
1937 | {
|
---|
1938 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
1939 | }
|
---|
1940 | treeLock.acquire();
|
---|
1941 | autoCaller.add();
|
---|
1942 | if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
|
---|
1943 | mlock.acquire();
|
---|
1944 | }
|
---|
1945 |
|
---|
1946 | switch (m->state)
|
---|
1947 | {
|
---|
1948 | case MediumState_Created:
|
---|
1949 | case MediumState_Inaccessible:
|
---|
1950 | break;
|
---|
1951 | default:
|
---|
1952 | return i_setStateError();
|
---|
1953 | }
|
---|
1954 |
|
---|
1955 | if (m->type == aType)
|
---|
1956 | {
|
---|
1957 | /* Nothing to do */
|
---|
1958 | return S_OK;
|
---|
1959 | }
|
---|
1960 |
|
---|
1961 | DeviceType_T devType = i_getDeviceType();
|
---|
1962 | // DVD media can only be readonly.
|
---|
1963 | if (devType == DeviceType_DVD && aType != MediumType_Readonly)
|
---|
1964 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1965 | tr("Cannot change the type of DVD medium '%s'"),
|
---|
1966 | m->strLocationFull.c_str());
|
---|
1967 | // Floppy media can only be writethrough or readonly.
|
---|
1968 | if ( devType == DeviceType_Floppy
|
---|
1969 | && aType != MediumType_Writethrough
|
---|
1970 | && aType != MediumType_Readonly)
|
---|
1971 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1972 | tr("Cannot change the type of floppy medium '%s'"),
|
---|
1973 | m->strLocationFull.c_str());
|
---|
1974 |
|
---|
1975 | /* cannot change the type of a differencing medium */
|
---|
1976 | if (m->pParent)
|
---|
1977 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1978 | tr("Cannot change the type of medium '%s' because it is a differencing medium"),
|
---|
1979 | m->strLocationFull.c_str());
|
---|
1980 |
|
---|
1981 | /* Cannot change the type of a medium being in use by more than one VM.
|
---|
1982 | * If the change is to Immutable or MultiAttach then it must not be
|
---|
1983 | * directly attached to any VM, otherwise the assumptions about indirect
|
---|
1984 | * attachment elsewhere are violated and the VM becomes inaccessible.
|
---|
1985 | * Attaching an immutable medium triggers the diff creation, and this is
|
---|
1986 | * vital for the correct operation. */
|
---|
1987 | if ( m->backRefs.size() > 1
|
---|
1988 | || ( ( aType == MediumType_Immutable
|
---|
1989 | || aType == MediumType_MultiAttach)
|
---|
1990 | && m->backRefs.size() > 0))
|
---|
1991 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1992 | tr("Cannot change the type of medium '%s' because it is attached to %d virtual machines",
|
---|
1993 | "", m->backRefs.size()),
|
---|
1994 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
1995 |
|
---|
1996 | switch (aType)
|
---|
1997 | {
|
---|
1998 | case MediumType_Normal:
|
---|
1999 | case MediumType_Immutable:
|
---|
2000 | case MediumType_MultiAttach:
|
---|
2001 | {
|
---|
2002 | /* normal can be easily converted to immutable and vice versa even
|
---|
2003 | * if they have children as long as they are not attached to any
|
---|
2004 | * machine themselves */
|
---|
2005 | break;
|
---|
2006 | }
|
---|
2007 | case MediumType_Writethrough:
|
---|
2008 | case MediumType_Shareable:
|
---|
2009 | case MediumType_Readonly:
|
---|
2010 | {
|
---|
2011 | /* cannot change to writethrough, shareable or readonly
|
---|
2012 | * if there are children */
|
---|
2013 | if (i_getChildren().size() != 0)
|
---|
2014 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
2015 | tr("Cannot change type for medium '%s' since it has %d child media", "", i_getChildren().size()),
|
---|
2016 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
2017 | if (aType == MediumType_Shareable)
|
---|
2018 | {
|
---|
2019 | MediumVariant_T variant = i_getVariant();
|
---|
2020 | if (!(variant & MediumVariant_Fixed))
|
---|
2021 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2022 | tr("Cannot change type for medium '%s' to 'Shareable' since it is a dynamic medium storage unit"),
|
---|
2023 | m->strLocationFull.c_str());
|
---|
2024 | }
|
---|
2025 | else if (aType == MediumType_Readonly && devType == DeviceType_HardDisk)
|
---|
2026 | {
|
---|
2027 | // Readonly hard disks are not allowed, this medium type is reserved for
|
---|
2028 | // DVDs and floppy images at the moment. Later we might allow readonly hard
|
---|
2029 | // disks, but that's extremely unusual and many guest OSes will have trouble.
|
---|
2030 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2031 | tr("Cannot change type for medium '%s' to 'Readonly' since it is a hard disk"),
|
---|
2032 | m->strLocationFull.c_str());
|
---|
2033 | }
|
---|
2034 | break;
|
---|
2035 | }
|
---|
2036 | default:
|
---|
2037 | AssertFailedReturn(E_FAIL);
|
---|
2038 | }
|
---|
2039 |
|
---|
2040 | if (aType == MediumType_MultiAttach)
|
---|
2041 | {
|
---|
2042 | // This type is new with VirtualBox 4.0 and therefore requires settings
|
---|
2043 | // version 1.11 in the settings backend. Unfortunately it is not enough to do
|
---|
2044 | // the usual routine in MachineConfigFile::bumpSettingsVersionIfNeeded() for
|
---|
2045 | // two reasons: The medium type is a property of the media registry tree, which
|
---|
2046 | // can reside in the global config file (for pre-4.0 media); we would therefore
|
---|
2047 | // possibly need to bump the global config version. We don't want to do that though
|
---|
2048 | // because that might make downgrading to pre-4.0 impossible.
|
---|
2049 | // As a result, we can only use these two new types if the medium is NOT in the
|
---|
2050 | // global registry:
|
---|
2051 | const Guid &uuidGlobalRegistry = m->pVirtualBox->i_getGlobalRegistryId();
|
---|
2052 | if (i_isInRegistry(uuidGlobalRegistry))
|
---|
2053 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2054 | tr("Cannot change type for medium '%s': the media type 'MultiAttach' can only be used "
|
---|
2055 | "on media registered with a machine that was created with VirtualBox 4.0 or later"),
|
---|
2056 | m->strLocationFull.c_str());
|
---|
2057 | }
|
---|
2058 |
|
---|
2059 | m->type = aType;
|
---|
2060 |
|
---|
2061 | // save the settings
|
---|
2062 | mlock.release();
|
---|
2063 | autoCaller.release();
|
---|
2064 | treeLock.release();
|
---|
2065 | i_markRegistriesModified();
|
---|
2066 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2067 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
2068 |
|
---|
2069 | return S_OK;
|
---|
2070 | }
|
---|
2071 |
|
---|
2072 | HRESULT Medium::getAllowedTypes(std::vector<MediumType_T> &aAllowedTypes)
|
---|
2073 | {
|
---|
2074 | NOREF(aAllowedTypes);
|
---|
2075 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2076 |
|
---|
2077 | ReturnComNotImplemented();
|
---|
2078 | }
|
---|
2079 |
|
---|
2080 | HRESULT Medium::getParent(AutoCaller &autoCaller, ComPtr<IMedium> &aParent)
|
---|
2081 | {
|
---|
2082 | autoCaller.release();
|
---|
2083 |
|
---|
2084 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
2085 | * the pVirtualBox reference, see #uninit(). */
|
---|
2086 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
2087 |
|
---|
2088 | /* we access m->pParent */
|
---|
2089 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
2090 |
|
---|
2091 | autoCaller.add();
|
---|
2092 | if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
|
---|
2093 |
|
---|
2094 | m->pParent.queryInterfaceTo(aParent.asOutParam());
|
---|
2095 |
|
---|
2096 | return S_OK;
|
---|
2097 | }
|
---|
2098 |
|
---|
2099 | HRESULT Medium::getChildren(AutoCaller &autoCaller, std::vector<ComPtr<IMedium> > &aChildren)
|
---|
2100 | {
|
---|
2101 | autoCaller.release();
|
---|
2102 |
|
---|
2103 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
2104 | * the pVirtualBox reference, see #uninit(). */
|
---|
2105 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
2106 |
|
---|
2107 | /* we access children */
|
---|
2108 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
2109 |
|
---|
2110 | autoCaller.add();
|
---|
2111 | if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
|
---|
2112 |
|
---|
2113 | MediaList children(this->i_getChildren());
|
---|
2114 | aChildren.resize(children.size());
|
---|
2115 | size_t i = 0;
|
---|
2116 | for (MediaList::const_iterator it = children.begin(); it != children.end(); ++it, ++i)
|
---|
2117 | (*it).queryInterfaceTo(aChildren[i].asOutParam());
|
---|
2118 | return S_OK;
|
---|
2119 | }
|
---|
2120 |
|
---|
2121 | HRESULT Medium::getBase(AutoCaller &autoCaller, ComPtr<IMedium> &aBase)
|
---|
2122 | {
|
---|
2123 | autoCaller.release();
|
---|
2124 |
|
---|
2125 | /* i_getBase() will do callers/locking */
|
---|
2126 | i_getBase().queryInterfaceTo(aBase.asOutParam());
|
---|
2127 |
|
---|
2128 | return S_OK;
|
---|
2129 | }
|
---|
2130 |
|
---|
2131 | HRESULT Medium::getReadOnly(AutoCaller &autoCaller, BOOL *aReadOnly)
|
---|
2132 | {
|
---|
2133 | autoCaller.release();
|
---|
2134 |
|
---|
2135 | /* isReadOnly() will do locking */
|
---|
2136 | *aReadOnly = i_isReadOnly();
|
---|
2137 |
|
---|
2138 | return S_OK;
|
---|
2139 | }
|
---|
2140 |
|
---|
2141 | HRESULT Medium::getLogicalSize(LONG64 *aLogicalSize)
|
---|
2142 | {
|
---|
2143 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2144 |
|
---|
2145 | *aLogicalSize = (LONG64)m->logicalSize;
|
---|
2146 |
|
---|
2147 | return S_OK;
|
---|
2148 | }
|
---|
2149 |
|
---|
2150 | HRESULT Medium::getAutoReset(BOOL *aAutoReset)
|
---|
2151 | {
|
---|
2152 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2153 |
|
---|
2154 | if (m->pParent.isNull())
|
---|
2155 | *aAutoReset = FALSE;
|
---|
2156 | else
|
---|
2157 | *aAutoReset = m->autoReset;
|
---|
2158 |
|
---|
2159 | return S_OK;
|
---|
2160 | }
|
---|
2161 |
|
---|
2162 | HRESULT Medium::setAutoReset(BOOL aAutoReset)
|
---|
2163 | {
|
---|
2164 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2165 |
|
---|
2166 | if (m->pParent.isNull())
|
---|
2167 | return setError(VBOX_E_NOT_SUPPORTED,
|
---|
2168 | tr("Medium '%s' is not differencing"),
|
---|
2169 | m->strLocationFull.c_str());
|
---|
2170 |
|
---|
2171 | if (m->autoReset != !!aAutoReset)
|
---|
2172 | {
|
---|
2173 | m->autoReset = !!aAutoReset;
|
---|
2174 |
|
---|
2175 | // save the settings
|
---|
2176 | mlock.release();
|
---|
2177 | i_markRegistriesModified();
|
---|
2178 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2179 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
2180 | }
|
---|
2181 |
|
---|
2182 | return S_OK;
|
---|
2183 | }
|
---|
2184 |
|
---|
2185 | HRESULT Medium::getLastAccessError(com::Utf8Str &aLastAccessError)
|
---|
2186 | {
|
---|
2187 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2188 |
|
---|
2189 | aLastAccessError = m->strLastAccessError;
|
---|
2190 |
|
---|
2191 | return S_OK;
|
---|
2192 | }
|
---|
2193 |
|
---|
2194 | HRESULT Medium::getMachineIds(std::vector<com::Guid> &aMachineIds)
|
---|
2195 | {
|
---|
2196 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2197 |
|
---|
2198 | if (m->backRefs.size() != 0)
|
---|
2199 | {
|
---|
2200 | BackRefList brlist(m->backRefs);
|
---|
2201 | aMachineIds.resize(brlist.size());
|
---|
2202 | size_t i = 0;
|
---|
2203 | for (BackRefList::const_iterator it = brlist.begin(); it != brlist.end(); ++it, ++i)
|
---|
2204 | aMachineIds[i] = it->machineId;
|
---|
2205 | }
|
---|
2206 |
|
---|
2207 | return S_OK;
|
---|
2208 | }
|
---|
2209 |
|
---|
2210 | HRESULT Medium::setIds(AutoCaller &autoCaller,
|
---|
2211 | BOOL aSetImageId,
|
---|
2212 | const com::Guid &aImageId,
|
---|
2213 | BOOL aSetParentId,
|
---|
2214 | const com::Guid &aParentId)
|
---|
2215 | {
|
---|
2216 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2217 |
|
---|
2218 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
2219 | if (m->queryInfoRunning)
|
---|
2220 | {
|
---|
2221 | /* Must not hold the media tree lock, as Medium::i_queryInfo needs this
|
---|
2222 | * lock and thus we would run into a deadlock here. */
|
---|
2223 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
2224 | while (m->queryInfoRunning)
|
---|
2225 | {
|
---|
2226 | alock.release();
|
---|
2227 | /* must not hold the object lock now */
|
---|
2228 | Assert(!isWriteLockOnCurrentThread());
|
---|
2229 | {
|
---|
2230 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
2231 | }
|
---|
2232 | alock.acquire();
|
---|
2233 | }
|
---|
2234 | }
|
---|
2235 |
|
---|
2236 | switch (m->state)
|
---|
2237 | {
|
---|
2238 | case MediumState_Created:
|
---|
2239 | break;
|
---|
2240 | default:
|
---|
2241 | return i_setStateError();
|
---|
2242 | }
|
---|
2243 |
|
---|
2244 | Guid imageId, parentId;
|
---|
2245 | if (aSetImageId)
|
---|
2246 | {
|
---|
2247 | if (aImageId.isZero())
|
---|
2248 | imageId.create();
|
---|
2249 | else
|
---|
2250 | {
|
---|
2251 | imageId = aImageId;
|
---|
2252 | if (!imageId.isValid())
|
---|
2253 | return setError(E_INVALIDARG, tr("Argument %s is invalid"), "aImageId");
|
---|
2254 | }
|
---|
2255 | }
|
---|
2256 | if (aSetParentId)
|
---|
2257 | {
|
---|
2258 | if (aParentId.isZero())
|
---|
2259 | parentId.create();
|
---|
2260 | else
|
---|
2261 | parentId = aParentId;
|
---|
2262 | }
|
---|
2263 |
|
---|
2264 | const Guid uPrevImage = m->uuidImage;
|
---|
2265 | unconst(m->uuidImage) = imageId;
|
---|
2266 | ComObjPtr<Medium> pPrevParent = i_getParent();
|
---|
2267 | unconst(m->uuidParentImage) = parentId;
|
---|
2268 |
|
---|
2269 | // must not hold any locks before calling Medium::i_queryInfo
|
---|
2270 | alock.release();
|
---|
2271 |
|
---|
2272 | HRESULT rc = i_queryInfo(!!aSetImageId /* fSetImageId */,
|
---|
2273 | !!aSetParentId /* fSetParentId */,
|
---|
2274 | autoCaller);
|
---|
2275 |
|
---|
2276 | AutoReadLock arlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2277 | const Guid uCurrImage = m->uuidImage;
|
---|
2278 | ComObjPtr<Medium> pCurrParent = i_getParent();
|
---|
2279 | arlock.release();
|
---|
2280 |
|
---|
2281 | if (SUCCEEDED(rc))
|
---|
2282 | {
|
---|
2283 | if (uCurrImage != uPrevImage)
|
---|
2284 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
2285 | if (pPrevParent != pCurrParent)
|
---|
2286 | {
|
---|
2287 | if (pPrevParent)
|
---|
2288 | m->pVirtualBox->i_onMediumConfigChanged(pPrevParent);
|
---|
2289 | if (pCurrParent)
|
---|
2290 | m->pVirtualBox->i_onMediumConfigChanged(pCurrParent);
|
---|
2291 | }
|
---|
2292 | }
|
---|
2293 |
|
---|
2294 | return rc;
|
---|
2295 | }
|
---|
2296 |
|
---|
2297 | HRESULT Medium::refreshState(AutoCaller &autoCaller, MediumState_T *aState)
|
---|
2298 | {
|
---|
2299 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2300 |
|
---|
2301 | HRESULT rc = S_OK;
|
---|
2302 |
|
---|
2303 | switch (m->state)
|
---|
2304 | {
|
---|
2305 | case MediumState_Created:
|
---|
2306 | case MediumState_Inaccessible:
|
---|
2307 | case MediumState_LockedRead:
|
---|
2308 | {
|
---|
2309 | // must not hold any locks before calling Medium::i_queryInfo
|
---|
2310 | alock.release();
|
---|
2311 |
|
---|
2312 | rc = i_queryInfo(false /* fSetImageId */, false /* fSetParentId */,
|
---|
2313 | autoCaller);
|
---|
2314 |
|
---|
2315 | alock.acquire();
|
---|
2316 | break;
|
---|
2317 | }
|
---|
2318 | default:
|
---|
2319 | break;
|
---|
2320 | }
|
---|
2321 |
|
---|
2322 | *aState = m->state;
|
---|
2323 |
|
---|
2324 | return rc;
|
---|
2325 | }
|
---|
2326 |
|
---|
2327 | HRESULT Medium::getSnapshotIds(const com::Guid &aMachineId,
|
---|
2328 | std::vector<com::Guid> &aSnapshotIds)
|
---|
2329 | {
|
---|
2330 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2331 |
|
---|
2332 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
2333 | it != m->backRefs.end(); ++it)
|
---|
2334 | {
|
---|
2335 | if (it->machineId == aMachineId)
|
---|
2336 | {
|
---|
2337 | size_t size = it->llSnapshotIds.size();
|
---|
2338 |
|
---|
2339 | /* if the medium is attached to the machine in the current state, we
|
---|
2340 | * return its ID as the first element of the array */
|
---|
2341 | if (it->fInCurState)
|
---|
2342 | ++size;
|
---|
2343 |
|
---|
2344 | if (size > 0)
|
---|
2345 | {
|
---|
2346 | aSnapshotIds.resize(size);
|
---|
2347 |
|
---|
2348 | size_t j = 0;
|
---|
2349 | if (it->fInCurState)
|
---|
2350 | aSnapshotIds[j++] = it->machineId.toUtf16();
|
---|
2351 |
|
---|
2352 | for(std::list<SnapshotRef>::const_iterator jt = it->llSnapshotIds.begin(); jt != it->llSnapshotIds.end(); ++jt, ++j)
|
---|
2353 | aSnapshotIds[j] = jt->snapshotId;
|
---|
2354 | }
|
---|
2355 |
|
---|
2356 | break;
|
---|
2357 | }
|
---|
2358 | }
|
---|
2359 |
|
---|
2360 | return S_OK;
|
---|
2361 | }
|
---|
2362 |
|
---|
2363 | HRESULT Medium::lockRead(ComPtr<IToken> &aToken)
|
---|
2364 | {
|
---|
2365 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2366 |
|
---|
2367 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
2368 | if (m->queryInfoRunning)
|
---|
2369 | {
|
---|
2370 | /* Must not hold the media tree lock, as Medium::i_queryInfo needs this
|
---|
2371 | * lock and thus we would run into a deadlock here. */
|
---|
2372 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
2373 | while (m->queryInfoRunning)
|
---|
2374 | {
|
---|
2375 | alock.release();
|
---|
2376 | /* must not hold the object lock now */
|
---|
2377 | Assert(!isWriteLockOnCurrentThread());
|
---|
2378 | {
|
---|
2379 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
2380 | }
|
---|
2381 | alock.acquire();
|
---|
2382 | }
|
---|
2383 | }
|
---|
2384 |
|
---|
2385 | HRESULT rc = S_OK;
|
---|
2386 |
|
---|
2387 | switch (m->state)
|
---|
2388 | {
|
---|
2389 | case MediumState_Created:
|
---|
2390 | case MediumState_Inaccessible:
|
---|
2391 | case MediumState_LockedRead:
|
---|
2392 | {
|
---|
2393 | ++m->readers;
|
---|
2394 |
|
---|
2395 | ComAssertMsgBreak(m->readers != 0, (tr("Counter overflow")), rc = E_FAIL);
|
---|
2396 |
|
---|
2397 | /* Remember pre-lock state */
|
---|
2398 | if (m->state != MediumState_LockedRead)
|
---|
2399 | m->preLockState = m->state;
|
---|
2400 |
|
---|
2401 | LogFlowThisFunc(("Okay - prev state=%d readers=%d\n", m->state, m->readers));
|
---|
2402 | m->state = MediumState_LockedRead;
|
---|
2403 |
|
---|
2404 | ComObjPtr<MediumLockToken> pToken;
|
---|
2405 | rc = pToken.createObject();
|
---|
2406 | if (SUCCEEDED(rc))
|
---|
2407 | rc = pToken->init(this, false /* fWrite */);
|
---|
2408 | if (FAILED(rc))
|
---|
2409 | {
|
---|
2410 | --m->readers;
|
---|
2411 | if (m->readers == 0)
|
---|
2412 | m->state = m->preLockState;
|
---|
2413 | return rc;
|
---|
2414 | }
|
---|
2415 |
|
---|
2416 | pToken.queryInterfaceTo(aToken.asOutParam());
|
---|
2417 | break;
|
---|
2418 | }
|
---|
2419 | default:
|
---|
2420 | {
|
---|
2421 | LogFlowThisFunc(("Failing - state=%d\n", m->state));
|
---|
2422 | rc = i_setStateError();
|
---|
2423 | break;
|
---|
2424 | }
|
---|
2425 | }
|
---|
2426 |
|
---|
2427 | return rc;
|
---|
2428 | }
|
---|
2429 |
|
---|
2430 | /**
|
---|
2431 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
2432 | * in-process calls).
|
---|
2433 | */
|
---|
2434 | HRESULT Medium::i_unlockRead(MediumState_T *aState)
|
---|
2435 | {
|
---|
2436 | AutoCaller autoCaller(this);
|
---|
2437 | if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
|
---|
2438 |
|
---|
2439 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2440 |
|
---|
2441 | HRESULT rc = S_OK;
|
---|
2442 |
|
---|
2443 | switch (m->state)
|
---|
2444 | {
|
---|
2445 | case MediumState_LockedRead:
|
---|
2446 | {
|
---|
2447 | ComAssertMsgBreak(m->readers != 0, (tr("Counter underflow")), rc = E_FAIL);
|
---|
2448 | --m->readers;
|
---|
2449 |
|
---|
2450 | /* Reset the state after the last reader */
|
---|
2451 | if (m->readers == 0)
|
---|
2452 | {
|
---|
2453 | m->state = m->preLockState;
|
---|
2454 | /* There are cases where we inject the deleting state into
|
---|
2455 | * a medium locked for reading. Make sure #unmarkForDeletion()
|
---|
2456 | * gets the right state afterwards. */
|
---|
2457 | if (m->preLockState == MediumState_Deleting)
|
---|
2458 | m->preLockState = MediumState_Created;
|
---|
2459 | }
|
---|
2460 |
|
---|
2461 | LogFlowThisFunc(("new state=%d\n", m->state));
|
---|
2462 | break;
|
---|
2463 | }
|
---|
2464 | default:
|
---|
2465 | {
|
---|
2466 | LogFlowThisFunc(("Failing - state=%d\n", m->state));
|
---|
2467 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2468 | tr("Medium '%s' is not locked for reading"),
|
---|
2469 | m->strLocationFull.c_str());
|
---|
2470 | break;
|
---|
2471 | }
|
---|
2472 | }
|
---|
2473 |
|
---|
2474 | /* return the current state after */
|
---|
2475 | if (aState)
|
---|
2476 | *aState = m->state;
|
---|
2477 |
|
---|
2478 | return rc;
|
---|
2479 | }
|
---|
2480 | HRESULT Medium::lockWrite(ComPtr<IToken> &aToken)
|
---|
2481 | {
|
---|
2482 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2483 |
|
---|
2484 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
2485 | if (m->queryInfoRunning)
|
---|
2486 | {
|
---|
2487 | /* Must not hold the media tree lock, as Medium::i_queryInfo needs this
|
---|
2488 | * lock and thus we would run into a deadlock here. */
|
---|
2489 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
2490 | while (m->queryInfoRunning)
|
---|
2491 | {
|
---|
2492 | alock.release();
|
---|
2493 | /* must not hold the object lock now */
|
---|
2494 | Assert(!isWriteLockOnCurrentThread());
|
---|
2495 | {
|
---|
2496 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
2497 | }
|
---|
2498 | alock.acquire();
|
---|
2499 | }
|
---|
2500 | }
|
---|
2501 |
|
---|
2502 | HRESULT rc = S_OK;
|
---|
2503 |
|
---|
2504 | switch (m->state)
|
---|
2505 | {
|
---|
2506 | case MediumState_Created:
|
---|
2507 | case MediumState_Inaccessible:
|
---|
2508 | {
|
---|
2509 | m->preLockState = m->state;
|
---|
2510 |
|
---|
2511 | LogFlowThisFunc(("Okay - prev state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2512 | m->state = MediumState_LockedWrite;
|
---|
2513 |
|
---|
2514 | ComObjPtr<MediumLockToken> pToken;
|
---|
2515 | rc = pToken.createObject();
|
---|
2516 | if (SUCCEEDED(rc))
|
---|
2517 | rc = pToken->init(this, true /* fWrite */);
|
---|
2518 | if (FAILED(rc))
|
---|
2519 | {
|
---|
2520 | m->state = m->preLockState;
|
---|
2521 | return rc;
|
---|
2522 | }
|
---|
2523 |
|
---|
2524 | pToken.queryInterfaceTo(aToken.asOutParam());
|
---|
2525 | break;
|
---|
2526 | }
|
---|
2527 | default:
|
---|
2528 | {
|
---|
2529 | LogFlowThisFunc(("Failing - state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2530 | rc = i_setStateError();
|
---|
2531 | break;
|
---|
2532 | }
|
---|
2533 | }
|
---|
2534 |
|
---|
2535 | return rc;
|
---|
2536 | }
|
---|
2537 |
|
---|
2538 | /**
|
---|
2539 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
2540 | * in-process calls).
|
---|
2541 | */
|
---|
2542 | HRESULT Medium::i_unlockWrite(MediumState_T *aState)
|
---|
2543 | {
|
---|
2544 | AutoCaller autoCaller(this);
|
---|
2545 | if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
|
---|
2546 |
|
---|
2547 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2548 |
|
---|
2549 | HRESULT rc = S_OK;
|
---|
2550 |
|
---|
2551 | switch (m->state)
|
---|
2552 | {
|
---|
2553 | case MediumState_LockedWrite:
|
---|
2554 | {
|
---|
2555 | m->state = m->preLockState;
|
---|
2556 | /* There are cases where we inject the deleting state into
|
---|
2557 | * a medium locked for writing. Make sure #unmarkForDeletion()
|
---|
2558 | * gets the right state afterwards. */
|
---|
2559 | if (m->preLockState == MediumState_Deleting)
|
---|
2560 | m->preLockState = MediumState_Created;
|
---|
2561 | LogFlowThisFunc(("new state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2562 | break;
|
---|
2563 | }
|
---|
2564 | default:
|
---|
2565 | {
|
---|
2566 | LogFlowThisFunc(("Failing - state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2567 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2568 | tr("Medium '%s' is not locked for writing"),
|
---|
2569 | m->strLocationFull.c_str());
|
---|
2570 | break;
|
---|
2571 | }
|
---|
2572 | }
|
---|
2573 |
|
---|
2574 | /* return the current state after */
|
---|
2575 | if (aState)
|
---|
2576 | *aState = m->state;
|
---|
2577 |
|
---|
2578 | return rc;
|
---|
2579 | }
|
---|
2580 |
|
---|
2581 | HRESULT Medium::close(AutoCaller &aAutoCaller)
|
---|
2582 | {
|
---|
2583 | // make a copy of VirtualBox pointer which gets nulled by uninit()
|
---|
2584 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
2585 |
|
---|
2586 | Guid uId = i_getId();
|
---|
2587 | DeviceType_T devType = i_getDeviceType();
|
---|
2588 | MultiResult mrc = i_close(aAutoCaller);
|
---|
2589 |
|
---|
2590 | pVirtualBox->i_saveModifiedRegistries();
|
---|
2591 |
|
---|
2592 | if (SUCCEEDED(mrc) && uId.isValid() && !uId.isZero())
|
---|
2593 | pVirtualBox->i_onMediumRegistered(uId, devType, FALSE);
|
---|
2594 |
|
---|
2595 | return mrc;
|
---|
2596 | }
|
---|
2597 |
|
---|
2598 | HRESULT Medium::getProperty(const com::Utf8Str &aName,
|
---|
2599 | com::Utf8Str &aValue)
|
---|
2600 | {
|
---|
2601 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2602 |
|
---|
2603 | settings::StringsMap::const_iterator it = m->mapProperties.find(aName);
|
---|
2604 | if (it == m->mapProperties.end())
|
---|
2605 | {
|
---|
2606 | if (!aName.startsWith("Special/"))
|
---|
2607 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2608 | tr("Property '%s' does not exist"), aName.c_str());
|
---|
2609 | else
|
---|
2610 | /* be more silent here */
|
---|
2611 | return VBOX_E_OBJECT_NOT_FOUND;
|
---|
2612 | }
|
---|
2613 |
|
---|
2614 | aValue = it->second;
|
---|
2615 |
|
---|
2616 | return S_OK;
|
---|
2617 | }
|
---|
2618 |
|
---|
2619 | HRESULT Medium::setProperty(const com::Utf8Str &aName,
|
---|
2620 | const com::Utf8Str &aValue)
|
---|
2621 | {
|
---|
2622 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2623 |
|
---|
2624 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
2625 | if (m->queryInfoRunning)
|
---|
2626 | {
|
---|
2627 | /* Must not hold the media tree lock, as Medium::i_queryInfo needs this
|
---|
2628 | * lock and thus we would run into a deadlock here. */
|
---|
2629 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
2630 | while (m->queryInfoRunning)
|
---|
2631 | {
|
---|
2632 | mlock.release();
|
---|
2633 | /* must not hold the object lock now */
|
---|
2634 | Assert(!isWriteLockOnCurrentThread());
|
---|
2635 | {
|
---|
2636 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
2637 | }
|
---|
2638 | mlock.acquire();
|
---|
2639 | }
|
---|
2640 | }
|
---|
2641 |
|
---|
2642 | switch (m->state)
|
---|
2643 | {
|
---|
2644 | case MediumState_NotCreated:
|
---|
2645 | case MediumState_Created:
|
---|
2646 | case MediumState_Inaccessible:
|
---|
2647 | break;
|
---|
2648 | default:
|
---|
2649 | return i_setStateError();
|
---|
2650 | }
|
---|
2651 |
|
---|
2652 | settings::StringsMap::iterator it = m->mapProperties.find(aName);
|
---|
2653 | if ( !aName.startsWith("Special/")
|
---|
2654 | && !i_isPropertyForFilter(aName))
|
---|
2655 | {
|
---|
2656 | if (it == m->mapProperties.end())
|
---|
2657 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2658 | tr("Property '%s' does not exist"),
|
---|
2659 | aName.c_str());
|
---|
2660 | it->second = aValue;
|
---|
2661 | }
|
---|
2662 | else
|
---|
2663 | {
|
---|
2664 | if (it == m->mapProperties.end())
|
---|
2665 | {
|
---|
2666 | if (!aValue.isEmpty())
|
---|
2667 | m->mapProperties[aName] = aValue;
|
---|
2668 | }
|
---|
2669 | else
|
---|
2670 | {
|
---|
2671 | if (!aValue.isEmpty())
|
---|
2672 | it->second = aValue;
|
---|
2673 | else
|
---|
2674 | m->mapProperties.erase(it);
|
---|
2675 | }
|
---|
2676 | }
|
---|
2677 |
|
---|
2678 | // save the settings
|
---|
2679 | mlock.release();
|
---|
2680 | i_markRegistriesModified();
|
---|
2681 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2682 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
2683 |
|
---|
2684 | return S_OK;
|
---|
2685 | }
|
---|
2686 |
|
---|
2687 | HRESULT Medium::getProperties(const com::Utf8Str &aNames,
|
---|
2688 | std::vector<com::Utf8Str> &aReturnNames,
|
---|
2689 | std::vector<com::Utf8Str> &aReturnValues)
|
---|
2690 | {
|
---|
2691 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2692 |
|
---|
2693 | /// @todo make use of aNames according to the documentation
|
---|
2694 | NOREF(aNames);
|
---|
2695 |
|
---|
2696 | aReturnNames.resize(m->mapProperties.size());
|
---|
2697 | aReturnValues.resize(m->mapProperties.size());
|
---|
2698 | size_t i = 0;
|
---|
2699 | for (settings::StringsMap::const_iterator it = m->mapProperties.begin();
|
---|
2700 | it != m->mapProperties.end();
|
---|
2701 | ++it, ++i)
|
---|
2702 | {
|
---|
2703 | aReturnNames[i] = it->first;
|
---|
2704 | aReturnValues[i] = it->second;
|
---|
2705 | }
|
---|
2706 | return S_OK;
|
---|
2707 | }
|
---|
2708 |
|
---|
2709 | HRESULT Medium::setProperties(const std::vector<com::Utf8Str> &aNames,
|
---|
2710 | const std::vector<com::Utf8Str> &aValues)
|
---|
2711 | {
|
---|
2712 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2713 |
|
---|
2714 | /* first pass: validate names */
|
---|
2715 | for (size_t i = 0;
|
---|
2716 | i < aNames.size();
|
---|
2717 | ++i)
|
---|
2718 | {
|
---|
2719 | Utf8Str strName(aNames[i]);
|
---|
2720 | if ( !strName.startsWith("Special/")
|
---|
2721 | && !i_isPropertyForFilter(strName)
|
---|
2722 | && m->mapProperties.find(strName) == m->mapProperties.end())
|
---|
2723 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2724 | tr("Property '%s' does not exist"), strName.c_str());
|
---|
2725 | }
|
---|
2726 |
|
---|
2727 | /* second pass: assign */
|
---|
2728 | for (size_t i = 0;
|
---|
2729 | i < aNames.size();
|
---|
2730 | ++i)
|
---|
2731 | {
|
---|
2732 | Utf8Str strName(aNames[i]);
|
---|
2733 | Utf8Str strValue(aValues[i]);
|
---|
2734 | settings::StringsMap::iterator it = m->mapProperties.find(strName);
|
---|
2735 | if ( !strName.startsWith("Special/")
|
---|
2736 | && !i_isPropertyForFilter(strName))
|
---|
2737 | {
|
---|
2738 | AssertReturn(it != m->mapProperties.end(), E_FAIL);
|
---|
2739 | it->second = strValue;
|
---|
2740 | }
|
---|
2741 | else
|
---|
2742 | {
|
---|
2743 | if (it == m->mapProperties.end())
|
---|
2744 | {
|
---|
2745 | if (!strValue.isEmpty())
|
---|
2746 | m->mapProperties[strName] = strValue;
|
---|
2747 | }
|
---|
2748 | else
|
---|
2749 | {
|
---|
2750 | if (!strValue.isEmpty())
|
---|
2751 | it->second = strValue;
|
---|
2752 | else
|
---|
2753 | m->mapProperties.erase(it);
|
---|
2754 | }
|
---|
2755 | }
|
---|
2756 | }
|
---|
2757 |
|
---|
2758 | // save the settings
|
---|
2759 | mlock.release();
|
---|
2760 | i_markRegistriesModified();
|
---|
2761 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2762 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
2763 |
|
---|
2764 | return S_OK;
|
---|
2765 | }
|
---|
2766 |
|
---|
2767 | HRESULT Medium::createBaseStorage(LONG64 aLogicalSize,
|
---|
2768 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2769 | ComPtr<IProgress> &aProgress)
|
---|
2770 | {
|
---|
2771 | if (aLogicalSize < 0)
|
---|
2772 | return setError(E_INVALIDARG, tr("The medium size argument (%lld) is negative"), aLogicalSize);
|
---|
2773 |
|
---|
2774 | HRESULT rc = S_OK;
|
---|
2775 | ComObjPtr<Progress> pProgress;
|
---|
2776 | Medium::Task *pTask = NULL;
|
---|
2777 |
|
---|
2778 | try
|
---|
2779 | {
|
---|
2780 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2781 |
|
---|
2782 | ULONG mediumVariantFlags = 0;
|
---|
2783 |
|
---|
2784 | if (aVariant.size())
|
---|
2785 | {
|
---|
2786 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
2787 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
2788 | }
|
---|
2789 |
|
---|
2790 | mediumVariantFlags &= ((unsigned)~MediumVariant_Diff);
|
---|
2791 |
|
---|
2792 | if ( !(mediumVariantFlags & MediumVariant_Fixed)
|
---|
2793 | && !(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_CreateDynamic))
|
---|
2794 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2795 | tr("Medium format '%s' does not support dynamic storage creation"),
|
---|
2796 | m->strFormat.c_str());
|
---|
2797 |
|
---|
2798 | if ( (mediumVariantFlags & MediumVariant_Fixed)
|
---|
2799 | && !(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_CreateFixed))
|
---|
2800 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2801 | tr("Medium format '%s' does not support fixed storage creation"),
|
---|
2802 | m->strFormat.c_str());
|
---|
2803 |
|
---|
2804 | if ( (mediumVariantFlags & MediumVariant_Formatted)
|
---|
2805 | && i_getDeviceType() != DeviceType_Floppy)
|
---|
2806 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2807 | tr("Medium variant 'formatted' applies to floppy images only"));
|
---|
2808 |
|
---|
2809 | if (m->state != MediumState_NotCreated)
|
---|
2810 | throw i_setStateError();
|
---|
2811 |
|
---|
2812 | pProgress.createObject();
|
---|
2813 | rc = pProgress->init(m->pVirtualBox,
|
---|
2814 | static_cast<IMedium*>(this),
|
---|
2815 | (mediumVariantFlags & MediumVariant_Fixed)
|
---|
2816 | ? BstrFmt(tr("Creating fixed medium storage unit '%s'"), m->strLocationFull.c_str()).raw()
|
---|
2817 | : BstrFmt(tr("Creating dynamic medium storage unit '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
2818 | TRUE /* aCancelable */);
|
---|
2819 | if (FAILED(rc))
|
---|
2820 | throw rc;
|
---|
2821 |
|
---|
2822 | /* setup task object to carry out the operation asynchronously */
|
---|
2823 | pTask = new Medium::CreateBaseTask(this, pProgress, (uint64_t)aLogicalSize,
|
---|
2824 | (MediumVariant_T)mediumVariantFlags);
|
---|
2825 | rc = pTask->hrc();
|
---|
2826 | AssertComRC(rc);
|
---|
2827 | if (FAILED(rc))
|
---|
2828 | throw rc;
|
---|
2829 |
|
---|
2830 | m->state = MediumState_Creating;
|
---|
2831 | }
|
---|
2832 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2833 |
|
---|
2834 | if (SUCCEEDED(rc))
|
---|
2835 | {
|
---|
2836 | rc = pTask->createThread();
|
---|
2837 | pTask = NULL;
|
---|
2838 |
|
---|
2839 | if (SUCCEEDED(rc))
|
---|
2840 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2841 | }
|
---|
2842 | else if (pTask != NULL)
|
---|
2843 | delete pTask;
|
---|
2844 |
|
---|
2845 | return rc;
|
---|
2846 | }
|
---|
2847 |
|
---|
2848 | HRESULT Medium::deleteStorage(ComPtr<IProgress> &aProgress)
|
---|
2849 | {
|
---|
2850 | ComObjPtr<Progress> pProgress;
|
---|
2851 |
|
---|
2852 | MultiResult mrc = i_deleteStorage(&pProgress,
|
---|
2853 | false /* aWait */,
|
---|
2854 | true /* aNotify */);
|
---|
2855 | /* Must save the registries in any case, since an entry was removed. */
|
---|
2856 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2857 |
|
---|
2858 | if (SUCCEEDED(mrc))
|
---|
2859 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2860 |
|
---|
2861 | return mrc;
|
---|
2862 | }
|
---|
2863 |
|
---|
2864 | HRESULT Medium::createDiffStorage(AutoCaller &autoCaller,
|
---|
2865 | const ComPtr<IMedium> &aTarget,
|
---|
2866 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2867 | ComPtr<IProgress> &aProgress)
|
---|
2868 | {
|
---|
2869 | IMedium *aT = aTarget;
|
---|
2870 | ComObjPtr<Medium> diff = static_cast<Medium*>(aT);
|
---|
2871 |
|
---|
2872 | autoCaller.release();
|
---|
2873 |
|
---|
2874 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
2875 | * the pVirtualBox reference, see #uninit(). */
|
---|
2876 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
2877 |
|
---|
2878 | // we access m->pParent
|
---|
2879 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
2880 |
|
---|
2881 | autoCaller.add();
|
---|
2882 | if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
|
---|
2883 |
|
---|
2884 | AutoMultiWriteLock2 alock(this, diff COMMA_LOCKVAL_SRC_POS);
|
---|
2885 |
|
---|
2886 | if (m->type == MediumType_Writethrough)
|
---|
2887 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2888 | tr("Medium type of '%s' is Writethrough"),
|
---|
2889 | m->strLocationFull.c_str());
|
---|
2890 | else if (m->type == MediumType_Shareable)
|
---|
2891 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2892 | tr("Medium type of '%s' is Shareable"),
|
---|
2893 | m->strLocationFull.c_str());
|
---|
2894 | else if (m->type == MediumType_Readonly)
|
---|
2895 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2896 | tr("Medium type of '%s' is Readonly"),
|
---|
2897 | m->strLocationFull.c_str());
|
---|
2898 |
|
---|
2899 | /* Apply the normal locking logic to the entire chain. */
|
---|
2900 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
2901 | alock.release();
|
---|
2902 | autoCaller.release();
|
---|
2903 | treeLock.release();
|
---|
2904 | HRESULT rc = diff->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2905 | diff /* pToLockWrite */,
|
---|
2906 | false /* fMediumLockWriteAll */,
|
---|
2907 | this,
|
---|
2908 | *pMediumLockList);
|
---|
2909 | treeLock.acquire();
|
---|
2910 | autoCaller.add();
|
---|
2911 | if (FAILED(autoCaller.hrc()))
|
---|
2912 | rc = autoCaller.hrc();
|
---|
2913 | alock.acquire();
|
---|
2914 | if (FAILED(rc))
|
---|
2915 | {
|
---|
2916 | delete pMediumLockList;
|
---|
2917 | return rc;
|
---|
2918 | }
|
---|
2919 |
|
---|
2920 | alock.release();
|
---|
2921 | autoCaller.release();
|
---|
2922 | treeLock.release();
|
---|
2923 | rc = pMediumLockList->Lock();
|
---|
2924 | treeLock.acquire();
|
---|
2925 | autoCaller.add();
|
---|
2926 | if (FAILED(autoCaller.hrc()))
|
---|
2927 | rc = autoCaller.hrc();
|
---|
2928 | alock.acquire();
|
---|
2929 | if (FAILED(rc))
|
---|
2930 | {
|
---|
2931 | delete pMediumLockList;
|
---|
2932 |
|
---|
2933 | return setError(rc, tr("Could not lock medium when creating diff '%s'"),
|
---|
2934 | diff->i_getLocationFull().c_str());
|
---|
2935 | }
|
---|
2936 |
|
---|
2937 | Guid parentMachineRegistry;
|
---|
2938 | if (i_getFirstRegistryMachineId(parentMachineRegistry))
|
---|
2939 | {
|
---|
2940 | /* since this medium has been just created it isn't associated yet */
|
---|
2941 | diff->m->llRegistryIDs.push_back(parentMachineRegistry);
|
---|
2942 | alock.release();
|
---|
2943 | autoCaller.release();
|
---|
2944 | treeLock.release();
|
---|
2945 | diff->i_markRegistriesModified();
|
---|
2946 | treeLock.acquire();
|
---|
2947 | autoCaller.add();
|
---|
2948 | alock.acquire();
|
---|
2949 | }
|
---|
2950 |
|
---|
2951 | alock.release();
|
---|
2952 | autoCaller.release();
|
---|
2953 | treeLock.release();
|
---|
2954 |
|
---|
2955 | ComObjPtr<Progress> pProgress;
|
---|
2956 |
|
---|
2957 | ULONG mediumVariantFlags = 0;
|
---|
2958 |
|
---|
2959 | if (aVariant.size())
|
---|
2960 | {
|
---|
2961 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
2962 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
2963 | }
|
---|
2964 |
|
---|
2965 | if (mediumVariantFlags & MediumVariant_Formatted)
|
---|
2966 | {
|
---|
2967 | delete pMediumLockList;
|
---|
2968 | return setError(VBOX_E_NOT_SUPPORTED,
|
---|
2969 | tr("Medium variant 'formatted' applies to floppy images only"));
|
---|
2970 | }
|
---|
2971 |
|
---|
2972 | rc = i_createDiffStorage(diff, (MediumVariant_T)mediumVariantFlags, pMediumLockList,
|
---|
2973 | &pProgress, false /* aWait */, true /* aNotify */);
|
---|
2974 | if (FAILED(rc))
|
---|
2975 | delete pMediumLockList;
|
---|
2976 | else
|
---|
2977 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2978 |
|
---|
2979 | return rc;
|
---|
2980 | }
|
---|
2981 |
|
---|
2982 | HRESULT Medium::mergeTo(const ComPtr<IMedium> &aTarget,
|
---|
2983 | ComPtr<IProgress> &aProgress)
|
---|
2984 | {
|
---|
2985 | IMedium *aT = aTarget;
|
---|
2986 |
|
---|
2987 | ComAssertRet(aT != this, E_INVALIDARG);
|
---|
2988 |
|
---|
2989 | ComObjPtr<Medium> pTarget = static_cast<Medium*>(aT);
|
---|
2990 |
|
---|
2991 | bool fMergeForward = false;
|
---|
2992 | ComObjPtr<Medium> pParentForTarget;
|
---|
2993 | MediumLockList *pChildrenToReparent = NULL;
|
---|
2994 | MediumLockList *pMediumLockList = NULL;
|
---|
2995 |
|
---|
2996 | HRESULT rc = S_OK;
|
---|
2997 |
|
---|
2998 | rc = i_prepareMergeTo(pTarget, NULL, NULL, true, fMergeForward,
|
---|
2999 | pParentForTarget, pChildrenToReparent, pMediumLockList);
|
---|
3000 | if (FAILED(rc)) return rc;
|
---|
3001 |
|
---|
3002 | ComObjPtr<Progress> pProgress;
|
---|
3003 |
|
---|
3004 | rc = i_mergeTo(pTarget, fMergeForward, pParentForTarget, pChildrenToReparent,
|
---|
3005 | pMediumLockList, &pProgress, false /* aWait */, true /* aNotify */);
|
---|
3006 | if (FAILED(rc))
|
---|
3007 | i_cancelMergeTo(pChildrenToReparent, pMediumLockList);
|
---|
3008 | else
|
---|
3009 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3010 |
|
---|
3011 | return rc;
|
---|
3012 | }
|
---|
3013 |
|
---|
3014 | HRESULT Medium::cloneToBase(const ComPtr<IMedium> &aTarget,
|
---|
3015 | const std::vector<MediumVariant_T> &aVariant,
|
---|
3016 | ComPtr<IProgress> &aProgress)
|
---|
3017 | {
|
---|
3018 | return cloneTo(aTarget, aVariant, NULL, aProgress);
|
---|
3019 | }
|
---|
3020 |
|
---|
3021 | HRESULT Medium::cloneTo(const ComPtr<IMedium> &aTarget,
|
---|
3022 | const std::vector<MediumVariant_T> &aVariant,
|
---|
3023 | const ComPtr<IMedium> &aParent,
|
---|
3024 | ComPtr<IProgress> &aProgress)
|
---|
3025 | {
|
---|
3026 | /** @todo r=jack: Remove redundancy. Call Medium::resizeAndCloneTo. */
|
---|
3027 |
|
---|
3028 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
3029 | * to lock order violations, it probably causes lock order issues related
|
---|
3030 | * to the AutoCaller usage. */
|
---|
3031 | ComAssertRet(aTarget != this, E_INVALIDARG);
|
---|
3032 |
|
---|
3033 | IMedium *aT = aTarget;
|
---|
3034 | ComObjPtr<Medium> pTarget = static_cast<Medium*>(aT);
|
---|
3035 | ComObjPtr<Medium> pParent;
|
---|
3036 | if (aParent)
|
---|
3037 | {
|
---|
3038 | IMedium *aP = aParent;
|
---|
3039 | pParent = static_cast<Medium*>(aP);
|
---|
3040 | }
|
---|
3041 |
|
---|
3042 | HRESULT rc = S_OK;
|
---|
3043 | ComObjPtr<Progress> pProgress;
|
---|
3044 | Medium::Task *pTask = NULL;
|
---|
3045 |
|
---|
3046 | try
|
---|
3047 | {
|
---|
3048 | // locking: we need the tree lock first because we access parent pointers
|
---|
3049 | // and we need to write-lock the media involved
|
---|
3050 | uint32_t cHandles = 3;
|
---|
3051 | LockHandle* pHandles[4] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
3052 | this->lockHandle(),
|
---|
3053 | pTarget->lockHandle() };
|
---|
3054 | /* Only add parent to the lock if it is not null */
|
---|
3055 | if (!pParent.isNull())
|
---|
3056 | pHandles[cHandles++] = pParent->lockHandle();
|
---|
3057 | AutoWriteLock alock(cHandles,
|
---|
3058 | pHandles
|
---|
3059 | COMMA_LOCKVAL_SRC_POS);
|
---|
3060 |
|
---|
3061 | if ( pTarget->m->state != MediumState_NotCreated
|
---|
3062 | && pTarget->m->state != MediumState_Created)
|
---|
3063 | throw pTarget->i_setStateError();
|
---|
3064 |
|
---|
3065 | /* Build the source lock list. */
|
---|
3066 | MediumLockList *pSourceMediumLockList(new MediumLockList());
|
---|
3067 | alock.release();
|
---|
3068 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
3069 | NULL /* pToLockWrite */,
|
---|
3070 | false /* fMediumLockWriteAll */,
|
---|
3071 | NULL,
|
---|
3072 | *pSourceMediumLockList);
|
---|
3073 | alock.acquire();
|
---|
3074 | if (FAILED(rc))
|
---|
3075 | {
|
---|
3076 | delete pSourceMediumLockList;
|
---|
3077 | throw rc;
|
---|
3078 | }
|
---|
3079 |
|
---|
3080 | /* Build the target lock list (including the to-be parent chain). */
|
---|
3081 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
3082 | alock.release();
|
---|
3083 | rc = pTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
3084 | pTarget /* pToLockWrite */,
|
---|
3085 | false /* fMediumLockWriteAll */,
|
---|
3086 | pParent,
|
---|
3087 | *pTargetMediumLockList);
|
---|
3088 | alock.acquire();
|
---|
3089 | if (FAILED(rc))
|
---|
3090 | {
|
---|
3091 | delete pSourceMediumLockList;
|
---|
3092 | delete pTargetMediumLockList;
|
---|
3093 | throw rc;
|
---|
3094 | }
|
---|
3095 |
|
---|
3096 | alock.release();
|
---|
3097 | rc = pSourceMediumLockList->Lock();
|
---|
3098 | alock.acquire();
|
---|
3099 | if (FAILED(rc))
|
---|
3100 | {
|
---|
3101 | delete pSourceMediumLockList;
|
---|
3102 | delete pTargetMediumLockList;
|
---|
3103 | throw setError(rc,
|
---|
3104 | tr("Failed to lock source media '%s'"),
|
---|
3105 | i_getLocationFull().c_str());
|
---|
3106 | }
|
---|
3107 | alock.release();
|
---|
3108 | rc = pTargetMediumLockList->Lock();
|
---|
3109 | alock.acquire();
|
---|
3110 | if (FAILED(rc))
|
---|
3111 | {
|
---|
3112 | delete pSourceMediumLockList;
|
---|
3113 | delete pTargetMediumLockList;
|
---|
3114 | throw setError(rc,
|
---|
3115 | tr("Failed to lock target media '%s'"),
|
---|
3116 | pTarget->i_getLocationFull().c_str());
|
---|
3117 | }
|
---|
3118 |
|
---|
3119 | pProgress.createObject();
|
---|
3120 | rc = pProgress->init(m->pVirtualBox,
|
---|
3121 | static_cast <IMedium *>(this),
|
---|
3122 | BstrFmt(tr("Creating clone medium '%s'"), pTarget->m->strLocationFull.c_str()).raw(),
|
---|
3123 | TRUE /* aCancelable */);
|
---|
3124 | if (FAILED(rc))
|
---|
3125 | {
|
---|
3126 | delete pSourceMediumLockList;
|
---|
3127 | delete pTargetMediumLockList;
|
---|
3128 | throw rc;
|
---|
3129 | }
|
---|
3130 |
|
---|
3131 | ULONG mediumVariantFlags = 0;
|
---|
3132 |
|
---|
3133 | if (aVariant.size())
|
---|
3134 | {
|
---|
3135 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
3136 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
3137 | }
|
---|
3138 |
|
---|
3139 | if (mediumVariantFlags & MediumVariant_Formatted)
|
---|
3140 | {
|
---|
3141 | delete pSourceMediumLockList;
|
---|
3142 | delete pTargetMediumLockList;
|
---|
3143 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3144 | tr("Medium variant 'formatted' applies to floppy images only"));
|
---|
3145 | }
|
---|
3146 |
|
---|
3147 | /* setup task object to carry out the operation asynchronously */
|
---|
3148 | pTask = new Medium::CloneTask(this, pProgress, pTarget,
|
---|
3149 | (MediumVariant_T)mediumVariantFlags,
|
---|
3150 | pParent, UINT32_MAX, UINT32_MAX,
|
---|
3151 | pSourceMediumLockList, pTargetMediumLockList);
|
---|
3152 | rc = pTask->hrc();
|
---|
3153 | AssertComRC(rc);
|
---|
3154 | if (FAILED(rc))
|
---|
3155 | throw rc;
|
---|
3156 |
|
---|
3157 | if (pTarget->m->state == MediumState_NotCreated)
|
---|
3158 | pTarget->m->state = MediumState_Creating;
|
---|
3159 | }
|
---|
3160 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3161 |
|
---|
3162 | if (SUCCEEDED(rc))
|
---|
3163 | {
|
---|
3164 | rc = pTask->createThread();
|
---|
3165 | pTask = NULL;
|
---|
3166 | if (SUCCEEDED(rc))
|
---|
3167 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3168 | }
|
---|
3169 | else if (pTask != NULL)
|
---|
3170 | delete pTask;
|
---|
3171 |
|
---|
3172 | return rc;
|
---|
3173 | }
|
---|
3174 |
|
---|
3175 | /**
|
---|
3176 | * This is a helper function that combines the functionality of
|
---|
3177 | * Medium::cloneTo() and Medium::resize(). The target medium will take the
|
---|
3178 | * contents of the calling medium.
|
---|
3179 | *
|
---|
3180 | * @param aTarget Medium to resize and clone to
|
---|
3181 | * @param aLogicalSize Desired size for targer medium
|
---|
3182 | * @param aVariant
|
---|
3183 | * @param aParent
|
---|
3184 | * @param aProgress
|
---|
3185 | * @return HRESULT
|
---|
3186 | */
|
---|
3187 | HRESULT Medium::resizeAndCloneTo(const ComPtr<IMedium> &aTarget,
|
---|
3188 | LONG64 aLogicalSize,
|
---|
3189 | const std::vector<MediumVariant_T> &aVariant,
|
---|
3190 | const ComPtr<IMedium> &aParent,
|
---|
3191 | ComPtr<IProgress> &aProgress)
|
---|
3192 | {
|
---|
3193 | /* Check for valid args */
|
---|
3194 | ComAssertRet(aTarget != this, E_INVALIDARG);
|
---|
3195 | CheckComArgExpr(aLogicalSize, aLogicalSize >= 0);
|
---|
3196 |
|
---|
3197 | /* Convert args to usable/needed types */
|
---|
3198 | IMedium *aT = aTarget;
|
---|
3199 | ComObjPtr<Medium> pTarget = static_cast<Medium*>(aT);
|
---|
3200 | ComObjPtr<Medium> pParent;
|
---|
3201 | if (aParent)
|
---|
3202 | {
|
---|
3203 | IMedium *aP = aParent;
|
---|
3204 | pParent = static_cast<Medium*>(aP);
|
---|
3205 | }
|
---|
3206 |
|
---|
3207 | /* Set up variables. Fetch needed data in lockable blocks */
|
---|
3208 | HRESULT rc = S_OK;
|
---|
3209 | Medium::Task *pTask = NULL;
|
---|
3210 |
|
---|
3211 | Utf8Str strSourceName;
|
---|
3212 | {
|
---|
3213 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3214 | strSourceName = i_getName();
|
---|
3215 | }
|
---|
3216 |
|
---|
3217 | uint64_t uTargetExistingSize = 0;
|
---|
3218 | Utf8Str strTargetName;
|
---|
3219 | {
|
---|
3220 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
3221 | uTargetExistingSize = pTarget->i_getLogicalSize();
|
---|
3222 | strTargetName = pTarget->i_getName();
|
---|
3223 | }
|
---|
3224 |
|
---|
3225 | /* Set up internal multi-subprocess progress object */
|
---|
3226 | ComObjPtr<Progress> pProgress;
|
---|
3227 | pProgress.createObject();
|
---|
3228 | rc = pProgress->init(m->pVirtualBox,
|
---|
3229 | static_cast<IMedium*>(this),
|
---|
3230 | BstrFmt(tr("Resizing medium and cloning into it")).raw(),
|
---|
3231 | TRUE, /* aCancelable */
|
---|
3232 | 2, /* Number of opearations */
|
---|
3233 | BstrFmt(tr("Resizing medium before clone")).raw()
|
---|
3234 | );
|
---|
3235 |
|
---|
3236 | if (FAILED(rc))
|
---|
3237 | return rc;
|
---|
3238 |
|
---|
3239 | /* If target does not exist, handle resize. */
|
---|
3240 | if (pTarget->m->state != MediumState_NotCreated && aLogicalSize > 0)
|
---|
3241 | {
|
---|
3242 | if ((LONG64)uTargetExistingSize != aLogicalSize) {
|
---|
3243 | if (!i_isMediumFormatFile())
|
---|
3244 | {
|
---|
3245 | rc = setError(VBOX_E_NOT_SUPPORTED,
|
---|
3246 | tr("Sizes of '%s' and '%s' are different and \
|
---|
3247 | medium format does not support resing"),
|
---|
3248 | strSourceName.c_str(), strTargetName.c_str());
|
---|
3249 | return rc;
|
---|
3250 | }
|
---|
3251 |
|
---|
3252 | /**
|
---|
3253 | * Need to lock the target medium as i_resize does do so
|
---|
3254 | * automatically.
|
---|
3255 | */
|
---|
3256 |
|
---|
3257 | ComPtr<IToken> pToken;
|
---|
3258 | rc = pTarget->LockWrite(pToken.asOutParam());
|
---|
3259 |
|
---|
3260 | if (FAILED(rc)) return rc;
|
---|
3261 |
|
---|
3262 | /**
|
---|
3263 | * Have to make own lock list, because "resize" method resizes only
|
---|
3264 | * last image in the lock chain.
|
---|
3265 | */
|
---|
3266 |
|
---|
3267 | MediumLockList* pMediumLockListForResize = new MediumLockList();
|
---|
3268 | pMediumLockListForResize->Append(pTarget, pTarget->m->state == MediumState_LockedWrite);
|
---|
3269 |
|
---|
3270 | rc = pMediumLockListForResize->Lock(true /* fSkipOverLockedMedia */);
|
---|
3271 |
|
---|
3272 | if (FAILED(rc))
|
---|
3273 | {
|
---|
3274 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3275 | rc = setError(rc,
|
---|
3276 | tr("Failed to lock the medium '%s' to resize before merge"),
|
---|
3277 | strTargetName.c_str());
|
---|
3278 | delete pMediumLockListForResize;
|
---|
3279 | return rc;
|
---|
3280 | }
|
---|
3281 |
|
---|
3282 |
|
---|
3283 | rc = pTarget->i_resize((uint64_t)aLogicalSize, pMediumLockListForResize, &pProgress, true, false);
|
---|
3284 |
|
---|
3285 | if (FAILED(rc))
|
---|
3286 | {
|
---|
3287 | /* No need to setError becasue i_resize and i_taskResizeHandler handle this automatically. */
|
---|
3288 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3289 | delete pMediumLockListForResize;
|
---|
3290 | return rc;
|
---|
3291 | }
|
---|
3292 |
|
---|
3293 | delete pMediumLockListForResize;
|
---|
3294 |
|
---|
3295 | pTarget->m->logicalSize = (uint64_t)aLogicalSize;
|
---|
3296 |
|
---|
3297 | pToken->Abandon();
|
---|
3298 | pToken.setNull();
|
---|
3299 | }
|
---|
3300 | }
|
---|
3301 |
|
---|
3302 | /* Report progress to supplied progress argument */
|
---|
3303 | if (SUCCEEDED(rc))
|
---|
3304 | {
|
---|
3305 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3306 | }
|
---|
3307 |
|
---|
3308 | try
|
---|
3309 | {
|
---|
3310 | // locking: we need the tree lock first because we access parent pointers
|
---|
3311 | // and we need to write-lock the media involved
|
---|
3312 | uint32_t cHandles = 3;
|
---|
3313 | LockHandle* pHandles[4] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
3314 | this->lockHandle(),
|
---|
3315 | pTarget->lockHandle() };
|
---|
3316 | /* Only add parent to the lock if it is not null */
|
---|
3317 | if (!pParent.isNull())
|
---|
3318 | pHandles[cHandles++] = pParent->lockHandle();
|
---|
3319 | AutoWriteLock alock(cHandles,
|
---|
3320 | pHandles
|
---|
3321 | COMMA_LOCKVAL_SRC_POS);
|
---|
3322 |
|
---|
3323 | if ( pTarget->m->state != MediumState_NotCreated
|
---|
3324 | && pTarget->m->state != MediumState_Created)
|
---|
3325 | throw pTarget->i_setStateError();
|
---|
3326 |
|
---|
3327 | /* Build the source lock list. */
|
---|
3328 | MediumLockList *pSourceMediumLockList(new MediumLockList());
|
---|
3329 | alock.release();
|
---|
3330 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
3331 | NULL /* pToLockWrite */,
|
---|
3332 | false /* fMediumLockWriteAll */,
|
---|
3333 | NULL,
|
---|
3334 | *pSourceMediumLockList);
|
---|
3335 | alock.acquire();
|
---|
3336 | if (FAILED(rc))
|
---|
3337 | {
|
---|
3338 | delete pSourceMediumLockList;
|
---|
3339 | throw rc;
|
---|
3340 | }
|
---|
3341 |
|
---|
3342 | /* Build the target lock list (including the to-be parent chain). */
|
---|
3343 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
3344 | alock.release();
|
---|
3345 | rc = pTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
3346 | pTarget /* pToLockWrite */,
|
---|
3347 | false /* fMediumLockWriteAll */,
|
---|
3348 | pParent,
|
---|
3349 | *pTargetMediumLockList);
|
---|
3350 | alock.acquire();
|
---|
3351 | if (FAILED(rc))
|
---|
3352 | {
|
---|
3353 | delete pSourceMediumLockList;
|
---|
3354 | delete pTargetMediumLockList;
|
---|
3355 | throw rc;
|
---|
3356 | }
|
---|
3357 |
|
---|
3358 | alock.release();
|
---|
3359 | rc = pSourceMediumLockList->Lock();
|
---|
3360 | alock.acquire();
|
---|
3361 | if (FAILED(rc))
|
---|
3362 | {
|
---|
3363 | delete pSourceMediumLockList;
|
---|
3364 | delete pTargetMediumLockList;
|
---|
3365 | throw setError(rc,
|
---|
3366 | tr("Failed to lock source media '%s'"),
|
---|
3367 | i_getLocationFull().c_str());
|
---|
3368 | }
|
---|
3369 | alock.release();
|
---|
3370 | rc = pTargetMediumLockList->Lock();
|
---|
3371 | alock.acquire();
|
---|
3372 | if (FAILED(rc))
|
---|
3373 | {
|
---|
3374 | delete pSourceMediumLockList;
|
---|
3375 | delete pTargetMediumLockList;
|
---|
3376 | throw setError(rc,
|
---|
3377 | tr("Failed to lock target media '%s'"),
|
---|
3378 | pTarget->i_getLocationFull().c_str());
|
---|
3379 | }
|
---|
3380 |
|
---|
3381 | ULONG mediumVariantFlags = 0;
|
---|
3382 |
|
---|
3383 | if (aVariant.size())
|
---|
3384 | {
|
---|
3385 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
3386 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
3387 | }
|
---|
3388 |
|
---|
3389 | if (mediumVariantFlags & MediumVariant_Formatted)
|
---|
3390 | {
|
---|
3391 | delete pSourceMediumLockList;
|
---|
3392 | delete pTargetMediumLockList;
|
---|
3393 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3394 | tr("Medium variant 'formatted' applies to floppy images only"));
|
---|
3395 | }
|
---|
3396 |
|
---|
3397 | if (pTarget->m->state != MediumState_NotCreated || aLogicalSize == 0)
|
---|
3398 | {
|
---|
3399 | /* setup task object to carry out the operation asynchronously */
|
---|
3400 | pTask = new Medium::CloneTask(this, pProgress, pTarget,
|
---|
3401 | (MediumVariant_T)mediumVariantFlags,
|
---|
3402 | pParent, UINT32_MAX, UINT32_MAX,
|
---|
3403 | pSourceMediumLockList, pTargetMediumLockList,
|
---|
3404 | false, false, true, 0);
|
---|
3405 | }
|
---|
3406 | else
|
---|
3407 | {
|
---|
3408 | /* setup task object to carry out the operation asynchronously */
|
---|
3409 | pTask = new Medium::CloneTask(this, pProgress, pTarget,
|
---|
3410 | (MediumVariant_T)mediumVariantFlags,
|
---|
3411 | pParent, UINT32_MAX, UINT32_MAX,
|
---|
3412 | pSourceMediumLockList, pTargetMediumLockList,
|
---|
3413 | false, false, true, (uint64_t)aLogicalSize);
|
---|
3414 | }
|
---|
3415 |
|
---|
3416 | rc = pTask->hrc();
|
---|
3417 | AssertComRC(rc);
|
---|
3418 | if (FAILED(rc))
|
---|
3419 | throw rc;
|
---|
3420 |
|
---|
3421 | if (pTarget->m->state == MediumState_NotCreated)
|
---|
3422 | pTarget->m->state = MediumState_Creating;
|
---|
3423 | }
|
---|
3424 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3425 |
|
---|
3426 | if (SUCCEEDED(rc))
|
---|
3427 | {
|
---|
3428 | rc = pTask->createThread();
|
---|
3429 | pTask = NULL;
|
---|
3430 | if (SUCCEEDED(rc))
|
---|
3431 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3432 | }
|
---|
3433 | else if (pTask != NULL)
|
---|
3434 | delete pTask;
|
---|
3435 |
|
---|
3436 | return rc;
|
---|
3437 | }
|
---|
3438 |
|
---|
3439 | HRESULT Medium::moveTo(AutoCaller &autoCaller, const com::Utf8Str &aLocation, ComPtr<IProgress> &aProgress)
|
---|
3440 | {
|
---|
3441 | ComObjPtr<Medium> pParent;
|
---|
3442 | ComObjPtr<Progress> pProgress;
|
---|
3443 | HRESULT rc = S_OK;
|
---|
3444 | Medium::Task *pTask = NULL;
|
---|
3445 |
|
---|
3446 | try
|
---|
3447 | {
|
---|
3448 | /// @todo NEWMEDIA for file names, add the default extension if no extension
|
---|
3449 | /// is present (using the information from the VD backend which also implies
|
---|
3450 | /// that one more parameter should be passed to moveTo() requesting
|
---|
3451 | /// that functionality since it is only allowed when called from this method
|
---|
3452 |
|
---|
3453 | /// @todo NEWMEDIA rename the file and set m->location on success, then save
|
---|
3454 | /// the global registry (and local registries of portable VMs referring to
|
---|
3455 | /// this medium), this will also require to add the mRegistered flag to data
|
---|
3456 |
|
---|
3457 | autoCaller.release();
|
---|
3458 |
|
---|
3459 | // locking: we need the tree lock first because we access parent pointers
|
---|
3460 | // and we need to write-lock the media involved
|
---|
3461 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3462 |
|
---|
3463 | autoCaller.add();
|
---|
3464 | AssertComRCThrowRC(autoCaller.hrc());
|
---|
3465 |
|
---|
3466 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3467 |
|
---|
3468 | /* play with locations */
|
---|
3469 | {
|
---|
3470 | /* get source path and filename */
|
---|
3471 | Utf8Str sourcePath = i_getLocationFull();
|
---|
3472 | Utf8Str sourceFName = i_getName();
|
---|
3473 |
|
---|
3474 | if (aLocation.isEmpty())
|
---|
3475 | {
|
---|
3476 | rc = setErrorVrc(VERR_PATH_ZERO_LENGTH,
|
---|
3477 | tr("Medium '%s' can't be moved. Destination path is empty."),
|
---|
3478 | i_getLocationFull().c_str());
|
---|
3479 | throw rc;
|
---|
3480 | }
|
---|
3481 |
|
---|
3482 | /* extract destination path and filename */
|
---|
3483 | Utf8Str destPath(aLocation);
|
---|
3484 | Utf8Str destFName(destPath);
|
---|
3485 | destFName.stripPath();
|
---|
3486 |
|
---|
3487 | if (destFName.isNotEmpty() && !RTPathHasSuffix(destFName.c_str()))
|
---|
3488 | {
|
---|
3489 | /*
|
---|
3490 | * The target path has no filename: Either "/path/to/new/location" or
|
---|
3491 | * just "newname" (no trailing backslash or there is no filename extension).
|
---|
3492 | */
|
---|
3493 | if (destPath.equals(destFName))
|
---|
3494 | {
|
---|
3495 | /* new path contains only "newname", no path, no extension */
|
---|
3496 | destFName.append(RTPathSuffix(sourceFName.c_str()));
|
---|
3497 | destPath = destFName;
|
---|
3498 | }
|
---|
3499 | else
|
---|
3500 | {
|
---|
3501 | /* new path looks like "/path/to/new/location" */
|
---|
3502 | destFName.setNull();
|
---|
3503 | destPath.append(RTPATH_SLASH);
|
---|
3504 | }
|
---|
3505 | }
|
---|
3506 |
|
---|
3507 | if (destFName.isEmpty())
|
---|
3508 | {
|
---|
3509 | /* No target name */
|
---|
3510 | destPath.append(sourceFName);
|
---|
3511 | }
|
---|
3512 | else
|
---|
3513 | {
|
---|
3514 | if (destPath.equals(destFName))
|
---|
3515 | {
|
---|
3516 | /*
|
---|
3517 | * The target path contains of only a filename without a directory.
|
---|
3518 | * Move the medium within the source directory to the new name
|
---|
3519 | * (actually rename operation).
|
---|
3520 | * Scratches sourcePath!
|
---|
3521 | */
|
---|
3522 | destPath = sourcePath.stripFilename().append(RTPATH_SLASH).append(destFName);
|
---|
3523 | }
|
---|
3524 |
|
---|
3525 | const char *pszSuffix = RTPathSuffix(sourceFName.c_str());
|
---|
3526 |
|
---|
3527 | /* Suffix is empty and one is deduced from the medium format */
|
---|
3528 | if (pszSuffix == NULL)
|
---|
3529 | {
|
---|
3530 | Utf8Str strExt = i_getFormat();
|
---|
3531 | if (strExt.compare("RAW", Utf8Str::CaseInsensitive) == 0)
|
---|
3532 | {
|
---|
3533 | DeviceType_T devType = i_getDeviceType();
|
---|
3534 | switch (devType)
|
---|
3535 | {
|
---|
3536 | case DeviceType_DVD:
|
---|
3537 | strExt = "iso";
|
---|
3538 | break;
|
---|
3539 | case DeviceType_Floppy:
|
---|
3540 | strExt = "img";
|
---|
3541 | break;
|
---|
3542 | default:
|
---|
3543 | rc = setErrorVrc(VERR_NOT_A_FILE, /** @todo r=bird: Mixing status codes again. */
|
---|
3544 | tr("Medium '%s' has RAW type. \"Move\" operation isn't supported for this type."),
|
---|
3545 | i_getLocationFull().c_str());
|
---|
3546 | throw rc;
|
---|
3547 | }
|
---|
3548 | }
|
---|
3549 | else if (strExt.compare("Parallels", Utf8Str::CaseInsensitive) == 0)
|
---|
3550 | {
|
---|
3551 | strExt = "hdd";
|
---|
3552 | }
|
---|
3553 |
|
---|
3554 | /* Set the target extension like on the source. Any conversions are prohibited */
|
---|
3555 | strExt.toLower();
|
---|
3556 | destPath.stripSuffix().append('.').append(strExt);
|
---|
3557 | }
|
---|
3558 | else
|
---|
3559 | destPath.stripSuffix().append(pszSuffix);
|
---|
3560 | }
|
---|
3561 |
|
---|
3562 | /* Simple check for existence */
|
---|
3563 | if (RTFileExists(destPath.c_str()))
|
---|
3564 | {
|
---|
3565 | rc = setError(VBOX_E_FILE_ERROR,
|
---|
3566 | tr("The given path '%s' is an existing file. Delete or rename this file."),
|
---|
3567 | destPath.c_str());
|
---|
3568 | throw rc;
|
---|
3569 | }
|
---|
3570 |
|
---|
3571 | if (!i_isMediumFormatFile())
|
---|
3572 | {
|
---|
3573 | rc = setErrorVrc(VERR_NOT_A_FILE,
|
---|
3574 | tr("Medium '%s' isn't a file object. \"Move\" operation isn't supported."),
|
---|
3575 | i_getLocationFull().c_str());
|
---|
3576 | throw rc;
|
---|
3577 | }
|
---|
3578 | /* Path must be absolute */
|
---|
3579 | if (!RTPathStartsWithRoot(destPath.c_str()))
|
---|
3580 | {
|
---|
3581 | rc = setError(VBOX_E_FILE_ERROR,
|
---|
3582 | tr("The given path '%s' is not fully qualified"),
|
---|
3583 | destPath.c_str());
|
---|
3584 | throw rc;
|
---|
3585 | }
|
---|
3586 | /* Check path for a new file object */
|
---|
3587 | rc = VirtualBox::i_ensureFilePathExists(destPath, true);
|
---|
3588 | if (FAILED(rc))
|
---|
3589 | throw rc;
|
---|
3590 |
|
---|
3591 | /* Set needed variables for "moving" procedure. It'll be used later in separate thread task */
|
---|
3592 | rc = i_preparationForMoving(destPath);
|
---|
3593 | if (FAILED(rc))
|
---|
3594 | {
|
---|
3595 | rc = setErrorVrc(VERR_NO_CHANGE,
|
---|
3596 | tr("Medium '%s' is already in the correct location"),
|
---|
3597 | i_getLocationFull().c_str());
|
---|
3598 | throw rc;
|
---|
3599 | }
|
---|
3600 | }
|
---|
3601 |
|
---|
3602 | /* Check VMs which have this medium attached to*/
|
---|
3603 | std::vector<com::Guid> aMachineIds;
|
---|
3604 | rc = getMachineIds(aMachineIds);
|
---|
3605 | std::vector<com::Guid>::const_iterator currMachineID = aMachineIds.begin();
|
---|
3606 | std::vector<com::Guid>::const_iterator lastMachineID = aMachineIds.end();
|
---|
3607 |
|
---|
3608 | while (currMachineID != lastMachineID)
|
---|
3609 | {
|
---|
3610 | Guid id(*currMachineID);
|
---|
3611 | ComObjPtr<Machine> aMachine;
|
---|
3612 |
|
---|
3613 | alock.release();
|
---|
3614 | autoCaller.release();
|
---|
3615 | treeLock.release();
|
---|
3616 | rc = m->pVirtualBox->i_findMachine(id, false, true, &aMachine);
|
---|
3617 | treeLock.acquire();
|
---|
3618 | autoCaller.add();
|
---|
3619 | AssertComRCThrowRC(autoCaller.hrc());
|
---|
3620 | alock.acquire();
|
---|
3621 |
|
---|
3622 | if (SUCCEEDED(rc))
|
---|
3623 | {
|
---|
3624 | ComObjPtr<SessionMachine> sm;
|
---|
3625 | ComPtr<IInternalSessionControl> ctl;
|
---|
3626 |
|
---|
3627 | alock.release();
|
---|
3628 | autoCaller.release();
|
---|
3629 | treeLock.release();
|
---|
3630 | bool ses = aMachine->i_isSessionOpenVM(sm, &ctl);
|
---|
3631 | treeLock.acquire();
|
---|
3632 | autoCaller.add();
|
---|
3633 | AssertComRCThrowRC(autoCaller.hrc());
|
---|
3634 | alock.acquire();
|
---|
3635 |
|
---|
3636 | if (ses)
|
---|
3637 | {
|
---|
3638 | rc = setError(VBOX_E_INVALID_VM_STATE,
|
---|
3639 | tr("At least the VM '%s' to whom this medium '%s' attached has currently an opened session. Stop all VMs before relocating this medium"),
|
---|
3640 | id.toString().c_str(),
|
---|
3641 | i_getLocationFull().c_str());
|
---|
3642 | throw rc;
|
---|
3643 | }
|
---|
3644 | }
|
---|
3645 | ++currMachineID;
|
---|
3646 | }
|
---|
3647 |
|
---|
3648 | /* Build the source lock list. */
|
---|
3649 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3650 | alock.release();
|
---|
3651 | autoCaller.release();
|
---|
3652 | treeLock.release();
|
---|
3653 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
3654 | this /* pToLockWrite */,
|
---|
3655 | true /* fMediumLockWriteAll */,
|
---|
3656 | NULL,
|
---|
3657 | *pMediumLockList);
|
---|
3658 | treeLock.acquire();
|
---|
3659 | autoCaller.add();
|
---|
3660 | AssertComRCThrowRC(autoCaller.hrc());
|
---|
3661 | alock.acquire();
|
---|
3662 | if (FAILED(rc))
|
---|
3663 | {
|
---|
3664 | delete pMediumLockList;
|
---|
3665 | throw setError(rc,
|
---|
3666 | tr("Failed to create medium lock list for '%s'"),
|
---|
3667 | i_getLocationFull().c_str());
|
---|
3668 | }
|
---|
3669 | alock.release();
|
---|
3670 | autoCaller.release();
|
---|
3671 | treeLock.release();
|
---|
3672 | rc = pMediumLockList->Lock();
|
---|
3673 | treeLock.acquire();
|
---|
3674 | autoCaller.add();
|
---|
3675 | AssertComRCThrowRC(autoCaller.hrc());
|
---|
3676 | alock.acquire();
|
---|
3677 | if (FAILED(rc))
|
---|
3678 | {
|
---|
3679 | delete pMediumLockList;
|
---|
3680 | throw setError(rc,
|
---|
3681 | tr("Failed to lock media '%s'"),
|
---|
3682 | i_getLocationFull().c_str());
|
---|
3683 | }
|
---|
3684 |
|
---|
3685 | pProgress.createObject();
|
---|
3686 | rc = pProgress->init(m->pVirtualBox,
|
---|
3687 | static_cast <IMedium *>(this),
|
---|
3688 | BstrFmt(tr("Moving medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3689 | TRUE /* aCancelable */);
|
---|
3690 |
|
---|
3691 | /* Do the disk moving. */
|
---|
3692 | if (SUCCEEDED(rc))
|
---|
3693 | {
|
---|
3694 | ULONG mediumVariantFlags = i_getVariant();
|
---|
3695 |
|
---|
3696 | /* setup task object to carry out the operation asynchronously */
|
---|
3697 | pTask = new Medium::MoveTask(this, pProgress,
|
---|
3698 | (MediumVariant_T)mediumVariantFlags,
|
---|
3699 | pMediumLockList);
|
---|
3700 | rc = pTask->hrc();
|
---|
3701 | AssertComRC(rc);
|
---|
3702 | if (FAILED(rc))
|
---|
3703 | throw rc;
|
---|
3704 | }
|
---|
3705 |
|
---|
3706 | }
|
---|
3707 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3708 |
|
---|
3709 | if (SUCCEEDED(rc))
|
---|
3710 | {
|
---|
3711 | rc = pTask->createThread();
|
---|
3712 | pTask = NULL;
|
---|
3713 | if (SUCCEEDED(rc))
|
---|
3714 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3715 | }
|
---|
3716 | else
|
---|
3717 | {
|
---|
3718 | if (pTask)
|
---|
3719 | delete pTask;
|
---|
3720 | }
|
---|
3721 |
|
---|
3722 | return rc;
|
---|
3723 | }
|
---|
3724 |
|
---|
3725 | HRESULT Medium::setLocation(const com::Utf8Str &aLocation)
|
---|
3726 | {
|
---|
3727 | HRESULT rc = S_OK;
|
---|
3728 |
|
---|
3729 | try
|
---|
3730 | {
|
---|
3731 | // locking: we need the tree lock first because we access parent pointers
|
---|
3732 | // and we need to write-lock the media involved
|
---|
3733 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3734 |
|
---|
3735 | AutoCaller autoCaller(this);
|
---|
3736 | AssertComRCThrowRC(autoCaller.hrc());
|
---|
3737 |
|
---|
3738 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3739 |
|
---|
3740 | Utf8Str destPath(aLocation);
|
---|
3741 |
|
---|
3742 | // some check for file based medium
|
---|
3743 | if (i_isMediumFormatFile())
|
---|
3744 | {
|
---|
3745 | /* Path must be absolute */
|
---|
3746 | if (!RTPathStartsWithRoot(destPath.c_str()))
|
---|
3747 | {
|
---|
3748 | rc = setError(VBOX_E_FILE_ERROR,
|
---|
3749 | tr("The given path '%s' is not fully qualified"),
|
---|
3750 | destPath.c_str());
|
---|
3751 | throw rc;
|
---|
3752 | }
|
---|
3753 |
|
---|
3754 | /* Simple check for existence */
|
---|
3755 | if (!RTFileExists(destPath.c_str()))
|
---|
3756 | {
|
---|
3757 | rc = setError(VBOX_E_FILE_ERROR,
|
---|
3758 | tr("The given path '%s' is not an existing file. New location is invalid."),
|
---|
3759 | destPath.c_str());
|
---|
3760 | throw rc;
|
---|
3761 | }
|
---|
3762 | }
|
---|
3763 |
|
---|
3764 | /* Check VMs which have this medium attached to*/
|
---|
3765 | std::vector<com::Guid> aMachineIds;
|
---|
3766 | rc = getMachineIds(aMachineIds);
|
---|
3767 |
|
---|
3768 | // switch locks only if there are machines with this medium attached
|
---|
3769 | if (!aMachineIds.empty())
|
---|
3770 | {
|
---|
3771 | std::vector<com::Guid>::const_iterator currMachineID = aMachineIds.begin();
|
---|
3772 | std::vector<com::Guid>::const_iterator lastMachineID = aMachineIds.end();
|
---|
3773 |
|
---|
3774 | alock.release();
|
---|
3775 | autoCaller.release();
|
---|
3776 | treeLock.release();
|
---|
3777 |
|
---|
3778 | while (currMachineID != lastMachineID)
|
---|
3779 | {
|
---|
3780 | Guid id(*currMachineID);
|
---|
3781 | ComObjPtr<Machine> aMachine;
|
---|
3782 | rc = m->pVirtualBox->i_findMachine(id, false, true, &aMachine);
|
---|
3783 | if (SUCCEEDED(rc))
|
---|
3784 | {
|
---|
3785 | ComObjPtr<SessionMachine> sm;
|
---|
3786 | ComPtr<IInternalSessionControl> ctl;
|
---|
3787 |
|
---|
3788 | bool ses = aMachine->i_isSessionOpenVM(sm, &ctl);
|
---|
3789 | if (ses)
|
---|
3790 | {
|
---|
3791 | treeLock.acquire();
|
---|
3792 | autoCaller.add();
|
---|
3793 | AssertComRCThrowRC(autoCaller.hrc());
|
---|
3794 | alock.acquire();
|
---|
3795 |
|
---|
3796 | rc = setError(VBOX_E_INVALID_VM_STATE,
|
---|
3797 | 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"),
|
---|
3798 | id.toString().c_str(),
|
---|
3799 | i_getLocationFull().c_str());
|
---|
3800 | throw rc;
|
---|
3801 | }
|
---|
3802 | }
|
---|
3803 | ++currMachineID;
|
---|
3804 | }
|
---|
3805 |
|
---|
3806 | treeLock.acquire();
|
---|
3807 | autoCaller.add();
|
---|
3808 | AssertComRCThrowRC(autoCaller.hrc());
|
---|
3809 | alock.acquire();
|
---|
3810 | }
|
---|
3811 |
|
---|
3812 | m->strLocationFull = destPath;
|
---|
3813 |
|
---|
3814 | // save the settings
|
---|
3815 | alock.release();
|
---|
3816 | autoCaller.release();
|
---|
3817 | treeLock.release();
|
---|
3818 |
|
---|
3819 | i_markRegistriesModified();
|
---|
3820 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
3821 |
|
---|
3822 | MediumState_T mediumState;
|
---|
3823 | refreshState(autoCaller, &mediumState);
|
---|
3824 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
3825 | }
|
---|
3826 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3827 |
|
---|
3828 | return rc;
|
---|
3829 | }
|
---|
3830 |
|
---|
3831 | HRESULT Medium::compact(ComPtr<IProgress> &aProgress)
|
---|
3832 | {
|
---|
3833 | HRESULT rc = S_OK;
|
---|
3834 | ComObjPtr<Progress> pProgress;
|
---|
3835 | Medium::Task *pTask = NULL;
|
---|
3836 |
|
---|
3837 | try
|
---|
3838 | {
|
---|
3839 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3840 |
|
---|
3841 | /* Build the medium lock list. */
|
---|
3842 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3843 | alock.release();
|
---|
3844 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
3845 | this /* pToLockWrite */,
|
---|
3846 | false /* fMediumLockWriteAll */,
|
---|
3847 | NULL,
|
---|
3848 | *pMediumLockList);
|
---|
3849 | alock.acquire();
|
---|
3850 | if (FAILED(rc))
|
---|
3851 | {
|
---|
3852 | delete pMediumLockList;
|
---|
3853 | throw rc;
|
---|
3854 | }
|
---|
3855 |
|
---|
3856 | alock.release();
|
---|
3857 | rc = pMediumLockList->Lock();
|
---|
3858 | alock.acquire();
|
---|
3859 | if (FAILED(rc))
|
---|
3860 | {
|
---|
3861 | delete pMediumLockList;
|
---|
3862 | throw setError(rc,
|
---|
3863 | tr("Failed to lock media when compacting '%s'"),
|
---|
3864 | i_getLocationFull().c_str());
|
---|
3865 | }
|
---|
3866 |
|
---|
3867 | pProgress.createObject();
|
---|
3868 | rc = pProgress->init(m->pVirtualBox,
|
---|
3869 | static_cast <IMedium *>(this),
|
---|
3870 | BstrFmt(tr("Compacting medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3871 | TRUE /* aCancelable */);
|
---|
3872 | if (FAILED(rc))
|
---|
3873 | {
|
---|
3874 | delete pMediumLockList;
|
---|
3875 | throw rc;
|
---|
3876 | }
|
---|
3877 |
|
---|
3878 | /* setup task object to carry out the operation asynchronously */
|
---|
3879 | pTask = new Medium::CompactTask(this, pProgress, pMediumLockList);
|
---|
3880 | rc = pTask->hrc();
|
---|
3881 | AssertComRC(rc);
|
---|
3882 | if (FAILED(rc))
|
---|
3883 | throw rc;
|
---|
3884 | }
|
---|
3885 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3886 |
|
---|
3887 | if (SUCCEEDED(rc))
|
---|
3888 | {
|
---|
3889 | rc = pTask->createThread();
|
---|
3890 | pTask = NULL;
|
---|
3891 | if (SUCCEEDED(rc))
|
---|
3892 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3893 | }
|
---|
3894 | else if (pTask != NULL)
|
---|
3895 | delete pTask;
|
---|
3896 |
|
---|
3897 | return rc;
|
---|
3898 | }
|
---|
3899 |
|
---|
3900 | HRESULT Medium::resize(LONG64 aLogicalSize,
|
---|
3901 | ComPtr<IProgress> &aProgress)
|
---|
3902 | {
|
---|
3903 | CheckComArgExpr(aLogicalSize, aLogicalSize > 0);
|
---|
3904 | HRESULT rc = S_OK;
|
---|
3905 | ComObjPtr<Progress> pProgress;
|
---|
3906 |
|
---|
3907 | /* Build the medium lock list. */
|
---|
3908 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3909 |
|
---|
3910 | try
|
---|
3911 | {
|
---|
3912 | const char *pszError = NULL;
|
---|
3913 |
|
---|
3914 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
3915 | this /* pToLockWrite */,
|
---|
3916 | false /* fMediumLockWriteAll */,
|
---|
3917 | NULL,
|
---|
3918 | *pMediumLockList);
|
---|
3919 | if (FAILED(rc))
|
---|
3920 | {
|
---|
3921 | pszError = tr("Failed to create medium lock list when resizing '%s'");
|
---|
3922 | }
|
---|
3923 | else
|
---|
3924 | {
|
---|
3925 | rc = pMediumLockList->Lock();
|
---|
3926 | if (FAILED(rc))
|
---|
3927 | pszError = tr("Failed to lock media when resizing '%s'");
|
---|
3928 | }
|
---|
3929 |
|
---|
3930 |
|
---|
3931 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3932 |
|
---|
3933 | if (FAILED(rc))
|
---|
3934 | {
|
---|
3935 | throw setError(rc, pszError, i_getLocationFull().c_str());
|
---|
3936 | }
|
---|
3937 |
|
---|
3938 | pProgress.createObject();
|
---|
3939 | rc = pProgress->init(m->pVirtualBox,
|
---|
3940 | static_cast <IMedium *>(this),
|
---|
3941 | BstrFmt(tr("Resizing medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3942 | TRUE /* aCancelable */);
|
---|
3943 | if (FAILED(rc))
|
---|
3944 | {
|
---|
3945 | throw rc;
|
---|
3946 | }
|
---|
3947 | }
|
---|
3948 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3949 |
|
---|
3950 | if (SUCCEEDED(rc))
|
---|
3951 | rc = i_resize((uint64_t)aLogicalSize, pMediumLockList, &pProgress, false /* aWait */, true /* aNotify */);
|
---|
3952 |
|
---|
3953 | if (SUCCEEDED(rc))
|
---|
3954 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3955 | else
|
---|
3956 | delete pMediumLockList;
|
---|
3957 |
|
---|
3958 | return rc;
|
---|
3959 | }
|
---|
3960 |
|
---|
3961 | HRESULT Medium::reset(AutoCaller &autoCaller, ComPtr<IProgress> &aProgress)
|
---|
3962 | {
|
---|
3963 | HRESULT rc = S_OK;
|
---|
3964 | ComObjPtr<Progress> pProgress;
|
---|
3965 | Medium::Task *pTask = NULL;
|
---|
3966 |
|
---|
3967 | try
|
---|
3968 | {
|
---|
3969 | autoCaller.release();
|
---|
3970 |
|
---|
3971 | /* It is possible that some previous/concurrent uninit has already
|
---|
3972 | * cleared the pVirtualBox reference, see #uninit(). */
|
---|
3973 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
3974 |
|
---|
3975 | /* i_canClose() needs the tree lock */
|
---|
3976 | AutoMultiWriteLock2 multilock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL,
|
---|
3977 | this->lockHandle()
|
---|
3978 | COMMA_LOCKVAL_SRC_POS);
|
---|
3979 |
|
---|
3980 | autoCaller.add();
|
---|
3981 | if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
|
---|
3982 |
|
---|
3983 | LogFlowThisFunc(("ENTER for medium %s\n", m->strLocationFull.c_str()));
|
---|
3984 |
|
---|
3985 | if (m->pParent.isNull())
|
---|
3986 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3987 | tr("Medium type of '%s' is not differencing"),
|
---|
3988 | m->strLocationFull.c_str());
|
---|
3989 |
|
---|
3990 | rc = i_canClose();
|
---|
3991 | if (FAILED(rc))
|
---|
3992 | throw rc;
|
---|
3993 |
|
---|
3994 | /* Build the medium lock list. */
|
---|
3995 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3996 | multilock.release();
|
---|
3997 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
3998 | this /* pToLockWrite */,
|
---|
3999 | false /* fMediumLockWriteAll */,
|
---|
4000 | NULL,
|
---|
4001 | *pMediumLockList);
|
---|
4002 | multilock.acquire();
|
---|
4003 | if (FAILED(rc))
|
---|
4004 | {
|
---|
4005 | delete pMediumLockList;
|
---|
4006 | throw rc;
|
---|
4007 | }
|
---|
4008 |
|
---|
4009 | multilock.release();
|
---|
4010 | rc = pMediumLockList->Lock();
|
---|
4011 | multilock.acquire();
|
---|
4012 | if (FAILED(rc))
|
---|
4013 | {
|
---|
4014 | delete pMediumLockList;
|
---|
4015 | throw setError(rc,
|
---|
4016 | tr("Failed to lock media when resetting '%s'"),
|
---|
4017 | i_getLocationFull().c_str());
|
---|
4018 | }
|
---|
4019 |
|
---|
4020 | pProgress.createObject();
|
---|
4021 | rc = pProgress->init(m->pVirtualBox,
|
---|
4022 | static_cast<IMedium*>(this),
|
---|
4023 | BstrFmt(tr("Resetting differencing medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
4024 | FALSE /* aCancelable */);
|
---|
4025 | if (FAILED(rc))
|
---|
4026 | throw rc;
|
---|
4027 |
|
---|
4028 | /* setup task object to carry out the operation asynchronously */
|
---|
4029 | pTask = new Medium::ResetTask(this, pProgress, pMediumLockList);
|
---|
4030 | rc = pTask->hrc();
|
---|
4031 | AssertComRC(rc);
|
---|
4032 | if (FAILED(rc))
|
---|
4033 | throw rc;
|
---|
4034 | }
|
---|
4035 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4036 |
|
---|
4037 | if (SUCCEEDED(rc))
|
---|
4038 | {
|
---|
4039 | rc = pTask->createThread();
|
---|
4040 | pTask = NULL;
|
---|
4041 | if (SUCCEEDED(rc))
|
---|
4042 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
4043 | }
|
---|
4044 | else if (pTask != NULL)
|
---|
4045 | delete pTask;
|
---|
4046 |
|
---|
4047 | LogFlowThisFunc(("LEAVE, rc=%Rhrc\n", rc));
|
---|
4048 |
|
---|
4049 | return rc;
|
---|
4050 | }
|
---|
4051 |
|
---|
4052 | HRESULT Medium::changeEncryption(const com::Utf8Str &aCurrentPassword, const com::Utf8Str &aCipher,
|
---|
4053 | const com::Utf8Str &aNewPassword, const com::Utf8Str &aNewPasswordId,
|
---|
4054 | ComPtr<IProgress> &aProgress)
|
---|
4055 | {
|
---|
4056 | HRESULT rc = S_OK;
|
---|
4057 | ComObjPtr<Progress> pProgress;
|
---|
4058 | Medium::Task *pTask = NULL;
|
---|
4059 |
|
---|
4060 | try
|
---|
4061 | {
|
---|
4062 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4063 |
|
---|
4064 | DeviceType_T devType = i_getDeviceType();
|
---|
4065 | /* Cannot encrypt DVD or floppy images so far. */
|
---|
4066 | if ( devType == DeviceType_DVD
|
---|
4067 | || devType == DeviceType_Floppy)
|
---|
4068 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4069 | tr("Cannot encrypt DVD or Floppy medium '%s'"),
|
---|
4070 | m->strLocationFull.c_str());
|
---|
4071 |
|
---|
4072 | /* Cannot encrypt media which are attached to more than one virtual machine. */
|
---|
4073 | if (m->backRefs.size() > 1)
|
---|
4074 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4075 | tr("Cannot encrypt medium '%s' because it is attached to %d virtual machines", "", m->backRefs.size()),
|
---|
4076 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
4077 |
|
---|
4078 | if (i_getChildren().size() != 0)
|
---|
4079 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4080 | tr("Cannot encrypt medium '%s' because it has %d children", "", i_getChildren().size()),
|
---|
4081 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
4082 |
|
---|
4083 | /* Build the medium lock list. */
|
---|
4084 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
4085 | alock.release();
|
---|
4086 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
4087 | this /* pToLockWrite */,
|
---|
4088 | true /* fMediumLockAllWrite */,
|
---|
4089 | NULL,
|
---|
4090 | *pMediumLockList);
|
---|
4091 | alock.acquire();
|
---|
4092 | if (FAILED(rc))
|
---|
4093 | {
|
---|
4094 | delete pMediumLockList;
|
---|
4095 | throw rc;
|
---|
4096 | }
|
---|
4097 |
|
---|
4098 | alock.release();
|
---|
4099 | rc = pMediumLockList->Lock();
|
---|
4100 | alock.acquire();
|
---|
4101 | if (FAILED(rc))
|
---|
4102 | {
|
---|
4103 | delete pMediumLockList;
|
---|
4104 | throw setError(rc,
|
---|
4105 | tr("Failed to lock media for encryption '%s'"),
|
---|
4106 | i_getLocationFull().c_str());
|
---|
4107 | }
|
---|
4108 |
|
---|
4109 | /*
|
---|
4110 | * Check all media in the chain to not contain any branches or references to
|
---|
4111 | * other virtual machines, we support encrypting only a list of differencing media at the moment.
|
---|
4112 | */
|
---|
4113 | MediumLockList::Base::const_iterator mediumListBegin = pMediumLockList->GetBegin();
|
---|
4114 | MediumLockList::Base::const_iterator mediumListEnd = pMediumLockList->GetEnd();
|
---|
4115 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
4116 | it != mediumListEnd;
|
---|
4117 | ++it)
|
---|
4118 | {
|
---|
4119 | const MediumLock &mediumLock = *it;
|
---|
4120 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
4121 | AutoReadLock mediumReadLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
4122 |
|
---|
4123 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
4124 |
|
---|
4125 | if (pMedium->m->backRefs.size() > 1)
|
---|
4126 | {
|
---|
4127 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4128 | tr("Cannot encrypt medium '%s' because it is attached to %d virtual machines", "",
|
---|
4129 | pMedium->m->backRefs.size()),
|
---|
4130 | pMedium->m->strLocationFull.c_str(), pMedium->m->backRefs.size());
|
---|
4131 | break;
|
---|
4132 | }
|
---|
4133 | else if (pMedium->i_getChildren().size() > 1)
|
---|
4134 | {
|
---|
4135 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4136 | tr("Cannot encrypt medium '%s' because it has %d children", "", pMedium->i_getChildren().size()),
|
---|
4137 | pMedium->m->strLocationFull.c_str(), pMedium->i_getChildren().size());
|
---|
4138 | break;
|
---|
4139 | }
|
---|
4140 | }
|
---|
4141 |
|
---|
4142 | if (FAILED(rc))
|
---|
4143 | {
|
---|
4144 | delete pMediumLockList;
|
---|
4145 | throw rc;
|
---|
4146 | }
|
---|
4147 |
|
---|
4148 | const char *pszAction = tr("Encrypting medium");
|
---|
4149 | if ( aCurrentPassword.isNotEmpty()
|
---|
4150 | && aCipher.isEmpty())
|
---|
4151 | pszAction = tr("Decrypting medium");
|
---|
4152 |
|
---|
4153 | pProgress.createObject();
|
---|
4154 | rc = pProgress->init(m->pVirtualBox,
|
---|
4155 | static_cast <IMedium *>(this),
|
---|
4156 | BstrFmt("%s '%s'", pszAction, m->strLocationFull.c_str()).raw(),
|
---|
4157 | TRUE /* aCancelable */);
|
---|
4158 | if (FAILED(rc))
|
---|
4159 | {
|
---|
4160 | delete pMediumLockList;
|
---|
4161 | throw rc;
|
---|
4162 | }
|
---|
4163 |
|
---|
4164 | /* setup task object to carry out the operation asynchronously */
|
---|
4165 | pTask = new Medium::EncryptTask(this, aNewPassword, aCurrentPassword,
|
---|
4166 | aCipher, aNewPasswordId, pProgress, pMediumLockList);
|
---|
4167 | rc = pTask->hrc();
|
---|
4168 | AssertComRC(rc);
|
---|
4169 | if (FAILED(rc))
|
---|
4170 | throw rc;
|
---|
4171 | }
|
---|
4172 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4173 |
|
---|
4174 | if (SUCCEEDED(rc))
|
---|
4175 | {
|
---|
4176 | rc = pTask->createThread();
|
---|
4177 | pTask = NULL;
|
---|
4178 | if (SUCCEEDED(rc))
|
---|
4179 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
4180 | }
|
---|
4181 | else if (pTask != NULL)
|
---|
4182 | delete pTask;
|
---|
4183 |
|
---|
4184 | return rc;
|
---|
4185 | }
|
---|
4186 |
|
---|
4187 | HRESULT Medium::getEncryptionSettings(AutoCaller &autoCaller, com::Utf8Str &aCipher, com::Utf8Str &aPasswordId)
|
---|
4188 | {
|
---|
4189 | #ifndef VBOX_WITH_EXTPACK
|
---|
4190 | RT_NOREF(aCipher, aPasswordId);
|
---|
4191 | #endif
|
---|
4192 | HRESULT rc = S_OK;
|
---|
4193 |
|
---|
4194 | try
|
---|
4195 | {
|
---|
4196 | autoCaller.release();
|
---|
4197 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
4198 | autoCaller.add();
|
---|
4199 | if (FAILED(autoCaller.hrc()))
|
---|
4200 | throw rc;
|
---|
4201 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4202 |
|
---|
4203 | /* Check whether encryption is configured for this medium. */
|
---|
4204 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
4205 | if (it == pBase->m->mapProperties.end())
|
---|
4206 | throw VBOX_E_NOT_SUPPORTED;
|
---|
4207 |
|
---|
4208 | # ifdef VBOX_WITH_EXTPACK
|
---|
4209 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
4210 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
4211 | {
|
---|
4212 | /* Load the plugin */
|
---|
4213 | Utf8Str strPlugin;
|
---|
4214 | rc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
4215 | if (SUCCEEDED(rc))
|
---|
4216 | {
|
---|
4217 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
4218 | if (RT_FAILURE(vrc))
|
---|
4219 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
4220 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
4221 | i_vdError(vrc).c_str());
|
---|
4222 | }
|
---|
4223 | else
|
---|
4224 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
4225 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
4226 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
4227 | }
|
---|
4228 | else
|
---|
4229 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
4230 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
4231 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
4232 |
|
---|
4233 | PVDISK pDisk = NULL;
|
---|
4234 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
4235 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
4236 |
|
---|
4237 | MediumCryptoFilterSettings CryptoSettings;
|
---|
4238 |
|
---|
4239 | i_taskEncryptSettingsSetup(&CryptoSettings, NULL, it->second.c_str(), NULL, false /* fCreateKeyStore */);
|
---|
4240 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ | VD_FILTER_FLAGS_INFO, CryptoSettings.vdFilterIfaces);
|
---|
4241 | if (RT_FAILURE(vrc))
|
---|
4242 | throw setErrorBoth(VBOX_E_INVALID_OBJECT_STATE, vrc,
|
---|
4243 | tr("Failed to load the encryption filter: %s"),
|
---|
4244 | i_vdError(vrc).c_str());
|
---|
4245 |
|
---|
4246 | it = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
4247 | if (it == pBase->m->mapProperties.end())
|
---|
4248 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4249 | tr("Image is configured for encryption but doesn't has a KeyId set"));
|
---|
4250 |
|
---|
4251 | aPasswordId = it->second.c_str();
|
---|
4252 | aCipher = CryptoSettings.pszCipherReturned;
|
---|
4253 | RTStrFree(CryptoSettings.pszCipherReturned);
|
---|
4254 |
|
---|
4255 | VDDestroy(pDisk);
|
---|
4256 | # else
|
---|
4257 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
4258 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
4259 | # endif
|
---|
4260 | }
|
---|
4261 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4262 |
|
---|
4263 | return rc;
|
---|
4264 | }
|
---|
4265 |
|
---|
4266 | HRESULT Medium::checkEncryptionPassword(const com::Utf8Str &aPassword)
|
---|
4267 | {
|
---|
4268 | HRESULT rc = S_OK;
|
---|
4269 |
|
---|
4270 | try
|
---|
4271 | {
|
---|
4272 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
4273 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4274 |
|
---|
4275 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
4276 | if (it == pBase->m->mapProperties.end())
|
---|
4277 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
4278 | tr("The image is not configured for encryption"));
|
---|
4279 |
|
---|
4280 | if (aPassword.isEmpty())
|
---|
4281 | throw setError(E_INVALIDARG,
|
---|
4282 | tr("The given password must not be empty"));
|
---|
4283 |
|
---|
4284 | # ifdef VBOX_WITH_EXTPACK
|
---|
4285 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
4286 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
4287 | {
|
---|
4288 | /* Load the plugin */
|
---|
4289 | Utf8Str strPlugin;
|
---|
4290 | rc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
4291 | if (SUCCEEDED(rc))
|
---|
4292 | {
|
---|
4293 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
4294 | if (RT_FAILURE(vrc))
|
---|
4295 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
4296 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
4297 | i_vdError(vrc).c_str());
|
---|
4298 | }
|
---|
4299 | else
|
---|
4300 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
4301 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
4302 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
4303 | }
|
---|
4304 | else
|
---|
4305 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
4306 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
4307 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
4308 |
|
---|
4309 | PVDISK pDisk = NULL;
|
---|
4310 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
4311 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
4312 |
|
---|
4313 | MediumCryptoFilterSettings CryptoSettings;
|
---|
4314 |
|
---|
4315 | i_taskEncryptSettingsSetup(&CryptoSettings, NULL, it->second.c_str(), aPassword.c_str(),
|
---|
4316 | false /* fCreateKeyStore */);
|
---|
4317 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ, CryptoSettings.vdFilterIfaces);
|
---|
4318 | if (vrc == VERR_VD_PASSWORD_INCORRECT)
|
---|
4319 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
4320 | tr("The given password is incorrect"));
|
---|
4321 | else if (RT_FAILURE(vrc))
|
---|
4322 | throw setErrorBoth(VBOX_E_INVALID_OBJECT_STATE, vrc,
|
---|
4323 | tr("Failed to load the encryption filter: %s"),
|
---|
4324 | i_vdError(vrc).c_str());
|
---|
4325 |
|
---|
4326 | VDDestroy(pDisk);
|
---|
4327 | # else
|
---|
4328 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
4329 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
4330 | # endif
|
---|
4331 | }
|
---|
4332 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4333 |
|
---|
4334 | return rc;
|
---|
4335 | }
|
---|
4336 |
|
---|
4337 | HRESULT Medium::openForIO(BOOL aWritable, com::Utf8Str const &aPassword, ComPtr<IMediumIO> &aMediumIO)
|
---|
4338 | {
|
---|
4339 | /*
|
---|
4340 | * Input validation.
|
---|
4341 | */
|
---|
4342 | if (aWritable && i_isReadOnly())
|
---|
4343 | return setError(E_ACCESSDENIED, tr("Write access denied: read-only"));
|
---|
4344 |
|
---|
4345 | com::Utf8Str const strKeyId = i_getKeyId();
|
---|
4346 | if (strKeyId.isEmpty() && aPassword.isNotEmpty())
|
---|
4347 | return setError(E_INVALIDARG, tr("Password given for unencrypted medium"));
|
---|
4348 | if (strKeyId.isNotEmpty() && aPassword.isEmpty())
|
---|
4349 | return setError(E_INVALIDARG, tr("Password needed for encrypted medium"));
|
---|
4350 |
|
---|
4351 | /*
|
---|
4352 | * Create IO object and return it.
|
---|
4353 | */
|
---|
4354 | ComObjPtr<MediumIO> ptrIO;
|
---|
4355 | HRESULT hrc = ptrIO.createObject();
|
---|
4356 | if (SUCCEEDED(hrc))
|
---|
4357 | {
|
---|
4358 | hrc = ptrIO->initForMedium(this, m->pVirtualBox, aWritable != FALSE, strKeyId, aPassword);
|
---|
4359 | if (SUCCEEDED(hrc))
|
---|
4360 | ptrIO.queryInterfaceTo(aMediumIO.asOutParam());
|
---|
4361 | }
|
---|
4362 | return hrc;
|
---|
4363 | }
|
---|
4364 |
|
---|
4365 |
|
---|
4366 | ////////////////////////////////////////////////////////////////////////////////
|
---|
4367 | //
|
---|
4368 | // Medium public internal methods
|
---|
4369 | //
|
---|
4370 | ////////////////////////////////////////////////////////////////////////////////
|
---|
4371 |
|
---|
4372 | /**
|
---|
4373 | * Internal method to return the medium's parent medium. Must have caller + locking!
|
---|
4374 | * @return
|
---|
4375 | */
|
---|
4376 | const ComObjPtr<Medium>& Medium::i_getParent() const
|
---|
4377 | {
|
---|
4378 | return m->pParent;
|
---|
4379 | }
|
---|
4380 |
|
---|
4381 | /**
|
---|
4382 | * Internal method to return the medium's list of child media. Must have caller + locking!
|
---|
4383 | * @return
|
---|
4384 | */
|
---|
4385 | const MediaList& Medium::i_getChildren() const
|
---|
4386 | {
|
---|
4387 | return m->llChildren;
|
---|
4388 | }
|
---|
4389 |
|
---|
4390 | /**
|
---|
4391 | * Internal method to return the medium's GUID. Must have caller + locking!
|
---|
4392 | * @return
|
---|
4393 | */
|
---|
4394 | const Guid& Medium::i_getId() const
|
---|
4395 | {
|
---|
4396 | return m->id;
|
---|
4397 | }
|
---|
4398 |
|
---|
4399 | /**
|
---|
4400 | * Internal method to return the medium's state. Must have caller + locking!
|
---|
4401 | * @return
|
---|
4402 | */
|
---|
4403 | MediumState_T Medium::i_getState() const
|
---|
4404 | {
|
---|
4405 | return m->state;
|
---|
4406 | }
|
---|
4407 |
|
---|
4408 | /**
|
---|
4409 | * Internal method to return the medium's variant. Must have caller + locking!
|
---|
4410 | * @return
|
---|
4411 | */
|
---|
4412 | MediumVariant_T Medium::i_getVariant() const
|
---|
4413 | {
|
---|
4414 | return m->variant;
|
---|
4415 | }
|
---|
4416 |
|
---|
4417 | /**
|
---|
4418 | * Internal method which returns true if this medium represents a host drive.
|
---|
4419 | * @return
|
---|
4420 | */
|
---|
4421 | bool Medium::i_isHostDrive() const
|
---|
4422 | {
|
---|
4423 | return m->hostDrive;
|
---|
4424 | }
|
---|
4425 |
|
---|
4426 | /**
|
---|
4427 | * Internal method to return the medium's full location. Must have caller + locking!
|
---|
4428 | * @return
|
---|
4429 | */
|
---|
4430 | const Utf8Str& Medium::i_getLocationFull() const
|
---|
4431 | {
|
---|
4432 | return m->strLocationFull;
|
---|
4433 | }
|
---|
4434 |
|
---|
4435 | /**
|
---|
4436 | * Internal method to return the medium's format string. Must have caller + locking!
|
---|
4437 | * @return
|
---|
4438 | */
|
---|
4439 | const Utf8Str& Medium::i_getFormat() const
|
---|
4440 | {
|
---|
4441 | return m->strFormat;
|
---|
4442 | }
|
---|
4443 |
|
---|
4444 | /**
|
---|
4445 | * Internal method to return the medium's format object. Must have caller + locking!
|
---|
4446 | * @return
|
---|
4447 | */
|
---|
4448 | const ComObjPtr<MediumFormat>& Medium::i_getMediumFormat() const
|
---|
4449 | {
|
---|
4450 | return m->formatObj;
|
---|
4451 | }
|
---|
4452 |
|
---|
4453 | /**
|
---|
4454 | * Internal method that returns true if the medium is represented by a file on the host disk
|
---|
4455 | * (and not iSCSI or something).
|
---|
4456 | * @return
|
---|
4457 | */
|
---|
4458 | bool Medium::i_isMediumFormatFile() const
|
---|
4459 | {
|
---|
4460 | if ( m->formatObj
|
---|
4461 | && (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
4462 | )
|
---|
4463 | return true;
|
---|
4464 | return false;
|
---|
4465 | }
|
---|
4466 |
|
---|
4467 | /**
|
---|
4468 | * Internal method to return the medium's size. Must have caller + locking!
|
---|
4469 | * @return
|
---|
4470 | */
|
---|
4471 | uint64_t Medium::i_getSize() const
|
---|
4472 | {
|
---|
4473 | return m->size;
|
---|
4474 | }
|
---|
4475 |
|
---|
4476 | /**
|
---|
4477 | * Internal method to return the medium's size. Must have caller + locking!
|
---|
4478 | * @return
|
---|
4479 | */
|
---|
4480 | uint64_t Medium::i_getLogicalSize() const
|
---|
4481 | {
|
---|
4482 | return m->logicalSize;
|
---|
4483 | }
|
---|
4484 |
|
---|
4485 | /**
|
---|
4486 | * Returns the medium device type. Must have caller + locking!
|
---|
4487 | * @return
|
---|
4488 | */
|
---|
4489 | DeviceType_T Medium::i_getDeviceType() const
|
---|
4490 | {
|
---|
4491 | return m->devType;
|
---|
4492 | }
|
---|
4493 |
|
---|
4494 | /**
|
---|
4495 | * Returns the medium type. Must have caller + locking!
|
---|
4496 | * @return
|
---|
4497 | */
|
---|
4498 | MediumType_T Medium::i_getType() const
|
---|
4499 | {
|
---|
4500 | return m->type;
|
---|
4501 | }
|
---|
4502 |
|
---|
4503 | /**
|
---|
4504 | * Returns a short version of the location attribute.
|
---|
4505 | *
|
---|
4506 | * @note Must be called from under this object's read or write lock.
|
---|
4507 | */
|
---|
4508 | Utf8Str Medium::i_getName()
|
---|
4509 | {
|
---|
4510 | Utf8Str name = RTPathFilename(m->strLocationFull.c_str());
|
---|
4511 | return name;
|
---|
4512 | }
|
---|
4513 |
|
---|
4514 | /**
|
---|
4515 | * Same as i_addRegistry() except that we don't check the object state, making
|
---|
4516 | * it safe to call with initFromSettings() on the call stack.
|
---|
4517 | */
|
---|
4518 | bool Medium::i_addRegistryNoCallerCheck(const Guid &id)
|
---|
4519 | {
|
---|
4520 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4521 |
|
---|
4522 | bool fAdd = true;
|
---|
4523 |
|
---|
4524 | // hard disks cannot be in more than one registry
|
---|
4525 | if ( m->devType == DeviceType_HardDisk
|
---|
4526 | && m->llRegistryIDs.size() > 0)
|
---|
4527 | fAdd = false;
|
---|
4528 |
|
---|
4529 | // no need to add the UUID twice
|
---|
4530 | if (fAdd)
|
---|
4531 | {
|
---|
4532 | for (GuidList::const_iterator it = m->llRegistryIDs.begin();
|
---|
4533 | it != m->llRegistryIDs.end();
|
---|
4534 | ++it)
|
---|
4535 | {
|
---|
4536 | if ((*it) == id)
|
---|
4537 | {
|
---|
4538 | fAdd = false;
|
---|
4539 | break;
|
---|
4540 | }
|
---|
4541 | }
|
---|
4542 | }
|
---|
4543 |
|
---|
4544 | if (fAdd)
|
---|
4545 | m->llRegistryIDs.push_back(id);
|
---|
4546 |
|
---|
4547 | return fAdd;
|
---|
4548 | }
|
---|
4549 |
|
---|
4550 | /**
|
---|
4551 | * This adds the given UUID to the list of media registries in which this
|
---|
4552 | * medium should be registered. The UUID can either be a machine UUID,
|
---|
4553 | * to add a machine registry, or the global registry UUID as returned by
|
---|
4554 | * VirtualBox::getGlobalRegistryId().
|
---|
4555 | *
|
---|
4556 | * Note that for hard disks, this method does nothing if the medium is
|
---|
4557 | * already in another registry to avoid having hard disks in more than
|
---|
4558 | * one registry, which causes trouble with keeping diff images in sync.
|
---|
4559 | * See getFirstRegistryMachineId() for details.
|
---|
4560 | *
|
---|
4561 | * @param id
|
---|
4562 | * @return true if the registry was added; false if the given id was already on the list.
|
---|
4563 | */
|
---|
4564 | bool Medium::i_addRegistry(const Guid &id)
|
---|
4565 | {
|
---|
4566 | AutoCaller autoCaller(this);
|
---|
4567 | if (FAILED(autoCaller.hrc()))
|
---|
4568 | return false;
|
---|
4569 | return i_addRegistryNoCallerCheck(id);
|
---|
4570 | }
|
---|
4571 |
|
---|
4572 | /**
|
---|
4573 | * This adds the given UUID to the list of media registries in which this
|
---|
4574 | * medium should be registered. The UUID can either be a machine UUID,
|
---|
4575 | * to add a machine registry, or the global registry UUID as returned by
|
---|
4576 | * VirtualBox::getGlobalRegistryId(). Thisis applied to all children.
|
---|
4577 | *
|
---|
4578 | * Note that for hard disks, this method does nothing if the medium is
|
---|
4579 | * already in another registry to avoid having hard disks in more than
|
---|
4580 | * one registry, which causes trouble with keeping diff images in sync.
|
---|
4581 | * See getFirstRegistryMachineId() for details.
|
---|
4582 | *
|
---|
4583 | * @note the caller must hold the media tree lock for reading.
|
---|
4584 | *
|
---|
4585 | * @param id
|
---|
4586 | * @return true if the registry was added; false if the given id was already on the list.
|
---|
4587 | */
|
---|
4588 | bool Medium::i_addRegistryAll(const Guid &id)
|
---|
4589 | {
|
---|
4590 | MediaList llMediaTodo;
|
---|
4591 | llMediaTodo.push_back(this);
|
---|
4592 |
|
---|
4593 | bool fAdd = false;
|
---|
4594 |
|
---|
4595 | while (!llMediaTodo.empty())
|
---|
4596 | {
|
---|
4597 | ComObjPtr<Medium> pMedium = llMediaTodo.front();
|
---|
4598 | llMediaTodo.pop_front();
|
---|
4599 |
|
---|
4600 | AutoCaller mediumCaller(pMedium);
|
---|
4601 | if (FAILED(mediumCaller.hrc())) continue;
|
---|
4602 |
|
---|
4603 | fAdd |= pMedium->i_addRegistryNoCallerCheck(id);
|
---|
4604 |
|
---|
4605 | // protected by the medium tree lock held by our original caller
|
---|
4606 | MediaList::const_iterator itBegin = pMedium->i_getChildren().begin();
|
---|
4607 | MediaList::const_iterator itEnd = pMedium->i_getChildren().end();
|
---|
4608 | for (MediaList::const_iterator it = itBegin; it != itEnd; ++it)
|
---|
4609 | llMediaTodo.push_back(*it);
|
---|
4610 | }
|
---|
4611 |
|
---|
4612 | return fAdd;
|
---|
4613 | }
|
---|
4614 |
|
---|
4615 | /**
|
---|
4616 | * Removes the given UUID from the list of media registry UUIDs of this medium.
|
---|
4617 | *
|
---|
4618 | * @param id
|
---|
4619 | * @return true if the UUID was found or false if not.
|
---|
4620 | */
|
---|
4621 | bool Medium::i_removeRegistry(const Guid &id)
|
---|
4622 | {
|
---|
4623 | AutoCaller autoCaller(this);
|
---|
4624 | if (FAILED(autoCaller.hrc()))
|
---|
4625 | return false;
|
---|
4626 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4627 |
|
---|
4628 | bool fRemove = false;
|
---|
4629 |
|
---|
4630 | /// @todo r=klaus eliminate this code, replace it by using find.
|
---|
4631 | for (GuidList::iterator it = m->llRegistryIDs.begin();
|
---|
4632 | it != m->llRegistryIDs.end();
|
---|
4633 | ++it)
|
---|
4634 | {
|
---|
4635 | if ((*it) == id)
|
---|
4636 | {
|
---|
4637 | // getting away with this as the iterator isn't used after
|
---|
4638 | m->llRegistryIDs.erase(it);
|
---|
4639 | fRemove = true;
|
---|
4640 | break;
|
---|
4641 | }
|
---|
4642 | }
|
---|
4643 |
|
---|
4644 | return fRemove;
|
---|
4645 | }
|
---|
4646 |
|
---|
4647 | /**
|
---|
4648 | * Removes the given UUID from the list of media registry UUIDs, for this
|
---|
4649 | * medium and all its children.
|
---|
4650 | *
|
---|
4651 | * @note the caller must hold the media tree lock for reading.
|
---|
4652 | *
|
---|
4653 | * @param id
|
---|
4654 | * @return true if the UUID was found or false if not.
|
---|
4655 | */
|
---|
4656 | bool Medium::i_removeRegistryAll(const Guid &id)
|
---|
4657 | {
|
---|
4658 | MediaList llMediaTodo;
|
---|
4659 | llMediaTodo.push_back(this);
|
---|
4660 |
|
---|
4661 | bool fRemove = false;
|
---|
4662 |
|
---|
4663 | while (!llMediaTodo.empty())
|
---|
4664 | {
|
---|
4665 | ComObjPtr<Medium> pMedium = llMediaTodo.front();
|
---|
4666 | llMediaTodo.pop_front();
|
---|
4667 |
|
---|
4668 | AutoCaller mediumCaller(pMedium);
|
---|
4669 | if (FAILED(mediumCaller.hrc())) continue;
|
---|
4670 |
|
---|
4671 | fRemove |= pMedium->i_removeRegistry(id);
|
---|
4672 |
|
---|
4673 | // protected by the medium tree lock held by our original caller
|
---|
4674 | MediaList::const_iterator itBegin = pMedium->i_getChildren().begin();
|
---|
4675 | MediaList::const_iterator itEnd = pMedium->i_getChildren().end();
|
---|
4676 | for (MediaList::const_iterator it = itBegin; it != itEnd; ++it)
|
---|
4677 | llMediaTodo.push_back(*it);
|
---|
4678 | }
|
---|
4679 |
|
---|
4680 | return fRemove;
|
---|
4681 | }
|
---|
4682 |
|
---|
4683 | /**
|
---|
4684 | * Returns true if id is in the list of media registries for this medium.
|
---|
4685 | *
|
---|
4686 | * Must have caller + read locking!
|
---|
4687 | *
|
---|
4688 | * @param id
|
---|
4689 | * @return
|
---|
4690 | */
|
---|
4691 | bool Medium::i_isInRegistry(const Guid &id)
|
---|
4692 | {
|
---|
4693 | /// @todo r=klaus eliminate this code, replace it by using find.
|
---|
4694 | for (GuidList::const_iterator it = m->llRegistryIDs.begin();
|
---|
4695 | it != m->llRegistryIDs.end();
|
---|
4696 | ++it)
|
---|
4697 | {
|
---|
4698 | if (*it == id)
|
---|
4699 | return true;
|
---|
4700 | }
|
---|
4701 |
|
---|
4702 | return false;
|
---|
4703 | }
|
---|
4704 |
|
---|
4705 | /**
|
---|
4706 | * Internal method to return the medium's first registry machine (i.e. the machine in whose
|
---|
4707 | * machine XML this medium is listed).
|
---|
4708 | *
|
---|
4709 | * Every attached medium must now (4.0) reside in at least one media registry, which is identified
|
---|
4710 | * by a UUID. This is either a machine UUID if the machine is from 4.0 or newer, in which case
|
---|
4711 | * machines have their own media registries, or it is the pseudo-UUID of the VirtualBox
|
---|
4712 | * object if the machine is old and still needs the global registry in VirtualBox.xml.
|
---|
4713 | *
|
---|
4714 | * By definition, hard disks may only be in one media registry, in which all its children
|
---|
4715 | * will be stored as well. Otherwise we run into problems with having keep multiple registries
|
---|
4716 | * in sync. (This is the "cloned VM" case in which VM1 may link to the disks of VM2; in this
|
---|
4717 | * case, only VM2's registry is used for the disk in question.)
|
---|
4718 | *
|
---|
4719 | * If there is no medium registry, particularly if the medium has not been attached yet, this
|
---|
4720 | * does not modify uuid and returns false.
|
---|
4721 | *
|
---|
4722 | * ISOs and RAWs, by contrast, can be in more than one repository to make things easier for
|
---|
4723 | * the user.
|
---|
4724 | *
|
---|
4725 | * Must have caller + locking!
|
---|
4726 | *
|
---|
4727 | * @param uuid Receives first registry machine UUID, if available.
|
---|
4728 | * @return true if uuid was set.
|
---|
4729 | */
|
---|
4730 | bool Medium::i_getFirstRegistryMachineId(Guid &uuid) const
|
---|
4731 | {
|
---|
4732 | if (m->llRegistryIDs.size())
|
---|
4733 | {
|
---|
4734 | uuid = m->llRegistryIDs.front();
|
---|
4735 | return true;
|
---|
4736 | }
|
---|
4737 | return false;
|
---|
4738 | }
|
---|
4739 |
|
---|
4740 | /**
|
---|
4741 | * Marks all the registries in which this medium is registered as modified.
|
---|
4742 | */
|
---|
4743 | void Medium::i_markRegistriesModified()
|
---|
4744 | {
|
---|
4745 | AutoCaller autoCaller(this);
|
---|
4746 | if (FAILED(autoCaller.hrc())) return;
|
---|
4747 |
|
---|
4748 | // Get local copy, as keeping the lock over VirtualBox::markRegistryModified
|
---|
4749 | // causes trouble with the lock order
|
---|
4750 | GuidList llRegistryIDs;
|
---|
4751 | {
|
---|
4752 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4753 | llRegistryIDs = m->llRegistryIDs;
|
---|
4754 | }
|
---|
4755 |
|
---|
4756 | autoCaller.release();
|
---|
4757 |
|
---|
4758 | /* Save the error information now, the implicit restore when this goes
|
---|
4759 | * out of scope will throw away spurious additional errors created below. */
|
---|
4760 | ErrorInfoKeeper eik;
|
---|
4761 | for (GuidList::const_iterator it = llRegistryIDs.begin();
|
---|
4762 | it != llRegistryIDs.end();
|
---|
4763 | ++it)
|
---|
4764 | {
|
---|
4765 | m->pVirtualBox->i_markRegistryModified(*it);
|
---|
4766 | }
|
---|
4767 | }
|
---|
4768 |
|
---|
4769 | /**
|
---|
4770 | * Adds the given machine and optionally the snapshot to the list of the objects
|
---|
4771 | * this medium is attached to.
|
---|
4772 | *
|
---|
4773 | * @param aMachineId Machine ID.
|
---|
4774 | * @param aSnapshotId Snapshot ID; when non-empty, adds a snapshot attachment.
|
---|
4775 | */
|
---|
4776 | HRESULT Medium::i_addBackReference(const Guid &aMachineId,
|
---|
4777 | const Guid &aSnapshotId /*= Guid::Empty*/)
|
---|
4778 | {
|
---|
4779 | AssertReturn(aMachineId.isValid(), E_FAIL);
|
---|
4780 |
|
---|
4781 | LogFlowThisFunc(("ENTER, aMachineId: {%RTuuid}, aSnapshotId: {%RTuuid}\n", aMachineId.raw(), aSnapshotId.raw()));
|
---|
4782 |
|
---|
4783 | AutoCaller autoCaller(this);
|
---|
4784 | AssertComRCReturnRC(autoCaller.hrc());
|
---|
4785 |
|
---|
4786 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4787 |
|
---|
4788 | switch (m->state)
|
---|
4789 | {
|
---|
4790 | case MediumState_Created:
|
---|
4791 | case MediumState_Inaccessible:
|
---|
4792 | case MediumState_LockedRead:
|
---|
4793 | case MediumState_LockedWrite:
|
---|
4794 | break;
|
---|
4795 |
|
---|
4796 | default:
|
---|
4797 | return i_setStateError();
|
---|
4798 | }
|
---|
4799 |
|
---|
4800 | if (m->numCreateDiffTasks > 0)
|
---|
4801 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4802 | tr("Cannot attach medium '%s' {%RTuuid}: %u differencing child media are being created", "",
|
---|
4803 | m->numCreateDiffTasks),
|
---|
4804 | m->strLocationFull.c_str(),
|
---|
4805 | m->id.raw(),
|
---|
4806 | m->numCreateDiffTasks);
|
---|
4807 |
|
---|
4808 | BackRefList::iterator it = std::find_if(m->backRefs.begin(),
|
---|
4809 | m->backRefs.end(),
|
---|
4810 | BackRef::EqualsTo(aMachineId));
|
---|
4811 | if (it == m->backRefs.end())
|
---|
4812 | {
|
---|
4813 | BackRef ref(aMachineId, aSnapshotId);
|
---|
4814 | m->backRefs.push_back(ref);
|
---|
4815 |
|
---|
4816 | return S_OK;
|
---|
4817 | }
|
---|
4818 | bool fDvd = false;
|
---|
4819 | {
|
---|
4820 | AutoReadLock arlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4821 | /*
|
---|
4822 | * Check the medium is DVD and readonly. It's for the case if DVD
|
---|
4823 | * will be able to be writable sometime in the future.
|
---|
4824 | */
|
---|
4825 | fDvd = m->type == MediumType_Readonly && m->devType == DeviceType_DVD;
|
---|
4826 | }
|
---|
4827 |
|
---|
4828 | // if the caller has not supplied a snapshot ID, then we're attaching
|
---|
4829 | // to a machine a medium which represents the machine's current state,
|
---|
4830 | // so set the flag
|
---|
4831 |
|
---|
4832 | if (aSnapshotId.isZero())
|
---|
4833 | {
|
---|
4834 | // Allow DVD having MediumType_Readonly to be attached twice.
|
---|
4835 | // (the medium already had been added to back reference)
|
---|
4836 | if (fDvd)
|
---|
4837 | {
|
---|
4838 | it->iRefCnt++;
|
---|
4839 | return S_OK;
|
---|
4840 | }
|
---|
4841 |
|
---|
4842 | /* sanity: no duplicate attachments */
|
---|
4843 | if (it->fInCurState)
|
---|
4844 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4845 | tr("Cannot attach medium '%s' {%RTuuid}: medium is already associated with the current state of machine uuid {%RTuuid}!"),
|
---|
4846 | m->strLocationFull.c_str(),
|
---|
4847 | m->id.raw(),
|
---|
4848 | aMachineId.raw());
|
---|
4849 | it->fInCurState = true;
|
---|
4850 |
|
---|
4851 | return S_OK;
|
---|
4852 | }
|
---|
4853 |
|
---|
4854 | // otherwise: a snapshot medium is being attached
|
---|
4855 |
|
---|
4856 | /* sanity: no duplicate attachments */
|
---|
4857 | for (std::list<SnapshotRef>::iterator jt = it->llSnapshotIds.begin();
|
---|
4858 | jt != it->llSnapshotIds.end();
|
---|
4859 | ++jt)
|
---|
4860 | {
|
---|
4861 | const Guid &idOldSnapshot = jt->snapshotId;
|
---|
4862 |
|
---|
4863 | if (idOldSnapshot == aSnapshotId)
|
---|
4864 | {
|
---|
4865 | if (fDvd)
|
---|
4866 | {
|
---|
4867 | jt->iRefCnt++;
|
---|
4868 | return S_OK;
|
---|
4869 | }
|
---|
4870 | #ifdef DEBUG
|
---|
4871 | i_dumpBackRefs();
|
---|
4872 | #endif
|
---|
4873 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4874 | tr("Cannot attach medium '%s' {%RTuuid} from snapshot '%RTuuid': medium is already in use by this snapshot!"),
|
---|
4875 | m->strLocationFull.c_str(),
|
---|
4876 | m->id.raw(),
|
---|
4877 | aSnapshotId.raw());
|
---|
4878 | }
|
---|
4879 | }
|
---|
4880 |
|
---|
4881 | it->llSnapshotIds.push_back(SnapshotRef(aSnapshotId));
|
---|
4882 | // Do not touch fInCurState, as the image may be attached to the current
|
---|
4883 | // state *and* a snapshot, otherwise we lose the current state association!
|
---|
4884 |
|
---|
4885 | LogFlowThisFuncLeave();
|
---|
4886 |
|
---|
4887 | return S_OK;
|
---|
4888 | }
|
---|
4889 |
|
---|
4890 | /**
|
---|
4891 | * Removes the given machine and optionally the snapshot from the list of the
|
---|
4892 | * objects this medium is attached to.
|
---|
4893 | *
|
---|
4894 | * @param aMachineId Machine ID.
|
---|
4895 | * @param aSnapshotId Snapshot ID; when non-empty, removes the snapshot
|
---|
4896 | * attachment.
|
---|
4897 | */
|
---|
4898 | HRESULT Medium::i_removeBackReference(const Guid &aMachineId,
|
---|
4899 | const Guid &aSnapshotId /*= Guid::Empty*/)
|
---|
4900 | {
|
---|
4901 | AssertReturn(aMachineId.isValid(), E_FAIL);
|
---|
4902 |
|
---|
4903 | AutoCaller autoCaller(this);
|
---|
4904 | AssertComRCReturnRC(autoCaller.hrc());
|
---|
4905 |
|
---|
4906 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4907 |
|
---|
4908 | BackRefList::iterator it =
|
---|
4909 | std::find_if(m->backRefs.begin(), m->backRefs.end(),
|
---|
4910 | BackRef::EqualsTo(aMachineId));
|
---|
4911 | AssertReturn(it != m->backRefs.end(), E_FAIL);
|
---|
4912 |
|
---|
4913 | if (aSnapshotId.isZero())
|
---|
4914 | {
|
---|
4915 | it->iRefCnt--;
|
---|
4916 | if (it->iRefCnt > 0)
|
---|
4917 | return S_OK;
|
---|
4918 |
|
---|
4919 | /* remove the current state attachment */
|
---|
4920 | it->fInCurState = false;
|
---|
4921 | }
|
---|
4922 | else
|
---|
4923 | {
|
---|
4924 | /* remove the snapshot attachment */
|
---|
4925 | std::list<SnapshotRef>::iterator jt =
|
---|
4926 | std::find_if(it->llSnapshotIds.begin(),
|
---|
4927 | it->llSnapshotIds.end(),
|
---|
4928 | SnapshotRef::EqualsTo(aSnapshotId));
|
---|
4929 |
|
---|
4930 | AssertReturn(jt != it->llSnapshotIds.end(), E_FAIL);
|
---|
4931 |
|
---|
4932 | jt->iRefCnt--;
|
---|
4933 | if (jt->iRefCnt > 0)
|
---|
4934 | return S_OK;
|
---|
4935 |
|
---|
4936 | it->llSnapshotIds.erase(jt);
|
---|
4937 | }
|
---|
4938 |
|
---|
4939 | /* if the backref becomes empty, remove it */
|
---|
4940 | if (it->fInCurState == false && it->llSnapshotIds.size() == 0)
|
---|
4941 | m->backRefs.erase(it);
|
---|
4942 |
|
---|
4943 | return S_OK;
|
---|
4944 | }
|
---|
4945 |
|
---|
4946 | /**
|
---|
4947 | * Internal method to return the medium's list of backrefs. Must have caller + locking!
|
---|
4948 | * @return
|
---|
4949 | */
|
---|
4950 | const Guid* Medium::i_getFirstMachineBackrefId() const
|
---|
4951 | {
|
---|
4952 | if (!m->backRefs.size())
|
---|
4953 | return NULL;
|
---|
4954 |
|
---|
4955 | return &m->backRefs.front().machineId;
|
---|
4956 | }
|
---|
4957 |
|
---|
4958 | /**
|
---|
4959 | * Internal method which returns a machine that either this medium or one of its children
|
---|
4960 | * is attached to. This is used for finding a replacement media registry when an existing
|
---|
4961 | * media registry is about to be deleted in VirtualBox::unregisterMachine().
|
---|
4962 | *
|
---|
4963 | * Must have caller + locking, *and* caller must hold the media tree lock!
|
---|
4964 | * @param aId Id to ignore when looking for backrefs.
|
---|
4965 | * @return
|
---|
4966 | */
|
---|
4967 | const Guid* Medium::i_getAnyMachineBackref(const Guid &aId) const
|
---|
4968 | {
|
---|
4969 | std::list<const Medium *> llMediaTodo;
|
---|
4970 | llMediaTodo.push_back(this);
|
---|
4971 |
|
---|
4972 | while (!llMediaTodo.empty())
|
---|
4973 | {
|
---|
4974 | const Medium *pMedium = llMediaTodo.front();
|
---|
4975 | llMediaTodo.pop_front();
|
---|
4976 |
|
---|
4977 | if (pMedium->m->backRefs.size())
|
---|
4978 | {
|
---|
4979 | if (pMedium->m->backRefs.front().machineId != aId)
|
---|
4980 | return &pMedium->m->backRefs.front().machineId;
|
---|
4981 | if (pMedium->m->backRefs.size() > 1)
|
---|
4982 | {
|
---|
4983 | BackRefList::const_iterator it = pMedium->m->backRefs.begin();
|
---|
4984 | ++it;
|
---|
4985 | return &it->machineId;
|
---|
4986 | }
|
---|
4987 | }
|
---|
4988 |
|
---|
4989 | MediaList::const_iterator itBegin = pMedium->i_getChildren().begin();
|
---|
4990 | MediaList::const_iterator itEnd = pMedium->i_getChildren().end();
|
---|
4991 | for (MediaList::const_iterator it = itBegin; it != itEnd; ++it)
|
---|
4992 | llMediaTodo.push_back(*it);
|
---|
4993 | }
|
---|
4994 |
|
---|
4995 | return NULL;
|
---|
4996 | }
|
---|
4997 |
|
---|
4998 | const Guid* Medium::i_getFirstMachineBackrefSnapshotId() const
|
---|
4999 | {
|
---|
5000 | if (!m->backRefs.size())
|
---|
5001 | return NULL;
|
---|
5002 |
|
---|
5003 | const BackRef &ref = m->backRefs.front();
|
---|
5004 | if (ref.llSnapshotIds.empty())
|
---|
5005 | return NULL;
|
---|
5006 |
|
---|
5007 | return &ref.llSnapshotIds.front().snapshotId;
|
---|
5008 | }
|
---|
5009 |
|
---|
5010 | size_t Medium::i_getMachineBackRefCount() const
|
---|
5011 | {
|
---|
5012 | return m->backRefs.size();
|
---|
5013 | }
|
---|
5014 |
|
---|
5015 | #ifdef DEBUG
|
---|
5016 | /**
|
---|
5017 | * Debugging helper that gets called after VirtualBox initialization that writes all
|
---|
5018 | * machine backreferences to the debug log.
|
---|
5019 | */
|
---|
5020 | void Medium::i_dumpBackRefs()
|
---|
5021 | {
|
---|
5022 | AutoCaller autoCaller(this);
|
---|
5023 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5024 |
|
---|
5025 | LogFlowThisFunc(("Dumping backrefs for medium '%s':\n", m->strLocationFull.c_str()));
|
---|
5026 |
|
---|
5027 | for (BackRefList::iterator it2 = m->backRefs.begin();
|
---|
5028 | it2 != m->backRefs.end();
|
---|
5029 | ++it2)
|
---|
5030 | {
|
---|
5031 | const BackRef &ref = *it2;
|
---|
5032 | LogFlowThisFunc((" Backref from machine {%RTuuid} (fInCurState: %d, iRefCnt: %d)\n", ref.machineId.raw(), ref.fInCurState, ref.iRefCnt));
|
---|
5033 |
|
---|
5034 | for (std::list<SnapshotRef>::const_iterator jt2 = it2->llSnapshotIds.begin();
|
---|
5035 | jt2 != it2->llSnapshotIds.end();
|
---|
5036 | ++jt2)
|
---|
5037 | {
|
---|
5038 | const Guid &id = jt2->snapshotId;
|
---|
5039 | LogFlowThisFunc((" Backref from snapshot {%RTuuid} (iRefCnt = %d)\n", id.raw(), jt2->iRefCnt));
|
---|
5040 | }
|
---|
5041 | }
|
---|
5042 | }
|
---|
5043 | #endif
|
---|
5044 |
|
---|
5045 | /**
|
---|
5046 | * Checks if the given change of \a aOldPath to \a aNewPath affects the location
|
---|
5047 | * of this media and updates it if necessary to reflect the new location.
|
---|
5048 | *
|
---|
5049 | * @param strOldPath Old path (full).
|
---|
5050 | * @param strNewPath New path (full).
|
---|
5051 | *
|
---|
5052 | * @note Locks this object for writing.
|
---|
5053 | */
|
---|
5054 | HRESULT Medium::i_updatePath(const Utf8Str &strOldPath, const Utf8Str &strNewPath)
|
---|
5055 | {
|
---|
5056 | AssertReturn(!strOldPath.isEmpty(), E_FAIL);
|
---|
5057 | AssertReturn(!strNewPath.isEmpty(), E_FAIL);
|
---|
5058 |
|
---|
5059 | AutoCaller autoCaller(this);
|
---|
5060 | if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
|
---|
5061 |
|
---|
5062 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5063 |
|
---|
5064 | LogFlowThisFunc(("locationFull.before='%s'\n", m->strLocationFull.c_str()));
|
---|
5065 |
|
---|
5066 | const char *pcszMediumPath = m->strLocationFull.c_str();
|
---|
5067 |
|
---|
5068 | if (RTPathStartsWith(pcszMediumPath, strOldPath.c_str()))
|
---|
5069 | {
|
---|
5070 | Utf8Str newPath(strNewPath);
|
---|
5071 | newPath.append(pcszMediumPath + strOldPath.length());
|
---|
5072 | unconst(m->strLocationFull) = newPath;
|
---|
5073 |
|
---|
5074 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
5075 |
|
---|
5076 | LogFlowThisFunc(("locationFull.after='%s'\n", m->strLocationFull.c_str()));
|
---|
5077 | // we changed something
|
---|
5078 | return S_OK;
|
---|
5079 | }
|
---|
5080 |
|
---|
5081 | // no change was necessary, signal error which the caller needs to interpret
|
---|
5082 | return VBOX_E_FILE_ERROR;
|
---|
5083 | }
|
---|
5084 |
|
---|
5085 | /**
|
---|
5086 | * Returns the base medium of the media chain this medium is part of.
|
---|
5087 | *
|
---|
5088 | * The base medium is found by walking up the parent-child relationship axis.
|
---|
5089 | * If the medium doesn't have a parent (i.e. it's a base medium), it
|
---|
5090 | * returns itself in response to this method.
|
---|
5091 | *
|
---|
5092 | * @param aLevel Where to store the number of ancestors of this medium
|
---|
5093 | * (zero for the base), may be @c NULL.
|
---|
5094 | *
|
---|
5095 | * @note Locks medium tree for reading.
|
---|
5096 | */
|
---|
5097 | ComObjPtr<Medium> Medium::i_getBase(uint32_t *aLevel /*= NULL*/)
|
---|
5098 | {
|
---|
5099 | ComObjPtr<Medium> pBase;
|
---|
5100 |
|
---|
5101 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
5102 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
5103 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
5104 | if (!pVirtualBox)
|
---|
5105 | return pBase;
|
---|
5106 |
|
---|
5107 | /* we access m->pParent */
|
---|
5108 | AutoReadLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5109 |
|
---|
5110 | AutoCaller autoCaller(this);
|
---|
5111 | AssertReturn(autoCaller.isOk(), pBase);
|
---|
5112 |
|
---|
5113 | pBase = this;
|
---|
5114 | uint32_t level = 0;
|
---|
5115 |
|
---|
5116 | if (m->pParent)
|
---|
5117 | {
|
---|
5118 | for (;;)
|
---|
5119 | {
|
---|
5120 | AutoCaller baseCaller(pBase);
|
---|
5121 | AssertReturn(baseCaller.isOk(), pBase);
|
---|
5122 |
|
---|
5123 | if (pBase->m->pParent.isNull())
|
---|
5124 | break;
|
---|
5125 |
|
---|
5126 | pBase = pBase->m->pParent;
|
---|
5127 | ++level;
|
---|
5128 | }
|
---|
5129 | }
|
---|
5130 |
|
---|
5131 | if (aLevel != NULL)
|
---|
5132 | *aLevel = level;
|
---|
5133 |
|
---|
5134 | return pBase;
|
---|
5135 | }
|
---|
5136 |
|
---|
5137 | /**
|
---|
5138 | * Returns the depth of this medium in the media chain.
|
---|
5139 | *
|
---|
5140 | * @note Locks medium tree for reading.
|
---|
5141 | */
|
---|
5142 | uint32_t Medium::i_getDepth()
|
---|
5143 | {
|
---|
5144 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
5145 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
5146 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
5147 | if (!pVirtualBox)
|
---|
5148 | return 1;
|
---|
5149 |
|
---|
5150 | /* we access m->pParent */
|
---|
5151 | AutoReadLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5152 |
|
---|
5153 | uint32_t cDepth = 0;
|
---|
5154 | ComObjPtr<Medium> pMedium(this);
|
---|
5155 | while (!pMedium.isNull())
|
---|
5156 | {
|
---|
5157 | AutoCaller autoCaller(this);
|
---|
5158 | AssertReturn(autoCaller.isOk(), cDepth + 1);
|
---|
5159 |
|
---|
5160 | pMedium = pMedium->m->pParent;
|
---|
5161 | cDepth++;
|
---|
5162 | }
|
---|
5163 |
|
---|
5164 | return cDepth;
|
---|
5165 | }
|
---|
5166 |
|
---|
5167 | /**
|
---|
5168 | * Returns @c true if this medium cannot be modified because it has
|
---|
5169 | * dependents (children) or is part of the snapshot. Related to the medium
|
---|
5170 | * type and posterity, not to the current media state.
|
---|
5171 | *
|
---|
5172 | * @note Locks this object and medium tree for reading.
|
---|
5173 | */
|
---|
5174 | bool Medium::i_isReadOnly()
|
---|
5175 | {
|
---|
5176 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
5177 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
5178 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
5179 | if (!pVirtualBox)
|
---|
5180 | return false;
|
---|
5181 |
|
---|
5182 | /* we access children */
|
---|
5183 | AutoReadLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5184 |
|
---|
5185 | AutoCaller autoCaller(this);
|
---|
5186 | AssertComRCReturn(autoCaller.hrc(), false);
|
---|
5187 |
|
---|
5188 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5189 |
|
---|
5190 | switch (m->type)
|
---|
5191 | {
|
---|
5192 | case MediumType_Normal:
|
---|
5193 | {
|
---|
5194 | if (i_getChildren().size() != 0)
|
---|
5195 | return true;
|
---|
5196 |
|
---|
5197 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
5198 | it != m->backRefs.end(); ++it)
|
---|
5199 | if (it->llSnapshotIds.size() != 0)
|
---|
5200 | return true;
|
---|
5201 |
|
---|
5202 | if (m->variant & MediumVariant_VmdkStreamOptimized)
|
---|
5203 | return true;
|
---|
5204 |
|
---|
5205 | return false;
|
---|
5206 | }
|
---|
5207 | case MediumType_Immutable:
|
---|
5208 | case MediumType_MultiAttach:
|
---|
5209 | return true;
|
---|
5210 | case MediumType_Writethrough:
|
---|
5211 | case MediumType_Shareable:
|
---|
5212 | case MediumType_Readonly: /* explicit readonly media has no diffs */
|
---|
5213 | return false;
|
---|
5214 | default:
|
---|
5215 | break;
|
---|
5216 | }
|
---|
5217 |
|
---|
5218 | AssertFailedReturn(false);
|
---|
5219 | }
|
---|
5220 |
|
---|
5221 | /**
|
---|
5222 | * Internal method to update the medium's id. Must have caller + locking!
|
---|
5223 | * @return
|
---|
5224 | */
|
---|
5225 | void Medium::i_updateId(const Guid &id)
|
---|
5226 | {
|
---|
5227 | unconst(m->id) = id;
|
---|
5228 | }
|
---|
5229 |
|
---|
5230 | /**
|
---|
5231 | * Saves the settings of one medium.
|
---|
5232 | *
|
---|
5233 | * @note Caller MUST take care of the medium tree lock and caller.
|
---|
5234 | *
|
---|
5235 | * @param data Settings struct to be updated.
|
---|
5236 | * @param strHardDiskFolder Folder for which paths should be relative.
|
---|
5237 | */
|
---|
5238 | void Medium::i_saveSettingsOne(settings::Medium &data, const Utf8Str &strHardDiskFolder)
|
---|
5239 | {
|
---|
5240 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5241 |
|
---|
5242 | data.uuid = m->id;
|
---|
5243 |
|
---|
5244 | // make path relative if needed
|
---|
5245 | if ( !strHardDiskFolder.isEmpty()
|
---|
5246 | && RTPathStartsWith(m->strLocationFull.c_str(), strHardDiskFolder.c_str())
|
---|
5247 | )
|
---|
5248 | data.strLocation = m->strLocationFull.substr(strHardDiskFolder.length() + 1);
|
---|
5249 | else
|
---|
5250 | data.strLocation = m->strLocationFull;
|
---|
5251 | data.strFormat = m->strFormat;
|
---|
5252 |
|
---|
5253 | /* optional, only for diffs, default is false */
|
---|
5254 | if (m->pParent)
|
---|
5255 | data.fAutoReset = m->autoReset;
|
---|
5256 | else
|
---|
5257 | data.fAutoReset = false;
|
---|
5258 |
|
---|
5259 | /* optional */
|
---|
5260 | data.strDescription = m->strDescription;
|
---|
5261 |
|
---|
5262 | /* optional properties */
|
---|
5263 | data.properties.clear();
|
---|
5264 |
|
---|
5265 | /* handle iSCSI initiator secrets transparently */
|
---|
5266 | bool fHaveInitiatorSecretEncrypted = false;
|
---|
5267 | Utf8Str strCiphertext;
|
---|
5268 | settings::StringsMap::const_iterator itPln = m->mapProperties.find("InitiatorSecret");
|
---|
5269 | if ( itPln != m->mapProperties.end()
|
---|
5270 | && !itPln->second.isEmpty())
|
---|
5271 | {
|
---|
5272 | /* Encrypt the plain secret. If that does not work (i.e. no or wrong settings key
|
---|
5273 | * specified), just use the encrypted secret (if there is any). */
|
---|
5274 | int rc = m->pVirtualBox->i_encryptSetting(itPln->second, &strCiphertext);
|
---|
5275 | if (RT_SUCCESS(rc))
|
---|
5276 | fHaveInitiatorSecretEncrypted = true;
|
---|
5277 | }
|
---|
5278 | for (settings::StringsMap::const_iterator it = m->mapProperties.begin();
|
---|
5279 | it != m->mapProperties.end();
|
---|
5280 | ++it)
|
---|
5281 | {
|
---|
5282 | /* only save properties that have non-default values */
|
---|
5283 | if (!it->second.isEmpty())
|
---|
5284 | {
|
---|
5285 | const Utf8Str &name = it->first;
|
---|
5286 | const Utf8Str &value = it->second;
|
---|
5287 | bool fCreateOnly = false;
|
---|
5288 | for (MediumFormat::PropertyArray::const_iterator itf = m->formatObj->i_getProperties().begin();
|
---|
5289 | itf != m->formatObj->i_getProperties().end();
|
---|
5290 | ++itf)
|
---|
5291 | {
|
---|
5292 | if ( itf->strName.equals(name)
|
---|
5293 | && (itf->flags & VD_CFGKEY_CREATEONLY))
|
---|
5294 | {
|
---|
5295 | fCreateOnly = true;
|
---|
5296 | break;
|
---|
5297 | }
|
---|
5298 | }
|
---|
5299 | if (!fCreateOnly)
|
---|
5300 | /* do NOT store the plain InitiatorSecret */
|
---|
5301 | if ( !fHaveInitiatorSecretEncrypted
|
---|
5302 | || !name.equals("InitiatorSecret"))
|
---|
5303 | data.properties[name] = value;
|
---|
5304 | }
|
---|
5305 | }
|
---|
5306 | if (fHaveInitiatorSecretEncrypted)
|
---|
5307 | data.properties["InitiatorSecretEncrypted"] = strCiphertext;
|
---|
5308 |
|
---|
5309 | /* only for base media */
|
---|
5310 | if (m->pParent.isNull())
|
---|
5311 | data.hdType = m->type;
|
---|
5312 | }
|
---|
5313 |
|
---|
5314 | /**
|
---|
5315 | * Saves medium data by putting it into the provided data structure.
|
---|
5316 | * The settings of all children is saved, too.
|
---|
5317 | *
|
---|
5318 | * @param data Settings struct to be updated.
|
---|
5319 | * @param strHardDiskFolder Folder for which paths should be relative.
|
---|
5320 | *
|
---|
5321 | * @note Locks this object, medium tree and children for reading.
|
---|
5322 | */
|
---|
5323 | HRESULT Medium::i_saveSettings(settings::Medium &data,
|
---|
5324 | const Utf8Str &strHardDiskFolder)
|
---|
5325 | {
|
---|
5326 | /* we access m->pParent */
|
---|
5327 | AutoReadLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5328 |
|
---|
5329 | AutoCaller autoCaller(this);
|
---|
5330 | if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
|
---|
5331 |
|
---|
5332 | MediaList llMediaTodo;
|
---|
5333 | llMediaTodo.push_back(this);
|
---|
5334 | std::list<settings::Medium *> llSettingsTodo;
|
---|
5335 | llSettingsTodo.push_back(&data);
|
---|
5336 |
|
---|
5337 | while (!llMediaTodo.empty())
|
---|
5338 | {
|
---|
5339 | ComObjPtr<Medium> pMedium = llMediaTodo.front();
|
---|
5340 | llMediaTodo.pop_front();
|
---|
5341 | settings::Medium *current = llSettingsTodo.front();
|
---|
5342 | llSettingsTodo.pop_front();
|
---|
5343 |
|
---|
5344 | AutoCaller mediumCaller(pMedium);
|
---|
5345 | if (FAILED(mediumCaller.hrc())) return mediumCaller.hrc();
|
---|
5346 |
|
---|
5347 | pMedium->i_saveSettingsOne(*current, strHardDiskFolder);
|
---|
5348 |
|
---|
5349 | /* save all children */
|
---|
5350 | MediaList::const_iterator itBegin = pMedium->i_getChildren().begin();
|
---|
5351 | MediaList::const_iterator itEnd = pMedium->i_getChildren().end();
|
---|
5352 | for (MediaList::const_iterator it = itBegin; it != itEnd; ++it)
|
---|
5353 | {
|
---|
5354 | llMediaTodo.push_back(*it);
|
---|
5355 | current->llChildren.push_back(settings::Medium::Empty);
|
---|
5356 | llSettingsTodo.push_back(¤t->llChildren.back());
|
---|
5357 | }
|
---|
5358 | }
|
---|
5359 |
|
---|
5360 | return S_OK;
|
---|
5361 | }
|
---|
5362 |
|
---|
5363 | /**
|
---|
5364 | * Constructs a medium lock list for this medium. The lock is not taken.
|
---|
5365 | *
|
---|
5366 | * @note Caller MUST NOT hold the media tree or medium lock.
|
---|
5367 | *
|
---|
5368 | * @param fFailIfInaccessible If true, this fails with an error if a medium is inaccessible. If false,
|
---|
5369 | * inaccessible media are silently skipped and not locked (i.e. their state remains "Inaccessible");
|
---|
5370 | * this is necessary for a VM's removable media VM startup for which we do not want to fail.
|
---|
5371 | * @param pToLockWrite If not NULL, associate a write lock with this medium object.
|
---|
5372 | * @param fMediumLockWriteAll Whether to associate a write lock to all other media too.
|
---|
5373 | * @param pToBeParent Medium which will become the parent of this medium.
|
---|
5374 | * @param mediumLockList Where to store the resulting list.
|
---|
5375 | */
|
---|
5376 | HRESULT Medium::i_createMediumLockList(bool fFailIfInaccessible,
|
---|
5377 | Medium *pToLockWrite,
|
---|
5378 | bool fMediumLockWriteAll,
|
---|
5379 | Medium *pToBeParent,
|
---|
5380 | MediumLockList &mediumLockList)
|
---|
5381 | {
|
---|
5382 | /** @todo r=klaus this needs to be reworked, as the code below uses
|
---|
5383 | * i_getParent without holding the tree lock, and changing this is
|
---|
5384 | * a significant amount of effort. */
|
---|
5385 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
5386 | Assert(!isWriteLockOnCurrentThread());
|
---|
5387 |
|
---|
5388 | AutoCaller autoCaller(this);
|
---|
5389 | if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
|
---|
5390 |
|
---|
5391 | HRESULT rc = S_OK;
|
---|
5392 |
|
---|
5393 | /* paranoid sanity checking if the medium has a to-be parent medium */
|
---|
5394 | if (pToBeParent)
|
---|
5395 | {
|
---|
5396 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5397 | ComAssertRet(i_getParent().isNull(), E_FAIL);
|
---|
5398 | ComAssertRet(i_getChildren().size() == 0, E_FAIL);
|
---|
5399 | }
|
---|
5400 |
|
---|
5401 | ErrorInfoKeeper eik;
|
---|
5402 | MultiResult mrc(S_OK);
|
---|
5403 |
|
---|
5404 | ComObjPtr<Medium> pMedium = this;
|
---|
5405 | while (!pMedium.isNull())
|
---|
5406 | {
|
---|
5407 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
5408 |
|
---|
5409 | /* Accessibility check must be first, otherwise locking interferes
|
---|
5410 | * with getting the medium state. Lock lists are not created for
|
---|
5411 | * fun, and thus getting the medium status is no luxury. */
|
---|
5412 | MediumState_T mediumState = pMedium->i_getState();
|
---|
5413 | if (mediumState == MediumState_Inaccessible)
|
---|
5414 | {
|
---|
5415 | alock.release();
|
---|
5416 | rc = pMedium->i_queryInfo(false /* fSetImageId */, false /* fSetParentId */,
|
---|
5417 | autoCaller);
|
---|
5418 | alock.acquire();
|
---|
5419 | if (FAILED(rc)) return rc;
|
---|
5420 |
|
---|
5421 | mediumState = pMedium->i_getState();
|
---|
5422 | if (mediumState == MediumState_Inaccessible)
|
---|
5423 | {
|
---|
5424 | // ignore inaccessible ISO media and silently return S_OK,
|
---|
5425 | // otherwise VM startup (esp. restore) may fail without good reason
|
---|
5426 | if (!fFailIfInaccessible)
|
---|
5427 | return S_OK;
|
---|
5428 |
|
---|
5429 | // otherwise report an error
|
---|
5430 | Bstr error;
|
---|
5431 | rc = pMedium->COMGETTER(LastAccessError)(error.asOutParam());
|
---|
5432 | if (FAILED(rc)) return rc;
|
---|
5433 |
|
---|
5434 | /* collect multiple errors */
|
---|
5435 | eik.restore();
|
---|
5436 | Assert(!error.isEmpty());
|
---|
5437 | mrc = setError(E_FAIL,
|
---|
5438 | "%ls",
|
---|
5439 | error.raw());
|
---|
5440 | // error message will be something like
|
---|
5441 | // "Could not open the medium ... VD: error VERR_FILE_NOT_FOUND opening image file ... (VERR_FILE_NOT_FOUND).
|
---|
5442 | eik.fetch();
|
---|
5443 | }
|
---|
5444 | }
|
---|
5445 |
|
---|
5446 | if (pMedium == pToLockWrite)
|
---|
5447 | mediumLockList.Prepend(pMedium, true);
|
---|
5448 | else
|
---|
5449 | mediumLockList.Prepend(pMedium, fMediumLockWriteAll);
|
---|
5450 |
|
---|
5451 | pMedium = pMedium->i_getParent();
|
---|
5452 | if (pMedium.isNull() && pToBeParent)
|
---|
5453 | {
|
---|
5454 | pMedium = pToBeParent;
|
---|
5455 | pToBeParent = NULL;
|
---|
5456 | }
|
---|
5457 | }
|
---|
5458 |
|
---|
5459 | return mrc;
|
---|
5460 | }
|
---|
5461 |
|
---|
5462 | /**
|
---|
5463 | * Creates a new differencing storage unit using the format of the given target
|
---|
5464 | * medium and the location. Note that @c aTarget must be NotCreated.
|
---|
5465 | *
|
---|
5466 | * The @a aMediumLockList parameter contains the associated medium lock list,
|
---|
5467 | * which must be in locked state. If @a aWait is @c true then the caller is
|
---|
5468 | * responsible for unlocking.
|
---|
5469 | *
|
---|
5470 | * If @a aProgress is not NULL but the object it points to is @c null then a
|
---|
5471 | * new progress object will be created and assigned to @a *aProgress on
|
---|
5472 | * success, otherwise the existing progress object is used. If @a aProgress is
|
---|
5473 | * NULL, then no progress object is created/used at all.
|
---|
5474 | *
|
---|
5475 | * When @a aWait is @c false, this method will create a thread to perform the
|
---|
5476 | * create operation asynchronously and will return immediately. Otherwise, it
|
---|
5477 | * will perform the operation on the calling thread and will not return to the
|
---|
5478 | * caller until the operation is completed. Note that @a aProgress cannot be
|
---|
5479 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
5480 | *
|
---|
5481 | * @param aTarget Target medium.
|
---|
5482 | * @param aVariant Precise medium variant to create.
|
---|
5483 | * @param aMediumLockList List of media which should be locked.
|
---|
5484 | * @param aProgress Where to find/store a Progress object to track
|
---|
5485 | * operation completion.
|
---|
5486 | * @param aWait @c true if this method should block instead of
|
---|
5487 | * creating an asynchronous thread.
|
---|
5488 | * @param aNotify Notify about mediums which metadatа are changed
|
---|
5489 | * during execution of the function.
|
---|
5490 | *
|
---|
5491 | * @note Locks this object and @a aTarget for writing.
|
---|
5492 | */
|
---|
5493 | HRESULT Medium::i_createDiffStorage(ComObjPtr<Medium> &aTarget,
|
---|
5494 | MediumVariant_T aVariant,
|
---|
5495 | MediumLockList *aMediumLockList,
|
---|
5496 | ComObjPtr<Progress> *aProgress,
|
---|
5497 | bool aWait,
|
---|
5498 | bool aNotify)
|
---|
5499 | {
|
---|
5500 | AssertReturn(!aTarget.isNull(), E_FAIL);
|
---|
5501 | AssertReturn(aMediumLockList, E_FAIL);
|
---|
5502 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
5503 |
|
---|
5504 | AutoCaller autoCaller(this);
|
---|
5505 | if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
|
---|
5506 |
|
---|
5507 | AutoCaller targetCaller(aTarget);
|
---|
5508 | if (FAILED(targetCaller.hrc())) return targetCaller.hrc();
|
---|
5509 |
|
---|
5510 | HRESULT rc = S_OK;
|
---|
5511 | ComObjPtr<Progress> pProgress;
|
---|
5512 | Medium::Task *pTask = NULL;
|
---|
5513 |
|
---|
5514 | try
|
---|
5515 | {
|
---|
5516 | AutoMultiWriteLock2 alock(this, aTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5517 |
|
---|
5518 | ComAssertThrow( m->type != MediumType_Writethrough
|
---|
5519 | && m->type != MediumType_Shareable
|
---|
5520 | && m->type != MediumType_Readonly, E_FAIL);
|
---|
5521 | ComAssertThrow(m->state == MediumState_LockedRead, E_FAIL);
|
---|
5522 |
|
---|
5523 | if (aTarget->m->state != MediumState_NotCreated)
|
---|
5524 | throw aTarget->i_setStateError();
|
---|
5525 |
|
---|
5526 | /* Check that the medium is not attached to the current state of
|
---|
5527 | * any VM referring to it. */
|
---|
5528 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
5529 | it != m->backRefs.end();
|
---|
5530 | ++it)
|
---|
5531 | {
|
---|
5532 | if (it->fInCurState)
|
---|
5533 | {
|
---|
5534 | /* Note: when a VM snapshot is being taken, all normal media
|
---|
5535 | * attached to the VM in the current state will be, as an
|
---|
5536 | * exception, also associated with the snapshot which is about
|
---|
5537 | * to create (see SnapshotMachine::init()) before deassociating
|
---|
5538 | * them from the current state (which takes place only on
|
---|
5539 | * success in Machine::fixupHardDisks()), so that the size of
|
---|
5540 | * snapshotIds will be 1 in this case. The extra condition is
|
---|
5541 | * used to filter out this legal situation. */
|
---|
5542 | if (it->llSnapshotIds.size() == 0)
|
---|
5543 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5544 | 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"),
|
---|
5545 | m->strLocationFull.c_str(), it->machineId.raw());
|
---|
5546 |
|
---|
5547 | Assert(it->llSnapshotIds.size() == 1);
|
---|
5548 | }
|
---|
5549 | }
|
---|
5550 |
|
---|
5551 | if (aProgress != NULL)
|
---|
5552 | {
|
---|
5553 | /* use the existing progress object... */
|
---|
5554 | pProgress = *aProgress;
|
---|
5555 |
|
---|
5556 | /* ...but create a new one if it is null */
|
---|
5557 | if (pProgress.isNull())
|
---|
5558 | {
|
---|
5559 | pProgress.createObject();
|
---|
5560 | rc = pProgress->init(m->pVirtualBox,
|
---|
5561 | static_cast<IMedium*>(this),
|
---|
5562 | BstrFmt(tr("Creating differencing medium storage unit '%s'"),
|
---|
5563 | aTarget->m->strLocationFull.c_str()).raw(),
|
---|
5564 | TRUE /* aCancelable */);
|
---|
5565 | if (FAILED(rc))
|
---|
5566 | throw rc;
|
---|
5567 | }
|
---|
5568 | }
|
---|
5569 |
|
---|
5570 | /* setup task object to carry out the operation sync/async */
|
---|
5571 | pTask = new Medium::CreateDiffTask(this, pProgress, aTarget, aVariant,
|
---|
5572 | aMediumLockList,
|
---|
5573 | aWait /* fKeepMediumLockList */,
|
---|
5574 | aNotify);
|
---|
5575 | rc = pTask->hrc();
|
---|
5576 | AssertComRC(rc);
|
---|
5577 | if (FAILED(rc))
|
---|
5578 | throw rc;
|
---|
5579 |
|
---|
5580 | /* register a task (it will deregister itself when done) */
|
---|
5581 | ++m->numCreateDiffTasks;
|
---|
5582 | Assert(m->numCreateDiffTasks != 0); /* overflow? */
|
---|
5583 |
|
---|
5584 | aTarget->m->state = MediumState_Creating;
|
---|
5585 | }
|
---|
5586 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5587 |
|
---|
5588 | if (SUCCEEDED(rc))
|
---|
5589 | {
|
---|
5590 | if (aWait)
|
---|
5591 | {
|
---|
5592 | rc = pTask->runNow();
|
---|
5593 | delete pTask;
|
---|
5594 | }
|
---|
5595 | else
|
---|
5596 | rc = pTask->createThread();
|
---|
5597 | pTask = NULL;
|
---|
5598 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
5599 | *aProgress = pProgress;
|
---|
5600 | }
|
---|
5601 | else if (pTask != NULL)
|
---|
5602 | delete pTask;
|
---|
5603 |
|
---|
5604 | return rc;
|
---|
5605 | }
|
---|
5606 |
|
---|
5607 | /**
|
---|
5608 | * Returns a preferred format for differencing media.
|
---|
5609 | */
|
---|
5610 | Utf8Str Medium::i_getPreferredDiffFormat()
|
---|
5611 | {
|
---|
5612 | AutoCaller autoCaller(this);
|
---|
5613 | AssertComRCReturn(autoCaller.hrc(), Utf8Str::Empty);
|
---|
5614 |
|
---|
5615 | /* check that our own format supports diffs */
|
---|
5616 | if (!(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_Differencing))
|
---|
5617 | {
|
---|
5618 | /* use the default format if not */
|
---|
5619 | Utf8Str tmp;
|
---|
5620 | m->pVirtualBox->i_getDefaultHardDiskFormat(tmp);
|
---|
5621 | return tmp;
|
---|
5622 | }
|
---|
5623 |
|
---|
5624 | /* m->strFormat is const, no need to lock */
|
---|
5625 | return m->strFormat;
|
---|
5626 | }
|
---|
5627 |
|
---|
5628 | /**
|
---|
5629 | * Returns a preferred variant for differencing media.
|
---|
5630 | */
|
---|
5631 | MediumVariant_T Medium::i_getPreferredDiffVariant()
|
---|
5632 | {
|
---|
5633 | AutoCaller autoCaller(this);
|
---|
5634 | AssertComRCReturn(autoCaller.hrc(), MediumVariant_Standard);
|
---|
5635 |
|
---|
5636 | /* check that our own format supports diffs */
|
---|
5637 | if (!(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_Differencing))
|
---|
5638 | return MediumVariant_Standard;
|
---|
5639 |
|
---|
5640 | /* m->variant is const, no need to lock */
|
---|
5641 | ULONG mediumVariantFlags = (ULONG)m->variant;
|
---|
5642 | mediumVariantFlags &= ~(ULONG)(MediumVariant_Fixed | MediumVariant_VmdkStreamOptimized | MediumVariant_VmdkESX | MediumVariant_VmdkRawDisk);
|
---|
5643 | mediumVariantFlags |= MediumVariant_Diff;
|
---|
5644 | return (MediumVariant_T)mediumVariantFlags;
|
---|
5645 | }
|
---|
5646 |
|
---|
5647 | /**
|
---|
5648 | * Implementation for the public Medium::Close() with the exception of calling
|
---|
5649 | * VirtualBox::saveRegistries(), in case someone wants to call this for several
|
---|
5650 | * media.
|
---|
5651 | *
|
---|
5652 | * After this returns with success, uninit() has been called on the medium, and
|
---|
5653 | * the object is no longer usable ("not ready" state).
|
---|
5654 | *
|
---|
5655 | * @param autoCaller AutoCaller instance which must have been created on the caller's
|
---|
5656 | * stack for this medium. This gets released hereupon
|
---|
5657 | * which the Medium instance gets uninitialized.
|
---|
5658 | * @return
|
---|
5659 | */
|
---|
5660 | HRESULT Medium::i_close(AutoCaller &autoCaller)
|
---|
5661 | {
|
---|
5662 | // must temporarily drop the caller, need the tree lock first
|
---|
5663 | autoCaller.release();
|
---|
5664 |
|
---|
5665 | // we're accessing parent/child and backrefs, so lock the tree first, then ourselves
|
---|
5666 | AutoMultiWriteLock2 multilock(&m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
5667 | this->lockHandle()
|
---|
5668 | COMMA_LOCKVAL_SRC_POS);
|
---|
5669 |
|
---|
5670 | autoCaller.add();
|
---|
5671 | if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
|
---|
5672 |
|
---|
5673 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
5674 | while (m->queryInfoRunning)
|
---|
5675 | {
|
---|
5676 | autoCaller.release();
|
---|
5677 | multilock.release();
|
---|
5678 | /* Must not hold the media tree lock, as Medium::i_queryInfo needs
|
---|
5679 | * this lock and thus we would run into a deadlock here. */
|
---|
5680 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
5681 | /* must not hold the object lock now */
|
---|
5682 | Assert(!isWriteLockOnCurrentThread());
|
---|
5683 | {
|
---|
5684 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
5685 | }
|
---|
5686 | multilock.acquire();
|
---|
5687 | autoCaller.add();
|
---|
5688 | if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
|
---|
5689 | }
|
---|
5690 |
|
---|
5691 | LogFlowFunc(("ENTER for %s\n", i_getLocationFull().c_str()));
|
---|
5692 |
|
---|
5693 | bool wasCreated = true;
|
---|
5694 |
|
---|
5695 | switch (m->state)
|
---|
5696 | {
|
---|
5697 | case MediumState_NotCreated:
|
---|
5698 | wasCreated = false;
|
---|
5699 | break;
|
---|
5700 | case MediumState_Created:
|
---|
5701 | case MediumState_Inaccessible:
|
---|
5702 | break;
|
---|
5703 | default:
|
---|
5704 | return i_setStateError();
|
---|
5705 | }
|
---|
5706 |
|
---|
5707 | if (m->backRefs.size() != 0)
|
---|
5708 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
5709 | tr("Medium '%s' cannot be closed because it is still attached to %d virtual machines", "",
|
---|
5710 | m->backRefs.size()),
|
---|
5711 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
5712 |
|
---|
5713 | // perform extra media-dependent close checks
|
---|
5714 | HRESULT rc = i_canClose();
|
---|
5715 | if (FAILED(rc)) return rc;
|
---|
5716 |
|
---|
5717 | m->fClosing = true;
|
---|
5718 |
|
---|
5719 | if (wasCreated)
|
---|
5720 | {
|
---|
5721 | // remove from the list of known media before performing actual
|
---|
5722 | // uninitialization (to keep the media registry consistent on
|
---|
5723 | // failure to do so)
|
---|
5724 | rc = i_unregisterWithVirtualBox();
|
---|
5725 | if (FAILED(rc)) return rc;
|
---|
5726 |
|
---|
5727 | multilock.release();
|
---|
5728 | // Release the AutoCaller now, as otherwise uninit() will simply hang.
|
---|
5729 | // Needs to be done before mark the registries as modified and saving
|
---|
5730 | // the registry, as otherwise there may be a deadlock with someone else
|
---|
5731 | // closing this object while we're in i_saveModifiedRegistries(), which
|
---|
5732 | // needs the media tree lock, which the other thread holds until after
|
---|
5733 | // uninit() below.
|
---|
5734 | autoCaller.release();
|
---|
5735 | i_markRegistriesModified();
|
---|
5736 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
5737 | }
|
---|
5738 | else
|
---|
5739 | {
|
---|
5740 | multilock.release();
|
---|
5741 | // release the AutoCaller, as otherwise uninit() will simply hang
|
---|
5742 | autoCaller.release();
|
---|
5743 | }
|
---|
5744 |
|
---|
5745 | // Keep the locks held until after uninit, as otherwise the consistency
|
---|
5746 | // of the medium tree cannot be guaranteed.
|
---|
5747 | uninit();
|
---|
5748 |
|
---|
5749 | LogFlowFuncLeave();
|
---|
5750 |
|
---|
5751 | return rc;
|
---|
5752 | }
|
---|
5753 |
|
---|
5754 | /**
|
---|
5755 | * Deletes the medium storage unit.
|
---|
5756 | *
|
---|
5757 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
5758 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
5759 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
5760 | * progress object is created/used at all.
|
---|
5761 | *
|
---|
5762 | * When @a aWait is @c false, this method will create a thread to perform the
|
---|
5763 | * delete operation asynchronously and will return immediately. Otherwise, it
|
---|
5764 | * will perform the operation on the calling thread and will not return to the
|
---|
5765 | * caller until the operation is completed. Note that @a aProgress cannot be
|
---|
5766 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
5767 | *
|
---|
5768 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
5769 | * completion.
|
---|
5770 | * @param aWait @c true if this method should block instead of creating
|
---|
5771 | * an asynchronous thread.
|
---|
5772 | * @param aNotify Notify about mediums which metadatа are changed
|
---|
5773 | * during execution of the function.
|
---|
5774 | *
|
---|
5775 | * @note Locks mVirtualBox and this object for writing. Locks medium tree for
|
---|
5776 | * writing.
|
---|
5777 | */
|
---|
5778 | HRESULT Medium::i_deleteStorage(ComObjPtr<Progress> *aProgress,
|
---|
5779 | bool aWait, bool aNotify)
|
---|
5780 | {
|
---|
5781 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
5782 |
|
---|
5783 | HRESULT rc = S_OK;
|
---|
5784 | ComObjPtr<Progress> pProgress;
|
---|
5785 | Medium::Task *pTask = NULL;
|
---|
5786 |
|
---|
5787 | try
|
---|
5788 | {
|
---|
5789 | /* we're accessing the media tree, and i_canClose() needs it too */
|
---|
5790 | AutoWriteLock treelock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5791 |
|
---|
5792 | AutoCaller autoCaller(this);
|
---|
5793 | AssertComRCThrowRC(autoCaller.hrc());
|
---|
5794 |
|
---|
5795 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5796 |
|
---|
5797 | LogFlowThisFunc(("aWait=%RTbool locationFull=%s\n", aWait, i_getLocationFull().c_str() ));
|
---|
5798 |
|
---|
5799 | if ( !(m->formatObj->i_getCapabilities() & ( MediumFormatCapabilities_CreateDynamic
|
---|
5800 | | MediumFormatCapabilities_CreateFixed)))
|
---|
5801 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
5802 | tr("Medium format '%s' does not support storage deletion"),
|
---|
5803 | m->strFormat.c_str());
|
---|
5804 |
|
---|
5805 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
5806 | /** @todo r=klaus would be great if this could be moved to the async
|
---|
5807 | * part of the operation as it can take quite a while */
|
---|
5808 | while (m->queryInfoRunning)
|
---|
5809 | {
|
---|
5810 | alock.release();
|
---|
5811 | autoCaller.release();
|
---|
5812 | treelock.release();
|
---|
5813 | /* Must not hold the media tree lock or the object lock, as
|
---|
5814 | * Medium::i_queryInfo needs this lock and thus we would run
|
---|
5815 | * into a deadlock here. */
|
---|
5816 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
5817 | Assert(!isWriteLockOnCurrentThread());
|
---|
5818 | {
|
---|
5819 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
5820 | }
|
---|
5821 | treelock.acquire();
|
---|
5822 | autoCaller.add();
|
---|
5823 | AssertComRCThrowRC(autoCaller.hrc());
|
---|
5824 | alock.acquire();
|
---|
5825 | }
|
---|
5826 |
|
---|
5827 | /* Note that we are fine with Inaccessible state too: a) for symmetry
|
---|
5828 | * with create calls and b) because it doesn't really harm to try, if
|
---|
5829 | * it is really inaccessible, the delete operation will fail anyway.
|
---|
5830 | * Accepting Inaccessible state is especially important because all
|
---|
5831 | * registered media are initially Inaccessible upon VBoxSVC startup
|
---|
5832 | * until COMGETTER(RefreshState) is called. Accept Deleting state
|
---|
5833 | * because some callers need to put the medium in this state early
|
---|
5834 | * to prevent races. */
|
---|
5835 | switch (m->state)
|
---|
5836 | {
|
---|
5837 | case MediumState_Created:
|
---|
5838 | case MediumState_Deleting:
|
---|
5839 | case MediumState_Inaccessible:
|
---|
5840 | break;
|
---|
5841 | default:
|
---|
5842 | throw i_setStateError();
|
---|
5843 | }
|
---|
5844 |
|
---|
5845 | if (m->backRefs.size() != 0)
|
---|
5846 | {
|
---|
5847 | Utf8Str strMachines;
|
---|
5848 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
5849 | it != m->backRefs.end();
|
---|
5850 | ++it)
|
---|
5851 | {
|
---|
5852 | const BackRef &b = *it;
|
---|
5853 | if (strMachines.length())
|
---|
5854 | strMachines.append(", ");
|
---|
5855 | strMachines.append(b.machineId.toString().c_str());
|
---|
5856 | }
|
---|
5857 | #ifdef DEBUG
|
---|
5858 | i_dumpBackRefs();
|
---|
5859 | #endif
|
---|
5860 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5861 | tr("Cannot delete storage: medium '%s' is still attached to the following %d virtual machine(s): %s",
|
---|
5862 | "", m->backRefs.size()),
|
---|
5863 | m->strLocationFull.c_str(),
|
---|
5864 | m->backRefs.size(),
|
---|
5865 | strMachines.c_str());
|
---|
5866 | }
|
---|
5867 |
|
---|
5868 | rc = i_canClose();
|
---|
5869 | if (FAILED(rc))
|
---|
5870 | throw rc;
|
---|
5871 |
|
---|
5872 | /* go to Deleting state, so that the medium is not actually locked */
|
---|
5873 | if (m->state != MediumState_Deleting)
|
---|
5874 | {
|
---|
5875 | rc = i_markForDeletion();
|
---|
5876 | if (FAILED(rc))
|
---|
5877 | throw rc;
|
---|
5878 | }
|
---|
5879 |
|
---|
5880 | /* Build the medium lock list. */
|
---|
5881 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
5882 | alock.release();
|
---|
5883 | autoCaller.release();
|
---|
5884 | treelock.release();
|
---|
5885 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5886 | this /* pToLockWrite */,
|
---|
5887 | false /* fMediumLockWriteAll */,
|
---|
5888 | NULL,
|
---|
5889 | *pMediumLockList);
|
---|
5890 | treelock.acquire();
|
---|
5891 | autoCaller.add();
|
---|
5892 | AssertComRCThrowRC(autoCaller.hrc());
|
---|
5893 | alock.acquire();
|
---|
5894 | if (FAILED(rc))
|
---|
5895 | {
|
---|
5896 | delete pMediumLockList;
|
---|
5897 | throw rc;
|
---|
5898 | }
|
---|
5899 |
|
---|
5900 | alock.release();
|
---|
5901 | autoCaller.release();
|
---|
5902 | treelock.release();
|
---|
5903 | rc = pMediumLockList->Lock();
|
---|
5904 | treelock.acquire();
|
---|
5905 | autoCaller.add();
|
---|
5906 | AssertComRCThrowRC(autoCaller.hrc());
|
---|
5907 | alock.acquire();
|
---|
5908 | if (FAILED(rc))
|
---|
5909 | {
|
---|
5910 | delete pMediumLockList;
|
---|
5911 | throw setError(rc,
|
---|
5912 | tr("Failed to lock media when deleting '%s'"),
|
---|
5913 | i_getLocationFull().c_str());
|
---|
5914 | }
|
---|
5915 |
|
---|
5916 | /* try to remove from the list of known media before performing
|
---|
5917 | * actual deletion (we favor the consistency of the media registry
|
---|
5918 | * which would have been broken if unregisterWithVirtualBox() failed
|
---|
5919 | * after we successfully deleted the storage) */
|
---|
5920 | rc = i_unregisterWithVirtualBox();
|
---|
5921 | if (FAILED(rc))
|
---|
5922 | throw rc;
|
---|
5923 | // no longer need lock
|
---|
5924 | alock.release();
|
---|
5925 | autoCaller.release();
|
---|
5926 | treelock.release();
|
---|
5927 | i_markRegistriesModified();
|
---|
5928 |
|
---|
5929 | if (aProgress != NULL)
|
---|
5930 | {
|
---|
5931 | /* use the existing progress object... */
|
---|
5932 | pProgress = *aProgress;
|
---|
5933 |
|
---|
5934 | /* ...but create a new one if it is null */
|
---|
5935 | if (pProgress.isNull())
|
---|
5936 | {
|
---|
5937 | pProgress.createObject();
|
---|
5938 | rc = pProgress->init(m->pVirtualBox,
|
---|
5939 | static_cast<IMedium*>(this),
|
---|
5940 | BstrFmt(tr("Deleting medium storage unit '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
5941 | FALSE /* aCancelable */);
|
---|
5942 | if (FAILED(rc))
|
---|
5943 | throw rc;
|
---|
5944 | }
|
---|
5945 | }
|
---|
5946 |
|
---|
5947 | /* setup task object to carry out the operation sync/async */
|
---|
5948 | pTask = new Medium::DeleteTask(this, pProgress, pMediumLockList, false, aNotify);
|
---|
5949 | rc = pTask->hrc();
|
---|
5950 | AssertComRC(rc);
|
---|
5951 | if (FAILED(rc))
|
---|
5952 | throw rc;
|
---|
5953 | }
|
---|
5954 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5955 |
|
---|
5956 | if (SUCCEEDED(rc))
|
---|
5957 | {
|
---|
5958 | if (aWait)
|
---|
5959 | {
|
---|
5960 | rc = pTask->runNow();
|
---|
5961 | delete pTask;
|
---|
5962 | }
|
---|
5963 | else
|
---|
5964 | rc = pTask->createThread();
|
---|
5965 | pTask = NULL;
|
---|
5966 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
5967 | *aProgress = pProgress;
|
---|
5968 | }
|
---|
5969 | else
|
---|
5970 | {
|
---|
5971 | if (pTask)
|
---|
5972 | delete pTask;
|
---|
5973 |
|
---|
5974 | /* Undo deleting state if necessary. */
|
---|
5975 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5976 | /* Make sure that any error signalled by unmarkForDeletion() is not
|
---|
5977 | * ending up in the error list (if the caller uses MultiResult). It
|
---|
5978 | * usually is spurious, as in most cases the medium hasn't been marked
|
---|
5979 | * for deletion when the error was thrown above. */
|
---|
5980 | ErrorInfoKeeper eik;
|
---|
5981 | i_unmarkForDeletion();
|
---|
5982 | }
|
---|
5983 |
|
---|
5984 | return rc;
|
---|
5985 | }
|
---|
5986 |
|
---|
5987 | /**
|
---|
5988 | * Mark a medium for deletion.
|
---|
5989 | *
|
---|
5990 | * @note Caller must hold the write lock on this medium!
|
---|
5991 | */
|
---|
5992 | HRESULT Medium::i_markForDeletion()
|
---|
5993 | {
|
---|
5994 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5995 | switch (m->state)
|
---|
5996 | {
|
---|
5997 | case MediumState_Created:
|
---|
5998 | case MediumState_Inaccessible:
|
---|
5999 | m->preLockState = m->state;
|
---|
6000 | m->state = MediumState_Deleting;
|
---|
6001 | return S_OK;
|
---|
6002 | default:
|
---|
6003 | return i_setStateError();
|
---|
6004 | }
|
---|
6005 | }
|
---|
6006 |
|
---|
6007 | /**
|
---|
6008 | * Removes the "mark for deletion".
|
---|
6009 | *
|
---|
6010 | * @note Caller must hold the write lock on this medium!
|
---|
6011 | */
|
---|
6012 | HRESULT Medium::i_unmarkForDeletion()
|
---|
6013 | {
|
---|
6014 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
6015 | switch (m->state)
|
---|
6016 | {
|
---|
6017 | case MediumState_Deleting:
|
---|
6018 | m->state = m->preLockState;
|
---|
6019 | return S_OK;
|
---|
6020 | default:
|
---|
6021 | return i_setStateError();
|
---|
6022 | }
|
---|
6023 | }
|
---|
6024 |
|
---|
6025 | /**
|
---|
6026 | * Mark a medium for deletion which is in locked state.
|
---|
6027 | *
|
---|
6028 | * @note Caller must hold the write lock on this medium!
|
---|
6029 | */
|
---|
6030 | HRESULT Medium::i_markLockedForDeletion()
|
---|
6031 | {
|
---|
6032 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
6033 | if ( ( m->state == MediumState_LockedRead
|
---|
6034 | || m->state == MediumState_LockedWrite)
|
---|
6035 | && m->preLockState == MediumState_Created)
|
---|
6036 | {
|
---|
6037 | m->preLockState = MediumState_Deleting;
|
---|
6038 | return S_OK;
|
---|
6039 | }
|
---|
6040 | else
|
---|
6041 | return i_setStateError();
|
---|
6042 | }
|
---|
6043 |
|
---|
6044 | /**
|
---|
6045 | * Removes the "mark for deletion" for a medium in locked state.
|
---|
6046 | *
|
---|
6047 | * @note Caller must hold the write lock on this medium!
|
---|
6048 | */
|
---|
6049 | HRESULT Medium::i_unmarkLockedForDeletion()
|
---|
6050 | {
|
---|
6051 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
6052 | if ( ( m->state == MediumState_LockedRead
|
---|
6053 | || m->state == MediumState_LockedWrite)
|
---|
6054 | && m->preLockState == MediumState_Deleting)
|
---|
6055 | {
|
---|
6056 | m->preLockState = MediumState_Created;
|
---|
6057 | return S_OK;
|
---|
6058 | }
|
---|
6059 | else
|
---|
6060 | return i_setStateError();
|
---|
6061 | }
|
---|
6062 |
|
---|
6063 | /**
|
---|
6064 | * Queries the preferred merge direction from this to the other medium, i.e.
|
---|
6065 | * the one which requires the least amount of I/O and therefore time and
|
---|
6066 | * disk consumption.
|
---|
6067 | *
|
---|
6068 | * @returns Status code.
|
---|
6069 | * @retval E_FAIL in case determining the merge direction fails for some reason,
|
---|
6070 | * for example if getting the size of the media fails. There is no
|
---|
6071 | * error set though and the caller is free to continue to find out
|
---|
6072 | * what was going wrong later. Leaves fMergeForward unset.
|
---|
6073 | * @retval VBOX_E_INVALID_OBJECT_STATE if both media are not related to each other
|
---|
6074 | * An error is set.
|
---|
6075 | * @param pOther The other medium to merge with.
|
---|
6076 | * @param fMergeForward Resulting preferred merge direction (out).
|
---|
6077 | */
|
---|
6078 | HRESULT Medium::i_queryPreferredMergeDirection(const ComObjPtr<Medium> &pOther,
|
---|
6079 | bool &fMergeForward)
|
---|
6080 | {
|
---|
6081 | AssertReturn(pOther != NULL, E_FAIL);
|
---|
6082 | AssertReturn(pOther != this, E_FAIL);
|
---|
6083 |
|
---|
6084 | HRESULT rc = S_OK;
|
---|
6085 | bool fThisParent = false; /**<< Flag whether this medium is the parent of pOther. */
|
---|
6086 |
|
---|
6087 | try
|
---|
6088 | {
|
---|
6089 | // locking: we need the tree lock first because we access parent pointers
|
---|
6090 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
6091 |
|
---|
6092 | AutoCaller autoCaller(this);
|
---|
6093 | AssertComRCThrowRC(autoCaller.hrc());
|
---|
6094 |
|
---|
6095 | AutoCaller otherCaller(pOther);
|
---|
6096 | AssertComRCThrowRC(otherCaller.hrc());
|
---|
6097 |
|
---|
6098 | /* more sanity checking and figuring out the current merge direction */
|
---|
6099 | ComObjPtr<Medium> pMedium = i_getParent();
|
---|
6100 | while (!pMedium.isNull() && pMedium != pOther)
|
---|
6101 | pMedium = pMedium->i_getParent();
|
---|
6102 | if (pMedium == pOther)
|
---|
6103 | fThisParent = false;
|
---|
6104 | else
|
---|
6105 | {
|
---|
6106 | pMedium = pOther->i_getParent();
|
---|
6107 | while (!pMedium.isNull() && pMedium != this)
|
---|
6108 | pMedium = pMedium->i_getParent();
|
---|
6109 | if (pMedium == this)
|
---|
6110 | fThisParent = true;
|
---|
6111 | else
|
---|
6112 | {
|
---|
6113 | Utf8Str tgtLoc;
|
---|
6114 | {
|
---|
6115 | AutoReadLock alock(pOther COMMA_LOCKVAL_SRC_POS);
|
---|
6116 | tgtLoc = pOther->i_getLocationFull();
|
---|
6117 | }
|
---|
6118 |
|
---|
6119 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6120 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
6121 | tr("Media '%s' and '%s' are unrelated"),
|
---|
6122 | m->strLocationFull.c_str(), tgtLoc.c_str());
|
---|
6123 | }
|
---|
6124 | }
|
---|
6125 |
|
---|
6126 | /*
|
---|
6127 | * Figure out the preferred merge direction. The current way is to
|
---|
6128 | * get the current sizes of file based images and select the merge
|
---|
6129 | * direction depending on the size.
|
---|
6130 | *
|
---|
6131 | * Can't use the VD API to get current size here as the media might
|
---|
6132 | * be write locked by a running VM. Resort to RTFileQuerySize().
|
---|
6133 | */
|
---|
6134 | int vrc = VINF_SUCCESS;
|
---|
6135 | uint64_t cbMediumThis = 0;
|
---|
6136 | uint64_t cbMediumOther = 0;
|
---|
6137 |
|
---|
6138 | if (i_isMediumFormatFile() && pOther->i_isMediumFormatFile())
|
---|
6139 | {
|
---|
6140 | vrc = RTFileQuerySizeByPath(this->i_getLocationFull().c_str(), &cbMediumThis);
|
---|
6141 | if (RT_SUCCESS(vrc))
|
---|
6142 | {
|
---|
6143 | vrc = RTFileQuerySizeByPath(pOther->i_getLocationFull().c_str(),
|
---|
6144 | &cbMediumOther);
|
---|
6145 | }
|
---|
6146 |
|
---|
6147 | if (RT_FAILURE(vrc))
|
---|
6148 | rc = E_FAIL;
|
---|
6149 | else
|
---|
6150 | {
|
---|
6151 | /*
|
---|
6152 | * Check which merge direction might be more optimal.
|
---|
6153 | * This method is not bullet proof of course as there might
|
---|
6154 | * be overlapping blocks in the images so the file size is
|
---|
6155 | * not the best indicator but it is good enough for our purpose
|
---|
6156 | * and everything else is too complicated, especially when the
|
---|
6157 | * media are used by a running VM.
|
---|
6158 | */
|
---|
6159 |
|
---|
6160 | uint32_t mediumVariants = MediumVariant_Fixed | MediumVariant_VmdkStreamOptimized;
|
---|
6161 | uint32_t mediumCaps = MediumFormatCapabilities_CreateDynamic | MediumFormatCapabilities_File;
|
---|
6162 |
|
---|
6163 | bool fDynamicOther = pOther->i_getMediumFormat()->i_getCapabilities() & mediumCaps
|
---|
6164 | && pOther->i_getVariant() & ~mediumVariants;
|
---|
6165 | bool fDynamicThis = i_getMediumFormat()->i_getCapabilities() & mediumCaps
|
---|
6166 | && i_getVariant() & ~mediumVariants;
|
---|
6167 | bool fMergeIntoThis = (fDynamicThis && !fDynamicOther)
|
---|
6168 | || (fDynamicThis == fDynamicOther && cbMediumThis > cbMediumOther);
|
---|
6169 | fMergeForward = fMergeIntoThis != fThisParent;
|
---|
6170 | }
|
---|
6171 | }
|
---|
6172 | }
|
---|
6173 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6174 |
|
---|
6175 | return rc;
|
---|
6176 | }
|
---|
6177 |
|
---|
6178 | /**
|
---|
6179 | * Prepares this (source) medium, target medium and all intermediate media
|
---|
6180 | * for the merge operation.
|
---|
6181 | *
|
---|
6182 | * This method is to be called prior to calling the #mergeTo() to perform
|
---|
6183 | * necessary consistency checks and place involved media to appropriate
|
---|
6184 | * states. If #mergeTo() is not called or fails, the state modifications
|
---|
6185 | * performed by this method must be undone by #i_cancelMergeTo().
|
---|
6186 | *
|
---|
6187 | * See #mergeTo() for more information about merging.
|
---|
6188 | *
|
---|
6189 | * @param pTarget Target medium.
|
---|
6190 | * @param aMachineId Allowed machine attachment. NULL means do not check.
|
---|
6191 | * @param aSnapshotId Allowed snapshot attachment. NULL or empty UUID means
|
---|
6192 | * do not check.
|
---|
6193 | * @param fLockMedia Flag whether to lock the medium lock list or not.
|
---|
6194 | * If set to false and the medium lock list locking fails
|
---|
6195 | * later you must call #i_cancelMergeTo().
|
---|
6196 | * @param fMergeForward Resulting merge direction (out).
|
---|
6197 | * @param pParentForTarget New parent for target medium after merge (out).
|
---|
6198 | * @param aChildrenToReparent Medium lock list containing all children of the
|
---|
6199 | * source which will have to be reparented to the target
|
---|
6200 | * after merge (out).
|
---|
6201 | * @param aMediumLockList Medium locking information (out).
|
---|
6202 | *
|
---|
6203 | * @note Locks medium tree for reading. Locks this object, aTarget and all
|
---|
6204 | * intermediate media for writing.
|
---|
6205 | */
|
---|
6206 | HRESULT Medium::i_prepareMergeTo(const ComObjPtr<Medium> &pTarget,
|
---|
6207 | const Guid *aMachineId,
|
---|
6208 | const Guid *aSnapshotId,
|
---|
6209 | bool fLockMedia,
|
---|
6210 | bool &fMergeForward,
|
---|
6211 | ComObjPtr<Medium> &pParentForTarget,
|
---|
6212 | MediumLockList * &aChildrenToReparent,
|
---|
6213 | MediumLockList * &aMediumLockList)
|
---|
6214 | {
|
---|
6215 | AssertReturn(pTarget != NULL, E_FAIL);
|
---|
6216 | AssertReturn(pTarget != this, E_FAIL);
|
---|
6217 |
|
---|
6218 | HRESULT rc = S_OK;
|
---|
6219 | fMergeForward = false;
|
---|
6220 | pParentForTarget.setNull();
|
---|
6221 | Assert(aChildrenToReparent == NULL);
|
---|
6222 | aChildrenToReparent = NULL;
|
---|
6223 | Assert(aMediumLockList == NULL);
|
---|
6224 | aMediumLockList = NULL;
|
---|
6225 |
|
---|
6226 | try
|
---|
6227 | {
|
---|
6228 | // locking: we need the tree lock first because we access parent pointers
|
---|
6229 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
6230 |
|
---|
6231 | AutoCaller autoCaller(this);
|
---|
6232 | AssertComRCThrowRC(autoCaller.hrc());
|
---|
6233 |
|
---|
6234 | AutoCaller targetCaller(pTarget);
|
---|
6235 | AssertComRCThrowRC(targetCaller.hrc());
|
---|
6236 |
|
---|
6237 | /* more sanity checking and figuring out the merge direction */
|
---|
6238 | ComObjPtr<Medium> pMedium = i_getParent();
|
---|
6239 | while (!pMedium.isNull() && pMedium != pTarget)
|
---|
6240 | pMedium = pMedium->i_getParent();
|
---|
6241 | if (pMedium == pTarget)
|
---|
6242 | fMergeForward = false;
|
---|
6243 | else
|
---|
6244 | {
|
---|
6245 | pMedium = pTarget->i_getParent();
|
---|
6246 | while (!pMedium.isNull() && pMedium != this)
|
---|
6247 | pMedium = pMedium->i_getParent();
|
---|
6248 | if (pMedium == this)
|
---|
6249 | fMergeForward = true;
|
---|
6250 | else
|
---|
6251 | {
|
---|
6252 | Utf8Str tgtLoc;
|
---|
6253 | {
|
---|
6254 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
6255 | tgtLoc = pTarget->i_getLocationFull();
|
---|
6256 | }
|
---|
6257 |
|
---|
6258 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6259 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
6260 | tr("Media '%s' and '%s' are unrelated"),
|
---|
6261 | m->strLocationFull.c_str(), tgtLoc.c_str());
|
---|
6262 | }
|
---|
6263 | }
|
---|
6264 |
|
---|
6265 | /* Build the lock list. */
|
---|
6266 | aMediumLockList = new MediumLockList();
|
---|
6267 | targetCaller.release();
|
---|
6268 | autoCaller.release();
|
---|
6269 | treeLock.release();
|
---|
6270 | if (fMergeForward)
|
---|
6271 | rc = pTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
6272 | pTarget /* pToLockWrite */,
|
---|
6273 | false /* fMediumLockWriteAll */,
|
---|
6274 | NULL,
|
---|
6275 | *aMediumLockList);
|
---|
6276 | else
|
---|
6277 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
6278 | pTarget /* pToLockWrite */,
|
---|
6279 | false /* fMediumLockWriteAll */,
|
---|
6280 | NULL,
|
---|
6281 | *aMediumLockList);
|
---|
6282 | treeLock.acquire();
|
---|
6283 | autoCaller.add();
|
---|
6284 | AssertComRCThrowRC(autoCaller.hrc());
|
---|
6285 | targetCaller.add();
|
---|
6286 | AssertComRCThrowRC(targetCaller.hrc());
|
---|
6287 | if (FAILED(rc))
|
---|
6288 | throw rc;
|
---|
6289 |
|
---|
6290 | /* Sanity checking, must be after lock list creation as it depends on
|
---|
6291 | * valid medium states. The medium objects must be accessible. Only
|
---|
6292 | * do this if immediate locking is requested, otherwise it fails when
|
---|
6293 | * we construct a medium lock list for an already running VM. Snapshot
|
---|
6294 | * deletion uses this to simplify its life. */
|
---|
6295 | if (fLockMedia)
|
---|
6296 | {
|
---|
6297 | {
|
---|
6298 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6299 | if (m->state != MediumState_Created)
|
---|
6300 | throw i_setStateError();
|
---|
6301 | }
|
---|
6302 | {
|
---|
6303 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
6304 | if (pTarget->m->state != MediumState_Created)
|
---|
6305 | throw pTarget->i_setStateError();
|
---|
6306 | }
|
---|
6307 | }
|
---|
6308 |
|
---|
6309 | /* check medium attachment and other sanity conditions */
|
---|
6310 | if (fMergeForward)
|
---|
6311 | {
|
---|
6312 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6313 | if (i_getChildren().size() > 1)
|
---|
6314 | {
|
---|
6315 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
6316 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
6317 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
6318 | }
|
---|
6319 | /* One backreference is only allowed if the machine ID is not empty
|
---|
6320 | * and it matches the machine the medium is attached to (including
|
---|
6321 | * the snapshot ID if not empty). */
|
---|
6322 | if ( m->backRefs.size() != 0
|
---|
6323 | && ( !aMachineId
|
---|
6324 | || m->backRefs.size() != 1
|
---|
6325 | || aMachineId->isZero()
|
---|
6326 | || *i_getFirstMachineBackrefId() != *aMachineId
|
---|
6327 | || ( (!aSnapshotId || !aSnapshotId->isZero())
|
---|
6328 | && *i_getFirstMachineBackrefSnapshotId() != *aSnapshotId)))
|
---|
6329 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
6330 | tr("Medium '%s' is attached to %d virtual machines", "", m->backRefs.size()),
|
---|
6331 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
6332 | if (m->type == MediumType_Immutable)
|
---|
6333 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
6334 | tr("Medium '%s' is immutable"),
|
---|
6335 | m->strLocationFull.c_str());
|
---|
6336 | if (m->type == MediumType_MultiAttach)
|
---|
6337 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
6338 | tr("Medium '%s' is multi-attach"),
|
---|
6339 | m->strLocationFull.c_str());
|
---|
6340 | }
|
---|
6341 | else
|
---|
6342 | {
|
---|
6343 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
6344 | if (pTarget->i_getChildren().size() > 1)
|
---|
6345 | {
|
---|
6346 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
6347 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
6348 | pTarget->m->strLocationFull.c_str(),
|
---|
6349 | pTarget->i_getChildren().size());
|
---|
6350 | }
|
---|
6351 | if (pTarget->m->type == MediumType_Immutable)
|
---|
6352 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
6353 | tr("Medium '%s' is immutable"),
|
---|
6354 | pTarget->m->strLocationFull.c_str());
|
---|
6355 | if (pTarget->m->type == MediumType_MultiAttach)
|
---|
6356 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
6357 | tr("Medium '%s' is multi-attach"),
|
---|
6358 | pTarget->m->strLocationFull.c_str());
|
---|
6359 | }
|
---|
6360 | ComObjPtr<Medium> pLast(fMergeForward ? (Medium *)pTarget : this);
|
---|
6361 | ComObjPtr<Medium> pLastIntermediate = pLast->i_getParent();
|
---|
6362 | for (pLast = pLastIntermediate;
|
---|
6363 | !pLast.isNull() && pLast != pTarget && pLast != this;
|
---|
6364 | pLast = pLast->i_getParent())
|
---|
6365 | {
|
---|
6366 | AutoReadLock alock(pLast COMMA_LOCKVAL_SRC_POS);
|
---|
6367 | if (pLast->i_getChildren().size() > 1)
|
---|
6368 | {
|
---|
6369 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
6370 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
6371 | pLast->m->strLocationFull.c_str(),
|
---|
6372 | pLast->i_getChildren().size());
|
---|
6373 | }
|
---|
6374 | if (pLast->m->backRefs.size() != 0)
|
---|
6375 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
6376 | tr("Medium '%s' is attached to %d virtual machines", "", pLast->m->backRefs.size()),
|
---|
6377 | pLast->m->strLocationFull.c_str(),
|
---|
6378 | pLast->m->backRefs.size());
|
---|
6379 |
|
---|
6380 | }
|
---|
6381 |
|
---|
6382 | /* Update medium states appropriately */
|
---|
6383 | {
|
---|
6384 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6385 |
|
---|
6386 | if (m->state == MediumState_Created)
|
---|
6387 | {
|
---|
6388 | rc = i_markForDeletion();
|
---|
6389 | if (FAILED(rc))
|
---|
6390 | throw rc;
|
---|
6391 | }
|
---|
6392 | else
|
---|
6393 | {
|
---|
6394 | if (fLockMedia)
|
---|
6395 | throw i_setStateError();
|
---|
6396 | else if ( m->state == MediumState_LockedWrite
|
---|
6397 | || m->state == MediumState_LockedRead)
|
---|
6398 | {
|
---|
6399 | /* Either mark it for deletion in locked state or allow
|
---|
6400 | * others to have done so. */
|
---|
6401 | if (m->preLockState == MediumState_Created)
|
---|
6402 | i_markLockedForDeletion();
|
---|
6403 | else if (m->preLockState != MediumState_Deleting)
|
---|
6404 | throw i_setStateError();
|
---|
6405 | }
|
---|
6406 | else
|
---|
6407 | throw i_setStateError();
|
---|
6408 | }
|
---|
6409 | }
|
---|
6410 |
|
---|
6411 | if (fMergeForward)
|
---|
6412 | {
|
---|
6413 | /* we will need parent to reparent target */
|
---|
6414 | pParentForTarget = i_getParent();
|
---|
6415 | }
|
---|
6416 | else
|
---|
6417 | {
|
---|
6418 | /* we will need to reparent children of the source */
|
---|
6419 | aChildrenToReparent = new MediumLockList();
|
---|
6420 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
6421 | it != i_getChildren().end();
|
---|
6422 | ++it)
|
---|
6423 | {
|
---|
6424 | pMedium = *it;
|
---|
6425 | aChildrenToReparent->Append(pMedium, true /* fLockWrite */);
|
---|
6426 | }
|
---|
6427 | if (fLockMedia && aChildrenToReparent)
|
---|
6428 | {
|
---|
6429 | targetCaller.release();
|
---|
6430 | autoCaller.release();
|
---|
6431 | treeLock.release();
|
---|
6432 | rc = aChildrenToReparent->Lock();
|
---|
6433 | treeLock.acquire();
|
---|
6434 | autoCaller.add();
|
---|
6435 | AssertComRCThrowRC(autoCaller.hrc());
|
---|
6436 | targetCaller.add();
|
---|
6437 | AssertComRCThrowRC(targetCaller.hrc());
|
---|
6438 | if (FAILED(rc))
|
---|
6439 | throw rc;
|
---|
6440 | }
|
---|
6441 | }
|
---|
6442 | for (pLast = pLastIntermediate;
|
---|
6443 | !pLast.isNull() && pLast != pTarget && pLast != this;
|
---|
6444 | pLast = pLast->i_getParent())
|
---|
6445 | {
|
---|
6446 | AutoWriteLock alock(pLast COMMA_LOCKVAL_SRC_POS);
|
---|
6447 | if (pLast->m->state == MediumState_Created)
|
---|
6448 | {
|
---|
6449 | rc = pLast->i_markForDeletion();
|
---|
6450 | if (FAILED(rc))
|
---|
6451 | throw rc;
|
---|
6452 | }
|
---|
6453 | else
|
---|
6454 | throw pLast->i_setStateError();
|
---|
6455 | }
|
---|
6456 |
|
---|
6457 | /* Tweak the lock list in the backward merge case, as the target
|
---|
6458 | * isn't marked to be locked for writing yet. */
|
---|
6459 | if (!fMergeForward)
|
---|
6460 | {
|
---|
6461 | MediumLockList::Base::iterator lockListBegin =
|
---|
6462 | aMediumLockList->GetBegin();
|
---|
6463 | MediumLockList::Base::iterator lockListEnd =
|
---|
6464 | aMediumLockList->GetEnd();
|
---|
6465 | ++lockListEnd;
|
---|
6466 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
6467 | it != lockListEnd;
|
---|
6468 | ++it)
|
---|
6469 | {
|
---|
6470 | MediumLock &mediumLock = *it;
|
---|
6471 | if (mediumLock.GetMedium() == pTarget)
|
---|
6472 | {
|
---|
6473 | HRESULT rc2 = mediumLock.UpdateLock(true);
|
---|
6474 | AssertComRC(rc2);
|
---|
6475 | break;
|
---|
6476 | }
|
---|
6477 | }
|
---|
6478 | }
|
---|
6479 |
|
---|
6480 | if (fLockMedia)
|
---|
6481 | {
|
---|
6482 | targetCaller.release();
|
---|
6483 | autoCaller.release();
|
---|
6484 | treeLock.release();
|
---|
6485 | rc = aMediumLockList->Lock();
|
---|
6486 | treeLock.acquire();
|
---|
6487 | autoCaller.add();
|
---|
6488 | AssertComRCThrowRC(autoCaller.hrc());
|
---|
6489 | targetCaller.add();
|
---|
6490 | AssertComRCThrowRC(targetCaller.hrc());
|
---|
6491 | if (FAILED(rc))
|
---|
6492 | {
|
---|
6493 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
6494 | throw setError(rc,
|
---|
6495 | tr("Failed to lock media when merging to '%s'"),
|
---|
6496 | pTarget->i_getLocationFull().c_str());
|
---|
6497 | }
|
---|
6498 | }
|
---|
6499 | }
|
---|
6500 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6501 |
|
---|
6502 | if (FAILED(rc))
|
---|
6503 | {
|
---|
6504 | if (aMediumLockList)
|
---|
6505 | {
|
---|
6506 | delete aMediumLockList;
|
---|
6507 | aMediumLockList = NULL;
|
---|
6508 | }
|
---|
6509 | if (aChildrenToReparent)
|
---|
6510 | {
|
---|
6511 | delete aChildrenToReparent;
|
---|
6512 | aChildrenToReparent = NULL;
|
---|
6513 | }
|
---|
6514 | }
|
---|
6515 |
|
---|
6516 | return rc;
|
---|
6517 | }
|
---|
6518 |
|
---|
6519 | /**
|
---|
6520 | * Merges this medium to the specified medium which must be either its
|
---|
6521 | * direct ancestor or descendant.
|
---|
6522 | *
|
---|
6523 | * Given this medium is SOURCE and the specified medium is TARGET, we will
|
---|
6524 | * get two variants of the merge operation:
|
---|
6525 | *
|
---|
6526 | * forward merge
|
---|
6527 | * ------------------------->
|
---|
6528 | * [Extra] <- SOURCE <- Intermediate <- TARGET
|
---|
6529 | * Any Del Del LockWr
|
---|
6530 | *
|
---|
6531 | *
|
---|
6532 | * backward merge
|
---|
6533 | * <-------------------------
|
---|
6534 | * TARGET <- Intermediate <- SOURCE <- [Extra]
|
---|
6535 | * LockWr Del Del LockWr
|
---|
6536 | *
|
---|
6537 | * Each diagram shows the involved media on the media chain where
|
---|
6538 | * SOURCE and TARGET belong. Under each medium there is a state value which
|
---|
6539 | * the medium must have at a time of the mergeTo() call.
|
---|
6540 | *
|
---|
6541 | * The media in the square braces may be absent (e.g. when the forward
|
---|
6542 | * operation takes place and SOURCE is the base medium, or when the backward
|
---|
6543 | * merge operation takes place and TARGET is the last child in the chain) but if
|
---|
6544 | * they present they are involved too as shown.
|
---|
6545 | *
|
---|
6546 | * Neither the source medium nor intermediate media may be attached to
|
---|
6547 | * any VM directly or in the snapshot, otherwise this method will assert.
|
---|
6548 | *
|
---|
6549 | * The #i_prepareMergeTo() method must be called prior to this method to place
|
---|
6550 | * all involved to necessary states and perform other consistency checks.
|
---|
6551 | *
|
---|
6552 | * If @a aWait is @c true then this method will perform the operation on the
|
---|
6553 | * calling thread and will not return to the caller until the operation is
|
---|
6554 | * completed. When this method succeeds, all intermediate medium objects in
|
---|
6555 | * the chain will be uninitialized, the state of the target medium (and all
|
---|
6556 | * involved extra media) will be restored. @a aMediumLockList will not be
|
---|
6557 | * deleted, whether the operation is successful or not. The caller has to do
|
---|
6558 | * this if appropriate. Note that this (source) medium is not uninitialized
|
---|
6559 | * because of possible AutoCaller instances held by the caller of this method
|
---|
6560 | * on the current thread. It's therefore the responsibility of the caller to
|
---|
6561 | * call Medium::uninit() after releasing all callers.
|
---|
6562 | *
|
---|
6563 | * If @a aWait is @c false then this method will create a thread to perform the
|
---|
6564 | * operation asynchronously and will return immediately. If the operation
|
---|
6565 | * succeeds, the thread will uninitialize the source medium object and all
|
---|
6566 | * intermediate medium objects in the chain, reset the state of the target
|
---|
6567 | * medium (and all involved extra media) and delete @a aMediumLockList.
|
---|
6568 | * If the operation fails, the thread will only reset the states of all
|
---|
6569 | * involved media and delete @a aMediumLockList.
|
---|
6570 | *
|
---|
6571 | * When this method fails (regardless of the @a aWait mode), it is a caller's
|
---|
6572 | * responsibility to undo state changes and delete @a aMediumLockList using
|
---|
6573 | * #i_cancelMergeTo().
|
---|
6574 | *
|
---|
6575 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
6576 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
6577 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
6578 | * progress object is created/used at all. Note that @a aProgress cannot be
|
---|
6579 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
6580 | *
|
---|
6581 | * @param pTarget Target medium.
|
---|
6582 | * @param fMergeForward Merge direction.
|
---|
6583 | * @param pParentForTarget New parent for target medium after merge.
|
---|
6584 | * @param aChildrenToReparent List of children of the source which will have
|
---|
6585 | * to be reparented to the target after merge.
|
---|
6586 | * @param aMediumLockList Medium locking information.
|
---|
6587 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
6588 | * completion.
|
---|
6589 | * @param aWait @c true if this method should block instead of creating
|
---|
6590 | * an asynchronous thread.
|
---|
6591 | * @param aNotify Notify about mediums which metadatа are changed
|
---|
6592 | * during execution of the function.
|
---|
6593 | *
|
---|
6594 | * @note Locks the tree lock for writing. Locks the media from the chain
|
---|
6595 | * for writing.
|
---|
6596 | */
|
---|
6597 | HRESULT Medium::i_mergeTo(const ComObjPtr<Medium> &pTarget,
|
---|
6598 | bool fMergeForward,
|
---|
6599 | const ComObjPtr<Medium> &pParentForTarget,
|
---|
6600 | MediumLockList *aChildrenToReparent,
|
---|
6601 | MediumLockList *aMediumLockList,
|
---|
6602 | ComObjPtr<Progress> *aProgress,
|
---|
6603 | bool aWait, bool aNotify)
|
---|
6604 | {
|
---|
6605 | AssertReturn(pTarget != NULL, E_FAIL);
|
---|
6606 | AssertReturn(pTarget != this, E_FAIL);
|
---|
6607 | AssertReturn(aMediumLockList != NULL, E_FAIL);
|
---|
6608 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
6609 |
|
---|
6610 | AutoCaller autoCaller(this);
|
---|
6611 | AssertComRCReturnRC(autoCaller.hrc());
|
---|
6612 |
|
---|
6613 | AutoCaller targetCaller(pTarget);
|
---|
6614 | AssertComRCReturnRC(targetCaller.hrc());
|
---|
6615 |
|
---|
6616 | HRESULT rc = S_OK;
|
---|
6617 | ComObjPtr<Progress> pProgress;
|
---|
6618 | Medium::Task *pTask = NULL;
|
---|
6619 |
|
---|
6620 | try
|
---|
6621 | {
|
---|
6622 | if (aProgress != NULL)
|
---|
6623 | {
|
---|
6624 | /* use the existing progress object... */
|
---|
6625 | pProgress = *aProgress;
|
---|
6626 |
|
---|
6627 | /* ...but create a new one if it is null */
|
---|
6628 | if (pProgress.isNull())
|
---|
6629 | {
|
---|
6630 | Utf8Str tgtName;
|
---|
6631 | {
|
---|
6632 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
6633 | tgtName = pTarget->i_getName();
|
---|
6634 | }
|
---|
6635 |
|
---|
6636 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6637 |
|
---|
6638 | pProgress.createObject();
|
---|
6639 | rc = pProgress->init(m->pVirtualBox,
|
---|
6640 | static_cast<IMedium*>(this),
|
---|
6641 | BstrFmt(tr("Merging medium '%s' to '%s'"),
|
---|
6642 | i_getName().c_str(),
|
---|
6643 | tgtName.c_str()).raw(),
|
---|
6644 | TRUE, /* aCancelable */
|
---|
6645 | 2, /* Number of opearations */
|
---|
6646 | BstrFmt(tr("Resizing medium '%s' before merge"),
|
---|
6647 | tgtName.c_str()).raw()
|
---|
6648 | );
|
---|
6649 | if (FAILED(rc))
|
---|
6650 | throw rc;
|
---|
6651 | }
|
---|
6652 | }
|
---|
6653 |
|
---|
6654 | /* setup task object to carry out the operation sync/async */
|
---|
6655 | pTask = new Medium::MergeTask(this, pTarget, fMergeForward,
|
---|
6656 | pParentForTarget, aChildrenToReparent,
|
---|
6657 | pProgress, aMediumLockList,
|
---|
6658 | aWait /* fKeepMediumLockList */,
|
---|
6659 | aNotify);
|
---|
6660 | rc = pTask->hrc();
|
---|
6661 | AssertComRC(rc);
|
---|
6662 | if (FAILED(rc))
|
---|
6663 | throw rc;
|
---|
6664 | }
|
---|
6665 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6666 |
|
---|
6667 | if (SUCCEEDED(rc))
|
---|
6668 | {
|
---|
6669 | if (aWait)
|
---|
6670 | {
|
---|
6671 | rc = pTask->runNow();
|
---|
6672 | delete pTask;
|
---|
6673 | }
|
---|
6674 | else
|
---|
6675 | rc = pTask->createThread();
|
---|
6676 | pTask = NULL;
|
---|
6677 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
6678 | *aProgress = pProgress;
|
---|
6679 | }
|
---|
6680 | else if (pTask != NULL)
|
---|
6681 | delete pTask;
|
---|
6682 |
|
---|
6683 | return rc;
|
---|
6684 | }
|
---|
6685 |
|
---|
6686 | /**
|
---|
6687 | * Undoes what #i_prepareMergeTo() did. Must be called if #mergeTo() is not
|
---|
6688 | * called or fails. Frees memory occupied by @a aMediumLockList and unlocks
|
---|
6689 | * the medium objects in @a aChildrenToReparent.
|
---|
6690 | *
|
---|
6691 | * @param aChildrenToReparent List of children of the source which will have
|
---|
6692 | * to be reparented to the target after merge.
|
---|
6693 | * @param aMediumLockList Medium locking information.
|
---|
6694 | *
|
---|
6695 | * @note Locks the tree lock for writing. Locks the media from the chain
|
---|
6696 | * for writing.
|
---|
6697 | */
|
---|
6698 | void Medium::i_cancelMergeTo(MediumLockList *aChildrenToReparent,
|
---|
6699 | MediumLockList *aMediumLockList)
|
---|
6700 | {
|
---|
6701 | AutoCaller autoCaller(this);
|
---|
6702 | AssertComRCReturnVoid(autoCaller.hrc());
|
---|
6703 |
|
---|
6704 | AssertReturnVoid(aMediumLockList != NULL);
|
---|
6705 |
|
---|
6706 | /* Revert media marked for deletion to previous state. */
|
---|
6707 | HRESULT rc;
|
---|
6708 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
6709 | aMediumLockList->GetBegin();
|
---|
6710 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
6711 | aMediumLockList->GetEnd();
|
---|
6712 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
6713 | it != mediumListEnd;
|
---|
6714 | ++it)
|
---|
6715 | {
|
---|
6716 | const MediumLock &mediumLock = *it;
|
---|
6717 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
6718 | AutoWriteLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
6719 |
|
---|
6720 | if (pMedium->m->state == MediumState_Deleting)
|
---|
6721 | {
|
---|
6722 | rc = pMedium->i_unmarkForDeletion();
|
---|
6723 | AssertComRC(rc);
|
---|
6724 | }
|
---|
6725 | else if ( ( pMedium->m->state == MediumState_LockedWrite
|
---|
6726 | || pMedium->m->state == MediumState_LockedRead)
|
---|
6727 | && pMedium->m->preLockState == MediumState_Deleting)
|
---|
6728 | {
|
---|
6729 | rc = pMedium->i_unmarkLockedForDeletion();
|
---|
6730 | AssertComRC(rc);
|
---|
6731 | }
|
---|
6732 | }
|
---|
6733 |
|
---|
6734 | /* the destructor will do the work */
|
---|
6735 | delete aMediumLockList;
|
---|
6736 |
|
---|
6737 | /* unlock the children which had to be reparented, the destructor will do
|
---|
6738 | * the work */
|
---|
6739 | if (aChildrenToReparent)
|
---|
6740 | delete aChildrenToReparent;
|
---|
6741 | }
|
---|
6742 |
|
---|
6743 | /**
|
---|
6744 | * Resizes the media.
|
---|
6745 | *
|
---|
6746 | * If @a aWait is @c true then this method will perform the operation on the
|
---|
6747 | * calling thread and will not return to the caller until the operation is
|
---|
6748 | * completed. When this method succeeds, the state of the target medium (and all
|
---|
6749 | * involved extra media) will be restored. @a aMediumLockList will not be
|
---|
6750 | * deleted, whether the operation is successful or not. The caller has to do
|
---|
6751 | * this if appropriate.
|
---|
6752 | *
|
---|
6753 | * If @a aWait is @c false then this method will create a thread to perform the
|
---|
6754 | * operation asynchronously and will return immediately. The thread will reset
|
---|
6755 | * the state of the target medium (and all involved extra media) and delete
|
---|
6756 | * @a aMediumLockList.
|
---|
6757 | *
|
---|
6758 | * When this method fails (regardless of the @a aWait mode), it is a caller's
|
---|
6759 | * responsibility to undo state changes and delete @a aMediumLockList.
|
---|
6760 | *
|
---|
6761 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
6762 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
6763 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
6764 | * progress object is created/used at all. Note that @a aProgress cannot be
|
---|
6765 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
6766 | *
|
---|
6767 | * @param aLogicalSize New nominal capacity of the medium in bytes.
|
---|
6768 | * @param aMediumLockList Medium locking information.
|
---|
6769 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
6770 | * completion.
|
---|
6771 | * @param aWait @c true if this method should block instead of creating
|
---|
6772 | * an asynchronous thread.
|
---|
6773 | * @param aNotify Notify about mediums which metadatа are changed
|
---|
6774 | * during execution of the function.
|
---|
6775 | *
|
---|
6776 | * @note Locks the media from the chain for writing.
|
---|
6777 | */
|
---|
6778 |
|
---|
6779 | HRESULT Medium::i_resize(uint64_t aLogicalSize,
|
---|
6780 | MediumLockList *aMediumLockList,
|
---|
6781 | ComObjPtr<Progress> *aProgress,
|
---|
6782 | bool aWait,
|
---|
6783 | bool aNotify)
|
---|
6784 | {
|
---|
6785 | AssertReturn(aMediumLockList != NULL, E_FAIL);
|
---|
6786 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
6787 |
|
---|
6788 | AutoCaller autoCaller(this);
|
---|
6789 | AssertComRCReturnRC(autoCaller.hrc());
|
---|
6790 |
|
---|
6791 | HRESULT rc = S_OK;
|
---|
6792 | ComObjPtr<Progress> pProgress;
|
---|
6793 | Medium::Task *pTask = NULL;
|
---|
6794 |
|
---|
6795 | try
|
---|
6796 | {
|
---|
6797 | if (aProgress != NULL)
|
---|
6798 | {
|
---|
6799 | /* use the existing progress object... */
|
---|
6800 | pProgress = *aProgress;
|
---|
6801 |
|
---|
6802 | /* ...but create a new one if it is null */
|
---|
6803 | if (pProgress.isNull())
|
---|
6804 | {
|
---|
6805 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6806 |
|
---|
6807 | pProgress.createObject();
|
---|
6808 | rc = pProgress->init(m->pVirtualBox,
|
---|
6809 | static_cast <IMedium *>(this),
|
---|
6810 | BstrFmt(tr("Resizing medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
6811 | TRUE /* aCancelable */);
|
---|
6812 | if (FAILED(rc))
|
---|
6813 | throw rc;
|
---|
6814 | }
|
---|
6815 | }
|
---|
6816 |
|
---|
6817 | /* setup task object to carry out the operation asynchronously */
|
---|
6818 | pTask = new Medium::ResizeTask(this,
|
---|
6819 | aLogicalSize,
|
---|
6820 | pProgress,
|
---|
6821 | aMediumLockList,
|
---|
6822 | aWait /* fKeepMediumLockList */,
|
---|
6823 | aNotify);
|
---|
6824 | rc = pTask->hrc();
|
---|
6825 | AssertComRC(rc);
|
---|
6826 | if (FAILED(rc))
|
---|
6827 | throw rc;
|
---|
6828 | }
|
---|
6829 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6830 |
|
---|
6831 | if (SUCCEEDED(rc))
|
---|
6832 | {
|
---|
6833 | if (aWait)
|
---|
6834 | {
|
---|
6835 | rc = pTask->runNow();
|
---|
6836 | delete pTask;
|
---|
6837 | }
|
---|
6838 | else
|
---|
6839 | rc = pTask->createThread();
|
---|
6840 | pTask = NULL;
|
---|
6841 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
6842 | *aProgress = pProgress;
|
---|
6843 | }
|
---|
6844 | else if (pTask != NULL)
|
---|
6845 | delete pTask;
|
---|
6846 |
|
---|
6847 | return rc;
|
---|
6848 | }
|
---|
6849 |
|
---|
6850 | /**
|
---|
6851 | * Fix the parent UUID of all children to point to this medium as their
|
---|
6852 | * parent.
|
---|
6853 | */
|
---|
6854 | HRESULT Medium::i_fixParentUuidOfChildren(MediumLockList *pChildrenToReparent)
|
---|
6855 | {
|
---|
6856 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
6857 | * to lock order violations, it probably causes lock order issues related
|
---|
6858 | * to the AutoCaller usage. Likewise the code using this method seems
|
---|
6859 | * problematic. */
|
---|
6860 | Assert(!isWriteLockOnCurrentThread());
|
---|
6861 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
6862 | MediumLockList mediumLockList;
|
---|
6863 | HRESULT rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
6864 | NULL /* pToLockWrite */,
|
---|
6865 | false /* fMediumLockWriteAll */,
|
---|
6866 | this,
|
---|
6867 | mediumLockList);
|
---|
6868 | AssertComRCReturnRC(rc);
|
---|
6869 |
|
---|
6870 | try
|
---|
6871 | {
|
---|
6872 | PVDISK hdd;
|
---|
6873 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
6874 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6875 |
|
---|
6876 | try
|
---|
6877 | {
|
---|
6878 | MediumLockList::Base::iterator lockListBegin =
|
---|
6879 | mediumLockList.GetBegin();
|
---|
6880 | MediumLockList::Base::iterator lockListEnd =
|
---|
6881 | mediumLockList.GetEnd();
|
---|
6882 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
6883 | it != lockListEnd;
|
---|
6884 | ++it)
|
---|
6885 | {
|
---|
6886 | MediumLock &mediumLock = *it;
|
---|
6887 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
6888 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
6889 |
|
---|
6890 | // open the medium
|
---|
6891 | vrc = VDOpen(hdd,
|
---|
6892 | pMedium->m->strFormat.c_str(),
|
---|
6893 | pMedium->m->strLocationFull.c_str(),
|
---|
6894 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
6895 | pMedium->m->vdImageIfaces);
|
---|
6896 | if (RT_FAILURE(vrc))
|
---|
6897 | throw vrc;
|
---|
6898 | }
|
---|
6899 |
|
---|
6900 | MediumLockList::Base::iterator childrenBegin = pChildrenToReparent->GetBegin();
|
---|
6901 | MediumLockList::Base::iterator childrenEnd = pChildrenToReparent->GetEnd();
|
---|
6902 | for (MediumLockList::Base::iterator it = childrenBegin;
|
---|
6903 | it != childrenEnd;
|
---|
6904 | ++it)
|
---|
6905 | {
|
---|
6906 | Medium *pMedium = it->GetMedium();
|
---|
6907 | /* VD_OPEN_FLAGS_INFO since UUID is wrong yet */
|
---|
6908 | vrc = VDOpen(hdd,
|
---|
6909 | pMedium->m->strFormat.c_str(),
|
---|
6910 | pMedium->m->strLocationFull.c_str(),
|
---|
6911 | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
6912 | pMedium->m->vdImageIfaces);
|
---|
6913 | if (RT_FAILURE(vrc))
|
---|
6914 | throw vrc;
|
---|
6915 |
|
---|
6916 | vrc = VDSetParentUuid(hdd, VD_LAST_IMAGE, m->id.raw());
|
---|
6917 | if (RT_FAILURE(vrc))
|
---|
6918 | throw vrc;
|
---|
6919 |
|
---|
6920 | vrc = VDClose(hdd, false /* fDelete */);
|
---|
6921 | if (RT_FAILURE(vrc))
|
---|
6922 | throw vrc;
|
---|
6923 | }
|
---|
6924 | }
|
---|
6925 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6926 | catch (int aVRC)
|
---|
6927 | {
|
---|
6928 | rc = setErrorBoth(E_FAIL, aVRC,
|
---|
6929 | tr("Could not update medium UUID references to parent '%s' (%s)"),
|
---|
6930 | m->strLocationFull.c_str(),
|
---|
6931 | i_vdError(aVRC).c_str());
|
---|
6932 | }
|
---|
6933 |
|
---|
6934 | VDDestroy(hdd);
|
---|
6935 | }
|
---|
6936 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6937 |
|
---|
6938 | return rc;
|
---|
6939 | }
|
---|
6940 |
|
---|
6941 | /**
|
---|
6942 | *
|
---|
6943 | * @note Similar code exists in i_taskExportHandler.
|
---|
6944 | */
|
---|
6945 | HRESULT Medium::i_addRawToFss(const char *aFilename, SecretKeyStore *pKeyStore, RTVFSFSSTREAM hVfsFssDst,
|
---|
6946 | const ComObjPtr<Progress> &aProgress, bool fSparse)
|
---|
6947 | {
|
---|
6948 | AutoCaller autoCaller(this);
|
---|
6949 | HRESULT hrc = autoCaller.hrc();
|
---|
6950 | if (SUCCEEDED(hrc))
|
---|
6951 | {
|
---|
6952 | /*
|
---|
6953 | * Get a readonly hdd for this medium.
|
---|
6954 | */
|
---|
6955 | MediumCryptoFilterSettings CryptoSettingsRead;
|
---|
6956 | MediumLockList SourceMediumLockList;
|
---|
6957 | PVDISK pHdd;
|
---|
6958 | hrc = i_openForIO(false /*fWritable*/, pKeyStore, &pHdd, &SourceMediumLockList, &CryptoSettingsRead);
|
---|
6959 | if (SUCCEEDED(hrc))
|
---|
6960 | {
|
---|
6961 | /*
|
---|
6962 | * Create a VFS file interface to the HDD and attach a progress wrapper
|
---|
6963 | * that monitors the progress reading of the raw image. The image will
|
---|
6964 | * be read twice if hVfsFssDst does sparse processing.
|
---|
6965 | */
|
---|
6966 | RTVFSFILE hVfsFileDisk = NIL_RTVFSFILE;
|
---|
6967 | int vrc = VDCreateVfsFileFromDisk(pHdd, 0 /*fFlags*/, &hVfsFileDisk);
|
---|
6968 | if (RT_SUCCESS(vrc))
|
---|
6969 | {
|
---|
6970 | RTVFSFILE hVfsFileProgress = NIL_RTVFSFILE;
|
---|
6971 | vrc = RTVfsCreateProgressForFile(hVfsFileDisk, aProgress->i_iprtProgressCallback, &*aProgress,
|
---|
6972 | RTVFSPROGRESS_F_CANCELABLE | RTVFSPROGRESS_F_FORWARD_SEEK_AS_READ,
|
---|
6973 | VDGetSize(pHdd, VD_LAST_IMAGE) * (fSparse ? 2 : 1) /*cbExpectedRead*/,
|
---|
6974 | 0 /*cbExpectedWritten*/, &hVfsFileProgress);
|
---|
6975 | RTVfsFileRelease(hVfsFileDisk);
|
---|
6976 | if (RT_SUCCESS(vrc))
|
---|
6977 | {
|
---|
6978 | RTVFSOBJ hVfsObj = RTVfsObjFromFile(hVfsFileProgress);
|
---|
6979 | RTVfsFileRelease(hVfsFileProgress);
|
---|
6980 |
|
---|
6981 | vrc = RTVfsFsStrmAdd(hVfsFssDst, aFilename, hVfsObj, 0 /*fFlags*/);
|
---|
6982 | RTVfsObjRelease(hVfsObj);
|
---|
6983 | if (RT_FAILURE(vrc))
|
---|
6984 | hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Failed to add '%s' to output (%Rrc)"), aFilename, vrc);
|
---|
6985 | }
|
---|
6986 | else
|
---|
6987 | hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
6988 | tr("RTVfsCreateProgressForFile failed when processing '%s' (%Rrc)"), aFilename, vrc);
|
---|
6989 | }
|
---|
6990 | else
|
---|
6991 | hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("VDCreateVfsFileFromDisk failed for '%s' (%Rrc)"), aFilename, vrc);
|
---|
6992 | VDDestroy(pHdd);
|
---|
6993 | }
|
---|
6994 | }
|
---|
6995 | return hrc;
|
---|
6996 | }
|
---|
6997 |
|
---|
6998 | /**
|
---|
6999 | * Used by IAppliance to export disk images.
|
---|
7000 | *
|
---|
7001 | * @param aFilename Filename to create (UTF8).
|
---|
7002 | * @param aFormat Medium format for creating @a aFilename.
|
---|
7003 | * @param aVariant Which exact image format variant to use for the
|
---|
7004 | * destination image.
|
---|
7005 | * @param pKeyStore The optional key store for decrypting the data for
|
---|
7006 | * encrypted media during the export.
|
---|
7007 | * @param hVfsIosDst The destination I/O stream object.
|
---|
7008 | * @param aProgress Progress object to use.
|
---|
7009 | * @return
|
---|
7010 | *
|
---|
7011 | * @note The source format is defined by the Medium instance.
|
---|
7012 | */
|
---|
7013 | HRESULT Medium::i_exportFile(const char *aFilename,
|
---|
7014 | const ComObjPtr<MediumFormat> &aFormat,
|
---|
7015 | MediumVariant_T aVariant,
|
---|
7016 | SecretKeyStore *pKeyStore,
|
---|
7017 | RTVFSIOSTREAM hVfsIosDst,
|
---|
7018 | const ComObjPtr<Progress> &aProgress)
|
---|
7019 | {
|
---|
7020 | AssertPtrReturn(aFilename, E_INVALIDARG);
|
---|
7021 | AssertReturn(aFormat.isNotNull(), E_INVALIDARG);
|
---|
7022 | AssertReturn(aProgress.isNotNull(), E_INVALIDARG);
|
---|
7023 |
|
---|
7024 | AutoCaller autoCaller(this);
|
---|
7025 | HRESULT hrc = autoCaller.hrc();
|
---|
7026 | if (SUCCEEDED(hrc))
|
---|
7027 | {
|
---|
7028 | /*
|
---|
7029 | * Setup VD interfaces.
|
---|
7030 | */
|
---|
7031 | PVDINTERFACE pVDImageIfaces = m->vdImageIfaces;
|
---|
7032 | PVDINTERFACEIO pVfsIoIf;
|
---|
7033 | int vrc = VDIfCreateFromVfsStream(hVfsIosDst, RTFILE_O_WRITE, &pVfsIoIf);
|
---|
7034 | if (RT_SUCCESS(vrc))
|
---|
7035 | {
|
---|
7036 | vrc = VDInterfaceAdd(&pVfsIoIf->Core, "Medium::ExportTaskVfsIos", VDINTERFACETYPE_IO,
|
---|
7037 | pVfsIoIf, sizeof(VDINTERFACEIO), &pVDImageIfaces);
|
---|
7038 | if (RT_SUCCESS(vrc))
|
---|
7039 | {
|
---|
7040 | /*
|
---|
7041 | * Get a readonly hdd for this medium (source).
|
---|
7042 | */
|
---|
7043 | MediumCryptoFilterSettings CryptoSettingsRead;
|
---|
7044 | MediumLockList SourceMediumLockList;
|
---|
7045 | PVDISK pSrcHdd;
|
---|
7046 | hrc = i_openForIO(false /*fWritable*/, pKeyStore, &pSrcHdd, &SourceMediumLockList, &CryptoSettingsRead);
|
---|
7047 | if (SUCCEEDED(hrc))
|
---|
7048 | {
|
---|
7049 | /*
|
---|
7050 | * Create the target medium.
|
---|
7051 | */
|
---|
7052 | Utf8Str strDstFormat(aFormat->i_getId());
|
---|
7053 |
|
---|
7054 | /* ensure the target directory exists */
|
---|
7055 | uint64_t fDstCapabilities = aFormat->i_getCapabilities();
|
---|
7056 | if (fDstCapabilities & MediumFormatCapabilities_File)
|
---|
7057 | {
|
---|
7058 | Utf8Str strDstLocation(aFilename);
|
---|
7059 | hrc = VirtualBox::i_ensureFilePathExists(strDstLocation.c_str(),
|
---|
7060 | !(aVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
7061 | }
|
---|
7062 | if (SUCCEEDED(hrc))
|
---|
7063 | {
|
---|
7064 | PVDISK pDstHdd;
|
---|
7065 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDstHdd);
|
---|
7066 | if (RT_SUCCESS(vrc))
|
---|
7067 | {
|
---|
7068 | /*
|
---|
7069 | * Create an interface for getting progress callbacks.
|
---|
7070 | */
|
---|
7071 | VDINTERFACEPROGRESS ProgressIf = VDINTERFACEPROGRESS_INITALIZER(aProgress->i_vdProgressCallback);
|
---|
7072 | PVDINTERFACE pProgress = NULL;
|
---|
7073 | vrc = VDInterfaceAdd(&ProgressIf.Core, "export-progress", VDINTERFACETYPE_PROGRESS,
|
---|
7074 | &*aProgress, sizeof(ProgressIf), &pProgress);
|
---|
7075 | AssertRC(vrc);
|
---|
7076 |
|
---|
7077 | /*
|
---|
7078 | * Do the exporting.
|
---|
7079 | */
|
---|
7080 | vrc = VDCopy(pSrcHdd,
|
---|
7081 | VD_LAST_IMAGE,
|
---|
7082 | pDstHdd,
|
---|
7083 | strDstFormat.c_str(),
|
---|
7084 | aFilename,
|
---|
7085 | false /* fMoveByRename */,
|
---|
7086 | 0 /* cbSize */,
|
---|
7087 | aVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted | MediumVariant_VmdkESX | MediumVariant_VmdkRawDisk),
|
---|
7088 | NULL /* pDstUuid */,
|
---|
7089 | VD_OPEN_FLAGS_NORMAL | VD_OPEN_FLAGS_SEQUENTIAL,
|
---|
7090 | pProgress,
|
---|
7091 | pVDImageIfaces,
|
---|
7092 | NULL);
|
---|
7093 | if (RT_SUCCESS(vrc))
|
---|
7094 | hrc = S_OK;
|
---|
7095 | else
|
---|
7096 | hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Could not create the exported medium '%s'%s"),
|
---|
7097 | aFilename, i_vdError(vrc).c_str());
|
---|
7098 | VDDestroy(pDstHdd);
|
---|
7099 | }
|
---|
7100 | else
|
---|
7101 | hrc = setErrorVrc(vrc);
|
---|
7102 | }
|
---|
7103 | }
|
---|
7104 | VDDestroy(pSrcHdd);
|
---|
7105 | }
|
---|
7106 | else
|
---|
7107 | hrc = setErrorVrc(vrc, "VDInterfaceAdd -> %Rrc", vrc);
|
---|
7108 | VDIfDestroyFromVfsStream(pVfsIoIf);
|
---|
7109 | }
|
---|
7110 | else
|
---|
7111 | hrc = setErrorVrc(vrc, "VDIfCreateFromVfsStream -> %Rrc", vrc);
|
---|
7112 | }
|
---|
7113 | return hrc;
|
---|
7114 | }
|
---|
7115 |
|
---|
7116 | /**
|
---|
7117 | * Used by IAppliance to import disk images.
|
---|
7118 | *
|
---|
7119 | * @param aFilename Filename to read (UTF8).
|
---|
7120 | * @param aFormat Medium format for reading @a aFilename.
|
---|
7121 | * @param aVariant Which exact image format variant to use
|
---|
7122 | * for the destination image.
|
---|
7123 | * @param aVfsIosSrc Handle to the source I/O stream.
|
---|
7124 | * @param aParent Parent medium. May be NULL.
|
---|
7125 | * @param aProgress Progress object to use.
|
---|
7126 | * @param aNotify Notify about mediums which metadatа are changed
|
---|
7127 | * during execution of the function.
|
---|
7128 | * @return
|
---|
7129 | * @note The destination format is defined by the Medium instance.
|
---|
7130 | *
|
---|
7131 | * @todo The only consumer of this method (Appliance::i_importOneDiskImage) is
|
---|
7132 | * already on a worker thread, so perhaps consider bypassing the thread
|
---|
7133 | * here and run in the task synchronously? VBoxSVC has enough threads as
|
---|
7134 | * it is...
|
---|
7135 | */
|
---|
7136 | HRESULT Medium::i_importFile(const char *aFilename,
|
---|
7137 | const ComObjPtr<MediumFormat> &aFormat,
|
---|
7138 | MediumVariant_T aVariant,
|
---|
7139 | RTVFSIOSTREAM aVfsIosSrc,
|
---|
7140 | const ComObjPtr<Medium> &aParent,
|
---|
7141 | const ComObjPtr<Progress> &aProgress,
|
---|
7142 | bool aNotify)
|
---|
7143 | {
|
---|
7144 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
7145 | * to lock order violations, it probably causes lock order issues related
|
---|
7146 | * to the AutoCaller usage. */
|
---|
7147 | AssertPtrReturn(aFilename, E_INVALIDARG);
|
---|
7148 | AssertReturn(!aFormat.isNull(), E_INVALIDARG);
|
---|
7149 | AssertReturn(!aProgress.isNull(), E_INVALIDARG);
|
---|
7150 |
|
---|
7151 | AutoCaller autoCaller(this);
|
---|
7152 | if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
|
---|
7153 |
|
---|
7154 | HRESULT rc = S_OK;
|
---|
7155 | Medium::Task *pTask = NULL;
|
---|
7156 |
|
---|
7157 | try
|
---|
7158 | {
|
---|
7159 | // locking: we need the tree lock first because we access parent pointers
|
---|
7160 | // and we need to write-lock the media involved
|
---|
7161 | uint32_t cHandles = 2;
|
---|
7162 | LockHandle* pHandles[3] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
7163 | this->lockHandle() };
|
---|
7164 | /* Only add parent to the lock if it is not null */
|
---|
7165 | if (!aParent.isNull())
|
---|
7166 | pHandles[cHandles++] = aParent->lockHandle();
|
---|
7167 | AutoWriteLock alock(cHandles,
|
---|
7168 | pHandles
|
---|
7169 | COMMA_LOCKVAL_SRC_POS);
|
---|
7170 |
|
---|
7171 | if ( m->state != MediumState_NotCreated
|
---|
7172 | && m->state != MediumState_Created)
|
---|
7173 | throw i_setStateError();
|
---|
7174 |
|
---|
7175 | /* Build the target lock list. */
|
---|
7176 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
7177 | alock.release();
|
---|
7178 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
7179 | this /* pToLockWrite */,
|
---|
7180 | false /* fMediumLockWriteAll */,
|
---|
7181 | aParent,
|
---|
7182 | *pTargetMediumLockList);
|
---|
7183 | alock.acquire();
|
---|
7184 | if (FAILED(rc))
|
---|
7185 | {
|
---|
7186 | delete pTargetMediumLockList;
|
---|
7187 | throw rc;
|
---|
7188 | }
|
---|
7189 |
|
---|
7190 | alock.release();
|
---|
7191 | rc = pTargetMediumLockList->Lock();
|
---|
7192 | alock.acquire();
|
---|
7193 | if (FAILED(rc))
|
---|
7194 | {
|
---|
7195 | delete pTargetMediumLockList;
|
---|
7196 | throw setError(rc,
|
---|
7197 | tr("Failed to lock target media '%s'"),
|
---|
7198 | i_getLocationFull().c_str());
|
---|
7199 | }
|
---|
7200 |
|
---|
7201 | /* setup task object to carry out the operation asynchronously */
|
---|
7202 | pTask = new Medium::ImportTask(this, aProgress, aFilename, aFormat, aVariant,
|
---|
7203 | aVfsIosSrc, aParent, pTargetMediumLockList, false, aNotify);
|
---|
7204 | rc = pTask->hrc();
|
---|
7205 | AssertComRC(rc);
|
---|
7206 | if (FAILED(rc))
|
---|
7207 | throw rc;
|
---|
7208 |
|
---|
7209 | if (m->state == MediumState_NotCreated)
|
---|
7210 | m->state = MediumState_Creating;
|
---|
7211 | }
|
---|
7212 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7213 |
|
---|
7214 | if (SUCCEEDED(rc))
|
---|
7215 | {
|
---|
7216 | rc = pTask->createThread();
|
---|
7217 | pTask = NULL;
|
---|
7218 | }
|
---|
7219 | else if (pTask != NULL)
|
---|
7220 | delete pTask;
|
---|
7221 |
|
---|
7222 | return rc;
|
---|
7223 | }
|
---|
7224 |
|
---|
7225 | /**
|
---|
7226 | * Internal version of the public CloneTo API which allows to enable certain
|
---|
7227 | * optimizations to improve speed during VM cloning.
|
---|
7228 | *
|
---|
7229 | * @param aTarget Target medium
|
---|
7230 | * @param aVariant Which exact image format variant to use
|
---|
7231 | * for the destination image.
|
---|
7232 | * @param aParent Parent medium. May be NULL.
|
---|
7233 | * @param aProgress Progress object to use.
|
---|
7234 | * @param idxSrcImageSame The last image in the source chain which has the
|
---|
7235 | * same content as the given image in the destination
|
---|
7236 | * chain. Use UINT32_MAX to disable this optimization.
|
---|
7237 | * @param idxDstImageSame The last image in the destination chain which has the
|
---|
7238 | * same content as the given image in the source chain.
|
---|
7239 | * Use UINT32_MAX to disable this optimization.
|
---|
7240 | * @param aNotify Notify about mediums which metadatа are changed
|
---|
7241 | * during execution of the function.
|
---|
7242 | * @return
|
---|
7243 | */
|
---|
7244 | HRESULT Medium::i_cloneToEx(const ComObjPtr<Medium> &aTarget, MediumVariant_T aVariant,
|
---|
7245 | const ComObjPtr<Medium> &aParent, IProgress **aProgress,
|
---|
7246 | uint32_t idxSrcImageSame, uint32_t idxDstImageSame, bool aNotify)
|
---|
7247 | {
|
---|
7248 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
7249 | * to lock order violations, it probably causes lock order issues related
|
---|
7250 | * to the AutoCaller usage. */
|
---|
7251 | CheckComArgNotNull(aTarget);
|
---|
7252 | CheckComArgOutPointerValid(aProgress);
|
---|
7253 | ComAssertRet(aTarget != this, E_INVALIDARG);
|
---|
7254 |
|
---|
7255 | AutoCaller autoCaller(this);
|
---|
7256 | if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
|
---|
7257 |
|
---|
7258 | HRESULT rc = S_OK;
|
---|
7259 | ComObjPtr<Progress> pProgress;
|
---|
7260 | Medium::Task *pTask = NULL;
|
---|
7261 |
|
---|
7262 | try
|
---|
7263 | {
|
---|
7264 | // locking: we need the tree lock first because we access parent pointers
|
---|
7265 | // and we need to write-lock the media involved
|
---|
7266 | uint32_t cHandles = 3;
|
---|
7267 | LockHandle* pHandles[4] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
7268 | this->lockHandle(),
|
---|
7269 | aTarget->lockHandle() };
|
---|
7270 | /* Only add parent to the lock if it is not null */
|
---|
7271 | if (!aParent.isNull())
|
---|
7272 | pHandles[cHandles++] = aParent->lockHandle();
|
---|
7273 | AutoWriteLock alock(cHandles,
|
---|
7274 | pHandles
|
---|
7275 | COMMA_LOCKVAL_SRC_POS);
|
---|
7276 |
|
---|
7277 | if ( aTarget->m->state != MediumState_NotCreated
|
---|
7278 | && aTarget->m->state != MediumState_Created)
|
---|
7279 | throw aTarget->i_setStateError();
|
---|
7280 |
|
---|
7281 | /* Build the source lock list. */
|
---|
7282 | MediumLockList *pSourceMediumLockList(new MediumLockList());
|
---|
7283 | alock.release();
|
---|
7284 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
7285 | NULL /* pToLockWrite */,
|
---|
7286 | false /* fMediumLockWriteAll */,
|
---|
7287 | NULL,
|
---|
7288 | *pSourceMediumLockList);
|
---|
7289 | alock.acquire();
|
---|
7290 | if (FAILED(rc))
|
---|
7291 | {
|
---|
7292 | delete pSourceMediumLockList;
|
---|
7293 | throw rc;
|
---|
7294 | }
|
---|
7295 |
|
---|
7296 | /* Build the target lock list (including the to-be parent chain). */
|
---|
7297 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
7298 | alock.release();
|
---|
7299 | rc = aTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
7300 | aTarget /* pToLockWrite */,
|
---|
7301 | false /* fMediumLockWriteAll */,
|
---|
7302 | aParent,
|
---|
7303 | *pTargetMediumLockList);
|
---|
7304 | alock.acquire();
|
---|
7305 | if (FAILED(rc))
|
---|
7306 | {
|
---|
7307 | delete pSourceMediumLockList;
|
---|
7308 | delete pTargetMediumLockList;
|
---|
7309 | throw rc;
|
---|
7310 | }
|
---|
7311 |
|
---|
7312 | alock.release();
|
---|
7313 | rc = pSourceMediumLockList->Lock();
|
---|
7314 | alock.acquire();
|
---|
7315 | if (FAILED(rc))
|
---|
7316 | {
|
---|
7317 | delete pSourceMediumLockList;
|
---|
7318 | delete pTargetMediumLockList;
|
---|
7319 | throw setError(rc,
|
---|
7320 | tr("Failed to lock source media '%s'"),
|
---|
7321 | i_getLocationFull().c_str());
|
---|
7322 | }
|
---|
7323 | alock.release();
|
---|
7324 | rc = pTargetMediumLockList->Lock();
|
---|
7325 | alock.acquire();
|
---|
7326 | if (FAILED(rc))
|
---|
7327 | {
|
---|
7328 | delete pSourceMediumLockList;
|
---|
7329 | delete pTargetMediumLockList;
|
---|
7330 | throw setError(rc,
|
---|
7331 | tr("Failed to lock target media '%s'"),
|
---|
7332 | aTarget->i_getLocationFull().c_str());
|
---|
7333 | }
|
---|
7334 |
|
---|
7335 | pProgress.createObject();
|
---|
7336 | rc = pProgress->init(m->pVirtualBox,
|
---|
7337 | static_cast <IMedium *>(this),
|
---|
7338 | BstrFmt(tr("Creating clone medium '%s'"), aTarget->m->strLocationFull.c_str()).raw(),
|
---|
7339 | TRUE /* aCancelable */);
|
---|
7340 | if (FAILED(rc))
|
---|
7341 | {
|
---|
7342 | delete pSourceMediumLockList;
|
---|
7343 | delete pTargetMediumLockList;
|
---|
7344 | throw rc;
|
---|
7345 | }
|
---|
7346 |
|
---|
7347 | /* setup task object to carry out the operation asynchronously */
|
---|
7348 | pTask = new Medium::CloneTask(this, pProgress, aTarget, aVariant,
|
---|
7349 | aParent, idxSrcImageSame,
|
---|
7350 | idxDstImageSame, pSourceMediumLockList,
|
---|
7351 | pTargetMediumLockList, false, false, aNotify);
|
---|
7352 | rc = pTask->hrc();
|
---|
7353 | AssertComRC(rc);
|
---|
7354 | if (FAILED(rc))
|
---|
7355 | throw rc;
|
---|
7356 |
|
---|
7357 | if (aTarget->m->state == MediumState_NotCreated)
|
---|
7358 | aTarget->m->state = MediumState_Creating;
|
---|
7359 | }
|
---|
7360 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7361 |
|
---|
7362 | if (SUCCEEDED(rc))
|
---|
7363 | {
|
---|
7364 | rc = pTask->createThread();
|
---|
7365 | pTask = NULL;
|
---|
7366 | if (SUCCEEDED(rc))
|
---|
7367 | pProgress.queryInterfaceTo(aProgress);
|
---|
7368 | }
|
---|
7369 | else if (pTask != NULL)
|
---|
7370 | delete pTask;
|
---|
7371 |
|
---|
7372 | return rc;
|
---|
7373 | }
|
---|
7374 |
|
---|
7375 | /**
|
---|
7376 | * Returns the key identifier for this medium if encryption is configured.
|
---|
7377 | *
|
---|
7378 | * @returns Key identifier or empty string if no encryption is configured.
|
---|
7379 | */
|
---|
7380 | const Utf8Str& Medium::i_getKeyId()
|
---|
7381 | {
|
---|
7382 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
7383 |
|
---|
7384 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7385 |
|
---|
7386 | settings::StringsMap::const_iterator it = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
7387 | if (it == pBase->m->mapProperties.end())
|
---|
7388 | return Utf8Str::Empty;
|
---|
7389 |
|
---|
7390 | return it->second;
|
---|
7391 | }
|
---|
7392 |
|
---|
7393 |
|
---|
7394 | /**
|
---|
7395 | * Returns all filter related properties.
|
---|
7396 | *
|
---|
7397 | * @returns COM status code.
|
---|
7398 | * @param aReturnNames Where to store the properties names on success.
|
---|
7399 | * @param aReturnValues Where to store the properties values on success.
|
---|
7400 | */
|
---|
7401 | HRESULT Medium::i_getFilterProperties(std::vector<com::Utf8Str> &aReturnNames,
|
---|
7402 | std::vector<com::Utf8Str> &aReturnValues)
|
---|
7403 | {
|
---|
7404 | std::vector<com::Utf8Str> aPropNames;
|
---|
7405 | std::vector<com::Utf8Str> aPropValues;
|
---|
7406 | HRESULT hrc = getProperties(Utf8Str(""), aPropNames, aPropValues);
|
---|
7407 |
|
---|
7408 | if (SUCCEEDED(hrc))
|
---|
7409 | {
|
---|
7410 | unsigned cReturnSize = 0;
|
---|
7411 | aReturnNames.resize(0);
|
---|
7412 | aReturnValues.resize(0);
|
---|
7413 | for (unsigned idx = 0; idx < aPropNames.size(); idx++)
|
---|
7414 | {
|
---|
7415 | if (i_isPropertyForFilter(aPropNames[idx]))
|
---|
7416 | {
|
---|
7417 | aReturnNames.resize(cReturnSize + 1);
|
---|
7418 | aReturnValues.resize(cReturnSize + 1);
|
---|
7419 | aReturnNames[cReturnSize] = aPropNames[idx];
|
---|
7420 | aReturnValues[cReturnSize] = aPropValues[idx];
|
---|
7421 | cReturnSize++;
|
---|
7422 | }
|
---|
7423 | }
|
---|
7424 | }
|
---|
7425 |
|
---|
7426 | return hrc;
|
---|
7427 | }
|
---|
7428 |
|
---|
7429 | /**
|
---|
7430 | * Preparation to move this medium to a new location
|
---|
7431 | *
|
---|
7432 | * @param aLocation Location of the storage unit. If the location is a FS-path,
|
---|
7433 | * then it can be relative to the VirtualBox home directory.
|
---|
7434 | *
|
---|
7435 | * @note Must be called from under this object's write lock.
|
---|
7436 | */
|
---|
7437 | HRESULT Medium::i_preparationForMoving(const Utf8Str &aLocation)
|
---|
7438 | {
|
---|
7439 | HRESULT rc = E_FAIL;
|
---|
7440 |
|
---|
7441 | if (i_getLocationFull() != aLocation)
|
---|
7442 | {
|
---|
7443 | m->strNewLocationFull = aLocation;
|
---|
7444 | m->fMoveThisMedium = true;
|
---|
7445 | rc = S_OK;
|
---|
7446 | }
|
---|
7447 |
|
---|
7448 | return rc;
|
---|
7449 | }
|
---|
7450 |
|
---|
7451 | /**
|
---|
7452 | * Checking whether current operation "moving" or not
|
---|
7453 | */
|
---|
7454 | bool Medium::i_isMoveOperation(const ComObjPtr<Medium> &aTarget) const
|
---|
7455 | {
|
---|
7456 | RT_NOREF(aTarget);
|
---|
7457 | return m->fMoveThisMedium;
|
---|
7458 | }
|
---|
7459 |
|
---|
7460 | bool Medium::i_resetMoveOperationData()
|
---|
7461 | {
|
---|
7462 | m->strNewLocationFull.setNull();
|
---|
7463 | m->fMoveThisMedium = false;
|
---|
7464 | return true;
|
---|
7465 | }
|
---|
7466 |
|
---|
7467 | Utf8Str Medium::i_getNewLocationForMoving() const
|
---|
7468 | {
|
---|
7469 | if (m->fMoveThisMedium == true)
|
---|
7470 | return m->strNewLocationFull;
|
---|
7471 | else
|
---|
7472 | return Utf8Str();
|
---|
7473 | }
|
---|
7474 | ////////////////////////////////////////////////////////////////////////////////
|
---|
7475 | //
|
---|
7476 | // Private methods
|
---|
7477 | //
|
---|
7478 | ////////////////////////////////////////////////////////////////////////////////
|
---|
7479 |
|
---|
7480 | /**
|
---|
7481 | * Queries information from the medium.
|
---|
7482 | *
|
---|
7483 | * As a result of this call, the accessibility state and data members such as
|
---|
7484 | * size and description will be updated with the current information.
|
---|
7485 | *
|
---|
7486 | * @note This method may block during a system I/O call that checks storage
|
---|
7487 | * accessibility.
|
---|
7488 | *
|
---|
7489 | * @note Caller MUST NOT hold the media tree or medium lock.
|
---|
7490 | *
|
---|
7491 | * @note Locks m->pParent for reading. Locks this object for writing.
|
---|
7492 | *
|
---|
7493 | * @param fSetImageId Whether to reset the UUID contained in the image file
|
---|
7494 | * to the UUID in the medium instance data (see SetIDs())
|
---|
7495 | * @param fSetParentId Whether to reset the parent UUID contained in the image
|
---|
7496 | * file to the parent UUID in the medium instance data (see
|
---|
7497 | * SetIDs())
|
---|
7498 | * @param autoCaller
|
---|
7499 | * @return
|
---|
7500 | */
|
---|
7501 | HRESULT Medium::i_queryInfo(bool fSetImageId, bool fSetParentId, AutoCaller &autoCaller)
|
---|
7502 | {
|
---|
7503 | Assert(!isWriteLockOnCurrentThread());
|
---|
7504 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7505 |
|
---|
7506 | if ( ( m->state != MediumState_Created
|
---|
7507 | && m->state != MediumState_Inaccessible
|
---|
7508 | && m->state != MediumState_LockedRead)
|
---|
7509 | || m->fClosing)
|
---|
7510 | return E_FAIL;
|
---|
7511 |
|
---|
7512 | HRESULT rc = S_OK;
|
---|
7513 |
|
---|
7514 | int vrc = VINF_SUCCESS;
|
---|
7515 |
|
---|
7516 | /* check if a blocking i_queryInfo() call is in progress on some other thread,
|
---|
7517 | * and wait for it to finish if so instead of querying data ourselves */
|
---|
7518 | if (m->queryInfoRunning)
|
---|
7519 | {
|
---|
7520 | Assert( m->state == MediumState_LockedRead
|
---|
7521 | || m->state == MediumState_LockedWrite);
|
---|
7522 |
|
---|
7523 | while (m->queryInfoRunning)
|
---|
7524 | {
|
---|
7525 | alock.release();
|
---|
7526 | /* must not hold the object lock now */
|
---|
7527 | Assert(!isWriteLockOnCurrentThread());
|
---|
7528 | {
|
---|
7529 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
7530 | }
|
---|
7531 | alock.acquire();
|
---|
7532 | }
|
---|
7533 |
|
---|
7534 | return S_OK;
|
---|
7535 | }
|
---|
7536 |
|
---|
7537 | bool success = false;
|
---|
7538 | Utf8Str lastAccessError;
|
---|
7539 |
|
---|
7540 | /* are we dealing with a new medium constructed using the existing
|
---|
7541 | * location? */
|
---|
7542 | bool isImport = m->id.isZero();
|
---|
7543 | unsigned uOpenFlags = VD_OPEN_FLAGS_INFO;
|
---|
7544 |
|
---|
7545 | /* Note that we don't use VD_OPEN_FLAGS_READONLY when opening new
|
---|
7546 | * media because that would prevent necessary modifications
|
---|
7547 | * when opening media of some third-party formats for the first
|
---|
7548 | * time in VirtualBox (such as VMDK for which VDOpen() needs to
|
---|
7549 | * generate an UUID if it is missing) */
|
---|
7550 | if ( m->hddOpenMode == OpenReadOnly
|
---|
7551 | || m->type == MediumType_Readonly
|
---|
7552 | || (!isImport && !fSetImageId && !fSetParentId)
|
---|
7553 | )
|
---|
7554 | uOpenFlags |= VD_OPEN_FLAGS_READONLY;
|
---|
7555 |
|
---|
7556 | /* Open shareable medium with the appropriate flags */
|
---|
7557 | if (m->type == MediumType_Shareable)
|
---|
7558 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
7559 |
|
---|
7560 | /* Lock the medium, which makes the behavior much more consistent, must be
|
---|
7561 | * done before dropping the object lock and setting queryInfoRunning. */
|
---|
7562 | ComPtr<IToken> pToken;
|
---|
7563 | if (uOpenFlags & (VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SHAREABLE))
|
---|
7564 | rc = LockRead(pToken.asOutParam());
|
---|
7565 | else
|
---|
7566 | rc = LockWrite(pToken.asOutParam());
|
---|
7567 | if (FAILED(rc)) return rc;
|
---|
7568 |
|
---|
7569 | /* Copies of the input state fields which are not read-only,
|
---|
7570 | * as we're dropping the lock. CAUTION: be extremely careful what
|
---|
7571 | * you do with the contents of this medium object, as you will
|
---|
7572 | * create races if there are concurrent changes. */
|
---|
7573 | Utf8Str format(m->strFormat);
|
---|
7574 | Utf8Str location(m->strLocationFull);
|
---|
7575 | ComObjPtr<MediumFormat> formatObj = m->formatObj;
|
---|
7576 |
|
---|
7577 | /* "Output" values which can't be set because the lock isn't held
|
---|
7578 | * at the time the values are determined. */
|
---|
7579 | Guid mediumId = m->id;
|
---|
7580 | uint64_t mediumSize = 0;
|
---|
7581 | uint64_t mediumLogicalSize = 0;
|
---|
7582 |
|
---|
7583 | /* Flag whether a base image has a non-zero parent UUID and thus
|
---|
7584 | * need repairing after it was closed again. */
|
---|
7585 | bool fRepairImageZeroParentUuid = false;
|
---|
7586 |
|
---|
7587 | ComObjPtr<VirtualBox> pVirtualBox = m->pVirtualBox;
|
---|
7588 |
|
---|
7589 | /* must be set before leaving the object lock the first time */
|
---|
7590 | m->queryInfoRunning = true;
|
---|
7591 |
|
---|
7592 | /* must leave object lock now, because a lock from a higher lock class
|
---|
7593 | * is needed and also a lengthy operation is coming */
|
---|
7594 | alock.release();
|
---|
7595 | autoCaller.release();
|
---|
7596 |
|
---|
7597 | /* Note that taking the queryInfoSem after leaving the object lock above
|
---|
7598 | * can lead to short spinning of the loops waiting for i_queryInfo() to
|
---|
7599 | * complete. This is unavoidable since the other order causes a lock order
|
---|
7600 | * violation: here it would be requesting the object lock (at the beginning
|
---|
7601 | * of the method), then queryInfoSem, and below the other way round. */
|
---|
7602 | AutoWriteLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
7603 |
|
---|
7604 | /* take the opportunity to have a media tree lock, released initially */
|
---|
7605 | Assert(!isWriteLockOnCurrentThread());
|
---|
7606 | Assert(!pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
7607 | AutoWriteLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
7608 | treeLock.release();
|
---|
7609 |
|
---|
7610 | /* re-take the caller, but not the object lock, to keep uninit away */
|
---|
7611 | autoCaller.add();
|
---|
7612 | if (FAILED(autoCaller.hrc()))
|
---|
7613 | {
|
---|
7614 | m->queryInfoRunning = false;
|
---|
7615 | return autoCaller.hrc();
|
---|
7616 | }
|
---|
7617 |
|
---|
7618 | try
|
---|
7619 | {
|
---|
7620 | /* skip accessibility checks for host drives */
|
---|
7621 | if (m->hostDrive)
|
---|
7622 | {
|
---|
7623 | success = true;
|
---|
7624 | throw S_OK;
|
---|
7625 | }
|
---|
7626 |
|
---|
7627 | PVDISK hdd;
|
---|
7628 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
7629 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7630 |
|
---|
7631 | try
|
---|
7632 | {
|
---|
7633 | /** @todo This kind of opening of media is assuming that diff
|
---|
7634 | * media can be opened as base media. Should be documented that
|
---|
7635 | * it must work for all medium format backends. */
|
---|
7636 | vrc = VDOpen(hdd,
|
---|
7637 | format.c_str(),
|
---|
7638 | location.c_str(),
|
---|
7639 | uOpenFlags | m->uOpenFlagsDef,
|
---|
7640 | m->vdImageIfaces);
|
---|
7641 | if (RT_FAILURE(vrc))
|
---|
7642 | {
|
---|
7643 | lastAccessError = Utf8StrFmt(tr("Could not open the medium '%s'%s"),
|
---|
7644 | location.c_str(), i_vdError(vrc).c_str());
|
---|
7645 | throw S_OK;
|
---|
7646 | }
|
---|
7647 |
|
---|
7648 | if (formatObj->i_getCapabilities() & MediumFormatCapabilities_Uuid)
|
---|
7649 | {
|
---|
7650 | /* Modify the UUIDs if necessary. The associated fields are
|
---|
7651 | * not modified by other code, so no need to copy. */
|
---|
7652 | if (fSetImageId)
|
---|
7653 | {
|
---|
7654 | alock.acquire();
|
---|
7655 | vrc = VDSetUuid(hdd, 0, m->uuidImage.raw());
|
---|
7656 | alock.release();
|
---|
7657 | if (RT_FAILURE(vrc))
|
---|
7658 | {
|
---|
7659 | lastAccessError = Utf8StrFmt(tr("Could not update the UUID of medium '%s'%s"),
|
---|
7660 | location.c_str(), i_vdError(vrc).c_str());
|
---|
7661 | throw S_OK;
|
---|
7662 | }
|
---|
7663 | mediumId = m->uuidImage;
|
---|
7664 | }
|
---|
7665 | if (fSetParentId)
|
---|
7666 | {
|
---|
7667 | alock.acquire();
|
---|
7668 | vrc = VDSetParentUuid(hdd, 0, m->uuidParentImage.raw());
|
---|
7669 | alock.release();
|
---|
7670 | if (RT_FAILURE(vrc))
|
---|
7671 | {
|
---|
7672 | lastAccessError = Utf8StrFmt(tr("Could not update the parent UUID of medium '%s'%s"),
|
---|
7673 | location.c_str(), i_vdError(vrc).c_str());
|
---|
7674 | throw S_OK;
|
---|
7675 | }
|
---|
7676 | }
|
---|
7677 | /* zap the information, these are no long-term members */
|
---|
7678 | alock.acquire();
|
---|
7679 | unconst(m->uuidImage).clear();
|
---|
7680 | unconst(m->uuidParentImage).clear();
|
---|
7681 | alock.release();
|
---|
7682 |
|
---|
7683 | /* check the UUID */
|
---|
7684 | RTUUID uuid;
|
---|
7685 | vrc = VDGetUuid(hdd, 0, &uuid);
|
---|
7686 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7687 |
|
---|
7688 | if (isImport)
|
---|
7689 | {
|
---|
7690 | mediumId = uuid;
|
---|
7691 |
|
---|
7692 | if (mediumId.isZero() && (m->hddOpenMode == OpenReadOnly))
|
---|
7693 | // only when importing a VDMK that has no UUID, create one in memory
|
---|
7694 | mediumId.create();
|
---|
7695 | }
|
---|
7696 | else
|
---|
7697 | {
|
---|
7698 | Assert(!mediumId.isZero());
|
---|
7699 |
|
---|
7700 | if (mediumId != uuid)
|
---|
7701 | {
|
---|
7702 | /** @todo r=klaus this always refers to VirtualBox.xml as the medium registry, even for new VMs */
|
---|
7703 | lastAccessError = Utf8StrFmt(
|
---|
7704 | tr("UUID {%RTuuid} of the medium '%s' does not match the value {%RTuuid} stored in the media registry ('%s')"),
|
---|
7705 | &uuid,
|
---|
7706 | location.c_str(),
|
---|
7707 | mediumId.raw(),
|
---|
7708 | pVirtualBox->i_settingsFilePath().c_str());
|
---|
7709 | throw S_OK;
|
---|
7710 | }
|
---|
7711 | }
|
---|
7712 | }
|
---|
7713 | else
|
---|
7714 | {
|
---|
7715 | /* the backend does not support storing UUIDs within the
|
---|
7716 | * underlying storage so use what we store in XML */
|
---|
7717 |
|
---|
7718 | if (fSetImageId)
|
---|
7719 | {
|
---|
7720 | /* set the UUID if an API client wants to change it */
|
---|
7721 | alock.acquire();
|
---|
7722 | mediumId = m->uuidImage;
|
---|
7723 | alock.release();
|
---|
7724 | }
|
---|
7725 | else if (isImport)
|
---|
7726 | {
|
---|
7727 | /* generate an UUID for an imported UUID-less medium */
|
---|
7728 | mediumId.create();
|
---|
7729 | }
|
---|
7730 | }
|
---|
7731 |
|
---|
7732 | /* set the image uuid before the below parent uuid handling code
|
---|
7733 | * might place it somewhere in the media tree, so that the medium
|
---|
7734 | * UUID is valid at this point */
|
---|
7735 | alock.acquire();
|
---|
7736 | if (isImport || fSetImageId)
|
---|
7737 | unconst(m->id) = mediumId;
|
---|
7738 | alock.release();
|
---|
7739 |
|
---|
7740 | /* get the medium variant */
|
---|
7741 | unsigned uImageFlags;
|
---|
7742 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
7743 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7744 | alock.acquire();
|
---|
7745 | m->variant = (MediumVariant_T)uImageFlags;
|
---|
7746 | alock.release();
|
---|
7747 |
|
---|
7748 | /* check/get the parent uuid and update corresponding state */
|
---|
7749 | if (uImageFlags & VD_IMAGE_FLAGS_DIFF)
|
---|
7750 | {
|
---|
7751 | RTUUID parentId;
|
---|
7752 | vrc = VDGetParentUuid(hdd, 0, &parentId);
|
---|
7753 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7754 |
|
---|
7755 | /* streamOptimized VMDK images are only accepted as base
|
---|
7756 | * images, as this allows automatic repair of OVF appliances.
|
---|
7757 | * Since such images don't support random writes they will not
|
---|
7758 | * be created for diff images. Only an overly smart user might
|
---|
7759 | * manually create this case. Too bad for him. */
|
---|
7760 | if ( (isImport || fSetParentId)
|
---|
7761 | && !(uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
|
---|
7762 | {
|
---|
7763 | /* the parent must be known to us. Note that we freely
|
---|
7764 | * call locking methods of mVirtualBox and parent, as all
|
---|
7765 | * relevant locks must be already held. There may be no
|
---|
7766 | * concurrent access to the just opened medium on other
|
---|
7767 | * threads yet (and init() will fail if this method reports
|
---|
7768 | * MediumState_Inaccessible) */
|
---|
7769 |
|
---|
7770 | ComObjPtr<Medium> pParent;
|
---|
7771 | if (RTUuidIsNull(&parentId))
|
---|
7772 | rc = VBOX_E_OBJECT_NOT_FOUND;
|
---|
7773 | else
|
---|
7774 | rc = pVirtualBox->i_findHardDiskById(Guid(parentId), false /* aSetError */, &pParent);
|
---|
7775 | if (FAILED(rc))
|
---|
7776 | {
|
---|
7777 | if (fSetImageId && !fSetParentId)
|
---|
7778 | {
|
---|
7779 | /* If the image UUID gets changed for an existing
|
---|
7780 | * image then the parent UUID can be stale. In such
|
---|
7781 | * cases clear the parent information. The parent
|
---|
7782 | * information may/will be re-set later if the
|
---|
7783 | * API client wants to adjust a complete medium
|
---|
7784 | * hierarchy one by one. */
|
---|
7785 | rc = S_OK;
|
---|
7786 | alock.acquire();
|
---|
7787 | RTUuidClear(&parentId);
|
---|
7788 | vrc = VDSetParentUuid(hdd, 0, &parentId);
|
---|
7789 | alock.release();
|
---|
7790 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7791 | }
|
---|
7792 | else
|
---|
7793 | {
|
---|
7794 | lastAccessError = Utf8StrFmt(tr("Parent medium with UUID {%RTuuid} of the medium '%s' is not found in the media registry ('%s')"),
|
---|
7795 | &parentId, location.c_str(),
|
---|
7796 | pVirtualBox->i_settingsFilePath().c_str());
|
---|
7797 | throw S_OK;
|
---|
7798 | }
|
---|
7799 | }
|
---|
7800 |
|
---|
7801 | /* must drop the caller before taking the tree lock */
|
---|
7802 | autoCaller.release();
|
---|
7803 | /* we set m->pParent & children() */
|
---|
7804 | treeLock.acquire();
|
---|
7805 | autoCaller.add();
|
---|
7806 | if (FAILED(autoCaller.hrc()))
|
---|
7807 | throw autoCaller.hrc();
|
---|
7808 |
|
---|
7809 | if (m->pParent)
|
---|
7810 | i_deparent();
|
---|
7811 |
|
---|
7812 | if (!pParent.isNull())
|
---|
7813 | if (pParent->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
7814 | {
|
---|
7815 | AutoReadLock plock(pParent COMMA_LOCKVAL_SRC_POS);
|
---|
7816 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7817 | 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"),
|
---|
7818 | pParent->m->strLocationFull.c_str());
|
---|
7819 | }
|
---|
7820 | i_setParent(pParent);
|
---|
7821 |
|
---|
7822 | treeLock.release();
|
---|
7823 | }
|
---|
7824 | else
|
---|
7825 | {
|
---|
7826 | /* must drop the caller before taking the tree lock */
|
---|
7827 | autoCaller.release();
|
---|
7828 | /* we access m->pParent */
|
---|
7829 | treeLock.acquire();
|
---|
7830 | autoCaller.add();
|
---|
7831 | if (FAILED(autoCaller.hrc()))
|
---|
7832 | throw autoCaller.hrc();
|
---|
7833 |
|
---|
7834 | /* check that parent UUIDs match. Note that there's no need
|
---|
7835 | * for the parent's AutoCaller (our lifetime is bound to
|
---|
7836 | * it) */
|
---|
7837 |
|
---|
7838 | if (m->pParent.isNull())
|
---|
7839 | {
|
---|
7840 | /* Due to a bug in VDCopy() in VirtualBox 3.0.0-3.0.14
|
---|
7841 | * and 3.1.0-3.1.8 there are base images out there
|
---|
7842 | * which have a non-zero parent UUID. No point in
|
---|
7843 | * complaining about them, instead automatically
|
---|
7844 | * repair the problem. Later we can bring back the
|
---|
7845 | * error message, but we should wait until really
|
---|
7846 | * most users have repaired their images, either with
|
---|
7847 | * VBoxFixHdd or this way. */
|
---|
7848 | #if 1
|
---|
7849 | fRepairImageZeroParentUuid = true;
|
---|
7850 | #else /* 0 */
|
---|
7851 | lastAccessError = Utf8StrFmt(
|
---|
7852 | tr("Medium type of '%s' is differencing but it is not associated with any parent medium in the media registry ('%s')"),
|
---|
7853 | location.c_str(),
|
---|
7854 | pVirtualBox->settingsFilePath().c_str());
|
---|
7855 | treeLock.release();
|
---|
7856 | throw S_OK;
|
---|
7857 | #endif /* 0 */
|
---|
7858 | }
|
---|
7859 |
|
---|
7860 | {
|
---|
7861 | autoCaller.release();
|
---|
7862 | AutoReadLock parentLock(m->pParent COMMA_LOCKVAL_SRC_POS);
|
---|
7863 | autoCaller.add();
|
---|
7864 | if (FAILED(autoCaller.hrc()))
|
---|
7865 | throw autoCaller.hrc();
|
---|
7866 |
|
---|
7867 | if ( !fRepairImageZeroParentUuid
|
---|
7868 | && m->pParent->i_getState() != MediumState_Inaccessible
|
---|
7869 | && m->pParent->i_getId() != parentId)
|
---|
7870 | {
|
---|
7871 | /** @todo r=klaus this always refers to VirtualBox.xml as the medium registry, even for new VMs */
|
---|
7872 | lastAccessError = Utf8StrFmt(
|
---|
7873 | tr("Parent UUID {%RTuuid} of the medium '%s' does not match UUID {%RTuuid} of its parent medium stored in the media registry ('%s')"),
|
---|
7874 | &parentId, location.c_str(),
|
---|
7875 | m->pParent->i_getId().raw(),
|
---|
7876 | pVirtualBox->i_settingsFilePath().c_str());
|
---|
7877 | parentLock.release();
|
---|
7878 | treeLock.release();
|
---|
7879 | throw S_OK;
|
---|
7880 | }
|
---|
7881 | }
|
---|
7882 |
|
---|
7883 | /// @todo NEWMEDIA what to do if the parent is not
|
---|
7884 | /// accessible while the diff is? Probably nothing. The
|
---|
7885 | /// real code will detect the mismatch anyway.
|
---|
7886 |
|
---|
7887 | treeLock.release();
|
---|
7888 | }
|
---|
7889 | }
|
---|
7890 |
|
---|
7891 | mediumSize = VDGetFileSize(hdd, 0);
|
---|
7892 | mediumLogicalSize = VDGetSize(hdd, 0);
|
---|
7893 |
|
---|
7894 | success = true;
|
---|
7895 | }
|
---|
7896 | catch (HRESULT aRC)
|
---|
7897 | {
|
---|
7898 | rc = aRC;
|
---|
7899 | }
|
---|
7900 |
|
---|
7901 | vrc = VDDestroy(hdd);
|
---|
7902 | if (RT_FAILURE(vrc))
|
---|
7903 | {
|
---|
7904 | lastAccessError = Utf8StrFmt(tr("Could not update and close the medium '%s'%s"),
|
---|
7905 | location.c_str(), i_vdError(vrc).c_str());
|
---|
7906 | success = false;
|
---|
7907 | throw S_OK;
|
---|
7908 | }
|
---|
7909 | }
|
---|
7910 | catch (HRESULT aRC)
|
---|
7911 | {
|
---|
7912 | rc = aRC;
|
---|
7913 | }
|
---|
7914 |
|
---|
7915 | autoCaller.release();
|
---|
7916 | treeLock.acquire();
|
---|
7917 | autoCaller.add();
|
---|
7918 | if (FAILED(autoCaller.hrc()))
|
---|
7919 | {
|
---|
7920 | m->queryInfoRunning = false;
|
---|
7921 | return autoCaller.hrc();
|
---|
7922 | }
|
---|
7923 | alock.acquire();
|
---|
7924 |
|
---|
7925 | if (success)
|
---|
7926 | {
|
---|
7927 | m->size = mediumSize;
|
---|
7928 | m->logicalSize = mediumLogicalSize;
|
---|
7929 | m->strLastAccessError.setNull();
|
---|
7930 | }
|
---|
7931 | else
|
---|
7932 | {
|
---|
7933 | m->strLastAccessError = lastAccessError;
|
---|
7934 | Log1WarningFunc(("'%s' is not accessible (error='%s', rc=%Rhrc, vrc=%Rrc)\n",
|
---|
7935 | location.c_str(), m->strLastAccessError.c_str(), rc, vrc));
|
---|
7936 | }
|
---|
7937 |
|
---|
7938 | /* Set the proper state according to the result of the check */
|
---|
7939 | if (success)
|
---|
7940 | m->preLockState = MediumState_Created;
|
---|
7941 | else
|
---|
7942 | m->preLockState = MediumState_Inaccessible;
|
---|
7943 |
|
---|
7944 | /* unblock anyone waiting for the i_queryInfo results */
|
---|
7945 | qlock.release();
|
---|
7946 | m->queryInfoRunning = false;
|
---|
7947 |
|
---|
7948 | pToken->Abandon();
|
---|
7949 | pToken.setNull();
|
---|
7950 |
|
---|
7951 | if (FAILED(rc))
|
---|
7952 | return rc;
|
---|
7953 |
|
---|
7954 | /* If this is a base image which incorrectly has a parent UUID set,
|
---|
7955 | * repair the image now by zeroing the parent UUID. This is only done
|
---|
7956 | * when we have structural information from a config file, on import
|
---|
7957 | * this is not possible. If someone would accidentally call openMedium
|
---|
7958 | * with a diff image before the base is registered this would destroy
|
---|
7959 | * the diff. Not acceptable. */
|
---|
7960 | do
|
---|
7961 | {
|
---|
7962 | if (fRepairImageZeroParentUuid)
|
---|
7963 | {
|
---|
7964 | rc = LockWrite(pToken.asOutParam());
|
---|
7965 | if (FAILED(rc))
|
---|
7966 | break;
|
---|
7967 |
|
---|
7968 | alock.release();
|
---|
7969 |
|
---|
7970 | try
|
---|
7971 | {
|
---|
7972 | PVDISK hdd;
|
---|
7973 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
7974 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7975 |
|
---|
7976 | try
|
---|
7977 | {
|
---|
7978 | vrc = VDOpen(hdd,
|
---|
7979 | format.c_str(),
|
---|
7980 | location.c_str(),
|
---|
7981 | (uOpenFlags & ~VD_OPEN_FLAGS_READONLY) | m->uOpenFlagsDef,
|
---|
7982 | m->vdImageIfaces);
|
---|
7983 | if (RT_FAILURE(vrc))
|
---|
7984 | throw S_OK;
|
---|
7985 |
|
---|
7986 | RTUUID zeroParentUuid;
|
---|
7987 | RTUuidClear(&zeroParentUuid);
|
---|
7988 | vrc = VDSetParentUuid(hdd, 0, &zeroParentUuid);
|
---|
7989 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7990 | }
|
---|
7991 | catch (HRESULT aRC)
|
---|
7992 | {
|
---|
7993 | rc = aRC;
|
---|
7994 | }
|
---|
7995 |
|
---|
7996 | VDDestroy(hdd);
|
---|
7997 | }
|
---|
7998 | catch (HRESULT aRC)
|
---|
7999 | {
|
---|
8000 | rc = aRC;
|
---|
8001 | }
|
---|
8002 |
|
---|
8003 | pToken->Abandon();
|
---|
8004 | pToken.setNull();
|
---|
8005 | if (FAILED(rc))
|
---|
8006 | break;
|
---|
8007 | }
|
---|
8008 | } while(0);
|
---|
8009 |
|
---|
8010 | return rc;
|
---|
8011 | }
|
---|
8012 |
|
---|
8013 | /**
|
---|
8014 | * Performs extra checks if the medium can be closed and returns S_OK in
|
---|
8015 | * this case. Otherwise, returns a respective error message. Called by
|
---|
8016 | * Close() under the medium tree lock and the medium lock.
|
---|
8017 | *
|
---|
8018 | * @note Also reused by Medium::Reset().
|
---|
8019 | *
|
---|
8020 | * @note Caller must hold the media tree write lock!
|
---|
8021 | */
|
---|
8022 | HRESULT Medium::i_canClose()
|
---|
8023 | {
|
---|
8024 | Assert(m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
8025 |
|
---|
8026 | if (i_getChildren().size() != 0)
|
---|
8027 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
8028 | tr("Cannot close medium '%s' because it has %d child media", "", i_getChildren().size()),
|
---|
8029 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
8030 |
|
---|
8031 | return S_OK;
|
---|
8032 | }
|
---|
8033 |
|
---|
8034 | /**
|
---|
8035 | * Unregisters this medium with mVirtualBox. Called by close() under the medium tree lock.
|
---|
8036 | *
|
---|
8037 | * @note Caller must have locked the media tree lock for writing!
|
---|
8038 | */
|
---|
8039 | HRESULT Medium::i_unregisterWithVirtualBox()
|
---|
8040 | {
|
---|
8041 | /* Note that we need to de-associate ourselves from the parent to let
|
---|
8042 | * VirtualBox::i_unregisterMedium() properly save the registry */
|
---|
8043 |
|
---|
8044 | /* we modify m->pParent and access children */
|
---|
8045 | Assert(m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
8046 |
|
---|
8047 | Medium *pParentBackup = m->pParent;
|
---|
8048 | AssertReturn(i_getChildren().size() == 0, E_FAIL);
|
---|
8049 | if (m->pParent)
|
---|
8050 | i_deparent();
|
---|
8051 |
|
---|
8052 | HRESULT rc = m->pVirtualBox->i_unregisterMedium(this);
|
---|
8053 | if (FAILED(rc))
|
---|
8054 | {
|
---|
8055 | if (pParentBackup)
|
---|
8056 | {
|
---|
8057 | // re-associate with the parent as we are still relatives in the registry
|
---|
8058 | i_setParent(pParentBackup);
|
---|
8059 | }
|
---|
8060 | }
|
---|
8061 |
|
---|
8062 | return rc;
|
---|
8063 | }
|
---|
8064 |
|
---|
8065 | /**
|
---|
8066 | * Like SetProperty but do not trigger a settings store. Only for internal use!
|
---|
8067 | */
|
---|
8068 | HRESULT Medium::i_setPropertyDirect(const Utf8Str &aName, const Utf8Str &aValue)
|
---|
8069 | {
|
---|
8070 | AutoCaller autoCaller(this);
|
---|
8071 | if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
|
---|
8072 |
|
---|
8073 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8074 |
|
---|
8075 | switch (m->state)
|
---|
8076 | {
|
---|
8077 | case MediumState_Created:
|
---|
8078 | case MediumState_Inaccessible:
|
---|
8079 | break;
|
---|
8080 | default:
|
---|
8081 | return i_setStateError();
|
---|
8082 | }
|
---|
8083 |
|
---|
8084 | m->mapProperties[aName] = aValue;
|
---|
8085 |
|
---|
8086 | return S_OK;
|
---|
8087 | }
|
---|
8088 |
|
---|
8089 | /**
|
---|
8090 | * Sets the extended error info according to the current media state.
|
---|
8091 | *
|
---|
8092 | * @note Must be called from under this object's write or read lock.
|
---|
8093 | */
|
---|
8094 | HRESULT Medium::i_setStateError()
|
---|
8095 | {
|
---|
8096 | HRESULT rc = E_FAIL;
|
---|
8097 |
|
---|
8098 | switch (m->state)
|
---|
8099 | {
|
---|
8100 | case MediumState_NotCreated:
|
---|
8101 | {
|
---|
8102 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8103 | tr("Storage for the medium '%s' is not created"),
|
---|
8104 | m->strLocationFull.c_str());
|
---|
8105 | break;
|
---|
8106 | }
|
---|
8107 | case MediumState_Created:
|
---|
8108 | {
|
---|
8109 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8110 | tr("Storage for the medium '%s' is already created"),
|
---|
8111 | m->strLocationFull.c_str());
|
---|
8112 | break;
|
---|
8113 | }
|
---|
8114 | case MediumState_LockedRead:
|
---|
8115 | {
|
---|
8116 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8117 | tr("Medium '%s' is locked for reading by another task"),
|
---|
8118 | m->strLocationFull.c_str());
|
---|
8119 | break;
|
---|
8120 | }
|
---|
8121 | case MediumState_LockedWrite:
|
---|
8122 | {
|
---|
8123 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8124 | tr("Medium '%s' is locked for writing by another task"),
|
---|
8125 | m->strLocationFull.c_str());
|
---|
8126 | break;
|
---|
8127 | }
|
---|
8128 | case MediumState_Inaccessible:
|
---|
8129 | {
|
---|
8130 | /* be in sync with Console::powerUpThread() */
|
---|
8131 | if (!m->strLastAccessError.isEmpty())
|
---|
8132 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8133 | tr("Medium '%s' is not accessible. %s"),
|
---|
8134 | m->strLocationFull.c_str(), m->strLastAccessError.c_str());
|
---|
8135 | else
|
---|
8136 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8137 | tr("Medium '%s' is not accessible"),
|
---|
8138 | m->strLocationFull.c_str());
|
---|
8139 | break;
|
---|
8140 | }
|
---|
8141 | case MediumState_Creating:
|
---|
8142 | {
|
---|
8143 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8144 | tr("Storage for the medium '%s' is being created"),
|
---|
8145 | m->strLocationFull.c_str());
|
---|
8146 | break;
|
---|
8147 | }
|
---|
8148 | case MediumState_Deleting:
|
---|
8149 | {
|
---|
8150 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8151 | tr("Storage for the medium '%s' is being deleted"),
|
---|
8152 | m->strLocationFull.c_str());
|
---|
8153 | break;
|
---|
8154 | }
|
---|
8155 | default:
|
---|
8156 | {
|
---|
8157 | AssertFailed();
|
---|
8158 | break;
|
---|
8159 | }
|
---|
8160 | }
|
---|
8161 |
|
---|
8162 | return rc;
|
---|
8163 | }
|
---|
8164 |
|
---|
8165 | /**
|
---|
8166 | * Sets the value of m->strLocationFull. The given location must be a fully
|
---|
8167 | * qualified path; relative paths are not supported here.
|
---|
8168 | *
|
---|
8169 | * As a special exception, if the specified location is a file path that ends with '/'
|
---|
8170 | * then the file name part will be generated by this method automatically in the format
|
---|
8171 | * '{\<uuid\>}.\<ext\>' where \<uuid\> is a fresh UUID that this method will generate
|
---|
8172 | * and assign to this medium, and \<ext\> is the default extension for this
|
---|
8173 | * medium's storage format. Note that this procedure requires the media state to
|
---|
8174 | * be NotCreated and will return a failure otherwise.
|
---|
8175 | *
|
---|
8176 | * @param aLocation Location of the storage unit. If the location is a FS-path,
|
---|
8177 | * then it can be relative to the VirtualBox home directory.
|
---|
8178 | * @param aFormat Optional fallback format if it is an import and the format
|
---|
8179 | * cannot be determined.
|
---|
8180 | *
|
---|
8181 | * @note Must be called from under this object's write lock.
|
---|
8182 | */
|
---|
8183 | HRESULT Medium::i_setLocation(const Utf8Str &aLocation,
|
---|
8184 | const Utf8Str &aFormat /* = Utf8Str::Empty */)
|
---|
8185 | {
|
---|
8186 | AssertReturn(!aLocation.isEmpty(), E_FAIL);
|
---|
8187 |
|
---|
8188 | AutoCaller autoCaller(this);
|
---|
8189 | AssertComRCReturnRC(autoCaller.hrc());
|
---|
8190 |
|
---|
8191 | /* formatObj may be null only when initializing from an existing path and
|
---|
8192 | * no format is known yet */
|
---|
8193 | AssertReturn( (!m->strFormat.isEmpty() && !m->formatObj.isNull())
|
---|
8194 | || ( getObjectState().getState() == ObjectState::InInit
|
---|
8195 | && m->state != MediumState_NotCreated
|
---|
8196 | && m->id.isZero()
|
---|
8197 | && m->strFormat.isEmpty()
|
---|
8198 | && m->formatObj.isNull()),
|
---|
8199 | E_FAIL);
|
---|
8200 |
|
---|
8201 | /* are we dealing with a new medium constructed using the existing
|
---|
8202 | * location? */
|
---|
8203 | bool isImport = m->strFormat.isEmpty();
|
---|
8204 |
|
---|
8205 | if ( isImport
|
---|
8206 | || ( (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
8207 | && !m->hostDrive))
|
---|
8208 | {
|
---|
8209 | Guid id;
|
---|
8210 |
|
---|
8211 | Utf8Str locationFull(aLocation);
|
---|
8212 |
|
---|
8213 | if (m->state == MediumState_NotCreated)
|
---|
8214 | {
|
---|
8215 | /* must be a file (formatObj must be already known) */
|
---|
8216 | Assert(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File);
|
---|
8217 |
|
---|
8218 | if (RTPathFilename(aLocation.c_str()) == NULL)
|
---|
8219 | {
|
---|
8220 | /* no file name is given (either an empty string or ends with a
|
---|
8221 | * slash), generate a new UUID + file name if the state allows
|
---|
8222 | * this */
|
---|
8223 |
|
---|
8224 | ComAssertMsgRet(!m->formatObj->i_getFileExtensions().empty(),
|
---|
8225 | (tr("Must be at least one extension if it is MediumFormatCapabilities_File\n")),
|
---|
8226 | E_FAIL);
|
---|
8227 |
|
---|
8228 | Utf8Str strExt = m->formatObj->i_getFileExtensions().front();
|
---|
8229 | ComAssertMsgRet(!strExt.isEmpty(),
|
---|
8230 | (tr("Default extension must not be empty\n")),
|
---|
8231 | E_FAIL);
|
---|
8232 |
|
---|
8233 | id.create();
|
---|
8234 |
|
---|
8235 | locationFull = Utf8StrFmt("%s{%RTuuid}.%s",
|
---|
8236 | aLocation.c_str(), id.raw(), strExt.c_str());
|
---|
8237 | }
|
---|
8238 | }
|
---|
8239 |
|
---|
8240 | // we must always have full paths now (if it refers to a file)
|
---|
8241 | if ( ( m->formatObj.isNull()
|
---|
8242 | || m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
8243 | && !RTPathStartsWithRoot(locationFull.c_str()))
|
---|
8244 | return setError(VBOX_E_FILE_ERROR,
|
---|
8245 | tr("The given path '%s' is not fully qualified"),
|
---|
8246 | locationFull.c_str());
|
---|
8247 |
|
---|
8248 | /* detect the backend from the storage unit if importing */
|
---|
8249 | if (isImport)
|
---|
8250 | {
|
---|
8251 | VDTYPE const enmDesiredType = i_convertDeviceType();
|
---|
8252 | VDTYPE enmType = VDTYPE_INVALID;
|
---|
8253 | char *backendName = NULL;
|
---|
8254 |
|
---|
8255 | /* is it a file? */
|
---|
8256 | RTFILE hFile;
|
---|
8257 | int vrc = RTFileOpen(&hFile, locationFull.c_str(), RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
8258 | if (RT_SUCCESS(vrc))
|
---|
8259 | {
|
---|
8260 | RTFileClose(hFile);
|
---|
8261 | vrc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
|
---|
8262 | locationFull.c_str(), enmDesiredType, &backendName, &enmType);
|
---|
8263 | }
|
---|
8264 | else if ( vrc != VERR_FILE_NOT_FOUND
|
---|
8265 | && vrc != VERR_PATH_NOT_FOUND
|
---|
8266 | && vrc != VERR_ACCESS_DENIED
|
---|
8267 | && locationFull != aLocation)
|
---|
8268 | {
|
---|
8269 | /* assume it's not a file, restore the original location */
|
---|
8270 | locationFull = aLocation;
|
---|
8271 | vrc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
|
---|
8272 | locationFull.c_str(), enmDesiredType, &backendName, &enmType);
|
---|
8273 | }
|
---|
8274 |
|
---|
8275 | if (RT_FAILURE(vrc))
|
---|
8276 | {
|
---|
8277 | if (vrc == VERR_ACCESS_DENIED)
|
---|
8278 | return setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
8279 | tr("Permission problem accessing the file for the medium '%s' (%Rrc)"),
|
---|
8280 | locationFull.c_str(), vrc);
|
---|
8281 | if (vrc == VERR_FILE_NOT_FOUND || vrc == VERR_PATH_NOT_FOUND)
|
---|
8282 | return setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
8283 | tr("Could not find file for the medium '%s' (%Rrc)"),
|
---|
8284 | locationFull.c_str(), vrc);
|
---|
8285 | if (aFormat.isEmpty())
|
---|
8286 | return setErrorBoth(VBOX_E_IPRT_ERROR, vrc,
|
---|
8287 | tr("Could not get the storage format of the medium '%s' (%Rrc)"),
|
---|
8288 | locationFull.c_str(), vrc);
|
---|
8289 | HRESULT rc = i_setFormat(aFormat);
|
---|
8290 | /* setFormat() must not fail since we've just used the backend so
|
---|
8291 | * the format object must be there */
|
---|
8292 | AssertComRCReturnRC(rc);
|
---|
8293 | }
|
---|
8294 | else if ( enmType == VDTYPE_INVALID
|
---|
8295 | || m->devType != i_convertToDeviceType(enmType))
|
---|
8296 | {
|
---|
8297 | /*
|
---|
8298 | * The user tried to use a image as a device which is not supported
|
---|
8299 | * by the backend.
|
---|
8300 | */
|
---|
8301 | RTStrFree(backendName);
|
---|
8302 | return setError(E_FAIL,
|
---|
8303 | tr("The medium '%s' can't be used as the requested device type (%s, detected %s)"),
|
---|
8304 | locationFull.c_str(), getDeviceTypeName(m->devType), getVDTypeName(enmType));
|
---|
8305 | }
|
---|
8306 | else
|
---|
8307 | {
|
---|
8308 | ComAssertRet(backendName != NULL && *backendName != '\0', E_FAIL);
|
---|
8309 |
|
---|
8310 | HRESULT rc = i_setFormat(backendName);
|
---|
8311 | RTStrFree(backendName);
|
---|
8312 |
|
---|
8313 | /* setFormat() must not fail since we've just used the backend so
|
---|
8314 | * the format object must be there */
|
---|
8315 | AssertComRCReturnRC(rc);
|
---|
8316 | }
|
---|
8317 | }
|
---|
8318 |
|
---|
8319 | m->strLocationFull = locationFull;
|
---|
8320 |
|
---|
8321 | /* is it still a file? */
|
---|
8322 | if ( (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
8323 | && (m->state == MediumState_NotCreated)
|
---|
8324 | )
|
---|
8325 | /* assign a new UUID (this UUID will be used when calling
|
---|
8326 | * VDCreateBase/VDCreateDiff as a wanted UUID). Note that we
|
---|
8327 | * also do that if we didn't generate it to make sure it is
|
---|
8328 | * either generated by us or reset to null */
|
---|
8329 | unconst(m->id) = id;
|
---|
8330 | }
|
---|
8331 | else
|
---|
8332 | m->strLocationFull = aLocation;
|
---|
8333 |
|
---|
8334 | return S_OK;
|
---|
8335 | }
|
---|
8336 |
|
---|
8337 | /**
|
---|
8338 | * Checks that the format ID is valid and sets it on success.
|
---|
8339 | *
|
---|
8340 | * Note that this method will caller-reference the format object on success!
|
---|
8341 | * This reference must be released somewhere to let the MediumFormat object be
|
---|
8342 | * uninitialized.
|
---|
8343 | *
|
---|
8344 | * @note Must be called from under this object's write lock.
|
---|
8345 | */
|
---|
8346 | HRESULT Medium::i_setFormat(const Utf8Str &aFormat)
|
---|
8347 | {
|
---|
8348 | /* get the format object first */
|
---|
8349 | {
|
---|
8350 | SystemProperties *pSysProps = m->pVirtualBox->i_getSystemProperties();
|
---|
8351 | AutoReadLock propsLock(pSysProps COMMA_LOCKVAL_SRC_POS);
|
---|
8352 |
|
---|
8353 | unconst(m->formatObj) = pSysProps->i_mediumFormat(aFormat);
|
---|
8354 | if (m->formatObj.isNull())
|
---|
8355 | return setError(E_INVALIDARG,
|
---|
8356 | tr("Invalid medium storage format '%s'"),
|
---|
8357 | aFormat.c_str());
|
---|
8358 |
|
---|
8359 | /* get properties (preinsert them as keys in the map). Note that the
|
---|
8360 | * map doesn't grow over the object life time since the set of
|
---|
8361 | * properties is meant to be constant. */
|
---|
8362 |
|
---|
8363 | Assert(m->mapProperties.empty());
|
---|
8364 |
|
---|
8365 | for (MediumFormat::PropertyArray::const_iterator it = m->formatObj->i_getProperties().begin();
|
---|
8366 | it != m->formatObj->i_getProperties().end();
|
---|
8367 | ++it)
|
---|
8368 | {
|
---|
8369 | m->mapProperties.insert(std::make_pair(it->strName, Utf8Str::Empty));
|
---|
8370 | }
|
---|
8371 | }
|
---|
8372 |
|
---|
8373 | unconst(m->strFormat) = aFormat;
|
---|
8374 |
|
---|
8375 | return S_OK;
|
---|
8376 | }
|
---|
8377 |
|
---|
8378 | /**
|
---|
8379 | * Converts the Medium device type to the VD type.
|
---|
8380 | */
|
---|
8381 | VDTYPE Medium::i_convertDeviceType()
|
---|
8382 | {
|
---|
8383 | VDTYPE enmType;
|
---|
8384 |
|
---|
8385 | switch (m->devType)
|
---|
8386 | {
|
---|
8387 | case DeviceType_HardDisk:
|
---|
8388 | enmType = VDTYPE_HDD;
|
---|
8389 | break;
|
---|
8390 | case DeviceType_DVD:
|
---|
8391 | enmType = VDTYPE_OPTICAL_DISC;
|
---|
8392 | break;
|
---|
8393 | case DeviceType_Floppy:
|
---|
8394 | enmType = VDTYPE_FLOPPY;
|
---|
8395 | break;
|
---|
8396 | default:
|
---|
8397 | ComAssertFailedRet(VDTYPE_INVALID);
|
---|
8398 | }
|
---|
8399 |
|
---|
8400 | return enmType;
|
---|
8401 | }
|
---|
8402 |
|
---|
8403 | /**
|
---|
8404 | * Converts from the VD type to the medium type.
|
---|
8405 | */
|
---|
8406 | DeviceType_T Medium::i_convertToDeviceType(VDTYPE enmType)
|
---|
8407 | {
|
---|
8408 | DeviceType_T devType;
|
---|
8409 |
|
---|
8410 | switch (enmType)
|
---|
8411 | {
|
---|
8412 | case VDTYPE_HDD:
|
---|
8413 | devType = DeviceType_HardDisk;
|
---|
8414 | break;
|
---|
8415 | case VDTYPE_OPTICAL_DISC:
|
---|
8416 | devType = DeviceType_DVD;
|
---|
8417 | break;
|
---|
8418 | case VDTYPE_FLOPPY:
|
---|
8419 | devType = DeviceType_Floppy;
|
---|
8420 | break;
|
---|
8421 | default:
|
---|
8422 | ComAssertFailedRet(DeviceType_Null);
|
---|
8423 | }
|
---|
8424 |
|
---|
8425 | return devType;
|
---|
8426 | }
|
---|
8427 |
|
---|
8428 | /**
|
---|
8429 | * Internal method which checks whether a property name is for a filter plugin.
|
---|
8430 | */
|
---|
8431 | bool Medium::i_isPropertyForFilter(const com::Utf8Str &aName)
|
---|
8432 | {
|
---|
8433 | /* If the name contains "/" use the part before as a filter name and lookup the filter. */
|
---|
8434 | size_t offSlash;
|
---|
8435 | if ((offSlash = aName.find("/", 0)) != aName.npos)
|
---|
8436 | {
|
---|
8437 | com::Utf8Str strFilter;
|
---|
8438 | com::Utf8Str strKey;
|
---|
8439 |
|
---|
8440 | HRESULT rc = strFilter.assignEx(aName, 0, offSlash);
|
---|
8441 | if (FAILED(rc))
|
---|
8442 | return false;
|
---|
8443 |
|
---|
8444 | rc = strKey.assignEx(aName, offSlash + 1, aName.length() - offSlash - 1); /* Skip slash */
|
---|
8445 | if (FAILED(rc))
|
---|
8446 | return false;
|
---|
8447 |
|
---|
8448 | VDFILTERINFO FilterInfo;
|
---|
8449 | int vrc = VDFilterInfoOne(strFilter.c_str(), &FilterInfo);
|
---|
8450 | if (RT_SUCCESS(vrc))
|
---|
8451 | {
|
---|
8452 | /* Check that the property exists. */
|
---|
8453 | PCVDCONFIGINFO paConfig = FilterInfo.paConfigInfo;
|
---|
8454 | while (paConfig->pszKey)
|
---|
8455 | {
|
---|
8456 | if (strKey.equals(paConfig->pszKey))
|
---|
8457 | return true;
|
---|
8458 | paConfig++;
|
---|
8459 | }
|
---|
8460 | }
|
---|
8461 | }
|
---|
8462 |
|
---|
8463 | return false;
|
---|
8464 | }
|
---|
8465 |
|
---|
8466 | /**
|
---|
8467 | * Returns the last error message collected by the i_vdErrorCall callback and
|
---|
8468 | * resets it.
|
---|
8469 | *
|
---|
8470 | * The error message is returned prepended with a dot and a space, like this:
|
---|
8471 | * <code>
|
---|
8472 | * ". <error_text> (%Rrc)"
|
---|
8473 | * </code>
|
---|
8474 | * to make it easily appendable to a more general error message. The @c %Rrc
|
---|
8475 | * format string is given @a aVRC as an argument.
|
---|
8476 | *
|
---|
8477 | * If there is no last error message collected by i_vdErrorCall or if it is a
|
---|
8478 | * null or empty string, then this function returns the following text:
|
---|
8479 | * <code>
|
---|
8480 | * " (%Rrc)"
|
---|
8481 | * </code>
|
---|
8482 | *
|
---|
8483 | * @note Doesn't do any object locking; it is assumed that the caller makes sure
|
---|
8484 | * the callback isn't called by more than one thread at a time.
|
---|
8485 | *
|
---|
8486 | * @param aVRC VBox error code to use when no error message is provided.
|
---|
8487 | */
|
---|
8488 | Utf8Str Medium::i_vdError(int aVRC)
|
---|
8489 | {
|
---|
8490 | Utf8Str error;
|
---|
8491 |
|
---|
8492 | if (m->vdError.isEmpty())
|
---|
8493 | error = Utf8StrFmt(" (%Rrc)", aVRC);
|
---|
8494 | else
|
---|
8495 | error = Utf8StrFmt(".\n%s", m->vdError.c_str());
|
---|
8496 |
|
---|
8497 | m->vdError.setNull();
|
---|
8498 |
|
---|
8499 | return error;
|
---|
8500 | }
|
---|
8501 |
|
---|
8502 | /**
|
---|
8503 | * Error message callback.
|
---|
8504 | *
|
---|
8505 | * Puts the reported error message to the m->vdError field.
|
---|
8506 | *
|
---|
8507 | * @note Doesn't do any object locking; it is assumed that the caller makes sure
|
---|
8508 | * the callback isn't called by more than one thread at a time.
|
---|
8509 | *
|
---|
8510 | * @param pvUser The opaque data passed on container creation.
|
---|
8511 | * @param rc The VBox error code.
|
---|
8512 | * @param SRC_POS Use RT_SRC_POS.
|
---|
8513 | * @param pszFormat Error message format string.
|
---|
8514 | * @param va Error message arguments.
|
---|
8515 | */
|
---|
8516 | /*static*/
|
---|
8517 | DECLCALLBACK(void) Medium::i_vdErrorCall(void *pvUser, int rc, RT_SRC_POS_DECL,
|
---|
8518 | const char *pszFormat, va_list va)
|
---|
8519 | {
|
---|
8520 | NOREF(pszFile); NOREF(iLine); NOREF(pszFunction); /* RT_SRC_POS_DECL */
|
---|
8521 |
|
---|
8522 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
8523 | AssertReturnVoid(that != NULL);
|
---|
8524 |
|
---|
8525 | if (that->m->vdError.isEmpty())
|
---|
8526 | that->m->vdError =
|
---|
8527 | Utf8StrFmt("%s (%Rrc)", Utf8Str(pszFormat, va).c_str(), rc);
|
---|
8528 | else
|
---|
8529 | that->m->vdError =
|
---|
8530 | Utf8StrFmt("%s.\n%s (%Rrc)", that->m->vdError.c_str(),
|
---|
8531 | Utf8Str(pszFormat, va).c_str(), rc);
|
---|
8532 | }
|
---|
8533 |
|
---|
8534 | /* static */
|
---|
8535 | DECLCALLBACK(bool) Medium::i_vdConfigAreKeysValid(void *pvUser,
|
---|
8536 | const char * /* pszzValid */)
|
---|
8537 | {
|
---|
8538 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
8539 | AssertReturn(that != NULL, false);
|
---|
8540 |
|
---|
8541 | /* we always return true since the only keys we have are those found in
|
---|
8542 | * VDBACKENDINFO */
|
---|
8543 | return true;
|
---|
8544 | }
|
---|
8545 |
|
---|
8546 | /* static */
|
---|
8547 | DECLCALLBACK(int) Medium::i_vdConfigQuerySize(void *pvUser,
|
---|
8548 | const char *pszName,
|
---|
8549 | size_t *pcbValue)
|
---|
8550 | {
|
---|
8551 | AssertPtrReturn(pcbValue, VERR_INVALID_POINTER);
|
---|
8552 |
|
---|
8553 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
8554 | AssertReturn(that != NULL, VERR_GENERAL_FAILURE);
|
---|
8555 |
|
---|
8556 | settings::StringsMap::const_iterator it = that->m->mapProperties.find(Utf8Str(pszName));
|
---|
8557 | if (it == that->m->mapProperties.end())
|
---|
8558 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
8559 |
|
---|
8560 | /* we interpret null values as "no value" in Medium */
|
---|
8561 | if (it->second.isEmpty())
|
---|
8562 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
8563 |
|
---|
8564 | *pcbValue = it->second.length() + 1 /* include terminator */;
|
---|
8565 |
|
---|
8566 | return VINF_SUCCESS;
|
---|
8567 | }
|
---|
8568 |
|
---|
8569 | /* static */
|
---|
8570 | DECLCALLBACK(int) Medium::i_vdConfigQuery(void *pvUser,
|
---|
8571 | const char *pszName,
|
---|
8572 | char *pszValue,
|
---|
8573 | size_t cchValue)
|
---|
8574 | {
|
---|
8575 | AssertPtrReturn(pszValue, VERR_INVALID_POINTER);
|
---|
8576 |
|
---|
8577 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
8578 | AssertReturn(that != NULL, VERR_GENERAL_FAILURE);
|
---|
8579 |
|
---|
8580 | settings::StringsMap::const_iterator it = that->m->mapProperties.find(Utf8Str(pszName));
|
---|
8581 | if (it == that->m->mapProperties.end())
|
---|
8582 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
8583 |
|
---|
8584 | /* we interpret null values as "no value" in Medium */
|
---|
8585 | if (it->second.isEmpty())
|
---|
8586 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
8587 |
|
---|
8588 | const Utf8Str &value = it->second;
|
---|
8589 | if (value.length() >= cchValue)
|
---|
8590 | return VERR_CFGM_NOT_ENOUGH_SPACE;
|
---|
8591 |
|
---|
8592 | memcpy(pszValue, value.c_str(), value.length() + 1);
|
---|
8593 |
|
---|
8594 | return VINF_SUCCESS;
|
---|
8595 | }
|
---|
8596 |
|
---|
8597 | DECLCALLBACK(bool) Medium::i_vdCryptoConfigAreKeysValid(void *pvUser, const char *pszzValid)
|
---|
8598 | {
|
---|
8599 | /* Just return always true here. */
|
---|
8600 | NOREF(pvUser);
|
---|
8601 | NOREF(pszzValid);
|
---|
8602 | return true;
|
---|
8603 | }
|
---|
8604 |
|
---|
8605 | DECLCALLBACK(int) Medium::i_vdCryptoConfigQuerySize(void *pvUser, const char *pszName, size_t *pcbValue)
|
---|
8606 | {
|
---|
8607 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8608 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8609 | AssertPtrReturn(pcbValue, VERR_INVALID_POINTER);
|
---|
8610 |
|
---|
8611 | size_t cbValue = 0;
|
---|
8612 | if (!strcmp(pszName, "Algorithm"))
|
---|
8613 | cbValue = strlen(pSettings->pszCipher) + 1;
|
---|
8614 | else if (!strcmp(pszName, "KeyId"))
|
---|
8615 | cbValue = sizeof("irrelevant");
|
---|
8616 | else if (!strcmp(pszName, "KeyStore"))
|
---|
8617 | {
|
---|
8618 | if (!pSettings->pszKeyStoreLoad)
|
---|
8619 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
8620 | cbValue = strlen(pSettings->pszKeyStoreLoad) + 1;
|
---|
8621 | }
|
---|
8622 | else if (!strcmp(pszName, "CreateKeyStore"))
|
---|
8623 | cbValue = 2; /* Single digit + terminator. */
|
---|
8624 | else
|
---|
8625 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
8626 |
|
---|
8627 | *pcbValue = cbValue + 1 /* include terminator */;
|
---|
8628 |
|
---|
8629 | return VINF_SUCCESS;
|
---|
8630 | }
|
---|
8631 |
|
---|
8632 | DECLCALLBACK(int) Medium::i_vdCryptoConfigQuery(void *pvUser, const char *pszName,
|
---|
8633 | char *pszValue, size_t cchValue)
|
---|
8634 | {
|
---|
8635 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8636 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8637 | AssertPtrReturn(pszValue, VERR_INVALID_POINTER);
|
---|
8638 |
|
---|
8639 | const char *psz = NULL;
|
---|
8640 | if (!strcmp(pszName, "Algorithm"))
|
---|
8641 | psz = pSettings->pszCipher;
|
---|
8642 | else if (!strcmp(pszName, "KeyId"))
|
---|
8643 | psz = "irrelevant";
|
---|
8644 | else if (!strcmp(pszName, "KeyStore"))
|
---|
8645 | psz = pSettings->pszKeyStoreLoad;
|
---|
8646 | else if (!strcmp(pszName, "CreateKeyStore"))
|
---|
8647 | {
|
---|
8648 | if (pSettings->fCreateKeyStore)
|
---|
8649 | psz = "1";
|
---|
8650 | else
|
---|
8651 | psz = "0";
|
---|
8652 | }
|
---|
8653 | else
|
---|
8654 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
8655 |
|
---|
8656 | size_t cch = strlen(psz);
|
---|
8657 | if (cch >= cchValue)
|
---|
8658 | return VERR_CFGM_NOT_ENOUGH_SPACE;
|
---|
8659 |
|
---|
8660 | memcpy(pszValue, psz, cch + 1);
|
---|
8661 | return VINF_SUCCESS;
|
---|
8662 | }
|
---|
8663 |
|
---|
8664 | DECLCALLBACK(int) Medium::i_vdConfigUpdate(void *pvUser,
|
---|
8665 | bool fCreate,
|
---|
8666 | const char *pszName,
|
---|
8667 | const char *pszValue)
|
---|
8668 | {
|
---|
8669 | Medium *that = (Medium *)pvUser;
|
---|
8670 |
|
---|
8671 | // Detect if this runs inside i_queryInfo() on the current thread.
|
---|
8672 | // Skip if not. Check does not need synchronization.
|
---|
8673 | if (!that->m || !that->m->queryInfoRunning || !that->m->queryInfoSem.isWriteLockOnCurrentThread())
|
---|
8674 | return VINF_SUCCESS;
|
---|
8675 |
|
---|
8676 | // It's guaranteed that this code is executing inside Medium::i_queryInfo,
|
---|
8677 | // can assume it took care of synchronization.
|
---|
8678 | int rv = VINF_SUCCESS;
|
---|
8679 | Utf8Str strName(pszName);
|
---|
8680 | settings::StringsMap::const_iterator it = that->m->mapProperties.find(strName);
|
---|
8681 | if (it == that->m->mapProperties.end() && !fCreate)
|
---|
8682 | rv = VERR_CFGM_VALUE_NOT_FOUND;
|
---|
8683 | else
|
---|
8684 | that->m->mapProperties[strName] = Utf8Str(pszValue);
|
---|
8685 | return rv;
|
---|
8686 | }
|
---|
8687 |
|
---|
8688 | DECLCALLBACK(int) Medium::i_vdCryptoKeyRetain(void *pvUser, const char *pszId,
|
---|
8689 | const uint8_t **ppbKey, size_t *pcbKey)
|
---|
8690 | {
|
---|
8691 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8692 | NOREF(pszId);
|
---|
8693 | NOREF(ppbKey);
|
---|
8694 | NOREF(pcbKey);
|
---|
8695 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8696 | AssertMsgFailedReturn(("This method should not be called here!\n"), VERR_INVALID_STATE);
|
---|
8697 | }
|
---|
8698 |
|
---|
8699 | DECLCALLBACK(int) Medium::i_vdCryptoKeyRelease(void *pvUser, const char *pszId)
|
---|
8700 | {
|
---|
8701 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8702 | NOREF(pszId);
|
---|
8703 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8704 | AssertMsgFailedReturn(("This method should not be called here!\n"), VERR_INVALID_STATE);
|
---|
8705 | }
|
---|
8706 |
|
---|
8707 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStorePasswordRetain(void *pvUser, const char *pszId, const char **ppszPassword)
|
---|
8708 | {
|
---|
8709 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8710 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8711 |
|
---|
8712 | NOREF(pszId);
|
---|
8713 | *ppszPassword = pSettings->pszPassword;
|
---|
8714 | return VINF_SUCCESS;
|
---|
8715 | }
|
---|
8716 |
|
---|
8717 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStorePasswordRelease(void *pvUser, const char *pszId)
|
---|
8718 | {
|
---|
8719 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8720 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8721 | NOREF(pszId);
|
---|
8722 | return VINF_SUCCESS;
|
---|
8723 | }
|
---|
8724 |
|
---|
8725 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStoreSave(void *pvUser, const void *pvKeyStore, size_t cbKeyStore)
|
---|
8726 | {
|
---|
8727 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8728 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8729 |
|
---|
8730 | pSettings->pszKeyStore = (char *)RTMemAllocZ(cbKeyStore);
|
---|
8731 | if (!pSettings->pszKeyStore)
|
---|
8732 | return VERR_NO_MEMORY;
|
---|
8733 |
|
---|
8734 | memcpy(pSettings->pszKeyStore, pvKeyStore, cbKeyStore);
|
---|
8735 | return VINF_SUCCESS;
|
---|
8736 | }
|
---|
8737 |
|
---|
8738 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStoreReturnParameters(void *pvUser, const char *pszCipher,
|
---|
8739 | const uint8_t *pbDek, size_t cbDek)
|
---|
8740 | {
|
---|
8741 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8742 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8743 |
|
---|
8744 | pSettings->pszCipherReturned = RTStrDup(pszCipher);
|
---|
8745 | pSettings->pbDek = pbDek;
|
---|
8746 | pSettings->cbDek = cbDek;
|
---|
8747 |
|
---|
8748 | return pSettings->pszCipherReturned ? VINF_SUCCESS : VERR_NO_MEMORY;
|
---|
8749 | }
|
---|
8750 |
|
---|
8751 | /**
|
---|
8752 | * Creates a VDISK instance for this medium.
|
---|
8753 | *
|
---|
8754 | * @note Caller should not hold any medium related locks as this method will
|
---|
8755 | * acquire the medium lock for writing and others (VirtualBox).
|
---|
8756 | *
|
---|
8757 | * @returns COM status code.
|
---|
8758 | * @param fWritable Whether to return a writable VDISK instance
|
---|
8759 | * (true) or a read-only one (false).
|
---|
8760 | * @param pKeyStore The key store.
|
---|
8761 | * @param ppHdd Where to return the pointer to the VDISK on
|
---|
8762 | * success.
|
---|
8763 | * @param pMediumLockList The lock list to populate and lock. Caller
|
---|
8764 | * is responsible for calling the destructor or
|
---|
8765 | * MediumLockList::Clear() after destroying
|
---|
8766 | * @a *ppHdd
|
---|
8767 | * @param pCryptoSettings The crypto settings to use for setting up
|
---|
8768 | * decryption/encryption of the VDISK. This object
|
---|
8769 | * must be alive until the VDISK is destroyed!
|
---|
8770 | */
|
---|
8771 | HRESULT Medium::i_openForIO(bool fWritable, SecretKeyStore *pKeyStore, PVDISK *ppHdd, MediumLockList *pMediumLockList,
|
---|
8772 | MediumCryptoFilterSettings *pCryptoSettings)
|
---|
8773 | {
|
---|
8774 | /*
|
---|
8775 | * Create the media lock list and lock the media.
|
---|
8776 | */
|
---|
8777 | HRESULT hrc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
8778 | fWritable ? this : NULL /* pToLockWrite */,
|
---|
8779 | false /* fMediumLockWriteAll */,
|
---|
8780 | NULL,
|
---|
8781 | *pMediumLockList);
|
---|
8782 | if (SUCCEEDED(hrc))
|
---|
8783 | hrc = pMediumLockList->Lock();
|
---|
8784 | if (FAILED(hrc))
|
---|
8785 | return hrc;
|
---|
8786 |
|
---|
8787 | /*
|
---|
8788 | * Get the base medium before write locking this medium.
|
---|
8789 | */
|
---|
8790 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
8791 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8792 |
|
---|
8793 | /*
|
---|
8794 | * Create the VDISK instance.
|
---|
8795 | */
|
---|
8796 | PVDISK pHdd;
|
---|
8797 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pHdd);
|
---|
8798 | AssertRCReturn(vrc, E_FAIL);
|
---|
8799 |
|
---|
8800 | /*
|
---|
8801 | * Goto avoidance using try/catch/throw(HRESULT).
|
---|
8802 | */
|
---|
8803 | try
|
---|
8804 | {
|
---|
8805 | settings::StringsMap::iterator itKeyStore = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
8806 | if (itKeyStore != pBase->m->mapProperties.end())
|
---|
8807 | {
|
---|
8808 | #ifdef VBOX_WITH_EXTPACK
|
---|
8809 | settings::StringsMap::iterator itKeyId = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
8810 |
|
---|
8811 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
8812 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
8813 | {
|
---|
8814 | /* Load the plugin */
|
---|
8815 | Utf8Str strPlugin;
|
---|
8816 | hrc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
8817 | if (SUCCEEDED(hrc))
|
---|
8818 | {
|
---|
8819 | vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
8820 | if (RT_FAILURE(vrc))
|
---|
8821 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
8822 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
8823 | i_vdError(vrc).c_str());
|
---|
8824 | }
|
---|
8825 | else
|
---|
8826 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
8827 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
8828 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
8829 | }
|
---|
8830 | else
|
---|
8831 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
8832 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
8833 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
8834 |
|
---|
8835 | if (itKeyId == pBase->m->mapProperties.end())
|
---|
8836 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8837 | tr("Image '%s' is configured for encryption but doesn't has a key identifier set"),
|
---|
8838 | pBase->m->strLocationFull.c_str());
|
---|
8839 |
|
---|
8840 | /* Find the proper secret key in the key store. */
|
---|
8841 | if (!pKeyStore)
|
---|
8842 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8843 | tr("Image '%s' is configured for encryption but there is no key store to retrieve the password from"),
|
---|
8844 | pBase->m->strLocationFull.c_str());
|
---|
8845 |
|
---|
8846 | SecretKey *pKey = NULL;
|
---|
8847 | vrc = pKeyStore->retainSecretKey(itKeyId->second, &pKey);
|
---|
8848 | if (RT_FAILURE(vrc))
|
---|
8849 | throw setErrorBoth(VBOX_E_INVALID_OBJECT_STATE, vrc,
|
---|
8850 | tr("Failed to retrieve the secret key with ID \"%s\" from the store (%Rrc)"),
|
---|
8851 | itKeyId->second.c_str(), vrc);
|
---|
8852 |
|
---|
8853 | i_taskEncryptSettingsSetup(pCryptoSettings, NULL, itKeyStore->second.c_str(), (const char *)pKey->getKeyBuffer(),
|
---|
8854 | false /* fCreateKeyStore */);
|
---|
8855 | vrc = VDFilterAdd(pHdd, "CRYPT", VD_FILTER_FLAGS_DEFAULT, pCryptoSettings->vdFilterIfaces);
|
---|
8856 | pKeyStore->releaseSecretKey(itKeyId->second);
|
---|
8857 | if (vrc == VERR_VD_PASSWORD_INCORRECT)
|
---|
8858 | throw setErrorBoth(VBOX_E_PASSWORD_INCORRECT, vrc, tr("The password to decrypt the image is incorrect"));
|
---|
8859 | if (RT_FAILURE(vrc))
|
---|
8860 | throw setErrorBoth(VBOX_E_INVALID_OBJECT_STATE, vrc, tr("Failed to load the decryption filter: %s"),
|
---|
8861 | i_vdError(vrc).c_str());
|
---|
8862 | #else
|
---|
8863 | RT_NOREF(pKeyStore, pCryptoSettings);
|
---|
8864 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
8865 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
8866 | #endif /* VBOX_WITH_EXTPACK */
|
---|
8867 | }
|
---|
8868 |
|
---|
8869 | /*
|
---|
8870 | * Open all media in the source chain.
|
---|
8871 | */
|
---|
8872 | MediumLockList::Base::const_iterator sourceListBegin = pMediumLockList->GetBegin();
|
---|
8873 | MediumLockList::Base::const_iterator sourceListEnd = pMediumLockList->GetEnd();
|
---|
8874 | MediumLockList::Base::const_iterator mediumListLast = sourceListEnd;
|
---|
8875 | --mediumListLast;
|
---|
8876 |
|
---|
8877 | for (MediumLockList::Base::const_iterator it = sourceListBegin; it != sourceListEnd; ++it)
|
---|
8878 | {
|
---|
8879 | const MediumLock &mediumLock = *it;
|
---|
8880 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8881 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8882 |
|
---|
8883 | /* sanity check */
|
---|
8884 | Assert(pMedium->m->state == (fWritable && it == mediumListLast ? MediumState_LockedWrite : MediumState_LockedRead));
|
---|
8885 |
|
---|
8886 | /* Open all media in read-only mode. */
|
---|
8887 | vrc = VDOpen(pHdd,
|
---|
8888 | pMedium->m->strFormat.c_str(),
|
---|
8889 | pMedium->m->strLocationFull.c_str(),
|
---|
8890 | m->uOpenFlagsDef | (fWritable && it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
8891 | pMedium->m->vdImageIfaces);
|
---|
8892 | if (RT_FAILURE(vrc))
|
---|
8893 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
8894 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8895 | pMedium->m->strLocationFull.c_str(),
|
---|
8896 | i_vdError(vrc).c_str());
|
---|
8897 | }
|
---|
8898 |
|
---|
8899 | Assert(m->state == (fWritable ? MediumState_LockedWrite : MediumState_LockedRead));
|
---|
8900 |
|
---|
8901 | /*
|
---|
8902 | * Done!
|
---|
8903 | */
|
---|
8904 | *ppHdd = pHdd;
|
---|
8905 | return S_OK;
|
---|
8906 | }
|
---|
8907 | catch (HRESULT hrc2)
|
---|
8908 | {
|
---|
8909 | hrc = hrc2;
|
---|
8910 | }
|
---|
8911 |
|
---|
8912 | VDDestroy(pHdd);
|
---|
8913 | return hrc;
|
---|
8914 |
|
---|
8915 | }
|
---|
8916 |
|
---|
8917 | /**
|
---|
8918 | * Implementation code for the "create base" task.
|
---|
8919 | *
|
---|
8920 | * This only gets started from Medium::CreateBaseStorage() and always runs
|
---|
8921 | * asynchronously. As a result, we always save the VirtualBox.xml file when
|
---|
8922 | * we're done here.
|
---|
8923 | *
|
---|
8924 | * @param task
|
---|
8925 | * @return
|
---|
8926 | */
|
---|
8927 | HRESULT Medium::i_taskCreateBaseHandler(Medium::CreateBaseTask &task)
|
---|
8928 | {
|
---|
8929 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
8930 | * to lock order violations, it probably causes lock order issues related
|
---|
8931 | * to the AutoCaller usage. */
|
---|
8932 | HRESULT rc = S_OK;
|
---|
8933 |
|
---|
8934 | /* these parameters we need after creation */
|
---|
8935 | uint64_t size = 0, logicalSize = 0;
|
---|
8936 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
8937 | bool fGenerateUuid = false;
|
---|
8938 |
|
---|
8939 | try
|
---|
8940 | {
|
---|
8941 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8942 |
|
---|
8943 | /* The object may request a specific UUID (through a special form of
|
---|
8944 | * the moveTo() argument). Otherwise we have to generate it */
|
---|
8945 | Guid id = m->id;
|
---|
8946 |
|
---|
8947 | fGenerateUuid = id.isZero();
|
---|
8948 | if (fGenerateUuid)
|
---|
8949 | {
|
---|
8950 | id.create();
|
---|
8951 | /* VirtualBox::i_registerMedium() will need UUID */
|
---|
8952 | unconst(m->id) = id;
|
---|
8953 | }
|
---|
8954 |
|
---|
8955 | Utf8Str format(m->strFormat);
|
---|
8956 | Utf8Str location(m->strLocationFull);
|
---|
8957 | uint64_t capabilities = m->formatObj->i_getCapabilities();
|
---|
8958 | ComAssertThrow(capabilities & ( MediumFormatCapabilities_CreateFixed
|
---|
8959 | | MediumFormatCapabilities_CreateDynamic), E_FAIL);
|
---|
8960 | Assert(m->state == MediumState_Creating);
|
---|
8961 |
|
---|
8962 | PVDISK hdd;
|
---|
8963 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8964 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8965 |
|
---|
8966 | /* unlock before the potentially lengthy operation */
|
---|
8967 | thisLock.release();
|
---|
8968 |
|
---|
8969 | try
|
---|
8970 | {
|
---|
8971 | /* ensure the directory exists */
|
---|
8972 | if (capabilities & MediumFormatCapabilities_File)
|
---|
8973 | {
|
---|
8974 | rc = VirtualBox::i_ensureFilePathExists(location, !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
8975 | if (FAILED(rc))
|
---|
8976 | throw rc;
|
---|
8977 | }
|
---|
8978 |
|
---|
8979 | VDGEOMETRY geo = { 0, 0, 0 }; /* auto-detect */
|
---|
8980 |
|
---|
8981 | vrc = VDCreateBase(hdd,
|
---|
8982 | format.c_str(),
|
---|
8983 | location.c_str(),
|
---|
8984 | task.mSize,
|
---|
8985 | task.mVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted),
|
---|
8986 | NULL,
|
---|
8987 | &geo,
|
---|
8988 | &geo,
|
---|
8989 | id.raw(),
|
---|
8990 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
8991 | m->vdImageIfaces,
|
---|
8992 | task.mVDOperationIfaces);
|
---|
8993 | if (RT_FAILURE(vrc))
|
---|
8994 | {
|
---|
8995 | if (vrc == VERR_VD_INVALID_TYPE)
|
---|
8996 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
8997 | tr("Parameters for creating the medium storage unit '%s' are invalid%s"),
|
---|
8998 | location.c_str(), i_vdError(vrc).c_str());
|
---|
8999 | else
|
---|
9000 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9001 | tr("Could not create the medium storage unit '%s'%s"),
|
---|
9002 | location.c_str(), i_vdError(vrc).c_str());
|
---|
9003 | }
|
---|
9004 |
|
---|
9005 | if (task.mVariant & MediumVariant_Formatted)
|
---|
9006 | {
|
---|
9007 | RTVFSFILE hVfsFile;
|
---|
9008 | vrc = VDCreateVfsFileFromDisk(hdd, 0 /*fFlags*/, &hVfsFile);
|
---|
9009 | if (RT_FAILURE(vrc))
|
---|
9010 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Opening medium storage unit '%s' failed%s"),
|
---|
9011 | location.c_str(), i_vdError(vrc).c_str());
|
---|
9012 | RTERRINFOSTATIC ErrInfo;
|
---|
9013 | vrc = RTFsFatVolFormat(hVfsFile, 0 /* offVol */, 0 /* cbVol */, RTFSFATVOL_FMT_F_FULL,
|
---|
9014 | 0 /* cbSector */, 0 /* cbSectorPerCluster */, RTFSFATTYPE_INVALID,
|
---|
9015 | 0 /* cHeads */, 0 /* cSectorsPerTrack*/, 0 /* bMedia */,
|
---|
9016 | 0 /* cRootDirEntries */, 0 /* cHiddenSectors */,
|
---|
9017 | RTErrInfoInitStatic(&ErrInfo));
|
---|
9018 | RTVfsFileRelease(hVfsFile);
|
---|
9019 | if (RT_FAILURE(vrc) && RTErrInfoIsSet(&ErrInfo.Core))
|
---|
9020 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Formatting medium storage unit '%s' failed: %s"),
|
---|
9021 | location.c_str(), ErrInfo.Core.pszMsg);
|
---|
9022 | if (RT_FAILURE(vrc))
|
---|
9023 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Formatting medium storage unit '%s' failed%s"),
|
---|
9024 | location.c_str(), i_vdError(vrc).c_str());
|
---|
9025 | }
|
---|
9026 |
|
---|
9027 | size = VDGetFileSize(hdd, 0);
|
---|
9028 | logicalSize = VDGetSize(hdd, 0);
|
---|
9029 | unsigned uImageFlags;
|
---|
9030 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
9031 | if (RT_SUCCESS(vrc))
|
---|
9032 | variant = (MediumVariant_T)uImageFlags;
|
---|
9033 | }
|
---|
9034 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9035 |
|
---|
9036 | VDDestroy(hdd);
|
---|
9037 | }
|
---|
9038 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9039 |
|
---|
9040 | if (SUCCEEDED(rc))
|
---|
9041 | {
|
---|
9042 | /* register with mVirtualBox as the last step and move to
|
---|
9043 | * Created state only on success (leaving an orphan file is
|
---|
9044 | * better than breaking media registry consistency) */
|
---|
9045 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
9046 | ComObjPtr<Medium> pMedium;
|
---|
9047 | rc = m->pVirtualBox->i_registerMedium(this, &pMedium, treeLock);
|
---|
9048 | Assert(pMedium == NULL || this == pMedium);
|
---|
9049 | }
|
---|
9050 |
|
---|
9051 | // re-acquire the lock before changing state
|
---|
9052 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9053 |
|
---|
9054 | if (SUCCEEDED(rc))
|
---|
9055 | {
|
---|
9056 | m->state = MediumState_Created;
|
---|
9057 |
|
---|
9058 | m->size = size;
|
---|
9059 | m->logicalSize = logicalSize;
|
---|
9060 | m->variant = variant;
|
---|
9061 |
|
---|
9062 | thisLock.release();
|
---|
9063 | i_markRegistriesModified();
|
---|
9064 | if (task.isAsync())
|
---|
9065 | {
|
---|
9066 | // in asynchronous mode, save settings now
|
---|
9067 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
9068 | }
|
---|
9069 | }
|
---|
9070 | else
|
---|
9071 | {
|
---|
9072 | /* back to NotCreated on failure */
|
---|
9073 | m->state = MediumState_NotCreated;
|
---|
9074 |
|
---|
9075 | /* reset UUID to prevent it from being reused next time */
|
---|
9076 | if (fGenerateUuid)
|
---|
9077 | unconst(m->id).clear();
|
---|
9078 | }
|
---|
9079 |
|
---|
9080 | if (task.NotifyAboutChanges() && SUCCEEDED(rc))
|
---|
9081 | {
|
---|
9082 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
9083 | m->pVirtualBox->i_onMediumRegistered(m->id, m->devType, TRUE);
|
---|
9084 | }
|
---|
9085 |
|
---|
9086 | return rc;
|
---|
9087 | }
|
---|
9088 |
|
---|
9089 | /**
|
---|
9090 | * Implementation code for the "create diff" task.
|
---|
9091 | *
|
---|
9092 | * This task always gets started from Medium::createDiffStorage() and can run
|
---|
9093 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
9094 | * that function. If we run synchronously, the caller expects the medium
|
---|
9095 | * registry modification to be set before returning; otherwise (in asynchronous
|
---|
9096 | * mode), we save the settings ourselves.
|
---|
9097 | *
|
---|
9098 | * @param task
|
---|
9099 | * @return
|
---|
9100 | */
|
---|
9101 | HRESULT Medium::i_taskCreateDiffHandler(Medium::CreateDiffTask &task)
|
---|
9102 | {
|
---|
9103 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
9104 | * to lock order violations, it probably causes lock order issues related
|
---|
9105 | * to the AutoCaller usage. */
|
---|
9106 | HRESULT rcTmp = S_OK;
|
---|
9107 |
|
---|
9108 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
9109 |
|
---|
9110 | uint64_t size = 0, logicalSize = 0;
|
---|
9111 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
9112 | bool fGenerateUuid = false;
|
---|
9113 |
|
---|
9114 | try
|
---|
9115 | {
|
---|
9116 | if (i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
9117 | {
|
---|
9118 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9119 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
9120 | 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"),
|
---|
9121 | m->strLocationFull.c_str());
|
---|
9122 | }
|
---|
9123 |
|
---|
9124 | /* Lock both in {parent,child} order. */
|
---|
9125 | AutoMultiWriteLock2 mediaLock(this, pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
9126 |
|
---|
9127 | /* The object may request a specific UUID (through a special form of
|
---|
9128 | * the moveTo() argument). Otherwise we have to generate it */
|
---|
9129 | Guid targetId = pTarget->m->id;
|
---|
9130 |
|
---|
9131 | fGenerateUuid = targetId.isZero();
|
---|
9132 | if (fGenerateUuid)
|
---|
9133 | {
|
---|
9134 | targetId.create();
|
---|
9135 | /* VirtualBox::i_registerMedium() will need UUID */
|
---|
9136 | unconst(pTarget->m->id) = targetId;
|
---|
9137 | }
|
---|
9138 |
|
---|
9139 | Guid id = m->id;
|
---|
9140 |
|
---|
9141 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
9142 | Utf8Str targetLocation(pTarget->m->strLocationFull);
|
---|
9143 | uint64_t capabilities = pTarget->m->formatObj->i_getCapabilities();
|
---|
9144 | ComAssertThrow(capabilities & MediumFormatCapabilities_CreateDynamic, E_FAIL);
|
---|
9145 |
|
---|
9146 | Assert(pTarget->m->state == MediumState_Creating);
|
---|
9147 | Assert(m->state == MediumState_LockedRead);
|
---|
9148 |
|
---|
9149 | PVDISK hdd;
|
---|
9150 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9151 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9152 |
|
---|
9153 | /* the two media are now protected by their non-default states;
|
---|
9154 | * unlock the media before the potentially lengthy operation */
|
---|
9155 | mediaLock.release();
|
---|
9156 |
|
---|
9157 | try
|
---|
9158 | {
|
---|
9159 | /* Open all media in the target chain but the last. */
|
---|
9160 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
9161 | task.mpMediumLockList->GetBegin();
|
---|
9162 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
9163 | task.mpMediumLockList->GetEnd();
|
---|
9164 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
9165 | it != targetListEnd;
|
---|
9166 | ++it)
|
---|
9167 | {
|
---|
9168 | const MediumLock &mediumLock = *it;
|
---|
9169 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9170 |
|
---|
9171 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9172 |
|
---|
9173 | /* Skip over the target diff medium */
|
---|
9174 | if (pMedium->m->state == MediumState_Creating)
|
---|
9175 | continue;
|
---|
9176 |
|
---|
9177 | /* sanity check */
|
---|
9178 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
9179 |
|
---|
9180 | /* Open all media in appropriate mode. */
|
---|
9181 | vrc = VDOpen(hdd,
|
---|
9182 | pMedium->m->strFormat.c_str(),
|
---|
9183 | pMedium->m->strLocationFull.c_str(),
|
---|
9184 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
9185 | pMedium->m->vdImageIfaces);
|
---|
9186 | if (RT_FAILURE(vrc))
|
---|
9187 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9188 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9189 | pMedium->m->strLocationFull.c_str(),
|
---|
9190 | i_vdError(vrc).c_str());
|
---|
9191 | }
|
---|
9192 |
|
---|
9193 | /* ensure the target directory exists */
|
---|
9194 | if (capabilities & MediumFormatCapabilities_File)
|
---|
9195 | {
|
---|
9196 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
9197 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
9198 | if (FAILED(rc))
|
---|
9199 | throw rc;
|
---|
9200 | }
|
---|
9201 |
|
---|
9202 | vrc = VDCreateDiff(hdd,
|
---|
9203 | targetFormat.c_str(),
|
---|
9204 | targetLocation.c_str(),
|
---|
9205 | (task.mVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted | MediumVariant_VmdkESX | MediumVariant_VmdkRawDisk))
|
---|
9206 | | VD_IMAGE_FLAGS_DIFF,
|
---|
9207 | NULL,
|
---|
9208 | targetId.raw(),
|
---|
9209 | id.raw(),
|
---|
9210 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
9211 | pTarget->m->vdImageIfaces,
|
---|
9212 | task.mVDOperationIfaces);
|
---|
9213 | if (RT_FAILURE(vrc))
|
---|
9214 | {
|
---|
9215 | if (vrc == VERR_VD_INVALID_TYPE)
|
---|
9216 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9217 | tr("Parameters for creating the differencing medium storage unit '%s' are invalid%s"),
|
---|
9218 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
9219 | else
|
---|
9220 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9221 | tr("Could not create the differencing medium storage unit '%s'%s"),
|
---|
9222 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
9223 | }
|
---|
9224 |
|
---|
9225 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
9226 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
9227 | unsigned uImageFlags;
|
---|
9228 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
9229 | if (RT_SUCCESS(vrc))
|
---|
9230 | variant = (MediumVariant_T)uImageFlags;
|
---|
9231 | }
|
---|
9232 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9233 |
|
---|
9234 | VDDestroy(hdd);
|
---|
9235 | }
|
---|
9236 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9237 |
|
---|
9238 | MultiResult mrc(rcTmp);
|
---|
9239 |
|
---|
9240 | if (SUCCEEDED(mrc))
|
---|
9241 | {
|
---|
9242 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
9243 |
|
---|
9244 | Assert(pTarget->m->pParent.isNull());
|
---|
9245 |
|
---|
9246 | /* associate child with the parent, maximum depth was checked above */
|
---|
9247 | pTarget->i_setParent(this);
|
---|
9248 |
|
---|
9249 | /* diffs for immutable media are auto-reset by default */
|
---|
9250 | bool fAutoReset;
|
---|
9251 | {
|
---|
9252 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
9253 | AutoReadLock block(pBase COMMA_LOCKVAL_SRC_POS);
|
---|
9254 | fAutoReset = (pBase->m->type == MediumType_Immutable);
|
---|
9255 | }
|
---|
9256 | {
|
---|
9257 | AutoWriteLock tlock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
9258 | pTarget->m->autoReset = fAutoReset;
|
---|
9259 | }
|
---|
9260 |
|
---|
9261 | /* register with mVirtualBox as the last step and move to
|
---|
9262 | * Created state only on success (leaving an orphan file is
|
---|
9263 | * better than breaking media registry consistency) */
|
---|
9264 | ComObjPtr<Medium> pMedium;
|
---|
9265 | mrc = m->pVirtualBox->i_registerMedium(pTarget, &pMedium, treeLock);
|
---|
9266 | Assert(pTarget == pMedium);
|
---|
9267 |
|
---|
9268 | if (FAILED(mrc))
|
---|
9269 | /* break the parent association on failure to register */
|
---|
9270 | i_deparent();
|
---|
9271 | }
|
---|
9272 |
|
---|
9273 | AutoMultiWriteLock2 mediaLock(this, pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
9274 |
|
---|
9275 | if (SUCCEEDED(mrc))
|
---|
9276 | {
|
---|
9277 | pTarget->m->state = MediumState_Created;
|
---|
9278 |
|
---|
9279 | pTarget->m->size = size;
|
---|
9280 | pTarget->m->logicalSize = logicalSize;
|
---|
9281 | pTarget->m->variant = variant;
|
---|
9282 | }
|
---|
9283 | else
|
---|
9284 | {
|
---|
9285 | /* back to NotCreated on failure */
|
---|
9286 | pTarget->m->state = MediumState_NotCreated;
|
---|
9287 |
|
---|
9288 | pTarget->m->autoReset = false;
|
---|
9289 |
|
---|
9290 | /* reset UUID to prevent it from being reused next time */
|
---|
9291 | if (fGenerateUuid)
|
---|
9292 | unconst(pTarget->m->id).clear();
|
---|
9293 | }
|
---|
9294 |
|
---|
9295 | // deregister the task registered in createDiffStorage()
|
---|
9296 | Assert(m->numCreateDiffTasks != 0);
|
---|
9297 | --m->numCreateDiffTasks;
|
---|
9298 |
|
---|
9299 | mediaLock.release();
|
---|
9300 | i_markRegistriesModified();
|
---|
9301 | if (task.isAsync())
|
---|
9302 | {
|
---|
9303 | // in asynchronous mode, save settings now
|
---|
9304 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
9305 | }
|
---|
9306 |
|
---|
9307 | /* Note that in sync mode, it's the caller's responsibility to
|
---|
9308 | * unlock the medium. */
|
---|
9309 |
|
---|
9310 | if (task.NotifyAboutChanges() && SUCCEEDED(mrc))
|
---|
9311 | {
|
---|
9312 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
9313 | m->pVirtualBox->i_onMediumRegistered(m->id, m->devType, TRUE);
|
---|
9314 | }
|
---|
9315 |
|
---|
9316 | return mrc;
|
---|
9317 | }
|
---|
9318 |
|
---|
9319 | /**
|
---|
9320 | * Implementation code for the "merge" task.
|
---|
9321 | *
|
---|
9322 | * This task always gets started from Medium::mergeTo() and can run
|
---|
9323 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
9324 | * that function. If we run synchronously, the caller expects the medium
|
---|
9325 | * registry modification to be set before returning; otherwise (in asynchronous
|
---|
9326 | * mode), we save the settings ourselves.
|
---|
9327 | *
|
---|
9328 | * @param task
|
---|
9329 | * @return
|
---|
9330 | */
|
---|
9331 | HRESULT Medium::i_taskMergeHandler(Medium::MergeTask &task)
|
---|
9332 | {
|
---|
9333 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
9334 | * to lock order violations, it probably causes lock order issues related
|
---|
9335 | * to the AutoCaller usage. */
|
---|
9336 | HRESULT rcTmp = S_OK;
|
---|
9337 |
|
---|
9338 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
9339 |
|
---|
9340 | try
|
---|
9341 | {
|
---|
9342 | if (!task.mParentForTarget.isNull())
|
---|
9343 | if (task.mParentForTarget->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
9344 | {
|
---|
9345 | AutoReadLock plock(task.mParentForTarget COMMA_LOCKVAL_SRC_POS);
|
---|
9346 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
9347 | tr("Cannot merge image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
9348 | task.mParentForTarget->m->strLocationFull.c_str());
|
---|
9349 | }
|
---|
9350 |
|
---|
9351 | // Resize target to source size, if possible. Otherwise throw an error.
|
---|
9352 | // It's offline resizing. Online resizing will be called in the
|
---|
9353 | // SessionMachine::onlineMergeMedium.
|
---|
9354 |
|
---|
9355 | uint64_t sourceSize = 0;
|
---|
9356 | Utf8Str sourceName;
|
---|
9357 | {
|
---|
9358 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9359 | sourceSize = i_getLogicalSize();
|
---|
9360 | sourceName = i_getName();
|
---|
9361 | }
|
---|
9362 | uint64_t targetSize = 0;
|
---|
9363 | Utf8Str targetName;
|
---|
9364 | {
|
---|
9365 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
9366 | targetSize = pTarget->i_getLogicalSize();
|
---|
9367 | targetName = pTarget->i_getName();
|
---|
9368 | }
|
---|
9369 |
|
---|
9370 | //reducing vm disks are not implemented yet
|
---|
9371 | if (sourceSize > targetSize)
|
---|
9372 | {
|
---|
9373 | if (i_isMediumFormatFile())
|
---|
9374 | {
|
---|
9375 | // Have to make own lock list, because "resize" method resizes only last image
|
---|
9376 | // in the lock chain. The lock chain already in the task.mpMediumLockList, so
|
---|
9377 | // just make new lock list based on it. In fact the own lock list neither makes
|
---|
9378 | // double locking of mediums nor unlocks them during delete, because medium
|
---|
9379 | // already locked by task.mpMediumLockList and own list is used just to specify
|
---|
9380 | // what "resize" method should resize.
|
---|
9381 |
|
---|
9382 | MediumLockList* pMediumLockListForResize = new MediumLockList();
|
---|
9383 |
|
---|
9384 | for (MediumLockList::Base::iterator it = task.mpMediumLockList->GetBegin();
|
---|
9385 | it != task.mpMediumLockList->GetEnd();
|
---|
9386 | ++it)
|
---|
9387 | {
|
---|
9388 | ComObjPtr<Medium> pMedium = it->GetMedium();
|
---|
9389 | pMediumLockListForResize->Append(pMedium, pMedium->m->state == MediumState_LockedWrite);
|
---|
9390 | if (pMedium == pTarget)
|
---|
9391 | break;
|
---|
9392 | }
|
---|
9393 |
|
---|
9394 | // just to switch internal state of the lock list to avoid errors during list deletion,
|
---|
9395 | // because all meduims in the list already locked by task.mpMediumLockList
|
---|
9396 | HRESULT rc = pMediumLockListForResize->Lock(true /* fSkipOverLockedMedia */);
|
---|
9397 | if (FAILED(rc))
|
---|
9398 | {
|
---|
9399 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9400 | rc = setError(rc,
|
---|
9401 | tr("Failed to lock the medium '%s' to resize before merge"),
|
---|
9402 | targetName.c_str());
|
---|
9403 | delete pMediumLockListForResize;
|
---|
9404 | throw rc;
|
---|
9405 | }
|
---|
9406 |
|
---|
9407 | ComObjPtr<Progress> pProgress(task.GetProgressObject());
|
---|
9408 | rc = pTarget->i_resize(sourceSize, pMediumLockListForResize, &pProgress, true, false);
|
---|
9409 | if (FAILED(rc))
|
---|
9410 | {
|
---|
9411 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9412 | rc = setError(rc,
|
---|
9413 | tr("Failed to set size of '%s' to size of '%s'"),
|
---|
9414 | targetName.c_str(), sourceName.c_str());
|
---|
9415 | delete pMediumLockListForResize;
|
---|
9416 | throw rc;
|
---|
9417 | }
|
---|
9418 | delete pMediumLockListForResize;
|
---|
9419 | }
|
---|
9420 | else
|
---|
9421 | {
|
---|
9422 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9423 | HRESULT rc = setError(VBOX_E_NOT_SUPPORTED,
|
---|
9424 | tr("Sizes of '%s' and '%s' are different and medium format does not support resing"),
|
---|
9425 | sourceName.c_str(), targetName.c_str());
|
---|
9426 | throw rc;
|
---|
9427 | }
|
---|
9428 | }
|
---|
9429 |
|
---|
9430 | task.GetProgressObject()->SetNextOperation(BstrFmt(tr("Merging medium '%s' to '%s'"),
|
---|
9431 | i_getName().c_str(),
|
---|
9432 | targetName.c_str()).raw(),
|
---|
9433 | 1);
|
---|
9434 |
|
---|
9435 | PVDISK hdd;
|
---|
9436 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9437 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9438 |
|
---|
9439 | try
|
---|
9440 | {
|
---|
9441 | // Similar code appears in SessionMachine::onlineMergeMedium, so
|
---|
9442 | // if you make any changes below check whether they are applicable
|
---|
9443 | // in that context as well.
|
---|
9444 |
|
---|
9445 | unsigned uTargetIdx = VD_LAST_IMAGE;
|
---|
9446 | unsigned uSourceIdx = VD_LAST_IMAGE;
|
---|
9447 | /* Open all media in the chain. */
|
---|
9448 | MediumLockList::Base::iterator lockListBegin =
|
---|
9449 | task.mpMediumLockList->GetBegin();
|
---|
9450 | MediumLockList::Base::iterator lockListEnd =
|
---|
9451 | task.mpMediumLockList->GetEnd();
|
---|
9452 | unsigned i = 0;
|
---|
9453 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
9454 | it != lockListEnd;
|
---|
9455 | ++it)
|
---|
9456 | {
|
---|
9457 | MediumLock &mediumLock = *it;
|
---|
9458 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9459 |
|
---|
9460 | if (pMedium == this)
|
---|
9461 | uSourceIdx = i;
|
---|
9462 | else if (pMedium == pTarget)
|
---|
9463 | uTargetIdx = i;
|
---|
9464 |
|
---|
9465 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9466 |
|
---|
9467 | /*
|
---|
9468 | * complex sanity (sane complexity)
|
---|
9469 | *
|
---|
9470 | * The current medium must be in the Deleting (medium is merged)
|
---|
9471 | * or LockedRead (parent medium) state if it is not the target.
|
---|
9472 | * If it is the target it must be in the LockedWrite state.
|
---|
9473 | */
|
---|
9474 | Assert( ( pMedium != pTarget
|
---|
9475 | && ( pMedium->m->state == MediumState_Deleting
|
---|
9476 | || pMedium->m->state == MediumState_LockedRead))
|
---|
9477 | || ( pMedium == pTarget
|
---|
9478 | && pMedium->m->state == MediumState_LockedWrite));
|
---|
9479 | /*
|
---|
9480 | * Medium must be the target, in the LockedRead state
|
---|
9481 | * or Deleting state where it is not allowed to be attached
|
---|
9482 | * to a virtual machine.
|
---|
9483 | */
|
---|
9484 | Assert( pMedium == pTarget
|
---|
9485 | || pMedium->m->state == MediumState_LockedRead
|
---|
9486 | || ( pMedium->m->backRefs.size() == 0
|
---|
9487 | && pMedium->m->state == MediumState_Deleting));
|
---|
9488 | /* The source medium must be in Deleting state. */
|
---|
9489 | Assert( pMedium != this
|
---|
9490 | || pMedium->m->state == MediumState_Deleting);
|
---|
9491 |
|
---|
9492 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
9493 |
|
---|
9494 | if ( pMedium->m->state == MediumState_LockedRead
|
---|
9495 | || pMedium->m->state == MediumState_Deleting)
|
---|
9496 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
9497 | if (pMedium->m->type == MediumType_Shareable)
|
---|
9498 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
9499 |
|
---|
9500 | /* Open the medium */
|
---|
9501 | vrc = VDOpen(hdd,
|
---|
9502 | pMedium->m->strFormat.c_str(),
|
---|
9503 | pMedium->m->strLocationFull.c_str(),
|
---|
9504 | uOpenFlags | m->uOpenFlagsDef,
|
---|
9505 | pMedium->m->vdImageIfaces);
|
---|
9506 | if (RT_FAILURE(vrc))
|
---|
9507 | throw vrc;
|
---|
9508 |
|
---|
9509 | i++;
|
---|
9510 | }
|
---|
9511 |
|
---|
9512 | ComAssertThrow( uSourceIdx != VD_LAST_IMAGE
|
---|
9513 | && uTargetIdx != VD_LAST_IMAGE, E_FAIL);
|
---|
9514 |
|
---|
9515 | vrc = VDMerge(hdd, uSourceIdx, uTargetIdx,
|
---|
9516 | task.mVDOperationIfaces);
|
---|
9517 | if (RT_FAILURE(vrc))
|
---|
9518 | throw vrc;
|
---|
9519 |
|
---|
9520 | /* update parent UUIDs */
|
---|
9521 | if (!task.mfMergeForward)
|
---|
9522 | {
|
---|
9523 | /* we need to update UUIDs of all source's children
|
---|
9524 | * which cannot be part of the container at once so
|
---|
9525 | * add each one in there individually */
|
---|
9526 | if (task.mpChildrenToReparent)
|
---|
9527 | {
|
---|
9528 | MediumLockList::Base::iterator childrenBegin = task.mpChildrenToReparent->GetBegin();
|
---|
9529 | MediumLockList::Base::iterator childrenEnd = task.mpChildrenToReparent->GetEnd();
|
---|
9530 | for (MediumLockList::Base::iterator it = childrenBegin;
|
---|
9531 | it != childrenEnd;
|
---|
9532 | ++it)
|
---|
9533 | {
|
---|
9534 | Medium *pMedium = it->GetMedium();
|
---|
9535 | /* VD_OPEN_FLAGS_INFO since UUID is wrong yet */
|
---|
9536 | vrc = VDOpen(hdd,
|
---|
9537 | pMedium->m->strFormat.c_str(),
|
---|
9538 | pMedium->m->strLocationFull.c_str(),
|
---|
9539 | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
9540 | pMedium->m->vdImageIfaces);
|
---|
9541 | if (RT_FAILURE(vrc))
|
---|
9542 | throw vrc;
|
---|
9543 |
|
---|
9544 | vrc = VDSetParentUuid(hdd, VD_LAST_IMAGE,
|
---|
9545 | pTarget->m->id.raw());
|
---|
9546 | if (RT_FAILURE(vrc))
|
---|
9547 | throw vrc;
|
---|
9548 |
|
---|
9549 | vrc = VDClose(hdd, false /* fDelete */);
|
---|
9550 | if (RT_FAILURE(vrc))
|
---|
9551 | throw vrc;
|
---|
9552 | }
|
---|
9553 | }
|
---|
9554 | }
|
---|
9555 | }
|
---|
9556 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9557 | catch (int aVRC)
|
---|
9558 | {
|
---|
9559 | rcTmp = setErrorBoth(VBOX_E_FILE_ERROR, aVRC,
|
---|
9560 | tr("Could not merge the medium '%s' to '%s'%s"),
|
---|
9561 | m->strLocationFull.c_str(),
|
---|
9562 | pTarget->m->strLocationFull.c_str(),
|
---|
9563 | i_vdError(aVRC).c_str());
|
---|
9564 | }
|
---|
9565 |
|
---|
9566 | VDDestroy(hdd);
|
---|
9567 | }
|
---|
9568 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9569 |
|
---|
9570 | ErrorInfoKeeper eik;
|
---|
9571 | MultiResult mrc(rcTmp);
|
---|
9572 | HRESULT rc2;
|
---|
9573 |
|
---|
9574 | std::set<ComObjPtr<Medium> > pMediumsForNotify;
|
---|
9575 | std::map<Guid, DeviceType_T> uIdsForNotify;
|
---|
9576 |
|
---|
9577 | if (SUCCEEDED(mrc))
|
---|
9578 | {
|
---|
9579 | /* all media but the target were successfully deleted by
|
---|
9580 | * VDMerge; reparent the last one and uninitialize deleted media. */
|
---|
9581 |
|
---|
9582 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
9583 |
|
---|
9584 | if (task.mfMergeForward)
|
---|
9585 | {
|
---|
9586 | /* first, unregister the target since it may become a base
|
---|
9587 | * medium which needs re-registration */
|
---|
9588 | rc2 = m->pVirtualBox->i_unregisterMedium(pTarget);
|
---|
9589 | AssertComRC(rc2);
|
---|
9590 |
|
---|
9591 | /* then, reparent it and disconnect the deleted branch at both ends
|
---|
9592 | * (chain->parent() is source's parent). Depth check above. */
|
---|
9593 | pTarget->i_deparent();
|
---|
9594 | pTarget->i_setParent(task.mParentForTarget);
|
---|
9595 | if (task.mParentForTarget)
|
---|
9596 | {
|
---|
9597 | i_deparent();
|
---|
9598 | if (task.NotifyAboutChanges())
|
---|
9599 | pMediumsForNotify.insert(task.mParentForTarget);
|
---|
9600 | }
|
---|
9601 |
|
---|
9602 | /* then, register again */
|
---|
9603 | ComObjPtr<Medium> pMedium;
|
---|
9604 | rc2 = m->pVirtualBox->i_registerMedium(pTarget, &pMedium,
|
---|
9605 | treeLock);
|
---|
9606 | AssertComRC(rc2);
|
---|
9607 | }
|
---|
9608 | else
|
---|
9609 | {
|
---|
9610 | Assert(pTarget->i_getChildren().size() == 1);
|
---|
9611 | Medium *targetChild = pTarget->i_getChildren().front();
|
---|
9612 |
|
---|
9613 | /* disconnect the deleted branch at the elder end */
|
---|
9614 | targetChild->i_deparent();
|
---|
9615 |
|
---|
9616 | /* reparent source's children and disconnect the deleted
|
---|
9617 | * branch at the younger end */
|
---|
9618 | if (task.mpChildrenToReparent)
|
---|
9619 | {
|
---|
9620 | /* obey {parent,child} lock order */
|
---|
9621 | AutoWriteLock sourceLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9622 |
|
---|
9623 | MediumLockList::Base::iterator childrenBegin = task.mpChildrenToReparent->GetBegin();
|
---|
9624 | MediumLockList::Base::iterator childrenEnd = task.mpChildrenToReparent->GetEnd();
|
---|
9625 | for (MediumLockList::Base::iterator it = childrenBegin;
|
---|
9626 | it != childrenEnd;
|
---|
9627 | ++it)
|
---|
9628 | {
|
---|
9629 | Medium *pMedium = it->GetMedium();
|
---|
9630 | AutoWriteLock childLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9631 |
|
---|
9632 | pMedium->i_deparent(); // removes pMedium from source
|
---|
9633 | // no depth check, reduces depth
|
---|
9634 | pMedium->i_setParent(pTarget);
|
---|
9635 |
|
---|
9636 | if (task.NotifyAboutChanges())
|
---|
9637 | pMediumsForNotify.insert(pMedium);
|
---|
9638 | }
|
---|
9639 | }
|
---|
9640 | pMediumsForNotify.insert(pTarget);
|
---|
9641 | }
|
---|
9642 |
|
---|
9643 | /* unregister and uninitialize all media removed by the merge */
|
---|
9644 | MediumLockList::Base::iterator lockListBegin =
|
---|
9645 | task.mpMediumLockList->GetBegin();
|
---|
9646 | MediumLockList::Base::iterator lockListEnd =
|
---|
9647 | task.mpMediumLockList->GetEnd();
|
---|
9648 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
9649 | it != lockListEnd;
|
---|
9650 | )
|
---|
9651 | {
|
---|
9652 | MediumLock &mediumLock = *it;
|
---|
9653 | /* Create a real copy of the medium pointer, as the medium
|
---|
9654 | * lock deletion below would invalidate the referenced object. */
|
---|
9655 | const ComObjPtr<Medium> pMedium = mediumLock.GetMedium();
|
---|
9656 |
|
---|
9657 | /* The target and all media not merged (readonly) are skipped */
|
---|
9658 | if ( pMedium == pTarget
|
---|
9659 | || pMedium->m->state == MediumState_LockedRead)
|
---|
9660 | {
|
---|
9661 | ++it;
|
---|
9662 | continue;
|
---|
9663 | }
|
---|
9664 |
|
---|
9665 | uIdsForNotify[pMedium->i_getId()] = pMedium->i_getDeviceType();
|
---|
9666 | rc2 = pMedium->m->pVirtualBox->i_unregisterMedium(pMedium);
|
---|
9667 | AssertComRC(rc2);
|
---|
9668 |
|
---|
9669 | /* now, uninitialize the deleted medium (note that
|
---|
9670 | * due to the Deleting state, uninit() will not touch
|
---|
9671 | * the parent-child relationship so we need to
|
---|
9672 | * uninitialize each disk individually) */
|
---|
9673 |
|
---|
9674 | /* note that the operation initiator medium (which is
|
---|
9675 | * normally also the source medium) is a special case
|
---|
9676 | * -- there is one more caller added by Task to it which
|
---|
9677 | * we must release. Also, if we are in sync mode, the
|
---|
9678 | * caller may still hold an AutoCaller instance for it
|
---|
9679 | * and therefore we cannot uninit() it (it's therefore
|
---|
9680 | * the caller's responsibility) */
|
---|
9681 | if (pMedium == this)
|
---|
9682 | {
|
---|
9683 | Assert(i_getChildren().size() == 0);
|
---|
9684 | Assert(m->backRefs.size() == 0);
|
---|
9685 | task.mMediumCaller.release();
|
---|
9686 | }
|
---|
9687 |
|
---|
9688 | /* Delete the medium lock list entry, which also releases the
|
---|
9689 | * caller added by MergeChain before uninit() and updates the
|
---|
9690 | * iterator to point to the right place. */
|
---|
9691 | rc2 = task.mpMediumLockList->RemoveByIterator(it);
|
---|
9692 | AssertComRC(rc2);
|
---|
9693 |
|
---|
9694 | if (task.isAsync() || pMedium != this)
|
---|
9695 | {
|
---|
9696 | treeLock.release();
|
---|
9697 | pMedium->uninit();
|
---|
9698 | treeLock.acquire();
|
---|
9699 | }
|
---|
9700 | }
|
---|
9701 | }
|
---|
9702 |
|
---|
9703 | i_markRegistriesModified();
|
---|
9704 | if (task.isAsync())
|
---|
9705 | {
|
---|
9706 | // in asynchronous mode, save settings now
|
---|
9707 | eik.restore();
|
---|
9708 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
9709 | eik.fetch();
|
---|
9710 | }
|
---|
9711 |
|
---|
9712 | if (FAILED(mrc))
|
---|
9713 | {
|
---|
9714 | /* Here we come if either VDMerge() failed (in which case we
|
---|
9715 | * assume that it tried to do everything to make a further
|
---|
9716 | * retry possible -- e.g. not deleted intermediate media
|
---|
9717 | * and so on) or VirtualBox::saveRegistries() failed (where we
|
---|
9718 | * should have the original tree but with intermediate storage
|
---|
9719 | * units deleted by VDMerge()). We have to only restore states
|
---|
9720 | * (through the MergeChain dtor) unless we are run synchronously
|
---|
9721 | * in which case it's the responsibility of the caller as stated
|
---|
9722 | * in the mergeTo() docs. The latter also implies that we
|
---|
9723 | * don't own the merge chain, so release it in this case. */
|
---|
9724 | if (task.isAsync())
|
---|
9725 | i_cancelMergeTo(task.mpChildrenToReparent, task.mpMediumLockList);
|
---|
9726 | }
|
---|
9727 | else if (task.NotifyAboutChanges())
|
---|
9728 | {
|
---|
9729 | for (std::set<ComObjPtr<Medium> >::const_iterator it = pMediumsForNotify.begin();
|
---|
9730 | it != pMediumsForNotify.end();
|
---|
9731 | ++it)
|
---|
9732 | {
|
---|
9733 | if (it->isNotNull())
|
---|
9734 | m->pVirtualBox->i_onMediumConfigChanged(*it);
|
---|
9735 | }
|
---|
9736 | for (std::map<Guid, DeviceType_T>::const_iterator it = uIdsForNotify.begin();
|
---|
9737 | it != uIdsForNotify.end();
|
---|
9738 | ++it)
|
---|
9739 | {
|
---|
9740 | m->pVirtualBox->i_onMediumRegistered(it->first, it->second, FALSE);
|
---|
9741 | }
|
---|
9742 | }
|
---|
9743 |
|
---|
9744 | return mrc;
|
---|
9745 | }
|
---|
9746 |
|
---|
9747 | /**
|
---|
9748 | * Implementation code for the "clone" task.
|
---|
9749 | *
|
---|
9750 | * This only gets started from Medium::CloneTo() and always runs asynchronously.
|
---|
9751 | * As a result, we always save the VirtualBox.xml file when we're done here.
|
---|
9752 | *
|
---|
9753 | * @param task
|
---|
9754 | * @return
|
---|
9755 | */
|
---|
9756 | HRESULT Medium::i_taskCloneHandler(Medium::CloneTask &task)
|
---|
9757 | {
|
---|
9758 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
9759 | * to lock order violations, it probably causes lock order issues related
|
---|
9760 | * to the AutoCaller usage. */
|
---|
9761 | HRESULT rcTmp = S_OK;
|
---|
9762 |
|
---|
9763 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
9764 | const ComObjPtr<Medium> &pParent = task.mParent;
|
---|
9765 |
|
---|
9766 | bool fCreatingTarget = false;
|
---|
9767 |
|
---|
9768 | uint64_t size = 0, logicalSize = 0;
|
---|
9769 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
9770 | bool fGenerateUuid = false;
|
---|
9771 |
|
---|
9772 | try
|
---|
9773 | {
|
---|
9774 | if (!pParent.isNull())
|
---|
9775 | {
|
---|
9776 |
|
---|
9777 | if (pParent->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
9778 | {
|
---|
9779 | AutoReadLock plock(pParent COMMA_LOCKVAL_SRC_POS);
|
---|
9780 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
9781 | tr("Cannot clone image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
9782 | pParent->m->strLocationFull.c_str());
|
---|
9783 | }
|
---|
9784 | }
|
---|
9785 |
|
---|
9786 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
9787 | * signal from the task initiator (which releases it only after
|
---|
9788 | * RTThreadCreate()) that we can start the job. */
|
---|
9789 | AutoMultiWriteLock3 thisLock(this, pTarget, pParent COMMA_LOCKVAL_SRC_POS);
|
---|
9790 |
|
---|
9791 | fCreatingTarget = pTarget->m->state == MediumState_Creating;
|
---|
9792 |
|
---|
9793 | /* The object may request a specific UUID (through a special form of
|
---|
9794 | * the moveTo() argument). Otherwise we have to generate it */
|
---|
9795 | Guid targetId = pTarget->m->id;
|
---|
9796 |
|
---|
9797 | fGenerateUuid = targetId.isZero();
|
---|
9798 | if (fGenerateUuid)
|
---|
9799 | {
|
---|
9800 | targetId.create();
|
---|
9801 | /* VirtualBox::registerMedium() will need UUID */
|
---|
9802 | unconst(pTarget->m->id) = targetId;
|
---|
9803 | }
|
---|
9804 |
|
---|
9805 | PVDISK hdd;
|
---|
9806 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9807 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9808 |
|
---|
9809 | try
|
---|
9810 | {
|
---|
9811 | /* Open all media in the source chain. */
|
---|
9812 | MediumLockList::Base::const_iterator sourceListBegin =
|
---|
9813 | task.mpSourceMediumLockList->GetBegin();
|
---|
9814 | MediumLockList::Base::const_iterator sourceListEnd =
|
---|
9815 | task.mpSourceMediumLockList->GetEnd();
|
---|
9816 | for (MediumLockList::Base::const_iterator it = sourceListBegin;
|
---|
9817 | it != sourceListEnd;
|
---|
9818 | ++it)
|
---|
9819 | {
|
---|
9820 | const MediumLock &mediumLock = *it;
|
---|
9821 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9822 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9823 |
|
---|
9824 | /* sanity check */
|
---|
9825 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
9826 |
|
---|
9827 | /** Open all media in read-only mode. */
|
---|
9828 | vrc = VDOpen(hdd,
|
---|
9829 | pMedium->m->strFormat.c_str(),
|
---|
9830 | pMedium->m->strLocationFull.c_str(),
|
---|
9831 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
9832 | pMedium->m->vdImageIfaces);
|
---|
9833 | if (RT_FAILURE(vrc))
|
---|
9834 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9835 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9836 | pMedium->m->strLocationFull.c_str(),
|
---|
9837 | i_vdError(vrc).c_str());
|
---|
9838 | }
|
---|
9839 |
|
---|
9840 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
9841 | Utf8Str targetLocation(pTarget->m->strLocationFull);
|
---|
9842 | uint64_t capabilities = pTarget->m->formatObj->i_getCapabilities();
|
---|
9843 |
|
---|
9844 | Assert( pTarget->m->state == MediumState_Creating
|
---|
9845 | || pTarget->m->state == MediumState_LockedWrite);
|
---|
9846 | Assert(m->state == MediumState_LockedRead);
|
---|
9847 | Assert( pParent.isNull()
|
---|
9848 | || pParent->m->state == MediumState_LockedRead);
|
---|
9849 |
|
---|
9850 | /* unlock before the potentially lengthy operation */
|
---|
9851 | thisLock.release();
|
---|
9852 |
|
---|
9853 | /* ensure the target directory exists */
|
---|
9854 | if (capabilities & MediumFormatCapabilities_File)
|
---|
9855 | {
|
---|
9856 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
9857 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
9858 | if (FAILED(rc))
|
---|
9859 | throw rc;
|
---|
9860 | }
|
---|
9861 |
|
---|
9862 | PVDISK targetHdd;
|
---|
9863 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &targetHdd);
|
---|
9864 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9865 |
|
---|
9866 | try
|
---|
9867 | {
|
---|
9868 | /* Open all media in the target chain. */
|
---|
9869 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
9870 | task.mpTargetMediumLockList->GetBegin();
|
---|
9871 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
9872 | task.mpTargetMediumLockList->GetEnd();
|
---|
9873 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
9874 | it != targetListEnd;
|
---|
9875 | ++it)
|
---|
9876 | {
|
---|
9877 | const MediumLock &mediumLock = *it;
|
---|
9878 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9879 |
|
---|
9880 | /* If the target medium is not created yet there's no
|
---|
9881 | * reason to open it. */
|
---|
9882 | if (pMedium == pTarget && fCreatingTarget)
|
---|
9883 | continue;
|
---|
9884 |
|
---|
9885 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9886 |
|
---|
9887 | /* sanity check */
|
---|
9888 | Assert( pMedium->m->state == MediumState_LockedRead
|
---|
9889 | || pMedium->m->state == MediumState_LockedWrite);
|
---|
9890 |
|
---|
9891 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
9892 | if (pMedium->m->state != MediumState_LockedWrite)
|
---|
9893 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
9894 | if (pMedium->m->type == MediumType_Shareable)
|
---|
9895 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
9896 |
|
---|
9897 | /* Open all media in appropriate mode. */
|
---|
9898 | vrc = VDOpen(targetHdd,
|
---|
9899 | pMedium->m->strFormat.c_str(),
|
---|
9900 | pMedium->m->strLocationFull.c_str(),
|
---|
9901 | uOpenFlags | m->uOpenFlagsDef,
|
---|
9902 | pMedium->m->vdImageIfaces);
|
---|
9903 | if (RT_FAILURE(vrc))
|
---|
9904 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9905 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9906 | pMedium->m->strLocationFull.c_str(),
|
---|
9907 | i_vdError(vrc).c_str());
|
---|
9908 | }
|
---|
9909 |
|
---|
9910 | /* target isn't locked, but no changing data is accessed */
|
---|
9911 | if (task.midxSrcImageSame == UINT32_MAX)
|
---|
9912 | {
|
---|
9913 | vrc = VDCopy(hdd,
|
---|
9914 | VD_LAST_IMAGE,
|
---|
9915 | targetHdd,
|
---|
9916 | targetFormat.c_str(),
|
---|
9917 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
9918 | false /* fMoveByRename */,
|
---|
9919 | task.mTargetLogicalSize /* cbSize */,
|
---|
9920 | task.mVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted | MediumVariant_VmdkESX | MediumVariant_VmdkRawDisk),
|
---|
9921 | targetId.raw(),
|
---|
9922 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
9923 | NULL /* pVDIfsOperation */,
|
---|
9924 | pTarget->m->vdImageIfaces,
|
---|
9925 | task.mVDOperationIfaces);
|
---|
9926 | }
|
---|
9927 | else
|
---|
9928 | {
|
---|
9929 | vrc = VDCopyEx(hdd,
|
---|
9930 | VD_LAST_IMAGE,
|
---|
9931 | targetHdd,
|
---|
9932 | targetFormat.c_str(),
|
---|
9933 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
9934 | false /* fMoveByRename */,
|
---|
9935 | task.mTargetLogicalSize /* cbSize */,
|
---|
9936 | task.midxSrcImageSame,
|
---|
9937 | task.midxDstImageSame,
|
---|
9938 | task.mVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted | MediumVariant_VmdkESX | MediumVariant_VmdkRawDisk),
|
---|
9939 | targetId.raw(),
|
---|
9940 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
9941 | NULL /* pVDIfsOperation */,
|
---|
9942 | pTarget->m->vdImageIfaces,
|
---|
9943 | task.mVDOperationIfaces);
|
---|
9944 | }
|
---|
9945 | if (RT_FAILURE(vrc))
|
---|
9946 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9947 | tr("Could not create the clone medium '%s'%s"),
|
---|
9948 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
9949 |
|
---|
9950 | size = VDGetFileSize(targetHdd, VD_LAST_IMAGE);
|
---|
9951 | logicalSize = VDGetSize(targetHdd, VD_LAST_IMAGE);
|
---|
9952 | unsigned uImageFlags;
|
---|
9953 | vrc = VDGetImageFlags(targetHdd, 0, &uImageFlags);
|
---|
9954 | if (RT_SUCCESS(vrc))
|
---|
9955 | variant = (MediumVariant_T)uImageFlags;
|
---|
9956 | }
|
---|
9957 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9958 |
|
---|
9959 | VDDestroy(targetHdd);
|
---|
9960 | }
|
---|
9961 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9962 |
|
---|
9963 | VDDestroy(hdd);
|
---|
9964 | }
|
---|
9965 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9966 |
|
---|
9967 | ErrorInfoKeeper eik;
|
---|
9968 | MultiResult mrc(rcTmp);
|
---|
9969 |
|
---|
9970 | /* Only do the parent changes for newly created media. */
|
---|
9971 | if (SUCCEEDED(mrc) && fCreatingTarget)
|
---|
9972 | {
|
---|
9973 | /* we set m->pParent & children() */
|
---|
9974 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
9975 |
|
---|
9976 | Assert(pTarget->m->pParent.isNull());
|
---|
9977 |
|
---|
9978 | if (pParent)
|
---|
9979 | {
|
---|
9980 | /* Associate the clone with the parent and deassociate
|
---|
9981 | * from VirtualBox. Depth check above. */
|
---|
9982 | pTarget->i_setParent(pParent);
|
---|
9983 |
|
---|
9984 | /* register with mVirtualBox as the last step and move to
|
---|
9985 | * Created state only on success (leaving an orphan file is
|
---|
9986 | * better than breaking media registry consistency) */
|
---|
9987 | eik.restore();
|
---|
9988 | ComObjPtr<Medium> pMedium;
|
---|
9989 | mrc = pParent->m->pVirtualBox->i_registerMedium(pTarget, &pMedium,
|
---|
9990 | treeLock);
|
---|
9991 | Assert( FAILED(mrc)
|
---|
9992 | || pTarget == pMedium);
|
---|
9993 | eik.fetch();
|
---|
9994 |
|
---|
9995 | if (FAILED(mrc))
|
---|
9996 | /* break parent association on failure to register */
|
---|
9997 | pTarget->i_deparent(); // removes target from parent
|
---|
9998 | }
|
---|
9999 | else
|
---|
10000 | {
|
---|
10001 | /* just register */
|
---|
10002 | eik.restore();
|
---|
10003 | ComObjPtr<Medium> pMedium;
|
---|
10004 | mrc = m->pVirtualBox->i_registerMedium(pTarget, &pMedium,
|
---|
10005 | treeLock);
|
---|
10006 | Assert( FAILED(mrc)
|
---|
10007 | || pTarget == pMedium);
|
---|
10008 | eik.fetch();
|
---|
10009 | }
|
---|
10010 | }
|
---|
10011 |
|
---|
10012 | if (fCreatingTarget)
|
---|
10013 | {
|
---|
10014 | AutoWriteLock mLock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
10015 |
|
---|
10016 | if (SUCCEEDED(mrc))
|
---|
10017 | {
|
---|
10018 | pTarget->m->state = MediumState_Created;
|
---|
10019 |
|
---|
10020 | pTarget->m->size = size;
|
---|
10021 | pTarget->m->logicalSize = logicalSize;
|
---|
10022 | pTarget->m->variant = variant;
|
---|
10023 | }
|
---|
10024 | else
|
---|
10025 | {
|
---|
10026 | /* back to NotCreated on failure */
|
---|
10027 | pTarget->m->state = MediumState_NotCreated;
|
---|
10028 |
|
---|
10029 | /* reset UUID to prevent it from being reused next time */
|
---|
10030 | if (fGenerateUuid)
|
---|
10031 | unconst(pTarget->m->id).clear();
|
---|
10032 | }
|
---|
10033 | }
|
---|
10034 |
|
---|
10035 | /* Copy any filter related settings over to the target. */
|
---|
10036 | if (SUCCEEDED(mrc))
|
---|
10037 | {
|
---|
10038 | /* Copy any filter related settings over. */
|
---|
10039 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
10040 | ComObjPtr<Medium> pTargetBase = pTarget->i_getBase();
|
---|
10041 | std::vector<com::Utf8Str> aFilterPropNames;
|
---|
10042 | std::vector<com::Utf8Str> aFilterPropValues;
|
---|
10043 | mrc = pBase->i_getFilterProperties(aFilterPropNames, aFilterPropValues);
|
---|
10044 | if (SUCCEEDED(mrc))
|
---|
10045 | {
|
---|
10046 | /* Go through the properties and add them to the target medium. */
|
---|
10047 | for (unsigned idx = 0; idx < aFilterPropNames.size(); idx++)
|
---|
10048 | {
|
---|
10049 | mrc = pTargetBase->i_setPropertyDirect(aFilterPropNames[idx], aFilterPropValues[idx]);
|
---|
10050 | if (FAILED(mrc)) break;
|
---|
10051 | }
|
---|
10052 |
|
---|
10053 | // now, at the end of this task (always asynchronous), save the settings
|
---|
10054 | if (SUCCEEDED(mrc))
|
---|
10055 | {
|
---|
10056 | // save the settings
|
---|
10057 | i_markRegistriesModified();
|
---|
10058 | /* collect multiple errors */
|
---|
10059 | eik.restore();
|
---|
10060 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
10061 | eik.fetch();
|
---|
10062 |
|
---|
10063 | if (task.NotifyAboutChanges())
|
---|
10064 | {
|
---|
10065 | if (!fCreatingTarget)
|
---|
10066 | {
|
---|
10067 | if (!aFilterPropNames.empty())
|
---|
10068 | m->pVirtualBox->i_onMediumConfigChanged(pTargetBase);
|
---|
10069 | if (pParent)
|
---|
10070 | m->pVirtualBox->i_onMediumConfigChanged(pParent);
|
---|
10071 | }
|
---|
10072 | else
|
---|
10073 | {
|
---|
10074 | m->pVirtualBox->i_onMediumRegistered(pTarget->i_getId(), pTarget->i_getDeviceType(), TRUE);
|
---|
10075 | }
|
---|
10076 | }
|
---|
10077 | }
|
---|
10078 | }
|
---|
10079 | }
|
---|
10080 |
|
---|
10081 | /* Everything is explicitly unlocked when the task exits,
|
---|
10082 | * as the task destruction also destroys the source chain. */
|
---|
10083 |
|
---|
10084 | /* Make sure the source chain is released early. It could happen
|
---|
10085 | * that we get a deadlock in Appliance::Import when Medium::Close
|
---|
10086 | * is called & the source chain is released at the same time. */
|
---|
10087 | task.mpSourceMediumLockList->Clear();
|
---|
10088 |
|
---|
10089 | return mrc;
|
---|
10090 | }
|
---|
10091 |
|
---|
10092 | /**
|
---|
10093 | * Implementation code for the "move" task.
|
---|
10094 | *
|
---|
10095 | * This only gets started from Medium::MoveTo() and always
|
---|
10096 | * runs asynchronously.
|
---|
10097 | *
|
---|
10098 | * @param task
|
---|
10099 | * @return
|
---|
10100 | */
|
---|
10101 | HRESULT Medium::i_taskMoveHandler(Medium::MoveTask &task)
|
---|
10102 | {
|
---|
10103 | LogFlowFuncEnter();
|
---|
10104 | HRESULT rcOut = S_OK;
|
---|
10105 |
|
---|
10106 | /* pTarget is equal "this" in our case */
|
---|
10107 | const ComObjPtr<Medium> &pTarget = task.mMedium;
|
---|
10108 |
|
---|
10109 | uint64_t size = 0; NOREF(size);
|
---|
10110 | uint64_t logicalSize = 0; NOREF(logicalSize);
|
---|
10111 | MediumVariant_T variant = MediumVariant_Standard; NOREF(variant);
|
---|
10112 |
|
---|
10113 | /*
|
---|
10114 | * it's exactly moving, not cloning
|
---|
10115 | */
|
---|
10116 | if (!i_isMoveOperation(pTarget))
|
---|
10117 | {
|
---|
10118 | HRESULT rc = setError(VBOX_E_FILE_ERROR,
|
---|
10119 | tr("Wrong preconditions for moving the medium %s"),
|
---|
10120 | pTarget->m->strLocationFull.c_str());
|
---|
10121 | LogFlowFunc(("LEAVE: rc=%Rhrc (early)\n", rc));
|
---|
10122 | return rc;
|
---|
10123 | }
|
---|
10124 |
|
---|
10125 | try
|
---|
10126 | {
|
---|
10127 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
10128 | * signal from the task initiator (which releases it only after
|
---|
10129 | * RTThreadCreate()) that we can start the job. */
|
---|
10130 |
|
---|
10131 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
10132 |
|
---|
10133 | PVDISK hdd;
|
---|
10134 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
10135 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
10136 |
|
---|
10137 | try
|
---|
10138 | {
|
---|
10139 | /* Open all media in the source chain. */
|
---|
10140 | MediumLockList::Base::const_iterator sourceListBegin =
|
---|
10141 | task.mpMediumLockList->GetBegin();
|
---|
10142 | MediumLockList::Base::const_iterator sourceListEnd =
|
---|
10143 | task.mpMediumLockList->GetEnd();
|
---|
10144 | for (MediumLockList::Base::const_iterator it = sourceListBegin;
|
---|
10145 | it != sourceListEnd;
|
---|
10146 | ++it)
|
---|
10147 | {
|
---|
10148 | const MediumLock &mediumLock = *it;
|
---|
10149 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
10150 | AutoWriteLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
10151 |
|
---|
10152 | /* sanity check */
|
---|
10153 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
10154 |
|
---|
10155 | vrc = VDOpen(hdd,
|
---|
10156 | pMedium->m->strFormat.c_str(),
|
---|
10157 | pMedium->m->strLocationFull.c_str(),
|
---|
10158 | VD_OPEN_FLAGS_NORMAL,
|
---|
10159 | pMedium->m->vdImageIfaces);
|
---|
10160 | if (RT_FAILURE(vrc))
|
---|
10161 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10162 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
10163 | pMedium->m->strLocationFull.c_str(),
|
---|
10164 | i_vdError(vrc).c_str());
|
---|
10165 | }
|
---|
10166 |
|
---|
10167 | /* we can directly use pTarget->m->"variables" but for better reading we use local copies */
|
---|
10168 | Guid targetId = pTarget->m->id;
|
---|
10169 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
10170 | uint64_t targetCapabilities = pTarget->m->formatObj->i_getCapabilities();
|
---|
10171 |
|
---|
10172 | /*
|
---|
10173 | * change target location
|
---|
10174 | * m->strNewLocationFull has been set already together with m->fMoveThisMedium in
|
---|
10175 | * i_preparationForMoving()
|
---|
10176 | */
|
---|
10177 | Utf8Str targetLocation = i_getNewLocationForMoving();
|
---|
10178 |
|
---|
10179 | /* unlock before the potentially lengthy operation */
|
---|
10180 | thisLock.release();
|
---|
10181 |
|
---|
10182 | /* ensure the target directory exists */
|
---|
10183 | if (targetCapabilities & MediumFormatCapabilities_File)
|
---|
10184 | {
|
---|
10185 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
10186 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
10187 | if (FAILED(rc))
|
---|
10188 | throw rc;
|
---|
10189 | }
|
---|
10190 |
|
---|
10191 | try
|
---|
10192 | {
|
---|
10193 | vrc = VDCopy(hdd,
|
---|
10194 | VD_LAST_IMAGE,
|
---|
10195 | hdd,
|
---|
10196 | targetFormat.c_str(),
|
---|
10197 | targetLocation.c_str(),
|
---|
10198 | true /* fMoveByRename */,
|
---|
10199 | 0 /* cbSize */,
|
---|
10200 | VD_IMAGE_FLAGS_NONE,
|
---|
10201 | targetId.raw(),
|
---|
10202 | VD_OPEN_FLAGS_NORMAL,
|
---|
10203 | NULL /* pVDIfsOperation */,
|
---|
10204 | pTarget->m->vdImageIfaces,
|
---|
10205 | task.mVDOperationIfaces);
|
---|
10206 | if (RT_FAILURE(vrc))
|
---|
10207 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10208 | tr("Could not move medium '%s'%s"),
|
---|
10209 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
10210 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
10211 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
10212 | unsigned uImageFlags;
|
---|
10213 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
10214 | if (RT_SUCCESS(vrc))
|
---|
10215 | variant = (MediumVariant_T)uImageFlags;
|
---|
10216 |
|
---|
10217 | /*
|
---|
10218 | * set current location, because VDCopy\VDCopyEx doesn't do it.
|
---|
10219 | * also reset moving flag
|
---|
10220 | */
|
---|
10221 | i_resetMoveOperationData();
|
---|
10222 | m->strLocationFull = targetLocation;
|
---|
10223 |
|
---|
10224 | }
|
---|
10225 | catch (HRESULT aRC) { rcOut = aRC; }
|
---|
10226 |
|
---|
10227 | }
|
---|
10228 | catch (HRESULT aRC) { rcOut = aRC; }
|
---|
10229 |
|
---|
10230 | VDDestroy(hdd);
|
---|
10231 | }
|
---|
10232 | catch (HRESULT aRC) { rcOut = aRC; }
|
---|
10233 |
|
---|
10234 | ErrorInfoKeeper eik;
|
---|
10235 | MultiResult mrc(rcOut);
|
---|
10236 |
|
---|
10237 | // now, at the end of this task (always asynchronous), save the settings
|
---|
10238 | if (SUCCEEDED(mrc))
|
---|
10239 | {
|
---|
10240 | // save the settings
|
---|
10241 | i_markRegistriesModified();
|
---|
10242 | /* collect multiple errors */
|
---|
10243 | eik.restore();
|
---|
10244 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
10245 | eik.fetch();
|
---|
10246 | }
|
---|
10247 |
|
---|
10248 | /* Everything is explicitly unlocked when the task exits,
|
---|
10249 | * as the task destruction also destroys the source chain. */
|
---|
10250 |
|
---|
10251 | task.mpMediumLockList->Clear();
|
---|
10252 |
|
---|
10253 | if (task.NotifyAboutChanges() && SUCCEEDED(mrc))
|
---|
10254 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
10255 |
|
---|
10256 | LogFlowFunc(("LEAVE: mrc=%Rhrc\n", (HRESULT)mrc));
|
---|
10257 | return mrc;
|
---|
10258 | }
|
---|
10259 |
|
---|
10260 | /**
|
---|
10261 | * Implementation code for the "delete" task.
|
---|
10262 | *
|
---|
10263 | * This task always gets started from Medium::deleteStorage() and can run
|
---|
10264 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
10265 | * that function.
|
---|
10266 | *
|
---|
10267 | * @param task
|
---|
10268 | * @return
|
---|
10269 | */
|
---|
10270 | HRESULT Medium::i_taskDeleteHandler(Medium::DeleteTask &task)
|
---|
10271 | {
|
---|
10272 | NOREF(task);
|
---|
10273 | HRESULT rc = S_OK;
|
---|
10274 |
|
---|
10275 | try
|
---|
10276 | {
|
---|
10277 | /* The lock is also used as a signal from the task initiator (which
|
---|
10278 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
10279 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
10280 |
|
---|
10281 | PVDISK hdd;
|
---|
10282 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
10283 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
10284 |
|
---|
10285 | Utf8Str format(m->strFormat);
|
---|
10286 | Utf8Str location(m->strLocationFull);
|
---|
10287 |
|
---|
10288 | /* unlock before the potentially lengthy operation */
|
---|
10289 | Assert(m->state == MediumState_Deleting);
|
---|
10290 | thisLock.release();
|
---|
10291 |
|
---|
10292 | try
|
---|
10293 | {
|
---|
10294 | vrc = VDOpen(hdd,
|
---|
10295 | format.c_str(),
|
---|
10296 | location.c_str(),
|
---|
10297 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
10298 | m->vdImageIfaces);
|
---|
10299 | if (RT_SUCCESS(vrc))
|
---|
10300 | vrc = VDClose(hdd, true /* fDelete */);
|
---|
10301 |
|
---|
10302 | if (RT_FAILURE(vrc) && vrc != VERR_FILE_NOT_FOUND)
|
---|
10303 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10304 | tr("Could not delete the medium storage unit '%s'%s"),
|
---|
10305 | location.c_str(), i_vdError(vrc).c_str());
|
---|
10306 |
|
---|
10307 | }
|
---|
10308 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10309 |
|
---|
10310 | VDDestroy(hdd);
|
---|
10311 | }
|
---|
10312 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10313 |
|
---|
10314 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
10315 |
|
---|
10316 | /* go to the NotCreated state even on failure since the storage
|
---|
10317 | * may have been already partially deleted and cannot be used any
|
---|
10318 | * more. One will be able to manually re-open the storage if really
|
---|
10319 | * needed to re-register it. */
|
---|
10320 | m->state = MediumState_NotCreated;
|
---|
10321 |
|
---|
10322 | /* Reset UUID to prevent Create* from reusing it again */
|
---|
10323 | com::Guid uOldId = m->id;
|
---|
10324 | unconst(m->id).clear();
|
---|
10325 |
|
---|
10326 | if (task.NotifyAboutChanges() && SUCCEEDED(rc))
|
---|
10327 | {
|
---|
10328 | if (m->pParent.isNotNull())
|
---|
10329 | m->pVirtualBox->i_onMediumConfigChanged(m->pParent);
|
---|
10330 | m->pVirtualBox->i_onMediumRegistered(uOldId, m->devType, FALSE);
|
---|
10331 | }
|
---|
10332 |
|
---|
10333 | return rc;
|
---|
10334 | }
|
---|
10335 |
|
---|
10336 | /**
|
---|
10337 | * Implementation code for the "reset" task.
|
---|
10338 | *
|
---|
10339 | * This always gets started asynchronously from Medium::Reset().
|
---|
10340 | *
|
---|
10341 | * @param task
|
---|
10342 | * @return
|
---|
10343 | */
|
---|
10344 | HRESULT Medium::i_taskResetHandler(Medium::ResetTask &task)
|
---|
10345 | {
|
---|
10346 | HRESULT rc = S_OK;
|
---|
10347 |
|
---|
10348 | uint64_t size = 0, logicalSize = 0;
|
---|
10349 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
10350 |
|
---|
10351 | try
|
---|
10352 | {
|
---|
10353 | /* The lock is also used as a signal from the task initiator (which
|
---|
10354 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
10355 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
10356 |
|
---|
10357 | /// @todo Below we use a pair of delete/create operations to reset
|
---|
10358 | /// the diff contents but the most efficient way will of course be
|
---|
10359 | /// to add a VDResetDiff() API call
|
---|
10360 |
|
---|
10361 | PVDISK hdd;
|
---|
10362 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
10363 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
10364 |
|
---|
10365 | Guid id = m->id;
|
---|
10366 | Utf8Str format(m->strFormat);
|
---|
10367 | Utf8Str location(m->strLocationFull);
|
---|
10368 |
|
---|
10369 | Medium *pParent = m->pParent;
|
---|
10370 | Guid parentId = pParent->m->id;
|
---|
10371 | Utf8Str parentFormat(pParent->m->strFormat);
|
---|
10372 | Utf8Str parentLocation(pParent->m->strLocationFull);
|
---|
10373 |
|
---|
10374 | Assert(m->state == MediumState_LockedWrite);
|
---|
10375 |
|
---|
10376 | /* unlock before the potentially lengthy operation */
|
---|
10377 | thisLock.release();
|
---|
10378 |
|
---|
10379 | try
|
---|
10380 | {
|
---|
10381 | /* Open all media in the target chain but the last. */
|
---|
10382 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
10383 | task.mpMediumLockList->GetBegin();
|
---|
10384 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
10385 | task.mpMediumLockList->GetEnd();
|
---|
10386 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
10387 | it != targetListEnd;
|
---|
10388 | ++it)
|
---|
10389 | {
|
---|
10390 | const MediumLock &mediumLock = *it;
|
---|
10391 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
10392 |
|
---|
10393 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
10394 |
|
---|
10395 | /* sanity check, "this" is checked above */
|
---|
10396 | Assert( pMedium == this
|
---|
10397 | || pMedium->m->state == MediumState_LockedRead);
|
---|
10398 |
|
---|
10399 | /* Open all media in appropriate mode. */
|
---|
10400 | vrc = VDOpen(hdd,
|
---|
10401 | pMedium->m->strFormat.c_str(),
|
---|
10402 | pMedium->m->strLocationFull.c_str(),
|
---|
10403 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
10404 | pMedium->m->vdImageIfaces);
|
---|
10405 | if (RT_FAILURE(vrc))
|
---|
10406 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10407 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
10408 | pMedium->m->strLocationFull.c_str(),
|
---|
10409 | i_vdError(vrc).c_str());
|
---|
10410 |
|
---|
10411 | /* Done when we hit the media which should be reset */
|
---|
10412 | if (pMedium == this)
|
---|
10413 | break;
|
---|
10414 | }
|
---|
10415 |
|
---|
10416 | /* first, delete the storage unit */
|
---|
10417 | vrc = VDClose(hdd, true /* fDelete */);
|
---|
10418 | if (RT_FAILURE(vrc))
|
---|
10419 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10420 | tr("Could not delete the medium storage unit '%s'%s"),
|
---|
10421 | location.c_str(), i_vdError(vrc).c_str());
|
---|
10422 |
|
---|
10423 | /* next, create it again */
|
---|
10424 | vrc = VDOpen(hdd,
|
---|
10425 | parentFormat.c_str(),
|
---|
10426 | parentLocation.c_str(),
|
---|
10427 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
10428 | m->vdImageIfaces);
|
---|
10429 | if (RT_FAILURE(vrc))
|
---|
10430 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10431 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
10432 | parentLocation.c_str(), i_vdError(vrc).c_str());
|
---|
10433 |
|
---|
10434 | vrc = VDCreateDiff(hdd,
|
---|
10435 | format.c_str(),
|
---|
10436 | location.c_str(),
|
---|
10437 | /// @todo use the same medium variant as before
|
---|
10438 | VD_IMAGE_FLAGS_NONE,
|
---|
10439 | NULL,
|
---|
10440 | id.raw(),
|
---|
10441 | parentId.raw(),
|
---|
10442 | VD_OPEN_FLAGS_NORMAL,
|
---|
10443 | m->vdImageIfaces,
|
---|
10444 | task.mVDOperationIfaces);
|
---|
10445 | if (RT_FAILURE(vrc))
|
---|
10446 | {
|
---|
10447 | if (vrc == VERR_VD_INVALID_TYPE)
|
---|
10448 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10449 | tr("Parameters for creating the differencing medium storage unit '%s' are invalid%s"),
|
---|
10450 | location.c_str(), i_vdError(vrc).c_str());
|
---|
10451 | else
|
---|
10452 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10453 | tr("Could not create the differencing medium storage unit '%s'%s"),
|
---|
10454 | location.c_str(), i_vdError(vrc).c_str());
|
---|
10455 | }
|
---|
10456 |
|
---|
10457 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
10458 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
10459 | unsigned uImageFlags;
|
---|
10460 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
10461 | if (RT_SUCCESS(vrc))
|
---|
10462 | variant = (MediumVariant_T)uImageFlags;
|
---|
10463 | }
|
---|
10464 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10465 |
|
---|
10466 | VDDestroy(hdd);
|
---|
10467 | }
|
---|
10468 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10469 |
|
---|
10470 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
10471 |
|
---|
10472 | m->size = size;
|
---|
10473 | m->logicalSize = logicalSize;
|
---|
10474 | m->variant = variant;
|
---|
10475 |
|
---|
10476 | if (task.NotifyAboutChanges() && SUCCEEDED(rc))
|
---|
10477 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
10478 |
|
---|
10479 | /* Everything is explicitly unlocked when the task exits,
|
---|
10480 | * as the task destruction also destroys the media chain. */
|
---|
10481 |
|
---|
10482 | return rc;
|
---|
10483 | }
|
---|
10484 |
|
---|
10485 | /**
|
---|
10486 | * Implementation code for the "compact" task.
|
---|
10487 | *
|
---|
10488 | * @param task
|
---|
10489 | * @return
|
---|
10490 | */
|
---|
10491 | HRESULT Medium::i_taskCompactHandler(Medium::CompactTask &task)
|
---|
10492 | {
|
---|
10493 | HRESULT rc = S_OK;
|
---|
10494 |
|
---|
10495 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
10496 | * signal from the task initiator (which releases it only after
|
---|
10497 | * RTThreadCreate()) that we can start the job. */
|
---|
10498 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
10499 |
|
---|
10500 | try
|
---|
10501 | {
|
---|
10502 | PVDISK hdd;
|
---|
10503 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
10504 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
10505 |
|
---|
10506 | try
|
---|
10507 | {
|
---|
10508 | /* Open all media in the chain. */
|
---|
10509 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
10510 | task.mpMediumLockList->GetBegin();
|
---|
10511 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
10512 | task.mpMediumLockList->GetEnd();
|
---|
10513 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
10514 | mediumListEnd;
|
---|
10515 | --mediumListLast;
|
---|
10516 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
10517 | it != mediumListEnd;
|
---|
10518 | ++it)
|
---|
10519 | {
|
---|
10520 | const MediumLock &mediumLock = *it;
|
---|
10521 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
10522 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
10523 |
|
---|
10524 | /* sanity check */
|
---|
10525 | if (it == mediumListLast)
|
---|
10526 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
10527 | else
|
---|
10528 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
10529 |
|
---|
10530 | /* Open all media but last in read-only mode. Do not handle
|
---|
10531 | * shareable media, as compaction and sharing are mutually
|
---|
10532 | * exclusive. */
|
---|
10533 | vrc = VDOpen(hdd,
|
---|
10534 | pMedium->m->strFormat.c_str(),
|
---|
10535 | pMedium->m->strLocationFull.c_str(),
|
---|
10536 | m->uOpenFlagsDef | (it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
10537 | pMedium->m->vdImageIfaces);
|
---|
10538 | if (RT_FAILURE(vrc))
|
---|
10539 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10540 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
10541 | pMedium->m->strLocationFull.c_str(),
|
---|
10542 | i_vdError(vrc).c_str());
|
---|
10543 | }
|
---|
10544 |
|
---|
10545 | Assert(m->state == MediumState_LockedWrite);
|
---|
10546 |
|
---|
10547 | Utf8Str location(m->strLocationFull);
|
---|
10548 |
|
---|
10549 | /* unlock before the potentially lengthy operation */
|
---|
10550 | thisLock.release();
|
---|
10551 |
|
---|
10552 | vrc = VDCompact(hdd, VD_LAST_IMAGE, task.mVDOperationIfaces);
|
---|
10553 | if (RT_FAILURE(vrc))
|
---|
10554 | {
|
---|
10555 | if (vrc == VERR_NOT_SUPPORTED)
|
---|
10556 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
10557 | tr("Compacting is not yet supported for medium '%s'"),
|
---|
10558 | location.c_str());
|
---|
10559 | else if (vrc == VERR_NOT_IMPLEMENTED)
|
---|
10560 | throw setErrorBoth(E_NOTIMPL, vrc,
|
---|
10561 | tr("Compacting is not implemented, medium '%s'"),
|
---|
10562 | location.c_str());
|
---|
10563 | else
|
---|
10564 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10565 | tr("Could not compact medium '%s'%s"),
|
---|
10566 | location.c_str(),
|
---|
10567 | i_vdError(vrc).c_str());
|
---|
10568 | }
|
---|
10569 | }
|
---|
10570 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10571 |
|
---|
10572 | VDDestroy(hdd);
|
---|
10573 | }
|
---|
10574 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10575 |
|
---|
10576 | if (task.NotifyAboutChanges() && SUCCEEDED(rc))
|
---|
10577 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
10578 |
|
---|
10579 | /* Everything is explicitly unlocked when the task exits,
|
---|
10580 | * as the task destruction also destroys the media chain. */
|
---|
10581 |
|
---|
10582 | return rc;
|
---|
10583 | }
|
---|
10584 |
|
---|
10585 | /**
|
---|
10586 | * Implementation code for the "resize" task.
|
---|
10587 | *
|
---|
10588 | * @param task
|
---|
10589 | * @return
|
---|
10590 | */
|
---|
10591 | HRESULT Medium::i_taskResizeHandler(Medium::ResizeTask &task)
|
---|
10592 | {
|
---|
10593 | HRESULT rc = S_OK;
|
---|
10594 |
|
---|
10595 | uint64_t size = 0, logicalSize = 0;
|
---|
10596 |
|
---|
10597 | try
|
---|
10598 | {
|
---|
10599 | /* The lock is also used as a signal from the task initiator (which
|
---|
10600 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
10601 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
10602 |
|
---|
10603 | PVDISK hdd;
|
---|
10604 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
10605 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
10606 |
|
---|
10607 | try
|
---|
10608 | {
|
---|
10609 | /* Open all media in the chain. */
|
---|
10610 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
10611 | task.mpMediumLockList->GetBegin();
|
---|
10612 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
10613 | task.mpMediumLockList->GetEnd();
|
---|
10614 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
10615 | mediumListEnd;
|
---|
10616 | --mediumListLast;
|
---|
10617 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
10618 | it != mediumListEnd;
|
---|
10619 | ++it)
|
---|
10620 | {
|
---|
10621 | const MediumLock &mediumLock = *it;
|
---|
10622 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
10623 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
10624 |
|
---|
10625 | /* sanity check */
|
---|
10626 | if (it == mediumListLast)
|
---|
10627 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
10628 | else
|
---|
10629 | Assert(pMedium->m->state == MediumState_LockedRead ||
|
---|
10630 | // Allow resize the target image during mergeTo in case
|
---|
10631 | // of direction from parent to child because all intermediate
|
---|
10632 | // images are marked to MediumState_Deleting and will be
|
---|
10633 | // destroyed after successful merge
|
---|
10634 | pMedium->m->state == MediumState_Deleting);
|
---|
10635 |
|
---|
10636 | /* Open all media but last in read-only mode. Do not handle
|
---|
10637 | * shareable media, as compaction and sharing are mutually
|
---|
10638 | * exclusive. */
|
---|
10639 | vrc = VDOpen(hdd,
|
---|
10640 | pMedium->m->strFormat.c_str(),
|
---|
10641 | pMedium->m->strLocationFull.c_str(),
|
---|
10642 | m->uOpenFlagsDef | (it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
10643 | pMedium->m->vdImageIfaces);
|
---|
10644 | if (RT_FAILURE(vrc))
|
---|
10645 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10646 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
10647 | pMedium->m->strLocationFull.c_str(),
|
---|
10648 | i_vdError(vrc).c_str());
|
---|
10649 | }
|
---|
10650 |
|
---|
10651 | Assert(m->state == MediumState_LockedWrite);
|
---|
10652 |
|
---|
10653 | Utf8Str location(m->strLocationFull);
|
---|
10654 |
|
---|
10655 | /* unlock before the potentially lengthy operation */
|
---|
10656 | thisLock.release();
|
---|
10657 |
|
---|
10658 | VDGEOMETRY geo = {0, 0, 0}; /* auto */
|
---|
10659 | vrc = VDResize(hdd, task.mSize, &geo, &geo, task.mVDOperationIfaces);
|
---|
10660 | if (RT_FAILURE(vrc))
|
---|
10661 | {
|
---|
10662 | if (vrc == VERR_VD_SHRINK_NOT_SUPPORTED)
|
---|
10663 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
10664 | tr("Shrinking is not yet supported for medium '%s'"),
|
---|
10665 | location.c_str());
|
---|
10666 | if (vrc == VERR_NOT_SUPPORTED)
|
---|
10667 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
10668 | tr("Resizing to new size %llu is not yet supported for medium '%s'"),
|
---|
10669 | task.mSize, location.c_str());
|
---|
10670 | else if (vrc == VERR_NOT_IMPLEMENTED)
|
---|
10671 | throw setErrorBoth(E_NOTIMPL, vrc,
|
---|
10672 | tr("Resizing is not implemented, medium '%s'"),
|
---|
10673 | location.c_str());
|
---|
10674 | else
|
---|
10675 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10676 | tr("Could not resize medium '%s'%s"),
|
---|
10677 | location.c_str(),
|
---|
10678 | i_vdError(vrc).c_str());
|
---|
10679 | }
|
---|
10680 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
10681 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
10682 | }
|
---|
10683 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10684 |
|
---|
10685 | VDDestroy(hdd);
|
---|
10686 | }
|
---|
10687 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10688 |
|
---|
10689 | if (SUCCEEDED(rc))
|
---|
10690 | {
|
---|
10691 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
10692 | m->size = size;
|
---|
10693 | m->logicalSize = logicalSize;
|
---|
10694 |
|
---|
10695 | if (task.NotifyAboutChanges())
|
---|
10696 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
10697 | }
|
---|
10698 |
|
---|
10699 | /* Everything is explicitly unlocked when the task exits,
|
---|
10700 | * as the task destruction also destroys the media chain. */
|
---|
10701 |
|
---|
10702 | return rc;
|
---|
10703 | }
|
---|
10704 |
|
---|
10705 | /**
|
---|
10706 | * Implementation code for the "import" task.
|
---|
10707 | *
|
---|
10708 | * This only gets started from Medium::importFile() and always runs
|
---|
10709 | * asynchronously. It potentially touches the media registry, so we
|
---|
10710 | * always save the VirtualBox.xml file when we're done here.
|
---|
10711 | *
|
---|
10712 | * @param task
|
---|
10713 | * @return
|
---|
10714 | */
|
---|
10715 | HRESULT Medium::i_taskImportHandler(Medium::ImportTask &task)
|
---|
10716 | {
|
---|
10717 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
10718 | * to lock order violations, it probably causes lock order issues related
|
---|
10719 | * to the AutoCaller usage. */
|
---|
10720 | HRESULT rcTmp = S_OK;
|
---|
10721 |
|
---|
10722 | const ComObjPtr<Medium> &pParent = task.mParent;
|
---|
10723 |
|
---|
10724 | bool fCreatingTarget = false;
|
---|
10725 |
|
---|
10726 | uint64_t size = 0, logicalSize = 0;
|
---|
10727 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
10728 | bool fGenerateUuid = false;
|
---|
10729 |
|
---|
10730 | try
|
---|
10731 | {
|
---|
10732 | if (!pParent.isNull())
|
---|
10733 | if (pParent->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
10734 | {
|
---|
10735 | AutoReadLock plock(pParent COMMA_LOCKVAL_SRC_POS);
|
---|
10736 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10737 | tr("Cannot import image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
10738 | pParent->m->strLocationFull.c_str());
|
---|
10739 | }
|
---|
10740 |
|
---|
10741 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
10742 | * signal from the task initiator (which releases it only after
|
---|
10743 | * RTThreadCreate()) that we can start the job. */
|
---|
10744 | AutoMultiWriteLock2 thisLock(this, pParent COMMA_LOCKVAL_SRC_POS);
|
---|
10745 |
|
---|
10746 | fCreatingTarget = m->state == MediumState_Creating;
|
---|
10747 |
|
---|
10748 | /* The object may request a specific UUID (through a special form of
|
---|
10749 | * the moveTo() argument). Otherwise we have to generate it */
|
---|
10750 | Guid targetId = m->id;
|
---|
10751 |
|
---|
10752 | fGenerateUuid = targetId.isZero();
|
---|
10753 | if (fGenerateUuid)
|
---|
10754 | {
|
---|
10755 | targetId.create();
|
---|
10756 | /* VirtualBox::i_registerMedium() will need UUID */
|
---|
10757 | unconst(m->id) = targetId;
|
---|
10758 | }
|
---|
10759 |
|
---|
10760 |
|
---|
10761 | PVDISK hdd;
|
---|
10762 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
10763 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
10764 |
|
---|
10765 | try
|
---|
10766 | {
|
---|
10767 | /* Open source medium. */
|
---|
10768 | vrc = VDOpen(hdd,
|
---|
10769 | task.mFormat->i_getId().c_str(),
|
---|
10770 | task.mFilename.c_str(),
|
---|
10771 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SEQUENTIAL | m->uOpenFlagsDef,
|
---|
10772 | task.mVDImageIfaces);
|
---|
10773 | if (RT_FAILURE(vrc))
|
---|
10774 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10775 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
10776 | task.mFilename.c_str(),
|
---|
10777 | i_vdError(vrc).c_str());
|
---|
10778 |
|
---|
10779 | Utf8Str targetFormat(m->strFormat);
|
---|
10780 | Utf8Str targetLocation(m->strLocationFull);
|
---|
10781 | uint64_t capabilities = task.mFormat->i_getCapabilities();
|
---|
10782 |
|
---|
10783 | Assert( m->state == MediumState_Creating
|
---|
10784 | || m->state == MediumState_LockedWrite);
|
---|
10785 | Assert( pParent.isNull()
|
---|
10786 | || pParent->m->state == MediumState_LockedRead);
|
---|
10787 |
|
---|
10788 | /* unlock before the potentially lengthy operation */
|
---|
10789 | thisLock.release();
|
---|
10790 |
|
---|
10791 | /* ensure the target directory exists */
|
---|
10792 | if (capabilities & MediumFormatCapabilities_File)
|
---|
10793 | {
|
---|
10794 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
10795 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
10796 | if (FAILED(rc))
|
---|
10797 | throw rc;
|
---|
10798 | }
|
---|
10799 |
|
---|
10800 | PVDISK targetHdd;
|
---|
10801 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &targetHdd);
|
---|
10802 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
10803 |
|
---|
10804 | try
|
---|
10805 | {
|
---|
10806 | /* Open all media in the target chain. */
|
---|
10807 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
10808 | task.mpTargetMediumLockList->GetBegin();
|
---|
10809 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
10810 | task.mpTargetMediumLockList->GetEnd();
|
---|
10811 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
10812 | it != targetListEnd;
|
---|
10813 | ++it)
|
---|
10814 | {
|
---|
10815 | const MediumLock &mediumLock = *it;
|
---|
10816 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
10817 |
|
---|
10818 | /* If the target medium is not created yet there's no
|
---|
10819 | * reason to open it. */
|
---|
10820 | if (pMedium == this && fCreatingTarget)
|
---|
10821 | continue;
|
---|
10822 |
|
---|
10823 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
10824 |
|
---|
10825 | /* sanity check */
|
---|
10826 | Assert( pMedium->m->state == MediumState_LockedRead
|
---|
10827 | || pMedium->m->state == MediumState_LockedWrite);
|
---|
10828 |
|
---|
10829 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
10830 | if (pMedium->m->state != MediumState_LockedWrite)
|
---|
10831 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
10832 | if (pMedium->m->type == MediumType_Shareable)
|
---|
10833 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
10834 |
|
---|
10835 | /* Open all media in appropriate mode. */
|
---|
10836 | vrc = VDOpen(targetHdd,
|
---|
10837 | pMedium->m->strFormat.c_str(),
|
---|
10838 | pMedium->m->strLocationFull.c_str(),
|
---|
10839 | uOpenFlags | m->uOpenFlagsDef,
|
---|
10840 | pMedium->m->vdImageIfaces);
|
---|
10841 | if (RT_FAILURE(vrc))
|
---|
10842 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10843 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
10844 | pMedium->m->strLocationFull.c_str(),
|
---|
10845 | i_vdError(vrc).c_str());
|
---|
10846 | }
|
---|
10847 |
|
---|
10848 | vrc = VDCopy(hdd,
|
---|
10849 | VD_LAST_IMAGE,
|
---|
10850 | targetHdd,
|
---|
10851 | targetFormat.c_str(),
|
---|
10852 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
10853 | false /* fMoveByRename */,
|
---|
10854 | 0 /* cbSize */,
|
---|
10855 | task.mVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted | MediumVariant_VmdkESX | MediumVariant_VmdkRawDisk),
|
---|
10856 | targetId.raw(),
|
---|
10857 | VD_OPEN_FLAGS_NORMAL,
|
---|
10858 | NULL /* pVDIfsOperation */,
|
---|
10859 | m->vdImageIfaces,
|
---|
10860 | task.mVDOperationIfaces);
|
---|
10861 | if (RT_FAILURE(vrc))
|
---|
10862 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10863 | tr("Could not create the imported medium '%s'%s"),
|
---|
10864 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
10865 |
|
---|
10866 | size = VDGetFileSize(targetHdd, VD_LAST_IMAGE);
|
---|
10867 | logicalSize = VDGetSize(targetHdd, VD_LAST_IMAGE);
|
---|
10868 | unsigned uImageFlags;
|
---|
10869 | vrc = VDGetImageFlags(targetHdd, 0, &uImageFlags);
|
---|
10870 | if (RT_SUCCESS(vrc))
|
---|
10871 | variant = (MediumVariant_T)uImageFlags;
|
---|
10872 | }
|
---|
10873 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
10874 |
|
---|
10875 | VDDestroy(targetHdd);
|
---|
10876 | }
|
---|
10877 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
10878 |
|
---|
10879 | VDDestroy(hdd);
|
---|
10880 | }
|
---|
10881 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
10882 |
|
---|
10883 | ErrorInfoKeeper eik;
|
---|
10884 | MultiResult mrc(rcTmp);
|
---|
10885 |
|
---|
10886 | /* Only do the parent changes for newly created media. */
|
---|
10887 | if (SUCCEEDED(mrc) && fCreatingTarget)
|
---|
10888 | {
|
---|
10889 | /* we set m->pParent & children() */
|
---|
10890 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
10891 |
|
---|
10892 | Assert(m->pParent.isNull());
|
---|
10893 |
|
---|
10894 | if (pParent)
|
---|
10895 | {
|
---|
10896 | /* Associate the imported medium with the parent and deassociate
|
---|
10897 | * from VirtualBox. Depth check above. */
|
---|
10898 | i_setParent(pParent);
|
---|
10899 |
|
---|
10900 | /* register with mVirtualBox as the last step and move to
|
---|
10901 | * Created state only on success (leaving an orphan file is
|
---|
10902 | * better than breaking media registry consistency) */
|
---|
10903 | eik.restore();
|
---|
10904 | ComObjPtr<Medium> pMedium;
|
---|
10905 | mrc = pParent->m->pVirtualBox->i_registerMedium(this, &pMedium,
|
---|
10906 | treeLock);
|
---|
10907 | Assert(this == pMedium);
|
---|
10908 | eik.fetch();
|
---|
10909 |
|
---|
10910 | if (FAILED(mrc))
|
---|
10911 | /* break parent association on failure to register */
|
---|
10912 | this->i_deparent(); // removes target from parent
|
---|
10913 | }
|
---|
10914 | else
|
---|
10915 | {
|
---|
10916 | /* just register */
|
---|
10917 | eik.restore();
|
---|
10918 | ComObjPtr<Medium> pMedium;
|
---|
10919 | mrc = m->pVirtualBox->i_registerMedium(this, &pMedium, treeLock);
|
---|
10920 | Assert(this == pMedium);
|
---|
10921 | eik.fetch();
|
---|
10922 | }
|
---|
10923 | }
|
---|
10924 |
|
---|
10925 | if (fCreatingTarget)
|
---|
10926 | {
|
---|
10927 | AutoWriteLock mLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
10928 |
|
---|
10929 | if (SUCCEEDED(mrc))
|
---|
10930 | {
|
---|
10931 | m->state = MediumState_Created;
|
---|
10932 |
|
---|
10933 | m->size = size;
|
---|
10934 | m->logicalSize = logicalSize;
|
---|
10935 | m->variant = variant;
|
---|
10936 | }
|
---|
10937 | else
|
---|
10938 | {
|
---|
10939 | /* back to NotCreated on failure */
|
---|
10940 | m->state = MediumState_NotCreated;
|
---|
10941 |
|
---|
10942 | /* reset UUID to prevent it from being reused next time */
|
---|
10943 | if (fGenerateUuid)
|
---|
10944 | unconst(m->id).clear();
|
---|
10945 | }
|
---|
10946 | }
|
---|
10947 |
|
---|
10948 | // now, at the end of this task (always asynchronous), save the settings
|
---|
10949 | {
|
---|
10950 | // save the settings
|
---|
10951 | i_markRegistriesModified();
|
---|
10952 | /* collect multiple errors */
|
---|
10953 | eik.restore();
|
---|
10954 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
10955 | eik.fetch();
|
---|
10956 | }
|
---|
10957 |
|
---|
10958 | /* Everything is explicitly unlocked when the task exits,
|
---|
10959 | * as the task destruction also destroys the target chain. */
|
---|
10960 |
|
---|
10961 | /* Make sure the target chain is released early, otherwise it can
|
---|
10962 | * lead to deadlocks with concurrent IAppliance activities. */
|
---|
10963 | task.mpTargetMediumLockList->Clear();
|
---|
10964 |
|
---|
10965 | if (task.NotifyAboutChanges() && SUCCEEDED(mrc))
|
---|
10966 | {
|
---|
10967 | if (pParent)
|
---|
10968 | m->pVirtualBox->i_onMediumConfigChanged(pParent);
|
---|
10969 | if (fCreatingTarget)
|
---|
10970 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
10971 | else
|
---|
10972 | m->pVirtualBox->i_onMediumRegistered(m->id, m->devType, TRUE);
|
---|
10973 | }
|
---|
10974 |
|
---|
10975 | return mrc;
|
---|
10976 | }
|
---|
10977 |
|
---|
10978 | /**
|
---|
10979 | * Sets up the encryption settings for a filter.
|
---|
10980 | */
|
---|
10981 | void Medium::i_taskEncryptSettingsSetup(MediumCryptoFilterSettings *pSettings, const char *pszCipher,
|
---|
10982 | const char *pszKeyStore, const char *pszPassword,
|
---|
10983 | bool fCreateKeyStore)
|
---|
10984 | {
|
---|
10985 | pSettings->pszCipher = pszCipher;
|
---|
10986 | pSettings->pszPassword = pszPassword;
|
---|
10987 | pSettings->pszKeyStoreLoad = pszKeyStore;
|
---|
10988 | pSettings->fCreateKeyStore = fCreateKeyStore;
|
---|
10989 | pSettings->pbDek = NULL;
|
---|
10990 | pSettings->cbDek = 0;
|
---|
10991 | pSettings->vdFilterIfaces = NULL;
|
---|
10992 |
|
---|
10993 | pSettings->vdIfCfg.pfnAreKeysValid = i_vdCryptoConfigAreKeysValid;
|
---|
10994 | pSettings->vdIfCfg.pfnQuerySize = i_vdCryptoConfigQuerySize;
|
---|
10995 | pSettings->vdIfCfg.pfnQuery = i_vdCryptoConfigQuery;
|
---|
10996 | pSettings->vdIfCfg.pfnQueryBytes = NULL;
|
---|
10997 |
|
---|
10998 | pSettings->vdIfCrypto.pfnKeyRetain = i_vdCryptoKeyRetain;
|
---|
10999 | pSettings->vdIfCrypto.pfnKeyRelease = i_vdCryptoKeyRelease;
|
---|
11000 | pSettings->vdIfCrypto.pfnKeyStorePasswordRetain = i_vdCryptoKeyStorePasswordRetain;
|
---|
11001 | pSettings->vdIfCrypto.pfnKeyStorePasswordRelease = i_vdCryptoKeyStorePasswordRelease;
|
---|
11002 | pSettings->vdIfCrypto.pfnKeyStoreSave = i_vdCryptoKeyStoreSave;
|
---|
11003 | pSettings->vdIfCrypto.pfnKeyStoreReturnParameters = i_vdCryptoKeyStoreReturnParameters;
|
---|
11004 |
|
---|
11005 | int vrc = VDInterfaceAdd(&pSettings->vdIfCfg.Core,
|
---|
11006 | "Medium::vdInterfaceCfgCrypto",
|
---|
11007 | VDINTERFACETYPE_CONFIG, pSettings,
|
---|
11008 | sizeof(VDINTERFACECONFIG), &pSettings->vdFilterIfaces);
|
---|
11009 | AssertRC(vrc);
|
---|
11010 |
|
---|
11011 | vrc = VDInterfaceAdd(&pSettings->vdIfCrypto.Core,
|
---|
11012 | "Medium::vdInterfaceCrypto",
|
---|
11013 | VDINTERFACETYPE_CRYPTO, pSettings,
|
---|
11014 | sizeof(VDINTERFACECRYPTO), &pSettings->vdFilterIfaces);
|
---|
11015 | AssertRC(vrc);
|
---|
11016 | }
|
---|
11017 |
|
---|
11018 | /**
|
---|
11019 | * Implementation code for the "encrypt" task.
|
---|
11020 | *
|
---|
11021 | * @param task
|
---|
11022 | * @return
|
---|
11023 | */
|
---|
11024 | HRESULT Medium::i_taskEncryptHandler(Medium::EncryptTask &task)
|
---|
11025 | {
|
---|
11026 | # ifndef VBOX_WITH_EXTPACK
|
---|
11027 | RT_NOREF(task);
|
---|
11028 | # endif
|
---|
11029 | HRESULT rc = S_OK;
|
---|
11030 |
|
---|
11031 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
11032 | * signal from the task initiator (which releases it only after
|
---|
11033 | * RTThreadCreate()) that we can start the job. */
|
---|
11034 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
11035 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
11036 |
|
---|
11037 | try
|
---|
11038 | {
|
---|
11039 | # ifdef VBOX_WITH_EXTPACK
|
---|
11040 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
11041 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
11042 | {
|
---|
11043 | /* Load the plugin */
|
---|
11044 | Utf8Str strPlugin;
|
---|
11045 | rc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
11046 | if (SUCCEEDED(rc))
|
---|
11047 | {
|
---|
11048 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
11049 | if (RT_FAILURE(vrc))
|
---|
11050 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
11051 | tr("Encrypting the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
11052 | i_vdError(vrc).c_str());
|
---|
11053 | }
|
---|
11054 | else
|
---|
11055 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
11056 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
11057 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
11058 | }
|
---|
11059 | else
|
---|
11060 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
11061 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
11062 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
11063 |
|
---|
11064 | PVDISK pDisk = NULL;
|
---|
11065 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
11066 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
11067 |
|
---|
11068 | MediumCryptoFilterSettings CryptoSettingsRead;
|
---|
11069 | MediumCryptoFilterSettings CryptoSettingsWrite;
|
---|
11070 |
|
---|
11071 | void *pvBuf = NULL;
|
---|
11072 | const char *pszPasswordNew = NULL;
|
---|
11073 | try
|
---|
11074 | {
|
---|
11075 | /* Set up disk encryption filters. */
|
---|
11076 | if (task.mstrCurrentPassword.isEmpty())
|
---|
11077 | {
|
---|
11078 | /*
|
---|
11079 | * Query whether the medium property indicating that encryption is
|
---|
11080 | * configured is existing.
|
---|
11081 | */
|
---|
11082 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
11083 | if (it != pBase->m->mapProperties.end())
|
---|
11084 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
11085 | tr("The password given for the encrypted image is incorrect"));
|
---|
11086 | }
|
---|
11087 | else
|
---|
11088 | {
|
---|
11089 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
11090 | if (it == pBase->m->mapProperties.end())
|
---|
11091 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
11092 | tr("The image is not configured for encryption"));
|
---|
11093 |
|
---|
11094 | i_taskEncryptSettingsSetup(&CryptoSettingsRead, NULL, it->second.c_str(), task.mstrCurrentPassword.c_str(),
|
---|
11095 | false /* fCreateKeyStore */);
|
---|
11096 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ, CryptoSettingsRead.vdFilterIfaces);
|
---|
11097 | if (vrc == VERR_VD_PASSWORD_INCORRECT)
|
---|
11098 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
11099 | tr("The password to decrypt the image is incorrect"));
|
---|
11100 | else if (RT_FAILURE(vrc))
|
---|
11101 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
11102 | tr("Failed to load the decryption filter: %s"),
|
---|
11103 | i_vdError(vrc).c_str());
|
---|
11104 | }
|
---|
11105 |
|
---|
11106 | if (task.mstrCipher.isNotEmpty())
|
---|
11107 | {
|
---|
11108 | if ( task.mstrNewPassword.isEmpty()
|
---|
11109 | && task.mstrNewPasswordId.isEmpty()
|
---|
11110 | && task.mstrCurrentPassword.isNotEmpty())
|
---|
11111 | {
|
---|
11112 | /* An empty password and password ID will default to the current password. */
|
---|
11113 | pszPasswordNew = task.mstrCurrentPassword.c_str();
|
---|
11114 | }
|
---|
11115 | else if (task.mstrNewPassword.isEmpty())
|
---|
11116 | throw setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
11117 | tr("A password must be given for the image encryption"));
|
---|
11118 | else if (task.mstrNewPasswordId.isEmpty())
|
---|
11119 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
11120 | tr("A valid identifier for the password must be given"));
|
---|
11121 | else
|
---|
11122 | pszPasswordNew = task.mstrNewPassword.c_str();
|
---|
11123 |
|
---|
11124 | i_taskEncryptSettingsSetup(&CryptoSettingsWrite, task.mstrCipher.c_str(), NULL,
|
---|
11125 | pszPasswordNew, true /* fCreateKeyStore */);
|
---|
11126 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_WRITE, CryptoSettingsWrite.vdFilterIfaces);
|
---|
11127 | if (RT_FAILURE(vrc))
|
---|
11128 | throw setErrorBoth(VBOX_E_INVALID_OBJECT_STATE, vrc,
|
---|
11129 | tr("Failed to load the encryption filter: %s"),
|
---|
11130 | i_vdError(vrc).c_str());
|
---|
11131 | }
|
---|
11132 | else if (task.mstrNewPasswordId.isNotEmpty() || task.mstrNewPassword.isNotEmpty())
|
---|
11133 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
11134 | tr("The password and password identifier must be empty if the output should be unencrypted"));
|
---|
11135 |
|
---|
11136 | /* Open all media in the chain. */
|
---|
11137 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
11138 | task.mpMediumLockList->GetBegin();
|
---|
11139 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
11140 | task.mpMediumLockList->GetEnd();
|
---|
11141 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
11142 | mediumListEnd;
|
---|
11143 | --mediumListLast;
|
---|
11144 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
11145 | it != mediumListEnd;
|
---|
11146 | ++it)
|
---|
11147 | {
|
---|
11148 | const MediumLock &mediumLock = *it;
|
---|
11149 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
11150 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
11151 |
|
---|
11152 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
11153 |
|
---|
11154 | /* Open all media but last in read-only mode. Do not handle
|
---|
11155 | * shareable media, as compaction and sharing are mutually
|
---|
11156 | * exclusive. */
|
---|
11157 | vrc = VDOpen(pDisk,
|
---|
11158 | pMedium->m->strFormat.c_str(),
|
---|
11159 | pMedium->m->strLocationFull.c_str(),
|
---|
11160 | m->uOpenFlagsDef | (it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
11161 | pMedium->m->vdImageIfaces);
|
---|
11162 | if (RT_FAILURE(vrc))
|
---|
11163 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
11164 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
11165 | pMedium->m->strLocationFull.c_str(),
|
---|
11166 | i_vdError(vrc).c_str());
|
---|
11167 | }
|
---|
11168 |
|
---|
11169 | Assert(m->state == MediumState_LockedWrite);
|
---|
11170 |
|
---|
11171 | Utf8Str location(m->strLocationFull);
|
---|
11172 |
|
---|
11173 | /* unlock before the potentially lengthy operation */
|
---|
11174 | thisLock.release();
|
---|
11175 |
|
---|
11176 | vrc = VDPrepareWithFilters(pDisk, task.mVDOperationIfaces);
|
---|
11177 | if (RT_FAILURE(vrc))
|
---|
11178 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
11179 | tr("Could not prepare disk images for encryption (%Rrc): %s"),
|
---|
11180 | vrc, i_vdError(vrc).c_str());
|
---|
11181 |
|
---|
11182 | thisLock.acquire();
|
---|
11183 | /* If everything went well set the new key store. */
|
---|
11184 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
11185 | if (it != pBase->m->mapProperties.end())
|
---|
11186 | pBase->m->mapProperties.erase(it);
|
---|
11187 |
|
---|
11188 | /* Delete KeyId if encryption is removed or the password did change. */
|
---|
11189 | if ( task.mstrNewPasswordId.isNotEmpty()
|
---|
11190 | || task.mstrCipher.isEmpty())
|
---|
11191 | {
|
---|
11192 | it = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
11193 | if (it != pBase->m->mapProperties.end())
|
---|
11194 | pBase->m->mapProperties.erase(it);
|
---|
11195 | }
|
---|
11196 |
|
---|
11197 | if (CryptoSettingsWrite.pszKeyStore)
|
---|
11198 | {
|
---|
11199 | pBase->m->mapProperties["CRYPT/KeyStore"] = Utf8Str(CryptoSettingsWrite.pszKeyStore);
|
---|
11200 | if (task.mstrNewPasswordId.isNotEmpty())
|
---|
11201 | pBase->m->mapProperties["CRYPT/KeyId"] = task.mstrNewPasswordId;
|
---|
11202 | }
|
---|
11203 |
|
---|
11204 | if (CryptoSettingsRead.pszCipherReturned)
|
---|
11205 | RTStrFree(CryptoSettingsRead.pszCipherReturned);
|
---|
11206 |
|
---|
11207 | if (CryptoSettingsWrite.pszCipherReturned)
|
---|
11208 | RTStrFree(CryptoSettingsWrite.pszCipherReturned);
|
---|
11209 |
|
---|
11210 | thisLock.release();
|
---|
11211 | pBase->i_markRegistriesModified();
|
---|
11212 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
11213 | }
|
---|
11214 | catch (HRESULT aRC) { rc = aRC; }
|
---|
11215 |
|
---|
11216 | if (pvBuf)
|
---|
11217 | RTMemFree(pvBuf);
|
---|
11218 |
|
---|
11219 | VDDestroy(pDisk);
|
---|
11220 | # else
|
---|
11221 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
11222 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
11223 | # endif
|
---|
11224 | }
|
---|
11225 | catch (HRESULT aRC) { rc = aRC; }
|
---|
11226 |
|
---|
11227 | /* Everything is explicitly unlocked when the task exits,
|
---|
11228 | * as the task destruction also destroys the media chain. */
|
---|
11229 |
|
---|
11230 | return rc;
|
---|
11231 | }
|
---|
11232 |
|
---|
11233 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|