1 | /* $Id: MediumImpl.cpp 57413 2015-08-18 10:44:47Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox COM class implementation
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2015 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 | #include "MediumImpl.h"
|
---|
18 | #include "TokenImpl.h"
|
---|
19 | #include "ProgressImpl.h"
|
---|
20 | #include "SystemPropertiesImpl.h"
|
---|
21 | #include "VirtualBoxImpl.h"
|
---|
22 | #include "ExtPackManagerImpl.h"
|
---|
23 |
|
---|
24 | #include "AutoCaller.h"
|
---|
25 | #include "Logging.h"
|
---|
26 |
|
---|
27 | #include "VBox/com/MultiResult.h"
|
---|
28 | #include "VBox/com/ErrorInfo.h"
|
---|
29 |
|
---|
30 | #include <VBox/err.h>
|
---|
31 | #include <VBox/settings.h>
|
---|
32 |
|
---|
33 | #include <iprt/param.h>
|
---|
34 | #include <iprt/path.h>
|
---|
35 | #include <iprt/file.h>
|
---|
36 | #include <iprt/tcp.h>
|
---|
37 | #include <iprt/cpp/utils.h>
|
---|
38 | #include <iprt/memsafer.h>
|
---|
39 | #include <iprt/base64.h>
|
---|
40 |
|
---|
41 | #include <VBox/vd.h>
|
---|
42 |
|
---|
43 | #include <algorithm>
|
---|
44 | #include <list>
|
---|
45 |
|
---|
46 | #include <openssl/rand.h>
|
---|
47 |
|
---|
48 | typedef std::list<Guid> GuidList;
|
---|
49 |
|
---|
50 | ////////////////////////////////////////////////////////////////////////////////
|
---|
51 | //
|
---|
52 | // Medium data definition
|
---|
53 | //
|
---|
54 | ////////////////////////////////////////////////////////////////////////////////
|
---|
55 |
|
---|
56 | /** Describes how a machine refers to this medium. */
|
---|
57 | struct BackRef
|
---|
58 | {
|
---|
59 | /** Equality predicate for stdc++. */
|
---|
60 | struct EqualsTo : public std::unary_function <BackRef, bool>
|
---|
61 | {
|
---|
62 | explicit EqualsTo(const Guid &aMachineId) : machineId(aMachineId) {}
|
---|
63 |
|
---|
64 | bool operator()(const argument_type &aThat) const
|
---|
65 | {
|
---|
66 | return aThat.machineId == machineId;
|
---|
67 | }
|
---|
68 |
|
---|
69 | const Guid machineId;
|
---|
70 | };
|
---|
71 |
|
---|
72 | BackRef(const Guid &aMachineId,
|
---|
73 | const Guid &aSnapshotId = Guid::Empty)
|
---|
74 | : machineId(aMachineId),
|
---|
75 | fInCurState(aSnapshotId.isZero())
|
---|
76 | {
|
---|
77 | if (aSnapshotId.isValid() && !aSnapshotId.isZero())
|
---|
78 | llSnapshotIds.push_back(aSnapshotId);
|
---|
79 | }
|
---|
80 |
|
---|
81 | Guid machineId;
|
---|
82 | bool fInCurState : 1;
|
---|
83 | GuidList llSnapshotIds;
|
---|
84 | };
|
---|
85 |
|
---|
86 | typedef std::list<BackRef> BackRefList;
|
---|
87 |
|
---|
88 | struct Medium::Data
|
---|
89 | {
|
---|
90 | Data()
|
---|
91 | : pVirtualBox(NULL),
|
---|
92 | state(MediumState_NotCreated),
|
---|
93 | variant(MediumVariant_Standard),
|
---|
94 | size(0),
|
---|
95 | readers(0),
|
---|
96 | preLockState(MediumState_NotCreated),
|
---|
97 | queryInfoSem(LOCKCLASS_MEDIUMQUERY),
|
---|
98 | queryInfoRunning(false),
|
---|
99 | type(MediumType_Normal),
|
---|
100 | devType(DeviceType_HardDisk),
|
---|
101 | logicalSize(0),
|
---|
102 | hddOpenMode(OpenReadWrite),
|
---|
103 | autoReset(false),
|
---|
104 | hostDrive(false),
|
---|
105 | implicit(false),
|
---|
106 | fClosing(false),
|
---|
107 | uOpenFlagsDef(VD_OPEN_FLAGS_IGNORE_FLUSH),
|
---|
108 | numCreateDiffTasks(0),
|
---|
109 | vdDiskIfaces(NULL),
|
---|
110 | vdImageIfaces(NULL)
|
---|
111 | { }
|
---|
112 |
|
---|
113 | /** weak VirtualBox parent */
|
---|
114 | VirtualBox * const pVirtualBox;
|
---|
115 |
|
---|
116 | // pParent and llChildren are protected by VirtualBox::i_getMediaTreeLockHandle()
|
---|
117 | ComObjPtr<Medium> pParent;
|
---|
118 | MediaList llChildren; // to add a child, just call push_back; to remove
|
---|
119 | // a child, call child->deparent() which does a lookup
|
---|
120 |
|
---|
121 | GuidList llRegistryIDs; // media registries in which this medium is listed
|
---|
122 |
|
---|
123 | const Guid id;
|
---|
124 | Utf8Str strDescription;
|
---|
125 | MediumState_T state;
|
---|
126 | MediumVariant_T variant;
|
---|
127 | Utf8Str strLocationFull;
|
---|
128 | uint64_t size;
|
---|
129 | Utf8Str strLastAccessError;
|
---|
130 |
|
---|
131 | BackRefList backRefs;
|
---|
132 |
|
---|
133 | size_t readers;
|
---|
134 | MediumState_T preLockState;
|
---|
135 |
|
---|
136 | /** Special synchronization for operations which must wait for
|
---|
137 | * Medium::i_queryInfo in another thread to complete. Using a SemRW is
|
---|
138 | * not quite ideal, but at least it is subject to the lock validator,
|
---|
139 | * unlike the SemEventMulti which we had here for many years. Catching
|
---|
140 | * possible deadlocks is more important than a tiny bit of efficiency. */
|
---|
141 | RWLockHandle queryInfoSem;
|
---|
142 | bool queryInfoRunning : 1;
|
---|
143 |
|
---|
144 | const Utf8Str strFormat;
|
---|
145 | ComObjPtr<MediumFormat> formatObj;
|
---|
146 |
|
---|
147 | MediumType_T type;
|
---|
148 | DeviceType_T devType;
|
---|
149 | uint64_t logicalSize;
|
---|
150 |
|
---|
151 | HDDOpenMode hddOpenMode;
|
---|
152 |
|
---|
153 | bool autoReset : 1;
|
---|
154 |
|
---|
155 | /** New UUID to be set on the next Medium::i_queryInfo call. */
|
---|
156 | const Guid uuidImage;
|
---|
157 | /** New parent UUID to be set on the next Medium::i_queryInfo call. */
|
---|
158 | const Guid uuidParentImage;
|
---|
159 |
|
---|
160 | bool hostDrive : 1;
|
---|
161 |
|
---|
162 | settings::StringsMap mapProperties;
|
---|
163 |
|
---|
164 | bool implicit : 1;
|
---|
165 | /** Flag whether the medium is in the process of being closed. */
|
---|
166 | bool fClosing: 1;
|
---|
167 |
|
---|
168 | /** Default flags passed to VDOpen(). */
|
---|
169 | unsigned uOpenFlagsDef;
|
---|
170 |
|
---|
171 | uint32_t numCreateDiffTasks;
|
---|
172 |
|
---|
173 | Utf8Str vdError; /*< Error remembered by the VD error callback. */
|
---|
174 |
|
---|
175 | VDINTERFACEERROR vdIfError;
|
---|
176 |
|
---|
177 | VDINTERFACECONFIG vdIfConfig;
|
---|
178 |
|
---|
179 | VDINTERFACETCPNET vdIfTcpNet;
|
---|
180 |
|
---|
181 | PVDINTERFACE vdDiskIfaces;
|
---|
182 | PVDINTERFACE vdImageIfaces;
|
---|
183 | };
|
---|
184 |
|
---|
185 | typedef struct VDSOCKETINT
|
---|
186 | {
|
---|
187 | /** Socket handle. */
|
---|
188 | RTSOCKET hSocket;
|
---|
189 | } VDSOCKETINT, *PVDSOCKETINT;
|
---|
190 |
|
---|
191 | ////////////////////////////////////////////////////////////////////////////////
|
---|
192 | //
|
---|
193 | // Globals
|
---|
194 | //
|
---|
195 | ////////////////////////////////////////////////////////////////////////////////
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Medium::Task class for asynchronous operations.
|
---|
199 | *
|
---|
200 | * @note Instances of this class must be created using new() because the
|
---|
201 | * task thread function will delete them when the task is complete.
|
---|
202 | *
|
---|
203 | * @note The constructor of this class adds a caller on the managed Medium
|
---|
204 | * object which is automatically released upon destruction.
|
---|
205 | */
|
---|
206 | class Medium::Task
|
---|
207 | {
|
---|
208 | public:
|
---|
209 | Task(Medium *aMedium, Progress *aProgress)
|
---|
210 | : mVDOperationIfaces(NULL),
|
---|
211 | mMedium(aMedium),
|
---|
212 | mMediumCaller(aMedium),
|
---|
213 | mThread(NIL_RTTHREAD),
|
---|
214 | mProgress(aProgress),
|
---|
215 | mVirtualBoxCaller(NULL)
|
---|
216 | {
|
---|
217 | AssertReturnVoidStmt(aMedium, mRC = E_FAIL);
|
---|
218 | mRC = mMediumCaller.rc();
|
---|
219 | if (FAILED(mRC))
|
---|
220 | return;
|
---|
221 |
|
---|
222 | /* Get strong VirtualBox reference, see below. */
|
---|
223 | VirtualBox *pVirtualBox = aMedium->m->pVirtualBox;
|
---|
224 | mVirtualBox = pVirtualBox;
|
---|
225 | mVirtualBoxCaller.attach(pVirtualBox);
|
---|
226 | mRC = mVirtualBoxCaller.rc();
|
---|
227 | if (FAILED(mRC))
|
---|
228 | return;
|
---|
229 |
|
---|
230 | /* Set up a per-operation progress interface, can be used freely (for
|
---|
231 | * binary operations you can use it either on the source or target). */
|
---|
232 | mVDIfProgress.pfnProgress = vdProgressCall;
|
---|
233 | int vrc = VDInterfaceAdd(&mVDIfProgress.Core,
|
---|
234 | "Medium::Task::vdInterfaceProgress",
|
---|
235 | VDINTERFACETYPE_PROGRESS,
|
---|
236 | mProgress,
|
---|
237 | sizeof(VDINTERFACEPROGRESS),
|
---|
238 | &mVDOperationIfaces);
|
---|
239 | AssertRC(vrc);
|
---|
240 | if (RT_FAILURE(vrc))
|
---|
241 | mRC = E_FAIL;
|
---|
242 | }
|
---|
243 |
|
---|
244 | // Make all destructors virtual. Just in case.
|
---|
245 | virtual ~Task()
|
---|
246 | {}
|
---|
247 |
|
---|
248 | HRESULT rc() const { return mRC; }
|
---|
249 | bool isOk() const { return SUCCEEDED(rc()); }
|
---|
250 |
|
---|
251 | static DECLCALLBACK(int) fntMediumTask(RTTHREAD aThread, void *pvUser);
|
---|
252 |
|
---|
253 | bool isAsync() { return mThread != NIL_RTTHREAD; }
|
---|
254 |
|
---|
255 | PVDINTERFACE mVDOperationIfaces;
|
---|
256 |
|
---|
257 | const ComObjPtr<Medium> mMedium;
|
---|
258 | AutoCaller mMediumCaller;
|
---|
259 |
|
---|
260 | friend HRESULT Medium::i_runNow(Medium::Task*);
|
---|
261 |
|
---|
262 | protected:
|
---|
263 | HRESULT mRC;
|
---|
264 | RTTHREAD mThread;
|
---|
265 |
|
---|
266 | private:
|
---|
267 | virtual HRESULT handler() = 0;
|
---|
268 |
|
---|
269 | const ComObjPtr<Progress> mProgress;
|
---|
270 |
|
---|
271 | static DECLCALLBACK(int) vdProgressCall(void *pvUser, unsigned uPercent);
|
---|
272 |
|
---|
273 | VDINTERFACEPROGRESS mVDIfProgress;
|
---|
274 |
|
---|
275 | /* Must have a strong VirtualBox reference during a task otherwise the
|
---|
276 | * reference count might drop to 0 while a task is still running. This
|
---|
277 | * would result in weird behavior, including deadlocks due to uninit and
|
---|
278 | * locking order issues. The deadlock often is not detectable because the
|
---|
279 | * uninit uses event semaphores which sabotages deadlock detection. */
|
---|
280 | ComObjPtr<VirtualBox> mVirtualBox;
|
---|
281 | AutoCaller mVirtualBoxCaller;
|
---|
282 | };
|
---|
283 |
|
---|
284 | class Medium::CreateBaseTask : public Medium::Task
|
---|
285 | {
|
---|
286 | public:
|
---|
287 | CreateBaseTask(Medium *aMedium,
|
---|
288 | Progress *aProgress,
|
---|
289 | uint64_t aSize,
|
---|
290 | MediumVariant_T aVariant)
|
---|
291 | : Medium::Task(aMedium, aProgress),
|
---|
292 | mSize(aSize),
|
---|
293 | mVariant(aVariant)
|
---|
294 | {}
|
---|
295 |
|
---|
296 | uint64_t mSize;
|
---|
297 | MediumVariant_T mVariant;
|
---|
298 |
|
---|
299 | private:
|
---|
300 | virtual HRESULT handler();
|
---|
301 | };
|
---|
302 |
|
---|
303 | class Medium::CreateDiffTask : public Medium::Task
|
---|
304 | {
|
---|
305 | public:
|
---|
306 | CreateDiffTask(Medium *aMedium,
|
---|
307 | Progress *aProgress,
|
---|
308 | Medium *aTarget,
|
---|
309 | MediumVariant_T aVariant,
|
---|
310 | MediumLockList *aMediumLockList,
|
---|
311 | bool fKeepMediumLockList = false)
|
---|
312 | : Medium::Task(aMedium, aProgress),
|
---|
313 | mpMediumLockList(aMediumLockList),
|
---|
314 | mTarget(aTarget),
|
---|
315 | mVariant(aVariant),
|
---|
316 | mTargetCaller(aTarget),
|
---|
317 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
318 | {
|
---|
319 | AssertReturnVoidStmt(aTarget != NULL, mRC = E_FAIL);
|
---|
320 | mRC = mTargetCaller.rc();
|
---|
321 | if (FAILED(mRC))
|
---|
322 | return;
|
---|
323 | }
|
---|
324 |
|
---|
325 | ~CreateDiffTask()
|
---|
326 | {
|
---|
327 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
328 | delete mpMediumLockList;
|
---|
329 | }
|
---|
330 |
|
---|
331 | MediumLockList *mpMediumLockList;
|
---|
332 |
|
---|
333 | const ComObjPtr<Medium> mTarget;
|
---|
334 | MediumVariant_T mVariant;
|
---|
335 |
|
---|
336 | private:
|
---|
337 | virtual HRESULT handler();
|
---|
338 |
|
---|
339 | AutoCaller mTargetCaller;
|
---|
340 | bool mfKeepMediumLockList;
|
---|
341 | };
|
---|
342 |
|
---|
343 | class Medium::CloneTask : public Medium::Task
|
---|
344 | {
|
---|
345 | public:
|
---|
346 | CloneTask(Medium *aMedium,
|
---|
347 | Progress *aProgress,
|
---|
348 | Medium *aTarget,
|
---|
349 | MediumVariant_T aVariant,
|
---|
350 | Medium *aParent,
|
---|
351 | uint32_t idxSrcImageSame,
|
---|
352 | uint32_t idxDstImageSame,
|
---|
353 | MediumLockList *aSourceMediumLockList,
|
---|
354 | MediumLockList *aTargetMediumLockList,
|
---|
355 | bool fKeepSourceMediumLockList = false,
|
---|
356 | bool fKeepTargetMediumLockList = false)
|
---|
357 | : Medium::Task(aMedium, aProgress),
|
---|
358 | mTarget(aTarget),
|
---|
359 | mParent(aParent),
|
---|
360 | mpSourceMediumLockList(aSourceMediumLockList),
|
---|
361 | mpTargetMediumLockList(aTargetMediumLockList),
|
---|
362 | mVariant(aVariant),
|
---|
363 | midxSrcImageSame(idxSrcImageSame),
|
---|
364 | midxDstImageSame(idxDstImageSame),
|
---|
365 | mTargetCaller(aTarget),
|
---|
366 | mParentCaller(aParent),
|
---|
367 | mfKeepSourceMediumLockList(fKeepSourceMediumLockList),
|
---|
368 | mfKeepTargetMediumLockList(fKeepTargetMediumLockList)
|
---|
369 | {
|
---|
370 | AssertReturnVoidStmt(aTarget != NULL, mRC = E_FAIL);
|
---|
371 | mRC = mTargetCaller.rc();
|
---|
372 | if (FAILED(mRC))
|
---|
373 | return;
|
---|
374 | /* aParent may be NULL */
|
---|
375 | mRC = mParentCaller.rc();
|
---|
376 | if (FAILED(mRC))
|
---|
377 | return;
|
---|
378 | AssertReturnVoidStmt(aSourceMediumLockList != NULL, mRC = E_FAIL);
|
---|
379 | AssertReturnVoidStmt(aTargetMediumLockList != NULL, mRC = E_FAIL);
|
---|
380 | }
|
---|
381 |
|
---|
382 | ~CloneTask()
|
---|
383 | {
|
---|
384 | if (!mfKeepSourceMediumLockList && mpSourceMediumLockList)
|
---|
385 | delete mpSourceMediumLockList;
|
---|
386 | if (!mfKeepTargetMediumLockList && mpTargetMediumLockList)
|
---|
387 | delete mpTargetMediumLockList;
|
---|
388 | }
|
---|
389 |
|
---|
390 | const ComObjPtr<Medium> mTarget;
|
---|
391 | const ComObjPtr<Medium> mParent;
|
---|
392 | MediumLockList *mpSourceMediumLockList;
|
---|
393 | MediumLockList *mpTargetMediumLockList;
|
---|
394 | MediumVariant_T mVariant;
|
---|
395 | uint32_t midxSrcImageSame;
|
---|
396 | uint32_t midxDstImageSame;
|
---|
397 |
|
---|
398 | private:
|
---|
399 | virtual HRESULT handler();
|
---|
400 |
|
---|
401 | AutoCaller mTargetCaller;
|
---|
402 | AutoCaller mParentCaller;
|
---|
403 | bool mfKeepSourceMediumLockList;
|
---|
404 | bool mfKeepTargetMediumLockList;
|
---|
405 | };
|
---|
406 |
|
---|
407 | class Medium::CompactTask : public Medium::Task
|
---|
408 | {
|
---|
409 | public:
|
---|
410 | CompactTask(Medium *aMedium,
|
---|
411 | Progress *aProgress,
|
---|
412 | MediumLockList *aMediumLockList,
|
---|
413 | bool fKeepMediumLockList = false)
|
---|
414 | : Medium::Task(aMedium, aProgress),
|
---|
415 | mpMediumLockList(aMediumLockList),
|
---|
416 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
417 | {
|
---|
418 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
419 | }
|
---|
420 |
|
---|
421 | ~CompactTask()
|
---|
422 | {
|
---|
423 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
424 | delete mpMediumLockList;
|
---|
425 | }
|
---|
426 |
|
---|
427 | MediumLockList *mpMediumLockList;
|
---|
428 |
|
---|
429 | private:
|
---|
430 | virtual HRESULT handler();
|
---|
431 |
|
---|
432 | bool mfKeepMediumLockList;
|
---|
433 | };
|
---|
434 |
|
---|
435 | class Medium::ResizeTask : public Medium::Task
|
---|
436 | {
|
---|
437 | public:
|
---|
438 | ResizeTask(Medium *aMedium,
|
---|
439 | uint64_t aSize,
|
---|
440 | Progress *aProgress,
|
---|
441 | MediumLockList *aMediumLockList,
|
---|
442 | bool fKeepMediumLockList = false)
|
---|
443 | : Medium::Task(aMedium, aProgress),
|
---|
444 | mSize(aSize),
|
---|
445 | mpMediumLockList(aMediumLockList),
|
---|
446 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
447 | {
|
---|
448 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
449 | }
|
---|
450 |
|
---|
451 | ~ResizeTask()
|
---|
452 | {
|
---|
453 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
454 | delete mpMediumLockList;
|
---|
455 | }
|
---|
456 |
|
---|
457 | uint64_t mSize;
|
---|
458 | MediumLockList *mpMediumLockList;
|
---|
459 |
|
---|
460 | private:
|
---|
461 | virtual HRESULT handler();
|
---|
462 |
|
---|
463 | bool mfKeepMediumLockList;
|
---|
464 | };
|
---|
465 |
|
---|
466 | class Medium::ResetTask : public Medium::Task
|
---|
467 | {
|
---|
468 | public:
|
---|
469 | ResetTask(Medium *aMedium,
|
---|
470 | Progress *aProgress,
|
---|
471 | MediumLockList *aMediumLockList,
|
---|
472 | bool fKeepMediumLockList = false)
|
---|
473 | : Medium::Task(aMedium, aProgress),
|
---|
474 | mpMediumLockList(aMediumLockList),
|
---|
475 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
476 | {}
|
---|
477 |
|
---|
478 | ~ResetTask()
|
---|
479 | {
|
---|
480 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
481 | delete mpMediumLockList;
|
---|
482 | }
|
---|
483 |
|
---|
484 | MediumLockList *mpMediumLockList;
|
---|
485 |
|
---|
486 | private:
|
---|
487 | virtual HRESULT handler();
|
---|
488 |
|
---|
489 | bool mfKeepMediumLockList;
|
---|
490 | };
|
---|
491 |
|
---|
492 | class Medium::DeleteTask : public Medium::Task
|
---|
493 | {
|
---|
494 | public:
|
---|
495 | DeleteTask(Medium *aMedium,
|
---|
496 | Progress *aProgress,
|
---|
497 | MediumLockList *aMediumLockList,
|
---|
498 | bool fKeepMediumLockList = false)
|
---|
499 | : Medium::Task(aMedium, aProgress),
|
---|
500 | mpMediumLockList(aMediumLockList),
|
---|
501 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
502 | {}
|
---|
503 |
|
---|
504 | ~DeleteTask()
|
---|
505 | {
|
---|
506 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
507 | delete mpMediumLockList;
|
---|
508 | }
|
---|
509 |
|
---|
510 | MediumLockList *mpMediumLockList;
|
---|
511 |
|
---|
512 | private:
|
---|
513 | virtual HRESULT handler();
|
---|
514 |
|
---|
515 | bool mfKeepMediumLockList;
|
---|
516 | };
|
---|
517 |
|
---|
518 | class Medium::MergeTask : public Medium::Task
|
---|
519 | {
|
---|
520 | public:
|
---|
521 | MergeTask(Medium *aMedium,
|
---|
522 | Medium *aTarget,
|
---|
523 | bool fMergeForward,
|
---|
524 | Medium *aParentForTarget,
|
---|
525 | MediumLockList *aChildrenToReparent,
|
---|
526 | Progress *aProgress,
|
---|
527 | MediumLockList *aMediumLockList,
|
---|
528 | bool fKeepMediumLockList = false)
|
---|
529 | : Medium::Task(aMedium, aProgress),
|
---|
530 | mTarget(aTarget),
|
---|
531 | mfMergeForward(fMergeForward),
|
---|
532 | mParentForTarget(aParentForTarget),
|
---|
533 | mpChildrenToReparent(aChildrenToReparent),
|
---|
534 | mpMediumLockList(aMediumLockList),
|
---|
535 | mTargetCaller(aTarget),
|
---|
536 | mParentForTargetCaller(aParentForTarget),
|
---|
537 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
538 | {
|
---|
539 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
540 | }
|
---|
541 |
|
---|
542 | ~MergeTask()
|
---|
543 | {
|
---|
544 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
545 | delete mpMediumLockList;
|
---|
546 | if (mpChildrenToReparent)
|
---|
547 | delete mpChildrenToReparent;
|
---|
548 | }
|
---|
549 |
|
---|
550 | const ComObjPtr<Medium> mTarget;
|
---|
551 | bool mfMergeForward;
|
---|
552 | /* When mpChildrenToReparent is null then mParentForTarget is non-null and
|
---|
553 | * vice versa. In other words: they are used in different cases. */
|
---|
554 | const ComObjPtr<Medium> mParentForTarget;
|
---|
555 | MediumLockList *mpChildrenToReparent;
|
---|
556 | MediumLockList *mpMediumLockList;
|
---|
557 |
|
---|
558 | private:
|
---|
559 | virtual HRESULT handler();
|
---|
560 |
|
---|
561 | AutoCaller mTargetCaller;
|
---|
562 | AutoCaller mParentForTargetCaller;
|
---|
563 | bool mfKeepMediumLockList;
|
---|
564 | };
|
---|
565 |
|
---|
566 | class Medium::ExportTask : public Medium::Task
|
---|
567 | {
|
---|
568 | public:
|
---|
569 | ExportTask(Medium *aMedium,
|
---|
570 | Progress *aProgress,
|
---|
571 | const char *aFilename,
|
---|
572 | MediumFormat *aFormat,
|
---|
573 | MediumVariant_T aVariant,
|
---|
574 | SecretKeyStore *pSecretKeyStore,
|
---|
575 | VDINTERFACEIO *aVDImageIOIf,
|
---|
576 | void *aVDImageIOUser,
|
---|
577 | MediumLockList *aSourceMediumLockList,
|
---|
578 | bool fKeepSourceMediumLockList = false)
|
---|
579 | : Medium::Task(aMedium, aProgress),
|
---|
580 | mpSourceMediumLockList(aSourceMediumLockList),
|
---|
581 | mFilename(aFilename),
|
---|
582 | mFormat(aFormat),
|
---|
583 | mVariant(aVariant),
|
---|
584 | m_pSecretKeyStore(pSecretKeyStore),
|
---|
585 | mfKeepSourceMediumLockList(fKeepSourceMediumLockList)
|
---|
586 | {
|
---|
587 | AssertReturnVoidStmt(aSourceMediumLockList != NULL, mRC = E_FAIL);
|
---|
588 |
|
---|
589 | mVDImageIfaces = aMedium->m->vdImageIfaces;
|
---|
590 | if (aVDImageIOIf)
|
---|
591 | {
|
---|
592 | int vrc = VDInterfaceAdd(&aVDImageIOIf->Core, "Medium::vdInterfaceIO",
|
---|
593 | VDINTERFACETYPE_IO, aVDImageIOUser,
|
---|
594 | sizeof(VDINTERFACEIO), &mVDImageIfaces);
|
---|
595 | AssertRCReturnVoidStmt(vrc, mRC = E_FAIL);
|
---|
596 | }
|
---|
597 | }
|
---|
598 |
|
---|
599 | ~ExportTask()
|
---|
600 | {
|
---|
601 | if (!mfKeepSourceMediumLockList && mpSourceMediumLockList)
|
---|
602 | delete mpSourceMediumLockList;
|
---|
603 | }
|
---|
604 |
|
---|
605 | MediumLockList *mpSourceMediumLockList;
|
---|
606 | Utf8Str mFilename;
|
---|
607 | ComObjPtr<MediumFormat> mFormat;
|
---|
608 | MediumVariant_T mVariant;
|
---|
609 | PVDINTERFACE mVDImageIfaces;
|
---|
610 | SecretKeyStore *m_pSecretKeyStore;
|
---|
611 |
|
---|
612 | private:
|
---|
613 | virtual HRESULT handler();
|
---|
614 |
|
---|
615 | bool mfKeepSourceMediumLockList;
|
---|
616 | };
|
---|
617 |
|
---|
618 | class Medium::ImportTask : public Medium::Task
|
---|
619 | {
|
---|
620 | public:
|
---|
621 | ImportTask(Medium *aMedium,
|
---|
622 | Progress *aProgress,
|
---|
623 | const char *aFilename,
|
---|
624 | MediumFormat *aFormat,
|
---|
625 | MediumVariant_T aVariant,
|
---|
626 | VDINTERFACEIO *aVDImageIOIf,
|
---|
627 | void *aVDImageIOUser,
|
---|
628 | Medium *aParent,
|
---|
629 | MediumLockList *aTargetMediumLockList,
|
---|
630 | bool fKeepTargetMediumLockList = false)
|
---|
631 | : Medium::Task(aMedium, aProgress),
|
---|
632 | mFilename(aFilename),
|
---|
633 | mFormat(aFormat),
|
---|
634 | mVariant(aVariant),
|
---|
635 | mParent(aParent),
|
---|
636 | mpTargetMediumLockList(aTargetMediumLockList),
|
---|
637 | mParentCaller(aParent),
|
---|
638 | mfKeepTargetMediumLockList(fKeepTargetMediumLockList)
|
---|
639 | {
|
---|
640 | AssertReturnVoidStmt(aTargetMediumLockList != NULL, mRC = E_FAIL);
|
---|
641 | /* aParent may be NULL */
|
---|
642 | mRC = mParentCaller.rc();
|
---|
643 | if (FAILED(mRC))
|
---|
644 | return;
|
---|
645 |
|
---|
646 | mVDImageIfaces = aMedium->m->vdImageIfaces;
|
---|
647 | if (aVDImageIOIf)
|
---|
648 | {
|
---|
649 | int vrc = VDInterfaceAdd(&aVDImageIOIf->Core, "Medium::vdInterfaceIO",
|
---|
650 | VDINTERFACETYPE_IO, aVDImageIOUser,
|
---|
651 | sizeof(VDINTERFACEIO), &mVDImageIfaces);
|
---|
652 | AssertRCReturnVoidStmt(vrc, mRC = E_FAIL);
|
---|
653 | }
|
---|
654 | }
|
---|
655 |
|
---|
656 | ~ImportTask()
|
---|
657 | {
|
---|
658 | if (!mfKeepTargetMediumLockList && mpTargetMediumLockList)
|
---|
659 | delete mpTargetMediumLockList;
|
---|
660 | }
|
---|
661 |
|
---|
662 | Utf8Str mFilename;
|
---|
663 | ComObjPtr<MediumFormat> mFormat;
|
---|
664 | MediumVariant_T mVariant;
|
---|
665 | const ComObjPtr<Medium> mParent;
|
---|
666 | MediumLockList *mpTargetMediumLockList;
|
---|
667 | PVDINTERFACE mVDImageIfaces;
|
---|
668 |
|
---|
669 | private:
|
---|
670 | virtual HRESULT handler();
|
---|
671 |
|
---|
672 | AutoCaller mParentCaller;
|
---|
673 | bool mfKeepTargetMediumLockList;
|
---|
674 | };
|
---|
675 |
|
---|
676 | class Medium::EncryptTask : public Medium::Task
|
---|
677 | {
|
---|
678 | public:
|
---|
679 | EncryptTask(Medium *aMedium,
|
---|
680 | const com::Utf8Str &strNewPassword,
|
---|
681 | const com::Utf8Str &strCurrentPassword,
|
---|
682 | const com::Utf8Str &strCipher,
|
---|
683 | const com::Utf8Str &strNewPasswordId,
|
---|
684 | Progress *aProgress,
|
---|
685 | MediumLockList *aMediumLockList)
|
---|
686 | : Medium::Task(aMedium, aProgress),
|
---|
687 | mstrNewPassword(strNewPassword),
|
---|
688 | mstrCurrentPassword(strCurrentPassword),
|
---|
689 | mstrCipher(strCipher),
|
---|
690 | mstrNewPasswordId(strNewPasswordId),
|
---|
691 | mpMediumLockList(aMediumLockList)
|
---|
692 | {
|
---|
693 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
694 | /* aParent may be NULL */
|
---|
695 | mRC = mParentCaller.rc();
|
---|
696 | if (FAILED(mRC))
|
---|
697 | return;
|
---|
698 |
|
---|
699 | mVDImageIfaces = aMedium->m->vdImageIfaces;
|
---|
700 | }
|
---|
701 |
|
---|
702 | ~EncryptTask()
|
---|
703 | {
|
---|
704 | if (mstrNewPassword.length())
|
---|
705 | RTMemWipeThoroughly(mstrNewPassword.mutableRaw(), mstrNewPassword.length(), 10 /* cPasses */);
|
---|
706 | if (mstrCurrentPassword.length())
|
---|
707 | RTMemWipeThoroughly(mstrCurrentPassword.mutableRaw(), mstrCurrentPassword.length(), 10 /* cPasses */);
|
---|
708 |
|
---|
709 | /* Keep any errors which might be set when deleting the lock list. */
|
---|
710 | ErrorInfoKeeper eik;
|
---|
711 | delete mpMediumLockList;
|
---|
712 | }
|
---|
713 |
|
---|
714 | Utf8Str mstrNewPassword;
|
---|
715 | Utf8Str mstrCurrentPassword;
|
---|
716 | Utf8Str mstrCipher;
|
---|
717 | Utf8Str mstrNewPasswordId;
|
---|
718 | MediumLockList *mpMediumLockList;
|
---|
719 | PVDINTERFACE mVDImageIfaces;
|
---|
720 |
|
---|
721 | private:
|
---|
722 | virtual HRESULT handler();
|
---|
723 |
|
---|
724 | AutoCaller mParentCaller;
|
---|
725 | };
|
---|
726 |
|
---|
727 | /**
|
---|
728 | * Settings for a crypto filter instance.
|
---|
729 | */
|
---|
730 | struct Medium::CryptoFilterSettings
|
---|
731 | {
|
---|
732 | CryptoFilterSettings()
|
---|
733 | : fCreateKeyStore(false),
|
---|
734 | pszPassword(NULL),
|
---|
735 | pszKeyStore(NULL),
|
---|
736 | pszKeyStoreLoad(NULL),
|
---|
737 | pbDek(NULL),
|
---|
738 | cbDek(0),
|
---|
739 | pszCipher(NULL),
|
---|
740 | pszCipherReturned(NULL)
|
---|
741 | { }
|
---|
742 |
|
---|
743 | bool fCreateKeyStore;
|
---|
744 | const char *pszPassword;
|
---|
745 | char *pszKeyStore;
|
---|
746 | const char *pszKeyStoreLoad;
|
---|
747 |
|
---|
748 | const uint8_t *pbDek;
|
---|
749 | size_t cbDek;
|
---|
750 | const char *pszCipher;
|
---|
751 |
|
---|
752 | /** The cipher returned by the crypto filter. */
|
---|
753 | char *pszCipherReturned;
|
---|
754 |
|
---|
755 | PVDINTERFACE vdFilterIfaces;
|
---|
756 |
|
---|
757 | VDINTERFACECONFIG vdIfCfg;
|
---|
758 | VDINTERFACECRYPTO vdIfCrypto;
|
---|
759 | };
|
---|
760 |
|
---|
761 | /**
|
---|
762 | * Thread function for time-consuming medium tasks.
|
---|
763 | *
|
---|
764 | * @param pvUser Pointer to the Medium::Task instance.
|
---|
765 | */
|
---|
766 | /* static */
|
---|
767 | DECLCALLBACK(int) Medium::Task::fntMediumTask(RTTHREAD aThread, void *pvUser)
|
---|
768 | {
|
---|
769 | LogFlowFuncEnter();
|
---|
770 | AssertReturn(pvUser, (int)E_INVALIDARG);
|
---|
771 | Medium::Task *pTask = static_cast<Medium::Task *>(pvUser);
|
---|
772 |
|
---|
773 | pTask->mThread = aThread;
|
---|
774 |
|
---|
775 | HRESULT rc = pTask->handler();
|
---|
776 |
|
---|
777 | /*
|
---|
778 | * save the progress reference if run asynchronously, since we want to
|
---|
779 | * destroy the task before we send out the completion notification.
|
---|
780 | * see @bugref{7763}
|
---|
781 | */
|
---|
782 | ComObjPtr<Progress> pProgress;
|
---|
783 | if (pTask->isAsync())
|
---|
784 | pProgress = pTask->mProgress;
|
---|
785 |
|
---|
786 | /* pTask is no longer needed, delete it. */
|
---|
787 | delete pTask;
|
---|
788 |
|
---|
789 | /* complete the progress if run asynchronously */
|
---|
790 | if (!pProgress.isNull())
|
---|
791 | pProgress->i_notifyComplete(rc);
|
---|
792 |
|
---|
793 | LogFlowFunc(("rc=%Rhrc\n", rc));
|
---|
794 | LogFlowFuncLeave();
|
---|
795 |
|
---|
796 | return (int)rc;
|
---|
797 | }
|
---|
798 |
|
---|
799 | /**
|
---|
800 | * PFNVDPROGRESS callback handler for Task operations.
|
---|
801 | *
|
---|
802 | * @param pvUser Pointer to the Progress instance.
|
---|
803 | * @param uPercent Completion percentage (0-100).
|
---|
804 | */
|
---|
805 | /*static*/
|
---|
806 | DECLCALLBACK(int) Medium::Task::vdProgressCall(void *pvUser, unsigned uPercent)
|
---|
807 | {
|
---|
808 | Progress *that = static_cast<Progress *>(pvUser);
|
---|
809 |
|
---|
810 | if (that != NULL)
|
---|
811 | {
|
---|
812 | /* update the progress object, capping it at 99% as the final percent
|
---|
813 | * is used for additional operations like setting the UUIDs and similar. */
|
---|
814 | HRESULT rc = that->SetCurrentOperationProgress(uPercent * 99 / 100);
|
---|
815 | if (FAILED(rc))
|
---|
816 | {
|
---|
817 | if (rc == E_FAIL)
|
---|
818 | return VERR_CANCELLED;
|
---|
819 | else
|
---|
820 | return VERR_INVALID_STATE;
|
---|
821 | }
|
---|
822 | }
|
---|
823 |
|
---|
824 | return VINF_SUCCESS;
|
---|
825 | }
|
---|
826 |
|
---|
827 | /**
|
---|
828 | * Implementation code for the "create base" task.
|
---|
829 | */
|
---|
830 | HRESULT Medium::CreateBaseTask::handler()
|
---|
831 | {
|
---|
832 | return mMedium->i_taskCreateBaseHandler(*this);
|
---|
833 | }
|
---|
834 |
|
---|
835 | /**
|
---|
836 | * Implementation code for the "create diff" task.
|
---|
837 | */
|
---|
838 | HRESULT Medium::CreateDiffTask::handler()
|
---|
839 | {
|
---|
840 | return mMedium->i_taskCreateDiffHandler(*this);
|
---|
841 | }
|
---|
842 |
|
---|
843 | /**
|
---|
844 | * Implementation code for the "clone" task.
|
---|
845 | */
|
---|
846 | HRESULT Medium::CloneTask::handler()
|
---|
847 | {
|
---|
848 | return mMedium->i_taskCloneHandler(*this);
|
---|
849 | }
|
---|
850 |
|
---|
851 | /**
|
---|
852 | * Implementation code for the "compact" task.
|
---|
853 | */
|
---|
854 | HRESULT Medium::CompactTask::handler()
|
---|
855 | {
|
---|
856 | return mMedium->i_taskCompactHandler(*this);
|
---|
857 | }
|
---|
858 |
|
---|
859 | /**
|
---|
860 | * Implementation code for the "resize" task.
|
---|
861 | */
|
---|
862 | HRESULT Medium::ResizeTask::handler()
|
---|
863 | {
|
---|
864 | return mMedium->i_taskResizeHandler(*this);
|
---|
865 | }
|
---|
866 |
|
---|
867 |
|
---|
868 | /**
|
---|
869 | * Implementation code for the "reset" task.
|
---|
870 | */
|
---|
871 | HRESULT Medium::ResetTask::handler()
|
---|
872 | {
|
---|
873 | return mMedium->i_taskResetHandler(*this);
|
---|
874 | }
|
---|
875 |
|
---|
876 | /**
|
---|
877 | * Implementation code for the "delete" task.
|
---|
878 | */
|
---|
879 | HRESULT Medium::DeleteTask::handler()
|
---|
880 | {
|
---|
881 | return mMedium->i_taskDeleteHandler(*this);
|
---|
882 | }
|
---|
883 |
|
---|
884 | /**
|
---|
885 | * Implementation code for the "merge" task.
|
---|
886 | */
|
---|
887 | HRESULT Medium::MergeTask::handler()
|
---|
888 | {
|
---|
889 | return mMedium->i_taskMergeHandler(*this);
|
---|
890 | }
|
---|
891 |
|
---|
892 | /**
|
---|
893 | * Implementation code for the "export" task.
|
---|
894 | */
|
---|
895 | HRESULT Medium::ExportTask::handler()
|
---|
896 | {
|
---|
897 | return mMedium->i_taskExportHandler(*this);
|
---|
898 | }
|
---|
899 |
|
---|
900 | /**
|
---|
901 | * Implementation code for the "import" task.
|
---|
902 | */
|
---|
903 | HRESULT Medium::ImportTask::handler()
|
---|
904 | {
|
---|
905 | return mMedium->i_taskImportHandler(*this);
|
---|
906 | }
|
---|
907 |
|
---|
908 | /**
|
---|
909 | * Implementation code for the "encrypt" task.
|
---|
910 | */
|
---|
911 | HRESULT Medium::EncryptTask::handler()
|
---|
912 | {
|
---|
913 | return mMedium->i_taskEncryptHandler(*this);
|
---|
914 | }
|
---|
915 |
|
---|
916 | ////////////////////////////////////////////////////////////////////////////////
|
---|
917 | //
|
---|
918 | // Medium constructor / destructor
|
---|
919 | //
|
---|
920 | ////////////////////////////////////////////////////////////////////////////////
|
---|
921 |
|
---|
922 | DEFINE_EMPTY_CTOR_DTOR(Medium)
|
---|
923 |
|
---|
924 | HRESULT Medium::FinalConstruct()
|
---|
925 | {
|
---|
926 | m = new Data;
|
---|
927 |
|
---|
928 | /* Initialize the callbacks of the VD error interface */
|
---|
929 | m->vdIfError.pfnError = i_vdErrorCall;
|
---|
930 | m->vdIfError.pfnMessage = NULL;
|
---|
931 |
|
---|
932 | /* Initialize the callbacks of the VD config interface */
|
---|
933 | m->vdIfConfig.pfnAreKeysValid = i_vdConfigAreKeysValid;
|
---|
934 | m->vdIfConfig.pfnQuerySize = i_vdConfigQuerySize;
|
---|
935 | m->vdIfConfig.pfnQuery = i_vdConfigQuery;
|
---|
936 | m->vdIfConfig.pfnQueryBytes = NULL;
|
---|
937 |
|
---|
938 | /* Initialize the callbacks of the VD TCP interface (we always use the host
|
---|
939 | * IP stack for now) */
|
---|
940 | m->vdIfTcpNet.pfnSocketCreate = i_vdTcpSocketCreate;
|
---|
941 | m->vdIfTcpNet.pfnSocketDestroy = i_vdTcpSocketDestroy;
|
---|
942 | m->vdIfTcpNet.pfnClientConnect = i_vdTcpClientConnect;
|
---|
943 | m->vdIfTcpNet.pfnClientClose = i_vdTcpClientClose;
|
---|
944 | m->vdIfTcpNet.pfnIsClientConnected = i_vdTcpIsClientConnected;
|
---|
945 | m->vdIfTcpNet.pfnSelectOne = i_vdTcpSelectOne;
|
---|
946 | m->vdIfTcpNet.pfnRead = i_vdTcpRead;
|
---|
947 | m->vdIfTcpNet.pfnWrite = i_vdTcpWrite;
|
---|
948 | m->vdIfTcpNet.pfnSgWrite = i_vdTcpSgWrite;
|
---|
949 | m->vdIfTcpNet.pfnFlush = i_vdTcpFlush;
|
---|
950 | m->vdIfTcpNet.pfnSetSendCoalescing = i_vdTcpSetSendCoalescing;
|
---|
951 | m->vdIfTcpNet.pfnGetLocalAddress = i_vdTcpGetLocalAddress;
|
---|
952 | m->vdIfTcpNet.pfnGetPeerAddress = i_vdTcpGetPeerAddress;
|
---|
953 | m->vdIfTcpNet.pfnSelectOneEx = NULL;
|
---|
954 | m->vdIfTcpNet.pfnPoke = NULL;
|
---|
955 |
|
---|
956 | /* Initialize the per-disk interface chain (could be done more globally,
|
---|
957 | * but it's not wasting much time or space so it's not worth it). */
|
---|
958 | int vrc;
|
---|
959 | vrc = VDInterfaceAdd(&m->vdIfError.Core,
|
---|
960 | "Medium::vdInterfaceError",
|
---|
961 | VDINTERFACETYPE_ERROR, this,
|
---|
962 | sizeof(VDINTERFACEERROR), &m->vdDiskIfaces);
|
---|
963 | AssertRCReturn(vrc, E_FAIL);
|
---|
964 |
|
---|
965 | /* Initialize the per-image interface chain */
|
---|
966 | vrc = VDInterfaceAdd(&m->vdIfConfig.Core,
|
---|
967 | "Medium::vdInterfaceConfig",
|
---|
968 | VDINTERFACETYPE_CONFIG, this,
|
---|
969 | sizeof(VDINTERFACECONFIG), &m->vdImageIfaces);
|
---|
970 | AssertRCReturn(vrc, E_FAIL);
|
---|
971 |
|
---|
972 | vrc = VDInterfaceAdd(&m->vdIfTcpNet.Core,
|
---|
973 | "Medium::vdInterfaceTcpNet",
|
---|
974 | VDINTERFACETYPE_TCPNET, this,
|
---|
975 | sizeof(VDINTERFACETCPNET), &m->vdImageIfaces);
|
---|
976 | AssertRCReturn(vrc, E_FAIL);
|
---|
977 |
|
---|
978 | return BaseFinalConstruct();
|
---|
979 | }
|
---|
980 |
|
---|
981 | void Medium::FinalRelease()
|
---|
982 | {
|
---|
983 | uninit();
|
---|
984 |
|
---|
985 | delete m;
|
---|
986 |
|
---|
987 | BaseFinalRelease();
|
---|
988 | }
|
---|
989 |
|
---|
990 | /**
|
---|
991 | * Initializes an empty hard disk object without creating or opening an associated
|
---|
992 | * storage unit.
|
---|
993 | *
|
---|
994 | * This gets called by VirtualBox::CreateMedium() in which case uuidMachineRegistry
|
---|
995 | * is empty since starting with VirtualBox 4.0, we no longer add opened media to a
|
---|
996 | * registry automatically (this is deferred until the medium is attached to a machine).
|
---|
997 | *
|
---|
998 | * This also gets called when VirtualBox creates diff images; in this case uuidMachineRegistry
|
---|
999 | * is set to the registry of the parent image to make sure they all end up in the same
|
---|
1000 | * file.
|
---|
1001 | *
|
---|
1002 | * For hard disks that don't have the MediumFormatCapabilities_CreateFixed or
|
---|
1003 | * MediumFormatCapabilities_CreateDynamic capability (and therefore cannot be created or deleted
|
---|
1004 | * with the means of VirtualBox) the associated storage unit is assumed to be
|
---|
1005 | * ready for use so the state of the hard disk object will be set to Created.
|
---|
1006 | *
|
---|
1007 | * @param aVirtualBox VirtualBox object.
|
---|
1008 | * @param aFormat
|
---|
1009 | * @param aLocation Storage unit location.
|
---|
1010 | * @param uuidMachineRegistry The registry to which this medium should be added
|
---|
1011 | * (global registry UUID or machine UUID or empty if none).
|
---|
1012 | * @param deviceType Device Type.
|
---|
1013 | */
|
---|
1014 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1015 | const Utf8Str &aFormat,
|
---|
1016 | const Utf8Str &aLocation,
|
---|
1017 | const Guid &uuidMachineRegistry,
|
---|
1018 | const DeviceType_T aDeviceType)
|
---|
1019 | {
|
---|
1020 | AssertReturn(aVirtualBox != NULL, E_FAIL);
|
---|
1021 | AssertReturn(!aFormat.isEmpty(), E_FAIL);
|
---|
1022 |
|
---|
1023 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1024 | AutoInitSpan autoInitSpan(this);
|
---|
1025 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1026 |
|
---|
1027 | HRESULT rc = S_OK;
|
---|
1028 |
|
---|
1029 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1030 |
|
---|
1031 | if (uuidMachineRegistry.isValid() && !uuidMachineRegistry.isZero())
|
---|
1032 | m->llRegistryIDs.push_back(uuidMachineRegistry);
|
---|
1033 |
|
---|
1034 | /* no storage yet */
|
---|
1035 | m->state = MediumState_NotCreated;
|
---|
1036 |
|
---|
1037 | /* cannot be a host drive */
|
---|
1038 | m->hostDrive = false;
|
---|
1039 |
|
---|
1040 | m->devType = aDeviceType;
|
---|
1041 |
|
---|
1042 | /* No storage unit is created yet, no need to call Medium::i_queryInfo */
|
---|
1043 |
|
---|
1044 | rc = i_setFormat(aFormat);
|
---|
1045 | if (FAILED(rc)) return rc;
|
---|
1046 |
|
---|
1047 | rc = i_setLocation(aLocation);
|
---|
1048 | if (FAILED(rc)) return rc;
|
---|
1049 |
|
---|
1050 | if (!(m->formatObj->i_getCapabilities() & ( MediumFormatCapabilities_CreateFixed
|
---|
1051 | | MediumFormatCapabilities_CreateDynamic))
|
---|
1052 | )
|
---|
1053 | {
|
---|
1054 | /* Storage for mediums of this format can neither be explicitly
|
---|
1055 | * created by VirtualBox nor deleted, so we place the medium to
|
---|
1056 | * Inaccessible state here and also add it to the registry. The
|
---|
1057 | * state means that one has to use RefreshState() to update the
|
---|
1058 | * medium format specific fields. */
|
---|
1059 | m->state = MediumState_Inaccessible;
|
---|
1060 | // create new UUID
|
---|
1061 | unconst(m->id).create();
|
---|
1062 |
|
---|
1063 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1064 | ComObjPtr<Medium> pMedium;
|
---|
1065 |
|
---|
1066 | /*
|
---|
1067 | * Check whether the UUID is taken already and create a new one
|
---|
1068 | * if required.
|
---|
1069 | * Try this only a limited amount of times in case the PRNG is broken
|
---|
1070 | * in some way to prevent an endless loop.
|
---|
1071 | */
|
---|
1072 | for (unsigned i = 0; i < 5; i++)
|
---|
1073 | {
|
---|
1074 | bool fInUse;
|
---|
1075 |
|
---|
1076 | fInUse = m->pVirtualBox->i_isMediaUuidInUse(m->id, aDeviceType);
|
---|
1077 | if (fInUse)
|
---|
1078 | {
|
---|
1079 | // create new UUID
|
---|
1080 | unconst(m->id).create();
|
---|
1081 | }
|
---|
1082 | else
|
---|
1083 | break;
|
---|
1084 | }
|
---|
1085 |
|
---|
1086 | rc = m->pVirtualBox->i_registerMedium(this, &pMedium, treeLock);
|
---|
1087 | Assert(this == pMedium || FAILED(rc));
|
---|
1088 | }
|
---|
1089 |
|
---|
1090 | /* Confirm a successful initialization when it's the case */
|
---|
1091 | if (SUCCEEDED(rc))
|
---|
1092 | autoInitSpan.setSucceeded();
|
---|
1093 |
|
---|
1094 | return rc;
|
---|
1095 | }
|
---|
1096 |
|
---|
1097 | /**
|
---|
1098 | * Initializes the medium object by opening the storage unit at the specified
|
---|
1099 | * location. The enOpenMode parameter defines whether the medium will be opened
|
---|
1100 | * read/write or read-only.
|
---|
1101 | *
|
---|
1102 | * This gets called by VirtualBox::OpenMedium() and also by
|
---|
1103 | * Machine::AttachDevice() and createImplicitDiffs() when new diff
|
---|
1104 | * images are created.
|
---|
1105 | *
|
---|
1106 | * There is no registry for this case since starting with VirtualBox 4.0, we
|
---|
1107 | * no longer add opened media to a registry automatically (this is deferred
|
---|
1108 | * until the medium is attached to a machine).
|
---|
1109 | *
|
---|
1110 | * For hard disks, the UUID, format and the parent of this medium will be
|
---|
1111 | * determined when reading the medium storage unit. For DVD and floppy images,
|
---|
1112 | * which have no UUIDs in their storage units, new UUIDs are created.
|
---|
1113 | * If the detected or set parent is not known to VirtualBox, then this method
|
---|
1114 | * will fail.
|
---|
1115 | *
|
---|
1116 | * @param aVirtualBox VirtualBox object.
|
---|
1117 | * @param aLocation Storage unit location.
|
---|
1118 | * @param enOpenMode Whether to open the medium read/write or read-only.
|
---|
1119 | * @param fForceNewUuid Whether a new UUID should be set to avoid duplicates.
|
---|
1120 | * @param aDeviceType Device type of medium.
|
---|
1121 | */
|
---|
1122 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1123 | const Utf8Str &aLocation,
|
---|
1124 | HDDOpenMode enOpenMode,
|
---|
1125 | bool fForceNewUuid,
|
---|
1126 | DeviceType_T aDeviceType)
|
---|
1127 | {
|
---|
1128 | AssertReturn(aVirtualBox, E_INVALIDARG);
|
---|
1129 | AssertReturn(!aLocation.isEmpty(), E_INVALIDARG);
|
---|
1130 |
|
---|
1131 | HRESULT rc = S_OK;
|
---|
1132 |
|
---|
1133 | {
|
---|
1134 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1135 | AutoInitSpan autoInitSpan(this);
|
---|
1136 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1137 |
|
---|
1138 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1139 |
|
---|
1140 | /* there must be a storage unit */
|
---|
1141 | m->state = MediumState_Created;
|
---|
1142 |
|
---|
1143 | /* remember device type for correct unregistering later */
|
---|
1144 | m->devType = aDeviceType;
|
---|
1145 |
|
---|
1146 | /* cannot be a host drive */
|
---|
1147 | m->hostDrive = false;
|
---|
1148 |
|
---|
1149 | /* remember the open mode (defaults to ReadWrite) */
|
---|
1150 | m->hddOpenMode = enOpenMode;
|
---|
1151 |
|
---|
1152 | if (aDeviceType == DeviceType_DVD)
|
---|
1153 | m->type = MediumType_Readonly;
|
---|
1154 | else if (aDeviceType == DeviceType_Floppy)
|
---|
1155 | m->type = MediumType_Writethrough;
|
---|
1156 |
|
---|
1157 | rc = i_setLocation(aLocation);
|
---|
1158 | if (FAILED(rc)) return rc;
|
---|
1159 |
|
---|
1160 | /* get all the information about the medium from the storage unit */
|
---|
1161 | if (fForceNewUuid)
|
---|
1162 | unconst(m->uuidImage).create();
|
---|
1163 |
|
---|
1164 | m->state = MediumState_Inaccessible;
|
---|
1165 | m->strLastAccessError = tr("Accessibility check was not yet performed");
|
---|
1166 |
|
---|
1167 | /* Confirm a successful initialization before the call to i_queryInfo.
|
---|
1168 | * Otherwise we can end up with a AutoCaller deadlock because the
|
---|
1169 | * medium becomes visible but is not marked as initialized. Causes
|
---|
1170 | * locking trouble (e.g. trying to save media registries) which is
|
---|
1171 | * hard to solve. */
|
---|
1172 | autoInitSpan.setSucceeded();
|
---|
1173 | }
|
---|
1174 |
|
---|
1175 | /* we're normal code from now on, no longer init */
|
---|
1176 | AutoCaller autoCaller(this);
|
---|
1177 | if (FAILED(autoCaller.rc()))
|
---|
1178 | return autoCaller.rc();
|
---|
1179 |
|
---|
1180 | /* need to call i_queryInfo immediately to correctly place the medium in
|
---|
1181 | * the respective media tree and update other information such as uuid */
|
---|
1182 | rc = i_queryInfo(fForceNewUuid /* fSetImageId */, false /* fSetParentId */,
|
---|
1183 | autoCaller);
|
---|
1184 | if (SUCCEEDED(rc))
|
---|
1185 | {
|
---|
1186 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1187 |
|
---|
1188 | /* if the storage unit is not accessible, it's not acceptable for the
|
---|
1189 | * newly opened media so convert this into an error */
|
---|
1190 | if (m->state == MediumState_Inaccessible)
|
---|
1191 | {
|
---|
1192 | Assert(!m->strLastAccessError.isEmpty());
|
---|
1193 | rc = setError(E_FAIL, "%s", m->strLastAccessError.c_str());
|
---|
1194 | alock.release();
|
---|
1195 | autoCaller.release();
|
---|
1196 | uninit();
|
---|
1197 | }
|
---|
1198 | else
|
---|
1199 | {
|
---|
1200 | AssertStmt(!m->id.isZero(),
|
---|
1201 | alock.release(); autoCaller.release(); uninit(); return E_FAIL);
|
---|
1202 |
|
---|
1203 | /* storage format must be detected by Medium::i_queryInfo if the
|
---|
1204 | * medium is accessible */
|
---|
1205 | AssertStmt(!m->strFormat.isEmpty(),
|
---|
1206 | alock.release(); autoCaller.release(); uninit(); return E_FAIL);
|
---|
1207 | }
|
---|
1208 | }
|
---|
1209 | else
|
---|
1210 | {
|
---|
1211 | /* opening this image failed, mark the object as dead */
|
---|
1212 | autoCaller.release();
|
---|
1213 | uninit();
|
---|
1214 | }
|
---|
1215 |
|
---|
1216 | return rc;
|
---|
1217 | }
|
---|
1218 |
|
---|
1219 | /**
|
---|
1220 | * Initializes the medium object by loading its data from the given settings
|
---|
1221 | * node. The medium will always be opened read/write.
|
---|
1222 | *
|
---|
1223 | * In this case, since we're loading from a registry, uuidMachineRegistry is
|
---|
1224 | * always set: it's either the global registry UUID or a machine UUID when
|
---|
1225 | * loading from a per-machine registry.
|
---|
1226 | *
|
---|
1227 | * @param aParent Parent medium disk or NULL for a root (base) medium.
|
---|
1228 | * @param aDeviceType Device type of the medium.
|
---|
1229 | * @param uuidMachineRegistry The registry to which this medium should be
|
---|
1230 | * added (global registry UUID or machine UUID).
|
---|
1231 | * @param data Configuration settings.
|
---|
1232 | * @param strMachineFolder The machine folder with which to resolve relative paths;
|
---|
1233 | * if empty, then we use the VirtualBox home directory
|
---|
1234 | *
|
---|
1235 | * @note Locks the medium tree for writing.
|
---|
1236 | */
|
---|
1237 | HRESULT Medium::initOne(Medium *aParent,
|
---|
1238 | DeviceType_T aDeviceType,
|
---|
1239 | const Guid &uuidMachineRegistry,
|
---|
1240 | const settings::Medium &data,
|
---|
1241 | const Utf8Str &strMachineFolder)
|
---|
1242 | {
|
---|
1243 | HRESULT rc;
|
---|
1244 |
|
---|
1245 | if (uuidMachineRegistry.isValid() && !uuidMachineRegistry.isZero())
|
---|
1246 | m->llRegistryIDs.push_back(uuidMachineRegistry);
|
---|
1247 |
|
---|
1248 | /* register with VirtualBox/parent early, since uninit() will
|
---|
1249 | * unconditionally unregister on failure */
|
---|
1250 | if (aParent)
|
---|
1251 | {
|
---|
1252 | // differencing medium: add to parent
|
---|
1253 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1254 | // no need to check maximum depth as settings reading did it
|
---|
1255 | i_setParent(aParent);
|
---|
1256 | }
|
---|
1257 |
|
---|
1258 | /* see below why we don't call Medium::i_queryInfo (and therefore treat
|
---|
1259 | * the medium as inaccessible for now */
|
---|
1260 | m->state = MediumState_Inaccessible;
|
---|
1261 | m->strLastAccessError = tr("Accessibility check was not yet performed");
|
---|
1262 |
|
---|
1263 | /* required */
|
---|
1264 | unconst(m->id) = data.uuid;
|
---|
1265 |
|
---|
1266 | /* assume not a host drive */
|
---|
1267 | m->hostDrive = false;
|
---|
1268 |
|
---|
1269 | /* optional */
|
---|
1270 | m->strDescription = data.strDescription;
|
---|
1271 |
|
---|
1272 | /* required */
|
---|
1273 | if (aDeviceType == DeviceType_HardDisk)
|
---|
1274 | {
|
---|
1275 | AssertReturn(!data.strFormat.isEmpty(), E_FAIL);
|
---|
1276 | rc = i_setFormat(data.strFormat);
|
---|
1277 | if (FAILED(rc)) return rc;
|
---|
1278 | }
|
---|
1279 | else
|
---|
1280 | {
|
---|
1281 | /// @todo handle host drive settings here as well?
|
---|
1282 | if (!data.strFormat.isEmpty())
|
---|
1283 | rc = i_setFormat(data.strFormat);
|
---|
1284 | else
|
---|
1285 | rc = i_setFormat("RAW");
|
---|
1286 | if (FAILED(rc)) return rc;
|
---|
1287 | }
|
---|
1288 |
|
---|
1289 | /* optional, only for diffs, default is false; we can only auto-reset
|
---|
1290 | * diff media so they must have a parent */
|
---|
1291 | if (aParent != NULL)
|
---|
1292 | m->autoReset = data.fAutoReset;
|
---|
1293 | else
|
---|
1294 | m->autoReset = false;
|
---|
1295 |
|
---|
1296 | /* properties (after setting the format as it populates the map). Note that
|
---|
1297 | * if some properties are not supported but present in the settings file,
|
---|
1298 | * they will still be read and accessible (for possible backward
|
---|
1299 | * compatibility; we can also clean them up from the XML upon next
|
---|
1300 | * XML format version change if we wish) */
|
---|
1301 | for (settings::StringsMap::const_iterator it = data.properties.begin();
|
---|
1302 | it != data.properties.end();
|
---|
1303 | ++it)
|
---|
1304 | {
|
---|
1305 | const Utf8Str &name = it->first;
|
---|
1306 | const Utf8Str &value = it->second;
|
---|
1307 | m->mapProperties[name] = value;
|
---|
1308 | }
|
---|
1309 |
|
---|
1310 | /* try to decrypt an optional iSCSI initiator secret */
|
---|
1311 | settings::StringsMap::const_iterator itCph = data.properties.find("InitiatorSecretEncrypted");
|
---|
1312 | if ( itCph != data.properties.end()
|
---|
1313 | && !itCph->second.isEmpty())
|
---|
1314 | {
|
---|
1315 | Utf8Str strPlaintext;
|
---|
1316 | int vrc = m->pVirtualBox->i_decryptSetting(&strPlaintext, itCph->second);
|
---|
1317 | if (RT_SUCCESS(vrc))
|
---|
1318 | m->mapProperties["InitiatorSecret"] = strPlaintext;
|
---|
1319 | }
|
---|
1320 |
|
---|
1321 | Utf8Str strFull;
|
---|
1322 | if (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
1323 | {
|
---|
1324 | // compose full path of the medium, if it's not fully qualified...
|
---|
1325 | // slightly convoluted logic here. If the caller has given us a
|
---|
1326 | // machine folder, then a relative path will be relative to that:
|
---|
1327 | if ( !strMachineFolder.isEmpty()
|
---|
1328 | && !RTPathStartsWithRoot(data.strLocation.c_str())
|
---|
1329 | )
|
---|
1330 | {
|
---|
1331 | strFull = strMachineFolder;
|
---|
1332 | strFull += RTPATH_SLASH;
|
---|
1333 | strFull += data.strLocation;
|
---|
1334 | }
|
---|
1335 | else
|
---|
1336 | {
|
---|
1337 | // Otherwise use the old VirtualBox "make absolute path" logic:
|
---|
1338 | rc = m->pVirtualBox->i_calculateFullPath(data.strLocation, strFull);
|
---|
1339 | if (FAILED(rc)) return rc;
|
---|
1340 | }
|
---|
1341 | }
|
---|
1342 | else
|
---|
1343 | strFull = data.strLocation;
|
---|
1344 |
|
---|
1345 | rc = i_setLocation(strFull);
|
---|
1346 | if (FAILED(rc)) return rc;
|
---|
1347 |
|
---|
1348 | if (aDeviceType == DeviceType_HardDisk)
|
---|
1349 | {
|
---|
1350 | /* type is only for base hard disks */
|
---|
1351 | if (m->pParent.isNull())
|
---|
1352 | m->type = data.hdType;
|
---|
1353 | }
|
---|
1354 | else if (aDeviceType == DeviceType_DVD)
|
---|
1355 | m->type = MediumType_Readonly;
|
---|
1356 | else
|
---|
1357 | m->type = MediumType_Writethrough;
|
---|
1358 |
|
---|
1359 | /* remember device type for correct unregistering later */
|
---|
1360 | m->devType = aDeviceType;
|
---|
1361 |
|
---|
1362 | LogFlowThisFunc(("m->strLocationFull='%s', m->strFormat=%s, m->id={%RTuuid}\n",
|
---|
1363 | m->strLocationFull.c_str(), m->strFormat.c_str(), m->id.raw()));
|
---|
1364 |
|
---|
1365 | return S_OK;
|
---|
1366 | }
|
---|
1367 |
|
---|
1368 | /**
|
---|
1369 | * Initializes the medium object and its children by loading its data from the
|
---|
1370 | * given settings node. The medium will always be opened read/write.
|
---|
1371 | *
|
---|
1372 | * In this case, since we're loading from a registry, uuidMachineRegistry is
|
---|
1373 | * always set: it's either the global registry UUID or a machine UUID when
|
---|
1374 | * loading from a per-machine registry.
|
---|
1375 | *
|
---|
1376 | * @param aVirtualBox VirtualBox object.
|
---|
1377 | * @param aParent Parent medium disk or NULL for a root (base) medium.
|
---|
1378 | * @param aDeviceType Device type of the medium.
|
---|
1379 | * @param uuidMachineRegistry The registry to which this medium should be added (global registry UUID or machine UUID).
|
---|
1380 | * @param data Configuration settings.
|
---|
1381 | * @param strMachineFolder The machine folder with which to resolve relative paths; if empty, then we use the VirtualBox home directory
|
---|
1382 | *
|
---|
1383 | * @note Locks the medium tree for writing.
|
---|
1384 | */
|
---|
1385 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1386 | Medium *aParent,
|
---|
1387 | DeviceType_T aDeviceType,
|
---|
1388 | const Guid &uuidMachineRegistry,
|
---|
1389 | const settings::Medium &data,
|
---|
1390 | const Utf8Str &strMachineFolder,
|
---|
1391 | AutoWriteLock &mediaTreeLock)
|
---|
1392 | {
|
---|
1393 | using namespace settings;
|
---|
1394 |
|
---|
1395 | Assert(aVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
1396 | AssertReturn(aVirtualBox, E_INVALIDARG);
|
---|
1397 |
|
---|
1398 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1399 | AutoInitSpan autoInitSpan(this);
|
---|
1400 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1401 |
|
---|
1402 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1403 |
|
---|
1404 | // Do not inline this method call, as the purpose of having this separate
|
---|
1405 | // is to save on stack size. Less local variables are the key for reaching
|
---|
1406 | // deep recursion levels with small stack (XPCOM/g++ without optimization).
|
---|
1407 | HRESULT rc = initOne(aParent, aDeviceType, uuidMachineRegistry, data, strMachineFolder);
|
---|
1408 |
|
---|
1409 |
|
---|
1410 | /* Don't call Medium::i_queryInfo for registered media to prevent the calling
|
---|
1411 | * thread (i.e. the VirtualBox server startup thread) from an unexpected
|
---|
1412 | * freeze but mark it as initially inaccessible instead. The vital UUID,
|
---|
1413 | * location and format properties are read from the registry file above; to
|
---|
1414 | * get the actual state and the rest of the data, the user will have to call
|
---|
1415 | * COMGETTER(State). */
|
---|
1416 |
|
---|
1417 | /* load all children */
|
---|
1418 | for (settings::MediaList::const_iterator it = data.llChildren.begin();
|
---|
1419 | it != data.llChildren.end();
|
---|
1420 | ++it)
|
---|
1421 | {
|
---|
1422 | const settings::Medium &med = *it;
|
---|
1423 |
|
---|
1424 | ComObjPtr<Medium> pMedium;
|
---|
1425 | pMedium.createObject();
|
---|
1426 | rc = pMedium->init(aVirtualBox,
|
---|
1427 | this, // parent
|
---|
1428 | aDeviceType,
|
---|
1429 | uuidMachineRegistry,
|
---|
1430 | med, // child data
|
---|
1431 | strMachineFolder,
|
---|
1432 | mediaTreeLock);
|
---|
1433 | if (FAILED(rc)) break;
|
---|
1434 |
|
---|
1435 | rc = m->pVirtualBox->i_registerMedium(pMedium, &pMedium, mediaTreeLock);
|
---|
1436 | if (FAILED(rc)) break;
|
---|
1437 | }
|
---|
1438 |
|
---|
1439 | /* Confirm a successful initialization when it's the case */
|
---|
1440 | if (SUCCEEDED(rc))
|
---|
1441 | autoInitSpan.setSucceeded();
|
---|
1442 |
|
---|
1443 | return rc;
|
---|
1444 | }
|
---|
1445 |
|
---|
1446 | /**
|
---|
1447 | * Initializes the medium object by providing the host drive information.
|
---|
1448 | * Not used for anything but the host floppy/host DVD case.
|
---|
1449 | *
|
---|
1450 | * There is no registry for this case.
|
---|
1451 | *
|
---|
1452 | * @param aVirtualBox VirtualBox object.
|
---|
1453 | * @param aDeviceType Device type of the medium.
|
---|
1454 | * @param aLocation Location of the host drive.
|
---|
1455 | * @param aDescription Comment for this host drive.
|
---|
1456 | *
|
---|
1457 | * @note Locks VirtualBox lock for writing.
|
---|
1458 | */
|
---|
1459 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1460 | DeviceType_T aDeviceType,
|
---|
1461 | const Utf8Str &aLocation,
|
---|
1462 | const Utf8Str &aDescription /* = Utf8Str::Empty */)
|
---|
1463 | {
|
---|
1464 | ComAssertRet(aDeviceType == DeviceType_DVD || aDeviceType == DeviceType_Floppy, E_INVALIDARG);
|
---|
1465 | ComAssertRet(!aLocation.isEmpty(), E_INVALIDARG);
|
---|
1466 |
|
---|
1467 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1468 | AutoInitSpan autoInitSpan(this);
|
---|
1469 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1470 |
|
---|
1471 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1472 |
|
---|
1473 | // We do not store host drives in VirtualBox.xml or anywhere else, so if we want
|
---|
1474 | // host drives to be identifiable by UUID and not give the drive a different UUID
|
---|
1475 | // every time VirtualBox starts, we need to fake a reproducible UUID here:
|
---|
1476 | RTUUID uuid;
|
---|
1477 | RTUuidClear(&uuid);
|
---|
1478 | if (aDeviceType == DeviceType_DVD)
|
---|
1479 | memcpy(&uuid.au8[0], "DVD", 3);
|
---|
1480 | else
|
---|
1481 | memcpy(&uuid.au8[0], "FD", 2);
|
---|
1482 | /* use device name, adjusted to the end of uuid, shortened if necessary */
|
---|
1483 | size_t lenLocation = aLocation.length();
|
---|
1484 | if (lenLocation > 12)
|
---|
1485 | memcpy(&uuid.au8[4], aLocation.c_str() + (lenLocation - 12), 12);
|
---|
1486 | else
|
---|
1487 | memcpy(&uuid.au8[4 + 12 - lenLocation], aLocation.c_str(), lenLocation);
|
---|
1488 | unconst(m->id) = uuid;
|
---|
1489 |
|
---|
1490 | if (aDeviceType == DeviceType_DVD)
|
---|
1491 | m->type = MediumType_Readonly;
|
---|
1492 | else
|
---|
1493 | m->type = MediumType_Writethrough;
|
---|
1494 | m->devType = aDeviceType;
|
---|
1495 | m->state = MediumState_Created;
|
---|
1496 | m->hostDrive = true;
|
---|
1497 | HRESULT rc = i_setFormat("RAW");
|
---|
1498 | if (FAILED(rc)) return rc;
|
---|
1499 | rc = i_setLocation(aLocation);
|
---|
1500 | if (FAILED(rc)) return rc;
|
---|
1501 | m->strDescription = aDescription;
|
---|
1502 |
|
---|
1503 | autoInitSpan.setSucceeded();
|
---|
1504 | return S_OK;
|
---|
1505 | }
|
---|
1506 |
|
---|
1507 | /**
|
---|
1508 | * Uninitializes the instance.
|
---|
1509 | *
|
---|
1510 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
1511 | *
|
---|
1512 | * @note All children of this medium get uninitialized by calling their
|
---|
1513 | * uninit() methods.
|
---|
1514 | */
|
---|
1515 | void Medium::uninit()
|
---|
1516 | {
|
---|
1517 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1518 | * the pVirtualBox reference, and in this case we don't need to continue.
|
---|
1519 | * Normally this would be handled through the AutoUninitSpan magic,
|
---|
1520 | * however this cannot be done at this point as the media tree must be
|
---|
1521 | * locked before reaching the AutoUninitSpan, otherwise deadlocks can
|
---|
1522 | * happen due to*/
|
---|
1523 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
1524 | if (!pVirtualBox)
|
---|
1525 | return;
|
---|
1526 |
|
---|
1527 | /* Caller must not hold the object or media tree lock over uninit(). */
|
---|
1528 | Assert(!isWriteLockOnCurrentThread());
|
---|
1529 | Assert(!pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
1530 |
|
---|
1531 | AutoWriteLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1532 |
|
---|
1533 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
1534 | AutoUninitSpan autoUninitSpan(this);
|
---|
1535 | if (autoUninitSpan.uninitDone())
|
---|
1536 | return;
|
---|
1537 |
|
---|
1538 | if (!m->formatObj.isNull())
|
---|
1539 | m->formatObj.setNull();
|
---|
1540 |
|
---|
1541 | if (m->state == MediumState_Deleting)
|
---|
1542 | {
|
---|
1543 | /* This medium has been already deleted (directly or as part of a
|
---|
1544 | * merge). Reparenting has already been done. */
|
---|
1545 | Assert(m->pParent.isNull());
|
---|
1546 | }
|
---|
1547 | else
|
---|
1548 | {
|
---|
1549 | MediaList llChildren(m->llChildren);
|
---|
1550 | m->llChildren.clear();
|
---|
1551 | autoUninitSpan.setSucceeded();
|
---|
1552 |
|
---|
1553 | while (!llChildren.empty())
|
---|
1554 | {
|
---|
1555 | ComObjPtr<Medium> pChild = llChildren.front();
|
---|
1556 | llChildren.pop_front();
|
---|
1557 | pChild->m->pParent.setNull();
|
---|
1558 | treeLock.release();
|
---|
1559 | pChild->uninit();
|
---|
1560 | treeLock.acquire();
|
---|
1561 | }
|
---|
1562 |
|
---|
1563 | if (m->pParent)
|
---|
1564 | {
|
---|
1565 | // this is a differencing disk: then remove it from the parent's children list
|
---|
1566 | i_deparent();
|
---|
1567 | }
|
---|
1568 | }
|
---|
1569 |
|
---|
1570 | unconst(m->pVirtualBox) = NULL;
|
---|
1571 | }
|
---|
1572 |
|
---|
1573 | /**
|
---|
1574 | * Internal helper that removes "this" from the list of children of its
|
---|
1575 | * parent. Used in uninit() and other places when reparenting is necessary.
|
---|
1576 | *
|
---|
1577 | * The caller must hold the medium tree lock!
|
---|
1578 | */
|
---|
1579 | void Medium::i_deparent()
|
---|
1580 | {
|
---|
1581 | MediaList &llParent = m->pParent->m->llChildren;
|
---|
1582 | for (MediaList::iterator it = llParent.begin();
|
---|
1583 | it != llParent.end();
|
---|
1584 | ++it)
|
---|
1585 | {
|
---|
1586 | Medium *pParentsChild = *it;
|
---|
1587 | if (this == pParentsChild)
|
---|
1588 | {
|
---|
1589 | llParent.erase(it);
|
---|
1590 | break;
|
---|
1591 | }
|
---|
1592 | }
|
---|
1593 | m->pParent.setNull();
|
---|
1594 | }
|
---|
1595 |
|
---|
1596 | /**
|
---|
1597 | * Internal helper that removes "this" from the list of children of its
|
---|
1598 | * parent. Used in uninit() and other places when reparenting is necessary.
|
---|
1599 | *
|
---|
1600 | * The caller must hold the medium tree lock!
|
---|
1601 | */
|
---|
1602 | void Medium::i_setParent(const ComObjPtr<Medium> &pParent)
|
---|
1603 | {
|
---|
1604 | m->pParent = pParent;
|
---|
1605 | if (pParent)
|
---|
1606 | pParent->m->llChildren.push_back(this);
|
---|
1607 | }
|
---|
1608 |
|
---|
1609 |
|
---|
1610 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1611 | //
|
---|
1612 | // IMedium public methods
|
---|
1613 | //
|
---|
1614 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1615 |
|
---|
1616 | HRESULT Medium::getId(com::Guid &aId)
|
---|
1617 | {
|
---|
1618 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1619 |
|
---|
1620 | aId = m->id;
|
---|
1621 |
|
---|
1622 | return S_OK;
|
---|
1623 | }
|
---|
1624 |
|
---|
1625 | HRESULT Medium::getDescription(com::Utf8Str &aDescription)
|
---|
1626 | {
|
---|
1627 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1628 |
|
---|
1629 | aDescription = m->strDescription;
|
---|
1630 |
|
---|
1631 | return S_OK;
|
---|
1632 | }
|
---|
1633 |
|
---|
1634 | HRESULT Medium::setDescription(const com::Utf8Str &aDescription)
|
---|
1635 | {
|
---|
1636 | // AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1637 |
|
---|
1638 | /// @todo update m->description and save the global registry (and local
|
---|
1639 | /// registries of portable VMs referring to this medium), this will also
|
---|
1640 | /// require to add the mRegistered flag to data
|
---|
1641 | NOREF(aDescription);
|
---|
1642 | ReturnComNotImplemented();
|
---|
1643 | }
|
---|
1644 |
|
---|
1645 | HRESULT Medium::getState(MediumState_T *aState)
|
---|
1646 | {
|
---|
1647 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1648 | *aState = m->state;
|
---|
1649 |
|
---|
1650 | return S_OK;
|
---|
1651 | }
|
---|
1652 |
|
---|
1653 | HRESULT Medium::getVariant(std::vector<MediumVariant_T> &aVariant)
|
---|
1654 | {
|
---|
1655 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1656 |
|
---|
1657 | const size_t cBits = sizeof(MediumVariant_T) * 8;
|
---|
1658 | aVariant.resize(cBits);
|
---|
1659 | for (size_t i = 0; i < cBits; ++i)
|
---|
1660 | aVariant[i] = (MediumVariant_T)(m->variant & RT_BIT(i));
|
---|
1661 |
|
---|
1662 | return S_OK;
|
---|
1663 | }
|
---|
1664 |
|
---|
1665 | HRESULT Medium::getLocation(com::Utf8Str &aLocation)
|
---|
1666 | {
|
---|
1667 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1668 |
|
---|
1669 | aLocation = m->strLocationFull;
|
---|
1670 |
|
---|
1671 | return S_OK;
|
---|
1672 | }
|
---|
1673 |
|
---|
1674 | HRESULT Medium::getName(com::Utf8Str &aName)
|
---|
1675 | {
|
---|
1676 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1677 |
|
---|
1678 | aName = i_getName();
|
---|
1679 |
|
---|
1680 | return S_OK;
|
---|
1681 | }
|
---|
1682 |
|
---|
1683 | HRESULT Medium::getDeviceType(DeviceType_T *aDeviceType)
|
---|
1684 | {
|
---|
1685 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1686 |
|
---|
1687 | *aDeviceType = m->devType;
|
---|
1688 |
|
---|
1689 | return S_OK;
|
---|
1690 | }
|
---|
1691 |
|
---|
1692 | HRESULT Medium::getHostDrive(BOOL *aHostDrive)
|
---|
1693 | {
|
---|
1694 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1695 |
|
---|
1696 | *aHostDrive = m->hostDrive;
|
---|
1697 |
|
---|
1698 | return S_OK;
|
---|
1699 | }
|
---|
1700 |
|
---|
1701 | HRESULT Medium::getSize(LONG64 *aSize)
|
---|
1702 | {
|
---|
1703 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1704 |
|
---|
1705 | *aSize = m->size;
|
---|
1706 |
|
---|
1707 | return S_OK;
|
---|
1708 | }
|
---|
1709 |
|
---|
1710 | HRESULT Medium::getFormat(com::Utf8Str &aFormat)
|
---|
1711 | {
|
---|
1712 | /* no need to lock, m->strFormat is const */
|
---|
1713 |
|
---|
1714 | aFormat = m->strFormat;
|
---|
1715 | return S_OK;
|
---|
1716 | }
|
---|
1717 |
|
---|
1718 | HRESULT Medium::getMediumFormat(ComPtr<IMediumFormat> &aMediumFormat)
|
---|
1719 | {
|
---|
1720 | /* no need to lock, m->formatObj is const */
|
---|
1721 | m->formatObj.queryInterfaceTo(aMediumFormat.asOutParam());
|
---|
1722 |
|
---|
1723 | return S_OK;
|
---|
1724 | }
|
---|
1725 |
|
---|
1726 | HRESULT Medium::getType(MediumType_T *aType)
|
---|
1727 | {
|
---|
1728 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1729 |
|
---|
1730 | *aType = m->type;
|
---|
1731 |
|
---|
1732 | return S_OK;
|
---|
1733 | }
|
---|
1734 |
|
---|
1735 | HRESULT Medium::setType(MediumType_T aType)
|
---|
1736 | {
|
---|
1737 | // we access mParent and members
|
---|
1738 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1739 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1740 |
|
---|
1741 | switch (m->state)
|
---|
1742 | {
|
---|
1743 | case MediumState_Created:
|
---|
1744 | case MediumState_Inaccessible:
|
---|
1745 | break;
|
---|
1746 | default:
|
---|
1747 | return i_setStateError();
|
---|
1748 | }
|
---|
1749 |
|
---|
1750 | if (m->type == aType)
|
---|
1751 | {
|
---|
1752 | /* Nothing to do */
|
---|
1753 | return S_OK;
|
---|
1754 | }
|
---|
1755 |
|
---|
1756 | DeviceType_T devType = i_getDeviceType();
|
---|
1757 | // DVD media can only be readonly.
|
---|
1758 | if (devType == DeviceType_DVD && aType != MediumType_Readonly)
|
---|
1759 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1760 | tr("Cannot change the type of DVD medium '%s'"),
|
---|
1761 | m->strLocationFull.c_str());
|
---|
1762 | // Floppy media can only be writethrough or readonly.
|
---|
1763 | if ( devType == DeviceType_Floppy
|
---|
1764 | && aType != MediumType_Writethrough
|
---|
1765 | && aType != MediumType_Readonly)
|
---|
1766 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1767 | tr("Cannot change the type of floppy medium '%s'"),
|
---|
1768 | m->strLocationFull.c_str());
|
---|
1769 |
|
---|
1770 | /* cannot change the type of a differencing medium */
|
---|
1771 | if (m->pParent)
|
---|
1772 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1773 | tr("Cannot change the type of medium '%s' because it is a differencing medium"),
|
---|
1774 | m->strLocationFull.c_str());
|
---|
1775 |
|
---|
1776 | /* Cannot change the type of a medium being in use by more than one VM.
|
---|
1777 | * If the change is to Immutable or MultiAttach then it must not be
|
---|
1778 | * directly attached to any VM, otherwise the assumptions about indirect
|
---|
1779 | * attachment elsewhere are violated and the VM becomes inaccessible.
|
---|
1780 | * Attaching an immutable medium triggers the diff creation, and this is
|
---|
1781 | * vital for the correct operation. */
|
---|
1782 | if ( m->backRefs.size() > 1
|
---|
1783 | || ( ( aType == MediumType_Immutable
|
---|
1784 | || aType == MediumType_MultiAttach)
|
---|
1785 | && m->backRefs.size() > 0))
|
---|
1786 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1787 | tr("Cannot change the type of medium '%s' because it is attached to %d virtual machines"),
|
---|
1788 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
1789 |
|
---|
1790 | switch (aType)
|
---|
1791 | {
|
---|
1792 | case MediumType_Normal:
|
---|
1793 | case MediumType_Immutable:
|
---|
1794 | case MediumType_MultiAttach:
|
---|
1795 | {
|
---|
1796 | /* normal can be easily converted to immutable and vice versa even
|
---|
1797 | * if they have children as long as they are not attached to any
|
---|
1798 | * machine themselves */
|
---|
1799 | break;
|
---|
1800 | }
|
---|
1801 | case MediumType_Writethrough:
|
---|
1802 | case MediumType_Shareable:
|
---|
1803 | case MediumType_Readonly:
|
---|
1804 | {
|
---|
1805 | /* cannot change to writethrough, shareable or readonly
|
---|
1806 | * if there are children */
|
---|
1807 | if (i_getChildren().size() != 0)
|
---|
1808 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
1809 | tr("Cannot change type for medium '%s' since it has %d child media"),
|
---|
1810 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
1811 | if (aType == MediumType_Shareable)
|
---|
1812 | {
|
---|
1813 | MediumVariant_T variant = i_getVariant();
|
---|
1814 | if (!(variant & MediumVariant_Fixed))
|
---|
1815 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1816 | tr("Cannot change type for medium '%s' to 'Shareable' since it is a dynamic medium storage unit"),
|
---|
1817 | m->strLocationFull.c_str());
|
---|
1818 | }
|
---|
1819 | else if (aType == MediumType_Readonly && devType == DeviceType_HardDisk)
|
---|
1820 | {
|
---|
1821 | // Readonly hard disks are not allowed, this medium type is reserved for
|
---|
1822 | // DVDs and floppy images at the moment. Later we might allow readonly hard
|
---|
1823 | // disks, but that's extremely unusual and many guest OSes will have trouble.
|
---|
1824 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1825 | tr("Cannot change type for medium '%s' to 'Readonly' since it is a hard disk"),
|
---|
1826 | m->strLocationFull.c_str());
|
---|
1827 | }
|
---|
1828 | break;
|
---|
1829 | }
|
---|
1830 | default:
|
---|
1831 | AssertFailedReturn(E_FAIL);
|
---|
1832 | }
|
---|
1833 |
|
---|
1834 | if (aType == MediumType_MultiAttach)
|
---|
1835 | {
|
---|
1836 | // This type is new with VirtualBox 4.0 and therefore requires settings
|
---|
1837 | // version 1.11 in the settings backend. Unfortunately it is not enough to do
|
---|
1838 | // the usual routine in MachineConfigFile::bumpSettingsVersionIfNeeded() for
|
---|
1839 | // two reasons: The medium type is a property of the media registry tree, which
|
---|
1840 | // can reside in the global config file (for pre-4.0 media); we would therefore
|
---|
1841 | // possibly need to bump the global config version. We don't want to do that though
|
---|
1842 | // because that might make downgrading to pre-4.0 impossible.
|
---|
1843 | // As a result, we can only use these two new types if the medium is NOT in the
|
---|
1844 | // global registry:
|
---|
1845 | const Guid &uuidGlobalRegistry = m->pVirtualBox->i_getGlobalRegistryId();
|
---|
1846 | if (i_isInRegistry(uuidGlobalRegistry))
|
---|
1847 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1848 | tr("Cannot change type for medium '%s': the media type 'MultiAttach' can only be used "
|
---|
1849 | "on media registered with a machine that was created with VirtualBox 4.0 or later"),
|
---|
1850 | m->strLocationFull.c_str());
|
---|
1851 | }
|
---|
1852 |
|
---|
1853 | m->type = aType;
|
---|
1854 |
|
---|
1855 | // save the settings
|
---|
1856 | mlock.release();
|
---|
1857 | treeLock.release();
|
---|
1858 | i_markRegistriesModified();
|
---|
1859 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
1860 |
|
---|
1861 | return S_OK;
|
---|
1862 | }
|
---|
1863 |
|
---|
1864 | HRESULT Medium::getAllowedTypes(std::vector<MediumType_T> &aAllowedTypes)
|
---|
1865 | {
|
---|
1866 | NOREF(aAllowedTypes);
|
---|
1867 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1868 |
|
---|
1869 | ReturnComNotImplemented();
|
---|
1870 | }
|
---|
1871 |
|
---|
1872 | HRESULT Medium::getParent(AutoCaller &autoCaller, ComPtr<IMedium> &aParent)
|
---|
1873 | {
|
---|
1874 | autoCaller.release();
|
---|
1875 |
|
---|
1876 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1877 | * the pVirtualBox reference, see #uninit(). */
|
---|
1878 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
1879 |
|
---|
1880 | /* we access mParent */
|
---|
1881 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
1882 |
|
---|
1883 | autoCaller.add();
|
---|
1884 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1885 |
|
---|
1886 | m->pParent.queryInterfaceTo(aParent.asOutParam());
|
---|
1887 |
|
---|
1888 | return S_OK;
|
---|
1889 | }
|
---|
1890 |
|
---|
1891 | HRESULT Medium::getChildren(AutoCaller &autoCaller, std::vector<ComPtr<IMedium> > &aChildren)
|
---|
1892 | {
|
---|
1893 | autoCaller.release();
|
---|
1894 |
|
---|
1895 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1896 | * the pVirtualBox reference, see #uninit(). */
|
---|
1897 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
1898 |
|
---|
1899 | /* we access children */
|
---|
1900 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
1901 |
|
---|
1902 | autoCaller.add();
|
---|
1903 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1904 |
|
---|
1905 | MediaList children(this->i_getChildren());
|
---|
1906 | aChildren.resize(children.size());
|
---|
1907 | size_t i = 0;
|
---|
1908 | for (MediaList::const_iterator it = children.begin(); it != children.end(); ++it, ++i)
|
---|
1909 | (*it).queryInterfaceTo(aChildren[i].asOutParam());
|
---|
1910 | return S_OK;
|
---|
1911 | }
|
---|
1912 |
|
---|
1913 | HRESULT Medium::getBase(AutoCaller &autoCaller, ComPtr<IMedium> &aBase)
|
---|
1914 | {
|
---|
1915 | autoCaller.release();
|
---|
1916 |
|
---|
1917 | /* i_getBase() will do callers/locking */
|
---|
1918 | i_getBase().queryInterfaceTo(aBase.asOutParam());
|
---|
1919 |
|
---|
1920 | return S_OK;
|
---|
1921 | }
|
---|
1922 |
|
---|
1923 | HRESULT Medium::getReadOnly(BOOL *aReadOnly)
|
---|
1924 | {
|
---|
1925 | /* isReadOnly() will do locking */
|
---|
1926 | *aReadOnly = i_isReadOnly();
|
---|
1927 |
|
---|
1928 | return S_OK;
|
---|
1929 | }
|
---|
1930 |
|
---|
1931 | HRESULT Medium::getLogicalSize(LONG64 *aLogicalSize)
|
---|
1932 | {
|
---|
1933 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1934 |
|
---|
1935 | *aLogicalSize = m->logicalSize;
|
---|
1936 |
|
---|
1937 | return S_OK;
|
---|
1938 | }
|
---|
1939 |
|
---|
1940 | HRESULT Medium::getAutoReset(BOOL *aAutoReset)
|
---|
1941 | {
|
---|
1942 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1943 |
|
---|
1944 | if (m->pParent.isNull())
|
---|
1945 | *aAutoReset = FALSE;
|
---|
1946 | else
|
---|
1947 | *aAutoReset = m->autoReset;
|
---|
1948 |
|
---|
1949 | return S_OK;
|
---|
1950 | }
|
---|
1951 |
|
---|
1952 | HRESULT Medium::setAutoReset(BOOL aAutoReset)
|
---|
1953 | {
|
---|
1954 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1955 |
|
---|
1956 | if (m->pParent.isNull())
|
---|
1957 | return setError(VBOX_E_NOT_SUPPORTED,
|
---|
1958 | tr("Medium '%s' is not differencing"),
|
---|
1959 | m->strLocationFull.c_str());
|
---|
1960 |
|
---|
1961 | if (m->autoReset != !!aAutoReset)
|
---|
1962 | {
|
---|
1963 | m->autoReset = !!aAutoReset;
|
---|
1964 |
|
---|
1965 | // save the settings
|
---|
1966 | mlock.release();
|
---|
1967 | i_markRegistriesModified();
|
---|
1968 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
1969 | }
|
---|
1970 |
|
---|
1971 | return S_OK;
|
---|
1972 | }
|
---|
1973 |
|
---|
1974 | HRESULT Medium::getLastAccessError(com::Utf8Str &aLastAccessError)
|
---|
1975 | {
|
---|
1976 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1977 |
|
---|
1978 | aLastAccessError = m->strLastAccessError;
|
---|
1979 |
|
---|
1980 | return S_OK;
|
---|
1981 | }
|
---|
1982 |
|
---|
1983 | HRESULT Medium::getMachineIds(std::vector<com::Guid> &aMachineIds)
|
---|
1984 | {
|
---|
1985 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1986 |
|
---|
1987 | if (m->backRefs.size() != 0)
|
---|
1988 | {
|
---|
1989 | BackRefList brlist(m->backRefs);
|
---|
1990 | aMachineIds.resize(brlist.size());
|
---|
1991 | size_t i = 0;
|
---|
1992 | for (BackRefList::const_iterator it = brlist.begin(); it != brlist.end(); ++it, ++i)
|
---|
1993 | aMachineIds[i] = it->machineId;
|
---|
1994 | }
|
---|
1995 |
|
---|
1996 | return S_OK;
|
---|
1997 | }
|
---|
1998 |
|
---|
1999 | HRESULT Medium::setIds(AutoCaller &autoCaller,
|
---|
2000 | BOOL aSetImageId,
|
---|
2001 | const com::Guid &aImageId,
|
---|
2002 | BOOL aSetParentId,
|
---|
2003 | const com::Guid &aParentId)
|
---|
2004 | {
|
---|
2005 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2006 |
|
---|
2007 | switch (m->state)
|
---|
2008 | {
|
---|
2009 | case MediumState_Created:
|
---|
2010 | break;
|
---|
2011 | default:
|
---|
2012 | return i_setStateError();
|
---|
2013 | }
|
---|
2014 |
|
---|
2015 | Guid imageId, parentId;
|
---|
2016 | if (aSetImageId)
|
---|
2017 | {
|
---|
2018 | if (aImageId.isZero())
|
---|
2019 | imageId.create();
|
---|
2020 | else
|
---|
2021 | {
|
---|
2022 | imageId = aImageId;
|
---|
2023 | if (!imageId.isValid())
|
---|
2024 | return setError(E_INVALIDARG, tr("Argument %s is invalid"), "aImageId");
|
---|
2025 | }
|
---|
2026 | }
|
---|
2027 | if (aSetParentId)
|
---|
2028 | {
|
---|
2029 | if (aParentId.isZero())
|
---|
2030 | parentId.create();
|
---|
2031 | else
|
---|
2032 | parentId = aParentId;
|
---|
2033 | }
|
---|
2034 |
|
---|
2035 | unconst(m->uuidImage) = imageId;
|
---|
2036 | unconst(m->uuidParentImage) = parentId;
|
---|
2037 |
|
---|
2038 | // must not hold any locks before calling Medium::i_queryInfo
|
---|
2039 | alock.release();
|
---|
2040 |
|
---|
2041 | HRESULT rc = i_queryInfo(!!aSetImageId /* fSetImageId */,
|
---|
2042 | !!aSetParentId /* fSetParentId */,
|
---|
2043 | autoCaller);
|
---|
2044 |
|
---|
2045 | return rc;
|
---|
2046 | }
|
---|
2047 |
|
---|
2048 | HRESULT Medium::refreshState(AutoCaller &autoCaller, MediumState_T *aState)
|
---|
2049 | {
|
---|
2050 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2051 |
|
---|
2052 | HRESULT rc = S_OK;
|
---|
2053 |
|
---|
2054 | switch (m->state)
|
---|
2055 | {
|
---|
2056 | case MediumState_Created:
|
---|
2057 | case MediumState_Inaccessible:
|
---|
2058 | case MediumState_LockedRead:
|
---|
2059 | {
|
---|
2060 | // must not hold any locks before calling Medium::i_queryInfo
|
---|
2061 | alock.release();
|
---|
2062 |
|
---|
2063 | rc = i_queryInfo(false /* fSetImageId */, false /* fSetParentId */,
|
---|
2064 | autoCaller);
|
---|
2065 |
|
---|
2066 | alock.acquire();
|
---|
2067 | break;
|
---|
2068 | }
|
---|
2069 | default:
|
---|
2070 | break;
|
---|
2071 | }
|
---|
2072 |
|
---|
2073 | *aState = m->state;
|
---|
2074 |
|
---|
2075 | return rc;
|
---|
2076 | }
|
---|
2077 |
|
---|
2078 | HRESULT Medium::getSnapshotIds(const com::Guid &aMachineId,
|
---|
2079 | std::vector<com::Guid> &aSnapshotIds)
|
---|
2080 | {
|
---|
2081 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2082 |
|
---|
2083 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
2084 | it != m->backRefs.end(); ++it)
|
---|
2085 | {
|
---|
2086 | if (it->machineId == aMachineId)
|
---|
2087 | {
|
---|
2088 | size_t size = it->llSnapshotIds.size();
|
---|
2089 |
|
---|
2090 | /* if the medium is attached to the machine in the current state, we
|
---|
2091 | * return its ID as the first element of the array */
|
---|
2092 | if (it->fInCurState)
|
---|
2093 | ++size;
|
---|
2094 |
|
---|
2095 | if (size > 0)
|
---|
2096 | {
|
---|
2097 | aSnapshotIds.resize(size);
|
---|
2098 |
|
---|
2099 | size_t j = 0;
|
---|
2100 | if (it->fInCurState)
|
---|
2101 | aSnapshotIds[j++] = it->machineId.toUtf16();
|
---|
2102 |
|
---|
2103 | for(GuidList::const_iterator jt = it->llSnapshotIds.begin(); jt != it->llSnapshotIds.end(); ++jt, ++j)
|
---|
2104 | aSnapshotIds[j] = (*jt);
|
---|
2105 | }
|
---|
2106 |
|
---|
2107 | break;
|
---|
2108 | }
|
---|
2109 | }
|
---|
2110 |
|
---|
2111 | return S_OK;
|
---|
2112 | }
|
---|
2113 |
|
---|
2114 | HRESULT Medium::lockRead(ComPtr<IToken> &aToken)
|
---|
2115 | {
|
---|
2116 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2117 |
|
---|
2118 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
2119 | if (m->queryInfoRunning)
|
---|
2120 | {
|
---|
2121 | /* Must not hold the media tree lock, as Medium::i_queryInfo needs this
|
---|
2122 | * lock and thus we would run into a deadlock here. */
|
---|
2123 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
2124 | while (m->queryInfoRunning)
|
---|
2125 | {
|
---|
2126 | alock.release();
|
---|
2127 | /* must not hold the object lock now */
|
---|
2128 | Assert(!isWriteLockOnCurrentThread());
|
---|
2129 | {
|
---|
2130 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
2131 | }
|
---|
2132 | alock.acquire();
|
---|
2133 | }
|
---|
2134 | }
|
---|
2135 |
|
---|
2136 | HRESULT rc = S_OK;
|
---|
2137 |
|
---|
2138 | switch (m->state)
|
---|
2139 | {
|
---|
2140 | case MediumState_Created:
|
---|
2141 | case MediumState_Inaccessible:
|
---|
2142 | case MediumState_LockedRead:
|
---|
2143 | {
|
---|
2144 | ++m->readers;
|
---|
2145 |
|
---|
2146 | ComAssertMsgBreak(m->readers != 0, ("Counter overflow"), rc = E_FAIL);
|
---|
2147 |
|
---|
2148 | /* Remember pre-lock state */
|
---|
2149 | if (m->state != MediumState_LockedRead)
|
---|
2150 | m->preLockState = m->state;
|
---|
2151 |
|
---|
2152 | LogFlowThisFunc(("Okay - prev state=%d readers=%d\n", m->state, m->readers));
|
---|
2153 | m->state = MediumState_LockedRead;
|
---|
2154 |
|
---|
2155 | ComObjPtr<MediumLockToken> pToken;
|
---|
2156 | rc = pToken.createObject();
|
---|
2157 | if (SUCCEEDED(rc))
|
---|
2158 | rc = pToken->init(this, false /* fWrite */);
|
---|
2159 | if (FAILED(rc))
|
---|
2160 | {
|
---|
2161 | --m->readers;
|
---|
2162 | if (m->readers == 0)
|
---|
2163 | m->state = m->preLockState;
|
---|
2164 | return rc;
|
---|
2165 | }
|
---|
2166 |
|
---|
2167 | pToken.queryInterfaceTo(aToken.asOutParam());
|
---|
2168 | break;
|
---|
2169 | }
|
---|
2170 | default:
|
---|
2171 | {
|
---|
2172 | LogFlowThisFunc(("Failing - state=%d\n", m->state));
|
---|
2173 | rc = i_setStateError();
|
---|
2174 | break;
|
---|
2175 | }
|
---|
2176 | }
|
---|
2177 |
|
---|
2178 | return rc;
|
---|
2179 | }
|
---|
2180 |
|
---|
2181 | /**
|
---|
2182 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
2183 | * in-process calls).
|
---|
2184 | */
|
---|
2185 | HRESULT Medium::i_unlockRead(MediumState_T *aState)
|
---|
2186 | {
|
---|
2187 | AutoCaller autoCaller(this);
|
---|
2188 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2189 |
|
---|
2190 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2191 |
|
---|
2192 | HRESULT rc = S_OK;
|
---|
2193 |
|
---|
2194 | switch (m->state)
|
---|
2195 | {
|
---|
2196 | case MediumState_LockedRead:
|
---|
2197 | {
|
---|
2198 | ComAssertMsgBreak(m->readers != 0, ("Counter underflow"), rc = E_FAIL);
|
---|
2199 | --m->readers;
|
---|
2200 |
|
---|
2201 | /* Reset the state after the last reader */
|
---|
2202 | if (m->readers == 0)
|
---|
2203 | {
|
---|
2204 | m->state = m->preLockState;
|
---|
2205 | /* There are cases where we inject the deleting state into
|
---|
2206 | * a medium locked for reading. Make sure #unmarkForDeletion()
|
---|
2207 | * gets the right state afterwards. */
|
---|
2208 | if (m->preLockState == MediumState_Deleting)
|
---|
2209 | m->preLockState = MediumState_Created;
|
---|
2210 | }
|
---|
2211 |
|
---|
2212 | LogFlowThisFunc(("new state=%d\n", m->state));
|
---|
2213 | break;
|
---|
2214 | }
|
---|
2215 | default:
|
---|
2216 | {
|
---|
2217 | LogFlowThisFunc(("Failing - state=%d\n", m->state));
|
---|
2218 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2219 | tr("Medium '%s' is not locked for reading"),
|
---|
2220 | m->strLocationFull.c_str());
|
---|
2221 | break;
|
---|
2222 | }
|
---|
2223 | }
|
---|
2224 |
|
---|
2225 | /* return the current state after */
|
---|
2226 | if (aState)
|
---|
2227 | *aState = m->state;
|
---|
2228 |
|
---|
2229 | return rc;
|
---|
2230 | }
|
---|
2231 | HRESULT Medium::lockWrite(ComPtr<IToken> &aToken)
|
---|
2232 | {
|
---|
2233 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2234 |
|
---|
2235 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
2236 | if (m->queryInfoRunning)
|
---|
2237 | {
|
---|
2238 | /* Must not hold the media tree lock, as Medium::i_queryInfo needs this
|
---|
2239 | * lock and thus we would run into a deadlock here. */
|
---|
2240 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
2241 | while (m->queryInfoRunning)
|
---|
2242 | {
|
---|
2243 | alock.release();
|
---|
2244 | /* must not hold the object lock now */
|
---|
2245 | Assert(!isWriteLockOnCurrentThread());
|
---|
2246 | {
|
---|
2247 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
2248 | }
|
---|
2249 | alock.acquire();
|
---|
2250 | }
|
---|
2251 | }
|
---|
2252 |
|
---|
2253 | HRESULT rc = S_OK;
|
---|
2254 |
|
---|
2255 | switch (m->state)
|
---|
2256 | {
|
---|
2257 | case MediumState_Created:
|
---|
2258 | case MediumState_Inaccessible:
|
---|
2259 | {
|
---|
2260 | m->preLockState = m->state;
|
---|
2261 |
|
---|
2262 | LogFlowThisFunc(("Okay - prev state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2263 | m->state = MediumState_LockedWrite;
|
---|
2264 |
|
---|
2265 | ComObjPtr<MediumLockToken> pToken;
|
---|
2266 | rc = pToken.createObject();
|
---|
2267 | if (SUCCEEDED(rc))
|
---|
2268 | rc = pToken->init(this, true /* fWrite */);
|
---|
2269 | if (FAILED(rc))
|
---|
2270 | {
|
---|
2271 | m->state = m->preLockState;
|
---|
2272 | return rc;
|
---|
2273 | }
|
---|
2274 |
|
---|
2275 | pToken.queryInterfaceTo(aToken.asOutParam());
|
---|
2276 | break;
|
---|
2277 | }
|
---|
2278 | default:
|
---|
2279 | {
|
---|
2280 | LogFlowThisFunc(("Failing - state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2281 | rc = i_setStateError();
|
---|
2282 | break;
|
---|
2283 | }
|
---|
2284 | }
|
---|
2285 |
|
---|
2286 | return rc;
|
---|
2287 | }
|
---|
2288 |
|
---|
2289 | /**
|
---|
2290 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
2291 | * in-process calls).
|
---|
2292 | */
|
---|
2293 | HRESULT Medium::i_unlockWrite(MediumState_T *aState)
|
---|
2294 | {
|
---|
2295 | AutoCaller autoCaller(this);
|
---|
2296 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2297 |
|
---|
2298 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2299 |
|
---|
2300 | HRESULT rc = S_OK;
|
---|
2301 |
|
---|
2302 | switch (m->state)
|
---|
2303 | {
|
---|
2304 | case MediumState_LockedWrite:
|
---|
2305 | {
|
---|
2306 | m->state = m->preLockState;
|
---|
2307 | /* There are cases where we inject the deleting state into
|
---|
2308 | * a medium locked for writing. Make sure #unmarkForDeletion()
|
---|
2309 | * gets the right state afterwards. */
|
---|
2310 | if (m->preLockState == MediumState_Deleting)
|
---|
2311 | m->preLockState = MediumState_Created;
|
---|
2312 | LogFlowThisFunc(("new state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2313 | break;
|
---|
2314 | }
|
---|
2315 | default:
|
---|
2316 | {
|
---|
2317 | LogFlowThisFunc(("Failing - state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2318 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2319 | tr("Medium '%s' is not locked for writing"),
|
---|
2320 | m->strLocationFull.c_str());
|
---|
2321 | break;
|
---|
2322 | }
|
---|
2323 | }
|
---|
2324 |
|
---|
2325 | /* return the current state after */
|
---|
2326 | if (aState)
|
---|
2327 | *aState = m->state;
|
---|
2328 |
|
---|
2329 | return rc;
|
---|
2330 | }
|
---|
2331 |
|
---|
2332 | HRESULT Medium::close(AutoCaller &aAutoCaller)
|
---|
2333 | {
|
---|
2334 | // make a copy of VirtualBox pointer which gets nulled by uninit()
|
---|
2335 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
2336 |
|
---|
2337 | MultiResult mrc = i_close(aAutoCaller);
|
---|
2338 |
|
---|
2339 | pVirtualBox->i_saveModifiedRegistries();
|
---|
2340 |
|
---|
2341 | return mrc;
|
---|
2342 | }
|
---|
2343 |
|
---|
2344 | HRESULT Medium::getProperty(const com::Utf8Str &aName,
|
---|
2345 | com::Utf8Str &aValue)
|
---|
2346 | {
|
---|
2347 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2348 |
|
---|
2349 | settings::StringsMap::const_iterator it = m->mapProperties.find(aName);
|
---|
2350 | if (it == m->mapProperties.end())
|
---|
2351 | {
|
---|
2352 | if (!aName.startsWith("Special/"))
|
---|
2353 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2354 | tr("Property '%s' does not exist"), aName.c_str());
|
---|
2355 | else
|
---|
2356 | /* be more silent here */
|
---|
2357 | return VBOX_E_OBJECT_NOT_FOUND;
|
---|
2358 | }
|
---|
2359 |
|
---|
2360 | aValue = it->second;
|
---|
2361 |
|
---|
2362 | return S_OK;
|
---|
2363 | }
|
---|
2364 |
|
---|
2365 | HRESULT Medium::setProperty(const com::Utf8Str &aName,
|
---|
2366 | const com::Utf8Str &aValue)
|
---|
2367 | {
|
---|
2368 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2369 |
|
---|
2370 | switch (m->state)
|
---|
2371 | {
|
---|
2372 | case MediumState_Created:
|
---|
2373 | case MediumState_Inaccessible:
|
---|
2374 | break;
|
---|
2375 | default:
|
---|
2376 | return i_setStateError();
|
---|
2377 | }
|
---|
2378 |
|
---|
2379 | settings::StringsMap::iterator it = m->mapProperties.find(aName);
|
---|
2380 | if ( !aName.startsWith("Special/")
|
---|
2381 | && !i_isPropertyForFilter(aName))
|
---|
2382 | {
|
---|
2383 | if (it == m->mapProperties.end())
|
---|
2384 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2385 | tr("Property '%s' does not exist"),
|
---|
2386 | aName.c_str());
|
---|
2387 | it->second = aValue;
|
---|
2388 | }
|
---|
2389 | else
|
---|
2390 | {
|
---|
2391 | if (it == m->mapProperties.end())
|
---|
2392 | {
|
---|
2393 | if (!aValue.isEmpty())
|
---|
2394 | m->mapProperties[aName] = aValue;
|
---|
2395 | }
|
---|
2396 | else
|
---|
2397 | {
|
---|
2398 | if (!aValue.isEmpty())
|
---|
2399 | it->second = aValue;
|
---|
2400 | else
|
---|
2401 | m->mapProperties.erase(it);
|
---|
2402 | }
|
---|
2403 | }
|
---|
2404 |
|
---|
2405 | // save the settings
|
---|
2406 | mlock.release();
|
---|
2407 | i_markRegistriesModified();
|
---|
2408 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2409 |
|
---|
2410 | return S_OK;
|
---|
2411 | }
|
---|
2412 |
|
---|
2413 | HRESULT Medium::getProperties(const com::Utf8Str &aNames,
|
---|
2414 | std::vector<com::Utf8Str> &aReturnNames,
|
---|
2415 | std::vector<com::Utf8Str> &aReturnValues)
|
---|
2416 | {
|
---|
2417 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2418 |
|
---|
2419 | /// @todo make use of aNames according to the documentation
|
---|
2420 | NOREF(aNames);
|
---|
2421 |
|
---|
2422 | aReturnNames.resize(m->mapProperties.size());
|
---|
2423 | aReturnValues.resize(m->mapProperties.size());
|
---|
2424 | size_t i = 0;
|
---|
2425 | for (settings::StringsMap::const_iterator it = m->mapProperties.begin();
|
---|
2426 | it != m->mapProperties.end();
|
---|
2427 | ++it, ++i)
|
---|
2428 | {
|
---|
2429 | aReturnNames[i] = it->first;
|
---|
2430 | aReturnValues[i] = it->second;
|
---|
2431 | }
|
---|
2432 | return S_OK;
|
---|
2433 | }
|
---|
2434 |
|
---|
2435 | HRESULT Medium::setProperties(const std::vector<com::Utf8Str> &aNames,
|
---|
2436 | const std::vector<com::Utf8Str> &aValues)
|
---|
2437 | {
|
---|
2438 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2439 |
|
---|
2440 | /* first pass: validate names */
|
---|
2441 | for (size_t i = 0;
|
---|
2442 | i < aNames.size();
|
---|
2443 | ++i)
|
---|
2444 | {
|
---|
2445 | Utf8Str strName(aNames[i]);
|
---|
2446 | if ( !strName.startsWith("Special/")
|
---|
2447 | && !i_isPropertyForFilter(strName)
|
---|
2448 | && m->mapProperties.find(strName) == m->mapProperties.end())
|
---|
2449 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2450 | tr("Property '%s' does not exist"), strName.c_str());
|
---|
2451 | }
|
---|
2452 |
|
---|
2453 | /* second pass: assign */
|
---|
2454 | for (size_t i = 0;
|
---|
2455 | i < aNames.size();
|
---|
2456 | ++i)
|
---|
2457 | {
|
---|
2458 | Utf8Str strName(aNames[i]);
|
---|
2459 | Utf8Str strValue(aValues[i]);
|
---|
2460 | settings::StringsMap::iterator it = m->mapProperties.find(strName);
|
---|
2461 | if ( !strName.startsWith("Special/")
|
---|
2462 | && !i_isPropertyForFilter(strName))
|
---|
2463 | {
|
---|
2464 | AssertReturn(it != m->mapProperties.end(), E_FAIL);
|
---|
2465 | it->second = strValue;
|
---|
2466 | }
|
---|
2467 | else
|
---|
2468 | {
|
---|
2469 | if (it == m->mapProperties.end())
|
---|
2470 | {
|
---|
2471 | if (!strValue.isEmpty())
|
---|
2472 | m->mapProperties[strName] = strValue;
|
---|
2473 | }
|
---|
2474 | else
|
---|
2475 | {
|
---|
2476 | if (!strValue.isEmpty())
|
---|
2477 | it->second = strValue;
|
---|
2478 | else
|
---|
2479 | m->mapProperties.erase(it);
|
---|
2480 | }
|
---|
2481 | }
|
---|
2482 | }
|
---|
2483 |
|
---|
2484 | // save the settings
|
---|
2485 | mlock.release();
|
---|
2486 | i_markRegistriesModified();
|
---|
2487 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2488 |
|
---|
2489 | return S_OK;
|
---|
2490 | }
|
---|
2491 | HRESULT Medium::createBaseStorage(LONG64 aLogicalSize,
|
---|
2492 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2493 | ComPtr<IProgress> &aProgress)
|
---|
2494 | {
|
---|
2495 | if (aLogicalSize < 0)
|
---|
2496 | return setError(E_INVALIDARG, tr("The medium size argument (%lld) is negative"), aLogicalSize);
|
---|
2497 |
|
---|
2498 | HRESULT rc = S_OK;
|
---|
2499 | ComObjPtr<Progress> pProgress;
|
---|
2500 | Medium::Task *pTask = NULL;
|
---|
2501 |
|
---|
2502 | try
|
---|
2503 | {
|
---|
2504 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2505 |
|
---|
2506 | ULONG mediumVariantFlags = 0;
|
---|
2507 |
|
---|
2508 | if (aVariant.size())
|
---|
2509 | {
|
---|
2510 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
2511 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
2512 | }
|
---|
2513 |
|
---|
2514 | mediumVariantFlags &= ((unsigned)~MediumVariant_Diff);
|
---|
2515 |
|
---|
2516 | if ( !(mediumVariantFlags & MediumVariant_Fixed)
|
---|
2517 | && !(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_CreateDynamic))
|
---|
2518 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2519 | tr("Medium format '%s' does not support dynamic storage creation"),
|
---|
2520 | m->strFormat.c_str());
|
---|
2521 |
|
---|
2522 | if ( (mediumVariantFlags & MediumVariant_Fixed)
|
---|
2523 | && !(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_CreateFixed))
|
---|
2524 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2525 | tr("Medium format '%s' does not support fixed storage creation"),
|
---|
2526 | m->strFormat.c_str());
|
---|
2527 |
|
---|
2528 | if (m->state != MediumState_NotCreated)
|
---|
2529 | throw i_setStateError();
|
---|
2530 |
|
---|
2531 | pProgress.createObject();
|
---|
2532 | rc = pProgress->init(m->pVirtualBox,
|
---|
2533 | static_cast<IMedium*>(this),
|
---|
2534 | (mediumVariantFlags & MediumVariant_Fixed)
|
---|
2535 | ? BstrFmt(tr("Creating fixed medium storage unit '%s'"), m->strLocationFull.c_str()).raw()
|
---|
2536 | : BstrFmt(tr("Creating dynamic medium storage unit '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
2537 | TRUE /* aCancelable */);
|
---|
2538 | if (FAILED(rc))
|
---|
2539 | throw rc;
|
---|
2540 |
|
---|
2541 | /* setup task object to carry out the operation asynchronously */
|
---|
2542 | pTask = new Medium::CreateBaseTask(this, pProgress, aLogicalSize,
|
---|
2543 | (MediumVariant_T)mediumVariantFlags);
|
---|
2544 | //(MediumVariant_T)aVariant);
|
---|
2545 | rc = pTask->rc();
|
---|
2546 | AssertComRC(rc);
|
---|
2547 | if (FAILED(rc))
|
---|
2548 | throw rc;
|
---|
2549 |
|
---|
2550 | m->state = MediumState_Creating;
|
---|
2551 | }
|
---|
2552 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2553 |
|
---|
2554 | if (SUCCEEDED(rc))
|
---|
2555 | {
|
---|
2556 | rc = i_startThread(pTask);
|
---|
2557 |
|
---|
2558 | if (SUCCEEDED(rc))
|
---|
2559 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2560 | }
|
---|
2561 | else if (pTask != NULL)
|
---|
2562 | delete pTask;
|
---|
2563 |
|
---|
2564 | return rc;
|
---|
2565 | }
|
---|
2566 |
|
---|
2567 | HRESULT Medium::deleteStorage(ComPtr<IProgress> &aProgress)
|
---|
2568 | {
|
---|
2569 | ComObjPtr<Progress> pProgress;
|
---|
2570 |
|
---|
2571 | MultiResult mrc = i_deleteStorage(&pProgress,
|
---|
2572 | false /* aWait */);
|
---|
2573 | /* Must save the registries in any case, since an entry was removed. */
|
---|
2574 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2575 |
|
---|
2576 | if (SUCCEEDED(mrc))
|
---|
2577 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2578 |
|
---|
2579 | return mrc;
|
---|
2580 | }
|
---|
2581 |
|
---|
2582 | HRESULT Medium::createDiffStorage(const ComPtr<IMedium> &aTarget,
|
---|
2583 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2584 | ComPtr<IProgress> &aProgress)
|
---|
2585 | {
|
---|
2586 | IMedium *aT = aTarget;
|
---|
2587 | ComObjPtr<Medium> diff = static_cast<Medium*>(aT);
|
---|
2588 |
|
---|
2589 | // locking: we need the tree lock first because we access parent pointers
|
---|
2590 | AutoMultiWriteLock3 alock(&m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
2591 | this->lockHandle(), diff->lockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
2592 |
|
---|
2593 | if (m->type == MediumType_Writethrough)
|
---|
2594 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2595 | tr("Medium type of '%s' is Writethrough"),
|
---|
2596 | m->strLocationFull.c_str());
|
---|
2597 | else if (m->type == MediumType_Shareable)
|
---|
2598 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2599 | tr("Medium type of '%s' is Shareable"),
|
---|
2600 | m->strLocationFull.c_str());
|
---|
2601 | else if (m->type == MediumType_Readonly)
|
---|
2602 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2603 | tr("Medium type of '%s' is Readonly"),
|
---|
2604 | m->strLocationFull.c_str());
|
---|
2605 |
|
---|
2606 | /* Apply the normal locking logic to the entire chain. */
|
---|
2607 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
2608 | alock.release();
|
---|
2609 | HRESULT rc = diff->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2610 | true /* fMediumLockWrite */,
|
---|
2611 | false /* fMediumLockWriteAll */,
|
---|
2612 | this,
|
---|
2613 | *pMediumLockList);
|
---|
2614 | alock.acquire();
|
---|
2615 | if (FAILED(rc))
|
---|
2616 | {
|
---|
2617 | delete pMediumLockList;
|
---|
2618 | return rc;
|
---|
2619 | }
|
---|
2620 |
|
---|
2621 | alock.release();
|
---|
2622 | rc = pMediumLockList->Lock();
|
---|
2623 | alock.acquire();
|
---|
2624 | if (FAILED(rc))
|
---|
2625 | {
|
---|
2626 | delete pMediumLockList;
|
---|
2627 |
|
---|
2628 | return setError(rc, tr("Could not lock medium when creating diff '%s'"),
|
---|
2629 | diff->i_getLocationFull().c_str());
|
---|
2630 | }
|
---|
2631 |
|
---|
2632 | Guid parentMachineRegistry;
|
---|
2633 | if (i_getFirstRegistryMachineId(parentMachineRegistry))
|
---|
2634 | {
|
---|
2635 | /* since this medium has been just created it isn't associated yet */
|
---|
2636 | diff->m->llRegistryIDs.push_back(parentMachineRegistry);
|
---|
2637 | alock.release();
|
---|
2638 | diff->i_markRegistriesModified();
|
---|
2639 | alock.acquire();
|
---|
2640 | }
|
---|
2641 |
|
---|
2642 | alock.release();
|
---|
2643 |
|
---|
2644 | ComObjPtr<Progress> pProgress;
|
---|
2645 |
|
---|
2646 | ULONG mediumVariantFlags = 0;
|
---|
2647 |
|
---|
2648 | if (aVariant.size())
|
---|
2649 | {
|
---|
2650 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
2651 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
2652 | }
|
---|
2653 |
|
---|
2654 | rc = i_createDiffStorage(diff, (MediumVariant_T)mediumVariantFlags, pMediumLockList,
|
---|
2655 | &pProgress, false /* aWait */);
|
---|
2656 | if (FAILED(rc))
|
---|
2657 | delete pMediumLockList;
|
---|
2658 | else
|
---|
2659 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2660 |
|
---|
2661 | return rc;
|
---|
2662 | }
|
---|
2663 |
|
---|
2664 | HRESULT Medium::mergeTo(const ComPtr<IMedium> &aTarget,
|
---|
2665 | ComPtr<IProgress> &aProgress)
|
---|
2666 | {
|
---|
2667 |
|
---|
2668 | IMedium *aT = aTarget;
|
---|
2669 |
|
---|
2670 | ComAssertRet(aT != this, E_INVALIDARG);
|
---|
2671 |
|
---|
2672 | ComObjPtr<Medium> pTarget = static_cast<Medium*>(aT);
|
---|
2673 |
|
---|
2674 | bool fMergeForward = false;
|
---|
2675 | ComObjPtr<Medium> pParentForTarget;
|
---|
2676 | MediumLockList *pChildrenToReparent = NULL;
|
---|
2677 | MediumLockList *pMediumLockList = NULL;
|
---|
2678 |
|
---|
2679 | HRESULT rc = S_OK;
|
---|
2680 |
|
---|
2681 | rc = i_prepareMergeTo(pTarget, NULL, NULL, true, fMergeForward,
|
---|
2682 | pParentForTarget, pChildrenToReparent, pMediumLockList);
|
---|
2683 | if (FAILED(rc)) return rc;
|
---|
2684 |
|
---|
2685 | ComObjPtr<Progress> pProgress;
|
---|
2686 |
|
---|
2687 | rc = i_mergeTo(pTarget, fMergeForward, pParentForTarget, pChildrenToReparent,
|
---|
2688 | pMediumLockList, &pProgress, false /* aWait */);
|
---|
2689 | if (FAILED(rc))
|
---|
2690 | i_cancelMergeTo(pChildrenToReparent, pMediumLockList);
|
---|
2691 | else
|
---|
2692 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2693 |
|
---|
2694 | return rc;
|
---|
2695 | }
|
---|
2696 |
|
---|
2697 | HRESULT Medium::cloneToBase(const ComPtr<IMedium> &aTarget,
|
---|
2698 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2699 | ComPtr<IProgress> &aProgress)
|
---|
2700 | {
|
---|
2701 | int rc = S_OK;
|
---|
2702 |
|
---|
2703 | rc = cloneTo(aTarget, aVariant, NULL, aProgress);
|
---|
2704 | return rc;
|
---|
2705 | }
|
---|
2706 |
|
---|
2707 | HRESULT Medium::cloneTo(const ComPtr<IMedium> &aTarget,
|
---|
2708 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2709 | const ComPtr<IMedium> &aParent,
|
---|
2710 | ComPtr<IProgress> &aProgress)
|
---|
2711 | {
|
---|
2712 | ComAssertRet(aTarget != this, E_INVALIDARG);
|
---|
2713 |
|
---|
2714 | IMedium *aT = aTarget;
|
---|
2715 | ComObjPtr<Medium> pTarget = static_cast<Medium*>(aT);
|
---|
2716 | ComObjPtr<Medium> pParent;
|
---|
2717 | if (aParent)
|
---|
2718 | {
|
---|
2719 | IMedium *aP = aParent;
|
---|
2720 | pParent = static_cast<Medium*>(aP);
|
---|
2721 | }
|
---|
2722 |
|
---|
2723 | HRESULT rc = S_OK;
|
---|
2724 | ComObjPtr<Progress> pProgress;
|
---|
2725 | Medium::Task *pTask = NULL;
|
---|
2726 |
|
---|
2727 | try
|
---|
2728 | {
|
---|
2729 | // locking: we need the tree lock first because we access parent pointers
|
---|
2730 | // and we need to write-lock the media involved
|
---|
2731 | uint32_t cHandles = 3;
|
---|
2732 | LockHandle* pHandles[4] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
2733 | this->lockHandle(),
|
---|
2734 | pTarget->lockHandle() };
|
---|
2735 | /* Only add parent to the lock if it is not null */
|
---|
2736 | if (!pParent.isNull())
|
---|
2737 | pHandles[cHandles++] = pParent->lockHandle();
|
---|
2738 | AutoWriteLock alock(cHandles,
|
---|
2739 | pHandles
|
---|
2740 | COMMA_LOCKVAL_SRC_POS);
|
---|
2741 |
|
---|
2742 | if ( pTarget->m->state != MediumState_NotCreated
|
---|
2743 | && pTarget->m->state != MediumState_Created)
|
---|
2744 | throw pTarget->i_setStateError();
|
---|
2745 |
|
---|
2746 | /* Build the source lock list. */
|
---|
2747 | MediumLockList *pSourceMediumLockList(new MediumLockList());
|
---|
2748 | alock.release();
|
---|
2749 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2750 | false /* fMediumLockWrite */,
|
---|
2751 | false /* fMediumLockWriteAll */,
|
---|
2752 | NULL,
|
---|
2753 | *pSourceMediumLockList);
|
---|
2754 | alock.acquire();
|
---|
2755 | if (FAILED(rc))
|
---|
2756 | {
|
---|
2757 | delete pSourceMediumLockList;
|
---|
2758 | throw rc;
|
---|
2759 | }
|
---|
2760 |
|
---|
2761 | /* Build the target lock list (including the to-be parent chain). */
|
---|
2762 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
2763 | alock.release();
|
---|
2764 | rc = pTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2765 | true /* fMediumLockWrite */,
|
---|
2766 | false /* fMediumLockWriteAll */,
|
---|
2767 | pParent,
|
---|
2768 | *pTargetMediumLockList);
|
---|
2769 | alock.acquire();
|
---|
2770 | if (FAILED(rc))
|
---|
2771 | {
|
---|
2772 | delete pSourceMediumLockList;
|
---|
2773 | delete pTargetMediumLockList;
|
---|
2774 | throw rc;
|
---|
2775 | }
|
---|
2776 |
|
---|
2777 | alock.release();
|
---|
2778 | rc = pSourceMediumLockList->Lock();
|
---|
2779 | alock.acquire();
|
---|
2780 | if (FAILED(rc))
|
---|
2781 | {
|
---|
2782 | delete pSourceMediumLockList;
|
---|
2783 | delete pTargetMediumLockList;
|
---|
2784 | throw setError(rc,
|
---|
2785 | tr("Failed to lock source media '%s'"),
|
---|
2786 | i_getLocationFull().c_str());
|
---|
2787 | }
|
---|
2788 | alock.release();
|
---|
2789 | rc = pTargetMediumLockList->Lock();
|
---|
2790 | alock.acquire();
|
---|
2791 | if (FAILED(rc))
|
---|
2792 | {
|
---|
2793 | delete pSourceMediumLockList;
|
---|
2794 | delete pTargetMediumLockList;
|
---|
2795 | throw setError(rc,
|
---|
2796 | tr("Failed to lock target media '%s'"),
|
---|
2797 | pTarget->i_getLocationFull().c_str());
|
---|
2798 | }
|
---|
2799 |
|
---|
2800 | pProgress.createObject();
|
---|
2801 | rc = pProgress->init(m->pVirtualBox,
|
---|
2802 | static_cast <IMedium *>(this),
|
---|
2803 | BstrFmt(tr("Creating clone medium '%s'"), pTarget->m->strLocationFull.c_str()).raw(),
|
---|
2804 | TRUE /* aCancelable */);
|
---|
2805 | if (FAILED(rc))
|
---|
2806 | {
|
---|
2807 | delete pSourceMediumLockList;
|
---|
2808 | delete pTargetMediumLockList;
|
---|
2809 | throw rc;
|
---|
2810 | }
|
---|
2811 |
|
---|
2812 | ULONG mediumVariantFlags = 0;
|
---|
2813 |
|
---|
2814 | if (aVariant.size())
|
---|
2815 | {
|
---|
2816 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
2817 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
2818 | }
|
---|
2819 |
|
---|
2820 | /* setup task object to carry out the operation asynchronously */
|
---|
2821 | pTask = new Medium::CloneTask(this, pProgress, pTarget,
|
---|
2822 | (MediumVariant_T)mediumVariantFlags,
|
---|
2823 | pParent, UINT32_MAX, UINT32_MAX,
|
---|
2824 | pSourceMediumLockList, pTargetMediumLockList);
|
---|
2825 | rc = pTask->rc();
|
---|
2826 | AssertComRC(rc);
|
---|
2827 | if (FAILED(rc))
|
---|
2828 | throw rc;
|
---|
2829 |
|
---|
2830 | if (pTarget->m->state == MediumState_NotCreated)
|
---|
2831 | pTarget->m->state = MediumState_Creating;
|
---|
2832 | }
|
---|
2833 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2834 |
|
---|
2835 | if (SUCCEEDED(rc))
|
---|
2836 | {
|
---|
2837 | rc = i_startThread(pTask);
|
---|
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::setLocation(const com::Utf8Str &aLocation, ComPtr<IProgress> &aProgress)
|
---|
2849 | {
|
---|
2850 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2851 | NOREF(aLocation);
|
---|
2852 | NOREF(aProgress);
|
---|
2853 |
|
---|
2854 | /// @todo NEWMEDIA for file names, add the default extension if no extension
|
---|
2855 | /// is present (using the information from the VD backend which also implies
|
---|
2856 | /// that one more parameter should be passed to setLocation() requesting
|
---|
2857 | /// that functionality since it is only allowed when called from this method
|
---|
2858 |
|
---|
2859 | /// @todo NEWMEDIA rename the file and set m->location on success, then save
|
---|
2860 | /// the global registry (and local registries of portable VMs referring to
|
---|
2861 | /// this medium), this will also require to add the mRegistered flag to data
|
---|
2862 |
|
---|
2863 | ReturnComNotImplemented();
|
---|
2864 | }
|
---|
2865 |
|
---|
2866 | HRESULT Medium::compact(ComPtr<IProgress> &aProgress)
|
---|
2867 | {
|
---|
2868 | HRESULT rc = S_OK;
|
---|
2869 | ComObjPtr<Progress> pProgress;
|
---|
2870 | Medium::Task *pTask = NULL;
|
---|
2871 |
|
---|
2872 | try
|
---|
2873 | {
|
---|
2874 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2875 |
|
---|
2876 | /* Build the medium lock list. */
|
---|
2877 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
2878 | alock.release();
|
---|
2879 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
2880 | true /* fMediumLockWrite */,
|
---|
2881 | false /* fMediumLockWriteAll */,
|
---|
2882 | NULL,
|
---|
2883 | *pMediumLockList);
|
---|
2884 | alock.acquire();
|
---|
2885 | if (FAILED(rc))
|
---|
2886 | {
|
---|
2887 | delete pMediumLockList;
|
---|
2888 | throw rc;
|
---|
2889 | }
|
---|
2890 |
|
---|
2891 | alock.release();
|
---|
2892 | rc = pMediumLockList->Lock();
|
---|
2893 | alock.acquire();
|
---|
2894 | if (FAILED(rc))
|
---|
2895 | {
|
---|
2896 | delete pMediumLockList;
|
---|
2897 | throw setError(rc,
|
---|
2898 | tr("Failed to lock media when compacting '%s'"),
|
---|
2899 | i_getLocationFull().c_str());
|
---|
2900 | }
|
---|
2901 |
|
---|
2902 | pProgress.createObject();
|
---|
2903 | rc = pProgress->init(m->pVirtualBox,
|
---|
2904 | static_cast <IMedium *>(this),
|
---|
2905 | BstrFmt(tr("Compacting medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
2906 | TRUE /* aCancelable */);
|
---|
2907 | if (FAILED(rc))
|
---|
2908 | {
|
---|
2909 | delete pMediumLockList;
|
---|
2910 | throw rc;
|
---|
2911 | }
|
---|
2912 |
|
---|
2913 | /* setup task object to carry out the operation asynchronously */
|
---|
2914 | pTask = new Medium::CompactTask(this, pProgress, pMediumLockList);
|
---|
2915 | rc = pTask->rc();
|
---|
2916 | AssertComRC(rc);
|
---|
2917 | if (FAILED(rc))
|
---|
2918 | throw rc;
|
---|
2919 | }
|
---|
2920 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2921 |
|
---|
2922 | if (SUCCEEDED(rc))
|
---|
2923 | {
|
---|
2924 | rc = i_startThread(pTask);
|
---|
2925 |
|
---|
2926 | if (SUCCEEDED(rc))
|
---|
2927 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2928 | }
|
---|
2929 | else if (pTask != NULL)
|
---|
2930 | delete pTask;
|
---|
2931 |
|
---|
2932 | return rc;
|
---|
2933 | }
|
---|
2934 |
|
---|
2935 | HRESULT Medium::resize(LONG64 aLogicalSize,
|
---|
2936 | ComPtr<IProgress> &aProgress)
|
---|
2937 | {
|
---|
2938 | HRESULT rc = S_OK;
|
---|
2939 | ComObjPtr<Progress> pProgress;
|
---|
2940 | Medium::Task *pTask = NULL;
|
---|
2941 |
|
---|
2942 | try
|
---|
2943 | {
|
---|
2944 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2945 |
|
---|
2946 | /* Build the medium lock list. */
|
---|
2947 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
2948 | alock.release();
|
---|
2949 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
2950 | true /* fMediumLockWrite */,
|
---|
2951 | false /* fMediumLockWriteAll */,
|
---|
2952 | NULL,
|
---|
2953 | *pMediumLockList);
|
---|
2954 | alock.acquire();
|
---|
2955 | if (FAILED(rc))
|
---|
2956 | {
|
---|
2957 | delete pMediumLockList;
|
---|
2958 | throw rc;
|
---|
2959 | }
|
---|
2960 |
|
---|
2961 | alock.release();
|
---|
2962 | rc = pMediumLockList->Lock();
|
---|
2963 | alock.acquire();
|
---|
2964 | if (FAILED(rc))
|
---|
2965 | {
|
---|
2966 | delete pMediumLockList;
|
---|
2967 | throw setError(rc,
|
---|
2968 | tr("Failed to lock media when compacting '%s'"),
|
---|
2969 | i_getLocationFull().c_str());
|
---|
2970 | }
|
---|
2971 |
|
---|
2972 | pProgress.createObject();
|
---|
2973 | rc = pProgress->init(m->pVirtualBox,
|
---|
2974 | static_cast <IMedium *>(this),
|
---|
2975 | BstrFmt(tr("Compacting medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
2976 | TRUE /* aCancelable */);
|
---|
2977 | if (FAILED(rc))
|
---|
2978 | {
|
---|
2979 | delete pMediumLockList;
|
---|
2980 | throw rc;
|
---|
2981 | }
|
---|
2982 |
|
---|
2983 | /* setup task object to carry out the operation asynchronously */
|
---|
2984 | pTask = new Medium::ResizeTask(this, aLogicalSize, pProgress, pMediumLockList);
|
---|
2985 | rc = pTask->rc();
|
---|
2986 | AssertComRC(rc);
|
---|
2987 | if (FAILED(rc))
|
---|
2988 | throw rc;
|
---|
2989 | }
|
---|
2990 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2991 |
|
---|
2992 | if (SUCCEEDED(rc))
|
---|
2993 | {
|
---|
2994 | rc = i_startThread(pTask);
|
---|
2995 |
|
---|
2996 | if (SUCCEEDED(rc))
|
---|
2997 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2998 | }
|
---|
2999 | else if (pTask != NULL)
|
---|
3000 | delete pTask;
|
---|
3001 |
|
---|
3002 | return rc;
|
---|
3003 | }
|
---|
3004 |
|
---|
3005 | HRESULT Medium::reset(ComPtr<IProgress> &aProgress)
|
---|
3006 | {
|
---|
3007 | HRESULT rc = S_OK;
|
---|
3008 | ComObjPtr<Progress> pProgress;
|
---|
3009 | Medium::Task *pTask = NULL;
|
---|
3010 |
|
---|
3011 | try
|
---|
3012 | {
|
---|
3013 | /* canClose() needs the tree lock */
|
---|
3014 | AutoMultiWriteLock2 multilock(&m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
3015 | this->lockHandle()
|
---|
3016 | COMMA_LOCKVAL_SRC_POS);
|
---|
3017 |
|
---|
3018 | LogFlowThisFunc(("ENTER for medium %s\n", m->strLocationFull.c_str()));
|
---|
3019 |
|
---|
3020 | if (m->pParent.isNull())
|
---|
3021 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3022 | tr("Medium type of '%s' is not differencing"),
|
---|
3023 | m->strLocationFull.c_str());
|
---|
3024 |
|
---|
3025 | rc = i_canClose();
|
---|
3026 | if (FAILED(rc))
|
---|
3027 | throw rc;
|
---|
3028 |
|
---|
3029 | /* Build the medium lock list. */
|
---|
3030 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3031 | multilock.release();
|
---|
3032 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
3033 | true /* fMediumLockWrite */,
|
---|
3034 | false /* fMediumLockWriteAll */,
|
---|
3035 | NULL,
|
---|
3036 | *pMediumLockList);
|
---|
3037 | multilock.acquire();
|
---|
3038 | if (FAILED(rc))
|
---|
3039 | {
|
---|
3040 | delete pMediumLockList;
|
---|
3041 | throw rc;
|
---|
3042 | }
|
---|
3043 |
|
---|
3044 | multilock.release();
|
---|
3045 | rc = pMediumLockList->Lock();
|
---|
3046 | multilock.acquire();
|
---|
3047 | if (FAILED(rc))
|
---|
3048 | {
|
---|
3049 | delete pMediumLockList;
|
---|
3050 | throw setError(rc,
|
---|
3051 | tr("Failed to lock media when resetting '%s'"),
|
---|
3052 | i_getLocationFull().c_str());
|
---|
3053 | }
|
---|
3054 |
|
---|
3055 | pProgress.createObject();
|
---|
3056 | rc = pProgress->init(m->pVirtualBox,
|
---|
3057 | static_cast<IMedium*>(this),
|
---|
3058 | BstrFmt(tr("Resetting differencing medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3059 | FALSE /* aCancelable */);
|
---|
3060 | if (FAILED(rc))
|
---|
3061 | throw rc;
|
---|
3062 |
|
---|
3063 | /* setup task object to carry out the operation asynchronously */
|
---|
3064 | pTask = new Medium::ResetTask(this, pProgress, pMediumLockList);
|
---|
3065 | rc = pTask->rc();
|
---|
3066 | AssertComRC(rc);
|
---|
3067 | if (FAILED(rc))
|
---|
3068 | throw rc;
|
---|
3069 | }
|
---|
3070 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3071 |
|
---|
3072 | if (SUCCEEDED(rc))
|
---|
3073 | {
|
---|
3074 | rc = i_startThread(pTask);
|
---|
3075 |
|
---|
3076 | if (SUCCEEDED(rc))
|
---|
3077 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3078 | }
|
---|
3079 | else if (pTask != NULL)
|
---|
3080 | delete pTask;
|
---|
3081 |
|
---|
3082 | LogFlowThisFunc(("LEAVE, rc=%Rhrc\n", rc));
|
---|
3083 |
|
---|
3084 | return rc;
|
---|
3085 | }
|
---|
3086 |
|
---|
3087 | HRESULT Medium::changeEncryption(const com::Utf8Str &aCurrentPassword, const com::Utf8Str &aCipher,
|
---|
3088 | const com::Utf8Str &aNewPassword, const com::Utf8Str &aNewPasswordId,
|
---|
3089 | ComPtr<IProgress> &aProgress)
|
---|
3090 | {
|
---|
3091 | HRESULT rc = S_OK;
|
---|
3092 | ComObjPtr<Progress> pProgress;
|
---|
3093 | Medium::Task *pTask = NULL;
|
---|
3094 |
|
---|
3095 | try
|
---|
3096 | {
|
---|
3097 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3098 |
|
---|
3099 | DeviceType_T devType = i_getDeviceType();
|
---|
3100 | /* Cannot encrypt DVD or floppy images so far. */
|
---|
3101 | if ( devType == DeviceType_DVD
|
---|
3102 | || devType == DeviceType_Floppy)
|
---|
3103 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3104 | tr("Cannot encrypt DVD or Floppy medium '%s'"),
|
---|
3105 | m->strLocationFull.c_str());
|
---|
3106 |
|
---|
3107 | /* Cannot encrypt media which are attached to more than one virtual machine. */
|
---|
3108 | if (m->backRefs.size() > 1)
|
---|
3109 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3110 | tr("Cannot encrypt medium '%s' because it is attached to %d virtual machines"),
|
---|
3111 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
3112 |
|
---|
3113 | if (i_getChildren().size() != 0)
|
---|
3114 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3115 | tr("Cannot encrypt medium '%s' because it has %d children"),
|
---|
3116 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
3117 |
|
---|
3118 | /* Build the medium lock list. */
|
---|
3119 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3120 | alock.release();
|
---|
3121 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
3122 | true /* fMediumLockWrite */,
|
---|
3123 | true /* fMediumLockAllWrite */,
|
---|
3124 | NULL,
|
---|
3125 | *pMediumLockList);
|
---|
3126 | alock.acquire();
|
---|
3127 | if (FAILED(rc))
|
---|
3128 | {
|
---|
3129 | delete pMediumLockList;
|
---|
3130 | throw rc;
|
---|
3131 | }
|
---|
3132 |
|
---|
3133 | alock.release();
|
---|
3134 | rc = pMediumLockList->Lock();
|
---|
3135 | alock.acquire();
|
---|
3136 | if (FAILED(rc))
|
---|
3137 | {
|
---|
3138 | delete pMediumLockList;
|
---|
3139 | throw setError(rc,
|
---|
3140 | tr("Failed to lock media for encryption '%s'"),
|
---|
3141 | i_getLocationFull().c_str());
|
---|
3142 | }
|
---|
3143 |
|
---|
3144 | /*
|
---|
3145 | * Check all media in the chain to not contain any branches or references to
|
---|
3146 | * other virtual machines, we support encrypting only a list of differencing media at the moment.
|
---|
3147 | */
|
---|
3148 | MediumLockList::Base::const_iterator mediumListBegin = pMediumLockList->GetBegin();
|
---|
3149 | MediumLockList::Base::const_iterator mediumListEnd = pMediumLockList->GetEnd();
|
---|
3150 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
3151 | it != mediumListEnd;
|
---|
3152 | ++it)
|
---|
3153 | {
|
---|
3154 | const MediumLock &mediumLock = *it;
|
---|
3155 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
3156 | AutoReadLock mediumReadLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
3157 |
|
---|
3158 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
3159 |
|
---|
3160 | if (pMedium->m->backRefs.size() > 1)
|
---|
3161 | {
|
---|
3162 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3163 | tr("Cannot encrypt medium '%s' because it is attached to %d virtual machines"),
|
---|
3164 | pMedium->m->strLocationFull.c_str(), pMedium->m->backRefs.size());
|
---|
3165 | break;
|
---|
3166 | }
|
---|
3167 | else if (pMedium->i_getChildren().size() > 1)
|
---|
3168 | {
|
---|
3169 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3170 | tr("Cannot encrypt medium '%s' because it has %d children"),
|
---|
3171 | pMedium->m->strLocationFull.c_str(), pMedium->i_getChildren().size());
|
---|
3172 | break;
|
---|
3173 | }
|
---|
3174 | }
|
---|
3175 |
|
---|
3176 | if (FAILED(rc))
|
---|
3177 | {
|
---|
3178 | delete pMediumLockList;
|
---|
3179 | throw rc;
|
---|
3180 | }
|
---|
3181 |
|
---|
3182 | const char *pszAction = "Encrypting";
|
---|
3183 | if ( aCurrentPassword.isNotEmpty()
|
---|
3184 | && aCipher.isEmpty())
|
---|
3185 | pszAction = "Decrypting";
|
---|
3186 |
|
---|
3187 | pProgress.createObject();
|
---|
3188 | rc = pProgress->init(m->pVirtualBox,
|
---|
3189 | static_cast <IMedium *>(this),
|
---|
3190 | BstrFmt(tr("%s medium '%s'"), pszAction, m->strLocationFull.c_str()).raw(),
|
---|
3191 | TRUE /* aCancelable */);
|
---|
3192 | if (FAILED(rc))
|
---|
3193 | {
|
---|
3194 | delete pMediumLockList;
|
---|
3195 | throw rc;
|
---|
3196 | }
|
---|
3197 |
|
---|
3198 | /* setup task object to carry out the operation asynchronously */
|
---|
3199 | pTask = new Medium::EncryptTask(this, aNewPassword, aCurrentPassword,
|
---|
3200 | aCipher, aNewPasswordId, pProgress, pMediumLockList);
|
---|
3201 | rc = pTask->rc();
|
---|
3202 | AssertComRC(rc);
|
---|
3203 | if (FAILED(rc))
|
---|
3204 | throw rc;
|
---|
3205 | }
|
---|
3206 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3207 |
|
---|
3208 | if (SUCCEEDED(rc))
|
---|
3209 | {
|
---|
3210 | rc = i_startThread(pTask);
|
---|
3211 |
|
---|
3212 | if (SUCCEEDED(rc))
|
---|
3213 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3214 | }
|
---|
3215 | else if (pTask != NULL)
|
---|
3216 | delete pTask;
|
---|
3217 |
|
---|
3218 | return rc;
|
---|
3219 | }
|
---|
3220 |
|
---|
3221 | HRESULT Medium::getEncryptionSettings(com::Utf8Str &aCipher, com::Utf8Str &aPasswordId)
|
---|
3222 | {
|
---|
3223 | HRESULT rc = S_OK;
|
---|
3224 |
|
---|
3225 | try
|
---|
3226 | {
|
---|
3227 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
3228 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3229 |
|
---|
3230 | /* Check whether encryption is configured for this medium. */
|
---|
3231 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
3232 | if (it == pBase->m->mapProperties.end())
|
---|
3233 | throw VBOX_E_NOT_SUPPORTED;
|
---|
3234 |
|
---|
3235 | # ifdef VBOX_WITH_EXTPACK
|
---|
3236 | static const Utf8Str strExtPackPuel("Oracle VM VirtualBox Extension Pack");
|
---|
3237 | static const char *s_pszVDPlugin = "VDPluginCrypt";
|
---|
3238 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
3239 | if (pExtPackManager->i_isExtPackUsable(strExtPackPuel.c_str()))
|
---|
3240 | {
|
---|
3241 | /* Load the plugin */
|
---|
3242 | Utf8Str strPlugin;
|
---|
3243 | rc = pExtPackManager->i_getLibraryPathForExtPack(s_pszVDPlugin, &strExtPackPuel, &strPlugin);
|
---|
3244 | if (SUCCEEDED(rc))
|
---|
3245 | {
|
---|
3246 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
3247 | if (RT_FAILURE(vrc))
|
---|
3248 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3249 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
3250 | i_vdError(vrc).c_str());
|
---|
3251 | }
|
---|
3252 | else
|
---|
3253 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3254 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
3255 | strExtPackPuel.c_str());
|
---|
3256 | }
|
---|
3257 | else
|
---|
3258 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3259 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
3260 | strExtPackPuel.c_str());
|
---|
3261 |
|
---|
3262 | PVBOXHDD pDisk = NULL;
|
---|
3263 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
3264 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
3265 |
|
---|
3266 | Medium::CryptoFilterSettings CryptoSettings;
|
---|
3267 |
|
---|
3268 | i_taskEncryptSettingsSetup(&CryptoSettings, NULL, it->second.c_str(), NULL, false /* fCreateKeyStore */);
|
---|
3269 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ | VD_FILTER_FLAGS_INFO, CryptoSettings.vdFilterIfaces);
|
---|
3270 | if (RT_FAILURE(vrc))
|
---|
3271 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3272 | tr("Failed to load the encryption filter: %s"),
|
---|
3273 | i_vdError(vrc).c_str());
|
---|
3274 |
|
---|
3275 | it = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
3276 | if (it == pBase->m->mapProperties.end())
|
---|
3277 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3278 | tr("Image is configured for encryption but doesn't has a KeyId set"));
|
---|
3279 |
|
---|
3280 | aPasswordId = it->second.c_str();
|
---|
3281 | aCipher = CryptoSettings.pszCipherReturned;
|
---|
3282 | RTStrFree(CryptoSettings.pszCipherReturned);
|
---|
3283 |
|
---|
3284 | VDDestroy(pDisk);
|
---|
3285 | # else
|
---|
3286 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3287 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
3288 | # endif
|
---|
3289 | }
|
---|
3290 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3291 |
|
---|
3292 | return rc;
|
---|
3293 | }
|
---|
3294 |
|
---|
3295 | HRESULT Medium::checkEncryptionPassword(const com::Utf8Str &aPassword)
|
---|
3296 | {
|
---|
3297 | HRESULT rc = S_OK;
|
---|
3298 |
|
---|
3299 | try
|
---|
3300 | {
|
---|
3301 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
3302 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3303 |
|
---|
3304 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
3305 | if (it == pBase->m->mapProperties.end())
|
---|
3306 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3307 | tr("The image is not configured for encryption"));
|
---|
3308 |
|
---|
3309 | if (aPassword.isEmpty())
|
---|
3310 | throw setError(E_INVALIDARG,
|
---|
3311 | tr("The given password must not be empty"));
|
---|
3312 |
|
---|
3313 | # ifdef VBOX_WITH_EXTPACK
|
---|
3314 | static const Utf8Str strExtPackPuel("Oracle VM VirtualBox Extension Pack");
|
---|
3315 | static const char *s_pszVDPlugin = "VDPluginCrypt";
|
---|
3316 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
3317 | if (pExtPackManager->i_isExtPackUsable(strExtPackPuel.c_str()))
|
---|
3318 | {
|
---|
3319 | /* Load the plugin */
|
---|
3320 | Utf8Str strPlugin;
|
---|
3321 | rc = pExtPackManager->i_getLibraryPathForExtPack(s_pszVDPlugin, &strExtPackPuel, &strPlugin);
|
---|
3322 | if (SUCCEEDED(rc))
|
---|
3323 | {
|
---|
3324 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
3325 | if (RT_FAILURE(vrc))
|
---|
3326 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3327 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
3328 | i_vdError(vrc).c_str());
|
---|
3329 | }
|
---|
3330 | else
|
---|
3331 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3332 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
3333 | strExtPackPuel.c_str());
|
---|
3334 | }
|
---|
3335 | else
|
---|
3336 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3337 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
3338 | strExtPackPuel.c_str());
|
---|
3339 |
|
---|
3340 | PVBOXHDD pDisk = NULL;
|
---|
3341 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
3342 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
3343 |
|
---|
3344 | Medium::CryptoFilterSettings CryptoSettings;
|
---|
3345 |
|
---|
3346 | i_taskEncryptSettingsSetup(&CryptoSettings, NULL, it->second.c_str(), aPassword.c_str(),
|
---|
3347 | false /* fCreateKeyStore */);
|
---|
3348 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ, CryptoSettings.vdFilterIfaces);
|
---|
3349 | if (vrc == VERR_VD_PASSWORD_INCORRECT)
|
---|
3350 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
3351 | tr("The given password is incorrect"));
|
---|
3352 | else if (RT_FAILURE(vrc))
|
---|
3353 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3354 | tr("Failed to load the encryption filter: %s"),
|
---|
3355 | i_vdError(vrc).c_str());
|
---|
3356 |
|
---|
3357 | VDDestroy(pDisk);
|
---|
3358 | # else
|
---|
3359 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3360 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
3361 | # endif
|
---|
3362 | }
|
---|
3363 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3364 |
|
---|
3365 | return rc;
|
---|
3366 | }
|
---|
3367 |
|
---|
3368 | ////////////////////////////////////////////////////////////////////////////////
|
---|
3369 | //
|
---|
3370 | // Medium public internal methods
|
---|
3371 | //
|
---|
3372 | ////////////////////////////////////////////////////////////////////////////////
|
---|
3373 |
|
---|
3374 | /**
|
---|
3375 | * Internal method to return the medium's parent medium. Must have caller + locking!
|
---|
3376 | * @return
|
---|
3377 | */
|
---|
3378 | const ComObjPtr<Medium>& Medium::i_getParent() const
|
---|
3379 | {
|
---|
3380 | return m->pParent;
|
---|
3381 | }
|
---|
3382 |
|
---|
3383 | /**
|
---|
3384 | * Internal method to return the medium's list of child media. Must have caller + locking!
|
---|
3385 | * @return
|
---|
3386 | */
|
---|
3387 | const MediaList& Medium::i_getChildren() const
|
---|
3388 | {
|
---|
3389 | return m->llChildren;
|
---|
3390 | }
|
---|
3391 |
|
---|
3392 | /**
|
---|
3393 | * Internal method to return the medium's GUID. Must have caller + locking!
|
---|
3394 | * @return
|
---|
3395 | */
|
---|
3396 | const Guid& Medium::i_getId() const
|
---|
3397 | {
|
---|
3398 | return m->id;
|
---|
3399 | }
|
---|
3400 |
|
---|
3401 | /**
|
---|
3402 | * Internal method to return the medium's state. Must have caller + locking!
|
---|
3403 | * @return
|
---|
3404 | */
|
---|
3405 | MediumState_T Medium::i_getState() const
|
---|
3406 | {
|
---|
3407 | return m->state;
|
---|
3408 | }
|
---|
3409 |
|
---|
3410 | /**
|
---|
3411 | * Internal method to return the medium's variant. Must have caller + locking!
|
---|
3412 | * @return
|
---|
3413 | */
|
---|
3414 | MediumVariant_T Medium::i_getVariant() const
|
---|
3415 | {
|
---|
3416 | return m->variant;
|
---|
3417 | }
|
---|
3418 |
|
---|
3419 | /**
|
---|
3420 | * Internal method which returns true if this medium represents a host drive.
|
---|
3421 | * @return
|
---|
3422 | */
|
---|
3423 | bool Medium::i_isHostDrive() const
|
---|
3424 | {
|
---|
3425 | return m->hostDrive;
|
---|
3426 | }
|
---|
3427 |
|
---|
3428 | /**
|
---|
3429 | * Internal method to return the medium's full location. Must have caller + locking!
|
---|
3430 | * @return
|
---|
3431 | */
|
---|
3432 | const Utf8Str& Medium::i_getLocationFull() const
|
---|
3433 | {
|
---|
3434 | return m->strLocationFull;
|
---|
3435 | }
|
---|
3436 |
|
---|
3437 | /**
|
---|
3438 | * Internal method to return the medium's format string. Must have caller + locking!
|
---|
3439 | * @return
|
---|
3440 | */
|
---|
3441 | const Utf8Str& Medium::i_getFormat() const
|
---|
3442 | {
|
---|
3443 | return m->strFormat;
|
---|
3444 | }
|
---|
3445 |
|
---|
3446 | /**
|
---|
3447 | * Internal method to return the medium's format object. Must have caller + locking!
|
---|
3448 | * @return
|
---|
3449 | */
|
---|
3450 | const ComObjPtr<MediumFormat>& Medium::i_getMediumFormat() const
|
---|
3451 | {
|
---|
3452 | return m->formatObj;
|
---|
3453 | }
|
---|
3454 |
|
---|
3455 | /**
|
---|
3456 | * Internal method that returns true if the medium is represented by a file on the host disk
|
---|
3457 | * (and not iSCSI or something).
|
---|
3458 | * @return
|
---|
3459 | */
|
---|
3460 | bool Medium::i_isMediumFormatFile() const
|
---|
3461 | {
|
---|
3462 | if ( m->formatObj
|
---|
3463 | && (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
3464 | )
|
---|
3465 | return true;
|
---|
3466 | return false;
|
---|
3467 | }
|
---|
3468 |
|
---|
3469 | /**
|
---|
3470 | * Internal method to return the medium's size. Must have caller + locking!
|
---|
3471 | * @return
|
---|
3472 | */
|
---|
3473 | uint64_t Medium::i_getSize() const
|
---|
3474 | {
|
---|
3475 | return m->size;
|
---|
3476 | }
|
---|
3477 |
|
---|
3478 | /**
|
---|
3479 | * Returns the medium device type. Must have caller + locking!
|
---|
3480 | * @return
|
---|
3481 | */
|
---|
3482 | DeviceType_T Medium::i_getDeviceType() const
|
---|
3483 | {
|
---|
3484 | return m->devType;
|
---|
3485 | }
|
---|
3486 |
|
---|
3487 | /**
|
---|
3488 | * Returns the medium type. Must have caller + locking!
|
---|
3489 | * @return
|
---|
3490 | */
|
---|
3491 | MediumType_T Medium::i_getType() const
|
---|
3492 | {
|
---|
3493 | return m->type;
|
---|
3494 | }
|
---|
3495 |
|
---|
3496 | /**
|
---|
3497 | * Returns a short version of the location attribute.
|
---|
3498 | *
|
---|
3499 | * @note Must be called from under this object's read or write lock.
|
---|
3500 | */
|
---|
3501 | Utf8Str Medium::i_getName()
|
---|
3502 | {
|
---|
3503 | Utf8Str name = RTPathFilename(m->strLocationFull.c_str());
|
---|
3504 | return name;
|
---|
3505 | }
|
---|
3506 |
|
---|
3507 | /**
|
---|
3508 | * This adds the given UUID to the list of media registries in which this
|
---|
3509 | * medium should be registered. The UUID can either be a machine UUID,
|
---|
3510 | * to add a machine registry, or the global registry UUID as returned by
|
---|
3511 | * VirtualBox::getGlobalRegistryId().
|
---|
3512 | *
|
---|
3513 | * Note that for hard disks, this method does nothing if the medium is
|
---|
3514 | * already in another registry to avoid having hard disks in more than
|
---|
3515 | * one registry, which causes trouble with keeping diff images in sync.
|
---|
3516 | * See getFirstRegistryMachineId() for details.
|
---|
3517 | *
|
---|
3518 | * @param id
|
---|
3519 | * @return true if the registry was added; false if the given id was already on the list.
|
---|
3520 | */
|
---|
3521 | bool Medium::i_addRegistry(const Guid& id)
|
---|
3522 | {
|
---|
3523 | AutoCaller autoCaller(this);
|
---|
3524 | if (FAILED(autoCaller.rc()))
|
---|
3525 | return false;
|
---|
3526 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3527 |
|
---|
3528 | bool fAdd = true;
|
---|
3529 |
|
---|
3530 | // hard disks cannot be in more than one registry
|
---|
3531 | if ( m->devType == DeviceType_HardDisk
|
---|
3532 | && m->llRegistryIDs.size() > 0)
|
---|
3533 | fAdd = false;
|
---|
3534 |
|
---|
3535 | // no need to add the UUID twice
|
---|
3536 | if (fAdd)
|
---|
3537 | {
|
---|
3538 | for (GuidList::const_iterator it = m->llRegistryIDs.begin();
|
---|
3539 | it != m->llRegistryIDs.end();
|
---|
3540 | ++it)
|
---|
3541 | {
|
---|
3542 | if ((*it) == id)
|
---|
3543 | {
|
---|
3544 | fAdd = false;
|
---|
3545 | break;
|
---|
3546 | }
|
---|
3547 | }
|
---|
3548 | }
|
---|
3549 |
|
---|
3550 | if (fAdd)
|
---|
3551 | m->llRegistryIDs.push_back(id);
|
---|
3552 |
|
---|
3553 | return fAdd;
|
---|
3554 | }
|
---|
3555 |
|
---|
3556 | /**
|
---|
3557 | * This adds the given UUID to the list of media registries in which this
|
---|
3558 | * medium should be registered. The UUID can either be a machine UUID,
|
---|
3559 | * to add a machine registry, or the global registry UUID as returned by
|
---|
3560 | * VirtualBox::getGlobalRegistryId(). This recurses over all children.
|
---|
3561 | *
|
---|
3562 | * Note that for hard disks, this method does nothing if the medium is
|
---|
3563 | * already in another registry to avoid having hard disks in more than
|
---|
3564 | * one registry, which causes trouble with keeping diff images in sync.
|
---|
3565 | * See getFirstRegistryMachineId() for details.
|
---|
3566 | *
|
---|
3567 | * @note the caller must hold the media tree lock for reading.
|
---|
3568 | *
|
---|
3569 | * @param id
|
---|
3570 | * @return true if the registry was added; false if the given id was already on the list.
|
---|
3571 | */
|
---|
3572 | bool Medium::i_addRegistryRecursive(const Guid &id)
|
---|
3573 | {
|
---|
3574 | AutoCaller autoCaller(this);
|
---|
3575 | if (FAILED(autoCaller.rc()))
|
---|
3576 | return false;
|
---|
3577 |
|
---|
3578 | bool fAdd = i_addRegistry(id);
|
---|
3579 |
|
---|
3580 | // protected by the medium tree lock held by our original caller
|
---|
3581 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
3582 | it != i_getChildren().end();
|
---|
3583 | ++it)
|
---|
3584 | {
|
---|
3585 | Medium *pChild = *it;
|
---|
3586 | fAdd |= pChild->i_addRegistryRecursive(id);
|
---|
3587 | }
|
---|
3588 |
|
---|
3589 | return fAdd;
|
---|
3590 | }
|
---|
3591 |
|
---|
3592 | /**
|
---|
3593 | * Removes the given UUID from the list of media registry UUIDs of this medium.
|
---|
3594 | *
|
---|
3595 | * @param id
|
---|
3596 | * @return true if the UUID was found or false if not.
|
---|
3597 | */
|
---|
3598 | bool Medium::i_removeRegistry(const Guid &id)
|
---|
3599 | {
|
---|
3600 | AutoCaller autoCaller(this);
|
---|
3601 | if (FAILED(autoCaller.rc()))
|
---|
3602 | return false;
|
---|
3603 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3604 |
|
---|
3605 | bool fRemove = false;
|
---|
3606 |
|
---|
3607 | // @todo r=klaus eliminate this code, replace it by using find.
|
---|
3608 | for (GuidList::iterator it = m->llRegistryIDs.begin();
|
---|
3609 | it != m->llRegistryIDs.end();
|
---|
3610 | ++it)
|
---|
3611 | {
|
---|
3612 | if ((*it) == id)
|
---|
3613 | {
|
---|
3614 | // getting away with this as the iterator isn't used after
|
---|
3615 | m->llRegistryIDs.erase(it);
|
---|
3616 | fRemove = true;
|
---|
3617 | break;
|
---|
3618 | }
|
---|
3619 | }
|
---|
3620 |
|
---|
3621 | return fRemove;
|
---|
3622 | }
|
---|
3623 |
|
---|
3624 | /**
|
---|
3625 | * Removes the given UUID from the list of media registry UUIDs, for this
|
---|
3626 | * medium and all its children recursively.
|
---|
3627 | *
|
---|
3628 | * @note the caller must hold the media tree lock for reading.
|
---|
3629 | *
|
---|
3630 | * @param id
|
---|
3631 | * @return true if the UUID was found or false if not.
|
---|
3632 | */
|
---|
3633 | bool Medium::i_removeRegistryRecursive(const Guid &id)
|
---|
3634 | {
|
---|
3635 | AutoCaller autoCaller(this);
|
---|
3636 | if (FAILED(autoCaller.rc()))
|
---|
3637 | return false;
|
---|
3638 |
|
---|
3639 | bool fRemove = i_removeRegistry(id);
|
---|
3640 |
|
---|
3641 | // protected by the medium tree lock held by our original caller
|
---|
3642 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
3643 | it != i_getChildren().end();
|
---|
3644 | ++it)
|
---|
3645 | {
|
---|
3646 | Medium *pChild = *it;
|
---|
3647 | fRemove |= pChild->i_removeRegistryRecursive(id);
|
---|
3648 | }
|
---|
3649 |
|
---|
3650 | return fRemove;
|
---|
3651 | }
|
---|
3652 |
|
---|
3653 | /**
|
---|
3654 | * Returns true if id is in the list of media registries for this medium.
|
---|
3655 | *
|
---|
3656 | * Must have caller + read locking!
|
---|
3657 | *
|
---|
3658 | * @param id
|
---|
3659 | * @return
|
---|
3660 | */
|
---|
3661 | bool Medium::i_isInRegistry(const Guid &id)
|
---|
3662 | {
|
---|
3663 | // @todo r=klaus eliminate this code, replace it by using find.
|
---|
3664 | for (GuidList::const_iterator it = m->llRegistryIDs.begin();
|
---|
3665 | it != m->llRegistryIDs.end();
|
---|
3666 | ++it)
|
---|
3667 | {
|
---|
3668 | if (*it == id)
|
---|
3669 | return true;
|
---|
3670 | }
|
---|
3671 |
|
---|
3672 | return false;
|
---|
3673 | }
|
---|
3674 |
|
---|
3675 | /**
|
---|
3676 | * Internal method to return the medium's first registry machine (i.e. the machine in whose
|
---|
3677 | * machine XML this medium is listed).
|
---|
3678 | *
|
---|
3679 | * Every attached medium must now (4.0) reside in at least one media registry, which is identified
|
---|
3680 | * by a UUID. This is either a machine UUID if the machine is from 4.0 or newer, in which case
|
---|
3681 | * machines have their own media registries, or it is the pseudo-UUID of the VirtualBox
|
---|
3682 | * object if the machine is old and still needs the global registry in VirtualBox.xml.
|
---|
3683 | *
|
---|
3684 | * By definition, hard disks may only be in one media registry, in which all its children
|
---|
3685 | * will be stored as well. Otherwise we run into problems with having keep multiple registries
|
---|
3686 | * in sync. (This is the "cloned VM" case in which VM1 may link to the disks of VM2; in this
|
---|
3687 | * case, only VM2's registry is used for the disk in question.)
|
---|
3688 | *
|
---|
3689 | * If there is no medium registry, particularly if the medium has not been attached yet, this
|
---|
3690 | * does not modify uuid and returns false.
|
---|
3691 | *
|
---|
3692 | * ISOs and RAWs, by contrast, can be in more than one repository to make things easier for
|
---|
3693 | * the user.
|
---|
3694 | *
|
---|
3695 | * Must have caller + locking!
|
---|
3696 | *
|
---|
3697 | * @param uuid Receives first registry machine UUID, if available.
|
---|
3698 | * @return true if uuid was set.
|
---|
3699 | */
|
---|
3700 | bool Medium::i_getFirstRegistryMachineId(Guid &uuid) const
|
---|
3701 | {
|
---|
3702 | if (m->llRegistryIDs.size())
|
---|
3703 | {
|
---|
3704 | uuid = m->llRegistryIDs.front();
|
---|
3705 | return true;
|
---|
3706 | }
|
---|
3707 | return false;
|
---|
3708 | }
|
---|
3709 |
|
---|
3710 | /**
|
---|
3711 | * Marks all the registries in which this medium is registered as modified.
|
---|
3712 | */
|
---|
3713 | void Medium::i_markRegistriesModified()
|
---|
3714 | {
|
---|
3715 | AutoCaller autoCaller(this);
|
---|
3716 | if (FAILED(autoCaller.rc())) return;
|
---|
3717 |
|
---|
3718 | // Get local copy, as keeping the lock over VirtualBox::markRegistryModified
|
---|
3719 | // causes trouble with the lock order
|
---|
3720 | GuidList llRegistryIDs;
|
---|
3721 | {
|
---|
3722 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3723 | llRegistryIDs = m->llRegistryIDs;
|
---|
3724 | }
|
---|
3725 |
|
---|
3726 | autoCaller.release();
|
---|
3727 |
|
---|
3728 | /* Save the error information now, the implicit restore when this goes
|
---|
3729 | * out of scope will throw away spurious additional errors created below. */
|
---|
3730 | ErrorInfoKeeper eik;
|
---|
3731 | for (GuidList::const_iterator it = llRegistryIDs.begin();
|
---|
3732 | it != llRegistryIDs.end();
|
---|
3733 | ++it)
|
---|
3734 | {
|
---|
3735 | m->pVirtualBox->i_markRegistryModified(*it);
|
---|
3736 | }
|
---|
3737 | }
|
---|
3738 |
|
---|
3739 | /**
|
---|
3740 | * Adds the given machine and optionally the snapshot to the list of the objects
|
---|
3741 | * this medium is attached to.
|
---|
3742 | *
|
---|
3743 | * @param aMachineId Machine ID.
|
---|
3744 | * @param aSnapshotId Snapshot ID; when non-empty, adds a snapshot attachment.
|
---|
3745 | */
|
---|
3746 | HRESULT Medium::i_addBackReference(const Guid &aMachineId,
|
---|
3747 | const Guid &aSnapshotId /*= Guid::Empty*/)
|
---|
3748 | {
|
---|
3749 | AssertReturn(aMachineId.isValid(), E_FAIL);
|
---|
3750 |
|
---|
3751 | LogFlowThisFunc(("ENTER, aMachineId: {%RTuuid}, aSnapshotId: {%RTuuid}\n", aMachineId.raw(), aSnapshotId.raw()));
|
---|
3752 |
|
---|
3753 | AutoCaller autoCaller(this);
|
---|
3754 | AssertComRCReturnRC(autoCaller.rc());
|
---|
3755 |
|
---|
3756 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3757 |
|
---|
3758 | switch (m->state)
|
---|
3759 | {
|
---|
3760 | case MediumState_Created:
|
---|
3761 | case MediumState_Inaccessible:
|
---|
3762 | case MediumState_LockedRead:
|
---|
3763 | case MediumState_LockedWrite:
|
---|
3764 | break;
|
---|
3765 |
|
---|
3766 | default:
|
---|
3767 | return i_setStateError();
|
---|
3768 | }
|
---|
3769 |
|
---|
3770 | if (m->numCreateDiffTasks > 0)
|
---|
3771 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
3772 | tr("Cannot attach medium '%s' {%RTuuid}: %u differencing child media are being created"),
|
---|
3773 | m->strLocationFull.c_str(),
|
---|
3774 | m->id.raw(),
|
---|
3775 | m->numCreateDiffTasks);
|
---|
3776 |
|
---|
3777 | BackRefList::iterator it = std::find_if(m->backRefs.begin(),
|
---|
3778 | m->backRefs.end(),
|
---|
3779 | BackRef::EqualsTo(aMachineId));
|
---|
3780 | if (it == m->backRefs.end())
|
---|
3781 | {
|
---|
3782 | BackRef ref(aMachineId, aSnapshotId);
|
---|
3783 | m->backRefs.push_back(ref);
|
---|
3784 |
|
---|
3785 | return S_OK;
|
---|
3786 | }
|
---|
3787 |
|
---|
3788 | // if the caller has not supplied a snapshot ID, then we're attaching
|
---|
3789 | // to a machine a medium which represents the machine's current state,
|
---|
3790 | // so set the flag
|
---|
3791 |
|
---|
3792 | if (aSnapshotId.isZero())
|
---|
3793 | {
|
---|
3794 | /* sanity: no duplicate attachments */
|
---|
3795 | if (it->fInCurState)
|
---|
3796 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
3797 | tr("Cannot attach medium '%s' {%RTuuid}: medium is already associated with the current state of machine uuid {%RTuuid}!"),
|
---|
3798 | m->strLocationFull.c_str(),
|
---|
3799 | m->id.raw(),
|
---|
3800 | aMachineId.raw());
|
---|
3801 | it->fInCurState = true;
|
---|
3802 |
|
---|
3803 | return S_OK;
|
---|
3804 | }
|
---|
3805 |
|
---|
3806 | // otherwise: a snapshot medium is being attached
|
---|
3807 |
|
---|
3808 | /* sanity: no duplicate attachments */
|
---|
3809 | for (GuidList::const_iterator jt = it->llSnapshotIds.begin();
|
---|
3810 | jt != it->llSnapshotIds.end();
|
---|
3811 | ++jt)
|
---|
3812 | {
|
---|
3813 | const Guid &idOldSnapshot = *jt;
|
---|
3814 |
|
---|
3815 | if (idOldSnapshot == aSnapshotId)
|
---|
3816 | {
|
---|
3817 | #ifdef DEBUG
|
---|
3818 | i_dumpBackRefs();
|
---|
3819 | #endif
|
---|
3820 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
3821 | tr("Cannot attach medium '%s' {%RTuuid} from snapshot '%RTuuid': medium is already in use by this snapshot!"),
|
---|
3822 | m->strLocationFull.c_str(),
|
---|
3823 | m->id.raw(),
|
---|
3824 | aSnapshotId.raw());
|
---|
3825 | }
|
---|
3826 | }
|
---|
3827 |
|
---|
3828 | it->llSnapshotIds.push_back(aSnapshotId);
|
---|
3829 | // Do not touch fInCurState, as the image may be attached to the current
|
---|
3830 | // state *and* a snapshot, otherwise we lose the current state association!
|
---|
3831 |
|
---|
3832 | LogFlowThisFuncLeave();
|
---|
3833 |
|
---|
3834 | return S_OK;
|
---|
3835 | }
|
---|
3836 |
|
---|
3837 | /**
|
---|
3838 | * Removes the given machine and optionally the snapshot from the list of the
|
---|
3839 | * objects this medium is attached to.
|
---|
3840 | *
|
---|
3841 | * @param aMachineId Machine ID.
|
---|
3842 | * @param aSnapshotId Snapshot ID; when non-empty, removes the snapshot
|
---|
3843 | * attachment.
|
---|
3844 | */
|
---|
3845 | HRESULT Medium::i_removeBackReference(const Guid &aMachineId,
|
---|
3846 | const Guid &aSnapshotId /*= Guid::Empty*/)
|
---|
3847 | {
|
---|
3848 | AssertReturn(aMachineId.isValid(), E_FAIL);
|
---|
3849 |
|
---|
3850 | AutoCaller autoCaller(this);
|
---|
3851 | AssertComRCReturnRC(autoCaller.rc());
|
---|
3852 |
|
---|
3853 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3854 |
|
---|
3855 | BackRefList::iterator it =
|
---|
3856 | std::find_if(m->backRefs.begin(), m->backRefs.end(),
|
---|
3857 | BackRef::EqualsTo(aMachineId));
|
---|
3858 | AssertReturn(it != m->backRefs.end(), E_FAIL);
|
---|
3859 |
|
---|
3860 | if (aSnapshotId.isZero())
|
---|
3861 | {
|
---|
3862 | /* remove the current state attachment */
|
---|
3863 | it->fInCurState = false;
|
---|
3864 | }
|
---|
3865 | else
|
---|
3866 | {
|
---|
3867 | /* remove the snapshot attachment */
|
---|
3868 | GuidList::iterator jt = std::find(it->llSnapshotIds.begin(),
|
---|
3869 | it->llSnapshotIds.end(),
|
---|
3870 | aSnapshotId);
|
---|
3871 |
|
---|
3872 | AssertReturn(jt != it->llSnapshotIds.end(), E_FAIL);
|
---|
3873 | it->llSnapshotIds.erase(jt);
|
---|
3874 | }
|
---|
3875 |
|
---|
3876 | /* if the backref becomes empty, remove it */
|
---|
3877 | if (it->fInCurState == false && it->llSnapshotIds.size() == 0)
|
---|
3878 | m->backRefs.erase(it);
|
---|
3879 |
|
---|
3880 | return S_OK;
|
---|
3881 | }
|
---|
3882 |
|
---|
3883 | /**
|
---|
3884 | * Internal method to return the medium's list of backrefs. Must have caller + locking!
|
---|
3885 | * @return
|
---|
3886 | */
|
---|
3887 | const Guid* Medium::i_getFirstMachineBackrefId() const
|
---|
3888 | {
|
---|
3889 | if (!m->backRefs.size())
|
---|
3890 | return NULL;
|
---|
3891 |
|
---|
3892 | return &m->backRefs.front().machineId;
|
---|
3893 | }
|
---|
3894 |
|
---|
3895 | /**
|
---|
3896 | * Internal method which returns a machine that either this medium or one of its children
|
---|
3897 | * is attached to. This is used for finding a replacement media registry when an existing
|
---|
3898 | * media registry is about to be deleted in VirtualBox::unregisterMachine().
|
---|
3899 | *
|
---|
3900 | * Must have caller + locking, *and* caller must hold the media tree lock!
|
---|
3901 | * @return
|
---|
3902 | */
|
---|
3903 | const Guid* Medium::i_getAnyMachineBackref() const
|
---|
3904 | {
|
---|
3905 | if (m->backRefs.size())
|
---|
3906 | return &m->backRefs.front().machineId;
|
---|
3907 |
|
---|
3908 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
3909 | it != i_getChildren().end();
|
---|
3910 | ++it)
|
---|
3911 | {
|
---|
3912 | Medium *pChild = *it;
|
---|
3913 | // recurse for this child
|
---|
3914 | const Guid* puuid;
|
---|
3915 | if ((puuid = pChild->i_getAnyMachineBackref()))
|
---|
3916 | return puuid;
|
---|
3917 | }
|
---|
3918 |
|
---|
3919 | return NULL;
|
---|
3920 | }
|
---|
3921 |
|
---|
3922 | const Guid* Medium::i_getFirstMachineBackrefSnapshotId() const
|
---|
3923 | {
|
---|
3924 | if (!m->backRefs.size())
|
---|
3925 | return NULL;
|
---|
3926 |
|
---|
3927 | const BackRef &ref = m->backRefs.front();
|
---|
3928 | if (ref.llSnapshotIds.empty())
|
---|
3929 | return NULL;
|
---|
3930 |
|
---|
3931 | return &ref.llSnapshotIds.front();
|
---|
3932 | }
|
---|
3933 |
|
---|
3934 | size_t Medium::i_getMachineBackRefCount() const
|
---|
3935 | {
|
---|
3936 | return m->backRefs.size();
|
---|
3937 | }
|
---|
3938 |
|
---|
3939 | #ifdef DEBUG
|
---|
3940 | /**
|
---|
3941 | * Debugging helper that gets called after VirtualBox initialization that writes all
|
---|
3942 | * machine backreferences to the debug log.
|
---|
3943 | */
|
---|
3944 | void Medium::i_dumpBackRefs()
|
---|
3945 | {
|
---|
3946 | AutoCaller autoCaller(this);
|
---|
3947 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3948 |
|
---|
3949 | LogFlowThisFunc(("Dumping backrefs for medium '%s':\n", m->strLocationFull.c_str()));
|
---|
3950 |
|
---|
3951 | for (BackRefList::iterator it2 = m->backRefs.begin();
|
---|
3952 | it2 != m->backRefs.end();
|
---|
3953 | ++it2)
|
---|
3954 | {
|
---|
3955 | const BackRef &ref = *it2;
|
---|
3956 | LogFlowThisFunc((" Backref from machine {%RTuuid} (fInCurState: %d)\n", ref.machineId.raw(), ref.fInCurState));
|
---|
3957 |
|
---|
3958 | for (GuidList::const_iterator jt2 = it2->llSnapshotIds.begin();
|
---|
3959 | jt2 != it2->llSnapshotIds.end();
|
---|
3960 | ++jt2)
|
---|
3961 | {
|
---|
3962 | const Guid &id = *jt2;
|
---|
3963 | LogFlowThisFunc((" Backref from snapshot {%RTuuid}\n", id.raw()));
|
---|
3964 | }
|
---|
3965 | }
|
---|
3966 | }
|
---|
3967 | #endif
|
---|
3968 |
|
---|
3969 | /**
|
---|
3970 | * Checks if the given change of \a aOldPath to \a aNewPath affects the location
|
---|
3971 | * of this media and updates it if necessary to reflect the new location.
|
---|
3972 | *
|
---|
3973 | * @param aOldPath Old path (full).
|
---|
3974 | * @param aNewPath New path (full).
|
---|
3975 | *
|
---|
3976 | * @note Locks this object for writing.
|
---|
3977 | */
|
---|
3978 | HRESULT Medium::i_updatePath(const Utf8Str &strOldPath, const Utf8Str &strNewPath)
|
---|
3979 | {
|
---|
3980 | AssertReturn(!strOldPath.isEmpty(), E_FAIL);
|
---|
3981 | AssertReturn(!strNewPath.isEmpty(), E_FAIL);
|
---|
3982 |
|
---|
3983 | AutoCaller autoCaller(this);
|
---|
3984 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
3985 |
|
---|
3986 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3987 |
|
---|
3988 | LogFlowThisFunc(("locationFull.before='%s'\n", m->strLocationFull.c_str()));
|
---|
3989 |
|
---|
3990 | const char *pcszMediumPath = m->strLocationFull.c_str();
|
---|
3991 |
|
---|
3992 | if (RTPathStartsWith(pcszMediumPath, strOldPath.c_str()))
|
---|
3993 | {
|
---|
3994 | Utf8Str newPath(strNewPath);
|
---|
3995 | newPath.append(pcszMediumPath + strOldPath.length());
|
---|
3996 | unconst(m->strLocationFull) = newPath;
|
---|
3997 |
|
---|
3998 | LogFlowThisFunc(("locationFull.after='%s'\n", m->strLocationFull.c_str()));
|
---|
3999 | // we changed something
|
---|
4000 | return S_OK;
|
---|
4001 | }
|
---|
4002 |
|
---|
4003 | // no change was necessary, signal error which the caller needs to interpret
|
---|
4004 | return VBOX_E_FILE_ERROR;
|
---|
4005 | }
|
---|
4006 |
|
---|
4007 | /**
|
---|
4008 | * Returns the base medium of the media chain this medium is part of.
|
---|
4009 | *
|
---|
4010 | * The base medium is found by walking up the parent-child relationship axis.
|
---|
4011 | * If the medium doesn't have a parent (i.e. it's a base medium), it
|
---|
4012 | * returns itself in response to this method.
|
---|
4013 | *
|
---|
4014 | * @param aLevel Where to store the number of ancestors of this medium
|
---|
4015 | * (zero for the base), may be @c NULL.
|
---|
4016 | *
|
---|
4017 | * @note Locks medium tree for reading.
|
---|
4018 | */
|
---|
4019 | ComObjPtr<Medium> Medium::i_getBase(uint32_t *aLevel /*= NULL*/)
|
---|
4020 | {
|
---|
4021 | ComObjPtr<Medium> pBase;
|
---|
4022 |
|
---|
4023 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
4024 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
4025 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
4026 | if (!pVirtualBox)
|
---|
4027 | return pBase;
|
---|
4028 |
|
---|
4029 | /* we access mParent */
|
---|
4030 | AutoReadLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4031 |
|
---|
4032 | AutoCaller autoCaller(this);
|
---|
4033 | AssertReturn(autoCaller.isOk(), pBase);
|
---|
4034 |
|
---|
4035 | pBase = this;
|
---|
4036 | uint32_t level = 0;
|
---|
4037 |
|
---|
4038 | if (m->pParent)
|
---|
4039 | {
|
---|
4040 | for (;;)
|
---|
4041 | {
|
---|
4042 | AutoCaller baseCaller(pBase);
|
---|
4043 | AssertReturn(baseCaller.isOk(), pBase);
|
---|
4044 |
|
---|
4045 | if (pBase->m->pParent.isNull())
|
---|
4046 | break;
|
---|
4047 |
|
---|
4048 | pBase = pBase->m->pParent;
|
---|
4049 | ++level;
|
---|
4050 | }
|
---|
4051 | }
|
---|
4052 |
|
---|
4053 | if (aLevel != NULL)
|
---|
4054 | *aLevel = level;
|
---|
4055 |
|
---|
4056 | return pBase;
|
---|
4057 | }
|
---|
4058 |
|
---|
4059 | /**
|
---|
4060 | * Returns the depth of this medium in the media chain.
|
---|
4061 | *
|
---|
4062 | * @note Locks medium tree for reading.
|
---|
4063 | */
|
---|
4064 | uint32_t Medium::i_getDepth()
|
---|
4065 | {
|
---|
4066 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
4067 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
4068 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
4069 | if (!pVirtualBox)
|
---|
4070 | return 1;
|
---|
4071 |
|
---|
4072 | /* we access mParent */
|
---|
4073 | AutoReadLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4074 |
|
---|
4075 | uint32_t cDepth = 0;
|
---|
4076 | ComObjPtr<Medium> pMedium(this);
|
---|
4077 | while (!pMedium.isNull())
|
---|
4078 | {
|
---|
4079 | AutoCaller autoCaller(this);
|
---|
4080 | AssertReturn(autoCaller.isOk(), cDepth + 1);
|
---|
4081 |
|
---|
4082 | pMedium = pMedium->m->pParent;
|
---|
4083 | cDepth++;
|
---|
4084 | }
|
---|
4085 |
|
---|
4086 | return cDepth;
|
---|
4087 | }
|
---|
4088 |
|
---|
4089 | /**
|
---|
4090 | * Returns @c true if this medium cannot be modified because it has
|
---|
4091 | * dependents (children) or is part of the snapshot. Related to the medium
|
---|
4092 | * type and posterity, not to the current media state.
|
---|
4093 | *
|
---|
4094 | * @note Locks this object and medium tree for reading.
|
---|
4095 | */
|
---|
4096 | bool Medium::i_isReadOnly()
|
---|
4097 | {
|
---|
4098 | AutoCaller autoCaller(this);
|
---|
4099 | AssertComRCReturn(autoCaller.rc(), false);
|
---|
4100 |
|
---|
4101 | /* we access children */
|
---|
4102 | AutoReadLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4103 |
|
---|
4104 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4105 |
|
---|
4106 | switch (m->type)
|
---|
4107 | {
|
---|
4108 | case MediumType_Normal:
|
---|
4109 | {
|
---|
4110 | if (i_getChildren().size() != 0)
|
---|
4111 | return true;
|
---|
4112 |
|
---|
4113 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
4114 | it != m->backRefs.end(); ++it)
|
---|
4115 | if (it->llSnapshotIds.size() != 0)
|
---|
4116 | return true;
|
---|
4117 |
|
---|
4118 | if (m->variant & MediumVariant_VmdkStreamOptimized)
|
---|
4119 | return true;
|
---|
4120 |
|
---|
4121 | return false;
|
---|
4122 | }
|
---|
4123 | case MediumType_Immutable:
|
---|
4124 | case MediumType_MultiAttach:
|
---|
4125 | return true;
|
---|
4126 | case MediumType_Writethrough:
|
---|
4127 | case MediumType_Shareable:
|
---|
4128 | case MediumType_Readonly: /* explicit readonly media has no diffs */
|
---|
4129 | return false;
|
---|
4130 | default:
|
---|
4131 | break;
|
---|
4132 | }
|
---|
4133 |
|
---|
4134 | AssertFailedReturn(false);
|
---|
4135 | }
|
---|
4136 |
|
---|
4137 | /**
|
---|
4138 | * Internal method to return the medium's size. Must have caller + locking!
|
---|
4139 | * @return
|
---|
4140 | */
|
---|
4141 | void Medium::i_updateId(const Guid &id)
|
---|
4142 | {
|
---|
4143 | unconst(m->id) = id;
|
---|
4144 | }
|
---|
4145 |
|
---|
4146 | /**
|
---|
4147 | * Saves the settings of one medium.
|
---|
4148 | *
|
---|
4149 | * @note Caller MUST take care of the medium tree lock and caller.
|
---|
4150 | *
|
---|
4151 | * @param data Settings struct to be updated.
|
---|
4152 | * @param strHardDiskFolder Folder for which paths should be relative.
|
---|
4153 | */
|
---|
4154 | void Medium::i_saveSettingsOne(settings::Medium &data, const Utf8Str &strHardDiskFolder)
|
---|
4155 | {
|
---|
4156 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4157 |
|
---|
4158 | data.uuid = m->id;
|
---|
4159 |
|
---|
4160 | // make path relative if needed
|
---|
4161 | if ( !strHardDiskFolder.isEmpty()
|
---|
4162 | && RTPathStartsWith(m->strLocationFull.c_str(), strHardDiskFolder.c_str())
|
---|
4163 | )
|
---|
4164 | data.strLocation = m->strLocationFull.substr(strHardDiskFolder.length() + 1);
|
---|
4165 | else
|
---|
4166 | data.strLocation = m->strLocationFull;
|
---|
4167 | data.strFormat = m->strFormat;
|
---|
4168 |
|
---|
4169 | /* optional, only for diffs, default is false */
|
---|
4170 | if (m->pParent)
|
---|
4171 | data.fAutoReset = m->autoReset;
|
---|
4172 | else
|
---|
4173 | data.fAutoReset = false;
|
---|
4174 |
|
---|
4175 | /* optional */
|
---|
4176 | data.strDescription = m->strDescription;
|
---|
4177 |
|
---|
4178 | /* optional properties */
|
---|
4179 | data.properties.clear();
|
---|
4180 |
|
---|
4181 | /* handle iSCSI initiator secrets transparently */
|
---|
4182 | bool fHaveInitiatorSecretEncrypted = false;
|
---|
4183 | Utf8Str strCiphertext;
|
---|
4184 | settings::StringsMap::const_iterator itPln = m->mapProperties.find("InitiatorSecret");
|
---|
4185 | if ( itPln != m->mapProperties.end()
|
---|
4186 | && !itPln->second.isEmpty())
|
---|
4187 | {
|
---|
4188 | /* Encrypt the plain secret. If that does not work (i.e. no or wrong settings key
|
---|
4189 | * specified), just use the encrypted secret (if there is any). */
|
---|
4190 | int rc = m->pVirtualBox->i_encryptSetting(itPln->second, &strCiphertext);
|
---|
4191 | if (RT_SUCCESS(rc))
|
---|
4192 | fHaveInitiatorSecretEncrypted = true;
|
---|
4193 | }
|
---|
4194 | for (settings::StringsMap::const_iterator it = m->mapProperties.begin();
|
---|
4195 | it != m->mapProperties.end();
|
---|
4196 | ++it)
|
---|
4197 | {
|
---|
4198 | /* only save properties that have non-default values */
|
---|
4199 | if (!it->second.isEmpty())
|
---|
4200 | {
|
---|
4201 | const Utf8Str &name = it->first;
|
---|
4202 | const Utf8Str &value = it->second;
|
---|
4203 | /* do NOT store the plain InitiatorSecret */
|
---|
4204 | if ( !fHaveInitiatorSecretEncrypted
|
---|
4205 | || !name.equals("InitiatorSecret"))
|
---|
4206 | data.properties[name] = value;
|
---|
4207 | }
|
---|
4208 | }
|
---|
4209 | if (fHaveInitiatorSecretEncrypted)
|
---|
4210 | data.properties["InitiatorSecretEncrypted"] = strCiphertext;
|
---|
4211 |
|
---|
4212 | /* only for base media */
|
---|
4213 | if (m->pParent.isNull())
|
---|
4214 | data.hdType = m->type;
|
---|
4215 | }
|
---|
4216 |
|
---|
4217 | /**
|
---|
4218 | * Saves medium data by putting it into the provided data structure.
|
---|
4219 | * Recurses over all children to save their settings, too.
|
---|
4220 | *
|
---|
4221 | * @param data Settings struct to be updated.
|
---|
4222 | * @param strHardDiskFolder Folder for which paths should be relative.
|
---|
4223 | *
|
---|
4224 | * @note Locks this object, medium tree and children for reading.
|
---|
4225 | */
|
---|
4226 | HRESULT Medium::i_saveSettings(settings::Medium &data,
|
---|
4227 | const Utf8Str &strHardDiskFolder)
|
---|
4228 | {
|
---|
4229 | AutoCaller autoCaller(this);
|
---|
4230 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4231 |
|
---|
4232 | /* we access mParent */
|
---|
4233 | AutoReadLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4234 |
|
---|
4235 | i_saveSettingsOne(data, strHardDiskFolder);
|
---|
4236 |
|
---|
4237 | /* save all children */
|
---|
4238 | settings::MediaList &llSettingsChildren = data.llChildren;
|
---|
4239 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
4240 | it != i_getChildren().end();
|
---|
4241 | ++it)
|
---|
4242 | {
|
---|
4243 | // Use the element straight in the list to reduce both unnecessary
|
---|
4244 | // deep copying (when unwinding the recursion the entire medium
|
---|
4245 | // settings sub-tree is copied) and the stack footprint (the settings
|
---|
4246 | // need almost 1K, and there can be VMs with long image chains.
|
---|
4247 | llSettingsChildren.push_back(settings::g_MediumEmpty);
|
---|
4248 | HRESULT rc = (*it)->i_saveSettings(llSettingsChildren.back(), strHardDiskFolder);
|
---|
4249 | if (FAILED(rc))
|
---|
4250 | {
|
---|
4251 | llSettingsChildren.pop_back();
|
---|
4252 | return rc;
|
---|
4253 | }
|
---|
4254 | }
|
---|
4255 |
|
---|
4256 | return S_OK;
|
---|
4257 | }
|
---|
4258 |
|
---|
4259 | /**
|
---|
4260 | * Constructs a medium lock list for this medium. The lock is not taken.
|
---|
4261 | *
|
---|
4262 | * @note Caller MUST NOT hold the media tree or medium lock.
|
---|
4263 | *
|
---|
4264 | * @param fFailIfInaccessible If true, this fails with an error if a medium is inaccessible. If false,
|
---|
4265 | * inaccessible media are silently skipped and not locked (i.e. their state remains "Inaccessible");
|
---|
4266 | * this is necessary for a VM's removable media VM startup for which we do not want to fail.
|
---|
4267 | * @param fMediumLockWrite Whether to associate a write lock with this medium.
|
---|
4268 | * @param fMediumLockWriteAll Whether to associate a write lock to all other media too.
|
---|
4269 | * @param pToBeParent Medium which will become the parent of this medium.
|
---|
4270 | * @param mediumLockList Where to store the resulting list.
|
---|
4271 | */
|
---|
4272 | HRESULT Medium::i_createMediumLockList(bool fFailIfInaccessible,
|
---|
4273 | bool fMediumLockWrite,
|
---|
4274 | bool fMediumLockWriteAll,
|
---|
4275 | Medium *pToBeParent,
|
---|
4276 | MediumLockList &mediumLockList)
|
---|
4277 | {
|
---|
4278 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
4279 | Assert(!isWriteLockOnCurrentThread());
|
---|
4280 |
|
---|
4281 | AutoCaller autoCaller(this);
|
---|
4282 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4283 |
|
---|
4284 | HRESULT rc = S_OK;
|
---|
4285 |
|
---|
4286 | /* paranoid sanity checking if the medium has a to-be parent medium */
|
---|
4287 | if (pToBeParent)
|
---|
4288 | {
|
---|
4289 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4290 | ComAssertRet(i_getParent().isNull(), E_FAIL);
|
---|
4291 | ComAssertRet(i_getChildren().size() == 0, E_FAIL);
|
---|
4292 | }
|
---|
4293 |
|
---|
4294 | ErrorInfoKeeper eik;
|
---|
4295 | MultiResult mrc(S_OK);
|
---|
4296 |
|
---|
4297 | ComObjPtr<Medium> pMedium = this;
|
---|
4298 | while (!pMedium.isNull())
|
---|
4299 | {
|
---|
4300 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
4301 |
|
---|
4302 | /* Accessibility check must be first, otherwise locking interferes
|
---|
4303 | * with getting the medium state. Lock lists are not created for
|
---|
4304 | * fun, and thus getting the medium status is no luxury. */
|
---|
4305 | MediumState_T mediumState = pMedium->i_getState();
|
---|
4306 | if (mediumState == MediumState_Inaccessible)
|
---|
4307 | {
|
---|
4308 | alock.release();
|
---|
4309 | rc = pMedium->i_queryInfo(false /* fSetImageId */, false /* fSetParentId */,
|
---|
4310 | autoCaller);
|
---|
4311 | alock.acquire();
|
---|
4312 | if (FAILED(rc)) return rc;
|
---|
4313 |
|
---|
4314 | mediumState = pMedium->i_getState();
|
---|
4315 | if (mediumState == MediumState_Inaccessible)
|
---|
4316 | {
|
---|
4317 | // ignore inaccessible ISO media and silently return S_OK,
|
---|
4318 | // otherwise VM startup (esp. restore) may fail without good reason
|
---|
4319 | if (!fFailIfInaccessible)
|
---|
4320 | return S_OK;
|
---|
4321 |
|
---|
4322 | // otherwise report an error
|
---|
4323 | Bstr error;
|
---|
4324 | rc = pMedium->COMGETTER(LastAccessError)(error.asOutParam());
|
---|
4325 | if (FAILED(rc)) return rc;
|
---|
4326 |
|
---|
4327 | /* collect multiple errors */
|
---|
4328 | eik.restore();
|
---|
4329 | Assert(!error.isEmpty());
|
---|
4330 | mrc = setError(E_FAIL,
|
---|
4331 | "%ls",
|
---|
4332 | error.raw());
|
---|
4333 | // error message will be something like
|
---|
4334 | // "Could not open the medium ... VD: error VERR_FILE_NOT_FOUND opening image file ... (VERR_FILE_NOT_FOUND).
|
---|
4335 | eik.fetch();
|
---|
4336 | }
|
---|
4337 | }
|
---|
4338 |
|
---|
4339 | if (pMedium == this)
|
---|
4340 | mediumLockList.Prepend(pMedium, fMediumLockWrite);
|
---|
4341 | else
|
---|
4342 | mediumLockList.Prepend(pMedium, fMediumLockWriteAll);
|
---|
4343 |
|
---|
4344 | pMedium = pMedium->i_getParent();
|
---|
4345 | if (pMedium.isNull() && pToBeParent)
|
---|
4346 | {
|
---|
4347 | pMedium = pToBeParent;
|
---|
4348 | pToBeParent = NULL;
|
---|
4349 | }
|
---|
4350 | }
|
---|
4351 |
|
---|
4352 | return mrc;
|
---|
4353 | }
|
---|
4354 |
|
---|
4355 | /**
|
---|
4356 | * Creates a new differencing storage unit using the format of the given target
|
---|
4357 | * medium and the location. Note that @c aTarget must be NotCreated.
|
---|
4358 | *
|
---|
4359 | * The @a aMediumLockList parameter contains the associated medium lock list,
|
---|
4360 | * which must be in locked state. If @a aWait is @c true then the caller is
|
---|
4361 | * responsible for unlocking.
|
---|
4362 | *
|
---|
4363 | * If @a aProgress is not NULL but the object it points to is @c null then a
|
---|
4364 | * new progress object will be created and assigned to @a *aProgress on
|
---|
4365 | * success, otherwise the existing progress object is used. If @a aProgress is
|
---|
4366 | * NULL, then no progress object is created/used at all.
|
---|
4367 | *
|
---|
4368 | * When @a aWait is @c false, this method will create a thread to perform the
|
---|
4369 | * create operation asynchronously and will return immediately. Otherwise, it
|
---|
4370 | * will perform the operation on the calling thread and will not return to the
|
---|
4371 | * caller until the operation is completed. Note that @a aProgress cannot be
|
---|
4372 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
4373 | *
|
---|
4374 | * @param aTarget Target medium.
|
---|
4375 | * @param aVariant Precise medium variant to create.
|
---|
4376 | * @param aMediumLockList List of media which should be locked.
|
---|
4377 | * @param aProgress Where to find/store a Progress object to track
|
---|
4378 | * operation completion.
|
---|
4379 | * @param aWait @c true if this method should block instead of
|
---|
4380 | * creating an asynchronous thread.
|
---|
4381 | *
|
---|
4382 | * @note Locks this object and @a aTarget for writing.
|
---|
4383 | */
|
---|
4384 | HRESULT Medium::i_createDiffStorage(ComObjPtr<Medium> &aTarget,
|
---|
4385 | MediumVariant_T aVariant,
|
---|
4386 | MediumLockList *aMediumLockList,
|
---|
4387 | ComObjPtr<Progress> *aProgress,
|
---|
4388 | bool aWait)
|
---|
4389 | {
|
---|
4390 | AssertReturn(!aTarget.isNull(), E_FAIL);
|
---|
4391 | AssertReturn(aMediumLockList, E_FAIL);
|
---|
4392 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
4393 |
|
---|
4394 | AutoCaller autoCaller(this);
|
---|
4395 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4396 |
|
---|
4397 | AutoCaller targetCaller(aTarget);
|
---|
4398 | if (FAILED(targetCaller.rc())) return targetCaller.rc();
|
---|
4399 |
|
---|
4400 | HRESULT rc = S_OK;
|
---|
4401 | ComObjPtr<Progress> pProgress;
|
---|
4402 | Medium::Task *pTask = NULL;
|
---|
4403 |
|
---|
4404 | try
|
---|
4405 | {
|
---|
4406 | AutoMultiWriteLock2 alock(this, aTarget COMMA_LOCKVAL_SRC_POS);
|
---|
4407 |
|
---|
4408 | ComAssertThrow( m->type != MediumType_Writethrough
|
---|
4409 | && m->type != MediumType_Shareable
|
---|
4410 | && m->type != MediumType_Readonly, E_FAIL);
|
---|
4411 | ComAssertThrow(m->state == MediumState_LockedRead, E_FAIL);
|
---|
4412 |
|
---|
4413 | if (aTarget->m->state != MediumState_NotCreated)
|
---|
4414 | throw aTarget->i_setStateError();
|
---|
4415 |
|
---|
4416 | /* Check that the medium is not attached to the current state of
|
---|
4417 | * any VM referring to it. */
|
---|
4418 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
4419 | it != m->backRefs.end();
|
---|
4420 | ++it)
|
---|
4421 | {
|
---|
4422 | if (it->fInCurState)
|
---|
4423 | {
|
---|
4424 | /* Note: when a VM snapshot is being taken, all normal media
|
---|
4425 | * attached to the VM in the current state will be, as an
|
---|
4426 | * exception, also associated with the snapshot which is about
|
---|
4427 | * to create (see SnapshotMachine::init()) before deassociating
|
---|
4428 | * them from the current state (which takes place only on
|
---|
4429 | * success in Machine::fixupHardDisks()), so that the size of
|
---|
4430 | * snapshotIds will be 1 in this case. The extra condition is
|
---|
4431 | * used to filter out this legal situation. */
|
---|
4432 | if (it->llSnapshotIds.size() == 0)
|
---|
4433 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4434 | 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"),
|
---|
4435 | m->strLocationFull.c_str(), it->machineId.raw());
|
---|
4436 |
|
---|
4437 | Assert(it->llSnapshotIds.size() == 1);
|
---|
4438 | }
|
---|
4439 | }
|
---|
4440 |
|
---|
4441 | if (aProgress != NULL)
|
---|
4442 | {
|
---|
4443 | /* use the existing progress object... */
|
---|
4444 | pProgress = *aProgress;
|
---|
4445 |
|
---|
4446 | /* ...but create a new one if it is null */
|
---|
4447 | if (pProgress.isNull())
|
---|
4448 | {
|
---|
4449 | pProgress.createObject();
|
---|
4450 | rc = pProgress->init(m->pVirtualBox,
|
---|
4451 | static_cast<IMedium*>(this),
|
---|
4452 | BstrFmt(tr("Creating differencing medium storage unit '%s'"),
|
---|
4453 | aTarget->m->strLocationFull.c_str()).raw(),
|
---|
4454 | TRUE /* aCancelable */);
|
---|
4455 | if (FAILED(rc))
|
---|
4456 | throw rc;
|
---|
4457 | }
|
---|
4458 | }
|
---|
4459 |
|
---|
4460 | /* setup task object to carry out the operation sync/async */
|
---|
4461 | pTask = new Medium::CreateDiffTask(this, pProgress, aTarget, aVariant,
|
---|
4462 | aMediumLockList,
|
---|
4463 | aWait /* fKeepMediumLockList */);
|
---|
4464 | rc = pTask->rc();
|
---|
4465 | AssertComRC(rc);
|
---|
4466 | if (FAILED(rc))
|
---|
4467 | throw rc;
|
---|
4468 |
|
---|
4469 | /* register a task (it will deregister itself when done) */
|
---|
4470 | ++m->numCreateDiffTasks;
|
---|
4471 | Assert(m->numCreateDiffTasks != 0); /* overflow? */
|
---|
4472 |
|
---|
4473 | aTarget->m->state = MediumState_Creating;
|
---|
4474 | }
|
---|
4475 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4476 |
|
---|
4477 | if (SUCCEEDED(rc))
|
---|
4478 | {
|
---|
4479 | if (aWait)
|
---|
4480 | rc = i_runNow(pTask);
|
---|
4481 | else
|
---|
4482 | rc = i_startThread(pTask);
|
---|
4483 |
|
---|
4484 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
4485 | *aProgress = pProgress;
|
---|
4486 | }
|
---|
4487 | else if (pTask != NULL)
|
---|
4488 | delete pTask;
|
---|
4489 |
|
---|
4490 | return rc;
|
---|
4491 | }
|
---|
4492 |
|
---|
4493 | /**
|
---|
4494 | * Returns a preferred format for differencing media.
|
---|
4495 | */
|
---|
4496 | Utf8Str Medium::i_getPreferredDiffFormat()
|
---|
4497 | {
|
---|
4498 | AutoCaller autoCaller(this);
|
---|
4499 | AssertComRCReturn(autoCaller.rc(), Utf8Str::Empty);
|
---|
4500 |
|
---|
4501 | /* check that our own format supports diffs */
|
---|
4502 | if (!(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_Differencing))
|
---|
4503 | {
|
---|
4504 | /* use the default format if not */
|
---|
4505 | Utf8Str tmp;
|
---|
4506 | m->pVirtualBox->i_getDefaultHardDiskFormat(tmp);
|
---|
4507 | return tmp;
|
---|
4508 | }
|
---|
4509 |
|
---|
4510 | /* m->strFormat is const, no need to lock */
|
---|
4511 | return m->strFormat;
|
---|
4512 | }
|
---|
4513 |
|
---|
4514 | /**
|
---|
4515 | * Implementation for the public Medium::Close() with the exception of calling
|
---|
4516 | * VirtualBox::saveRegistries(), in case someone wants to call this for several
|
---|
4517 | * media.
|
---|
4518 | *
|
---|
4519 | * After this returns with success, uninit() has been called on the medium, and
|
---|
4520 | * the object is no longer usable ("not ready" state).
|
---|
4521 | *
|
---|
4522 | * @param autoCaller AutoCaller instance which must have been created on the caller's
|
---|
4523 | * stack for this medium. This gets released hereupon
|
---|
4524 | * which the Medium instance gets uninitialized.
|
---|
4525 | * @return
|
---|
4526 | */
|
---|
4527 | HRESULT Medium::i_close(AutoCaller &autoCaller)
|
---|
4528 | {
|
---|
4529 | // must temporarily drop the caller, need the tree lock first
|
---|
4530 | autoCaller.release();
|
---|
4531 |
|
---|
4532 | // we're accessing parent/child and backrefs, so lock the tree first, then ourselves
|
---|
4533 | AutoMultiWriteLock2 multilock(&m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
4534 | this->lockHandle()
|
---|
4535 | COMMA_LOCKVAL_SRC_POS);
|
---|
4536 |
|
---|
4537 | autoCaller.add();
|
---|
4538 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4539 |
|
---|
4540 | LogFlowFunc(("ENTER for %s\n", i_getLocationFull().c_str()));
|
---|
4541 |
|
---|
4542 | bool wasCreated = true;
|
---|
4543 |
|
---|
4544 | switch (m->state)
|
---|
4545 | {
|
---|
4546 | case MediumState_NotCreated:
|
---|
4547 | wasCreated = false;
|
---|
4548 | break;
|
---|
4549 | case MediumState_Created:
|
---|
4550 | case MediumState_Inaccessible:
|
---|
4551 | break;
|
---|
4552 | default:
|
---|
4553 | return i_setStateError();
|
---|
4554 | }
|
---|
4555 |
|
---|
4556 | if (m->backRefs.size() != 0)
|
---|
4557 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4558 | tr("Medium '%s' cannot be closed because it is still attached to %d virtual machines"),
|
---|
4559 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
4560 |
|
---|
4561 | // perform extra media-dependent close checks
|
---|
4562 | HRESULT rc = i_canClose();
|
---|
4563 | if (FAILED(rc)) return rc;
|
---|
4564 |
|
---|
4565 | m->fClosing = true;
|
---|
4566 |
|
---|
4567 | if (wasCreated)
|
---|
4568 | {
|
---|
4569 | // remove from the list of known media before performing actual
|
---|
4570 | // uninitialization (to keep the media registry consistent on
|
---|
4571 | // failure to do so)
|
---|
4572 | rc = i_unregisterWithVirtualBox();
|
---|
4573 | if (FAILED(rc)) return rc;
|
---|
4574 |
|
---|
4575 | multilock.release();
|
---|
4576 | // Release the AutoCaller now, as otherwise uninit() will simply hang.
|
---|
4577 | // Needs to be done before mark the registries as modified and saving
|
---|
4578 | // the registry, as otherwise there may be a deadlock with someone else
|
---|
4579 | // closing this object while we're in i_saveModifiedRegistries(), which
|
---|
4580 | // needs the media tree lock, which the other thread holds until after
|
---|
4581 | // uninit() below.
|
---|
4582 | autoCaller.release();
|
---|
4583 | i_markRegistriesModified();
|
---|
4584 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
4585 | }
|
---|
4586 | else
|
---|
4587 | {
|
---|
4588 | multilock.release();
|
---|
4589 | // release the AutoCaller, as otherwise uninit() will simply hang
|
---|
4590 | autoCaller.release();
|
---|
4591 | }
|
---|
4592 |
|
---|
4593 | // Keep the locks held until after uninit, as otherwise the consistency
|
---|
4594 | // of the medium tree cannot be guaranteed.
|
---|
4595 | uninit();
|
---|
4596 |
|
---|
4597 | LogFlowFuncLeave();
|
---|
4598 |
|
---|
4599 | return rc;
|
---|
4600 | }
|
---|
4601 |
|
---|
4602 | /**
|
---|
4603 | * Deletes the medium storage unit.
|
---|
4604 | *
|
---|
4605 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
4606 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
4607 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
4608 | * progress object is created/used at all.
|
---|
4609 | *
|
---|
4610 | * When @a aWait is @c false, this method will create a thread to perform the
|
---|
4611 | * delete operation asynchronously and will return immediately. Otherwise, it
|
---|
4612 | * will perform the operation on the calling thread and will not return to the
|
---|
4613 | * caller until the operation is completed. Note that @a aProgress cannot be
|
---|
4614 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
4615 | *
|
---|
4616 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
4617 | * completion.
|
---|
4618 | * @param aWait @c true if this method should block instead of creating
|
---|
4619 | * an asynchronous thread.
|
---|
4620 | *
|
---|
4621 | * @note Locks mVirtualBox and this object for writing. Locks medium tree for
|
---|
4622 | * writing.
|
---|
4623 | */
|
---|
4624 | HRESULT Medium::i_deleteStorage(ComObjPtr<Progress> *aProgress,
|
---|
4625 | bool aWait)
|
---|
4626 | {
|
---|
4627 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
4628 |
|
---|
4629 | AutoCaller autoCaller(this);
|
---|
4630 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4631 |
|
---|
4632 | HRESULT rc = S_OK;
|
---|
4633 | ComObjPtr<Progress> pProgress;
|
---|
4634 | Medium::Task *pTask = NULL;
|
---|
4635 |
|
---|
4636 | try
|
---|
4637 | {
|
---|
4638 | /* we're accessing the media tree, and canClose() needs it too */
|
---|
4639 | AutoMultiWriteLock2 multilock(&m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
4640 | this->lockHandle()
|
---|
4641 | COMMA_LOCKVAL_SRC_POS);
|
---|
4642 | LogFlowThisFunc(("aWait=%RTbool locationFull=%s\n", aWait, i_getLocationFull().c_str() ));
|
---|
4643 |
|
---|
4644 | if ( !(m->formatObj->i_getCapabilities() & ( MediumFormatCapabilities_CreateDynamic
|
---|
4645 | | MediumFormatCapabilities_CreateFixed)))
|
---|
4646 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
4647 | tr("Medium format '%s' does not support storage deletion"),
|
---|
4648 | m->strFormat.c_str());
|
---|
4649 |
|
---|
4650 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
4651 | /** @todo r=klaus would be great if this could be moved to the async
|
---|
4652 | * part of the operation as it can take quite a while */
|
---|
4653 | if (m->queryInfoRunning)
|
---|
4654 | {
|
---|
4655 | while (m->queryInfoRunning)
|
---|
4656 | {
|
---|
4657 | multilock.release();
|
---|
4658 | /* Must not hold the media tree lock or the object lock, as
|
---|
4659 | * Medium::i_queryInfo needs this lock and thus we would run
|
---|
4660 | * into a deadlock here. */
|
---|
4661 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
4662 | Assert(!isWriteLockOnCurrentThread());
|
---|
4663 | {
|
---|
4664 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
4665 | }
|
---|
4666 | multilock.acquire();
|
---|
4667 | }
|
---|
4668 | }
|
---|
4669 |
|
---|
4670 | /* Note that we are fine with Inaccessible state too: a) for symmetry
|
---|
4671 | * with create calls and b) because it doesn't really harm to try, if
|
---|
4672 | * it is really inaccessible, the delete operation will fail anyway.
|
---|
4673 | * Accepting Inaccessible state is especially important because all
|
---|
4674 | * registered media are initially Inaccessible upon VBoxSVC startup
|
---|
4675 | * until COMGETTER(RefreshState) is called. Accept Deleting state
|
---|
4676 | * because some callers need to put the medium in this state early
|
---|
4677 | * to prevent races. */
|
---|
4678 | switch (m->state)
|
---|
4679 | {
|
---|
4680 | case MediumState_Created:
|
---|
4681 | case MediumState_Deleting:
|
---|
4682 | case MediumState_Inaccessible:
|
---|
4683 | break;
|
---|
4684 | default:
|
---|
4685 | throw i_setStateError();
|
---|
4686 | }
|
---|
4687 |
|
---|
4688 | if (m->backRefs.size() != 0)
|
---|
4689 | {
|
---|
4690 | Utf8Str strMachines;
|
---|
4691 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
4692 | it != m->backRefs.end();
|
---|
4693 | ++it)
|
---|
4694 | {
|
---|
4695 | const BackRef &b = *it;
|
---|
4696 | if (strMachines.length())
|
---|
4697 | strMachines.append(", ");
|
---|
4698 | strMachines.append(b.machineId.toString().c_str());
|
---|
4699 | }
|
---|
4700 | #ifdef DEBUG
|
---|
4701 | i_dumpBackRefs();
|
---|
4702 | #endif
|
---|
4703 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
4704 | tr("Cannot delete storage: medium '%s' is still attached to the following %d virtual machine(s): %s"),
|
---|
4705 | m->strLocationFull.c_str(),
|
---|
4706 | m->backRefs.size(),
|
---|
4707 | strMachines.c_str());
|
---|
4708 | }
|
---|
4709 |
|
---|
4710 | rc = i_canClose();
|
---|
4711 | if (FAILED(rc))
|
---|
4712 | throw rc;
|
---|
4713 |
|
---|
4714 | /* go to Deleting state, so that the medium is not actually locked */
|
---|
4715 | if (m->state != MediumState_Deleting)
|
---|
4716 | {
|
---|
4717 | rc = i_markForDeletion();
|
---|
4718 | if (FAILED(rc))
|
---|
4719 | throw rc;
|
---|
4720 | }
|
---|
4721 |
|
---|
4722 | /* Build the medium lock list. */
|
---|
4723 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
4724 | multilock.release();
|
---|
4725 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
4726 | true /* fMediumLockWrite */,
|
---|
4727 | false /* fMediumLockWriteAll */,
|
---|
4728 | NULL,
|
---|
4729 | *pMediumLockList);
|
---|
4730 | multilock.acquire();
|
---|
4731 | if (FAILED(rc))
|
---|
4732 | {
|
---|
4733 | delete pMediumLockList;
|
---|
4734 | throw rc;
|
---|
4735 | }
|
---|
4736 |
|
---|
4737 | multilock.release();
|
---|
4738 | rc = pMediumLockList->Lock();
|
---|
4739 | multilock.acquire();
|
---|
4740 | if (FAILED(rc))
|
---|
4741 | {
|
---|
4742 | delete pMediumLockList;
|
---|
4743 | throw setError(rc,
|
---|
4744 | tr("Failed to lock media when deleting '%s'"),
|
---|
4745 | i_getLocationFull().c_str());
|
---|
4746 | }
|
---|
4747 |
|
---|
4748 | /* try to remove from the list of known media before performing
|
---|
4749 | * actual deletion (we favor the consistency of the media registry
|
---|
4750 | * which would have been broken if unregisterWithVirtualBox() failed
|
---|
4751 | * after we successfully deleted the storage) */
|
---|
4752 | rc = i_unregisterWithVirtualBox();
|
---|
4753 | if (FAILED(rc))
|
---|
4754 | throw rc;
|
---|
4755 | // no longer need lock
|
---|
4756 | multilock.release();
|
---|
4757 | i_markRegistriesModified();
|
---|
4758 |
|
---|
4759 | if (aProgress != NULL)
|
---|
4760 | {
|
---|
4761 | /* use the existing progress object... */
|
---|
4762 | pProgress = *aProgress;
|
---|
4763 |
|
---|
4764 | /* ...but create a new one if it is null */
|
---|
4765 | if (pProgress.isNull())
|
---|
4766 | {
|
---|
4767 | pProgress.createObject();
|
---|
4768 | rc = pProgress->init(m->pVirtualBox,
|
---|
4769 | static_cast<IMedium*>(this),
|
---|
4770 | BstrFmt(tr("Deleting medium storage unit '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
4771 | FALSE /* aCancelable */);
|
---|
4772 | if (FAILED(rc))
|
---|
4773 | throw rc;
|
---|
4774 | }
|
---|
4775 | }
|
---|
4776 |
|
---|
4777 | /* setup task object to carry out the operation sync/async */
|
---|
4778 | pTask = new Medium::DeleteTask(this, pProgress, pMediumLockList);
|
---|
4779 | rc = pTask->rc();
|
---|
4780 | AssertComRC(rc);
|
---|
4781 | if (FAILED(rc))
|
---|
4782 | throw rc;
|
---|
4783 | }
|
---|
4784 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4785 |
|
---|
4786 | if (SUCCEEDED(rc))
|
---|
4787 | {
|
---|
4788 | if (aWait)
|
---|
4789 | rc = i_runNow(pTask);
|
---|
4790 | else
|
---|
4791 | rc = i_startThread(pTask);
|
---|
4792 |
|
---|
4793 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
4794 | *aProgress = pProgress;
|
---|
4795 |
|
---|
4796 | }
|
---|
4797 | else
|
---|
4798 | {
|
---|
4799 | if (pTask)
|
---|
4800 | delete pTask;
|
---|
4801 |
|
---|
4802 | /* Undo deleting state if necessary. */
|
---|
4803 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4804 | /* Make sure that any error signalled by unmarkForDeletion() is not
|
---|
4805 | * ending up in the error list (if the caller uses MultiResult). It
|
---|
4806 | * usually is spurious, as in most cases the medium hasn't been marked
|
---|
4807 | * for deletion when the error was thrown above. */
|
---|
4808 | ErrorInfoKeeper eik;
|
---|
4809 | i_unmarkForDeletion();
|
---|
4810 | }
|
---|
4811 |
|
---|
4812 | return rc;
|
---|
4813 | }
|
---|
4814 |
|
---|
4815 | /**
|
---|
4816 | * Mark a medium for deletion.
|
---|
4817 | *
|
---|
4818 | * @note Caller must hold the write lock on this medium!
|
---|
4819 | */
|
---|
4820 | HRESULT Medium::i_markForDeletion()
|
---|
4821 | {
|
---|
4822 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
4823 | switch (m->state)
|
---|
4824 | {
|
---|
4825 | case MediumState_Created:
|
---|
4826 | case MediumState_Inaccessible:
|
---|
4827 | m->preLockState = m->state;
|
---|
4828 | m->state = MediumState_Deleting;
|
---|
4829 | return S_OK;
|
---|
4830 | default:
|
---|
4831 | return i_setStateError();
|
---|
4832 | }
|
---|
4833 | }
|
---|
4834 |
|
---|
4835 | /**
|
---|
4836 | * Removes the "mark for deletion".
|
---|
4837 | *
|
---|
4838 | * @note Caller must hold the write lock on this medium!
|
---|
4839 | */
|
---|
4840 | HRESULT Medium::i_unmarkForDeletion()
|
---|
4841 | {
|
---|
4842 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
4843 | switch (m->state)
|
---|
4844 | {
|
---|
4845 | case MediumState_Deleting:
|
---|
4846 | m->state = m->preLockState;
|
---|
4847 | return S_OK;
|
---|
4848 | default:
|
---|
4849 | return i_setStateError();
|
---|
4850 | }
|
---|
4851 | }
|
---|
4852 |
|
---|
4853 | /**
|
---|
4854 | * Mark a medium for deletion which is in locked state.
|
---|
4855 | *
|
---|
4856 | * @note Caller must hold the write lock on this medium!
|
---|
4857 | */
|
---|
4858 | HRESULT Medium::i_markLockedForDeletion()
|
---|
4859 | {
|
---|
4860 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
4861 | if ( ( m->state == MediumState_LockedRead
|
---|
4862 | || m->state == MediumState_LockedWrite)
|
---|
4863 | && m->preLockState == MediumState_Created)
|
---|
4864 | {
|
---|
4865 | m->preLockState = MediumState_Deleting;
|
---|
4866 | return S_OK;
|
---|
4867 | }
|
---|
4868 | else
|
---|
4869 | return i_setStateError();
|
---|
4870 | }
|
---|
4871 |
|
---|
4872 | /**
|
---|
4873 | * Removes the "mark for deletion" for a medium in locked state.
|
---|
4874 | *
|
---|
4875 | * @note Caller must hold the write lock on this medium!
|
---|
4876 | */
|
---|
4877 | HRESULT Medium::i_unmarkLockedForDeletion()
|
---|
4878 | {
|
---|
4879 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
4880 | if ( ( m->state == MediumState_LockedRead
|
---|
4881 | || m->state == MediumState_LockedWrite)
|
---|
4882 | && m->preLockState == MediumState_Deleting)
|
---|
4883 | {
|
---|
4884 | m->preLockState = MediumState_Created;
|
---|
4885 | return S_OK;
|
---|
4886 | }
|
---|
4887 | else
|
---|
4888 | return i_setStateError();
|
---|
4889 | }
|
---|
4890 |
|
---|
4891 | /**
|
---|
4892 | * Queries the preferred merge direction from this to the other medium, i.e.
|
---|
4893 | * the one which requires the least amount of I/O and therefore time and
|
---|
4894 | * disk consumption.
|
---|
4895 | *
|
---|
4896 | * @returns Status code.
|
---|
4897 | * @retval E_FAIL in case determining the merge direction fails for some reason,
|
---|
4898 | * for example if getting the size of the media fails. There is no
|
---|
4899 | * error set though and the caller is free to continue to find out
|
---|
4900 | * what was going wrong later. Leaves fMergeForward unset.
|
---|
4901 | * @retval VBOX_E_INVALID_OBJECT_STATE if both media are not related to each other
|
---|
4902 | * An error is set.
|
---|
4903 | * @param pOther The other medium to merge with.
|
---|
4904 | * @param fMergeForward Resulting preferred merge direction (out).
|
---|
4905 | */
|
---|
4906 | HRESULT Medium::i_queryPreferredMergeDirection(const ComObjPtr<Medium> &pOther,
|
---|
4907 | bool &fMergeForward)
|
---|
4908 | {
|
---|
4909 | AssertReturn(pOther != NULL, E_FAIL);
|
---|
4910 | AssertReturn(pOther != this, E_FAIL);
|
---|
4911 |
|
---|
4912 | AutoCaller autoCaller(this);
|
---|
4913 | AssertComRCReturnRC(autoCaller.rc());
|
---|
4914 |
|
---|
4915 | AutoCaller otherCaller(pOther);
|
---|
4916 | AssertComRCReturnRC(otherCaller.rc());
|
---|
4917 |
|
---|
4918 | HRESULT rc = S_OK;
|
---|
4919 | bool fThisParent = false; /**<< Flag whether this medium is the parent of pOther. */
|
---|
4920 |
|
---|
4921 | try
|
---|
4922 | {
|
---|
4923 | // locking: we need the tree lock first because we access parent pointers
|
---|
4924 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4925 |
|
---|
4926 | /* more sanity checking and figuring out the current merge direction */
|
---|
4927 | ComObjPtr<Medium> pMedium = i_getParent();
|
---|
4928 | while (!pMedium.isNull() && pMedium != pOther)
|
---|
4929 | pMedium = pMedium->i_getParent();
|
---|
4930 | if (pMedium == pOther)
|
---|
4931 | fThisParent = false;
|
---|
4932 | else
|
---|
4933 | {
|
---|
4934 | pMedium = pOther->i_getParent();
|
---|
4935 | while (!pMedium.isNull() && pMedium != this)
|
---|
4936 | pMedium = pMedium->i_getParent();
|
---|
4937 | if (pMedium == this)
|
---|
4938 | fThisParent = true;
|
---|
4939 | else
|
---|
4940 | {
|
---|
4941 | Utf8Str tgtLoc;
|
---|
4942 | {
|
---|
4943 | AutoReadLock alock(pOther COMMA_LOCKVAL_SRC_POS);
|
---|
4944 | tgtLoc = pOther->i_getLocationFull();
|
---|
4945 | }
|
---|
4946 |
|
---|
4947 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4948 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4949 | tr("Media '%s' and '%s' are unrelated"),
|
---|
4950 | m->strLocationFull.c_str(), tgtLoc.c_str());
|
---|
4951 | }
|
---|
4952 | }
|
---|
4953 |
|
---|
4954 | /*
|
---|
4955 | * Figure out the preferred merge direction. The current way is to
|
---|
4956 | * get the current sizes of file based images and select the merge
|
---|
4957 | * direction depending on the size.
|
---|
4958 | *
|
---|
4959 | * Can't use the VD API to get current size here as the media might
|
---|
4960 | * be write locked by a running VM. Resort to RTFileQuerySize().
|
---|
4961 | */
|
---|
4962 | int vrc = VINF_SUCCESS;
|
---|
4963 | uint64_t cbMediumThis = 0;
|
---|
4964 | uint64_t cbMediumOther = 0;
|
---|
4965 |
|
---|
4966 | if (i_isMediumFormatFile() && pOther->i_isMediumFormatFile())
|
---|
4967 | {
|
---|
4968 | vrc = RTFileQuerySize(this->i_getLocationFull().c_str(), &cbMediumThis);
|
---|
4969 | if (RT_SUCCESS(vrc))
|
---|
4970 | {
|
---|
4971 | vrc = RTFileQuerySize(pOther->i_getLocationFull().c_str(),
|
---|
4972 | &cbMediumOther);
|
---|
4973 | }
|
---|
4974 |
|
---|
4975 | if (RT_FAILURE(vrc))
|
---|
4976 | rc = E_FAIL;
|
---|
4977 | else
|
---|
4978 | {
|
---|
4979 | /*
|
---|
4980 | * Check which merge direction might be more optimal.
|
---|
4981 | * This method is not bullet proof of course as there might
|
---|
4982 | * be overlapping blocks in the images so the file size is
|
---|
4983 | * not the best indicator but it is good enough for our purpose
|
---|
4984 | * and everything else is too complicated, especially when the
|
---|
4985 | * media are used by a running VM.
|
---|
4986 | */
|
---|
4987 | bool fMergeIntoThis = cbMediumThis > cbMediumOther;
|
---|
4988 | fMergeForward = fMergeIntoThis ^ fThisParent;
|
---|
4989 | }
|
---|
4990 | }
|
---|
4991 | }
|
---|
4992 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4993 |
|
---|
4994 | return rc;
|
---|
4995 | }
|
---|
4996 |
|
---|
4997 | /**
|
---|
4998 | * Prepares this (source) medium, target medium and all intermediate media
|
---|
4999 | * for the merge operation.
|
---|
5000 | *
|
---|
5001 | * This method is to be called prior to calling the #mergeTo() to perform
|
---|
5002 | * necessary consistency checks and place involved media to appropriate
|
---|
5003 | * states. If #mergeTo() is not called or fails, the state modifications
|
---|
5004 | * performed by this method must be undone by #cancelMergeTo().
|
---|
5005 | *
|
---|
5006 | * See #mergeTo() for more information about merging.
|
---|
5007 | *
|
---|
5008 | * @param pTarget Target medium.
|
---|
5009 | * @param aMachineId Allowed machine attachment. NULL means do not check.
|
---|
5010 | * @param aSnapshotId Allowed snapshot attachment. NULL or empty UUID means
|
---|
5011 | * do not check.
|
---|
5012 | * @param fLockMedia Flag whether to lock the medium lock list or not.
|
---|
5013 | * If set to false and the medium lock list locking fails
|
---|
5014 | * later you must call #cancelMergeTo().
|
---|
5015 | * @param fMergeForward Resulting merge direction (out).
|
---|
5016 | * @param pParentForTarget New parent for target medium after merge (out).
|
---|
5017 | * @param aChildrenToReparent Medium lock list containing all children of the
|
---|
5018 | * source which will have to be reparented to the target
|
---|
5019 | * after merge (out).
|
---|
5020 | * @param aMediumLockList Medium locking information (out).
|
---|
5021 | *
|
---|
5022 | * @note Locks medium tree for reading. Locks this object, aTarget and all
|
---|
5023 | * intermediate media for writing.
|
---|
5024 | */
|
---|
5025 | HRESULT Medium::i_prepareMergeTo(const ComObjPtr<Medium> &pTarget,
|
---|
5026 | const Guid *aMachineId,
|
---|
5027 | const Guid *aSnapshotId,
|
---|
5028 | bool fLockMedia,
|
---|
5029 | bool &fMergeForward,
|
---|
5030 | ComObjPtr<Medium> &pParentForTarget,
|
---|
5031 | MediumLockList * &aChildrenToReparent,
|
---|
5032 | MediumLockList * &aMediumLockList)
|
---|
5033 | {
|
---|
5034 | AssertReturn(pTarget != NULL, E_FAIL);
|
---|
5035 | AssertReturn(pTarget != this, E_FAIL);
|
---|
5036 |
|
---|
5037 | AutoCaller autoCaller(this);
|
---|
5038 | AssertComRCReturnRC(autoCaller.rc());
|
---|
5039 |
|
---|
5040 | AutoCaller targetCaller(pTarget);
|
---|
5041 | AssertComRCReturnRC(targetCaller.rc());
|
---|
5042 |
|
---|
5043 | HRESULT rc = S_OK;
|
---|
5044 | fMergeForward = false;
|
---|
5045 | pParentForTarget.setNull();
|
---|
5046 | Assert(aChildrenToReparent == NULL);
|
---|
5047 | aChildrenToReparent = NULL;
|
---|
5048 | Assert(aMediumLockList == NULL);
|
---|
5049 | aMediumLockList = NULL;
|
---|
5050 |
|
---|
5051 | try
|
---|
5052 | {
|
---|
5053 | // locking: we need the tree lock first because we access parent pointers
|
---|
5054 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5055 |
|
---|
5056 | /* more sanity checking and figuring out the merge direction */
|
---|
5057 | ComObjPtr<Medium> pMedium = i_getParent();
|
---|
5058 | while (!pMedium.isNull() && pMedium != pTarget)
|
---|
5059 | pMedium = pMedium->i_getParent();
|
---|
5060 | if (pMedium == pTarget)
|
---|
5061 | fMergeForward = false;
|
---|
5062 | else
|
---|
5063 | {
|
---|
5064 | pMedium = pTarget->i_getParent();
|
---|
5065 | while (!pMedium.isNull() && pMedium != this)
|
---|
5066 | pMedium = pMedium->i_getParent();
|
---|
5067 | if (pMedium == this)
|
---|
5068 | fMergeForward = true;
|
---|
5069 | else
|
---|
5070 | {
|
---|
5071 | Utf8Str tgtLoc;
|
---|
5072 | {
|
---|
5073 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5074 | tgtLoc = pTarget->i_getLocationFull();
|
---|
5075 | }
|
---|
5076 |
|
---|
5077 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5078 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5079 | tr("Media '%s' and '%s' are unrelated"),
|
---|
5080 | m->strLocationFull.c_str(), tgtLoc.c_str());
|
---|
5081 | }
|
---|
5082 | }
|
---|
5083 |
|
---|
5084 | /* Build the lock list. */
|
---|
5085 | aMediumLockList = new MediumLockList();
|
---|
5086 | treeLock.release();
|
---|
5087 | if (fMergeForward)
|
---|
5088 | rc = pTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5089 | true /* fMediumLockWrite */,
|
---|
5090 | false /* fMediumLockWriteAll */,
|
---|
5091 | NULL,
|
---|
5092 | *aMediumLockList);
|
---|
5093 | else
|
---|
5094 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5095 | false /* fMediumLockWrite */,
|
---|
5096 | false /* fMediumLockWriteAll */,
|
---|
5097 | NULL,
|
---|
5098 | *aMediumLockList);
|
---|
5099 | treeLock.acquire();
|
---|
5100 | if (FAILED(rc))
|
---|
5101 | throw rc;
|
---|
5102 |
|
---|
5103 | /* Sanity checking, must be after lock list creation as it depends on
|
---|
5104 | * valid medium states. The medium objects must be accessible. Only
|
---|
5105 | * do this if immediate locking is requested, otherwise it fails when
|
---|
5106 | * we construct a medium lock list for an already running VM. Snapshot
|
---|
5107 | * deletion uses this to simplify its life. */
|
---|
5108 | if (fLockMedia)
|
---|
5109 | {
|
---|
5110 | {
|
---|
5111 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5112 | if (m->state != MediumState_Created)
|
---|
5113 | throw i_setStateError();
|
---|
5114 | }
|
---|
5115 | {
|
---|
5116 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5117 | if (pTarget->m->state != MediumState_Created)
|
---|
5118 | throw pTarget->i_setStateError();
|
---|
5119 | }
|
---|
5120 | }
|
---|
5121 |
|
---|
5122 | /* check medium attachment and other sanity conditions */
|
---|
5123 | if (fMergeForward)
|
---|
5124 | {
|
---|
5125 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5126 | if (i_getChildren().size() > 1)
|
---|
5127 | {
|
---|
5128 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5129 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
5130 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
5131 | }
|
---|
5132 | /* One backreference is only allowed if the machine ID is not empty
|
---|
5133 | * and it matches the machine the medium is attached to (including
|
---|
5134 | * the snapshot ID if not empty). */
|
---|
5135 | if ( m->backRefs.size() != 0
|
---|
5136 | && ( !aMachineId
|
---|
5137 | || m->backRefs.size() != 1
|
---|
5138 | || aMachineId->isZero()
|
---|
5139 | || *i_getFirstMachineBackrefId() != *aMachineId
|
---|
5140 | || ( (!aSnapshotId || !aSnapshotId->isZero())
|
---|
5141 | && *i_getFirstMachineBackrefSnapshotId() != *aSnapshotId)))
|
---|
5142 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5143 | tr("Medium '%s' is attached to %d virtual machines"),
|
---|
5144 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
5145 | if (m->type == MediumType_Immutable)
|
---|
5146 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5147 | tr("Medium '%s' is immutable"),
|
---|
5148 | m->strLocationFull.c_str());
|
---|
5149 | if (m->type == MediumType_MultiAttach)
|
---|
5150 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5151 | tr("Medium '%s' is multi-attach"),
|
---|
5152 | m->strLocationFull.c_str());
|
---|
5153 | }
|
---|
5154 | else
|
---|
5155 | {
|
---|
5156 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5157 | if (pTarget->i_getChildren().size() > 1)
|
---|
5158 | {
|
---|
5159 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5160 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
5161 | pTarget->m->strLocationFull.c_str(),
|
---|
5162 | pTarget->i_getChildren().size());
|
---|
5163 | }
|
---|
5164 | if (pTarget->m->type == MediumType_Immutable)
|
---|
5165 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5166 | tr("Medium '%s' is immutable"),
|
---|
5167 | pTarget->m->strLocationFull.c_str());
|
---|
5168 | if (pTarget->m->type == MediumType_MultiAttach)
|
---|
5169 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5170 | tr("Medium '%s' is multi-attach"),
|
---|
5171 | pTarget->m->strLocationFull.c_str());
|
---|
5172 | }
|
---|
5173 | ComObjPtr<Medium> pLast(fMergeForward ? (Medium *)pTarget : this);
|
---|
5174 | ComObjPtr<Medium> pLastIntermediate = pLast->i_getParent();
|
---|
5175 | for (pLast = pLastIntermediate;
|
---|
5176 | !pLast.isNull() && pLast != pTarget && pLast != this;
|
---|
5177 | pLast = pLast->i_getParent())
|
---|
5178 | {
|
---|
5179 | AutoReadLock alock(pLast COMMA_LOCKVAL_SRC_POS);
|
---|
5180 | if (pLast->i_getChildren().size() > 1)
|
---|
5181 | {
|
---|
5182 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5183 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
5184 | pLast->m->strLocationFull.c_str(),
|
---|
5185 | pLast->i_getChildren().size());
|
---|
5186 | }
|
---|
5187 | if (pLast->m->backRefs.size() != 0)
|
---|
5188 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5189 | tr("Medium '%s' is attached to %d virtual machines"),
|
---|
5190 | pLast->m->strLocationFull.c_str(),
|
---|
5191 | pLast->m->backRefs.size());
|
---|
5192 |
|
---|
5193 | }
|
---|
5194 |
|
---|
5195 | /* Update medium states appropriately */
|
---|
5196 | {
|
---|
5197 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5198 |
|
---|
5199 | if (m->state == MediumState_Created)
|
---|
5200 | {
|
---|
5201 | rc = i_markForDeletion();
|
---|
5202 | if (FAILED(rc))
|
---|
5203 | throw rc;
|
---|
5204 | }
|
---|
5205 | else
|
---|
5206 | {
|
---|
5207 | if (fLockMedia)
|
---|
5208 | throw i_setStateError();
|
---|
5209 | else if ( m->state == MediumState_LockedWrite
|
---|
5210 | || m->state == MediumState_LockedRead)
|
---|
5211 | {
|
---|
5212 | /* Either mark it for deletion in locked state or allow
|
---|
5213 | * others to have done so. */
|
---|
5214 | if (m->preLockState == MediumState_Created)
|
---|
5215 | i_markLockedForDeletion();
|
---|
5216 | else if (m->preLockState != MediumState_Deleting)
|
---|
5217 | throw i_setStateError();
|
---|
5218 | }
|
---|
5219 | else
|
---|
5220 | throw i_setStateError();
|
---|
5221 | }
|
---|
5222 | }
|
---|
5223 |
|
---|
5224 | if (fMergeForward)
|
---|
5225 | {
|
---|
5226 | /* we will need parent to reparent target */
|
---|
5227 | pParentForTarget = i_getParent();
|
---|
5228 | }
|
---|
5229 | else
|
---|
5230 | {
|
---|
5231 | /* we will need to reparent children of the source */
|
---|
5232 | aChildrenToReparent = new MediumLockList();
|
---|
5233 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
5234 | it != i_getChildren().end();
|
---|
5235 | ++it)
|
---|
5236 | {
|
---|
5237 | pMedium = *it;
|
---|
5238 | aChildrenToReparent->Append(pMedium, true /* fLockWrite */);
|
---|
5239 | }
|
---|
5240 | if (fLockMedia && aChildrenToReparent)
|
---|
5241 | {
|
---|
5242 | treeLock.release();
|
---|
5243 | rc = aChildrenToReparent->Lock();
|
---|
5244 | treeLock.acquire();
|
---|
5245 | if (FAILED(rc))
|
---|
5246 | throw rc;
|
---|
5247 | }
|
---|
5248 | }
|
---|
5249 | for (pLast = pLastIntermediate;
|
---|
5250 | !pLast.isNull() && pLast != pTarget && pLast != this;
|
---|
5251 | pLast = pLast->i_getParent())
|
---|
5252 | {
|
---|
5253 | AutoWriteLock alock(pLast COMMA_LOCKVAL_SRC_POS);
|
---|
5254 | if (pLast->m->state == MediumState_Created)
|
---|
5255 | {
|
---|
5256 | rc = pLast->i_markForDeletion();
|
---|
5257 | if (FAILED(rc))
|
---|
5258 | throw rc;
|
---|
5259 | }
|
---|
5260 | else
|
---|
5261 | throw pLast->i_setStateError();
|
---|
5262 | }
|
---|
5263 |
|
---|
5264 | /* Tweak the lock list in the backward merge case, as the target
|
---|
5265 | * isn't marked to be locked for writing yet. */
|
---|
5266 | if (!fMergeForward)
|
---|
5267 | {
|
---|
5268 | MediumLockList::Base::iterator lockListBegin =
|
---|
5269 | aMediumLockList->GetBegin();
|
---|
5270 | MediumLockList::Base::iterator lockListEnd =
|
---|
5271 | aMediumLockList->GetEnd();
|
---|
5272 | ++lockListEnd;
|
---|
5273 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
5274 | it != lockListEnd;
|
---|
5275 | ++it)
|
---|
5276 | {
|
---|
5277 | MediumLock &mediumLock = *it;
|
---|
5278 | if (mediumLock.GetMedium() == pTarget)
|
---|
5279 | {
|
---|
5280 | HRESULT rc2 = mediumLock.UpdateLock(true);
|
---|
5281 | AssertComRC(rc2);
|
---|
5282 | break;
|
---|
5283 | }
|
---|
5284 | }
|
---|
5285 | }
|
---|
5286 |
|
---|
5287 | if (fLockMedia)
|
---|
5288 | {
|
---|
5289 | treeLock.release();
|
---|
5290 | rc = aMediumLockList->Lock();
|
---|
5291 | treeLock.acquire();
|
---|
5292 | if (FAILED(rc))
|
---|
5293 | {
|
---|
5294 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5295 | throw setError(rc,
|
---|
5296 | tr("Failed to lock media when merging to '%s'"),
|
---|
5297 | pTarget->i_getLocationFull().c_str());
|
---|
5298 | }
|
---|
5299 | }
|
---|
5300 | }
|
---|
5301 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5302 |
|
---|
5303 | if (FAILED(rc))
|
---|
5304 | {
|
---|
5305 | if (aMediumLockList)
|
---|
5306 | {
|
---|
5307 | delete aMediumLockList;
|
---|
5308 | aMediumLockList = NULL;
|
---|
5309 | }
|
---|
5310 | if (aChildrenToReparent)
|
---|
5311 | {
|
---|
5312 | delete aChildrenToReparent;
|
---|
5313 | aChildrenToReparent = NULL;
|
---|
5314 | }
|
---|
5315 | }
|
---|
5316 |
|
---|
5317 | return rc;
|
---|
5318 | }
|
---|
5319 |
|
---|
5320 | /**
|
---|
5321 | * Merges this medium to the specified medium which must be either its
|
---|
5322 | * direct ancestor or descendant.
|
---|
5323 | *
|
---|
5324 | * Given this medium is SOURCE and the specified medium is TARGET, we will
|
---|
5325 | * get two variants of the merge operation:
|
---|
5326 | *
|
---|
5327 | * forward merge
|
---|
5328 | * ------------------------->
|
---|
5329 | * [Extra] <- SOURCE <- Intermediate <- TARGET
|
---|
5330 | * Any Del Del LockWr
|
---|
5331 | *
|
---|
5332 | *
|
---|
5333 | * backward merge
|
---|
5334 | * <-------------------------
|
---|
5335 | * TARGET <- Intermediate <- SOURCE <- [Extra]
|
---|
5336 | * LockWr Del Del LockWr
|
---|
5337 | *
|
---|
5338 | * Each diagram shows the involved media on the media chain where
|
---|
5339 | * SOURCE and TARGET belong. Under each medium there is a state value which
|
---|
5340 | * the medium must have at a time of the mergeTo() call.
|
---|
5341 | *
|
---|
5342 | * The media in the square braces may be absent (e.g. when the forward
|
---|
5343 | * operation takes place and SOURCE is the base medium, or when the backward
|
---|
5344 | * merge operation takes place and TARGET is the last child in the chain) but if
|
---|
5345 | * they present they are involved too as shown.
|
---|
5346 | *
|
---|
5347 | * Neither the source medium nor intermediate media may be attached to
|
---|
5348 | * any VM directly or in the snapshot, otherwise this method will assert.
|
---|
5349 | *
|
---|
5350 | * The #prepareMergeTo() method must be called prior to this method to place all
|
---|
5351 | * involved to necessary states and perform other consistency checks.
|
---|
5352 | *
|
---|
5353 | * If @a aWait is @c true then this method will perform the operation on the
|
---|
5354 | * calling thread and will not return to the caller until the operation is
|
---|
5355 | * completed. When this method succeeds, all intermediate medium objects in
|
---|
5356 | * the chain will be uninitialized, the state of the target medium (and all
|
---|
5357 | * involved extra media) will be restored. @a aMediumLockList will not be
|
---|
5358 | * deleted, whether the operation is successful or not. The caller has to do
|
---|
5359 | * this if appropriate. Note that this (source) medium is not uninitialized
|
---|
5360 | * because of possible AutoCaller instances held by the caller of this method
|
---|
5361 | * on the current thread. It's therefore the responsibility of the caller to
|
---|
5362 | * call Medium::uninit() after releasing all callers.
|
---|
5363 | *
|
---|
5364 | * If @a aWait is @c false then this method will create a thread to perform the
|
---|
5365 | * operation asynchronously and will return immediately. If the operation
|
---|
5366 | * succeeds, the thread will uninitialize the source medium object and all
|
---|
5367 | * intermediate medium objects in the chain, reset the state of the target
|
---|
5368 | * medium (and all involved extra media) and delete @a aMediumLockList.
|
---|
5369 | * If the operation fails, the thread will only reset the states of all
|
---|
5370 | * involved media and delete @a aMediumLockList.
|
---|
5371 | *
|
---|
5372 | * When this method fails (regardless of the @a aWait mode), it is a caller's
|
---|
5373 | * responsibility to undo state changes and delete @a aMediumLockList using
|
---|
5374 | * #cancelMergeTo().
|
---|
5375 | *
|
---|
5376 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
5377 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
5378 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
5379 | * progress object is created/used at all. Note that @a aProgress cannot be
|
---|
5380 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
5381 | *
|
---|
5382 | * @param pTarget Target medium.
|
---|
5383 | * @param fMergeForward Merge direction.
|
---|
5384 | * @param pParentForTarget New parent for target medium after merge.
|
---|
5385 | * @param aChildrenToReparent List of children of the source which will have
|
---|
5386 | * to be reparented to the target after merge.
|
---|
5387 | * @param aMediumLockList Medium locking information.
|
---|
5388 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
5389 | * completion.
|
---|
5390 | * @param aWait @c true if this method should block instead of creating
|
---|
5391 | * an asynchronous thread.
|
---|
5392 | *
|
---|
5393 | * @note Locks the tree lock for writing. Locks the media from the chain
|
---|
5394 | * for writing.
|
---|
5395 | */
|
---|
5396 | HRESULT Medium::i_mergeTo(const ComObjPtr<Medium> &pTarget,
|
---|
5397 | bool fMergeForward,
|
---|
5398 | const ComObjPtr<Medium> &pParentForTarget,
|
---|
5399 | MediumLockList *aChildrenToReparent,
|
---|
5400 | MediumLockList *aMediumLockList,
|
---|
5401 | ComObjPtr<Progress> *aProgress,
|
---|
5402 | bool aWait)
|
---|
5403 | {
|
---|
5404 | AssertReturn(pTarget != NULL, E_FAIL);
|
---|
5405 | AssertReturn(pTarget != this, E_FAIL);
|
---|
5406 | AssertReturn(aMediumLockList != NULL, E_FAIL);
|
---|
5407 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
5408 |
|
---|
5409 | AutoCaller autoCaller(this);
|
---|
5410 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
5411 |
|
---|
5412 | AutoCaller targetCaller(pTarget);
|
---|
5413 | AssertComRCReturnRC(targetCaller.rc());
|
---|
5414 |
|
---|
5415 | HRESULT rc = S_OK;
|
---|
5416 | ComObjPtr<Progress> pProgress;
|
---|
5417 | Medium::Task *pTask = NULL;
|
---|
5418 |
|
---|
5419 | try
|
---|
5420 | {
|
---|
5421 | if (aProgress != NULL)
|
---|
5422 | {
|
---|
5423 | /* use the existing progress object... */
|
---|
5424 | pProgress = *aProgress;
|
---|
5425 |
|
---|
5426 | /* ...but create a new one if it is null */
|
---|
5427 | if (pProgress.isNull())
|
---|
5428 | {
|
---|
5429 | Utf8Str tgtName;
|
---|
5430 | {
|
---|
5431 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5432 | tgtName = pTarget->i_getName();
|
---|
5433 | }
|
---|
5434 |
|
---|
5435 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5436 |
|
---|
5437 | pProgress.createObject();
|
---|
5438 | rc = pProgress->init(m->pVirtualBox,
|
---|
5439 | static_cast<IMedium*>(this),
|
---|
5440 | BstrFmt(tr("Merging medium '%s' to '%s'"),
|
---|
5441 | i_getName().c_str(),
|
---|
5442 | tgtName.c_str()).raw(),
|
---|
5443 | TRUE /* aCancelable */);
|
---|
5444 | if (FAILED(rc))
|
---|
5445 | throw rc;
|
---|
5446 | }
|
---|
5447 | }
|
---|
5448 |
|
---|
5449 | /* setup task object to carry out the operation sync/async */
|
---|
5450 | pTask = new Medium::MergeTask(this, pTarget, fMergeForward,
|
---|
5451 | pParentForTarget, aChildrenToReparent,
|
---|
5452 | pProgress, aMediumLockList,
|
---|
5453 | aWait /* fKeepMediumLockList */);
|
---|
5454 | rc = pTask->rc();
|
---|
5455 | AssertComRC(rc);
|
---|
5456 | if (FAILED(rc))
|
---|
5457 | throw rc;
|
---|
5458 | }
|
---|
5459 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5460 |
|
---|
5461 | if (SUCCEEDED(rc))
|
---|
5462 | {
|
---|
5463 | if (aWait)
|
---|
5464 | rc = i_runNow(pTask);
|
---|
5465 | else
|
---|
5466 | rc = i_startThread(pTask);
|
---|
5467 |
|
---|
5468 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
5469 | *aProgress = pProgress;
|
---|
5470 | }
|
---|
5471 | else if (pTask != NULL)
|
---|
5472 | delete pTask;
|
---|
5473 |
|
---|
5474 | return rc;
|
---|
5475 | }
|
---|
5476 |
|
---|
5477 | /**
|
---|
5478 | * Undoes what #prepareMergeTo() did. Must be called if #mergeTo() is not
|
---|
5479 | * called or fails. Frees memory occupied by @a aMediumLockList and unlocks
|
---|
5480 | * the medium objects in @a aChildrenToReparent.
|
---|
5481 | *
|
---|
5482 | * @param aChildrenToReparent List of children of the source which will have
|
---|
5483 | * to be reparented to the target after merge.
|
---|
5484 | * @param aMediumLockList Medium locking information.
|
---|
5485 | *
|
---|
5486 | * @note Locks the media from the chain for writing.
|
---|
5487 | */
|
---|
5488 | void Medium::i_cancelMergeTo(MediumLockList *aChildrenToReparent,
|
---|
5489 | MediumLockList *aMediumLockList)
|
---|
5490 | {
|
---|
5491 | AutoCaller autoCaller(this);
|
---|
5492 | AssertComRCReturnVoid(autoCaller.rc());
|
---|
5493 |
|
---|
5494 | AssertReturnVoid(aMediumLockList != NULL);
|
---|
5495 |
|
---|
5496 | /* Revert media marked for deletion to previous state. */
|
---|
5497 | HRESULT rc;
|
---|
5498 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
5499 | aMediumLockList->GetBegin();
|
---|
5500 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
5501 | aMediumLockList->GetEnd();
|
---|
5502 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
5503 | it != mediumListEnd;
|
---|
5504 | ++it)
|
---|
5505 | {
|
---|
5506 | const MediumLock &mediumLock = *it;
|
---|
5507 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
5508 | AutoWriteLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
5509 |
|
---|
5510 | if (pMedium->m->state == MediumState_Deleting)
|
---|
5511 | {
|
---|
5512 | rc = pMedium->i_unmarkForDeletion();
|
---|
5513 | AssertComRC(rc);
|
---|
5514 | }
|
---|
5515 | }
|
---|
5516 |
|
---|
5517 | /* the destructor will do the work */
|
---|
5518 | delete aMediumLockList;
|
---|
5519 |
|
---|
5520 | /* unlock the children which had to be reparented, the destructor will do
|
---|
5521 | * the work */
|
---|
5522 | if (aChildrenToReparent)
|
---|
5523 | delete aChildrenToReparent;
|
---|
5524 | }
|
---|
5525 |
|
---|
5526 | /**
|
---|
5527 | * Fix the parent UUID of all children to point to this medium as their
|
---|
5528 | * parent.
|
---|
5529 | */
|
---|
5530 | HRESULT Medium::i_fixParentUuidOfChildren(MediumLockList *pChildrenToReparent)
|
---|
5531 | {
|
---|
5532 | Assert(!isWriteLockOnCurrentThread());
|
---|
5533 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
5534 | MediumLockList mediumLockList;
|
---|
5535 | HRESULT rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5536 | false /* fMediumLockWrite */,
|
---|
5537 | false /* fMediumLockWriteAll */,
|
---|
5538 | this,
|
---|
5539 | mediumLockList);
|
---|
5540 | AssertComRCReturnRC(rc);
|
---|
5541 |
|
---|
5542 | try
|
---|
5543 | {
|
---|
5544 | PVBOXHDD hdd;
|
---|
5545 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
5546 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
5547 |
|
---|
5548 | try
|
---|
5549 | {
|
---|
5550 | MediumLockList::Base::iterator lockListBegin =
|
---|
5551 | mediumLockList.GetBegin();
|
---|
5552 | MediumLockList::Base::iterator lockListEnd =
|
---|
5553 | mediumLockList.GetEnd();
|
---|
5554 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
5555 | it != lockListEnd;
|
---|
5556 | ++it)
|
---|
5557 | {
|
---|
5558 | MediumLock &mediumLock = *it;
|
---|
5559 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
5560 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
5561 |
|
---|
5562 | // open the medium
|
---|
5563 | vrc = VDOpen(hdd,
|
---|
5564 | pMedium->m->strFormat.c_str(),
|
---|
5565 | pMedium->m->strLocationFull.c_str(),
|
---|
5566 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
5567 | pMedium->m->vdImageIfaces);
|
---|
5568 | if (RT_FAILURE(vrc))
|
---|
5569 | throw vrc;
|
---|
5570 | }
|
---|
5571 |
|
---|
5572 | MediumLockList::Base::iterator childrenBegin = pChildrenToReparent->GetBegin();
|
---|
5573 | MediumLockList::Base::iterator childrenEnd = pChildrenToReparent->GetEnd();
|
---|
5574 | for (MediumLockList::Base::iterator it = childrenBegin;
|
---|
5575 | it != childrenEnd;
|
---|
5576 | ++it)
|
---|
5577 | {
|
---|
5578 | Medium *pMedium = it->GetMedium();
|
---|
5579 | /* VD_OPEN_FLAGS_INFO since UUID is wrong yet */
|
---|
5580 | vrc = VDOpen(hdd,
|
---|
5581 | pMedium->m->strFormat.c_str(),
|
---|
5582 | pMedium->m->strLocationFull.c_str(),
|
---|
5583 | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
5584 | pMedium->m->vdImageIfaces);
|
---|
5585 | if (RT_FAILURE(vrc))
|
---|
5586 | throw vrc;
|
---|
5587 |
|
---|
5588 | vrc = VDSetParentUuid(hdd, VD_LAST_IMAGE, m->id.raw());
|
---|
5589 | if (RT_FAILURE(vrc))
|
---|
5590 | throw vrc;
|
---|
5591 |
|
---|
5592 | vrc = VDClose(hdd, false /* fDelete */);
|
---|
5593 | if (RT_FAILURE(vrc))
|
---|
5594 | throw vrc;
|
---|
5595 | }
|
---|
5596 | }
|
---|
5597 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5598 | catch (int aVRC)
|
---|
5599 | {
|
---|
5600 | rc = setError(E_FAIL,
|
---|
5601 | tr("Could not update medium UUID references to parent '%s' (%s)"),
|
---|
5602 | m->strLocationFull.c_str(),
|
---|
5603 | i_vdError(aVRC).c_str());
|
---|
5604 | }
|
---|
5605 |
|
---|
5606 | VDDestroy(hdd);
|
---|
5607 | }
|
---|
5608 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5609 |
|
---|
5610 | return rc;
|
---|
5611 | }
|
---|
5612 |
|
---|
5613 | /**
|
---|
5614 | * Used by IAppliance to export disk images.
|
---|
5615 | *
|
---|
5616 | * @param aFilename Filename to create (UTF8).
|
---|
5617 | * @param aFormat Medium format for creating @a aFilename.
|
---|
5618 | * @param aVariant Which exact image format variant to use
|
---|
5619 | * for the destination image.
|
---|
5620 | * @param pKeyStore The optional key store for decrypting the data
|
---|
5621 | * for encrypted media during the export.
|
---|
5622 | * @param aVDImageIOCallbacks Pointer to the callback table for a
|
---|
5623 | * VDINTERFACEIO interface. May be NULL.
|
---|
5624 | * @param aVDImageIOUser Opaque data for the callbacks.
|
---|
5625 | * @param aProgress Progress object to use.
|
---|
5626 | * @return
|
---|
5627 | * @note The source format is defined by the Medium instance.
|
---|
5628 | */
|
---|
5629 | HRESULT Medium::i_exportFile(const char *aFilename,
|
---|
5630 | const ComObjPtr<MediumFormat> &aFormat,
|
---|
5631 | MediumVariant_T aVariant,
|
---|
5632 | SecretKeyStore *pKeyStore,
|
---|
5633 | PVDINTERFACEIO aVDImageIOIf, void *aVDImageIOUser,
|
---|
5634 | const ComObjPtr<Progress> &aProgress)
|
---|
5635 | {
|
---|
5636 | AssertPtrReturn(aFilename, E_INVALIDARG);
|
---|
5637 | AssertReturn(!aFormat.isNull(), E_INVALIDARG);
|
---|
5638 | AssertReturn(!aProgress.isNull(), E_INVALIDARG);
|
---|
5639 |
|
---|
5640 | AutoCaller autoCaller(this);
|
---|
5641 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
5642 |
|
---|
5643 | HRESULT rc = S_OK;
|
---|
5644 | Medium::Task *pTask = NULL;
|
---|
5645 |
|
---|
5646 | try
|
---|
5647 | {
|
---|
5648 | // This needs no extra locks besides what is done in the called methods.
|
---|
5649 |
|
---|
5650 | /* Build the source lock list. */
|
---|
5651 | MediumLockList *pSourceMediumLockList(new MediumLockList());
|
---|
5652 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5653 | false /* fMediumLockWrite */,
|
---|
5654 | false /* fMediumLockWriteAll */,
|
---|
5655 | NULL,
|
---|
5656 | *pSourceMediumLockList);
|
---|
5657 | if (FAILED(rc))
|
---|
5658 | {
|
---|
5659 | delete pSourceMediumLockList;
|
---|
5660 | throw rc;
|
---|
5661 | }
|
---|
5662 |
|
---|
5663 | rc = pSourceMediumLockList->Lock();
|
---|
5664 | if (FAILED(rc))
|
---|
5665 | {
|
---|
5666 | delete pSourceMediumLockList;
|
---|
5667 | throw setError(rc,
|
---|
5668 | tr("Failed to lock source media '%s'"),
|
---|
5669 | i_getLocationFull().c_str());
|
---|
5670 | }
|
---|
5671 |
|
---|
5672 | /* setup task object to carry out the operation asynchronously */
|
---|
5673 | pTask = new Medium::ExportTask(this, aProgress, aFilename, aFormat,
|
---|
5674 | aVariant, pKeyStore, aVDImageIOIf,
|
---|
5675 | aVDImageIOUser, pSourceMediumLockList);
|
---|
5676 | rc = pTask->rc();
|
---|
5677 | AssertComRC(rc);
|
---|
5678 | if (FAILED(rc))
|
---|
5679 | throw rc;
|
---|
5680 | }
|
---|
5681 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5682 |
|
---|
5683 | if (SUCCEEDED(rc))
|
---|
5684 | rc = i_startThread(pTask);
|
---|
5685 | else if (pTask != NULL)
|
---|
5686 | delete pTask;
|
---|
5687 |
|
---|
5688 | return rc;
|
---|
5689 | }
|
---|
5690 |
|
---|
5691 | /**
|
---|
5692 | * Used by IAppliance to import disk images.
|
---|
5693 | *
|
---|
5694 | * @param aFilename Filename to read (UTF8).
|
---|
5695 | * @param aFormat Medium format for reading @a aFilename.
|
---|
5696 | * @param aVariant Which exact image format variant to use
|
---|
5697 | * for the destination image.
|
---|
5698 | * @param aVDImageIOCallbacks Pointer to the callback table for a
|
---|
5699 | * VDINTERFACEIO interface. May be NULL.
|
---|
5700 | * @param aVDImageIOUser Opaque data for the callbacks.
|
---|
5701 | * @param aParent Parent medium. May be NULL.
|
---|
5702 | * @param aProgress Progress object to use.
|
---|
5703 | * @return
|
---|
5704 | * @note The destination format is defined by the Medium instance.
|
---|
5705 | */
|
---|
5706 | HRESULT Medium::i_importFile(const char *aFilename,
|
---|
5707 | const ComObjPtr<MediumFormat> &aFormat,
|
---|
5708 | MediumVariant_T aVariant,
|
---|
5709 | PVDINTERFACEIO aVDImageIOIf, void *aVDImageIOUser,
|
---|
5710 | const ComObjPtr<Medium> &aParent,
|
---|
5711 | const ComObjPtr<Progress> &aProgress)
|
---|
5712 | {
|
---|
5713 | AssertPtrReturn(aFilename, E_INVALIDARG);
|
---|
5714 | AssertReturn(!aFormat.isNull(), E_INVALIDARG);
|
---|
5715 | AssertReturn(!aProgress.isNull(), E_INVALIDARG);
|
---|
5716 |
|
---|
5717 | AutoCaller autoCaller(this);
|
---|
5718 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
5719 |
|
---|
5720 | HRESULT rc = S_OK;
|
---|
5721 | Medium::Task *pTask = NULL;
|
---|
5722 |
|
---|
5723 | try
|
---|
5724 | {
|
---|
5725 | // locking: we need the tree lock first because we access parent pointers
|
---|
5726 | // and we need to write-lock the media involved
|
---|
5727 | uint32_t cHandles = 2;
|
---|
5728 | LockHandle* pHandles[3] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
5729 | this->lockHandle() };
|
---|
5730 | /* Only add parent to the lock if it is not null */
|
---|
5731 | if (!aParent.isNull())
|
---|
5732 | pHandles[cHandles++] = aParent->lockHandle();
|
---|
5733 | AutoWriteLock alock(cHandles,
|
---|
5734 | pHandles
|
---|
5735 | COMMA_LOCKVAL_SRC_POS);
|
---|
5736 |
|
---|
5737 | if ( m->state != MediumState_NotCreated
|
---|
5738 | && m->state != MediumState_Created)
|
---|
5739 | throw i_setStateError();
|
---|
5740 |
|
---|
5741 | /* Build the target lock list. */
|
---|
5742 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
5743 | alock.release();
|
---|
5744 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5745 | true /* fMediumLockWrite */,
|
---|
5746 | false /* fMediumLockWriteAll */,
|
---|
5747 | aParent,
|
---|
5748 | *pTargetMediumLockList);
|
---|
5749 | alock.acquire();
|
---|
5750 | if (FAILED(rc))
|
---|
5751 | {
|
---|
5752 | delete pTargetMediumLockList;
|
---|
5753 | throw rc;
|
---|
5754 | }
|
---|
5755 |
|
---|
5756 | alock.release();
|
---|
5757 | rc = pTargetMediumLockList->Lock();
|
---|
5758 | alock.acquire();
|
---|
5759 | if (FAILED(rc))
|
---|
5760 | {
|
---|
5761 | delete pTargetMediumLockList;
|
---|
5762 | throw setError(rc,
|
---|
5763 | tr("Failed to lock target media '%s'"),
|
---|
5764 | i_getLocationFull().c_str());
|
---|
5765 | }
|
---|
5766 |
|
---|
5767 | /* setup task object to carry out the operation asynchronously */
|
---|
5768 | pTask = new Medium::ImportTask(this, aProgress, aFilename, aFormat,
|
---|
5769 | aVariant, aVDImageIOIf,
|
---|
5770 | aVDImageIOUser, aParent,
|
---|
5771 | pTargetMediumLockList);
|
---|
5772 | rc = pTask->rc();
|
---|
5773 | AssertComRC(rc);
|
---|
5774 | if (FAILED(rc))
|
---|
5775 | throw rc;
|
---|
5776 |
|
---|
5777 | if (m->state == MediumState_NotCreated)
|
---|
5778 | m->state = MediumState_Creating;
|
---|
5779 | }
|
---|
5780 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5781 |
|
---|
5782 | if (SUCCEEDED(rc))
|
---|
5783 | rc = i_startThread(pTask);
|
---|
5784 | else if (pTask != NULL)
|
---|
5785 | delete pTask;
|
---|
5786 |
|
---|
5787 | return rc;
|
---|
5788 | }
|
---|
5789 |
|
---|
5790 | /**
|
---|
5791 | * Internal version of the public CloneTo API which allows to enable certain
|
---|
5792 | * optimizations to improve speed during VM cloning.
|
---|
5793 | *
|
---|
5794 | * @param aTarget Target medium
|
---|
5795 | * @param aVariant Which exact image format variant to use
|
---|
5796 | * for the destination image.
|
---|
5797 | * @param aParent Parent medium. May be NULL.
|
---|
5798 | * @param aProgress Progress object to use.
|
---|
5799 | * @param idxSrcImageSame The last image in the source chain which has the
|
---|
5800 | * same content as the given image in the destination
|
---|
5801 | * chain. Use UINT32_MAX to disable this optimization.
|
---|
5802 | * @param idxDstImageSame The last image in the destination chain which has the
|
---|
5803 | * same content as the given image in the source chain.
|
---|
5804 | * Use UINT32_MAX to disable this optimization.
|
---|
5805 | * @return
|
---|
5806 | */
|
---|
5807 | HRESULT Medium::i_cloneToEx(const ComObjPtr<Medium> &aTarget, ULONG aVariant,
|
---|
5808 | const ComObjPtr<Medium> &aParent, IProgress **aProgress,
|
---|
5809 | uint32_t idxSrcImageSame, uint32_t idxDstImageSame)
|
---|
5810 | {
|
---|
5811 | CheckComArgNotNull(aTarget);
|
---|
5812 | CheckComArgOutPointerValid(aProgress);
|
---|
5813 | ComAssertRet(aTarget != this, E_INVALIDARG);
|
---|
5814 |
|
---|
5815 | AutoCaller autoCaller(this);
|
---|
5816 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
5817 |
|
---|
5818 | HRESULT rc = S_OK;
|
---|
5819 | ComObjPtr<Progress> pProgress;
|
---|
5820 | Medium::Task *pTask = NULL;
|
---|
5821 |
|
---|
5822 | try
|
---|
5823 | {
|
---|
5824 | // locking: we need the tree lock first because we access parent pointers
|
---|
5825 | // and we need to write-lock the media involved
|
---|
5826 | uint32_t cHandles = 3;
|
---|
5827 | LockHandle* pHandles[4] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
5828 | this->lockHandle(),
|
---|
5829 | aTarget->lockHandle() };
|
---|
5830 | /* Only add parent to the lock if it is not null */
|
---|
5831 | if (!aParent.isNull())
|
---|
5832 | pHandles[cHandles++] = aParent->lockHandle();
|
---|
5833 | AutoWriteLock alock(cHandles,
|
---|
5834 | pHandles
|
---|
5835 | COMMA_LOCKVAL_SRC_POS);
|
---|
5836 |
|
---|
5837 | if ( aTarget->m->state != MediumState_NotCreated
|
---|
5838 | && aTarget->m->state != MediumState_Created)
|
---|
5839 | throw aTarget->i_setStateError();
|
---|
5840 |
|
---|
5841 | /* Build the source lock list. */
|
---|
5842 | MediumLockList *pSourceMediumLockList(new MediumLockList());
|
---|
5843 | alock.release();
|
---|
5844 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5845 | false /* fMediumLockWrite */,
|
---|
5846 | false /* fMediumLockWriteAll */,
|
---|
5847 | NULL,
|
---|
5848 | *pSourceMediumLockList);
|
---|
5849 | alock.acquire();
|
---|
5850 | if (FAILED(rc))
|
---|
5851 | {
|
---|
5852 | delete pSourceMediumLockList;
|
---|
5853 | throw rc;
|
---|
5854 | }
|
---|
5855 |
|
---|
5856 | /* Build the target lock list (including the to-be parent chain). */
|
---|
5857 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
5858 | alock.release();
|
---|
5859 | rc = aTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5860 | true /* fMediumLockWrite */,
|
---|
5861 | false /* fMediumLockWriteAll */,
|
---|
5862 | aParent,
|
---|
5863 | *pTargetMediumLockList);
|
---|
5864 | alock.acquire();
|
---|
5865 | if (FAILED(rc))
|
---|
5866 | {
|
---|
5867 | delete pSourceMediumLockList;
|
---|
5868 | delete pTargetMediumLockList;
|
---|
5869 | throw rc;
|
---|
5870 | }
|
---|
5871 |
|
---|
5872 | alock.release();
|
---|
5873 | rc = pSourceMediumLockList->Lock();
|
---|
5874 | alock.acquire();
|
---|
5875 | if (FAILED(rc))
|
---|
5876 | {
|
---|
5877 | delete pSourceMediumLockList;
|
---|
5878 | delete pTargetMediumLockList;
|
---|
5879 | throw setError(rc,
|
---|
5880 | tr("Failed to lock source media '%s'"),
|
---|
5881 | i_getLocationFull().c_str());
|
---|
5882 | }
|
---|
5883 | alock.release();
|
---|
5884 | rc = pTargetMediumLockList->Lock();
|
---|
5885 | alock.acquire();
|
---|
5886 | if (FAILED(rc))
|
---|
5887 | {
|
---|
5888 | delete pSourceMediumLockList;
|
---|
5889 | delete pTargetMediumLockList;
|
---|
5890 | throw setError(rc,
|
---|
5891 | tr("Failed to lock target media '%s'"),
|
---|
5892 | aTarget->i_getLocationFull().c_str());
|
---|
5893 | }
|
---|
5894 |
|
---|
5895 | pProgress.createObject();
|
---|
5896 | rc = pProgress->init(m->pVirtualBox,
|
---|
5897 | static_cast <IMedium *>(this),
|
---|
5898 | BstrFmt(tr("Creating clone medium '%s'"), aTarget->m->strLocationFull.c_str()).raw(),
|
---|
5899 | TRUE /* aCancelable */);
|
---|
5900 | if (FAILED(rc))
|
---|
5901 | {
|
---|
5902 | delete pSourceMediumLockList;
|
---|
5903 | delete pTargetMediumLockList;
|
---|
5904 | throw rc;
|
---|
5905 | }
|
---|
5906 |
|
---|
5907 | /* setup task object to carry out the operation asynchronously */
|
---|
5908 | pTask = new Medium::CloneTask(this, pProgress, aTarget,
|
---|
5909 | (MediumVariant_T)aVariant,
|
---|
5910 | aParent, idxSrcImageSame,
|
---|
5911 | idxDstImageSame, pSourceMediumLockList,
|
---|
5912 | pTargetMediumLockList);
|
---|
5913 | rc = pTask->rc();
|
---|
5914 | AssertComRC(rc);
|
---|
5915 | if (FAILED(rc))
|
---|
5916 | throw rc;
|
---|
5917 |
|
---|
5918 | if (aTarget->m->state == MediumState_NotCreated)
|
---|
5919 | aTarget->m->state = MediumState_Creating;
|
---|
5920 | }
|
---|
5921 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5922 |
|
---|
5923 | if (SUCCEEDED(rc))
|
---|
5924 | {
|
---|
5925 | rc = i_startThread(pTask);
|
---|
5926 |
|
---|
5927 | if (SUCCEEDED(rc))
|
---|
5928 | pProgress.queryInterfaceTo(aProgress);
|
---|
5929 | }
|
---|
5930 | else if (pTask != NULL)
|
---|
5931 | delete pTask;
|
---|
5932 |
|
---|
5933 | return rc;
|
---|
5934 | }
|
---|
5935 |
|
---|
5936 | /**
|
---|
5937 | * Returns the key identifier for this medium if encryption is configured.
|
---|
5938 | *
|
---|
5939 | * @returns Key identifier or empty string if no encryption is configured.
|
---|
5940 | */
|
---|
5941 | const Utf8Str& Medium::i_getKeyId()
|
---|
5942 | {
|
---|
5943 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
5944 |
|
---|
5945 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5946 |
|
---|
5947 | settings::StringsMap::const_iterator it = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
5948 | if (it == pBase->m->mapProperties.end())
|
---|
5949 | return Utf8Str::Empty;
|
---|
5950 |
|
---|
5951 | return it->second;
|
---|
5952 | }
|
---|
5953 |
|
---|
5954 | /**
|
---|
5955 | * Returns all filter related properties.
|
---|
5956 | *
|
---|
5957 | * @returns COM status code.
|
---|
5958 | * @param aReturnNames Where to store the properties names on success.
|
---|
5959 | * @param aReturnValues Where to store the properties values on success.
|
---|
5960 | */
|
---|
5961 | HRESULT Medium::i_getFilterProperties(std::vector<com::Utf8Str> &aReturnNames,
|
---|
5962 | std::vector<com::Utf8Str> &aReturnValues)
|
---|
5963 | {
|
---|
5964 | std::vector<com::Utf8Str> aPropNames;
|
---|
5965 | std::vector<com::Utf8Str> aPropValues;
|
---|
5966 | HRESULT hrc = getProperties(Utf8Str(""), aPropNames, aPropValues);
|
---|
5967 |
|
---|
5968 | if (SUCCEEDED(hrc))
|
---|
5969 | {
|
---|
5970 | unsigned cReturnSize = 0;
|
---|
5971 | aReturnNames.resize(0);
|
---|
5972 | aReturnValues.resize(0);
|
---|
5973 | for (unsigned idx = 0; idx < aPropNames.size(); idx++)
|
---|
5974 | {
|
---|
5975 | if (i_isPropertyForFilter(aPropNames[idx]))
|
---|
5976 | {
|
---|
5977 | aReturnNames.resize(cReturnSize + 1);
|
---|
5978 | aReturnValues.resize(cReturnSize + 1);
|
---|
5979 | aReturnNames[cReturnSize] = aPropNames[idx];
|
---|
5980 | aReturnValues[cReturnSize] = aPropValues[idx];
|
---|
5981 | cReturnSize++;
|
---|
5982 | }
|
---|
5983 | }
|
---|
5984 | }
|
---|
5985 |
|
---|
5986 | return hrc;
|
---|
5987 | }
|
---|
5988 |
|
---|
5989 | ////////////////////////////////////////////////////////////////////////////////
|
---|
5990 | //
|
---|
5991 | // Private methods
|
---|
5992 | //
|
---|
5993 | ////////////////////////////////////////////////////////////////////////////////
|
---|
5994 |
|
---|
5995 | /**
|
---|
5996 | * Queries information from the medium.
|
---|
5997 | *
|
---|
5998 | * As a result of this call, the accessibility state and data members such as
|
---|
5999 | * size and description will be updated with the current information.
|
---|
6000 | *
|
---|
6001 | * @note This method may block during a system I/O call that checks storage
|
---|
6002 | * accessibility.
|
---|
6003 | *
|
---|
6004 | * @note Caller MUST NOT hold the media tree or medium lock.
|
---|
6005 | *
|
---|
6006 | * @note Locks mParent for reading. Locks this object for writing.
|
---|
6007 | *
|
---|
6008 | * @param fSetImageId Whether to reset the UUID contained in the image file to the UUID in the medium instance data (see SetIDs())
|
---|
6009 | * @param fSetParentId Whether to reset the parent UUID contained in the image file to the parent
|
---|
6010 | * UUID in the medium instance data (see SetIDs())
|
---|
6011 | * @return
|
---|
6012 | */
|
---|
6013 | HRESULT Medium::i_queryInfo(bool fSetImageId, bool fSetParentId, AutoCaller &autoCaller)
|
---|
6014 | {
|
---|
6015 | Assert(!isWriteLockOnCurrentThread());
|
---|
6016 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6017 |
|
---|
6018 | if ( ( m->state != MediumState_Created
|
---|
6019 | && m->state != MediumState_Inaccessible
|
---|
6020 | && m->state != MediumState_LockedRead)
|
---|
6021 | || m->fClosing)
|
---|
6022 | return E_FAIL;
|
---|
6023 |
|
---|
6024 | HRESULT rc = S_OK;
|
---|
6025 |
|
---|
6026 | int vrc = VINF_SUCCESS;
|
---|
6027 |
|
---|
6028 | /* check if a blocking i_queryInfo() call is in progress on some other thread,
|
---|
6029 | * and wait for it to finish if so instead of querying data ourselves */
|
---|
6030 | if (m->queryInfoRunning)
|
---|
6031 | {
|
---|
6032 | Assert( m->state == MediumState_LockedRead
|
---|
6033 | || m->state == MediumState_LockedWrite);
|
---|
6034 |
|
---|
6035 | while (m->queryInfoRunning)
|
---|
6036 | {
|
---|
6037 | alock.release();
|
---|
6038 | /* must not hold the object lock now */
|
---|
6039 | Assert(!isWriteLockOnCurrentThread());
|
---|
6040 | {
|
---|
6041 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
6042 | }
|
---|
6043 | alock.acquire();
|
---|
6044 | }
|
---|
6045 |
|
---|
6046 | return S_OK;
|
---|
6047 | }
|
---|
6048 |
|
---|
6049 | bool success = false;
|
---|
6050 | Utf8Str lastAccessError;
|
---|
6051 |
|
---|
6052 | /* are we dealing with a new medium constructed using the existing
|
---|
6053 | * location? */
|
---|
6054 | bool isImport = m->id.isZero();
|
---|
6055 | unsigned uOpenFlags = VD_OPEN_FLAGS_INFO;
|
---|
6056 |
|
---|
6057 | /* Note that we don't use VD_OPEN_FLAGS_READONLY when opening new
|
---|
6058 | * media because that would prevent necessary modifications
|
---|
6059 | * when opening media of some third-party formats for the first
|
---|
6060 | * time in VirtualBox (such as VMDK for which VDOpen() needs to
|
---|
6061 | * generate an UUID if it is missing) */
|
---|
6062 | if ( m->hddOpenMode == OpenReadOnly
|
---|
6063 | || m->type == MediumType_Readonly
|
---|
6064 | || (!isImport && !fSetImageId && !fSetParentId)
|
---|
6065 | )
|
---|
6066 | uOpenFlags |= VD_OPEN_FLAGS_READONLY;
|
---|
6067 |
|
---|
6068 | /* Open shareable medium with the appropriate flags */
|
---|
6069 | if (m->type == MediumType_Shareable)
|
---|
6070 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
6071 |
|
---|
6072 | /* Lock the medium, which makes the behavior much more consistent, must be
|
---|
6073 | * done before dropping the object lock and setting queryInfoRunning. */
|
---|
6074 | ComPtr<IToken> pToken;
|
---|
6075 | if (uOpenFlags & (VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SHAREABLE))
|
---|
6076 | rc = LockRead(pToken.asOutParam());
|
---|
6077 | else
|
---|
6078 | rc = LockWrite(pToken.asOutParam());
|
---|
6079 | if (FAILED(rc)) return rc;
|
---|
6080 |
|
---|
6081 | /* Copies of the input state fields which are not read-only,
|
---|
6082 | * as we're dropping the lock. CAUTION: be extremely careful what
|
---|
6083 | * you do with the contents of this medium object, as you will
|
---|
6084 | * create races if there are concurrent changes. */
|
---|
6085 | Utf8Str format(m->strFormat);
|
---|
6086 | Utf8Str location(m->strLocationFull);
|
---|
6087 | ComObjPtr<MediumFormat> formatObj = m->formatObj;
|
---|
6088 |
|
---|
6089 | /* "Output" values which can't be set because the lock isn't held
|
---|
6090 | * at the time the values are determined. */
|
---|
6091 | Guid mediumId = m->id;
|
---|
6092 | uint64_t mediumSize = 0;
|
---|
6093 | uint64_t mediumLogicalSize = 0;
|
---|
6094 |
|
---|
6095 | /* Flag whether a base image has a non-zero parent UUID and thus
|
---|
6096 | * need repairing after it was closed again. */
|
---|
6097 | bool fRepairImageZeroParentUuid = false;
|
---|
6098 |
|
---|
6099 | ComObjPtr<VirtualBox> pVirtualBox = m->pVirtualBox;
|
---|
6100 |
|
---|
6101 | /* must be set before leaving the object lock the first time */
|
---|
6102 | m->queryInfoRunning = true;
|
---|
6103 |
|
---|
6104 | /* must leave object lock now, because a lock from a higher lock class
|
---|
6105 | * is needed and also a lengthy operation is coming */
|
---|
6106 | alock.release();
|
---|
6107 | autoCaller.release();
|
---|
6108 |
|
---|
6109 | /* Note that taking the queryInfoSem after leaving the object lock above
|
---|
6110 | * can lead to short spinning of the loops waiting for i_queryInfo() to
|
---|
6111 | * complete. This is unavoidable since the other order causes a lock order
|
---|
6112 | * violation: here it would be requesting the object lock (at the beginning
|
---|
6113 | * of the method), then queryInfoSem, and below the other way round. */
|
---|
6114 | AutoWriteLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
6115 |
|
---|
6116 | /* take the opportunity to have a media tree lock, released initially */
|
---|
6117 | Assert(!isWriteLockOnCurrentThread());
|
---|
6118 | Assert(!pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
6119 | AutoWriteLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
6120 | treeLock.release();
|
---|
6121 |
|
---|
6122 | /* re-take the caller, but not the object lock, to keep uninit away */
|
---|
6123 | autoCaller.add();
|
---|
6124 | if (FAILED(autoCaller.rc()))
|
---|
6125 | {
|
---|
6126 | m->queryInfoRunning = false;
|
---|
6127 | return autoCaller.rc();
|
---|
6128 | }
|
---|
6129 |
|
---|
6130 | try
|
---|
6131 | {
|
---|
6132 | /* skip accessibility checks for host drives */
|
---|
6133 | if (m->hostDrive)
|
---|
6134 | {
|
---|
6135 | success = true;
|
---|
6136 | throw S_OK;
|
---|
6137 | }
|
---|
6138 |
|
---|
6139 | PVBOXHDD hdd;
|
---|
6140 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
6141 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6142 |
|
---|
6143 | try
|
---|
6144 | {
|
---|
6145 | /** @todo This kind of opening of media is assuming that diff
|
---|
6146 | * media can be opened as base media. Should be documented that
|
---|
6147 | * it must work for all medium format backends. */
|
---|
6148 | vrc = VDOpen(hdd,
|
---|
6149 | format.c_str(),
|
---|
6150 | location.c_str(),
|
---|
6151 | uOpenFlags | m->uOpenFlagsDef,
|
---|
6152 | m->vdImageIfaces);
|
---|
6153 | if (RT_FAILURE(vrc))
|
---|
6154 | {
|
---|
6155 | lastAccessError = Utf8StrFmt(tr("Could not open the medium '%s'%s"),
|
---|
6156 | location.c_str(), i_vdError(vrc).c_str());
|
---|
6157 | throw S_OK;
|
---|
6158 | }
|
---|
6159 |
|
---|
6160 | if (formatObj->i_getCapabilities() & MediumFormatCapabilities_Uuid)
|
---|
6161 | {
|
---|
6162 | /* Modify the UUIDs if necessary. The associated fields are
|
---|
6163 | * not modified by other code, so no need to copy. */
|
---|
6164 | if (fSetImageId)
|
---|
6165 | {
|
---|
6166 | alock.acquire();
|
---|
6167 | vrc = VDSetUuid(hdd, 0, m->uuidImage.raw());
|
---|
6168 | alock.release();
|
---|
6169 | if (RT_FAILURE(vrc))
|
---|
6170 | {
|
---|
6171 | lastAccessError = Utf8StrFmt(tr("Could not update the UUID of medium '%s'%s"),
|
---|
6172 | location.c_str(), i_vdError(vrc).c_str());
|
---|
6173 | throw S_OK;
|
---|
6174 | }
|
---|
6175 | mediumId = m->uuidImage;
|
---|
6176 | }
|
---|
6177 | if (fSetParentId)
|
---|
6178 | {
|
---|
6179 | alock.acquire();
|
---|
6180 | vrc = VDSetParentUuid(hdd, 0, m->uuidParentImage.raw());
|
---|
6181 | alock.release();
|
---|
6182 | if (RT_FAILURE(vrc))
|
---|
6183 | {
|
---|
6184 | lastAccessError = Utf8StrFmt(tr("Could not update the parent UUID of medium '%s'%s"),
|
---|
6185 | location.c_str(), i_vdError(vrc).c_str());
|
---|
6186 | throw S_OK;
|
---|
6187 | }
|
---|
6188 | }
|
---|
6189 | /* zap the information, these are no long-term members */
|
---|
6190 | alock.acquire();
|
---|
6191 | unconst(m->uuidImage).clear();
|
---|
6192 | unconst(m->uuidParentImage).clear();
|
---|
6193 | alock.release();
|
---|
6194 |
|
---|
6195 | /* check the UUID */
|
---|
6196 | RTUUID uuid;
|
---|
6197 | vrc = VDGetUuid(hdd, 0, &uuid);
|
---|
6198 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6199 |
|
---|
6200 | if (isImport)
|
---|
6201 | {
|
---|
6202 | mediumId = uuid;
|
---|
6203 |
|
---|
6204 | if (mediumId.isZero() && (m->hddOpenMode == OpenReadOnly))
|
---|
6205 | // only when importing a VDMK that has no UUID, create one in memory
|
---|
6206 | mediumId.create();
|
---|
6207 | }
|
---|
6208 | else
|
---|
6209 | {
|
---|
6210 | Assert(!mediumId.isZero());
|
---|
6211 |
|
---|
6212 | if (mediumId != uuid)
|
---|
6213 | {
|
---|
6214 | /** @todo r=klaus this always refers to VirtualBox.xml as the medium registry, even for new VMs */
|
---|
6215 | lastAccessError = Utf8StrFmt(
|
---|
6216 | tr("UUID {%RTuuid} of the medium '%s' does not match the value {%RTuuid} stored in the media registry ('%s')"),
|
---|
6217 | &uuid,
|
---|
6218 | location.c_str(),
|
---|
6219 | mediumId.raw(),
|
---|
6220 | pVirtualBox->i_settingsFilePath().c_str());
|
---|
6221 | throw S_OK;
|
---|
6222 | }
|
---|
6223 | }
|
---|
6224 | }
|
---|
6225 | else
|
---|
6226 | {
|
---|
6227 | /* the backend does not support storing UUIDs within the
|
---|
6228 | * underlying storage so use what we store in XML */
|
---|
6229 |
|
---|
6230 | if (fSetImageId)
|
---|
6231 | {
|
---|
6232 | /* set the UUID if an API client wants to change it */
|
---|
6233 | alock.acquire();
|
---|
6234 | mediumId = m->uuidImage;
|
---|
6235 | alock.release();
|
---|
6236 | }
|
---|
6237 | else if (isImport)
|
---|
6238 | {
|
---|
6239 | /* generate an UUID for an imported UUID-less medium */
|
---|
6240 | mediumId.create();
|
---|
6241 | }
|
---|
6242 | }
|
---|
6243 |
|
---|
6244 | /* set the image uuid before the below parent uuid handling code
|
---|
6245 | * might place it somewhere in the media tree, so that the medium
|
---|
6246 | * UUID is valid at this point */
|
---|
6247 | alock.acquire();
|
---|
6248 | if (isImport || fSetImageId)
|
---|
6249 | unconst(m->id) = mediumId;
|
---|
6250 | alock.release();
|
---|
6251 |
|
---|
6252 | /* get the medium variant */
|
---|
6253 | unsigned uImageFlags;
|
---|
6254 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
6255 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6256 | alock.acquire();
|
---|
6257 | m->variant = (MediumVariant_T)uImageFlags;
|
---|
6258 | alock.release();
|
---|
6259 |
|
---|
6260 | /* check/get the parent uuid and update corresponding state */
|
---|
6261 | if (uImageFlags & VD_IMAGE_FLAGS_DIFF)
|
---|
6262 | {
|
---|
6263 | RTUUID parentId;
|
---|
6264 | vrc = VDGetParentUuid(hdd, 0, &parentId);
|
---|
6265 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6266 |
|
---|
6267 | /* streamOptimized VMDK images are only accepted as base
|
---|
6268 | * images, as this allows automatic repair of OVF appliances.
|
---|
6269 | * Since such images don't support random writes they will not
|
---|
6270 | * be created for diff images. Only an overly smart user might
|
---|
6271 | * manually create this case. Too bad for him. */
|
---|
6272 | if ( (isImport || fSetParentId)
|
---|
6273 | && !(uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
|
---|
6274 | {
|
---|
6275 | /* the parent must be known to us. Note that we freely
|
---|
6276 | * call locking methods of mVirtualBox and parent, as all
|
---|
6277 | * relevant locks must be already held. There may be no
|
---|
6278 | * concurrent access to the just opened medium on other
|
---|
6279 | * threads yet (and init() will fail if this method reports
|
---|
6280 | * MediumState_Inaccessible) */
|
---|
6281 |
|
---|
6282 | ComObjPtr<Medium> pParent;
|
---|
6283 | if (RTUuidIsNull(&parentId))
|
---|
6284 | rc = VBOX_E_OBJECT_NOT_FOUND;
|
---|
6285 | else
|
---|
6286 | rc = pVirtualBox->i_findHardDiskById(Guid(parentId), false /* aSetError */, &pParent);
|
---|
6287 | if (FAILED(rc))
|
---|
6288 | {
|
---|
6289 | if (fSetImageId && !fSetParentId)
|
---|
6290 | {
|
---|
6291 | /* If the image UUID gets changed for an existing
|
---|
6292 | * image then the parent UUID can be stale. In such
|
---|
6293 | * cases clear the parent information. The parent
|
---|
6294 | * information may/will be re-set later if the
|
---|
6295 | * API client wants to adjust a complete medium
|
---|
6296 | * hierarchy one by one. */
|
---|
6297 | rc = S_OK;
|
---|
6298 | alock.acquire();
|
---|
6299 | RTUuidClear(&parentId);
|
---|
6300 | vrc = VDSetParentUuid(hdd, 0, &parentId);
|
---|
6301 | alock.release();
|
---|
6302 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6303 | }
|
---|
6304 | else
|
---|
6305 | {
|
---|
6306 | lastAccessError = Utf8StrFmt(tr("Parent medium with UUID {%RTuuid} of the medium '%s' is not found in the media registry ('%s')"),
|
---|
6307 | &parentId, location.c_str(),
|
---|
6308 | pVirtualBox->i_settingsFilePath().c_str());
|
---|
6309 | throw S_OK;
|
---|
6310 | }
|
---|
6311 | }
|
---|
6312 |
|
---|
6313 | /* must drop the caller before taking the tree lock */
|
---|
6314 | autoCaller.release();
|
---|
6315 | /* we set mParent & children() */
|
---|
6316 | treeLock.acquire();
|
---|
6317 | autoCaller.add();
|
---|
6318 | if (FAILED(autoCaller.rc()))
|
---|
6319 | throw autoCaller.rc();
|
---|
6320 |
|
---|
6321 | if (m->pParent)
|
---|
6322 | i_deparent();
|
---|
6323 |
|
---|
6324 | if (!pParent.isNull())
|
---|
6325 | if (pParent->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
6326 | {
|
---|
6327 | AutoReadLock plock(pParent COMMA_LOCKVAL_SRC_POS);
|
---|
6328 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
6329 | 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"),
|
---|
6330 | pParent->m->strLocationFull.c_str());
|
---|
6331 | }
|
---|
6332 | i_setParent(pParent);
|
---|
6333 |
|
---|
6334 | treeLock.release();
|
---|
6335 | }
|
---|
6336 | else
|
---|
6337 | {
|
---|
6338 | /* must drop the caller before taking the tree lock */
|
---|
6339 | autoCaller.release();
|
---|
6340 | /* we access mParent */
|
---|
6341 | treeLock.acquire();
|
---|
6342 | autoCaller.add();
|
---|
6343 | if (FAILED(autoCaller.rc()))
|
---|
6344 | throw autoCaller.rc();
|
---|
6345 |
|
---|
6346 | /* check that parent UUIDs match. Note that there's no need
|
---|
6347 | * for the parent's AutoCaller (our lifetime is bound to
|
---|
6348 | * it) */
|
---|
6349 |
|
---|
6350 | if (m->pParent.isNull())
|
---|
6351 | {
|
---|
6352 | /* Due to a bug in VDCopy() in VirtualBox 3.0.0-3.0.14
|
---|
6353 | * and 3.1.0-3.1.8 there are base images out there
|
---|
6354 | * which have a non-zero parent UUID. No point in
|
---|
6355 | * complaining about them, instead automatically
|
---|
6356 | * repair the problem. Later we can bring back the
|
---|
6357 | * error message, but we should wait until really
|
---|
6358 | * most users have repaired their images, either with
|
---|
6359 | * VBoxFixHdd or this way. */
|
---|
6360 | #if 1
|
---|
6361 | fRepairImageZeroParentUuid = true;
|
---|
6362 | #else /* 0 */
|
---|
6363 | lastAccessError = Utf8StrFmt(
|
---|
6364 | tr("Medium type of '%s' is differencing but it is not associated with any parent medium in the media registry ('%s')"),
|
---|
6365 | location.c_str(),
|
---|
6366 | pVirtualBox->settingsFilePath().c_str());
|
---|
6367 | treeLock.release();
|
---|
6368 | throw S_OK;
|
---|
6369 | #endif /* 0 */
|
---|
6370 | }
|
---|
6371 |
|
---|
6372 | {
|
---|
6373 | autoCaller.release();
|
---|
6374 | AutoReadLock parentLock(m->pParent COMMA_LOCKVAL_SRC_POS);
|
---|
6375 | autoCaller.add();
|
---|
6376 | if (FAILED(autoCaller.rc()))
|
---|
6377 | throw autoCaller.rc();
|
---|
6378 |
|
---|
6379 | if ( !fRepairImageZeroParentUuid
|
---|
6380 | && m->pParent->i_getState() != MediumState_Inaccessible
|
---|
6381 | && m->pParent->i_getId() != parentId)
|
---|
6382 | {
|
---|
6383 | /** @todo r=klaus this always refers to VirtualBox.xml as the medium registry, even for new VMs */
|
---|
6384 | lastAccessError = Utf8StrFmt(
|
---|
6385 | tr("Parent UUID {%RTuuid} of the medium '%s' does not match UUID {%RTuuid} of its parent medium stored in the media registry ('%s')"),
|
---|
6386 | &parentId, location.c_str(),
|
---|
6387 | m->pParent->i_getId().raw(),
|
---|
6388 | pVirtualBox->i_settingsFilePath().c_str());
|
---|
6389 | parentLock.release();
|
---|
6390 | treeLock.release();
|
---|
6391 | throw S_OK;
|
---|
6392 | }
|
---|
6393 | }
|
---|
6394 |
|
---|
6395 | /// @todo NEWMEDIA what to do if the parent is not
|
---|
6396 | /// accessible while the diff is? Probably nothing. The
|
---|
6397 | /// real code will detect the mismatch anyway.
|
---|
6398 |
|
---|
6399 | treeLock.release();
|
---|
6400 | }
|
---|
6401 | }
|
---|
6402 |
|
---|
6403 | mediumSize = VDGetFileSize(hdd, 0);
|
---|
6404 | mediumLogicalSize = VDGetSize(hdd, 0);
|
---|
6405 |
|
---|
6406 | success = true;
|
---|
6407 | }
|
---|
6408 | catch (HRESULT aRC)
|
---|
6409 | {
|
---|
6410 | rc = aRC;
|
---|
6411 | }
|
---|
6412 |
|
---|
6413 | vrc = VDDestroy(hdd);
|
---|
6414 | if (RT_FAILURE(vrc))
|
---|
6415 | {
|
---|
6416 | lastAccessError = Utf8StrFmt(tr("Could not update and close the medium '%s'%s"),
|
---|
6417 | location.c_str(), i_vdError(vrc).c_str());
|
---|
6418 | success = false;
|
---|
6419 | throw S_OK;
|
---|
6420 | }
|
---|
6421 | }
|
---|
6422 | catch (HRESULT aRC)
|
---|
6423 | {
|
---|
6424 | rc = aRC;
|
---|
6425 | }
|
---|
6426 |
|
---|
6427 | autoCaller.release();
|
---|
6428 | treeLock.acquire();
|
---|
6429 | autoCaller.add();
|
---|
6430 | if (FAILED(autoCaller.rc()))
|
---|
6431 | {
|
---|
6432 | m->queryInfoRunning = false;
|
---|
6433 | return autoCaller.rc();
|
---|
6434 | }
|
---|
6435 | alock.acquire();
|
---|
6436 |
|
---|
6437 | if (success)
|
---|
6438 | {
|
---|
6439 | m->size = mediumSize;
|
---|
6440 | m->logicalSize = mediumLogicalSize;
|
---|
6441 | m->strLastAccessError.setNull();
|
---|
6442 | }
|
---|
6443 | else
|
---|
6444 | {
|
---|
6445 | m->strLastAccessError = lastAccessError;
|
---|
6446 | Log1WarningFunc(("'%s' is not accessible (error='%s', rc=%Rhrc, vrc=%Rrc)\n",
|
---|
6447 | location.c_str(), m->strLastAccessError.c_str(), rc, vrc));
|
---|
6448 | }
|
---|
6449 |
|
---|
6450 | /* Set the proper state according to the result of the check */
|
---|
6451 | if (success)
|
---|
6452 | m->preLockState = MediumState_Created;
|
---|
6453 | else
|
---|
6454 | m->preLockState = MediumState_Inaccessible;
|
---|
6455 |
|
---|
6456 | /* unblock anyone waiting for the i_queryInfo results */
|
---|
6457 | qlock.release();
|
---|
6458 | m->queryInfoRunning = false;
|
---|
6459 |
|
---|
6460 | pToken->Abandon();
|
---|
6461 | pToken.setNull();
|
---|
6462 |
|
---|
6463 | if (FAILED(rc)) return rc;
|
---|
6464 |
|
---|
6465 | /* If this is a base image which incorrectly has a parent UUID set,
|
---|
6466 | * repair the image now by zeroing the parent UUID. This is only done
|
---|
6467 | * when we have structural information from a config file, on import
|
---|
6468 | * this is not possible. If someone would accidentally call openMedium
|
---|
6469 | * with a diff image before the base is registered this would destroy
|
---|
6470 | * the diff. Not acceptable. */
|
---|
6471 | if (fRepairImageZeroParentUuid)
|
---|
6472 | {
|
---|
6473 | rc = LockWrite(pToken.asOutParam());
|
---|
6474 | if (FAILED(rc)) return rc;
|
---|
6475 |
|
---|
6476 | alock.release();
|
---|
6477 |
|
---|
6478 | try
|
---|
6479 | {
|
---|
6480 | PVBOXHDD hdd;
|
---|
6481 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
6482 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6483 |
|
---|
6484 | try
|
---|
6485 | {
|
---|
6486 | vrc = VDOpen(hdd,
|
---|
6487 | format.c_str(),
|
---|
6488 | location.c_str(),
|
---|
6489 | (uOpenFlags & ~VD_OPEN_FLAGS_READONLY) | m->uOpenFlagsDef,
|
---|
6490 | m->vdImageIfaces);
|
---|
6491 | if (RT_FAILURE(vrc))
|
---|
6492 | throw S_OK;
|
---|
6493 |
|
---|
6494 | RTUUID zeroParentUuid;
|
---|
6495 | RTUuidClear(&zeroParentUuid);
|
---|
6496 | vrc = VDSetParentUuid(hdd, 0, &zeroParentUuid);
|
---|
6497 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6498 | }
|
---|
6499 | catch (HRESULT aRC)
|
---|
6500 | {
|
---|
6501 | rc = aRC;
|
---|
6502 | }
|
---|
6503 |
|
---|
6504 | VDDestroy(hdd);
|
---|
6505 | }
|
---|
6506 | catch (HRESULT aRC)
|
---|
6507 | {
|
---|
6508 | rc = aRC;
|
---|
6509 | }
|
---|
6510 |
|
---|
6511 | pToken->Abandon();
|
---|
6512 | pToken.setNull();
|
---|
6513 | if (FAILED(rc)) return rc;
|
---|
6514 | }
|
---|
6515 |
|
---|
6516 | return rc;
|
---|
6517 | }
|
---|
6518 |
|
---|
6519 | /**
|
---|
6520 | * Performs extra checks if the medium can be closed and returns S_OK in
|
---|
6521 | * this case. Otherwise, returns a respective error message. Called by
|
---|
6522 | * Close() under the medium tree lock and the medium lock.
|
---|
6523 | *
|
---|
6524 | * @note Also reused by Medium::Reset().
|
---|
6525 | *
|
---|
6526 | * @note Caller must hold the media tree write lock!
|
---|
6527 | */
|
---|
6528 | HRESULT Medium::i_canClose()
|
---|
6529 | {
|
---|
6530 | Assert(m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
6531 |
|
---|
6532 | if (i_getChildren().size() != 0)
|
---|
6533 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
6534 | tr("Cannot close medium '%s' because it has %d child media"),
|
---|
6535 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
6536 |
|
---|
6537 | return S_OK;
|
---|
6538 | }
|
---|
6539 |
|
---|
6540 | /**
|
---|
6541 | * Unregisters this medium with mVirtualBox. Called by close() under the medium tree lock.
|
---|
6542 | *
|
---|
6543 | * @note Caller must have locked the media tree lock for writing!
|
---|
6544 | */
|
---|
6545 | HRESULT Medium::i_unregisterWithVirtualBox()
|
---|
6546 | {
|
---|
6547 | /* Note that we need to de-associate ourselves from the parent to let
|
---|
6548 | * VirtualBox::i_unregisterMedium() properly save the registry */
|
---|
6549 |
|
---|
6550 | /* we modify mParent and access children */
|
---|
6551 | Assert(m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
6552 |
|
---|
6553 | Medium *pParentBackup = m->pParent;
|
---|
6554 | AssertReturn(i_getChildren().size() == 0, E_FAIL);
|
---|
6555 | if (m->pParent)
|
---|
6556 | i_deparent();
|
---|
6557 |
|
---|
6558 | HRESULT rc = m->pVirtualBox->i_unregisterMedium(this);
|
---|
6559 | if (FAILED(rc))
|
---|
6560 | {
|
---|
6561 | if (pParentBackup)
|
---|
6562 | {
|
---|
6563 | // re-associate with the parent as we are still relatives in the registry
|
---|
6564 | i_setParent(pParentBackup);
|
---|
6565 | }
|
---|
6566 | }
|
---|
6567 |
|
---|
6568 | return rc;
|
---|
6569 | }
|
---|
6570 |
|
---|
6571 | /**
|
---|
6572 | * Like SetProperty but do not trigger a settings store. Only for internal use!
|
---|
6573 | */
|
---|
6574 | HRESULT Medium::i_setPropertyDirect(const Utf8Str &aName, const Utf8Str &aValue)
|
---|
6575 | {
|
---|
6576 | AutoCaller autoCaller(this);
|
---|
6577 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
6578 |
|
---|
6579 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6580 |
|
---|
6581 | switch (m->state)
|
---|
6582 | {
|
---|
6583 | case MediumState_Created:
|
---|
6584 | case MediumState_Inaccessible:
|
---|
6585 | break;
|
---|
6586 | default:
|
---|
6587 | return i_setStateError();
|
---|
6588 | }
|
---|
6589 |
|
---|
6590 | m->mapProperties[aName] = aValue;
|
---|
6591 |
|
---|
6592 | return S_OK;
|
---|
6593 | }
|
---|
6594 |
|
---|
6595 | /**
|
---|
6596 | * Sets the extended error info according to the current media state.
|
---|
6597 | *
|
---|
6598 | * @note Must be called from under this object's write or read lock.
|
---|
6599 | */
|
---|
6600 | HRESULT Medium::i_setStateError()
|
---|
6601 | {
|
---|
6602 | HRESULT rc = E_FAIL;
|
---|
6603 |
|
---|
6604 | switch (m->state)
|
---|
6605 | {
|
---|
6606 | case MediumState_NotCreated:
|
---|
6607 | {
|
---|
6608 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
6609 | tr("Storage for the medium '%s' is not created"),
|
---|
6610 | m->strLocationFull.c_str());
|
---|
6611 | break;
|
---|
6612 | }
|
---|
6613 | case MediumState_Created:
|
---|
6614 | {
|
---|
6615 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
6616 | tr("Storage for the medium '%s' is already created"),
|
---|
6617 | m->strLocationFull.c_str());
|
---|
6618 | break;
|
---|
6619 | }
|
---|
6620 | case MediumState_LockedRead:
|
---|
6621 | {
|
---|
6622 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
6623 | tr("Medium '%s' is locked for reading by another task"),
|
---|
6624 | m->strLocationFull.c_str());
|
---|
6625 | break;
|
---|
6626 | }
|
---|
6627 | case MediumState_LockedWrite:
|
---|
6628 | {
|
---|
6629 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
6630 | tr("Medium '%s' is locked for writing by another task"),
|
---|
6631 | m->strLocationFull.c_str());
|
---|
6632 | break;
|
---|
6633 | }
|
---|
6634 | case MediumState_Inaccessible:
|
---|
6635 | {
|
---|
6636 | /* be in sync with Console::powerUpThread() */
|
---|
6637 | if (!m->strLastAccessError.isEmpty())
|
---|
6638 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
6639 | tr("Medium '%s' is not accessible. %s"),
|
---|
6640 | m->strLocationFull.c_str(), m->strLastAccessError.c_str());
|
---|
6641 | else
|
---|
6642 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
6643 | tr("Medium '%s' is not accessible"),
|
---|
6644 | m->strLocationFull.c_str());
|
---|
6645 | break;
|
---|
6646 | }
|
---|
6647 | case MediumState_Creating:
|
---|
6648 | {
|
---|
6649 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
6650 | tr("Storage for the medium '%s' is being created"),
|
---|
6651 | m->strLocationFull.c_str());
|
---|
6652 | break;
|
---|
6653 | }
|
---|
6654 | case MediumState_Deleting:
|
---|
6655 | {
|
---|
6656 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
6657 | tr("Storage for the medium '%s' is being deleted"),
|
---|
6658 | m->strLocationFull.c_str());
|
---|
6659 | break;
|
---|
6660 | }
|
---|
6661 | default:
|
---|
6662 | {
|
---|
6663 | AssertFailed();
|
---|
6664 | break;
|
---|
6665 | }
|
---|
6666 | }
|
---|
6667 |
|
---|
6668 | return rc;
|
---|
6669 | }
|
---|
6670 |
|
---|
6671 | /**
|
---|
6672 | * Sets the value of m->strLocationFull. The given location must be a fully
|
---|
6673 | * qualified path; relative paths are not supported here.
|
---|
6674 | *
|
---|
6675 | * As a special exception, if the specified location is a file path that ends with '/'
|
---|
6676 | * then the file name part will be generated by this method automatically in the format
|
---|
6677 | * '{<uuid>}.<ext>' where <uuid> is a fresh UUID that this method will generate
|
---|
6678 | * and assign to this medium, and <ext> is the default extension for this
|
---|
6679 | * medium's storage format. Note that this procedure requires the media state to
|
---|
6680 | * be NotCreated and will return a failure otherwise.
|
---|
6681 | *
|
---|
6682 | * @param aLocation Location of the storage unit. If the location is a FS-path,
|
---|
6683 | * then it can be relative to the VirtualBox home directory.
|
---|
6684 | * @param aFormat Optional fallback format if it is an import and the format
|
---|
6685 | * cannot be determined.
|
---|
6686 | *
|
---|
6687 | * @note Must be called from under this object's write lock.
|
---|
6688 | */
|
---|
6689 | HRESULT Medium::i_setLocation(const Utf8Str &aLocation,
|
---|
6690 | const Utf8Str &aFormat /* = Utf8Str::Empty */)
|
---|
6691 | {
|
---|
6692 | AssertReturn(!aLocation.isEmpty(), E_FAIL);
|
---|
6693 |
|
---|
6694 | AutoCaller autoCaller(this);
|
---|
6695 | AssertComRCReturnRC(autoCaller.rc());
|
---|
6696 |
|
---|
6697 | /* formatObj may be null only when initializing from an existing path and
|
---|
6698 | * no format is known yet */
|
---|
6699 | AssertReturn( (!m->strFormat.isEmpty() && !m->formatObj.isNull())
|
---|
6700 | || ( getObjectState().getState() == ObjectState::InInit
|
---|
6701 | && m->state != MediumState_NotCreated
|
---|
6702 | && m->id.isZero()
|
---|
6703 | && m->strFormat.isEmpty()
|
---|
6704 | && m->formatObj.isNull()),
|
---|
6705 | E_FAIL);
|
---|
6706 |
|
---|
6707 | /* are we dealing with a new medium constructed using the existing
|
---|
6708 | * location? */
|
---|
6709 | bool isImport = m->strFormat.isEmpty();
|
---|
6710 |
|
---|
6711 | if ( isImport
|
---|
6712 | || ( (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
6713 | && !m->hostDrive))
|
---|
6714 | {
|
---|
6715 | Guid id;
|
---|
6716 |
|
---|
6717 | Utf8Str locationFull(aLocation);
|
---|
6718 |
|
---|
6719 | if (m->state == MediumState_NotCreated)
|
---|
6720 | {
|
---|
6721 | /* must be a file (formatObj must be already known) */
|
---|
6722 | Assert(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File);
|
---|
6723 |
|
---|
6724 | if (RTPathFilename(aLocation.c_str()) == NULL)
|
---|
6725 | {
|
---|
6726 | /* no file name is given (either an empty string or ends with a
|
---|
6727 | * slash), generate a new UUID + file name if the state allows
|
---|
6728 | * this */
|
---|
6729 |
|
---|
6730 | ComAssertMsgRet(!m->formatObj->i_getFileExtensions().empty(),
|
---|
6731 | ("Must be at least one extension if it is MediumFormatCapabilities_File\n"),
|
---|
6732 | E_FAIL);
|
---|
6733 |
|
---|
6734 | Utf8Str strExt = m->formatObj->i_getFileExtensions().front();
|
---|
6735 | ComAssertMsgRet(!strExt.isEmpty(),
|
---|
6736 | ("Default extension must not be empty\n"),
|
---|
6737 | E_FAIL);
|
---|
6738 |
|
---|
6739 | id.create();
|
---|
6740 |
|
---|
6741 | locationFull = Utf8StrFmt("%s{%RTuuid}.%s",
|
---|
6742 | aLocation.c_str(), id.raw(), strExt.c_str());
|
---|
6743 | }
|
---|
6744 | }
|
---|
6745 |
|
---|
6746 | // we must always have full paths now (if it refers to a file)
|
---|
6747 | if ( ( m->formatObj.isNull()
|
---|
6748 | || m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
6749 | && !RTPathStartsWithRoot(locationFull.c_str()))
|
---|
6750 | return setError(VBOX_E_FILE_ERROR,
|
---|
6751 | tr("The given path '%s' is not fully qualified"),
|
---|
6752 | locationFull.c_str());
|
---|
6753 |
|
---|
6754 | /* detect the backend from the storage unit if importing */
|
---|
6755 | if (isImport)
|
---|
6756 | {
|
---|
6757 | VDTYPE enmType = VDTYPE_INVALID;
|
---|
6758 | char *backendName = NULL;
|
---|
6759 |
|
---|
6760 | int vrc = VINF_SUCCESS;
|
---|
6761 |
|
---|
6762 | /* is it a file? */
|
---|
6763 | {
|
---|
6764 | RTFILE file;
|
---|
6765 | vrc = RTFileOpen(&file, locationFull.c_str(), RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
6766 | if (RT_SUCCESS(vrc))
|
---|
6767 | RTFileClose(file);
|
---|
6768 | }
|
---|
6769 | if (RT_SUCCESS(vrc))
|
---|
6770 | {
|
---|
6771 | vrc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
|
---|
6772 | locationFull.c_str(), &backendName, &enmType);
|
---|
6773 | }
|
---|
6774 | else if ( vrc != VERR_FILE_NOT_FOUND
|
---|
6775 | && vrc != VERR_PATH_NOT_FOUND
|
---|
6776 | && vrc != VERR_ACCESS_DENIED
|
---|
6777 | && locationFull != aLocation)
|
---|
6778 | {
|
---|
6779 | /* assume it's not a file, restore the original location */
|
---|
6780 | locationFull = aLocation;
|
---|
6781 | vrc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
|
---|
6782 | locationFull.c_str(), &backendName, &enmType);
|
---|
6783 | }
|
---|
6784 |
|
---|
6785 | if (RT_FAILURE(vrc))
|
---|
6786 | {
|
---|
6787 | if (vrc == VERR_ACCESS_DENIED)
|
---|
6788 | return setError(VBOX_E_FILE_ERROR,
|
---|
6789 | tr("Permission problem accessing the file for the medium '%s' (%Rrc)"),
|
---|
6790 | locationFull.c_str(), vrc);
|
---|
6791 | else if (vrc == VERR_FILE_NOT_FOUND || vrc == VERR_PATH_NOT_FOUND)
|
---|
6792 | return setError(VBOX_E_FILE_ERROR,
|
---|
6793 | tr("Could not find file for the medium '%s' (%Rrc)"),
|
---|
6794 | locationFull.c_str(), vrc);
|
---|
6795 | else if (aFormat.isEmpty())
|
---|
6796 | return setError(VBOX_E_IPRT_ERROR,
|
---|
6797 | tr("Could not get the storage format of the medium '%s' (%Rrc)"),
|
---|
6798 | locationFull.c_str(), vrc);
|
---|
6799 | else
|
---|
6800 | {
|
---|
6801 | HRESULT rc = i_setFormat(aFormat);
|
---|
6802 | /* setFormat() must not fail since we've just used the backend so
|
---|
6803 | * the format object must be there */
|
---|
6804 | AssertComRCReturnRC(rc);
|
---|
6805 | }
|
---|
6806 | }
|
---|
6807 | else if ( enmType == VDTYPE_INVALID
|
---|
6808 | || m->devType != i_convertToDeviceType(enmType))
|
---|
6809 | {
|
---|
6810 | /*
|
---|
6811 | * The user tried to use a image as a device which is not supported
|
---|
6812 | * by the backend.
|
---|
6813 | */
|
---|
6814 | return setError(E_FAIL,
|
---|
6815 | tr("The medium '%s' can't be used as the requested device type"),
|
---|
6816 | locationFull.c_str());
|
---|
6817 | }
|
---|
6818 | else
|
---|
6819 | {
|
---|
6820 | ComAssertRet(backendName != NULL && *backendName != '\0', E_FAIL);
|
---|
6821 |
|
---|
6822 | HRESULT rc = i_setFormat(backendName);
|
---|
6823 | RTStrFree(backendName);
|
---|
6824 |
|
---|
6825 | /* setFormat() must not fail since we've just used the backend so
|
---|
6826 | * the format object must be there */
|
---|
6827 | AssertComRCReturnRC(rc);
|
---|
6828 | }
|
---|
6829 | }
|
---|
6830 |
|
---|
6831 | m->strLocationFull = locationFull;
|
---|
6832 |
|
---|
6833 | /* is it still a file? */
|
---|
6834 | if ( (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
6835 | && (m->state == MediumState_NotCreated)
|
---|
6836 | )
|
---|
6837 | /* assign a new UUID (this UUID will be used when calling
|
---|
6838 | * VDCreateBase/VDCreateDiff as a wanted UUID). Note that we
|
---|
6839 | * also do that if we didn't generate it to make sure it is
|
---|
6840 | * either generated by us or reset to null */
|
---|
6841 | unconst(m->id) = id;
|
---|
6842 | }
|
---|
6843 | else
|
---|
6844 | m->strLocationFull = aLocation;
|
---|
6845 |
|
---|
6846 | return S_OK;
|
---|
6847 | }
|
---|
6848 |
|
---|
6849 | /**
|
---|
6850 | * Checks that the format ID is valid and sets it on success.
|
---|
6851 | *
|
---|
6852 | * Note that this method will caller-reference the format object on success!
|
---|
6853 | * This reference must be released somewhere to let the MediumFormat object be
|
---|
6854 | * uninitialized.
|
---|
6855 | *
|
---|
6856 | * @note Must be called from under this object's write lock.
|
---|
6857 | */
|
---|
6858 | HRESULT Medium::i_setFormat(const Utf8Str &aFormat)
|
---|
6859 | {
|
---|
6860 | /* get the format object first */
|
---|
6861 | {
|
---|
6862 | SystemProperties *pSysProps = m->pVirtualBox->i_getSystemProperties();
|
---|
6863 | AutoReadLock propsLock(pSysProps COMMA_LOCKVAL_SRC_POS);
|
---|
6864 |
|
---|
6865 | unconst(m->formatObj) = pSysProps->i_mediumFormat(aFormat);
|
---|
6866 | if (m->formatObj.isNull())
|
---|
6867 | return setError(E_INVALIDARG,
|
---|
6868 | tr("Invalid medium storage format '%s'"),
|
---|
6869 | aFormat.c_str());
|
---|
6870 |
|
---|
6871 | /* get properties (preinsert them as keys in the map). Note that the
|
---|
6872 | * map doesn't grow over the object life time since the set of
|
---|
6873 | * properties is meant to be constant. */
|
---|
6874 |
|
---|
6875 | Assert(m->mapProperties.empty());
|
---|
6876 |
|
---|
6877 | for (MediumFormat::PropertyArray::const_iterator it = m->formatObj->i_getProperties().begin();
|
---|
6878 | it != m->formatObj->i_getProperties().end();
|
---|
6879 | ++it)
|
---|
6880 | {
|
---|
6881 | m->mapProperties.insert(std::make_pair(it->strName, Utf8Str::Empty));
|
---|
6882 | }
|
---|
6883 | }
|
---|
6884 |
|
---|
6885 | unconst(m->strFormat) = aFormat;
|
---|
6886 |
|
---|
6887 | return S_OK;
|
---|
6888 | }
|
---|
6889 |
|
---|
6890 | /**
|
---|
6891 | * Converts the Medium device type to the VD type.
|
---|
6892 | */
|
---|
6893 | VDTYPE Medium::i_convertDeviceType()
|
---|
6894 | {
|
---|
6895 | VDTYPE enmType;
|
---|
6896 |
|
---|
6897 | switch (m->devType)
|
---|
6898 | {
|
---|
6899 | case DeviceType_HardDisk:
|
---|
6900 | enmType = VDTYPE_HDD;
|
---|
6901 | break;
|
---|
6902 | case DeviceType_DVD:
|
---|
6903 | enmType = VDTYPE_DVD;
|
---|
6904 | break;
|
---|
6905 | case DeviceType_Floppy:
|
---|
6906 | enmType = VDTYPE_FLOPPY;
|
---|
6907 | break;
|
---|
6908 | default:
|
---|
6909 | ComAssertFailedRet(VDTYPE_INVALID);
|
---|
6910 | }
|
---|
6911 |
|
---|
6912 | return enmType;
|
---|
6913 | }
|
---|
6914 |
|
---|
6915 | /**
|
---|
6916 | * Converts from the VD type to the medium type.
|
---|
6917 | */
|
---|
6918 | DeviceType_T Medium::i_convertToDeviceType(VDTYPE enmType)
|
---|
6919 | {
|
---|
6920 | DeviceType_T devType;
|
---|
6921 |
|
---|
6922 | switch (enmType)
|
---|
6923 | {
|
---|
6924 | case VDTYPE_HDD:
|
---|
6925 | devType = DeviceType_HardDisk;
|
---|
6926 | break;
|
---|
6927 | case VDTYPE_DVD:
|
---|
6928 | devType = DeviceType_DVD;
|
---|
6929 | break;
|
---|
6930 | case VDTYPE_FLOPPY:
|
---|
6931 | devType = DeviceType_Floppy;
|
---|
6932 | break;
|
---|
6933 | default:
|
---|
6934 | ComAssertFailedRet(DeviceType_Null);
|
---|
6935 | }
|
---|
6936 |
|
---|
6937 | return devType;
|
---|
6938 | }
|
---|
6939 |
|
---|
6940 | /**
|
---|
6941 | * Internal method which checks whether a property name is for a filter plugin.
|
---|
6942 | */
|
---|
6943 | bool Medium::i_isPropertyForFilter(const com::Utf8Str &aName)
|
---|
6944 | {
|
---|
6945 | /* If the name contains "/" use the part before as a filter name and lookup the filter. */
|
---|
6946 | size_t offSlash;
|
---|
6947 | if ((offSlash = aName.find("/", 0)) != aName.npos)
|
---|
6948 | {
|
---|
6949 | com::Utf8Str strFilter;
|
---|
6950 | com::Utf8Str strKey;
|
---|
6951 |
|
---|
6952 | HRESULT rc = strFilter.assignEx(aName, 0, offSlash);
|
---|
6953 | if (FAILED(rc))
|
---|
6954 | return false;
|
---|
6955 |
|
---|
6956 | rc = strKey.assignEx(aName, offSlash + 1, aName.length() - offSlash - 1); /* Skip slash */
|
---|
6957 | if (FAILED(rc))
|
---|
6958 | return false;
|
---|
6959 |
|
---|
6960 | VDFILTERINFO FilterInfo;
|
---|
6961 | int vrc = VDFilterInfoOne(strFilter.c_str(), &FilterInfo);
|
---|
6962 | if (RT_SUCCESS(vrc))
|
---|
6963 | {
|
---|
6964 | /* Check that the property exists. */
|
---|
6965 | PCVDCONFIGINFO paConfig = FilterInfo.paConfigInfo;
|
---|
6966 | while (paConfig->pszKey)
|
---|
6967 | {
|
---|
6968 | if (strKey.equals(paConfig->pszKey))
|
---|
6969 | return true;
|
---|
6970 | paConfig++;
|
---|
6971 | }
|
---|
6972 | }
|
---|
6973 | }
|
---|
6974 |
|
---|
6975 | return false;
|
---|
6976 | }
|
---|
6977 |
|
---|
6978 | /**
|
---|
6979 | * Returns the last error message collected by the i_vdErrorCall callback and
|
---|
6980 | * resets it.
|
---|
6981 | *
|
---|
6982 | * The error message is returned prepended with a dot and a space, like this:
|
---|
6983 | * <code>
|
---|
6984 | * ". <error_text> (%Rrc)"
|
---|
6985 | * </code>
|
---|
6986 | * to make it easily appendable to a more general error message. The @c %Rrc
|
---|
6987 | * format string is given @a aVRC as an argument.
|
---|
6988 | *
|
---|
6989 | * If there is no last error message collected by i_vdErrorCall or if it is a
|
---|
6990 | * null or empty string, then this function returns the following text:
|
---|
6991 | * <code>
|
---|
6992 | * " (%Rrc)"
|
---|
6993 | * </code>
|
---|
6994 | *
|
---|
6995 | * @note Doesn't do any object locking; it is assumed that the caller makes sure
|
---|
6996 | * the callback isn't called by more than one thread at a time.
|
---|
6997 | *
|
---|
6998 | * @param aVRC VBox error code to use when no error message is provided.
|
---|
6999 | */
|
---|
7000 | Utf8Str Medium::i_vdError(int aVRC)
|
---|
7001 | {
|
---|
7002 | Utf8Str error;
|
---|
7003 |
|
---|
7004 | if (m->vdError.isEmpty())
|
---|
7005 | error = Utf8StrFmt(" (%Rrc)", aVRC);
|
---|
7006 | else
|
---|
7007 | error = Utf8StrFmt(".\n%s", m->vdError.c_str());
|
---|
7008 |
|
---|
7009 | m->vdError.setNull();
|
---|
7010 |
|
---|
7011 | return error;
|
---|
7012 | }
|
---|
7013 |
|
---|
7014 | /**
|
---|
7015 | * Error message callback.
|
---|
7016 | *
|
---|
7017 | * Puts the reported error message to the m->vdError field.
|
---|
7018 | *
|
---|
7019 | * @note Doesn't do any object locking; it is assumed that the caller makes sure
|
---|
7020 | * the callback isn't called by more than one thread at a time.
|
---|
7021 | *
|
---|
7022 | * @param pvUser The opaque data passed on container creation.
|
---|
7023 | * @param rc The VBox error code.
|
---|
7024 | * @param RT_SRC_POS_DECL Use RT_SRC_POS.
|
---|
7025 | * @param pszFormat Error message format string.
|
---|
7026 | * @param va Error message arguments.
|
---|
7027 | */
|
---|
7028 | /*static*/
|
---|
7029 | DECLCALLBACK(void) Medium::i_vdErrorCall(void *pvUser, int rc, RT_SRC_POS_DECL,
|
---|
7030 | const char *pszFormat, va_list va)
|
---|
7031 | {
|
---|
7032 | NOREF(pszFile); NOREF(iLine); NOREF(pszFunction); /* RT_SRC_POS_DECL */
|
---|
7033 |
|
---|
7034 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
7035 | AssertReturnVoid(that != NULL);
|
---|
7036 |
|
---|
7037 | if (that->m->vdError.isEmpty())
|
---|
7038 | that->m->vdError =
|
---|
7039 | Utf8StrFmt("%s (%Rrc)", Utf8Str(pszFormat, va).c_str(), rc);
|
---|
7040 | else
|
---|
7041 | that->m->vdError =
|
---|
7042 | Utf8StrFmt("%s.\n%s (%Rrc)", that->m->vdError.c_str(),
|
---|
7043 | Utf8Str(pszFormat, va).c_str(), rc);
|
---|
7044 | }
|
---|
7045 |
|
---|
7046 | /* static */
|
---|
7047 | DECLCALLBACK(bool) Medium::i_vdConfigAreKeysValid(void *pvUser,
|
---|
7048 | const char * /* pszzValid */)
|
---|
7049 | {
|
---|
7050 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
7051 | AssertReturn(that != NULL, false);
|
---|
7052 |
|
---|
7053 | /* we always return true since the only keys we have are those found in
|
---|
7054 | * VDBACKENDINFO */
|
---|
7055 | return true;
|
---|
7056 | }
|
---|
7057 |
|
---|
7058 | /* static */
|
---|
7059 | DECLCALLBACK(int) Medium::i_vdConfigQuerySize(void *pvUser,
|
---|
7060 | const char *pszName,
|
---|
7061 | size_t *pcbValue)
|
---|
7062 | {
|
---|
7063 | AssertReturn(VALID_PTR(pcbValue), VERR_INVALID_POINTER);
|
---|
7064 |
|
---|
7065 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
7066 | AssertReturn(that != NULL, VERR_GENERAL_FAILURE);
|
---|
7067 |
|
---|
7068 | settings::StringsMap::const_iterator it = that->m->mapProperties.find(Utf8Str(pszName));
|
---|
7069 | if (it == that->m->mapProperties.end())
|
---|
7070 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7071 |
|
---|
7072 | /* we interpret null values as "no value" in Medium */
|
---|
7073 | if (it->second.isEmpty())
|
---|
7074 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7075 |
|
---|
7076 | *pcbValue = it->second.length() + 1 /* include terminator */;
|
---|
7077 |
|
---|
7078 | return VINF_SUCCESS;
|
---|
7079 | }
|
---|
7080 |
|
---|
7081 | /* static */
|
---|
7082 | DECLCALLBACK(int) Medium::i_vdConfigQuery(void *pvUser,
|
---|
7083 | const char *pszName,
|
---|
7084 | char *pszValue,
|
---|
7085 | size_t cchValue)
|
---|
7086 | {
|
---|
7087 | AssertReturn(VALID_PTR(pszValue), VERR_INVALID_POINTER);
|
---|
7088 |
|
---|
7089 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
7090 | AssertReturn(that != NULL, VERR_GENERAL_FAILURE);
|
---|
7091 |
|
---|
7092 | settings::StringsMap::const_iterator it = that->m->mapProperties.find(Utf8Str(pszName));
|
---|
7093 | if (it == that->m->mapProperties.end())
|
---|
7094 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7095 |
|
---|
7096 | /* we interpret null values as "no value" in Medium */
|
---|
7097 | if (it->second.isEmpty())
|
---|
7098 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7099 |
|
---|
7100 | const Utf8Str &value = it->second;
|
---|
7101 | if (value.length() >= cchValue)
|
---|
7102 | return VERR_CFGM_NOT_ENOUGH_SPACE;
|
---|
7103 |
|
---|
7104 | memcpy(pszValue, value.c_str(), value.length() + 1);
|
---|
7105 |
|
---|
7106 | return VINF_SUCCESS;
|
---|
7107 | }
|
---|
7108 |
|
---|
7109 | DECLCALLBACK(int) Medium::i_vdTcpSocketCreate(uint32_t fFlags, PVDSOCKET pSock)
|
---|
7110 | {
|
---|
7111 | PVDSOCKETINT pSocketInt = NULL;
|
---|
7112 |
|
---|
7113 | if ((fFlags & VD_INTERFACETCPNET_CONNECT_EXTENDED_SELECT) != 0)
|
---|
7114 | return VERR_NOT_SUPPORTED;
|
---|
7115 |
|
---|
7116 | pSocketInt = (PVDSOCKETINT)RTMemAllocZ(sizeof(VDSOCKETINT));
|
---|
7117 | if (!pSocketInt)
|
---|
7118 | return VERR_NO_MEMORY;
|
---|
7119 |
|
---|
7120 | pSocketInt->hSocket = NIL_RTSOCKET;
|
---|
7121 | *pSock = pSocketInt;
|
---|
7122 | return VINF_SUCCESS;
|
---|
7123 | }
|
---|
7124 |
|
---|
7125 | DECLCALLBACK(int) Medium::i_vdTcpSocketDestroy(VDSOCKET Sock)
|
---|
7126 | {
|
---|
7127 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7128 |
|
---|
7129 | if (pSocketInt->hSocket != NIL_RTSOCKET)
|
---|
7130 | RTTcpClientCloseEx(pSocketInt->hSocket, false /*fGracefulShutdown*/);
|
---|
7131 |
|
---|
7132 | RTMemFree(pSocketInt);
|
---|
7133 |
|
---|
7134 | return VINF_SUCCESS;
|
---|
7135 | }
|
---|
7136 |
|
---|
7137 | DECLCALLBACK(int) Medium::i_vdTcpClientConnect(VDSOCKET Sock, const char *pszAddress, uint32_t uPort,
|
---|
7138 | RTMSINTERVAL cMillies)
|
---|
7139 | {
|
---|
7140 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7141 |
|
---|
7142 | return RTTcpClientConnectEx(pszAddress, uPort, &pSocketInt->hSocket, cMillies, NULL);
|
---|
7143 | }
|
---|
7144 |
|
---|
7145 | DECLCALLBACK(int) Medium::i_vdTcpClientClose(VDSOCKET Sock)
|
---|
7146 | {
|
---|
7147 | int rc = VINF_SUCCESS;
|
---|
7148 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7149 |
|
---|
7150 | rc = RTTcpClientCloseEx(pSocketInt->hSocket, false /*fGracefulShutdown*/);
|
---|
7151 | pSocketInt->hSocket = NIL_RTSOCKET;
|
---|
7152 | return rc;
|
---|
7153 | }
|
---|
7154 |
|
---|
7155 | DECLCALLBACK(bool) Medium::i_vdTcpIsClientConnected(VDSOCKET Sock)
|
---|
7156 | {
|
---|
7157 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7158 | return pSocketInt->hSocket != NIL_RTSOCKET;
|
---|
7159 | }
|
---|
7160 |
|
---|
7161 | DECLCALLBACK(int) Medium::i_vdTcpSelectOne(VDSOCKET Sock, RTMSINTERVAL cMillies)
|
---|
7162 | {
|
---|
7163 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7164 | return RTTcpSelectOne(pSocketInt->hSocket, cMillies);
|
---|
7165 | }
|
---|
7166 |
|
---|
7167 | DECLCALLBACK(int) Medium::i_vdTcpRead(VDSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead)
|
---|
7168 | {
|
---|
7169 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7170 | return RTTcpRead(pSocketInt->hSocket, pvBuffer, cbBuffer, pcbRead);
|
---|
7171 | }
|
---|
7172 |
|
---|
7173 | DECLCALLBACK(int) Medium::i_vdTcpWrite(VDSOCKET Sock, const void *pvBuffer, size_t cbBuffer)
|
---|
7174 | {
|
---|
7175 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7176 | return RTTcpWrite(pSocketInt->hSocket, pvBuffer, cbBuffer);
|
---|
7177 | }
|
---|
7178 |
|
---|
7179 | DECLCALLBACK(int) Medium::i_vdTcpSgWrite(VDSOCKET Sock, PCRTSGBUF pSgBuf)
|
---|
7180 | {
|
---|
7181 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7182 | return RTTcpSgWrite(pSocketInt->hSocket, pSgBuf);
|
---|
7183 | }
|
---|
7184 |
|
---|
7185 | DECLCALLBACK(int) Medium::i_vdTcpFlush(VDSOCKET Sock)
|
---|
7186 | {
|
---|
7187 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7188 | return RTTcpFlush(pSocketInt->hSocket);
|
---|
7189 | }
|
---|
7190 |
|
---|
7191 | DECLCALLBACK(int) Medium::i_vdTcpSetSendCoalescing(VDSOCKET Sock, bool fEnable)
|
---|
7192 | {
|
---|
7193 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7194 | return RTTcpSetSendCoalescing(pSocketInt->hSocket, fEnable);
|
---|
7195 | }
|
---|
7196 |
|
---|
7197 | DECLCALLBACK(int) Medium::i_vdTcpGetLocalAddress(VDSOCKET Sock, PRTNETADDR pAddr)
|
---|
7198 | {
|
---|
7199 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7200 | return RTTcpGetLocalAddress(pSocketInt->hSocket, pAddr);
|
---|
7201 | }
|
---|
7202 |
|
---|
7203 | DECLCALLBACK(int) Medium::i_vdTcpGetPeerAddress(VDSOCKET Sock, PRTNETADDR pAddr)
|
---|
7204 | {
|
---|
7205 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
7206 | return RTTcpGetPeerAddress(pSocketInt->hSocket, pAddr);
|
---|
7207 | }
|
---|
7208 |
|
---|
7209 | DECLCALLBACK(bool) Medium::i_vdCryptoConfigAreKeysValid(void *pvUser, const char *pszzValid)
|
---|
7210 | {
|
---|
7211 | /* Just return always true here. */
|
---|
7212 | NOREF(pvUser);
|
---|
7213 | NOREF(pszzValid);
|
---|
7214 | return true;
|
---|
7215 | }
|
---|
7216 |
|
---|
7217 | DECLCALLBACK(int) Medium::i_vdCryptoConfigQuerySize(void *pvUser, const char *pszName, size_t *pcbValue)
|
---|
7218 | {
|
---|
7219 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7220 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7221 | AssertReturn(VALID_PTR(pcbValue), VERR_INVALID_POINTER);
|
---|
7222 |
|
---|
7223 | size_t cbValue = 0;
|
---|
7224 | if (!strcmp(pszName, "Algorithm"))
|
---|
7225 | cbValue = strlen(pSettings->pszCipher) + 1;
|
---|
7226 | else if (!strcmp(pszName, "KeyId"))
|
---|
7227 | cbValue = sizeof("irrelevant");
|
---|
7228 | else if (!strcmp(pszName, "KeyStore"))
|
---|
7229 | {
|
---|
7230 | if (!pSettings->pszKeyStoreLoad)
|
---|
7231 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7232 | cbValue = strlen(pSettings->pszKeyStoreLoad) + 1;
|
---|
7233 | }
|
---|
7234 | else if (!strcmp(pszName, "CreateKeyStore"))
|
---|
7235 | cbValue = 2; /* Single digit + terminator. */
|
---|
7236 | else
|
---|
7237 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7238 |
|
---|
7239 | *pcbValue = cbValue + 1 /* include terminator */;
|
---|
7240 |
|
---|
7241 | return VINF_SUCCESS;
|
---|
7242 | }
|
---|
7243 |
|
---|
7244 | DECLCALLBACK(int) Medium::i_vdCryptoConfigQuery(void *pvUser, const char *pszName,
|
---|
7245 | char *pszValue, size_t cchValue)
|
---|
7246 | {
|
---|
7247 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7248 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7249 | AssertReturn(VALID_PTR(pszValue), VERR_INVALID_POINTER);
|
---|
7250 |
|
---|
7251 | const char *psz = NULL;
|
---|
7252 | if (!strcmp(pszName, "Algorithm"))
|
---|
7253 | psz = pSettings->pszCipher;
|
---|
7254 | else if (!strcmp(pszName, "KeyId"))
|
---|
7255 | psz = "irrelevant";
|
---|
7256 | else if (!strcmp(pszName, "KeyStore"))
|
---|
7257 | psz = pSettings->pszKeyStoreLoad;
|
---|
7258 | else if (!strcmp(pszName, "CreateKeyStore"))
|
---|
7259 | {
|
---|
7260 | if (pSettings->fCreateKeyStore)
|
---|
7261 | psz = "1";
|
---|
7262 | else
|
---|
7263 | psz = "0";
|
---|
7264 | }
|
---|
7265 | else
|
---|
7266 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
7267 |
|
---|
7268 | size_t cch = strlen(psz);
|
---|
7269 | if (cch >= cchValue)
|
---|
7270 | return VERR_CFGM_NOT_ENOUGH_SPACE;
|
---|
7271 |
|
---|
7272 | memcpy(pszValue, psz, cch + 1);
|
---|
7273 | return VINF_SUCCESS;
|
---|
7274 | }
|
---|
7275 |
|
---|
7276 | DECLCALLBACK(int) Medium::i_vdCryptoKeyRetain(void *pvUser, const char *pszId,
|
---|
7277 | const uint8_t **ppbKey, size_t *pcbKey)
|
---|
7278 | {
|
---|
7279 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7280 | NOREF(pszId);
|
---|
7281 | NOREF(ppbKey);
|
---|
7282 | NOREF(pcbKey);
|
---|
7283 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7284 | AssertMsgFailedReturn(("This method should not be called here!\n"), VERR_INVALID_STATE);
|
---|
7285 | }
|
---|
7286 |
|
---|
7287 | DECLCALLBACK(int) Medium::i_vdCryptoKeyRelease(void *pvUser, const char *pszId)
|
---|
7288 | {
|
---|
7289 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7290 | NOREF(pszId);
|
---|
7291 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7292 | AssertMsgFailedReturn(("This method should not be called here!\n"), VERR_INVALID_STATE);
|
---|
7293 | }
|
---|
7294 |
|
---|
7295 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStorePasswordRetain(void *pvUser, const char *pszId, const char **ppszPassword)
|
---|
7296 | {
|
---|
7297 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7298 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7299 |
|
---|
7300 | NOREF(pszId);
|
---|
7301 | *ppszPassword = pSettings->pszPassword;
|
---|
7302 | return VINF_SUCCESS;
|
---|
7303 | }
|
---|
7304 |
|
---|
7305 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStorePasswordRelease(void *pvUser, const char *pszId)
|
---|
7306 | {
|
---|
7307 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7308 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7309 | NOREF(pszId);
|
---|
7310 | return VINF_SUCCESS;
|
---|
7311 | }
|
---|
7312 |
|
---|
7313 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStoreSave(void *pvUser, const void *pvKeyStore, size_t cbKeyStore)
|
---|
7314 | {
|
---|
7315 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7316 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7317 |
|
---|
7318 | pSettings->pszKeyStore = (char *)RTMemAllocZ(cbKeyStore);
|
---|
7319 | if (!pSettings->pszKeyStore)
|
---|
7320 | return VERR_NO_MEMORY;
|
---|
7321 |
|
---|
7322 | memcpy(pSettings->pszKeyStore, pvKeyStore, cbKeyStore);
|
---|
7323 | return VINF_SUCCESS;
|
---|
7324 | }
|
---|
7325 |
|
---|
7326 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStoreReturnParameters(void *pvUser, const char *pszCipher,
|
---|
7327 | const uint8_t *pbDek, size_t cbDek)
|
---|
7328 | {
|
---|
7329 | Medium::CryptoFilterSettings *pSettings = (Medium::CryptoFilterSettings *)pvUser;
|
---|
7330 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
7331 |
|
---|
7332 | pSettings->pszCipherReturned = RTStrDup(pszCipher);
|
---|
7333 | pSettings->pbDek = pbDek;
|
---|
7334 | pSettings->cbDek = cbDek;
|
---|
7335 |
|
---|
7336 | return pSettings->pszCipherReturned ? VINF_SUCCESS : VERR_NO_MEMORY;
|
---|
7337 | }
|
---|
7338 |
|
---|
7339 | /**
|
---|
7340 | * Starts a new thread driven by the appropriate Medium::Task::handler() method.
|
---|
7341 | *
|
---|
7342 | * @note When the task is executed by this method, IProgress::notifyComplete()
|
---|
7343 | * is automatically called for the progress object associated with this
|
---|
7344 | * task when the task is finished to signal the operation completion for
|
---|
7345 | * other threads asynchronously waiting for it.
|
---|
7346 | */
|
---|
7347 | HRESULT Medium::i_startThread(Medium::Task *pTask)
|
---|
7348 | {
|
---|
7349 | #ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
|
---|
7350 | /* Extreme paranoia: The calling thread should not hold the medium
|
---|
7351 | * tree lock or any medium lock. Since there is no separate lock class
|
---|
7352 | * for medium objects be even more strict: no other object locks. */
|
---|
7353 | Assert(!AutoLockHoldsLocksInClass(LOCKCLASS_LISTOFMEDIA));
|
---|
7354 | Assert(!AutoLockHoldsLocksInClass(getLockingClass()));
|
---|
7355 | #endif
|
---|
7356 |
|
---|
7357 | /// @todo use a more descriptive task name
|
---|
7358 | int vrc = RTThreadCreate(NULL, Medium::Task::fntMediumTask, pTask,
|
---|
7359 | 0, RTTHREADTYPE_MAIN_HEAVY_WORKER, 0,
|
---|
7360 | "Medium::Task");
|
---|
7361 | if (RT_FAILURE(vrc))
|
---|
7362 | {
|
---|
7363 | delete pTask;
|
---|
7364 | return setError(E_FAIL, "Could not create Medium::Task thread (%Rrc)\n", vrc);
|
---|
7365 | }
|
---|
7366 |
|
---|
7367 | return S_OK;
|
---|
7368 | }
|
---|
7369 |
|
---|
7370 | /**
|
---|
7371 | * Runs Medium::Task::handler() on the current thread instead of creating
|
---|
7372 | * a new one.
|
---|
7373 | *
|
---|
7374 | * This call implies that it is made on another temporary thread created for
|
---|
7375 | * some asynchronous task. Avoid calling it from a normal thread since the task
|
---|
7376 | * operations are potentially lengthy and will block the calling thread in this
|
---|
7377 | * case.
|
---|
7378 | *
|
---|
7379 | * @note When the task is executed by this method, IProgress::notifyComplete()
|
---|
7380 | * is not called for the progress object associated with this task when
|
---|
7381 | * the task is finished. Instead, the result of the operation is returned
|
---|
7382 | * by this method directly and it's the caller's responsibility to
|
---|
7383 | * complete the progress object in this case.
|
---|
7384 | */
|
---|
7385 | HRESULT Medium::i_runNow(Medium::Task *pTask)
|
---|
7386 | {
|
---|
7387 | #ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
|
---|
7388 | /* Extreme paranoia: The calling thread should not hold the medium
|
---|
7389 | * tree lock or any medium lock. Since there is no separate lock class
|
---|
7390 | * for medium objects be even more strict: no other object locks. */
|
---|
7391 | Assert(!AutoLockHoldsLocksInClass(LOCKCLASS_LISTOFMEDIA));
|
---|
7392 | Assert(!AutoLockHoldsLocksInClass(getLockingClass()));
|
---|
7393 | #endif
|
---|
7394 |
|
---|
7395 | /* NIL_RTTHREAD indicates synchronous call. */
|
---|
7396 | return (HRESULT)Medium::Task::fntMediumTask(NIL_RTTHREAD, pTask);
|
---|
7397 | }
|
---|
7398 |
|
---|
7399 | /**
|
---|
7400 | * Implementation code for the "create base" task.
|
---|
7401 | *
|
---|
7402 | * This only gets started from Medium::CreateBaseStorage() and always runs
|
---|
7403 | * asynchronously. As a result, we always save the VirtualBox.xml file when
|
---|
7404 | * we're done here.
|
---|
7405 | *
|
---|
7406 | * @param task
|
---|
7407 | * @return
|
---|
7408 | */
|
---|
7409 | HRESULT Medium::i_taskCreateBaseHandler(Medium::CreateBaseTask &task)
|
---|
7410 | {
|
---|
7411 | HRESULT rc = S_OK;
|
---|
7412 |
|
---|
7413 | /* these parameters we need after creation */
|
---|
7414 | uint64_t size = 0, logicalSize = 0;
|
---|
7415 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
7416 | bool fGenerateUuid = false;
|
---|
7417 |
|
---|
7418 | try
|
---|
7419 | {
|
---|
7420 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7421 |
|
---|
7422 | /* The object may request a specific UUID (through a special form of
|
---|
7423 | * the setLocation() argument). Otherwise we have to generate it */
|
---|
7424 | Guid id = m->id;
|
---|
7425 |
|
---|
7426 | fGenerateUuid = id.isZero();
|
---|
7427 | if (fGenerateUuid)
|
---|
7428 | {
|
---|
7429 | id.create();
|
---|
7430 | /* VirtualBox::i_registerMedium() will need UUID */
|
---|
7431 | unconst(m->id) = id;
|
---|
7432 | }
|
---|
7433 |
|
---|
7434 | Utf8Str format(m->strFormat);
|
---|
7435 | Utf8Str location(m->strLocationFull);
|
---|
7436 | uint64_t capabilities = m->formatObj->i_getCapabilities();
|
---|
7437 | ComAssertThrow(capabilities & ( MediumFormatCapabilities_CreateFixed
|
---|
7438 | | MediumFormatCapabilities_CreateDynamic), E_FAIL);
|
---|
7439 | Assert(m->state == MediumState_Creating);
|
---|
7440 |
|
---|
7441 | PVBOXHDD hdd;
|
---|
7442 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
7443 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7444 |
|
---|
7445 | /* unlock before the potentially lengthy operation */
|
---|
7446 | thisLock.release();
|
---|
7447 |
|
---|
7448 | try
|
---|
7449 | {
|
---|
7450 | /* ensure the directory exists */
|
---|
7451 | if (capabilities & MediumFormatCapabilities_File)
|
---|
7452 | {
|
---|
7453 | rc = VirtualBox::i_ensureFilePathExists(location, !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
7454 | if (FAILED(rc))
|
---|
7455 | throw rc;
|
---|
7456 | }
|
---|
7457 |
|
---|
7458 | VDGEOMETRY geo = { 0, 0, 0 }; /* auto-detect */
|
---|
7459 |
|
---|
7460 | vrc = VDCreateBase(hdd,
|
---|
7461 | format.c_str(),
|
---|
7462 | location.c_str(),
|
---|
7463 | task.mSize,
|
---|
7464 | task.mVariant & ~MediumVariant_NoCreateDir,
|
---|
7465 | NULL,
|
---|
7466 | &geo,
|
---|
7467 | &geo,
|
---|
7468 | id.raw(),
|
---|
7469 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
7470 | m->vdImageIfaces,
|
---|
7471 | task.mVDOperationIfaces);
|
---|
7472 | if (RT_FAILURE(vrc))
|
---|
7473 | {
|
---|
7474 | if (vrc == VERR_VD_INVALID_TYPE)
|
---|
7475 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7476 | tr("Parameters for creating the medium storage unit '%s' are invalid%s"),
|
---|
7477 | location.c_str(), i_vdError(vrc).c_str());
|
---|
7478 | else
|
---|
7479 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7480 | tr("Could not create the medium storage unit '%s'%s"),
|
---|
7481 | location.c_str(), i_vdError(vrc).c_str());
|
---|
7482 | }
|
---|
7483 |
|
---|
7484 | size = VDGetFileSize(hdd, 0);
|
---|
7485 | logicalSize = VDGetSize(hdd, 0);
|
---|
7486 | unsigned uImageFlags;
|
---|
7487 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
7488 | if (RT_SUCCESS(vrc))
|
---|
7489 | variant = (MediumVariant_T)uImageFlags;
|
---|
7490 | }
|
---|
7491 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7492 |
|
---|
7493 | VDDestroy(hdd);
|
---|
7494 | }
|
---|
7495 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7496 |
|
---|
7497 | if (SUCCEEDED(rc))
|
---|
7498 | {
|
---|
7499 | /* register with mVirtualBox as the last step and move to
|
---|
7500 | * Created state only on success (leaving an orphan file is
|
---|
7501 | * better than breaking media registry consistency) */
|
---|
7502 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
7503 | ComObjPtr<Medium> pMedium;
|
---|
7504 | rc = m->pVirtualBox->i_registerMedium(this, &pMedium, treeLock);
|
---|
7505 | Assert(this == pMedium);
|
---|
7506 | }
|
---|
7507 |
|
---|
7508 | // re-acquire the lock before changing state
|
---|
7509 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7510 |
|
---|
7511 | if (SUCCEEDED(rc))
|
---|
7512 | {
|
---|
7513 | m->state = MediumState_Created;
|
---|
7514 |
|
---|
7515 | m->size = size;
|
---|
7516 | m->logicalSize = logicalSize;
|
---|
7517 | m->variant = variant;
|
---|
7518 |
|
---|
7519 | thisLock.release();
|
---|
7520 | i_markRegistriesModified();
|
---|
7521 | if (task.isAsync())
|
---|
7522 | {
|
---|
7523 | // in asynchronous mode, save settings now
|
---|
7524 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
7525 | }
|
---|
7526 | }
|
---|
7527 | else
|
---|
7528 | {
|
---|
7529 | /* back to NotCreated on failure */
|
---|
7530 | m->state = MediumState_NotCreated;
|
---|
7531 |
|
---|
7532 | /* reset UUID to prevent it from being reused next time */
|
---|
7533 | if (fGenerateUuid)
|
---|
7534 | unconst(m->id).clear();
|
---|
7535 | }
|
---|
7536 |
|
---|
7537 | return rc;
|
---|
7538 | }
|
---|
7539 |
|
---|
7540 | /**
|
---|
7541 | * Implementation code for the "create diff" task.
|
---|
7542 | *
|
---|
7543 | * This task always gets started from Medium::createDiffStorage() and can run
|
---|
7544 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
7545 | * that function. If we run synchronously, the caller expects the medium
|
---|
7546 | * registry modification to be set before returning; otherwise (in asynchronous
|
---|
7547 | * mode), we save the settings ourselves.
|
---|
7548 | *
|
---|
7549 | * @param task
|
---|
7550 | * @return
|
---|
7551 | */
|
---|
7552 | HRESULT Medium::i_taskCreateDiffHandler(Medium::CreateDiffTask &task)
|
---|
7553 | {
|
---|
7554 | HRESULT rcTmp = S_OK;
|
---|
7555 |
|
---|
7556 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
7557 |
|
---|
7558 | uint64_t size = 0, logicalSize = 0;
|
---|
7559 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
7560 | bool fGenerateUuid = false;
|
---|
7561 |
|
---|
7562 | try
|
---|
7563 | {
|
---|
7564 | if (i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
7565 | {
|
---|
7566 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7567 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7568 | 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"),
|
---|
7569 | m->strLocationFull.c_str());
|
---|
7570 | }
|
---|
7571 |
|
---|
7572 | /* Lock both in {parent,child} order. */
|
---|
7573 | AutoMultiWriteLock2 mediaLock(this, pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
7574 |
|
---|
7575 | /* The object may request a specific UUID (through a special form of
|
---|
7576 | * the setLocation() argument). Otherwise we have to generate it */
|
---|
7577 | Guid targetId = pTarget->m->id;
|
---|
7578 |
|
---|
7579 | fGenerateUuid = targetId.isZero();
|
---|
7580 | if (fGenerateUuid)
|
---|
7581 | {
|
---|
7582 | targetId.create();
|
---|
7583 | /* VirtualBox::i_registerMedium() will need UUID */
|
---|
7584 | unconst(pTarget->m->id) = targetId;
|
---|
7585 | }
|
---|
7586 |
|
---|
7587 | Guid id = m->id;
|
---|
7588 |
|
---|
7589 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
7590 | Utf8Str targetLocation(pTarget->m->strLocationFull);
|
---|
7591 | uint64_t capabilities = pTarget->m->formatObj->i_getCapabilities();
|
---|
7592 | ComAssertThrow(capabilities & MediumFormatCapabilities_CreateDynamic, E_FAIL);
|
---|
7593 |
|
---|
7594 | Assert(pTarget->m->state == MediumState_Creating);
|
---|
7595 | Assert(m->state == MediumState_LockedRead);
|
---|
7596 |
|
---|
7597 | PVBOXHDD hdd;
|
---|
7598 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
7599 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7600 |
|
---|
7601 | /* the two media are now protected by their non-default states;
|
---|
7602 | * unlock the media before the potentially lengthy operation */
|
---|
7603 | mediaLock.release();
|
---|
7604 |
|
---|
7605 | try
|
---|
7606 | {
|
---|
7607 | /* Open all media in the target chain but the last. */
|
---|
7608 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
7609 | task.mpMediumLockList->GetBegin();
|
---|
7610 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
7611 | task.mpMediumLockList->GetEnd();
|
---|
7612 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
7613 | it != targetListEnd;
|
---|
7614 | ++it)
|
---|
7615 | {
|
---|
7616 | const MediumLock &mediumLock = *it;
|
---|
7617 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
7618 |
|
---|
7619 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
7620 |
|
---|
7621 | /* Skip over the target diff medium */
|
---|
7622 | if (pMedium->m->state == MediumState_Creating)
|
---|
7623 | continue;
|
---|
7624 |
|
---|
7625 | /* sanity check */
|
---|
7626 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
7627 |
|
---|
7628 | /* Open all media in appropriate mode. */
|
---|
7629 | vrc = VDOpen(hdd,
|
---|
7630 | pMedium->m->strFormat.c_str(),
|
---|
7631 | pMedium->m->strLocationFull.c_str(),
|
---|
7632 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
7633 | pMedium->m->vdImageIfaces);
|
---|
7634 | if (RT_FAILURE(vrc))
|
---|
7635 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7636 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
7637 | pMedium->m->strLocationFull.c_str(),
|
---|
7638 | i_vdError(vrc).c_str());
|
---|
7639 | }
|
---|
7640 |
|
---|
7641 | /* ensure the target directory exists */
|
---|
7642 | if (capabilities & MediumFormatCapabilities_File)
|
---|
7643 | {
|
---|
7644 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
7645 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
7646 | if (FAILED(rc))
|
---|
7647 | throw rc;
|
---|
7648 | }
|
---|
7649 |
|
---|
7650 | vrc = VDCreateDiff(hdd,
|
---|
7651 | targetFormat.c_str(),
|
---|
7652 | targetLocation.c_str(),
|
---|
7653 | (task.mVariant & ~MediumVariant_NoCreateDir) | VD_IMAGE_FLAGS_DIFF,
|
---|
7654 | NULL,
|
---|
7655 | targetId.raw(),
|
---|
7656 | id.raw(),
|
---|
7657 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
7658 | pTarget->m->vdImageIfaces,
|
---|
7659 | task.mVDOperationIfaces);
|
---|
7660 | if (RT_FAILURE(vrc))
|
---|
7661 | {
|
---|
7662 | if (vrc == VERR_VD_INVALID_TYPE)
|
---|
7663 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7664 | tr("Parameters for creating the differencing medium storage unit '%s' are invalid%s"),
|
---|
7665 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
7666 | else
|
---|
7667 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7668 | tr("Could not create the differencing medium storage unit '%s'%s"),
|
---|
7669 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
7670 | }
|
---|
7671 |
|
---|
7672 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
7673 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
7674 | unsigned uImageFlags;
|
---|
7675 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
7676 | if (RT_SUCCESS(vrc))
|
---|
7677 | variant = (MediumVariant_T)uImageFlags;
|
---|
7678 | }
|
---|
7679 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
7680 |
|
---|
7681 | VDDestroy(hdd);
|
---|
7682 | }
|
---|
7683 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
7684 |
|
---|
7685 | MultiResult mrc(rcTmp);
|
---|
7686 |
|
---|
7687 | if (SUCCEEDED(mrc))
|
---|
7688 | {
|
---|
7689 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
7690 |
|
---|
7691 | Assert(pTarget->m->pParent.isNull());
|
---|
7692 |
|
---|
7693 | /* associate child with the parent, maximum depth was checked above */
|
---|
7694 | pTarget->i_setParent(this);
|
---|
7695 |
|
---|
7696 | /* diffs for immutable media are auto-reset by default */
|
---|
7697 | bool fAutoReset;
|
---|
7698 | {
|
---|
7699 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
7700 | AutoReadLock block(pBase COMMA_LOCKVAL_SRC_POS);
|
---|
7701 | fAutoReset = (pBase->m->type == MediumType_Immutable);
|
---|
7702 | }
|
---|
7703 | {
|
---|
7704 | AutoWriteLock tlock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
7705 | pTarget->m->autoReset = fAutoReset;
|
---|
7706 | }
|
---|
7707 |
|
---|
7708 | /* register with mVirtualBox as the last step and move to
|
---|
7709 | * Created state only on success (leaving an orphan file is
|
---|
7710 | * better than breaking media registry consistency) */
|
---|
7711 | ComObjPtr<Medium> pMedium;
|
---|
7712 | mrc = m->pVirtualBox->i_registerMedium(pTarget, &pMedium, treeLock);
|
---|
7713 | Assert(pTarget == pMedium);
|
---|
7714 |
|
---|
7715 | if (FAILED(mrc))
|
---|
7716 | /* break the parent association on failure to register */
|
---|
7717 | i_deparent();
|
---|
7718 | }
|
---|
7719 |
|
---|
7720 | AutoMultiWriteLock2 mediaLock(this, pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
7721 |
|
---|
7722 | if (SUCCEEDED(mrc))
|
---|
7723 | {
|
---|
7724 | pTarget->m->state = MediumState_Created;
|
---|
7725 |
|
---|
7726 | pTarget->m->size = size;
|
---|
7727 | pTarget->m->logicalSize = logicalSize;
|
---|
7728 | pTarget->m->variant = variant;
|
---|
7729 | }
|
---|
7730 | else
|
---|
7731 | {
|
---|
7732 | /* back to NotCreated on failure */
|
---|
7733 | pTarget->m->state = MediumState_NotCreated;
|
---|
7734 |
|
---|
7735 | pTarget->m->autoReset = false;
|
---|
7736 |
|
---|
7737 | /* reset UUID to prevent it from being reused next time */
|
---|
7738 | if (fGenerateUuid)
|
---|
7739 | unconst(pTarget->m->id).clear();
|
---|
7740 | }
|
---|
7741 |
|
---|
7742 | // deregister the task registered in createDiffStorage()
|
---|
7743 | Assert(m->numCreateDiffTasks != 0);
|
---|
7744 | --m->numCreateDiffTasks;
|
---|
7745 |
|
---|
7746 | mediaLock.release();
|
---|
7747 | i_markRegistriesModified();
|
---|
7748 | if (task.isAsync())
|
---|
7749 | {
|
---|
7750 | // in asynchronous mode, save settings now
|
---|
7751 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
7752 | }
|
---|
7753 |
|
---|
7754 | /* Note that in sync mode, it's the caller's responsibility to
|
---|
7755 | * unlock the medium. */
|
---|
7756 |
|
---|
7757 | return mrc;
|
---|
7758 | }
|
---|
7759 |
|
---|
7760 | /**
|
---|
7761 | * Implementation code for the "merge" task.
|
---|
7762 | *
|
---|
7763 | * This task always gets started from Medium::mergeTo() and can run
|
---|
7764 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
7765 | * that function. If we run synchronously, the caller expects the medium
|
---|
7766 | * registry modification to be set before returning; otherwise (in asynchronous
|
---|
7767 | * mode), we save the settings ourselves.
|
---|
7768 | *
|
---|
7769 | * @param task
|
---|
7770 | * @return
|
---|
7771 | */
|
---|
7772 | HRESULT Medium::i_taskMergeHandler(Medium::MergeTask &task)
|
---|
7773 | {
|
---|
7774 | HRESULT rcTmp = S_OK;
|
---|
7775 |
|
---|
7776 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
7777 |
|
---|
7778 | try
|
---|
7779 | {
|
---|
7780 | if (!task.mParentForTarget.isNull())
|
---|
7781 | if (task.mParentForTarget->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
7782 | {
|
---|
7783 | AutoReadLock plock(task.mParentForTarget COMMA_LOCKVAL_SRC_POS);
|
---|
7784 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7785 | tr("Cannot merge image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
7786 | task.mParentForTarget->m->strLocationFull.c_str());
|
---|
7787 | }
|
---|
7788 |
|
---|
7789 | PVBOXHDD hdd;
|
---|
7790 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
7791 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7792 |
|
---|
7793 | try
|
---|
7794 | {
|
---|
7795 | // Similar code appears in SessionMachine::onlineMergeMedium, so
|
---|
7796 | // if you make any changes below check whether they are applicable
|
---|
7797 | // in that context as well.
|
---|
7798 |
|
---|
7799 | unsigned uTargetIdx = VD_LAST_IMAGE;
|
---|
7800 | unsigned uSourceIdx = VD_LAST_IMAGE;
|
---|
7801 | /* Open all media in the chain. */
|
---|
7802 | MediumLockList::Base::iterator lockListBegin =
|
---|
7803 | task.mpMediumLockList->GetBegin();
|
---|
7804 | MediumLockList::Base::iterator lockListEnd =
|
---|
7805 | task.mpMediumLockList->GetEnd();
|
---|
7806 | unsigned i = 0;
|
---|
7807 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
7808 | it != lockListEnd;
|
---|
7809 | ++it)
|
---|
7810 | {
|
---|
7811 | MediumLock &mediumLock = *it;
|
---|
7812 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
7813 |
|
---|
7814 | if (pMedium == this)
|
---|
7815 | uSourceIdx = i;
|
---|
7816 | else if (pMedium == pTarget)
|
---|
7817 | uTargetIdx = i;
|
---|
7818 |
|
---|
7819 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
7820 |
|
---|
7821 | /*
|
---|
7822 | * complex sanity (sane complexity)
|
---|
7823 | *
|
---|
7824 | * The current medium must be in the Deleting (medium is merged)
|
---|
7825 | * or LockedRead (parent medium) state if it is not the target.
|
---|
7826 | * If it is the target it must be in the LockedWrite state.
|
---|
7827 | */
|
---|
7828 | Assert( ( pMedium != pTarget
|
---|
7829 | && ( pMedium->m->state == MediumState_Deleting
|
---|
7830 | || pMedium->m->state == MediumState_LockedRead))
|
---|
7831 | || ( pMedium == pTarget
|
---|
7832 | && pMedium->m->state == MediumState_LockedWrite));
|
---|
7833 |
|
---|
7834 | /*
|
---|
7835 | * Medium must be the target, in the LockedRead state
|
---|
7836 | * or Deleting state where it is not allowed to be attached
|
---|
7837 | * to a virtual machine.
|
---|
7838 | */
|
---|
7839 | Assert( pMedium == pTarget
|
---|
7840 | || pMedium->m->state == MediumState_LockedRead
|
---|
7841 | || ( pMedium->m->backRefs.size() == 0
|
---|
7842 | && pMedium->m->state == MediumState_Deleting));
|
---|
7843 | /* The source medium must be in Deleting state. */
|
---|
7844 | Assert( pMedium != this
|
---|
7845 | || pMedium->m->state == MediumState_Deleting);
|
---|
7846 |
|
---|
7847 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
7848 |
|
---|
7849 | if ( pMedium->m->state == MediumState_LockedRead
|
---|
7850 | || pMedium->m->state == MediumState_Deleting)
|
---|
7851 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
7852 | if (pMedium->m->type == MediumType_Shareable)
|
---|
7853 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
7854 |
|
---|
7855 | /* Open the medium */
|
---|
7856 | vrc = VDOpen(hdd,
|
---|
7857 | pMedium->m->strFormat.c_str(),
|
---|
7858 | pMedium->m->strLocationFull.c_str(),
|
---|
7859 | uOpenFlags | m->uOpenFlagsDef,
|
---|
7860 | pMedium->m->vdImageIfaces);
|
---|
7861 | if (RT_FAILURE(vrc))
|
---|
7862 | throw vrc;
|
---|
7863 |
|
---|
7864 | i++;
|
---|
7865 | }
|
---|
7866 |
|
---|
7867 | ComAssertThrow( uSourceIdx != VD_LAST_IMAGE
|
---|
7868 | && uTargetIdx != VD_LAST_IMAGE, E_FAIL);
|
---|
7869 |
|
---|
7870 | vrc = VDMerge(hdd, uSourceIdx, uTargetIdx,
|
---|
7871 | task.mVDOperationIfaces);
|
---|
7872 | if (RT_FAILURE(vrc))
|
---|
7873 | throw vrc;
|
---|
7874 |
|
---|
7875 | /* update parent UUIDs */
|
---|
7876 | if (!task.mfMergeForward)
|
---|
7877 | {
|
---|
7878 | /* we need to update UUIDs of all source's children
|
---|
7879 | * which cannot be part of the container at once so
|
---|
7880 | * add each one in there individually */
|
---|
7881 | if (task.mpChildrenToReparent)
|
---|
7882 | {
|
---|
7883 | MediumLockList::Base::iterator childrenBegin = task.mpChildrenToReparent->GetBegin();
|
---|
7884 | MediumLockList::Base::iterator childrenEnd = task.mpChildrenToReparent->GetEnd();
|
---|
7885 | for (MediumLockList::Base::iterator it = childrenBegin;
|
---|
7886 | it != childrenEnd;
|
---|
7887 | ++it)
|
---|
7888 | {
|
---|
7889 | Medium *pMedium = it->GetMedium();
|
---|
7890 | /* VD_OPEN_FLAGS_INFO since UUID is wrong yet */
|
---|
7891 | vrc = VDOpen(hdd,
|
---|
7892 | pMedium->m->strFormat.c_str(),
|
---|
7893 | pMedium->m->strLocationFull.c_str(),
|
---|
7894 | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
7895 | pMedium->m->vdImageIfaces);
|
---|
7896 | if (RT_FAILURE(vrc))
|
---|
7897 | throw vrc;
|
---|
7898 |
|
---|
7899 | vrc = VDSetParentUuid(hdd, VD_LAST_IMAGE,
|
---|
7900 | pTarget->m->id.raw());
|
---|
7901 | if (RT_FAILURE(vrc))
|
---|
7902 | throw vrc;
|
---|
7903 |
|
---|
7904 | vrc = VDClose(hdd, false /* fDelete */);
|
---|
7905 | if (RT_FAILURE(vrc))
|
---|
7906 | throw vrc;
|
---|
7907 | }
|
---|
7908 | }
|
---|
7909 | }
|
---|
7910 | }
|
---|
7911 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
7912 | catch (int aVRC)
|
---|
7913 | {
|
---|
7914 | rcTmp = setError(VBOX_E_FILE_ERROR,
|
---|
7915 | tr("Could not merge the medium '%s' to '%s'%s"),
|
---|
7916 | m->strLocationFull.c_str(),
|
---|
7917 | pTarget->m->strLocationFull.c_str(),
|
---|
7918 | i_vdError(aVRC).c_str());
|
---|
7919 | }
|
---|
7920 |
|
---|
7921 | VDDestroy(hdd);
|
---|
7922 | }
|
---|
7923 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
7924 |
|
---|
7925 | ErrorInfoKeeper eik;
|
---|
7926 | MultiResult mrc(rcTmp);
|
---|
7927 | HRESULT rc2;
|
---|
7928 |
|
---|
7929 | if (SUCCEEDED(mrc))
|
---|
7930 | {
|
---|
7931 | /* all media but the target were successfully deleted by
|
---|
7932 | * VDMerge; reparent the last one and uninitialize deleted media. */
|
---|
7933 |
|
---|
7934 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
7935 |
|
---|
7936 | if (task.mfMergeForward)
|
---|
7937 | {
|
---|
7938 | /* first, unregister the target since it may become a base
|
---|
7939 | * medium which needs re-registration */
|
---|
7940 | rc2 = m->pVirtualBox->i_unregisterMedium(pTarget);
|
---|
7941 | AssertComRC(rc2);
|
---|
7942 |
|
---|
7943 | /* then, reparent it and disconnect the deleted branch at both ends
|
---|
7944 | * (chain->parent() is source's parent). Depth check above. */
|
---|
7945 | pTarget->i_deparent();
|
---|
7946 | pTarget->i_setParent(task.mParentForTarget);
|
---|
7947 | if (task.mParentForTarget)
|
---|
7948 | i_deparent();
|
---|
7949 |
|
---|
7950 | /* then, register again */
|
---|
7951 | ComObjPtr<Medium> pMedium;
|
---|
7952 | rc2 = m->pVirtualBox->i_registerMedium(pTarget, &pMedium,
|
---|
7953 | treeLock);
|
---|
7954 | AssertComRC(rc2);
|
---|
7955 | }
|
---|
7956 | else
|
---|
7957 | {
|
---|
7958 | Assert(pTarget->i_getChildren().size() == 1);
|
---|
7959 | Medium *targetChild = pTarget->i_getChildren().front();
|
---|
7960 |
|
---|
7961 | /* disconnect the deleted branch at the elder end */
|
---|
7962 | targetChild->i_deparent();
|
---|
7963 |
|
---|
7964 | /* reparent source's children and disconnect the deleted
|
---|
7965 | * branch at the younger end */
|
---|
7966 | if (task.mpChildrenToReparent)
|
---|
7967 | {
|
---|
7968 | /* obey {parent,child} lock order */
|
---|
7969 | AutoWriteLock sourceLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7970 |
|
---|
7971 | MediumLockList::Base::iterator childrenBegin = task.mpChildrenToReparent->GetBegin();
|
---|
7972 | MediumLockList::Base::iterator childrenEnd = task.mpChildrenToReparent->GetEnd();
|
---|
7973 | for (MediumLockList::Base::iterator it = childrenBegin;
|
---|
7974 | it != childrenEnd;
|
---|
7975 | ++it)
|
---|
7976 | {
|
---|
7977 | Medium *pMedium = it->GetMedium();
|
---|
7978 | AutoWriteLock childLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
7979 |
|
---|
7980 | pMedium->i_deparent(); // removes pMedium from source
|
---|
7981 | // no depth check, reduces depth
|
---|
7982 | pMedium->i_setParent(pTarget);
|
---|
7983 | }
|
---|
7984 | }
|
---|
7985 | }
|
---|
7986 |
|
---|
7987 | /* unregister and uninitialize all media removed by the merge */
|
---|
7988 | MediumLockList::Base::iterator lockListBegin =
|
---|
7989 | task.mpMediumLockList->GetBegin();
|
---|
7990 | MediumLockList::Base::iterator lockListEnd =
|
---|
7991 | task.mpMediumLockList->GetEnd();
|
---|
7992 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
7993 | it != lockListEnd;
|
---|
7994 | )
|
---|
7995 | {
|
---|
7996 | MediumLock &mediumLock = *it;
|
---|
7997 | /* Create a real copy of the medium pointer, as the medium
|
---|
7998 | * lock deletion below would invalidate the referenced object. */
|
---|
7999 | const ComObjPtr<Medium> pMedium = mediumLock.GetMedium();
|
---|
8000 |
|
---|
8001 | /* The target and all media not merged (readonly) are skipped */
|
---|
8002 | if ( pMedium == pTarget
|
---|
8003 | || pMedium->m->state == MediumState_LockedRead)
|
---|
8004 | {
|
---|
8005 | ++it;
|
---|
8006 | continue;
|
---|
8007 | }
|
---|
8008 |
|
---|
8009 | rc2 = pMedium->m->pVirtualBox->i_unregisterMedium(pMedium);
|
---|
8010 | AssertComRC(rc2);
|
---|
8011 |
|
---|
8012 | /* now, uninitialize the deleted medium (note that
|
---|
8013 | * due to the Deleting state, uninit() will not touch
|
---|
8014 | * the parent-child relationship so we need to
|
---|
8015 | * uninitialize each disk individually) */
|
---|
8016 |
|
---|
8017 | /* note that the operation initiator medium (which is
|
---|
8018 | * normally also the source medium) is a special case
|
---|
8019 | * -- there is one more caller added by Task to it which
|
---|
8020 | * we must release. Also, if we are in sync mode, the
|
---|
8021 | * caller may still hold an AutoCaller instance for it
|
---|
8022 | * and therefore we cannot uninit() it (it's therefore
|
---|
8023 | * the caller's responsibility) */
|
---|
8024 | if (pMedium == this)
|
---|
8025 | {
|
---|
8026 | Assert(i_getChildren().size() == 0);
|
---|
8027 | Assert(m->backRefs.size() == 0);
|
---|
8028 | task.mMediumCaller.release();
|
---|
8029 | }
|
---|
8030 |
|
---|
8031 | /* Delete the medium lock list entry, which also releases the
|
---|
8032 | * caller added by MergeChain before uninit() and updates the
|
---|
8033 | * iterator to point to the right place. */
|
---|
8034 | rc2 = task.mpMediumLockList->RemoveByIterator(it);
|
---|
8035 | AssertComRC(rc2);
|
---|
8036 |
|
---|
8037 | if (task.isAsync() || pMedium != this)
|
---|
8038 | {
|
---|
8039 | treeLock.release();
|
---|
8040 | pMedium->uninit();
|
---|
8041 | treeLock.acquire();
|
---|
8042 | }
|
---|
8043 | }
|
---|
8044 | }
|
---|
8045 |
|
---|
8046 | i_markRegistriesModified();
|
---|
8047 | if (task.isAsync())
|
---|
8048 | {
|
---|
8049 | // in asynchronous mode, save settings now
|
---|
8050 | eik.restore();
|
---|
8051 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
8052 | eik.fetch();
|
---|
8053 | }
|
---|
8054 |
|
---|
8055 | if (FAILED(mrc))
|
---|
8056 | {
|
---|
8057 | /* Here we come if either VDMerge() failed (in which case we
|
---|
8058 | * assume that it tried to do everything to make a further
|
---|
8059 | * retry possible -- e.g. not deleted intermediate media
|
---|
8060 | * and so on) or VirtualBox::saveRegistries() failed (where we
|
---|
8061 | * should have the original tree but with intermediate storage
|
---|
8062 | * units deleted by VDMerge()). We have to only restore states
|
---|
8063 | * (through the MergeChain dtor) unless we are run synchronously
|
---|
8064 | * in which case it's the responsibility of the caller as stated
|
---|
8065 | * in the mergeTo() docs. The latter also implies that we
|
---|
8066 | * don't own the merge chain, so release it in this case. */
|
---|
8067 | if (task.isAsync())
|
---|
8068 | i_cancelMergeTo(task.mpChildrenToReparent, task.mpMediumLockList);
|
---|
8069 | }
|
---|
8070 |
|
---|
8071 | return mrc;
|
---|
8072 | }
|
---|
8073 |
|
---|
8074 | /**
|
---|
8075 | * Implementation code for the "clone" task.
|
---|
8076 | *
|
---|
8077 | * This only gets started from Medium::CloneTo() and always runs asynchronously.
|
---|
8078 | * As a result, we always save the VirtualBox.xml file when we're done here.
|
---|
8079 | *
|
---|
8080 | * @param task
|
---|
8081 | * @return
|
---|
8082 | */
|
---|
8083 | HRESULT Medium::i_taskCloneHandler(Medium::CloneTask &task)
|
---|
8084 | {
|
---|
8085 | HRESULT rcTmp = S_OK;
|
---|
8086 |
|
---|
8087 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
8088 | const ComObjPtr<Medium> &pParent = task.mParent;
|
---|
8089 |
|
---|
8090 | bool fCreatingTarget = false;
|
---|
8091 |
|
---|
8092 | uint64_t size = 0, logicalSize = 0;
|
---|
8093 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
8094 | bool fGenerateUuid = false;
|
---|
8095 |
|
---|
8096 | try
|
---|
8097 | {
|
---|
8098 | if (!pParent.isNull())
|
---|
8099 | if (pParent->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
8100 | {
|
---|
8101 | AutoReadLock plock(pParent COMMA_LOCKVAL_SRC_POS);
|
---|
8102 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8103 | tr("Cannot clone image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
8104 | pParent->m->strLocationFull.c_str());
|
---|
8105 | }
|
---|
8106 |
|
---|
8107 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
8108 | * signal from the task initiator (which releases it only after
|
---|
8109 | * RTThreadCreate()) that we can start the job. */
|
---|
8110 | AutoMultiWriteLock3 thisLock(this, pTarget, pParent COMMA_LOCKVAL_SRC_POS);
|
---|
8111 |
|
---|
8112 | fCreatingTarget = pTarget->m->state == MediumState_Creating;
|
---|
8113 |
|
---|
8114 | /* The object may request a specific UUID (through a special form of
|
---|
8115 | * the setLocation() argument). Otherwise we have to generate it */
|
---|
8116 | Guid targetId = pTarget->m->id;
|
---|
8117 |
|
---|
8118 | fGenerateUuid = targetId.isZero();
|
---|
8119 | if (fGenerateUuid)
|
---|
8120 | {
|
---|
8121 | targetId.create();
|
---|
8122 | /* VirtualBox::registerMedium() will need UUID */
|
---|
8123 | unconst(pTarget->m->id) = targetId;
|
---|
8124 | }
|
---|
8125 |
|
---|
8126 | PVBOXHDD hdd;
|
---|
8127 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8128 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8129 |
|
---|
8130 | try
|
---|
8131 | {
|
---|
8132 | /* Open all media in the source chain. */
|
---|
8133 | MediumLockList::Base::const_iterator sourceListBegin =
|
---|
8134 | task.mpSourceMediumLockList->GetBegin();
|
---|
8135 | MediumLockList::Base::const_iterator sourceListEnd =
|
---|
8136 | task.mpSourceMediumLockList->GetEnd();
|
---|
8137 | for (MediumLockList::Base::const_iterator it = sourceListBegin;
|
---|
8138 | it != sourceListEnd;
|
---|
8139 | ++it)
|
---|
8140 | {
|
---|
8141 | const MediumLock &mediumLock = *it;
|
---|
8142 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8143 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8144 |
|
---|
8145 | /* sanity check */
|
---|
8146 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
8147 |
|
---|
8148 | /** Open all media in read-only mode. */
|
---|
8149 | vrc = VDOpen(hdd,
|
---|
8150 | pMedium->m->strFormat.c_str(),
|
---|
8151 | pMedium->m->strLocationFull.c_str(),
|
---|
8152 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
8153 | pMedium->m->vdImageIfaces);
|
---|
8154 | if (RT_FAILURE(vrc))
|
---|
8155 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8156 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8157 | pMedium->m->strLocationFull.c_str(),
|
---|
8158 | i_vdError(vrc).c_str());
|
---|
8159 | }
|
---|
8160 |
|
---|
8161 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
8162 | Utf8Str targetLocation(pTarget->m->strLocationFull);
|
---|
8163 | uint64_t capabilities = pTarget->m->formatObj->i_getCapabilities();
|
---|
8164 |
|
---|
8165 | Assert( pTarget->m->state == MediumState_Creating
|
---|
8166 | || pTarget->m->state == MediumState_LockedWrite);
|
---|
8167 | Assert(m->state == MediumState_LockedRead);
|
---|
8168 | Assert( pParent.isNull()
|
---|
8169 | || pParent->m->state == MediumState_LockedRead);
|
---|
8170 |
|
---|
8171 | /* unlock before the potentially lengthy operation */
|
---|
8172 | thisLock.release();
|
---|
8173 |
|
---|
8174 | /* ensure the target directory exists */
|
---|
8175 | if (capabilities & MediumFormatCapabilities_File)
|
---|
8176 | {
|
---|
8177 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
8178 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
8179 | if (FAILED(rc))
|
---|
8180 | throw rc;
|
---|
8181 | }
|
---|
8182 |
|
---|
8183 | PVBOXHDD targetHdd;
|
---|
8184 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &targetHdd);
|
---|
8185 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8186 |
|
---|
8187 | try
|
---|
8188 | {
|
---|
8189 | /* Open all media in the target chain. */
|
---|
8190 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
8191 | task.mpTargetMediumLockList->GetBegin();
|
---|
8192 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
8193 | task.mpTargetMediumLockList->GetEnd();
|
---|
8194 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
8195 | it != targetListEnd;
|
---|
8196 | ++it)
|
---|
8197 | {
|
---|
8198 | const MediumLock &mediumLock = *it;
|
---|
8199 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8200 |
|
---|
8201 | /* If the target medium is not created yet there's no
|
---|
8202 | * reason to open it. */
|
---|
8203 | if (pMedium == pTarget && fCreatingTarget)
|
---|
8204 | continue;
|
---|
8205 |
|
---|
8206 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8207 |
|
---|
8208 | /* sanity check */
|
---|
8209 | Assert( pMedium->m->state == MediumState_LockedRead
|
---|
8210 | || pMedium->m->state == MediumState_LockedWrite);
|
---|
8211 |
|
---|
8212 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
8213 | if (pMedium->m->state != MediumState_LockedWrite)
|
---|
8214 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
8215 | if (pMedium->m->type == MediumType_Shareable)
|
---|
8216 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
8217 |
|
---|
8218 | /* Open all media in appropriate mode. */
|
---|
8219 | vrc = VDOpen(targetHdd,
|
---|
8220 | pMedium->m->strFormat.c_str(),
|
---|
8221 | pMedium->m->strLocationFull.c_str(),
|
---|
8222 | uOpenFlags | m->uOpenFlagsDef,
|
---|
8223 | pMedium->m->vdImageIfaces);
|
---|
8224 | if (RT_FAILURE(vrc))
|
---|
8225 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8226 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8227 | pMedium->m->strLocationFull.c_str(),
|
---|
8228 | i_vdError(vrc).c_str());
|
---|
8229 | }
|
---|
8230 |
|
---|
8231 | /* target isn't locked, but no changing data is accessed */
|
---|
8232 | if (task.midxSrcImageSame == UINT32_MAX)
|
---|
8233 | {
|
---|
8234 | vrc = VDCopy(hdd,
|
---|
8235 | VD_LAST_IMAGE,
|
---|
8236 | targetHdd,
|
---|
8237 | targetFormat.c_str(),
|
---|
8238 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
8239 | false /* fMoveByRename */,
|
---|
8240 | 0 /* cbSize */,
|
---|
8241 | task.mVariant & ~MediumVariant_NoCreateDir,
|
---|
8242 | targetId.raw(),
|
---|
8243 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
8244 | NULL /* pVDIfsOperation */,
|
---|
8245 | pTarget->m->vdImageIfaces,
|
---|
8246 | task.mVDOperationIfaces);
|
---|
8247 | }
|
---|
8248 | else
|
---|
8249 | {
|
---|
8250 | vrc = VDCopyEx(hdd,
|
---|
8251 | VD_LAST_IMAGE,
|
---|
8252 | targetHdd,
|
---|
8253 | targetFormat.c_str(),
|
---|
8254 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
8255 | false /* fMoveByRename */,
|
---|
8256 | 0 /* cbSize */,
|
---|
8257 | task.midxSrcImageSame,
|
---|
8258 | task.midxDstImageSame,
|
---|
8259 | task.mVariant & ~MediumVariant_NoCreateDir,
|
---|
8260 | targetId.raw(),
|
---|
8261 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
8262 | NULL /* pVDIfsOperation */,
|
---|
8263 | pTarget->m->vdImageIfaces,
|
---|
8264 | task.mVDOperationIfaces);
|
---|
8265 | }
|
---|
8266 | if (RT_FAILURE(vrc))
|
---|
8267 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8268 | tr("Could not create the clone medium '%s'%s"),
|
---|
8269 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
8270 |
|
---|
8271 | size = VDGetFileSize(targetHdd, VD_LAST_IMAGE);
|
---|
8272 | logicalSize = VDGetSize(targetHdd, VD_LAST_IMAGE);
|
---|
8273 | unsigned uImageFlags;
|
---|
8274 | vrc = VDGetImageFlags(targetHdd, 0, &uImageFlags);
|
---|
8275 | if (RT_SUCCESS(vrc))
|
---|
8276 | variant = (MediumVariant_T)uImageFlags;
|
---|
8277 | }
|
---|
8278 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8279 |
|
---|
8280 | VDDestroy(targetHdd);
|
---|
8281 | }
|
---|
8282 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8283 |
|
---|
8284 | VDDestroy(hdd);
|
---|
8285 | }
|
---|
8286 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8287 |
|
---|
8288 | ErrorInfoKeeper eik;
|
---|
8289 | MultiResult mrc(rcTmp);
|
---|
8290 |
|
---|
8291 | /* Only do the parent changes for newly created media. */
|
---|
8292 | if (SUCCEEDED(mrc) && fCreatingTarget)
|
---|
8293 | {
|
---|
8294 | /* we set mParent & children() */
|
---|
8295 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
8296 |
|
---|
8297 | Assert(pTarget->m->pParent.isNull());
|
---|
8298 |
|
---|
8299 | if (pParent)
|
---|
8300 | {
|
---|
8301 | /* Associate the clone with the parent and deassociate
|
---|
8302 | * from VirtualBox. Depth check above. */
|
---|
8303 | pTarget->i_setParent(pParent);
|
---|
8304 |
|
---|
8305 | /* register with mVirtualBox as the last step and move to
|
---|
8306 | * Created state only on success (leaving an orphan file is
|
---|
8307 | * better than breaking media registry consistency) */
|
---|
8308 | eik.restore();
|
---|
8309 | ComObjPtr<Medium> pMedium;
|
---|
8310 | mrc = pParent->m->pVirtualBox->i_registerMedium(pTarget, &pMedium,
|
---|
8311 | treeLock);
|
---|
8312 | Assert( FAILED(mrc)
|
---|
8313 | || pTarget == pMedium);
|
---|
8314 | eik.fetch();
|
---|
8315 |
|
---|
8316 | if (FAILED(mrc))
|
---|
8317 | /* break parent association on failure to register */
|
---|
8318 | pTarget->i_deparent(); // removes target from parent
|
---|
8319 | }
|
---|
8320 | else
|
---|
8321 | {
|
---|
8322 | /* just register */
|
---|
8323 | eik.restore();
|
---|
8324 | ComObjPtr<Medium> pMedium;
|
---|
8325 | mrc = m->pVirtualBox->i_registerMedium(pTarget, &pMedium,
|
---|
8326 | treeLock);
|
---|
8327 | Assert( FAILED(mrc)
|
---|
8328 | || pTarget == pMedium);
|
---|
8329 | eik.fetch();
|
---|
8330 | }
|
---|
8331 | }
|
---|
8332 |
|
---|
8333 | if (fCreatingTarget)
|
---|
8334 | {
|
---|
8335 | AutoWriteLock mLock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8336 |
|
---|
8337 | if (SUCCEEDED(mrc))
|
---|
8338 | {
|
---|
8339 | pTarget->m->state = MediumState_Created;
|
---|
8340 |
|
---|
8341 | pTarget->m->size = size;
|
---|
8342 | pTarget->m->logicalSize = logicalSize;
|
---|
8343 | pTarget->m->variant = variant;
|
---|
8344 | }
|
---|
8345 | else
|
---|
8346 | {
|
---|
8347 | /* back to NotCreated on failure */
|
---|
8348 | pTarget->m->state = MediumState_NotCreated;
|
---|
8349 |
|
---|
8350 | /* reset UUID to prevent it from being reused next time */
|
---|
8351 | if (fGenerateUuid)
|
---|
8352 | unconst(pTarget->m->id).clear();
|
---|
8353 | }
|
---|
8354 | }
|
---|
8355 |
|
---|
8356 | /* Copy any filter related settings over to the target. */
|
---|
8357 | if (SUCCEEDED(mrc))
|
---|
8358 | {
|
---|
8359 | /* Copy any filter related settings over. */
|
---|
8360 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
8361 | ComObjPtr<Medium> pTargetBase = pTarget->i_getBase();
|
---|
8362 | std::vector<com::Utf8Str> aFilterPropNames;
|
---|
8363 | std::vector<com::Utf8Str> aFilterPropValues;
|
---|
8364 | mrc = pBase->i_getFilterProperties(aFilterPropNames, aFilterPropValues);
|
---|
8365 | if (SUCCEEDED(mrc))
|
---|
8366 | {
|
---|
8367 | /* Go through the properties and add them to the target medium. */
|
---|
8368 | for (unsigned idx = 0; idx < aFilterPropNames.size(); idx++)
|
---|
8369 | {
|
---|
8370 | mrc = pTargetBase->i_setPropertyDirect(aFilterPropNames[idx], aFilterPropValues[idx]);
|
---|
8371 | if (FAILED(mrc)) break;
|
---|
8372 | }
|
---|
8373 |
|
---|
8374 | // now, at the end of this task (always asynchronous), save the settings
|
---|
8375 | if (SUCCEEDED(mrc))
|
---|
8376 | {
|
---|
8377 | // save the settings
|
---|
8378 | i_markRegistriesModified();
|
---|
8379 | /* collect multiple errors */
|
---|
8380 | eik.restore();
|
---|
8381 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
8382 | eik.fetch();
|
---|
8383 | }
|
---|
8384 | }
|
---|
8385 | }
|
---|
8386 |
|
---|
8387 | /* Everything is explicitly unlocked when the task exits,
|
---|
8388 | * as the task destruction also destroys the source chain. */
|
---|
8389 |
|
---|
8390 | /* Make sure the source chain is released early. It could happen
|
---|
8391 | * that we get a deadlock in Appliance::Import when Medium::Close
|
---|
8392 | * is called & the source chain is released at the same time. */
|
---|
8393 | task.mpSourceMediumLockList->Clear();
|
---|
8394 |
|
---|
8395 | return mrc;
|
---|
8396 | }
|
---|
8397 |
|
---|
8398 | /**
|
---|
8399 | * Implementation code for the "delete" task.
|
---|
8400 | *
|
---|
8401 | * This task always gets started from Medium::deleteStorage() and can run
|
---|
8402 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
8403 | * that function.
|
---|
8404 | *
|
---|
8405 | * @param task
|
---|
8406 | * @return
|
---|
8407 | */
|
---|
8408 | HRESULT Medium::i_taskDeleteHandler(Medium::DeleteTask &task)
|
---|
8409 | {
|
---|
8410 | NOREF(task);
|
---|
8411 | HRESULT rc = S_OK;
|
---|
8412 |
|
---|
8413 | try
|
---|
8414 | {
|
---|
8415 | /* The lock is also used as a signal from the task initiator (which
|
---|
8416 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
8417 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8418 |
|
---|
8419 | PVBOXHDD hdd;
|
---|
8420 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8421 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8422 |
|
---|
8423 | Utf8Str format(m->strFormat);
|
---|
8424 | Utf8Str location(m->strLocationFull);
|
---|
8425 |
|
---|
8426 | /* unlock before the potentially lengthy operation */
|
---|
8427 | Assert(m->state == MediumState_Deleting);
|
---|
8428 | thisLock.release();
|
---|
8429 |
|
---|
8430 | try
|
---|
8431 | {
|
---|
8432 | vrc = VDOpen(hdd,
|
---|
8433 | format.c_str(),
|
---|
8434 | location.c_str(),
|
---|
8435 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
8436 | m->vdImageIfaces);
|
---|
8437 | if (RT_SUCCESS(vrc))
|
---|
8438 | vrc = VDClose(hdd, true /* fDelete */);
|
---|
8439 |
|
---|
8440 | if (RT_FAILURE(vrc))
|
---|
8441 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8442 | tr("Could not delete the medium storage unit '%s'%s"),
|
---|
8443 | location.c_str(), i_vdError(vrc).c_str());
|
---|
8444 |
|
---|
8445 | }
|
---|
8446 | catch (HRESULT aRC) { rc = aRC; }
|
---|
8447 |
|
---|
8448 | VDDestroy(hdd);
|
---|
8449 | }
|
---|
8450 | catch (HRESULT aRC) { rc = aRC; }
|
---|
8451 |
|
---|
8452 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8453 |
|
---|
8454 | /* go to the NotCreated state even on failure since the storage
|
---|
8455 | * may have been already partially deleted and cannot be used any
|
---|
8456 | * more. One will be able to manually re-open the storage if really
|
---|
8457 | * needed to re-register it. */
|
---|
8458 | m->state = MediumState_NotCreated;
|
---|
8459 |
|
---|
8460 | /* Reset UUID to prevent Create* from reusing it again */
|
---|
8461 | unconst(m->id).clear();
|
---|
8462 |
|
---|
8463 | return rc;
|
---|
8464 | }
|
---|
8465 |
|
---|
8466 | /**
|
---|
8467 | * Implementation code for the "reset" task.
|
---|
8468 | *
|
---|
8469 | * This always gets started asynchronously from Medium::Reset().
|
---|
8470 | *
|
---|
8471 | * @param task
|
---|
8472 | * @return
|
---|
8473 | */
|
---|
8474 | HRESULT Medium::i_taskResetHandler(Medium::ResetTask &task)
|
---|
8475 | {
|
---|
8476 | HRESULT rc = S_OK;
|
---|
8477 |
|
---|
8478 | uint64_t size = 0, logicalSize = 0;
|
---|
8479 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
8480 |
|
---|
8481 | try
|
---|
8482 | {
|
---|
8483 | /* The lock is also used as a signal from the task initiator (which
|
---|
8484 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
8485 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8486 |
|
---|
8487 | /// @todo Below we use a pair of delete/create operations to reset
|
---|
8488 | /// the diff contents but the most efficient way will of course be
|
---|
8489 | /// to add a VDResetDiff() API call
|
---|
8490 |
|
---|
8491 | PVBOXHDD hdd;
|
---|
8492 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8493 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8494 |
|
---|
8495 | Guid id = m->id;
|
---|
8496 | Utf8Str format(m->strFormat);
|
---|
8497 | Utf8Str location(m->strLocationFull);
|
---|
8498 |
|
---|
8499 | Medium *pParent = m->pParent;
|
---|
8500 | Guid parentId = pParent->m->id;
|
---|
8501 | Utf8Str parentFormat(pParent->m->strFormat);
|
---|
8502 | Utf8Str parentLocation(pParent->m->strLocationFull);
|
---|
8503 |
|
---|
8504 | Assert(m->state == MediumState_LockedWrite);
|
---|
8505 |
|
---|
8506 | /* unlock before the potentially lengthy operation */
|
---|
8507 | thisLock.release();
|
---|
8508 |
|
---|
8509 | try
|
---|
8510 | {
|
---|
8511 | /* Open all media in the target chain but the last. */
|
---|
8512 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
8513 | task.mpMediumLockList->GetBegin();
|
---|
8514 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
8515 | task.mpMediumLockList->GetEnd();
|
---|
8516 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
8517 | it != targetListEnd;
|
---|
8518 | ++it)
|
---|
8519 | {
|
---|
8520 | const MediumLock &mediumLock = *it;
|
---|
8521 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8522 |
|
---|
8523 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8524 |
|
---|
8525 | /* sanity check, "this" is checked above */
|
---|
8526 | Assert( pMedium == this
|
---|
8527 | || pMedium->m->state == MediumState_LockedRead);
|
---|
8528 |
|
---|
8529 | /* Open all media in appropriate mode. */
|
---|
8530 | vrc = VDOpen(hdd,
|
---|
8531 | pMedium->m->strFormat.c_str(),
|
---|
8532 | pMedium->m->strLocationFull.c_str(),
|
---|
8533 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
8534 | pMedium->m->vdImageIfaces);
|
---|
8535 | if (RT_FAILURE(vrc))
|
---|
8536 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8537 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8538 | pMedium->m->strLocationFull.c_str(),
|
---|
8539 | i_vdError(vrc).c_str());
|
---|
8540 |
|
---|
8541 | /* Done when we hit the media which should be reset */
|
---|
8542 | if (pMedium == this)
|
---|
8543 | break;
|
---|
8544 | }
|
---|
8545 |
|
---|
8546 | /* first, delete the storage unit */
|
---|
8547 | vrc = VDClose(hdd, true /* fDelete */);
|
---|
8548 | if (RT_FAILURE(vrc))
|
---|
8549 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8550 | tr("Could not delete the medium storage unit '%s'%s"),
|
---|
8551 | location.c_str(), i_vdError(vrc).c_str());
|
---|
8552 |
|
---|
8553 | /* next, create it again */
|
---|
8554 | vrc = VDOpen(hdd,
|
---|
8555 | parentFormat.c_str(),
|
---|
8556 | parentLocation.c_str(),
|
---|
8557 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
8558 | m->vdImageIfaces);
|
---|
8559 | if (RT_FAILURE(vrc))
|
---|
8560 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8561 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8562 | parentLocation.c_str(), i_vdError(vrc).c_str());
|
---|
8563 |
|
---|
8564 | vrc = VDCreateDiff(hdd,
|
---|
8565 | format.c_str(),
|
---|
8566 | location.c_str(),
|
---|
8567 | /// @todo use the same medium variant as before
|
---|
8568 | VD_IMAGE_FLAGS_NONE,
|
---|
8569 | NULL,
|
---|
8570 | id.raw(),
|
---|
8571 | parentId.raw(),
|
---|
8572 | VD_OPEN_FLAGS_NORMAL,
|
---|
8573 | m->vdImageIfaces,
|
---|
8574 | task.mVDOperationIfaces);
|
---|
8575 | if (RT_FAILURE(vrc))
|
---|
8576 | {
|
---|
8577 | if (vrc == VERR_VD_INVALID_TYPE)
|
---|
8578 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8579 | tr("Parameters for creating the differencing medium storage unit '%s' are invalid%s"),
|
---|
8580 | location.c_str(), i_vdError(vrc).c_str());
|
---|
8581 | else
|
---|
8582 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8583 | tr("Could not create the differencing medium storage unit '%s'%s"),
|
---|
8584 | location.c_str(), i_vdError(vrc).c_str());
|
---|
8585 | }
|
---|
8586 |
|
---|
8587 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
8588 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
8589 | unsigned uImageFlags;
|
---|
8590 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
8591 | if (RT_SUCCESS(vrc))
|
---|
8592 | variant = (MediumVariant_T)uImageFlags;
|
---|
8593 | }
|
---|
8594 | catch (HRESULT aRC) { rc = aRC; }
|
---|
8595 |
|
---|
8596 | VDDestroy(hdd);
|
---|
8597 | }
|
---|
8598 | catch (HRESULT aRC) { rc = aRC; }
|
---|
8599 |
|
---|
8600 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8601 |
|
---|
8602 | m->size = size;
|
---|
8603 | m->logicalSize = logicalSize;
|
---|
8604 | m->variant = variant;
|
---|
8605 |
|
---|
8606 | /* Everything is explicitly unlocked when the task exits,
|
---|
8607 | * as the task destruction also destroys the media chain. */
|
---|
8608 |
|
---|
8609 | return rc;
|
---|
8610 | }
|
---|
8611 |
|
---|
8612 | /**
|
---|
8613 | * Implementation code for the "compact" task.
|
---|
8614 | *
|
---|
8615 | * @param task
|
---|
8616 | * @return
|
---|
8617 | */
|
---|
8618 | HRESULT Medium::i_taskCompactHandler(Medium::CompactTask &task)
|
---|
8619 | {
|
---|
8620 | HRESULT rc = S_OK;
|
---|
8621 |
|
---|
8622 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
8623 | * signal from the task initiator (which releases it only after
|
---|
8624 | * RTThreadCreate()) that we can start the job. */
|
---|
8625 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8626 |
|
---|
8627 | try
|
---|
8628 | {
|
---|
8629 | PVBOXHDD hdd;
|
---|
8630 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8631 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8632 |
|
---|
8633 | try
|
---|
8634 | {
|
---|
8635 | /* Open all media in the chain. */
|
---|
8636 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
8637 | task.mpMediumLockList->GetBegin();
|
---|
8638 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
8639 | task.mpMediumLockList->GetEnd();
|
---|
8640 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
8641 | mediumListEnd;
|
---|
8642 | --mediumListLast;
|
---|
8643 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
8644 | it != mediumListEnd;
|
---|
8645 | ++it)
|
---|
8646 | {
|
---|
8647 | const MediumLock &mediumLock = *it;
|
---|
8648 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8649 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8650 |
|
---|
8651 | /* sanity check */
|
---|
8652 | if (it == mediumListLast)
|
---|
8653 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
8654 | else
|
---|
8655 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
8656 |
|
---|
8657 | /* Open all media but last in read-only mode. Do not handle
|
---|
8658 | * shareable media, as compaction and sharing are mutually
|
---|
8659 | * exclusive. */
|
---|
8660 | vrc = VDOpen(hdd,
|
---|
8661 | pMedium->m->strFormat.c_str(),
|
---|
8662 | pMedium->m->strLocationFull.c_str(),
|
---|
8663 | m->uOpenFlagsDef | (it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
8664 | pMedium->m->vdImageIfaces);
|
---|
8665 | if (RT_FAILURE(vrc))
|
---|
8666 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8667 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8668 | pMedium->m->strLocationFull.c_str(),
|
---|
8669 | i_vdError(vrc).c_str());
|
---|
8670 | }
|
---|
8671 |
|
---|
8672 | Assert(m->state == MediumState_LockedWrite);
|
---|
8673 |
|
---|
8674 | Utf8Str location(m->strLocationFull);
|
---|
8675 |
|
---|
8676 | /* unlock before the potentially lengthy operation */
|
---|
8677 | thisLock.release();
|
---|
8678 |
|
---|
8679 | vrc = VDCompact(hdd, VD_LAST_IMAGE, task.mVDOperationIfaces);
|
---|
8680 | if (RT_FAILURE(vrc))
|
---|
8681 | {
|
---|
8682 | if (vrc == VERR_NOT_SUPPORTED)
|
---|
8683 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
8684 | tr("Compacting is not yet supported for medium '%s'"),
|
---|
8685 | location.c_str());
|
---|
8686 | else if (vrc == VERR_NOT_IMPLEMENTED)
|
---|
8687 | throw setError(E_NOTIMPL,
|
---|
8688 | tr("Compacting is not implemented, medium '%s'"),
|
---|
8689 | location.c_str());
|
---|
8690 | else
|
---|
8691 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8692 | tr("Could not compact medium '%s'%s"),
|
---|
8693 | location.c_str(),
|
---|
8694 | i_vdError(vrc).c_str());
|
---|
8695 | }
|
---|
8696 | }
|
---|
8697 | catch (HRESULT aRC) { rc = aRC; }
|
---|
8698 |
|
---|
8699 | VDDestroy(hdd);
|
---|
8700 | }
|
---|
8701 | catch (HRESULT aRC) { rc = aRC; }
|
---|
8702 |
|
---|
8703 | /* Everything is explicitly unlocked when the task exits,
|
---|
8704 | * as the task destruction also destroys the media chain. */
|
---|
8705 |
|
---|
8706 | return rc;
|
---|
8707 | }
|
---|
8708 |
|
---|
8709 | /**
|
---|
8710 | * Implementation code for the "resize" task.
|
---|
8711 | *
|
---|
8712 | * @param task
|
---|
8713 | * @return
|
---|
8714 | */
|
---|
8715 | HRESULT Medium::i_taskResizeHandler(Medium::ResizeTask &task)
|
---|
8716 | {
|
---|
8717 | HRESULT rc = S_OK;
|
---|
8718 |
|
---|
8719 | uint64_t size = 0, logicalSize = 0;
|
---|
8720 |
|
---|
8721 | try
|
---|
8722 | {
|
---|
8723 | /* The lock is also used as a signal from the task initiator (which
|
---|
8724 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
8725 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8726 |
|
---|
8727 | PVBOXHDD hdd;
|
---|
8728 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8729 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8730 |
|
---|
8731 | try
|
---|
8732 | {
|
---|
8733 | /* Open all media in the chain. */
|
---|
8734 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
8735 | task.mpMediumLockList->GetBegin();
|
---|
8736 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
8737 | task.mpMediumLockList->GetEnd();
|
---|
8738 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
8739 | mediumListEnd;
|
---|
8740 | --mediumListLast;
|
---|
8741 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
8742 | it != mediumListEnd;
|
---|
8743 | ++it)
|
---|
8744 | {
|
---|
8745 | const MediumLock &mediumLock = *it;
|
---|
8746 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8747 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8748 |
|
---|
8749 | /* sanity check */
|
---|
8750 | if (it == mediumListLast)
|
---|
8751 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
8752 | else
|
---|
8753 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
8754 |
|
---|
8755 | /* Open all media but last in read-only mode. Do not handle
|
---|
8756 | * shareable media, as compaction and sharing are mutually
|
---|
8757 | * exclusive. */
|
---|
8758 | vrc = VDOpen(hdd,
|
---|
8759 | pMedium->m->strFormat.c_str(),
|
---|
8760 | pMedium->m->strLocationFull.c_str(),
|
---|
8761 | m->uOpenFlagsDef | (it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
8762 | pMedium->m->vdImageIfaces);
|
---|
8763 | if (RT_FAILURE(vrc))
|
---|
8764 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8765 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8766 | pMedium->m->strLocationFull.c_str(),
|
---|
8767 | i_vdError(vrc).c_str());
|
---|
8768 | }
|
---|
8769 |
|
---|
8770 | Assert(m->state == MediumState_LockedWrite);
|
---|
8771 |
|
---|
8772 | Utf8Str location(m->strLocationFull);
|
---|
8773 |
|
---|
8774 | /* unlock before the potentially lengthy operation */
|
---|
8775 | thisLock.release();
|
---|
8776 |
|
---|
8777 | VDGEOMETRY geo = {0, 0, 0}; /* auto */
|
---|
8778 | vrc = VDResize(hdd, task.mSize, &geo, &geo, task.mVDOperationIfaces);
|
---|
8779 | if (RT_FAILURE(vrc))
|
---|
8780 | {
|
---|
8781 | if (vrc == VERR_NOT_SUPPORTED)
|
---|
8782 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
8783 | tr("Resizing to new size %llu is not yet supported for medium '%s'"),
|
---|
8784 | task.mSize, location.c_str());
|
---|
8785 | else if (vrc == VERR_NOT_IMPLEMENTED)
|
---|
8786 | throw setError(E_NOTIMPL,
|
---|
8787 | tr("Resiting is not implemented, medium '%s'"),
|
---|
8788 | location.c_str());
|
---|
8789 | else
|
---|
8790 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8791 | tr("Could not resize medium '%s'%s"),
|
---|
8792 | location.c_str(),
|
---|
8793 | i_vdError(vrc).c_str());
|
---|
8794 | }
|
---|
8795 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
8796 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
8797 | }
|
---|
8798 | catch (HRESULT aRC) { rc = aRC; }
|
---|
8799 |
|
---|
8800 | VDDestroy(hdd);
|
---|
8801 | }
|
---|
8802 | catch (HRESULT aRC) { rc = aRC; }
|
---|
8803 |
|
---|
8804 | if (SUCCEEDED(rc))
|
---|
8805 | {
|
---|
8806 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8807 | m->size = size;
|
---|
8808 | m->logicalSize = logicalSize;
|
---|
8809 | }
|
---|
8810 |
|
---|
8811 | /* Everything is explicitly unlocked when the task exits,
|
---|
8812 | * as the task destruction also destroys the media chain. */
|
---|
8813 |
|
---|
8814 | return rc;
|
---|
8815 | }
|
---|
8816 |
|
---|
8817 | /**
|
---|
8818 | * Implementation code for the "export" task.
|
---|
8819 | *
|
---|
8820 | * This only gets started from Medium::exportFile() and always runs
|
---|
8821 | * asynchronously. It doesn't touch anything configuration related, so
|
---|
8822 | * we never save the VirtualBox.xml file here.
|
---|
8823 | *
|
---|
8824 | * @param task
|
---|
8825 | * @return
|
---|
8826 | */
|
---|
8827 | HRESULT Medium::i_taskExportHandler(Medium::ExportTask &task)
|
---|
8828 | {
|
---|
8829 | HRESULT rc = S_OK;
|
---|
8830 |
|
---|
8831 | try
|
---|
8832 | {
|
---|
8833 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
8834 | * signal from the task initiator (which releases it only after
|
---|
8835 | * RTThreadCreate()) that we can start the job. */
|
---|
8836 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
8837 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8838 |
|
---|
8839 | PVBOXHDD hdd;
|
---|
8840 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8841 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8842 |
|
---|
8843 | try
|
---|
8844 | {
|
---|
8845 | settings::StringsMap::iterator itKeyStore = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
8846 | if (itKeyStore != pBase->m->mapProperties.end())
|
---|
8847 | {
|
---|
8848 | settings::StringsMap::iterator itKeyId = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
8849 |
|
---|
8850 | #ifdef VBOX_WITH_EXTPACK
|
---|
8851 | static const Utf8Str strExtPackPuel("Oracle VM VirtualBox Extension Pack");
|
---|
8852 | static const char *s_pszVDPlugin = "VDPluginCrypt";
|
---|
8853 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
8854 | if (pExtPackManager->i_isExtPackUsable(strExtPackPuel.c_str()))
|
---|
8855 | {
|
---|
8856 | /* Load the plugin */
|
---|
8857 | Utf8Str strPlugin;
|
---|
8858 | rc = pExtPackManager->i_getLibraryPathForExtPack(s_pszVDPlugin, &strExtPackPuel, &strPlugin);
|
---|
8859 | if (SUCCEEDED(rc))
|
---|
8860 | {
|
---|
8861 | vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
8862 | if (RT_FAILURE(vrc))
|
---|
8863 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
8864 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
8865 | i_vdError(vrc).c_str());
|
---|
8866 | }
|
---|
8867 | else
|
---|
8868 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
8869 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
8870 | strExtPackPuel.c_str());
|
---|
8871 | }
|
---|
8872 | else
|
---|
8873 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
8874 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
8875 | strExtPackPuel.c_str());
|
---|
8876 | #else
|
---|
8877 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
8878 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
8879 | #endif
|
---|
8880 |
|
---|
8881 | if (itKeyId == pBase->m->mapProperties.end())
|
---|
8882 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8883 | tr("Image '%s' is configured for encryption but doesn't has a key identifier set"),
|
---|
8884 | pBase->m->strLocationFull.c_str());
|
---|
8885 |
|
---|
8886 | /* Find the proper secret key in the key store. */
|
---|
8887 | if (!task.m_pSecretKeyStore)
|
---|
8888 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8889 | tr("Image '%s' is configured for encryption but there is no key store to retrieve the password from"),
|
---|
8890 | pBase->m->strLocationFull.c_str());
|
---|
8891 |
|
---|
8892 | SecretKey *pKey = NULL;
|
---|
8893 | vrc = task.m_pSecretKeyStore->retainSecretKey(itKeyId->second, &pKey);
|
---|
8894 | if (RT_FAILURE(vrc))
|
---|
8895 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8896 | tr("Failed to retrieve the secret key with ID \"%s\" from the store (%Rrc)"),
|
---|
8897 | itKeyId->second.c_str(), vrc);
|
---|
8898 |
|
---|
8899 | Medium::CryptoFilterSettings CryptoSettingsRead;
|
---|
8900 | i_taskEncryptSettingsSetup(&CryptoSettingsRead, NULL, itKeyStore->second.c_str(), (const char *)pKey->getKeyBuffer(),
|
---|
8901 | false /* fCreateKeyStore */);
|
---|
8902 | vrc = VDFilterAdd(hdd, "CRYPT", VD_FILTER_FLAGS_READ, CryptoSettingsRead.vdFilterIfaces);
|
---|
8903 | if (vrc == VERR_VD_PASSWORD_INCORRECT)
|
---|
8904 | {
|
---|
8905 | task.m_pSecretKeyStore->releaseSecretKey(itKeyId->second);
|
---|
8906 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
8907 | tr("The password to decrypt the image is incorrect"));
|
---|
8908 | }
|
---|
8909 | else if (RT_FAILURE(vrc))
|
---|
8910 | {
|
---|
8911 | task.m_pSecretKeyStore->releaseSecretKey(itKeyId->second);
|
---|
8912 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8913 | tr("Failed to load the decryption filter: %s"),
|
---|
8914 | i_vdError(vrc).c_str());
|
---|
8915 | }
|
---|
8916 |
|
---|
8917 | task.m_pSecretKeyStore->releaseSecretKey(itKeyId->second);
|
---|
8918 | }
|
---|
8919 |
|
---|
8920 | /* Open all media in the source chain. */
|
---|
8921 | MediumLockList::Base::const_iterator sourceListBegin =
|
---|
8922 | task.mpSourceMediumLockList->GetBegin();
|
---|
8923 | MediumLockList::Base::const_iterator sourceListEnd =
|
---|
8924 | task.mpSourceMediumLockList->GetEnd();
|
---|
8925 | for (MediumLockList::Base::const_iterator it = sourceListBegin;
|
---|
8926 | it != sourceListEnd;
|
---|
8927 | ++it)
|
---|
8928 | {
|
---|
8929 | const MediumLock &mediumLock = *it;
|
---|
8930 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8931 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8932 |
|
---|
8933 | /* sanity check */
|
---|
8934 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
8935 |
|
---|
8936 | /* Open all media in read-only mode. */
|
---|
8937 | vrc = VDOpen(hdd,
|
---|
8938 | pMedium->m->strFormat.c_str(),
|
---|
8939 | pMedium->m->strLocationFull.c_str(),
|
---|
8940 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
8941 | pMedium->m->vdImageIfaces);
|
---|
8942 | if (RT_FAILURE(vrc))
|
---|
8943 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8944 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8945 | pMedium->m->strLocationFull.c_str(),
|
---|
8946 | i_vdError(vrc).c_str());
|
---|
8947 | }
|
---|
8948 |
|
---|
8949 | Utf8Str targetFormat(task.mFormat->i_getId());
|
---|
8950 | Utf8Str targetLocation(task.mFilename);
|
---|
8951 | uint64_t capabilities = task.mFormat->i_getCapabilities();
|
---|
8952 |
|
---|
8953 | Assert(m->state == MediumState_LockedRead);
|
---|
8954 |
|
---|
8955 | /* unlock before the potentially lengthy operation */
|
---|
8956 | thisLock.release();
|
---|
8957 |
|
---|
8958 | /* ensure the target directory exists */
|
---|
8959 | if (capabilities & MediumFormatCapabilities_File)
|
---|
8960 | {
|
---|
8961 | rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
8962 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
8963 | if (FAILED(rc))
|
---|
8964 | throw rc;
|
---|
8965 | }
|
---|
8966 |
|
---|
8967 | PVBOXHDD targetHdd;
|
---|
8968 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &targetHdd);
|
---|
8969 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8970 |
|
---|
8971 | try
|
---|
8972 | {
|
---|
8973 | vrc = VDCopy(hdd,
|
---|
8974 | VD_LAST_IMAGE,
|
---|
8975 | targetHdd,
|
---|
8976 | targetFormat.c_str(),
|
---|
8977 | targetLocation.c_str(),
|
---|
8978 | false /* fMoveByRename */,
|
---|
8979 | 0 /* cbSize */,
|
---|
8980 | task.mVariant & ~MediumVariant_NoCreateDir,
|
---|
8981 | NULL /* pDstUuid */,
|
---|
8982 | VD_OPEN_FLAGS_NORMAL | VD_OPEN_FLAGS_SEQUENTIAL,
|
---|
8983 | NULL /* pVDIfsOperation */,
|
---|
8984 | task.mVDImageIfaces,
|
---|
8985 | task.mVDOperationIfaces);
|
---|
8986 | if (RT_FAILURE(vrc))
|
---|
8987 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8988 | tr("Could not create the exported medium '%s'%s"),
|
---|
8989 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
8990 | }
|
---|
8991 | catch (HRESULT aRC) { rc = aRC; }
|
---|
8992 |
|
---|
8993 | VDDestroy(targetHdd);
|
---|
8994 | }
|
---|
8995 | catch (HRESULT aRC) { rc = aRC; }
|
---|
8996 |
|
---|
8997 | VDDestroy(hdd);
|
---|
8998 | }
|
---|
8999 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9000 |
|
---|
9001 | /* Everything is explicitly unlocked when the task exits,
|
---|
9002 | * as the task destruction also destroys the source chain. */
|
---|
9003 |
|
---|
9004 | /* Make sure the source chain is released early, otherwise it can
|
---|
9005 | * lead to deadlocks with concurrent IAppliance activities. */
|
---|
9006 | task.mpSourceMediumLockList->Clear();
|
---|
9007 |
|
---|
9008 | return rc;
|
---|
9009 | }
|
---|
9010 |
|
---|
9011 | /**
|
---|
9012 | * Implementation code for the "import" task.
|
---|
9013 | *
|
---|
9014 | * This only gets started from Medium::importFile() and always runs
|
---|
9015 | * asynchronously. It potentially touches the media registry, so we
|
---|
9016 | * always save the VirtualBox.xml file when we're done here.
|
---|
9017 | *
|
---|
9018 | * @param task
|
---|
9019 | * @return
|
---|
9020 | */
|
---|
9021 | HRESULT Medium::i_taskImportHandler(Medium::ImportTask &task)
|
---|
9022 | {
|
---|
9023 | HRESULT rcTmp = S_OK;
|
---|
9024 |
|
---|
9025 | const ComObjPtr<Medium> &pParent = task.mParent;
|
---|
9026 |
|
---|
9027 | bool fCreatingTarget = false;
|
---|
9028 |
|
---|
9029 | uint64_t size = 0, logicalSize = 0;
|
---|
9030 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
9031 | bool fGenerateUuid = false;
|
---|
9032 |
|
---|
9033 | try
|
---|
9034 | {
|
---|
9035 | if (!pParent.isNull())
|
---|
9036 | if (pParent->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
9037 | {
|
---|
9038 | AutoReadLock plock(pParent COMMA_LOCKVAL_SRC_POS);
|
---|
9039 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
9040 | tr("Cannot import image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
9041 | pParent->m->strLocationFull.c_str());
|
---|
9042 | }
|
---|
9043 |
|
---|
9044 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
9045 | * signal from the task initiator (which releases it only after
|
---|
9046 | * RTThreadCreate()) that we can start the job. */
|
---|
9047 | AutoMultiWriteLock2 thisLock(this, pParent COMMA_LOCKVAL_SRC_POS);
|
---|
9048 |
|
---|
9049 | fCreatingTarget = m->state == MediumState_Creating;
|
---|
9050 |
|
---|
9051 | /* The object may request a specific UUID (through a special form of
|
---|
9052 | * the setLocation() argument). Otherwise we have to generate it */
|
---|
9053 | Guid targetId = m->id;
|
---|
9054 |
|
---|
9055 | fGenerateUuid = targetId.isZero();
|
---|
9056 | if (fGenerateUuid)
|
---|
9057 | {
|
---|
9058 | targetId.create();
|
---|
9059 | /* VirtualBox::i_registerMedium() will need UUID */
|
---|
9060 | unconst(m->id) = targetId;
|
---|
9061 | }
|
---|
9062 |
|
---|
9063 |
|
---|
9064 | PVBOXHDD hdd;
|
---|
9065 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9066 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9067 |
|
---|
9068 | try
|
---|
9069 | {
|
---|
9070 | /* Open source medium. */
|
---|
9071 | vrc = VDOpen(hdd,
|
---|
9072 | task.mFormat->i_getId().c_str(),
|
---|
9073 | task.mFilename.c_str(),
|
---|
9074 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SEQUENTIAL | m->uOpenFlagsDef,
|
---|
9075 | task.mVDImageIfaces);
|
---|
9076 | if (RT_FAILURE(vrc))
|
---|
9077 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9078 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9079 | task.mFilename.c_str(),
|
---|
9080 | i_vdError(vrc).c_str());
|
---|
9081 |
|
---|
9082 | Utf8Str targetFormat(m->strFormat);
|
---|
9083 | Utf8Str targetLocation(m->strLocationFull);
|
---|
9084 | uint64_t capabilities = task.mFormat->i_getCapabilities();
|
---|
9085 |
|
---|
9086 | Assert( m->state == MediumState_Creating
|
---|
9087 | || m->state == MediumState_LockedWrite);
|
---|
9088 | Assert( pParent.isNull()
|
---|
9089 | || pParent->m->state == MediumState_LockedRead);
|
---|
9090 |
|
---|
9091 | /* unlock before the potentially lengthy operation */
|
---|
9092 | thisLock.release();
|
---|
9093 |
|
---|
9094 | /* ensure the target directory exists */
|
---|
9095 | if (capabilities & MediumFormatCapabilities_File)
|
---|
9096 | {
|
---|
9097 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
9098 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
9099 | if (FAILED(rc))
|
---|
9100 | throw rc;
|
---|
9101 | }
|
---|
9102 |
|
---|
9103 | PVBOXHDD targetHdd;
|
---|
9104 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &targetHdd);
|
---|
9105 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9106 |
|
---|
9107 | try
|
---|
9108 | {
|
---|
9109 | /* Open all media in the target chain. */
|
---|
9110 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
9111 | task.mpTargetMediumLockList->GetBegin();
|
---|
9112 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
9113 | task.mpTargetMediumLockList->GetEnd();
|
---|
9114 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
9115 | it != targetListEnd;
|
---|
9116 | ++it)
|
---|
9117 | {
|
---|
9118 | const MediumLock &mediumLock = *it;
|
---|
9119 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9120 |
|
---|
9121 | /* If the target medium is not created yet there's no
|
---|
9122 | * reason to open it. */
|
---|
9123 | if (pMedium == this && fCreatingTarget)
|
---|
9124 | continue;
|
---|
9125 |
|
---|
9126 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9127 |
|
---|
9128 | /* sanity check */
|
---|
9129 | Assert( pMedium->m->state == MediumState_LockedRead
|
---|
9130 | || pMedium->m->state == MediumState_LockedWrite);
|
---|
9131 |
|
---|
9132 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
9133 | if (pMedium->m->state != MediumState_LockedWrite)
|
---|
9134 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
9135 | if (pMedium->m->type == MediumType_Shareable)
|
---|
9136 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
9137 |
|
---|
9138 | /* Open all media in appropriate mode. */
|
---|
9139 | vrc = VDOpen(targetHdd,
|
---|
9140 | pMedium->m->strFormat.c_str(),
|
---|
9141 | pMedium->m->strLocationFull.c_str(),
|
---|
9142 | uOpenFlags | m->uOpenFlagsDef,
|
---|
9143 | pMedium->m->vdImageIfaces);
|
---|
9144 | if (RT_FAILURE(vrc))
|
---|
9145 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9146 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9147 | pMedium->m->strLocationFull.c_str(),
|
---|
9148 | i_vdError(vrc).c_str());
|
---|
9149 | }
|
---|
9150 |
|
---|
9151 | vrc = VDCopy(hdd,
|
---|
9152 | VD_LAST_IMAGE,
|
---|
9153 | targetHdd,
|
---|
9154 | targetFormat.c_str(),
|
---|
9155 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
9156 | false /* fMoveByRename */,
|
---|
9157 | 0 /* cbSize */,
|
---|
9158 | task.mVariant & ~MediumVariant_NoCreateDir,
|
---|
9159 | targetId.raw(),
|
---|
9160 | VD_OPEN_FLAGS_NORMAL,
|
---|
9161 | NULL /* pVDIfsOperation */,
|
---|
9162 | m->vdImageIfaces,
|
---|
9163 | task.mVDOperationIfaces);
|
---|
9164 | if (RT_FAILURE(vrc))
|
---|
9165 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9166 | tr("Could not create the imported medium '%s'%s"),
|
---|
9167 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
9168 |
|
---|
9169 | size = VDGetFileSize(targetHdd, VD_LAST_IMAGE);
|
---|
9170 | logicalSize = VDGetSize(targetHdd, VD_LAST_IMAGE);
|
---|
9171 | unsigned uImageFlags;
|
---|
9172 | vrc = VDGetImageFlags(targetHdd, 0, &uImageFlags);
|
---|
9173 | if (RT_SUCCESS(vrc))
|
---|
9174 | variant = (MediumVariant_T)uImageFlags;
|
---|
9175 | }
|
---|
9176 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9177 |
|
---|
9178 | VDDestroy(targetHdd);
|
---|
9179 | }
|
---|
9180 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9181 |
|
---|
9182 | VDDestroy(hdd);
|
---|
9183 | }
|
---|
9184 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9185 |
|
---|
9186 | ErrorInfoKeeper eik;
|
---|
9187 | MultiResult mrc(rcTmp);
|
---|
9188 |
|
---|
9189 | /* Only do the parent changes for newly created media. */
|
---|
9190 | if (SUCCEEDED(mrc) && fCreatingTarget)
|
---|
9191 | {
|
---|
9192 | /* we set mParent & children() */
|
---|
9193 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
9194 |
|
---|
9195 | Assert(m->pParent.isNull());
|
---|
9196 |
|
---|
9197 | if (pParent)
|
---|
9198 | {
|
---|
9199 | /* Associate the imported medium with the parent and deassociate
|
---|
9200 | * from VirtualBox. Depth check above. */
|
---|
9201 | i_setParent(pParent);
|
---|
9202 |
|
---|
9203 | /* register with mVirtualBox as the last step and move to
|
---|
9204 | * Created state only on success (leaving an orphan file is
|
---|
9205 | * better than breaking media registry consistency) */
|
---|
9206 | eik.restore();
|
---|
9207 | ComObjPtr<Medium> pMedium;
|
---|
9208 | mrc = pParent->m->pVirtualBox->i_registerMedium(this, &pMedium,
|
---|
9209 | treeLock);
|
---|
9210 | Assert(this == pMedium);
|
---|
9211 | eik.fetch();
|
---|
9212 |
|
---|
9213 | if (FAILED(mrc))
|
---|
9214 | /* break parent association on failure to register */
|
---|
9215 | this->i_deparent(); // removes target from parent
|
---|
9216 | }
|
---|
9217 | else
|
---|
9218 | {
|
---|
9219 | /* just register */
|
---|
9220 | eik.restore();
|
---|
9221 | ComObjPtr<Medium> pMedium;
|
---|
9222 | mrc = m->pVirtualBox->i_registerMedium(this, &pMedium, treeLock);
|
---|
9223 | Assert(this == pMedium);
|
---|
9224 | eik.fetch();
|
---|
9225 | }
|
---|
9226 | }
|
---|
9227 |
|
---|
9228 | if (fCreatingTarget)
|
---|
9229 | {
|
---|
9230 | AutoWriteLock mLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9231 |
|
---|
9232 | if (SUCCEEDED(mrc))
|
---|
9233 | {
|
---|
9234 | m->state = MediumState_Created;
|
---|
9235 |
|
---|
9236 | m->size = size;
|
---|
9237 | m->logicalSize = logicalSize;
|
---|
9238 | m->variant = variant;
|
---|
9239 | }
|
---|
9240 | else
|
---|
9241 | {
|
---|
9242 | /* back to NotCreated on failure */
|
---|
9243 | m->state = MediumState_NotCreated;
|
---|
9244 |
|
---|
9245 | /* reset UUID to prevent it from being reused next time */
|
---|
9246 | if (fGenerateUuid)
|
---|
9247 | unconst(m->id).clear();
|
---|
9248 | }
|
---|
9249 | }
|
---|
9250 |
|
---|
9251 | // now, at the end of this task (always asynchronous), save the settings
|
---|
9252 | {
|
---|
9253 | // save the settings
|
---|
9254 | i_markRegistriesModified();
|
---|
9255 | /* collect multiple errors */
|
---|
9256 | eik.restore();
|
---|
9257 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
9258 | eik.fetch();
|
---|
9259 | }
|
---|
9260 |
|
---|
9261 | /* Everything is explicitly unlocked when the task exits,
|
---|
9262 | * as the task destruction also destroys the target chain. */
|
---|
9263 |
|
---|
9264 | /* Make sure the target chain is released early, otherwise it can
|
---|
9265 | * lead to deadlocks with concurrent IAppliance activities. */
|
---|
9266 | task.mpTargetMediumLockList->Clear();
|
---|
9267 |
|
---|
9268 | return mrc;
|
---|
9269 | }
|
---|
9270 |
|
---|
9271 | /**
|
---|
9272 | * Sets up the encryption settings for a filter.
|
---|
9273 | */
|
---|
9274 | void Medium::i_taskEncryptSettingsSetup(CryptoFilterSettings *pSettings, const char *pszCipher,
|
---|
9275 | const char *pszKeyStore, const char *pszPassword,
|
---|
9276 | bool fCreateKeyStore)
|
---|
9277 | {
|
---|
9278 | pSettings->pszCipher = pszCipher;
|
---|
9279 | pSettings->pszPassword = pszPassword;
|
---|
9280 | pSettings->pszKeyStoreLoad = pszKeyStore;
|
---|
9281 | pSettings->fCreateKeyStore = fCreateKeyStore;
|
---|
9282 | pSettings->pbDek = NULL;
|
---|
9283 | pSettings->cbDek = 0;
|
---|
9284 | pSettings->vdFilterIfaces = NULL;
|
---|
9285 |
|
---|
9286 | pSettings->vdIfCfg.pfnAreKeysValid = i_vdCryptoConfigAreKeysValid;
|
---|
9287 | pSettings->vdIfCfg.pfnQuerySize = i_vdCryptoConfigQuerySize;
|
---|
9288 | pSettings->vdIfCfg.pfnQuery = i_vdCryptoConfigQuery;
|
---|
9289 | pSettings->vdIfCfg.pfnQueryBytes = NULL;
|
---|
9290 |
|
---|
9291 | pSettings->vdIfCrypto.pfnKeyRetain = i_vdCryptoKeyRetain;
|
---|
9292 | pSettings->vdIfCrypto.pfnKeyRelease = i_vdCryptoKeyRelease;
|
---|
9293 | pSettings->vdIfCrypto.pfnKeyStorePasswordRetain = i_vdCryptoKeyStorePasswordRetain;
|
---|
9294 | pSettings->vdIfCrypto.pfnKeyStorePasswordRelease = i_vdCryptoKeyStorePasswordRelease;
|
---|
9295 | pSettings->vdIfCrypto.pfnKeyStoreSave = i_vdCryptoKeyStoreSave;
|
---|
9296 | pSettings->vdIfCrypto.pfnKeyStoreReturnParameters = i_vdCryptoKeyStoreReturnParameters;
|
---|
9297 |
|
---|
9298 | int vrc = VDInterfaceAdd(&pSettings->vdIfCfg.Core,
|
---|
9299 | "Medium::vdInterfaceCfgCrypto",
|
---|
9300 | VDINTERFACETYPE_CONFIG, pSettings,
|
---|
9301 | sizeof(VDINTERFACECONFIG), &pSettings->vdFilterIfaces);
|
---|
9302 | AssertRC(vrc);
|
---|
9303 |
|
---|
9304 | vrc = VDInterfaceAdd(&pSettings->vdIfCrypto.Core,
|
---|
9305 | "Medium::vdInterfaceCrypto",
|
---|
9306 | VDINTERFACETYPE_CRYPTO, pSettings,
|
---|
9307 | sizeof(VDINTERFACECRYPTO), &pSettings->vdFilterIfaces);
|
---|
9308 | AssertRC(vrc);
|
---|
9309 | }
|
---|
9310 |
|
---|
9311 | /**
|
---|
9312 | * Implementation code for the "encrypt" task.
|
---|
9313 | *
|
---|
9314 | * @param task
|
---|
9315 | * @return
|
---|
9316 | */
|
---|
9317 | HRESULT Medium::i_taskEncryptHandler(Medium::EncryptTask &task)
|
---|
9318 | {
|
---|
9319 | HRESULT rc = S_OK;
|
---|
9320 |
|
---|
9321 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
9322 | * signal from the task initiator (which releases it only after
|
---|
9323 | * RTThreadCreate()) that we can start the job. */
|
---|
9324 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
9325 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9326 |
|
---|
9327 | try
|
---|
9328 | {
|
---|
9329 | # ifdef VBOX_WITH_EXTPACK
|
---|
9330 | static const Utf8Str strExtPackPuel("Oracle VM VirtualBox Extension Pack");
|
---|
9331 | static const char *s_pszVDPlugin = "VDPluginCrypt";
|
---|
9332 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
9333 | if (pExtPackManager->i_isExtPackUsable(strExtPackPuel.c_str()))
|
---|
9334 | {
|
---|
9335 | /* Load the plugin */
|
---|
9336 | Utf8Str strPlugin;
|
---|
9337 | rc = pExtPackManager->i_getLibraryPathForExtPack(s_pszVDPlugin, &strExtPackPuel, &strPlugin);
|
---|
9338 | if (SUCCEEDED(rc))
|
---|
9339 | {
|
---|
9340 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
9341 | if (RT_FAILURE(vrc))
|
---|
9342 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
9343 | tr("Encrypting the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
9344 | i_vdError(vrc).c_str());
|
---|
9345 | }
|
---|
9346 | else
|
---|
9347 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
9348 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
9349 | strExtPackPuel.c_str());
|
---|
9350 | }
|
---|
9351 | else
|
---|
9352 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
9353 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
9354 | strExtPackPuel.c_str());
|
---|
9355 |
|
---|
9356 | PVBOXHDD pDisk = NULL;
|
---|
9357 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
9358 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9359 |
|
---|
9360 | Medium::CryptoFilterSettings CryptoSettingsRead;
|
---|
9361 | Medium::CryptoFilterSettings CryptoSettingsWrite;
|
---|
9362 |
|
---|
9363 | void *pvBuf = NULL;
|
---|
9364 | const char *pszPasswordNew = NULL;
|
---|
9365 | try
|
---|
9366 | {
|
---|
9367 | /* Set up disk encryption filters. */
|
---|
9368 | if (task.mstrCurrentPassword.isEmpty())
|
---|
9369 | {
|
---|
9370 | /*
|
---|
9371 | * Query whether the medium property indicating that encryption is
|
---|
9372 | * configured is existing.
|
---|
9373 | */
|
---|
9374 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
9375 | if (it != pBase->m->mapProperties.end())
|
---|
9376 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
9377 | tr("The password given for the encrypted image is incorrect"));
|
---|
9378 | }
|
---|
9379 | else
|
---|
9380 | {
|
---|
9381 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
9382 | if (it == pBase->m->mapProperties.end())
|
---|
9383 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
9384 | tr("The image is not configured for encryption"));
|
---|
9385 |
|
---|
9386 | i_taskEncryptSettingsSetup(&CryptoSettingsRead, NULL, it->second.c_str(), task.mstrCurrentPassword.c_str(),
|
---|
9387 | false /* fCreateKeyStore */);
|
---|
9388 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ, CryptoSettingsRead.vdFilterIfaces);
|
---|
9389 | if (vrc == VERR_VD_PASSWORD_INCORRECT)
|
---|
9390 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
9391 | tr("The password to decrypt the image is incorrect"));
|
---|
9392 | else if (RT_FAILURE(vrc))
|
---|
9393 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
9394 | tr("Failed to load the decryption filter: %s"),
|
---|
9395 | i_vdError(vrc).c_str());
|
---|
9396 | }
|
---|
9397 |
|
---|
9398 | if (task.mstrCipher.isNotEmpty())
|
---|
9399 | {
|
---|
9400 | if ( task.mstrNewPassword.isEmpty()
|
---|
9401 | && task.mstrNewPasswordId.isEmpty()
|
---|
9402 | && task.mstrCurrentPassword.isNotEmpty())
|
---|
9403 | {
|
---|
9404 | /* An empty password and password ID will default to the current password. */
|
---|
9405 | pszPasswordNew = task.mstrCurrentPassword.c_str();
|
---|
9406 | }
|
---|
9407 | else if (task.mstrNewPassword.isEmpty())
|
---|
9408 | throw setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
9409 | tr("A password must be given for the image encryption"));
|
---|
9410 | else if (task.mstrNewPasswordId.isEmpty())
|
---|
9411 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
9412 | tr("A valid identifier for the password must be given"));
|
---|
9413 | else
|
---|
9414 | pszPasswordNew = task.mstrNewPassword.c_str();
|
---|
9415 |
|
---|
9416 | i_taskEncryptSettingsSetup(&CryptoSettingsWrite, task.mstrCipher.c_str(), NULL,
|
---|
9417 | pszPasswordNew, true /* fCreateKeyStore */);
|
---|
9418 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_WRITE, CryptoSettingsWrite.vdFilterIfaces);
|
---|
9419 | if (RT_FAILURE(vrc))
|
---|
9420 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
9421 | tr("Failed to load the encryption filter: %s"),
|
---|
9422 | i_vdError(vrc).c_str());
|
---|
9423 | }
|
---|
9424 | else if (task.mstrNewPasswordId.isNotEmpty() || task.mstrNewPassword.isNotEmpty())
|
---|
9425 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
9426 | tr("The password and password identifier must be empty if the output should be unencrypted"));
|
---|
9427 |
|
---|
9428 | /* Open all media in the chain. */
|
---|
9429 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
9430 | task.mpMediumLockList->GetBegin();
|
---|
9431 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
9432 | task.mpMediumLockList->GetEnd();
|
---|
9433 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
9434 | mediumListEnd;
|
---|
9435 | --mediumListLast;
|
---|
9436 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
9437 | it != mediumListEnd;
|
---|
9438 | ++it)
|
---|
9439 | {
|
---|
9440 | const MediumLock &mediumLock = *it;
|
---|
9441 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9442 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9443 |
|
---|
9444 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
9445 |
|
---|
9446 | /* Open all media but last in read-only mode. Do not handle
|
---|
9447 | * shareable media, as compaction and sharing are mutually
|
---|
9448 | * exclusive. */
|
---|
9449 | vrc = VDOpen(pDisk,
|
---|
9450 | pMedium->m->strFormat.c_str(),
|
---|
9451 | pMedium->m->strLocationFull.c_str(),
|
---|
9452 | m->uOpenFlagsDef | (it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
9453 | pMedium->m->vdImageIfaces);
|
---|
9454 | if (RT_FAILURE(vrc))
|
---|
9455 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9456 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9457 | pMedium->m->strLocationFull.c_str(),
|
---|
9458 | i_vdError(vrc).c_str());
|
---|
9459 | }
|
---|
9460 |
|
---|
9461 | Assert(m->state == MediumState_LockedWrite);
|
---|
9462 |
|
---|
9463 | Utf8Str location(m->strLocationFull);
|
---|
9464 |
|
---|
9465 | /* unlock before the potentially lengthy operation */
|
---|
9466 | thisLock.release();
|
---|
9467 |
|
---|
9468 | vrc = VDPrepareWithFilters(pDisk, task.mVDOperationIfaces);
|
---|
9469 | if (RT_FAILURE(vrc))
|
---|
9470 | throw setError(VBOX_E_FILE_ERROR,
|
---|
9471 | tr("Could not prepare disk images for encryption (%Rrc): %s"),
|
---|
9472 | vrc, i_vdError(vrc).c_str());
|
---|
9473 |
|
---|
9474 | thisLock.acquire();
|
---|
9475 | /* If everything went well set the new key store. */
|
---|
9476 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
9477 | if (it != pBase->m->mapProperties.end())
|
---|
9478 | pBase->m->mapProperties.erase(it);
|
---|
9479 |
|
---|
9480 | /* Delete KeyId if encryption is removed or the password did change. */
|
---|
9481 | if ( task.mstrNewPasswordId.isNotEmpty()
|
---|
9482 | || task.mstrCipher.isEmpty())
|
---|
9483 | {
|
---|
9484 | it = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
9485 | if (it != pBase->m->mapProperties.end())
|
---|
9486 | pBase->m->mapProperties.erase(it);
|
---|
9487 | }
|
---|
9488 |
|
---|
9489 | if (CryptoSettingsWrite.pszKeyStore)
|
---|
9490 | {
|
---|
9491 | pBase->m->mapProperties["CRYPT/KeyStore"] = Utf8Str(CryptoSettingsWrite.pszKeyStore);
|
---|
9492 | if (task.mstrNewPasswordId.isNotEmpty())
|
---|
9493 | pBase->m->mapProperties["CRYPT/KeyId"] = task.mstrNewPasswordId;
|
---|
9494 | }
|
---|
9495 |
|
---|
9496 | if (CryptoSettingsRead.pszCipherReturned)
|
---|
9497 | RTStrFree(CryptoSettingsRead.pszCipherReturned);
|
---|
9498 |
|
---|
9499 | if (CryptoSettingsWrite.pszCipherReturned)
|
---|
9500 | RTStrFree(CryptoSettingsWrite.pszCipherReturned);
|
---|
9501 |
|
---|
9502 | thisLock.release();
|
---|
9503 | pBase->i_markRegistriesModified();
|
---|
9504 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
9505 | }
|
---|
9506 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9507 |
|
---|
9508 | if (pvBuf)
|
---|
9509 | RTMemFree(pvBuf);
|
---|
9510 |
|
---|
9511 | VDDestroy(pDisk);
|
---|
9512 | # else
|
---|
9513 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
9514 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
9515 | # endif
|
---|
9516 | }
|
---|
9517 | catch (HRESULT aRC) { rc = aRC; }
|
---|
9518 |
|
---|
9519 | /* Everything is explicitly unlocked when the task exits,
|
---|
9520 | * as the task destruction also destroys the media chain. */
|
---|
9521 |
|
---|
9522 | return rc;
|
---|
9523 | }
|
---|
9524 |
|
---|
9525 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|