1 | /* $Id: MediumImpl.cpp 35126 2010-12-15 12:14:15Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox COM class implementation
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2010 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include "MediumImpl.h"
|
---|
19 | #include "ProgressImpl.h"
|
---|
20 | #include "SystemPropertiesImpl.h"
|
---|
21 | #include "VirtualBoxImpl.h"
|
---|
22 |
|
---|
23 | #include "AutoCaller.h"
|
---|
24 | #include "Logging.h"
|
---|
25 |
|
---|
26 | #include <VBox/com/array.h>
|
---|
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 |
|
---|
39 | #include <VBox/vd.h>
|
---|
40 |
|
---|
41 | #include <algorithm>
|
---|
42 |
|
---|
43 | ////////////////////////////////////////////////////////////////////////////////
|
---|
44 | //
|
---|
45 | // Medium data definition
|
---|
46 | //
|
---|
47 | ////////////////////////////////////////////////////////////////////////////////
|
---|
48 |
|
---|
49 | /** Describes how a machine refers to this medium. */
|
---|
50 | struct BackRef
|
---|
51 | {
|
---|
52 | /** Equality predicate for stdc++. */
|
---|
53 | struct EqualsTo : public std::unary_function <BackRef, bool>
|
---|
54 | {
|
---|
55 | explicit EqualsTo(const Guid &aMachineId) : machineId(aMachineId) {}
|
---|
56 |
|
---|
57 | bool operator()(const argument_type &aThat) const
|
---|
58 | {
|
---|
59 | return aThat.machineId == machineId;
|
---|
60 | }
|
---|
61 |
|
---|
62 | const Guid machineId;
|
---|
63 | };
|
---|
64 |
|
---|
65 | BackRef(const Guid &aMachineId,
|
---|
66 | const Guid &aSnapshotId = Guid::Empty)
|
---|
67 | : machineId(aMachineId),
|
---|
68 | fInCurState(aSnapshotId.isEmpty())
|
---|
69 | {
|
---|
70 | if (!aSnapshotId.isEmpty())
|
---|
71 | llSnapshotIds.push_back(aSnapshotId);
|
---|
72 | }
|
---|
73 |
|
---|
74 | Guid machineId;
|
---|
75 | bool fInCurState : 1;
|
---|
76 | GuidList llSnapshotIds;
|
---|
77 | };
|
---|
78 |
|
---|
79 | typedef std::list<BackRef> BackRefList;
|
---|
80 |
|
---|
81 | struct Medium::Data
|
---|
82 | {
|
---|
83 | Data()
|
---|
84 | : pVirtualBox(NULL),
|
---|
85 | state(MediumState_NotCreated),
|
---|
86 | variant(MediumVariant_Standard),
|
---|
87 | size(0),
|
---|
88 | readers(0),
|
---|
89 | preLockState(MediumState_NotCreated),
|
---|
90 | queryInfoSem(NIL_RTSEMEVENTMULTI),
|
---|
91 | queryInfoRunning(false),
|
---|
92 | type(MediumType_Normal),
|
---|
93 | devType(DeviceType_HardDisk),
|
---|
94 | logicalSize(0),
|
---|
95 | hddOpenMode(OpenReadWrite),
|
---|
96 | autoReset(false),
|
---|
97 | hostDrive(false),
|
---|
98 | implicit(false),
|
---|
99 | numCreateDiffTasks(0),
|
---|
100 | vdDiskIfaces(NULL),
|
---|
101 | vdImageIfaces(NULL)
|
---|
102 | { }
|
---|
103 |
|
---|
104 | /** weak VirtualBox parent */
|
---|
105 | VirtualBox * const pVirtualBox;
|
---|
106 |
|
---|
107 | // pParent and llChildren are protected by VirtualBox::getMediaTreeLockHandle()
|
---|
108 | ComObjPtr<Medium> pParent;
|
---|
109 | MediaList llChildren; // to add a child, just call push_back; to remove a child, call child->deparent() which does a lookup
|
---|
110 |
|
---|
111 | GuidList llRegistryIDs; // media registries in which this medium is listed
|
---|
112 |
|
---|
113 | const Guid id;
|
---|
114 | Utf8Str strDescription;
|
---|
115 | MediumState_T state;
|
---|
116 | MediumVariant_T variant;
|
---|
117 | Utf8Str strLocationFull;
|
---|
118 | uint64_t size;
|
---|
119 | Utf8Str strLastAccessError;
|
---|
120 |
|
---|
121 | BackRefList backRefs;
|
---|
122 |
|
---|
123 | size_t readers;
|
---|
124 | MediumState_T preLockState;
|
---|
125 |
|
---|
126 | RTSEMEVENTMULTI queryInfoSem;
|
---|
127 | bool queryInfoRunning : 1;
|
---|
128 |
|
---|
129 | const Utf8Str strFormat;
|
---|
130 | ComObjPtr<MediumFormat> formatObj;
|
---|
131 |
|
---|
132 | MediumType_T type;
|
---|
133 | DeviceType_T devType;
|
---|
134 | uint64_t logicalSize;
|
---|
135 |
|
---|
136 | HDDOpenMode hddOpenMode;
|
---|
137 |
|
---|
138 | bool autoReset : 1;
|
---|
139 |
|
---|
140 | const Guid uuidImage;
|
---|
141 | const Guid uuidParentImage;
|
---|
142 |
|
---|
143 | bool hostDrive : 1;
|
---|
144 |
|
---|
145 | settings::StringsMap mapProperties;
|
---|
146 |
|
---|
147 | bool implicit : 1;
|
---|
148 |
|
---|
149 | uint32_t numCreateDiffTasks;
|
---|
150 |
|
---|
151 | Utf8Str vdError; /*< Error remembered by the VD error callback. */
|
---|
152 |
|
---|
153 | VDINTERFACE vdIfError;
|
---|
154 | VDINTERFACEERROR vdIfCallsError;
|
---|
155 |
|
---|
156 | VDINTERFACE vdIfConfig;
|
---|
157 | VDINTERFACECONFIG vdIfCallsConfig;
|
---|
158 |
|
---|
159 | VDINTERFACE vdIfTcpNet;
|
---|
160 | VDINTERFACETCPNET vdIfCallsTcpNet;
|
---|
161 |
|
---|
162 | PVDINTERFACE vdDiskIfaces;
|
---|
163 | PVDINTERFACE vdImageIfaces;
|
---|
164 | };
|
---|
165 |
|
---|
166 | typedef struct VDSOCKETINT
|
---|
167 | {
|
---|
168 | /** Socket handle. */
|
---|
169 | RTSOCKET hSocket;
|
---|
170 | } VDSOCKETINT, *PVDSOCKETINT;
|
---|
171 |
|
---|
172 | ////////////////////////////////////////////////////////////////////////////////
|
---|
173 | //
|
---|
174 | // Globals
|
---|
175 | //
|
---|
176 | ////////////////////////////////////////////////////////////////////////////////
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Medium::Task class for asynchronous operations.
|
---|
180 | *
|
---|
181 | * @note Instances of this class must be created using new() because the
|
---|
182 | * task thread function will delete them when the task is complete.
|
---|
183 | *
|
---|
184 | * @note The constructor of this class adds a caller on the managed Medium
|
---|
185 | * object which is automatically released upon destruction.
|
---|
186 | */
|
---|
187 | class Medium::Task
|
---|
188 | {
|
---|
189 | public:
|
---|
190 | Task(Medium *aMedium, Progress *aProgress)
|
---|
191 | : mVDOperationIfaces(NULL),
|
---|
192 | m_pllRegistriesThatNeedSaving(NULL),
|
---|
193 | mMedium(aMedium),
|
---|
194 | mMediumCaller(aMedium),
|
---|
195 | mThread(NIL_RTTHREAD),
|
---|
196 | mProgress(aProgress),
|
---|
197 | mVirtualBoxCaller(NULL)
|
---|
198 | {
|
---|
199 | AssertReturnVoidStmt(aMedium, mRC = E_FAIL);
|
---|
200 | mRC = mMediumCaller.rc();
|
---|
201 | if (FAILED(mRC))
|
---|
202 | return;
|
---|
203 |
|
---|
204 | /* Get strong VirtualBox reference, see below. */
|
---|
205 | VirtualBox *pVirtualBox = aMedium->m->pVirtualBox;
|
---|
206 | mVirtualBox = pVirtualBox;
|
---|
207 | mVirtualBoxCaller.attach(pVirtualBox);
|
---|
208 | mRC = mVirtualBoxCaller.rc();
|
---|
209 | if (FAILED(mRC))
|
---|
210 | return;
|
---|
211 |
|
---|
212 | /* Set up a per-operation progress interface, can be used freely (for
|
---|
213 | * binary operations you can use it either on the source or target). */
|
---|
214 | mVDIfCallsProgress.cbSize = sizeof(VDINTERFACEPROGRESS);
|
---|
215 | mVDIfCallsProgress.enmInterface = VDINTERFACETYPE_PROGRESS;
|
---|
216 | mVDIfCallsProgress.pfnProgress = vdProgressCall;
|
---|
217 | int vrc = VDInterfaceAdd(&mVDIfProgress,
|
---|
218 | "Medium::Task::vdInterfaceProgress",
|
---|
219 | VDINTERFACETYPE_PROGRESS,
|
---|
220 | &mVDIfCallsProgress,
|
---|
221 | mProgress,
|
---|
222 | &mVDOperationIfaces);
|
---|
223 | AssertRC(vrc);
|
---|
224 | if (RT_FAILURE(vrc))
|
---|
225 | mRC = E_FAIL;
|
---|
226 | }
|
---|
227 |
|
---|
228 | // Make all destructors virtual. Just in case.
|
---|
229 | virtual ~Task()
|
---|
230 | {}
|
---|
231 |
|
---|
232 | HRESULT rc() const { return mRC; }
|
---|
233 | bool isOk() const { return SUCCEEDED(rc()); }
|
---|
234 |
|
---|
235 | static int fntMediumTask(RTTHREAD aThread, void *pvUser);
|
---|
236 |
|
---|
237 | bool isAsync() { return mThread != NIL_RTTHREAD; }
|
---|
238 |
|
---|
239 | PVDINTERFACE mVDOperationIfaces;
|
---|
240 |
|
---|
241 | // Whether the caller needs to call VirtualBox::saveSettings() after
|
---|
242 | // the task function returns. Only used in synchronous (wait) mode;
|
---|
243 | // otherwise the task will save the settings itself.
|
---|
244 | GuidList *m_pllRegistriesThatNeedSaving;
|
---|
245 |
|
---|
246 | const ComObjPtr<Medium> mMedium;
|
---|
247 | AutoCaller mMediumCaller;
|
---|
248 |
|
---|
249 | friend HRESULT Medium::runNow(Medium::Task*, GuidList *);
|
---|
250 |
|
---|
251 | protected:
|
---|
252 | HRESULT mRC;
|
---|
253 | RTTHREAD mThread;
|
---|
254 |
|
---|
255 | private:
|
---|
256 | virtual HRESULT handler() = 0;
|
---|
257 |
|
---|
258 | const ComObjPtr<Progress> mProgress;
|
---|
259 |
|
---|
260 | static DECLCALLBACK(int) vdProgressCall(void *pvUser, unsigned uPercent);
|
---|
261 |
|
---|
262 | VDINTERFACE mVDIfProgress;
|
---|
263 | VDINTERFACEPROGRESS mVDIfCallsProgress;
|
---|
264 |
|
---|
265 | /* Must have a strong VirtualBox reference during a task otherwise the
|
---|
266 | * reference count might drop to 0 while a task is still running. This
|
---|
267 | * would result in weird behavior, including deadlocks due to uninit and
|
---|
268 | * locking order issues. The deadlock often is not detectable because the
|
---|
269 | * uninit uses event semaphores which sabotages deadlock detection. */
|
---|
270 | ComObjPtr<VirtualBox> mVirtualBox;
|
---|
271 | AutoCaller mVirtualBoxCaller;
|
---|
272 | };
|
---|
273 |
|
---|
274 | class Medium::CreateBaseTask : public Medium::Task
|
---|
275 | {
|
---|
276 | public:
|
---|
277 | CreateBaseTask(Medium *aMedium,
|
---|
278 | Progress *aProgress,
|
---|
279 | uint64_t aSize,
|
---|
280 | MediumVariant_T aVariant)
|
---|
281 | : Medium::Task(aMedium, aProgress),
|
---|
282 | mSize(aSize),
|
---|
283 | mVariant(aVariant)
|
---|
284 | {}
|
---|
285 |
|
---|
286 | uint64_t mSize;
|
---|
287 | MediumVariant_T mVariant;
|
---|
288 |
|
---|
289 | private:
|
---|
290 | virtual HRESULT handler();
|
---|
291 | };
|
---|
292 |
|
---|
293 | class Medium::CreateDiffTask : public Medium::Task
|
---|
294 | {
|
---|
295 | public:
|
---|
296 | CreateDiffTask(Medium *aMedium,
|
---|
297 | Progress *aProgress,
|
---|
298 | Medium *aTarget,
|
---|
299 | MediumVariant_T aVariant,
|
---|
300 | MediumLockList *aMediumLockList,
|
---|
301 | bool fKeepMediumLockList = false)
|
---|
302 | : Medium::Task(aMedium, aProgress),
|
---|
303 | mpMediumLockList(aMediumLockList),
|
---|
304 | mTarget(aTarget),
|
---|
305 | mVariant(aVariant),
|
---|
306 | mTargetCaller(aTarget),
|
---|
307 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
308 | {
|
---|
309 | AssertReturnVoidStmt(aTarget != NULL, mRC = E_FAIL);
|
---|
310 | mRC = mTargetCaller.rc();
|
---|
311 | if (FAILED(mRC))
|
---|
312 | return;
|
---|
313 | }
|
---|
314 |
|
---|
315 | ~CreateDiffTask()
|
---|
316 | {
|
---|
317 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
318 | delete mpMediumLockList;
|
---|
319 | }
|
---|
320 |
|
---|
321 | MediumLockList *mpMediumLockList;
|
---|
322 |
|
---|
323 | const ComObjPtr<Medium> mTarget;
|
---|
324 | MediumVariant_T mVariant;
|
---|
325 |
|
---|
326 | private:
|
---|
327 | virtual HRESULT handler();
|
---|
328 |
|
---|
329 | AutoCaller mTargetCaller;
|
---|
330 | bool mfKeepMediumLockList;
|
---|
331 | };
|
---|
332 |
|
---|
333 | class Medium::CloneTask : public Medium::Task
|
---|
334 | {
|
---|
335 | public:
|
---|
336 | CloneTask(Medium *aMedium,
|
---|
337 | Progress *aProgress,
|
---|
338 | Medium *aTarget,
|
---|
339 | MediumVariant_T aVariant,
|
---|
340 | Medium *aParent,
|
---|
341 | MediumLockList *aSourceMediumLockList,
|
---|
342 | MediumLockList *aTargetMediumLockList,
|
---|
343 | bool fKeepSourceMediumLockList = false,
|
---|
344 | bool fKeepTargetMediumLockList = false)
|
---|
345 | : Medium::Task(aMedium, aProgress),
|
---|
346 | mTarget(aTarget),
|
---|
347 | mParent(aParent),
|
---|
348 | mpSourceMediumLockList(aSourceMediumLockList),
|
---|
349 | mpTargetMediumLockList(aTargetMediumLockList),
|
---|
350 | mVariant(aVariant),
|
---|
351 | mTargetCaller(aTarget),
|
---|
352 | mParentCaller(aParent),
|
---|
353 | mfKeepSourceMediumLockList(fKeepSourceMediumLockList),
|
---|
354 | mfKeepTargetMediumLockList(fKeepTargetMediumLockList)
|
---|
355 | {
|
---|
356 | AssertReturnVoidStmt(aTarget != NULL, mRC = E_FAIL);
|
---|
357 | mRC = mTargetCaller.rc();
|
---|
358 | if (FAILED(mRC))
|
---|
359 | return;
|
---|
360 | /* aParent may be NULL */
|
---|
361 | mRC = mParentCaller.rc();
|
---|
362 | if (FAILED(mRC))
|
---|
363 | return;
|
---|
364 | AssertReturnVoidStmt(aSourceMediumLockList != NULL, mRC = E_FAIL);
|
---|
365 | AssertReturnVoidStmt(aTargetMediumLockList != NULL, mRC = E_FAIL);
|
---|
366 | }
|
---|
367 |
|
---|
368 | ~CloneTask()
|
---|
369 | {
|
---|
370 | if (!mfKeepSourceMediumLockList && mpSourceMediumLockList)
|
---|
371 | delete mpSourceMediumLockList;
|
---|
372 | if (!mfKeepTargetMediumLockList && mpTargetMediumLockList)
|
---|
373 | delete mpTargetMediumLockList;
|
---|
374 | }
|
---|
375 |
|
---|
376 | const ComObjPtr<Medium> mTarget;
|
---|
377 | const ComObjPtr<Medium> mParent;
|
---|
378 | MediumLockList *mpSourceMediumLockList;
|
---|
379 | MediumLockList *mpTargetMediumLockList;
|
---|
380 | MediumVariant_T mVariant;
|
---|
381 |
|
---|
382 | private:
|
---|
383 | virtual HRESULT handler();
|
---|
384 |
|
---|
385 | AutoCaller mTargetCaller;
|
---|
386 | AutoCaller mParentCaller;
|
---|
387 | bool mfKeepSourceMediumLockList;
|
---|
388 | bool mfKeepTargetMediumLockList;
|
---|
389 | };
|
---|
390 |
|
---|
391 | class Medium::CompactTask : public Medium::Task
|
---|
392 | {
|
---|
393 | public:
|
---|
394 | CompactTask(Medium *aMedium,
|
---|
395 | Progress *aProgress,
|
---|
396 | MediumLockList *aMediumLockList,
|
---|
397 | bool fKeepMediumLockList = false)
|
---|
398 | : Medium::Task(aMedium, aProgress),
|
---|
399 | mpMediumLockList(aMediumLockList),
|
---|
400 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
401 | {
|
---|
402 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
403 | }
|
---|
404 |
|
---|
405 | ~CompactTask()
|
---|
406 | {
|
---|
407 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
408 | delete mpMediumLockList;
|
---|
409 | }
|
---|
410 |
|
---|
411 | MediumLockList *mpMediumLockList;
|
---|
412 |
|
---|
413 | private:
|
---|
414 | virtual HRESULT handler();
|
---|
415 |
|
---|
416 | bool mfKeepMediumLockList;
|
---|
417 | };
|
---|
418 |
|
---|
419 | class Medium::ResizeTask : public Medium::Task
|
---|
420 | {
|
---|
421 | public:
|
---|
422 | ResizeTask(Medium *aMedium,
|
---|
423 | uint64_t aSize,
|
---|
424 | Progress *aProgress,
|
---|
425 | MediumLockList *aMediumLockList,
|
---|
426 | bool fKeepMediumLockList = false)
|
---|
427 | : Medium::Task(aMedium, aProgress),
|
---|
428 | mSize(aSize),
|
---|
429 | mpMediumLockList(aMediumLockList),
|
---|
430 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
431 | {
|
---|
432 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
433 | }
|
---|
434 |
|
---|
435 | ~ResizeTask()
|
---|
436 | {
|
---|
437 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
438 | delete mpMediumLockList;
|
---|
439 | }
|
---|
440 |
|
---|
441 | uint64_t mSize;
|
---|
442 | MediumLockList *mpMediumLockList;
|
---|
443 |
|
---|
444 | private:
|
---|
445 | virtual HRESULT handler();
|
---|
446 |
|
---|
447 | bool mfKeepMediumLockList;
|
---|
448 | };
|
---|
449 |
|
---|
450 | class Medium::ResetTask : public Medium::Task
|
---|
451 | {
|
---|
452 | public:
|
---|
453 | ResetTask(Medium *aMedium,
|
---|
454 | Progress *aProgress,
|
---|
455 | MediumLockList *aMediumLockList,
|
---|
456 | bool fKeepMediumLockList = false)
|
---|
457 | : Medium::Task(aMedium, aProgress),
|
---|
458 | mpMediumLockList(aMediumLockList),
|
---|
459 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
460 | {}
|
---|
461 |
|
---|
462 | ~ResetTask()
|
---|
463 | {
|
---|
464 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
465 | delete mpMediumLockList;
|
---|
466 | }
|
---|
467 |
|
---|
468 | MediumLockList *mpMediumLockList;
|
---|
469 |
|
---|
470 | private:
|
---|
471 | virtual HRESULT handler();
|
---|
472 |
|
---|
473 | bool mfKeepMediumLockList;
|
---|
474 | };
|
---|
475 |
|
---|
476 | class Medium::DeleteTask : public Medium::Task
|
---|
477 | {
|
---|
478 | public:
|
---|
479 | DeleteTask(Medium *aMedium,
|
---|
480 | Progress *aProgress,
|
---|
481 | MediumLockList *aMediumLockList,
|
---|
482 | bool fKeepMediumLockList = false)
|
---|
483 | : Medium::Task(aMedium, aProgress),
|
---|
484 | mpMediumLockList(aMediumLockList),
|
---|
485 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
486 | {}
|
---|
487 |
|
---|
488 | ~DeleteTask()
|
---|
489 | {
|
---|
490 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
491 | delete mpMediumLockList;
|
---|
492 | }
|
---|
493 |
|
---|
494 | MediumLockList *mpMediumLockList;
|
---|
495 |
|
---|
496 | private:
|
---|
497 | virtual HRESULT handler();
|
---|
498 |
|
---|
499 | bool mfKeepMediumLockList;
|
---|
500 | };
|
---|
501 |
|
---|
502 | class Medium::MergeTask : public Medium::Task
|
---|
503 | {
|
---|
504 | public:
|
---|
505 | MergeTask(Medium *aMedium,
|
---|
506 | Medium *aTarget,
|
---|
507 | bool fMergeForward,
|
---|
508 | Medium *aParentForTarget,
|
---|
509 | const MediaList &aChildrenToReparent,
|
---|
510 | Progress *aProgress,
|
---|
511 | MediumLockList *aMediumLockList,
|
---|
512 | bool fKeepMediumLockList = false)
|
---|
513 | : Medium::Task(aMedium, aProgress),
|
---|
514 | mTarget(aTarget),
|
---|
515 | mfMergeForward(fMergeForward),
|
---|
516 | mParentForTarget(aParentForTarget),
|
---|
517 | mChildrenToReparent(aChildrenToReparent),
|
---|
518 | mpMediumLockList(aMediumLockList),
|
---|
519 | mTargetCaller(aTarget),
|
---|
520 | mParentForTargetCaller(aParentForTarget),
|
---|
521 | mfChildrenCaller(false),
|
---|
522 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
523 | {
|
---|
524 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
525 | for (MediaList::const_iterator it = mChildrenToReparent.begin();
|
---|
526 | it != mChildrenToReparent.end();
|
---|
527 | ++it)
|
---|
528 | {
|
---|
529 | HRESULT rc2 = (*it)->addCaller();
|
---|
530 | if (FAILED(rc2))
|
---|
531 | {
|
---|
532 | mRC = E_FAIL;
|
---|
533 | for (MediaList::const_iterator it2 = mChildrenToReparent.begin();
|
---|
534 | it2 != it;
|
---|
535 | --it2)
|
---|
536 | {
|
---|
537 | (*it2)->releaseCaller();
|
---|
538 | }
|
---|
539 | return;
|
---|
540 | }
|
---|
541 | }
|
---|
542 | mfChildrenCaller = true;
|
---|
543 | }
|
---|
544 |
|
---|
545 | ~MergeTask()
|
---|
546 | {
|
---|
547 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
548 | delete mpMediumLockList;
|
---|
549 | if (mfChildrenCaller)
|
---|
550 | {
|
---|
551 | for (MediaList::const_iterator it = mChildrenToReparent.begin();
|
---|
552 | it != mChildrenToReparent.end();
|
---|
553 | ++it)
|
---|
554 | {
|
---|
555 | (*it)->releaseCaller();
|
---|
556 | }
|
---|
557 | }
|
---|
558 | }
|
---|
559 |
|
---|
560 | const ComObjPtr<Medium> mTarget;
|
---|
561 | bool mfMergeForward;
|
---|
562 | /* When mChildrenToReparent is empty then mParentForTarget is non-null.
|
---|
563 | * In other words: they are used in different cases. */
|
---|
564 | const ComObjPtr<Medium> mParentForTarget;
|
---|
565 | MediaList mChildrenToReparent;
|
---|
566 | MediumLockList *mpMediumLockList;
|
---|
567 |
|
---|
568 | private:
|
---|
569 | virtual HRESULT handler();
|
---|
570 |
|
---|
571 | AutoCaller mTargetCaller;
|
---|
572 | AutoCaller mParentForTargetCaller;
|
---|
573 | bool mfChildrenCaller;
|
---|
574 | bool mfKeepMediumLockList;
|
---|
575 | };
|
---|
576 |
|
---|
577 | class Medium::ExportTask : public Medium::Task
|
---|
578 | {
|
---|
579 | public:
|
---|
580 | ExportTask(Medium *aMedium,
|
---|
581 | Progress *aProgress,
|
---|
582 | const char *aFilename,
|
---|
583 | MediumFormat *aFormat,
|
---|
584 | MediumVariant_T aVariant,
|
---|
585 | void *aVDImageIOCallbacks,
|
---|
586 | void *aVDImageIOUser,
|
---|
587 | MediumLockList *aSourceMediumLockList,
|
---|
588 | bool fKeepSourceMediumLockList = false)
|
---|
589 | : Medium::Task(aMedium, aProgress),
|
---|
590 | mpSourceMediumLockList(aSourceMediumLockList),
|
---|
591 | mFilename(aFilename),
|
---|
592 | mFormat(aFormat),
|
---|
593 | mVariant(aVariant),
|
---|
594 | mfKeepSourceMediumLockList(fKeepSourceMediumLockList)
|
---|
595 | {
|
---|
596 | AssertReturnVoidStmt(aSourceMediumLockList != NULL, mRC = E_FAIL);
|
---|
597 |
|
---|
598 | mVDImageIfaces = aMedium->m->vdImageIfaces;
|
---|
599 | if (aVDImageIOCallbacks)
|
---|
600 | {
|
---|
601 | int vrc = VDInterfaceAdd(&mVDInterfaceIO, "Medium::vdInterfaceIO",
|
---|
602 | VDINTERFACETYPE_IO, aVDImageIOCallbacks,
|
---|
603 | aVDImageIOUser, &mVDImageIfaces);
|
---|
604 | AssertRCReturnVoidStmt(vrc, mRC = E_FAIL);
|
---|
605 | }
|
---|
606 | }
|
---|
607 |
|
---|
608 | ~ExportTask()
|
---|
609 | {
|
---|
610 | if (!mfKeepSourceMediumLockList && mpSourceMediumLockList)
|
---|
611 | delete mpSourceMediumLockList;
|
---|
612 | }
|
---|
613 |
|
---|
614 | MediumLockList *mpSourceMediumLockList;
|
---|
615 | Utf8Str mFilename;
|
---|
616 | ComObjPtr<MediumFormat> mFormat;
|
---|
617 | MediumVariant_T mVariant;
|
---|
618 | PVDINTERFACE mVDImageIfaces;
|
---|
619 |
|
---|
620 | private:
|
---|
621 | virtual HRESULT handler();
|
---|
622 |
|
---|
623 | bool mfKeepSourceMediumLockList;
|
---|
624 | VDINTERFACE mVDInterfaceIO;
|
---|
625 | };
|
---|
626 |
|
---|
627 | class Medium::ImportTask : public Medium::Task
|
---|
628 | {
|
---|
629 | public:
|
---|
630 | ImportTask(Medium *aMedium,
|
---|
631 | Progress *aProgress,
|
---|
632 | const char *aFilename,
|
---|
633 | MediumFormat *aFormat,
|
---|
634 | MediumVariant_T aVariant,
|
---|
635 | void *aVDImageIOCallbacks,
|
---|
636 | void *aVDImageIOUser,
|
---|
637 | Medium *aParent,
|
---|
638 | MediumLockList *aTargetMediumLockList,
|
---|
639 | bool fKeepTargetMediumLockList = false)
|
---|
640 | : Medium::Task(aMedium, aProgress),
|
---|
641 | mFilename(aFilename),
|
---|
642 | mFormat(aFormat),
|
---|
643 | mVariant(aVariant),
|
---|
644 | mParent(aParent),
|
---|
645 | mpTargetMediumLockList(aTargetMediumLockList),
|
---|
646 | mParentCaller(aParent),
|
---|
647 | mfKeepTargetMediumLockList(fKeepTargetMediumLockList)
|
---|
648 | {
|
---|
649 | AssertReturnVoidStmt(aTargetMediumLockList != NULL, mRC = E_FAIL);
|
---|
650 | /* aParent may be NULL */
|
---|
651 | mRC = mParentCaller.rc();
|
---|
652 | if (FAILED(mRC))
|
---|
653 | return;
|
---|
654 |
|
---|
655 | mVDImageIfaces = aMedium->m->vdImageIfaces;
|
---|
656 | if (aVDImageIOCallbacks)
|
---|
657 | {
|
---|
658 | int vrc = VDInterfaceAdd(&mVDInterfaceIO, "Medium::vdInterfaceIO",
|
---|
659 | VDINTERFACETYPE_IO, aVDImageIOCallbacks,
|
---|
660 | aVDImageIOUser, &mVDImageIfaces);
|
---|
661 | AssertRCReturnVoidStmt(vrc, mRC = E_FAIL);
|
---|
662 | }
|
---|
663 | }
|
---|
664 |
|
---|
665 | ~ImportTask()
|
---|
666 | {
|
---|
667 | if (!mfKeepTargetMediumLockList && mpTargetMediumLockList)
|
---|
668 | delete mpTargetMediumLockList;
|
---|
669 | }
|
---|
670 |
|
---|
671 | Utf8Str mFilename;
|
---|
672 | ComObjPtr<MediumFormat> mFormat;
|
---|
673 | MediumVariant_T mVariant;
|
---|
674 | const ComObjPtr<Medium> mParent;
|
---|
675 | MediumLockList *mpTargetMediumLockList;
|
---|
676 | PVDINTERFACE mVDImageIfaces;
|
---|
677 |
|
---|
678 | private:
|
---|
679 | virtual HRESULT handler();
|
---|
680 |
|
---|
681 | AutoCaller mParentCaller;
|
---|
682 | bool mfKeepTargetMediumLockList;
|
---|
683 | VDINTERFACE mVDInterfaceIO;
|
---|
684 | };
|
---|
685 |
|
---|
686 | /**
|
---|
687 | * Thread function for time-consuming medium tasks.
|
---|
688 | *
|
---|
689 | * @param pvUser Pointer to the Medium::Task instance.
|
---|
690 | */
|
---|
691 | /* static */
|
---|
692 | DECLCALLBACK(int) Medium::Task::fntMediumTask(RTTHREAD aThread, void *pvUser)
|
---|
693 | {
|
---|
694 | LogFlowFuncEnter();
|
---|
695 | AssertReturn(pvUser, (int)E_INVALIDARG);
|
---|
696 | Medium::Task *pTask = static_cast<Medium::Task *>(pvUser);
|
---|
697 |
|
---|
698 | pTask->mThread = aThread;
|
---|
699 |
|
---|
700 | HRESULT rc = pTask->handler();
|
---|
701 |
|
---|
702 | /* complete the progress if run asynchronously */
|
---|
703 | if (pTask->isAsync())
|
---|
704 | {
|
---|
705 | if (!pTask->mProgress.isNull())
|
---|
706 | pTask->mProgress->notifyComplete(rc);
|
---|
707 | }
|
---|
708 |
|
---|
709 | /* pTask is no longer needed, delete it. */
|
---|
710 | delete pTask;
|
---|
711 |
|
---|
712 | LogFlowFunc(("rc=%Rhrc\n", rc));
|
---|
713 | LogFlowFuncLeave();
|
---|
714 |
|
---|
715 | return (int)rc;
|
---|
716 | }
|
---|
717 |
|
---|
718 | /**
|
---|
719 | * PFNVDPROGRESS callback handler for Task operations.
|
---|
720 | *
|
---|
721 | * @param pvUser Pointer to the Progress instance.
|
---|
722 | * @param uPercent Completion percentage (0-100).
|
---|
723 | */
|
---|
724 | /*static*/
|
---|
725 | DECLCALLBACK(int) Medium::Task::vdProgressCall(void *pvUser, unsigned uPercent)
|
---|
726 | {
|
---|
727 | Progress *that = static_cast<Progress *>(pvUser);
|
---|
728 |
|
---|
729 | if (that != NULL)
|
---|
730 | {
|
---|
731 | /* update the progress object, capping it at 99% as the final percent
|
---|
732 | * is used for additional operations like setting the UUIDs and similar. */
|
---|
733 | HRESULT rc = that->SetCurrentOperationProgress(uPercent * 99 / 100);
|
---|
734 | if (FAILED(rc))
|
---|
735 | {
|
---|
736 | if (rc == E_FAIL)
|
---|
737 | return VERR_CANCELLED;
|
---|
738 | else
|
---|
739 | return VERR_INVALID_STATE;
|
---|
740 | }
|
---|
741 | }
|
---|
742 |
|
---|
743 | return VINF_SUCCESS;
|
---|
744 | }
|
---|
745 |
|
---|
746 | /**
|
---|
747 | * Implementation code for the "create base" task.
|
---|
748 | */
|
---|
749 | HRESULT Medium::CreateBaseTask::handler()
|
---|
750 | {
|
---|
751 | return mMedium->taskCreateBaseHandler(*this);
|
---|
752 | }
|
---|
753 |
|
---|
754 | /**
|
---|
755 | * Implementation code for the "create diff" task.
|
---|
756 | */
|
---|
757 | HRESULT Medium::CreateDiffTask::handler()
|
---|
758 | {
|
---|
759 | return mMedium->taskCreateDiffHandler(*this);
|
---|
760 | }
|
---|
761 |
|
---|
762 | /**
|
---|
763 | * Implementation code for the "clone" task.
|
---|
764 | */
|
---|
765 | HRESULT Medium::CloneTask::handler()
|
---|
766 | {
|
---|
767 | return mMedium->taskCloneHandler(*this);
|
---|
768 | }
|
---|
769 |
|
---|
770 | /**
|
---|
771 | * Implementation code for the "compact" task.
|
---|
772 | */
|
---|
773 | HRESULT Medium::CompactTask::handler()
|
---|
774 | {
|
---|
775 | return mMedium->taskCompactHandler(*this);
|
---|
776 | }
|
---|
777 |
|
---|
778 | /**
|
---|
779 | * Implementation code for the "resize" task.
|
---|
780 | */
|
---|
781 | HRESULT Medium::ResizeTask::handler()
|
---|
782 | {
|
---|
783 | return mMedium->taskResizeHandler(*this);
|
---|
784 | }
|
---|
785 |
|
---|
786 |
|
---|
787 | /**
|
---|
788 | * Implementation code for the "reset" task.
|
---|
789 | */
|
---|
790 | HRESULT Medium::ResetTask::handler()
|
---|
791 | {
|
---|
792 | return mMedium->taskResetHandler(*this);
|
---|
793 | }
|
---|
794 |
|
---|
795 | /**
|
---|
796 | * Implementation code for the "delete" task.
|
---|
797 | */
|
---|
798 | HRESULT Medium::DeleteTask::handler()
|
---|
799 | {
|
---|
800 | return mMedium->taskDeleteHandler(*this);
|
---|
801 | }
|
---|
802 |
|
---|
803 | /**
|
---|
804 | * Implementation code for the "merge" task.
|
---|
805 | */
|
---|
806 | HRESULT Medium::MergeTask::handler()
|
---|
807 | {
|
---|
808 | return mMedium->taskMergeHandler(*this);
|
---|
809 | }
|
---|
810 |
|
---|
811 | /**
|
---|
812 | * Implementation code for the "export" task.
|
---|
813 | */
|
---|
814 | HRESULT Medium::ExportTask::handler()
|
---|
815 | {
|
---|
816 | return mMedium->taskExportHandler(*this);
|
---|
817 | }
|
---|
818 |
|
---|
819 | /**
|
---|
820 | * Implementation code for the "import" task.
|
---|
821 | */
|
---|
822 | HRESULT Medium::ImportTask::handler()
|
---|
823 | {
|
---|
824 | return mMedium->taskImportHandler(*this);
|
---|
825 | }
|
---|
826 |
|
---|
827 | ////////////////////////////////////////////////////////////////////////////////
|
---|
828 | //
|
---|
829 | // Medium constructor / destructor
|
---|
830 | //
|
---|
831 | ////////////////////////////////////////////////////////////////////////////////
|
---|
832 |
|
---|
833 | DEFINE_EMPTY_CTOR_DTOR(Medium)
|
---|
834 |
|
---|
835 | HRESULT Medium::FinalConstruct()
|
---|
836 | {
|
---|
837 | m = new Data;
|
---|
838 |
|
---|
839 | /* Initialize the callbacks of the VD error interface */
|
---|
840 | m->vdIfCallsError.cbSize = sizeof(VDINTERFACEERROR);
|
---|
841 | m->vdIfCallsError.enmInterface = VDINTERFACETYPE_ERROR;
|
---|
842 | m->vdIfCallsError.pfnError = vdErrorCall;
|
---|
843 | m->vdIfCallsError.pfnMessage = NULL;
|
---|
844 |
|
---|
845 | /* Initialize the callbacks of the VD config interface */
|
---|
846 | m->vdIfCallsConfig.cbSize = sizeof(VDINTERFACECONFIG);
|
---|
847 | m->vdIfCallsConfig.enmInterface = VDINTERFACETYPE_CONFIG;
|
---|
848 | m->vdIfCallsConfig.pfnAreKeysValid = vdConfigAreKeysValid;
|
---|
849 | m->vdIfCallsConfig.pfnQuerySize = vdConfigQuerySize;
|
---|
850 | m->vdIfCallsConfig.pfnQuery = vdConfigQuery;
|
---|
851 |
|
---|
852 | /* Initialize the callbacks of the VD TCP interface (we always use the host
|
---|
853 | * IP stack for now) */
|
---|
854 | m->vdIfCallsTcpNet.cbSize = sizeof(VDINTERFACETCPNET);
|
---|
855 | m->vdIfCallsTcpNet.enmInterface = VDINTERFACETYPE_TCPNET;
|
---|
856 | m->vdIfCallsTcpNet.pfnSocketCreate = vdTcpSocketCreate;
|
---|
857 | m->vdIfCallsTcpNet.pfnSocketDestroy = vdTcpSocketDestroy;
|
---|
858 | m->vdIfCallsTcpNet.pfnClientConnect = vdTcpClientConnect;
|
---|
859 | m->vdIfCallsTcpNet.pfnClientClose = vdTcpClientClose;
|
---|
860 | m->vdIfCallsTcpNet.pfnIsClientConnected = vdTcpIsClientConnected;
|
---|
861 | m->vdIfCallsTcpNet.pfnSelectOne = vdTcpSelectOne;
|
---|
862 | m->vdIfCallsTcpNet.pfnRead = vdTcpRead;
|
---|
863 | m->vdIfCallsTcpNet.pfnWrite = vdTcpWrite;
|
---|
864 | m->vdIfCallsTcpNet.pfnSgWrite = vdTcpSgWrite;
|
---|
865 | m->vdIfCallsTcpNet.pfnFlush = vdTcpFlush;
|
---|
866 | m->vdIfCallsTcpNet.pfnSetSendCoalescing = vdTcpSetSendCoalescing;
|
---|
867 | m->vdIfCallsTcpNet.pfnGetLocalAddress = vdTcpGetLocalAddress;
|
---|
868 | m->vdIfCallsTcpNet.pfnGetPeerAddress = vdTcpGetPeerAddress;
|
---|
869 | m->vdIfCallsTcpNet.pfnSelectOneEx = NULL;
|
---|
870 | m->vdIfCallsTcpNet.pfnPoke = NULL;
|
---|
871 |
|
---|
872 | /* Initialize the per-disk interface chain (could be done more globally,
|
---|
873 | * but it's not wasting much time or space so it's not worth it). */
|
---|
874 | int vrc;
|
---|
875 | vrc = VDInterfaceAdd(&m->vdIfError,
|
---|
876 | "Medium::vdInterfaceError",
|
---|
877 | VDINTERFACETYPE_ERROR,
|
---|
878 | &m->vdIfCallsError, this, &m->vdDiskIfaces);
|
---|
879 | AssertRCReturn(vrc, E_FAIL);
|
---|
880 |
|
---|
881 | /* Initialize the per-image interface chain */
|
---|
882 | vrc = VDInterfaceAdd(&m->vdIfConfig,
|
---|
883 | "Medium::vdInterfaceConfig",
|
---|
884 | VDINTERFACETYPE_CONFIG,
|
---|
885 | &m->vdIfCallsConfig, this, &m->vdImageIfaces);
|
---|
886 | AssertRCReturn(vrc, E_FAIL);
|
---|
887 |
|
---|
888 | vrc = VDInterfaceAdd(&m->vdIfTcpNet,
|
---|
889 | "Medium::vdInterfaceTcpNet",
|
---|
890 | VDINTERFACETYPE_TCPNET,
|
---|
891 | &m->vdIfCallsTcpNet, this, &m->vdImageIfaces);
|
---|
892 | AssertRCReturn(vrc, E_FAIL);
|
---|
893 |
|
---|
894 | vrc = RTSemEventMultiCreate(&m->queryInfoSem);
|
---|
895 | AssertRCReturn(vrc, E_FAIL);
|
---|
896 | vrc = RTSemEventMultiSignal(m->queryInfoSem);
|
---|
897 | AssertRCReturn(vrc, E_FAIL);
|
---|
898 |
|
---|
899 | return S_OK;
|
---|
900 | }
|
---|
901 |
|
---|
902 | void Medium::FinalRelease()
|
---|
903 | {
|
---|
904 | uninit();
|
---|
905 |
|
---|
906 | delete m;
|
---|
907 | }
|
---|
908 |
|
---|
909 | /**
|
---|
910 | * Initializes an empty hard disk object without creating or opening an associated
|
---|
911 | * storage unit.
|
---|
912 | *
|
---|
913 | * This gets called by VirtualBox::CreateHardDisk() in which case uuidMachineRegistry
|
---|
914 | * is empty since starting with VirtualBox 4.0, we no longer add opened media to a
|
---|
915 | * registry automatically (this is deferred until the medium is attached to a machine).
|
---|
916 | *
|
---|
917 | * This also gets called when VirtualBox creates diff images; in this case uuidMachineRegistry
|
---|
918 | * is set to the registry of the parent image to make sure they all end up in the same
|
---|
919 | * file.
|
---|
920 | *
|
---|
921 | * For hard disks that don't have the VD_CAP_CREATE_FIXED or
|
---|
922 | * VD_CAP_CREATE_DYNAMIC capability (and therefore cannot be created or deleted
|
---|
923 | * with the means of VirtualBox) the associated storage unit is assumed to be
|
---|
924 | * ready for use so the state of the hard disk object will be set to Created.
|
---|
925 | *
|
---|
926 | * @param aVirtualBox VirtualBox object.
|
---|
927 | * @param aFormat
|
---|
928 | * @param aLocation Storage unit location.
|
---|
929 | * @param uuidMachineRegistry The registry to which this medium should be added (global registry UUI or medium UUID or empty if none).
|
---|
930 | * @param pllRegistriesThatNeedSaving Optional list to receive the UUIDs of the media registries that need saving.
|
---|
931 | */
|
---|
932 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
933 | const Utf8Str &aFormat,
|
---|
934 | const Utf8Str &aLocation,
|
---|
935 | const Guid &uuidMachineRegistry,
|
---|
936 | GuidList *pllRegistriesThatNeedSaving)
|
---|
937 | {
|
---|
938 | AssertReturn(aVirtualBox != NULL, E_FAIL);
|
---|
939 | AssertReturn(!aFormat.isEmpty(), E_FAIL);
|
---|
940 |
|
---|
941 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
942 | AutoInitSpan autoInitSpan(this);
|
---|
943 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
944 |
|
---|
945 | HRESULT rc = S_OK;
|
---|
946 |
|
---|
947 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
948 |
|
---|
949 | if (!uuidMachineRegistry.isEmpty())
|
---|
950 | m->llRegistryIDs.push_back(uuidMachineRegistry);
|
---|
951 |
|
---|
952 | /* no storage yet */
|
---|
953 | m->state = MediumState_NotCreated;
|
---|
954 |
|
---|
955 | /* cannot be a host drive */
|
---|
956 | m->hostDrive = false;
|
---|
957 |
|
---|
958 | /* No storage unit is created yet, no need to queryInfo() */
|
---|
959 |
|
---|
960 | rc = setFormat(aFormat);
|
---|
961 | if (FAILED(rc)) return rc;
|
---|
962 |
|
---|
963 | rc = setLocation(aLocation);
|
---|
964 | if (FAILED(rc)) return rc;
|
---|
965 |
|
---|
966 | if (!(m->formatObj->getCapabilities() & ( MediumFormatCapabilities_CreateFixed
|
---|
967 | | MediumFormatCapabilities_CreateDynamic))
|
---|
968 | )
|
---|
969 | {
|
---|
970 | /* Storage for hard disks of this format can neither be explicitly
|
---|
971 | * created by VirtualBox nor deleted, so we place the hard disk to
|
---|
972 | * Inaccessible state here and also add it to the registry. The
|
---|
973 | * state means that one has to use RefreshState() to update the
|
---|
974 | * medium format specific fields. */
|
---|
975 | m->state = MediumState_Inaccessible;
|
---|
976 | // create new UUID
|
---|
977 | unconst(m->id).create();
|
---|
978 |
|
---|
979 | AutoWriteLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
980 | rc = m->pVirtualBox->registerHardDisk(this, pllRegistriesThatNeedSaving);
|
---|
981 | }
|
---|
982 |
|
---|
983 | /* Confirm a successful initialization when it's the case */
|
---|
984 | if (SUCCEEDED(rc))
|
---|
985 | autoInitSpan.setSucceeded();
|
---|
986 |
|
---|
987 | return rc;
|
---|
988 | }
|
---|
989 |
|
---|
990 | /**
|
---|
991 | * Initializes the medium object by opening the storage unit at the specified
|
---|
992 | * location. The enOpenMode parameter defines whether the medium will be opened
|
---|
993 | * read/write or read-only.
|
---|
994 | *
|
---|
995 | * This gets called by VirtualBox::OpenMedium() and also by
|
---|
996 | * Machine::AttachDevice() and createImplicitDiffs() when new diff
|
---|
997 | * images are created.
|
---|
998 | *
|
---|
999 | * There is no registry for this case since starting with VirtualBox 4.0, we
|
---|
1000 | * no longer add opened media to a registry automatically (this is deferred
|
---|
1001 | * until the medium is attached to a machine).
|
---|
1002 | *
|
---|
1003 | * For hard disks, the UUID, format and the parent of this medium will be
|
---|
1004 | * determined when reading the medium storage unit. For DVD and floppy images,
|
---|
1005 | * which have no UUIDs in their storage units, new UUIDs are created.
|
---|
1006 | * If the detected or set parent is not known to VirtualBox, then this method
|
---|
1007 | * will fail.
|
---|
1008 | *
|
---|
1009 | * @param aVirtualBox VirtualBox object.
|
---|
1010 | * @param aLocation Storage unit location.
|
---|
1011 | * @param enOpenMode Whether to open the medium read/write or read-only.
|
---|
1012 | * @param aDeviceType Device type of medium.
|
---|
1013 | */
|
---|
1014 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1015 | const Utf8Str &aLocation,
|
---|
1016 | HDDOpenMode enOpenMode,
|
---|
1017 | DeviceType_T aDeviceType)
|
---|
1018 | {
|
---|
1019 | AssertReturn(aVirtualBox, E_INVALIDARG);
|
---|
1020 | AssertReturn(!aLocation.isEmpty(), E_INVALIDARG);
|
---|
1021 |
|
---|
1022 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1023 | AutoInitSpan autoInitSpan(this);
|
---|
1024 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1025 |
|
---|
1026 | HRESULT rc = S_OK;
|
---|
1027 |
|
---|
1028 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1029 |
|
---|
1030 | /* there must be a storage unit */
|
---|
1031 | m->state = MediumState_Created;
|
---|
1032 |
|
---|
1033 | /* remember device type for correct unregistering later */
|
---|
1034 | m->devType = aDeviceType;
|
---|
1035 |
|
---|
1036 | /* cannot be a host drive */
|
---|
1037 | m->hostDrive = false;
|
---|
1038 |
|
---|
1039 | /* remember the open mode (defaults to ReadWrite) */
|
---|
1040 | m->hddOpenMode = enOpenMode;
|
---|
1041 |
|
---|
1042 | if (aDeviceType == DeviceType_DVD)
|
---|
1043 | m->type = MediumType_Readonly;
|
---|
1044 | else if (aDeviceType == DeviceType_Floppy)
|
---|
1045 | m->type = MediumType_Writethrough;
|
---|
1046 |
|
---|
1047 | rc = setLocation(aLocation);
|
---|
1048 | if (FAILED(rc)) return rc;
|
---|
1049 |
|
---|
1050 | /* get all the information about the medium from the storage unit */
|
---|
1051 | rc = queryInfo(false /* fSetImageId */, false /* fSetParentId */);
|
---|
1052 |
|
---|
1053 | if (SUCCEEDED(rc))
|
---|
1054 | {
|
---|
1055 | /* if the storage unit is not accessible, it's not acceptable for the
|
---|
1056 | * newly opened media so convert this into an error */
|
---|
1057 | if (m->state == MediumState_Inaccessible)
|
---|
1058 | {
|
---|
1059 | Assert(!m->strLastAccessError.isEmpty());
|
---|
1060 | rc = setError(E_FAIL, "%s", m->strLastAccessError.c_str());
|
---|
1061 | }
|
---|
1062 | else
|
---|
1063 | {
|
---|
1064 | AssertReturn(!m->id.isEmpty(), E_FAIL);
|
---|
1065 |
|
---|
1066 | /* storage format must be detected by queryInfo() if the medium is accessible */
|
---|
1067 | AssertReturn(!m->strFormat.isEmpty(), E_FAIL);
|
---|
1068 | }
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 | /* Confirm a successful initialization when it's the case */
|
---|
1072 | if (SUCCEEDED(rc))
|
---|
1073 | autoInitSpan.setSucceeded();
|
---|
1074 |
|
---|
1075 | return rc;
|
---|
1076 | }
|
---|
1077 |
|
---|
1078 | /**
|
---|
1079 | * Initializes the medium object by loading its data from the given settings
|
---|
1080 | * node. In this mode, the medium will always be opened read/write.
|
---|
1081 | *
|
---|
1082 | * In this case, since we're loading from a registry, uuidMachineRegistry is
|
---|
1083 | * always set: it's either the global registry UUID or a machine UUID when
|
---|
1084 | * loading from a per-machine registry.
|
---|
1085 | *
|
---|
1086 | * @param aVirtualBox VirtualBox object.
|
---|
1087 | * @param aParent Parent medium disk or NULL for a root (base) medium.
|
---|
1088 | * @param aDeviceType Device type of the medium.
|
---|
1089 | * @param uuidMachineRegistry The registry to which this medium should be added (global registry UUI or medium UUID).
|
---|
1090 | * @param aNode Configuration settings.
|
---|
1091 | * @param strMachineFolder The machine folder with which to resolve relative paths; if empty, then we use the VirtualBox home directory
|
---|
1092 | *
|
---|
1093 | * @note Locks the medium tree for writing.
|
---|
1094 | */
|
---|
1095 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1096 | Medium *aParent,
|
---|
1097 | DeviceType_T aDeviceType,
|
---|
1098 | const Guid &uuidMachineRegistry,
|
---|
1099 | const settings::Medium &data,
|
---|
1100 | const Utf8Str &strMachineFolder)
|
---|
1101 | {
|
---|
1102 | using namespace settings;
|
---|
1103 |
|
---|
1104 | AssertReturn(aVirtualBox, E_INVALIDARG);
|
---|
1105 |
|
---|
1106 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1107 | AutoInitSpan autoInitSpan(this);
|
---|
1108 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1109 |
|
---|
1110 | HRESULT rc = S_OK;
|
---|
1111 |
|
---|
1112 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1113 |
|
---|
1114 | if (!uuidMachineRegistry.isEmpty())
|
---|
1115 | m->llRegistryIDs.push_back(uuidMachineRegistry);
|
---|
1116 |
|
---|
1117 | /* register with VirtualBox/parent early, since uninit() will
|
---|
1118 | * unconditionally unregister on failure */
|
---|
1119 | if (aParent)
|
---|
1120 | {
|
---|
1121 | // differencing medium: add to parent
|
---|
1122 | AutoWriteLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1123 | m->pParent = aParent;
|
---|
1124 | aParent->m->llChildren.push_back(this);
|
---|
1125 | }
|
---|
1126 |
|
---|
1127 | /* see below why we don't call queryInfo() (and therefore treat the medium
|
---|
1128 | * as inaccessible for now */
|
---|
1129 | m->state = MediumState_Inaccessible;
|
---|
1130 | m->strLastAccessError = tr("Accessibility check was not yet performed");
|
---|
1131 |
|
---|
1132 | /* required */
|
---|
1133 | unconst(m->id) = data.uuid;
|
---|
1134 |
|
---|
1135 | /* assume not a host drive */
|
---|
1136 | m->hostDrive = false;
|
---|
1137 |
|
---|
1138 | /* optional */
|
---|
1139 | m->strDescription = data.strDescription;
|
---|
1140 |
|
---|
1141 | /* required */
|
---|
1142 | if (aDeviceType == DeviceType_HardDisk)
|
---|
1143 | {
|
---|
1144 | AssertReturn(!data.strFormat.isEmpty(), E_FAIL);
|
---|
1145 | rc = setFormat(data.strFormat);
|
---|
1146 | if (FAILED(rc)) return rc;
|
---|
1147 | }
|
---|
1148 | else
|
---|
1149 | {
|
---|
1150 | /// @todo handle host drive settings here as well?
|
---|
1151 | if (!data.strFormat.isEmpty())
|
---|
1152 | rc = setFormat(data.strFormat);
|
---|
1153 | else
|
---|
1154 | rc = setFormat("RAW");
|
---|
1155 | if (FAILED(rc)) return rc;
|
---|
1156 | }
|
---|
1157 |
|
---|
1158 | /* optional, only for diffs, default is false; we can only auto-reset
|
---|
1159 | * diff media so they must have a parent */
|
---|
1160 | if (aParent != NULL)
|
---|
1161 | m->autoReset = data.fAutoReset;
|
---|
1162 | else
|
---|
1163 | m->autoReset = false;
|
---|
1164 |
|
---|
1165 | /* properties (after setting the format as it populates the map). Note that
|
---|
1166 | * if some properties are not supported but present in the settings file,
|
---|
1167 | * they will still be read and accessible (for possible backward
|
---|
1168 | * compatibility; we can also clean them up from the XML upon next
|
---|
1169 | * XML format version change if we wish) */
|
---|
1170 | for (settings::StringsMap::const_iterator it = data.properties.begin();
|
---|
1171 | it != data.properties.end();
|
---|
1172 | ++it)
|
---|
1173 | {
|
---|
1174 | const Utf8Str &name = it->first;
|
---|
1175 | const Utf8Str &value = it->second;
|
---|
1176 | m->mapProperties[name] = value;
|
---|
1177 | }
|
---|
1178 |
|
---|
1179 | // compose full path of the medium, if it's not fully qualified...
|
---|
1180 | // slightly convoluted logic here. If the caller has given us a
|
---|
1181 | // machine folder, then a relative path will be relative to that:
|
---|
1182 | Utf8Str strFull;
|
---|
1183 | if ( !strMachineFolder.isEmpty()
|
---|
1184 | && !RTPathStartsWithRoot(data.strLocation.c_str())
|
---|
1185 | )
|
---|
1186 | {
|
---|
1187 | strFull = strMachineFolder;
|
---|
1188 | strFull += RTPATH_SLASH;
|
---|
1189 | strFull += data.strLocation;
|
---|
1190 | }
|
---|
1191 | else
|
---|
1192 | {
|
---|
1193 | // Otherwise use the old VirtualBox "make absolute path" logic:
|
---|
1194 | rc = m->pVirtualBox->calculateFullPath(data.strLocation, strFull);
|
---|
1195 | if (FAILED(rc)) return rc;
|
---|
1196 | }
|
---|
1197 |
|
---|
1198 | rc = setLocation(strFull);
|
---|
1199 | if (FAILED(rc)) return rc;
|
---|
1200 |
|
---|
1201 | if (aDeviceType == DeviceType_HardDisk)
|
---|
1202 | {
|
---|
1203 | /* type is only for base hard disks */
|
---|
1204 | if (m->pParent.isNull())
|
---|
1205 | m->type = data.hdType;
|
---|
1206 | }
|
---|
1207 | else if (aDeviceType == DeviceType_DVD)
|
---|
1208 | m->type = MediumType_Readonly;
|
---|
1209 | else
|
---|
1210 | m->type = MediumType_Writethrough;
|
---|
1211 |
|
---|
1212 | /* remember device type for correct unregistering later */
|
---|
1213 | m->devType = aDeviceType;
|
---|
1214 |
|
---|
1215 | LogFlowThisFunc(("m->strLocationFull='%s', m->strFormat=%s, m->id={%RTuuid}\n",
|
---|
1216 | m->strLocationFull.c_str(), m->strFormat.c_str(), m->id.raw()));
|
---|
1217 |
|
---|
1218 | /* Don't call queryInfo() for registered media to prevent the calling
|
---|
1219 | * thread (i.e. the VirtualBox server startup thread) from an unexpected
|
---|
1220 | * freeze but mark it as initially inaccessible instead. The vital UUID,
|
---|
1221 | * location and format properties are read from the registry file above; to
|
---|
1222 | * get the actual state and the rest of the data, the user will have to call
|
---|
1223 | * COMGETTER(State). */
|
---|
1224 |
|
---|
1225 | AutoWriteLock treeLock(aVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1226 |
|
---|
1227 | /* load all children */
|
---|
1228 | for (settings::MediaList::const_iterator it = data.llChildren.begin();
|
---|
1229 | it != data.llChildren.end();
|
---|
1230 | ++it)
|
---|
1231 | {
|
---|
1232 | const settings::Medium &med = *it;
|
---|
1233 |
|
---|
1234 | ComObjPtr<Medium> pHD;
|
---|
1235 | pHD.createObject();
|
---|
1236 | rc = pHD->init(aVirtualBox,
|
---|
1237 | this, // parent
|
---|
1238 | aDeviceType,
|
---|
1239 | uuidMachineRegistry,
|
---|
1240 | med, // child data
|
---|
1241 | strMachineFolder);
|
---|
1242 | if (FAILED(rc)) break;
|
---|
1243 |
|
---|
1244 | rc = m->pVirtualBox->registerHardDisk(pHD, NULL /* pllRegistriesThatNeedSaving */ );
|
---|
1245 | if (FAILED(rc)) break;
|
---|
1246 | }
|
---|
1247 |
|
---|
1248 | /* Confirm a successful initialization when it's the case */
|
---|
1249 | if (SUCCEEDED(rc))
|
---|
1250 | autoInitSpan.setSucceeded();
|
---|
1251 |
|
---|
1252 | return rc;
|
---|
1253 | }
|
---|
1254 |
|
---|
1255 | /**
|
---|
1256 | * Initializes the medium object by providing the host drive information.
|
---|
1257 | * Not used for anything but the host floppy/host DVD case.
|
---|
1258 | *
|
---|
1259 | * There is no registry for this case.
|
---|
1260 | *
|
---|
1261 | * @param aVirtualBox VirtualBox object.
|
---|
1262 | * @param aDeviceType Device type of the medium.
|
---|
1263 | * @param aLocation Location of the host drive.
|
---|
1264 | * @param aDescription Comment for this host drive.
|
---|
1265 | *
|
---|
1266 | * @note Locks VirtualBox lock for writing.
|
---|
1267 | */
|
---|
1268 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1269 | DeviceType_T aDeviceType,
|
---|
1270 | const Utf8Str &aLocation,
|
---|
1271 | const Utf8Str &aDescription /* = Utf8Str::Empty */)
|
---|
1272 | {
|
---|
1273 | ComAssertRet(aDeviceType == DeviceType_DVD || aDeviceType == DeviceType_Floppy, E_INVALIDARG);
|
---|
1274 | ComAssertRet(!aLocation.isEmpty(), E_INVALIDARG);
|
---|
1275 |
|
---|
1276 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1277 | AutoInitSpan autoInitSpan(this);
|
---|
1278 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1279 |
|
---|
1280 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1281 |
|
---|
1282 | /* fake up a UUID which is unique, but also reproducible */
|
---|
1283 | RTUUID uuid;
|
---|
1284 | RTUuidClear(&uuid);
|
---|
1285 | if (aDeviceType == DeviceType_DVD)
|
---|
1286 | memcpy(&uuid.au8[0], "DVD", 3);
|
---|
1287 | else
|
---|
1288 | memcpy(&uuid.au8[0], "FD", 2);
|
---|
1289 | /* use device name, adjusted to the end of uuid, shortened if necessary */
|
---|
1290 | size_t lenLocation = aLocation.length();
|
---|
1291 | if (lenLocation > 12)
|
---|
1292 | memcpy(&uuid.au8[4], aLocation.c_str() + (lenLocation - 12), 12);
|
---|
1293 | else
|
---|
1294 | memcpy(&uuid.au8[4 + 12 - lenLocation], aLocation.c_str(), lenLocation);
|
---|
1295 | unconst(m->id) = uuid;
|
---|
1296 |
|
---|
1297 | if (aDeviceType == DeviceType_DVD)
|
---|
1298 | m->type = MediumType_Readonly;
|
---|
1299 | else
|
---|
1300 | m->type = MediumType_Writethrough;
|
---|
1301 | m->devType = aDeviceType;
|
---|
1302 | m->state = MediumState_Created;
|
---|
1303 | m->hostDrive = true;
|
---|
1304 | HRESULT rc = setFormat("RAW");
|
---|
1305 | if (FAILED(rc)) return rc;
|
---|
1306 | rc = setLocation(aLocation);
|
---|
1307 | if (FAILED(rc)) return rc;
|
---|
1308 | m->strDescription = aDescription;
|
---|
1309 |
|
---|
1310 | /// @todo generate uuid (similarly to host network interface uuid) from location and device type
|
---|
1311 |
|
---|
1312 | autoInitSpan.setSucceeded();
|
---|
1313 | return S_OK;
|
---|
1314 | }
|
---|
1315 |
|
---|
1316 | /**
|
---|
1317 | * Uninitializes the instance.
|
---|
1318 | *
|
---|
1319 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
1320 | *
|
---|
1321 | * @note All children of this medium get uninitialized by calling their
|
---|
1322 | * uninit() methods.
|
---|
1323 | *
|
---|
1324 | * @note Caller must hold the tree lock of the medium tree this medium is on.
|
---|
1325 | */
|
---|
1326 | void Medium::uninit()
|
---|
1327 | {
|
---|
1328 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
1329 | AutoUninitSpan autoUninitSpan(this);
|
---|
1330 | if (autoUninitSpan.uninitDone())
|
---|
1331 | return;
|
---|
1332 |
|
---|
1333 | if (!m->formatObj.isNull())
|
---|
1334 | {
|
---|
1335 | /* remove the caller reference we added in setFormat() */
|
---|
1336 | m->formatObj->releaseCaller();
|
---|
1337 | m->formatObj.setNull();
|
---|
1338 | }
|
---|
1339 |
|
---|
1340 | if (m->state == MediumState_Deleting)
|
---|
1341 | {
|
---|
1342 | /* This medium has been already deleted (directly or as part of a
|
---|
1343 | * merge). Reparenting has already been done. */
|
---|
1344 | Assert(m->pParent.isNull());
|
---|
1345 | }
|
---|
1346 | else
|
---|
1347 | {
|
---|
1348 | MediaList::iterator it;
|
---|
1349 | for (it = m->llChildren.begin();
|
---|
1350 | it != m->llChildren.end();
|
---|
1351 | ++it)
|
---|
1352 | {
|
---|
1353 | Medium *pChild = *it;
|
---|
1354 | pChild->m->pParent.setNull();
|
---|
1355 | pChild->uninit();
|
---|
1356 | }
|
---|
1357 | m->llChildren.clear(); // this unsets all the ComPtrs and probably calls delete
|
---|
1358 |
|
---|
1359 | if (m->pParent)
|
---|
1360 | {
|
---|
1361 | // this is a differencing disk: then remove it from the parent's children list
|
---|
1362 | deparent();
|
---|
1363 | }
|
---|
1364 | }
|
---|
1365 |
|
---|
1366 | RTSemEventMultiSignal(m->queryInfoSem);
|
---|
1367 | RTSemEventMultiDestroy(m->queryInfoSem);
|
---|
1368 | m->queryInfoSem = NIL_RTSEMEVENTMULTI;
|
---|
1369 |
|
---|
1370 | unconst(m->pVirtualBox) = NULL;
|
---|
1371 | }
|
---|
1372 |
|
---|
1373 | /**
|
---|
1374 | * Internal helper that removes "this" from the list of children of its
|
---|
1375 | * parent. Used in uninit() and other places when reparenting is necessary.
|
---|
1376 | *
|
---|
1377 | * The caller must hold the medium tree lock!
|
---|
1378 | */
|
---|
1379 | void Medium::deparent()
|
---|
1380 | {
|
---|
1381 | MediaList &llParent = m->pParent->m->llChildren;
|
---|
1382 | for (MediaList::iterator it = llParent.begin();
|
---|
1383 | it != llParent.end();
|
---|
1384 | ++it)
|
---|
1385 | {
|
---|
1386 | Medium *pParentsChild = *it;
|
---|
1387 | if (this == pParentsChild)
|
---|
1388 | {
|
---|
1389 | llParent.erase(it);
|
---|
1390 | break;
|
---|
1391 | }
|
---|
1392 | }
|
---|
1393 | m->pParent.setNull();
|
---|
1394 | }
|
---|
1395 |
|
---|
1396 | /**
|
---|
1397 | * Internal helper that removes "this" from the list of children of its
|
---|
1398 | * parent. Used in uninit() and other places when reparenting is necessary.
|
---|
1399 | *
|
---|
1400 | * The caller must hold the medium tree lock!
|
---|
1401 | */
|
---|
1402 | void Medium::setParent(const ComObjPtr<Medium> &pParent)
|
---|
1403 | {
|
---|
1404 | m->pParent = pParent;
|
---|
1405 | if (pParent)
|
---|
1406 | pParent->m->llChildren.push_back(this);
|
---|
1407 | }
|
---|
1408 |
|
---|
1409 |
|
---|
1410 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1411 | //
|
---|
1412 | // IMedium public methods
|
---|
1413 | //
|
---|
1414 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1415 |
|
---|
1416 | STDMETHODIMP Medium::COMGETTER(Id)(BSTR *aId)
|
---|
1417 | {
|
---|
1418 | CheckComArgOutPointerValid(aId);
|
---|
1419 |
|
---|
1420 | AutoCaller autoCaller(this);
|
---|
1421 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1422 |
|
---|
1423 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1424 |
|
---|
1425 | m->id.toUtf16().cloneTo(aId);
|
---|
1426 |
|
---|
1427 | return S_OK;
|
---|
1428 | }
|
---|
1429 |
|
---|
1430 | STDMETHODIMP Medium::COMGETTER(Description)(BSTR *aDescription)
|
---|
1431 | {
|
---|
1432 | CheckComArgOutPointerValid(aDescription);
|
---|
1433 |
|
---|
1434 | AutoCaller autoCaller(this);
|
---|
1435 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1436 |
|
---|
1437 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1438 |
|
---|
1439 | m->strDescription.cloneTo(aDescription);
|
---|
1440 |
|
---|
1441 | return S_OK;
|
---|
1442 | }
|
---|
1443 |
|
---|
1444 | STDMETHODIMP Medium::COMSETTER(Description)(IN_BSTR aDescription)
|
---|
1445 | {
|
---|
1446 | AutoCaller autoCaller(this);
|
---|
1447 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1448 |
|
---|
1449 | // AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1450 |
|
---|
1451 | /// @todo update m->description and save the global registry (and local
|
---|
1452 | /// registries of portable VMs referring to this medium), this will also
|
---|
1453 | /// require to add the mRegistered flag to data
|
---|
1454 |
|
---|
1455 | NOREF(aDescription);
|
---|
1456 |
|
---|
1457 | ReturnComNotImplemented();
|
---|
1458 | }
|
---|
1459 |
|
---|
1460 | STDMETHODIMP Medium::COMGETTER(State)(MediumState_T *aState)
|
---|
1461 | {
|
---|
1462 | CheckComArgOutPointerValid(aState);
|
---|
1463 |
|
---|
1464 | AutoCaller autoCaller(this);
|
---|
1465 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1466 |
|
---|
1467 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1468 | *aState = m->state;
|
---|
1469 |
|
---|
1470 | return S_OK;
|
---|
1471 | }
|
---|
1472 |
|
---|
1473 | STDMETHODIMP Medium::COMGETTER(Variant)(MediumVariant_T *aVariant)
|
---|
1474 | {
|
---|
1475 | CheckComArgOutPointerValid(aVariant);
|
---|
1476 |
|
---|
1477 | AutoCaller autoCaller(this);
|
---|
1478 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1479 |
|
---|
1480 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1481 | *aVariant = m->variant;
|
---|
1482 |
|
---|
1483 | return S_OK;
|
---|
1484 | }
|
---|
1485 |
|
---|
1486 |
|
---|
1487 | STDMETHODIMP Medium::COMGETTER(Location)(BSTR *aLocation)
|
---|
1488 | {
|
---|
1489 | CheckComArgOutPointerValid(aLocation);
|
---|
1490 |
|
---|
1491 | AutoCaller autoCaller(this);
|
---|
1492 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1493 |
|
---|
1494 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1495 |
|
---|
1496 | m->strLocationFull.cloneTo(aLocation);
|
---|
1497 |
|
---|
1498 | return S_OK;
|
---|
1499 | }
|
---|
1500 |
|
---|
1501 | STDMETHODIMP Medium::COMSETTER(Location)(IN_BSTR aLocation)
|
---|
1502 | {
|
---|
1503 | CheckComArgStrNotEmptyOrNull(aLocation);
|
---|
1504 |
|
---|
1505 | AutoCaller autoCaller(this);
|
---|
1506 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1507 |
|
---|
1508 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1509 |
|
---|
1510 | /// @todo NEWMEDIA for file names, add the default extension if no extension
|
---|
1511 | /// is present (using the information from the VD backend which also implies
|
---|
1512 | /// that one more parameter should be passed to setLocation() requesting
|
---|
1513 | /// that functionality since it is only allowed when called from this method
|
---|
1514 |
|
---|
1515 | /// @todo NEWMEDIA rename the file and set m->location on success, then save
|
---|
1516 | /// the global registry (and local registries of portable VMs referring to
|
---|
1517 | /// this medium), this will also require to add the mRegistered flag to data
|
---|
1518 |
|
---|
1519 | ReturnComNotImplemented();
|
---|
1520 | }
|
---|
1521 |
|
---|
1522 | STDMETHODIMP Medium::COMGETTER(Name)(BSTR *aName)
|
---|
1523 | {
|
---|
1524 | CheckComArgOutPointerValid(aName);
|
---|
1525 |
|
---|
1526 | AutoCaller autoCaller(this);
|
---|
1527 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1528 |
|
---|
1529 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1530 |
|
---|
1531 | getName().cloneTo(aName);
|
---|
1532 |
|
---|
1533 | return S_OK;
|
---|
1534 | }
|
---|
1535 |
|
---|
1536 | STDMETHODIMP Medium::COMGETTER(DeviceType)(DeviceType_T *aDeviceType)
|
---|
1537 | {
|
---|
1538 | CheckComArgOutPointerValid(aDeviceType);
|
---|
1539 |
|
---|
1540 | AutoCaller autoCaller(this);
|
---|
1541 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1542 |
|
---|
1543 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1544 |
|
---|
1545 | *aDeviceType = m->devType;
|
---|
1546 |
|
---|
1547 | return S_OK;
|
---|
1548 | }
|
---|
1549 |
|
---|
1550 | STDMETHODIMP Medium::COMGETTER(HostDrive)(BOOL *aHostDrive)
|
---|
1551 | {
|
---|
1552 | CheckComArgOutPointerValid(aHostDrive);
|
---|
1553 |
|
---|
1554 | AutoCaller autoCaller(this);
|
---|
1555 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1556 |
|
---|
1557 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1558 |
|
---|
1559 | *aHostDrive = m->hostDrive;
|
---|
1560 |
|
---|
1561 | return S_OK;
|
---|
1562 | }
|
---|
1563 |
|
---|
1564 | STDMETHODIMP Medium::COMGETTER(Size)(LONG64 *aSize)
|
---|
1565 | {
|
---|
1566 | CheckComArgOutPointerValid(aSize);
|
---|
1567 |
|
---|
1568 | AutoCaller autoCaller(this);
|
---|
1569 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1570 |
|
---|
1571 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1572 |
|
---|
1573 | *aSize = m->size;
|
---|
1574 |
|
---|
1575 | return S_OK;
|
---|
1576 | }
|
---|
1577 |
|
---|
1578 | STDMETHODIMP Medium::COMGETTER(Format)(BSTR *aFormat)
|
---|
1579 | {
|
---|
1580 | CheckComArgOutPointerValid(aFormat);
|
---|
1581 |
|
---|
1582 | AutoCaller autoCaller(this);
|
---|
1583 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1584 |
|
---|
1585 | /* no need to lock, m->strFormat is const */
|
---|
1586 | m->strFormat.cloneTo(aFormat);
|
---|
1587 |
|
---|
1588 | return S_OK;
|
---|
1589 | }
|
---|
1590 |
|
---|
1591 | STDMETHODIMP Medium::COMGETTER(MediumFormat)(IMediumFormat **aMediumFormat)
|
---|
1592 | {
|
---|
1593 | CheckComArgOutPointerValid(aMediumFormat);
|
---|
1594 |
|
---|
1595 | AutoCaller autoCaller(this);
|
---|
1596 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1597 |
|
---|
1598 | /* no need to lock, m->formatObj is const */
|
---|
1599 | m->formatObj.queryInterfaceTo(aMediumFormat);
|
---|
1600 |
|
---|
1601 | return S_OK;
|
---|
1602 | }
|
---|
1603 |
|
---|
1604 | STDMETHODIMP Medium::COMGETTER(Type)(MediumType_T *aType)
|
---|
1605 | {
|
---|
1606 | CheckComArgOutPointerValid(aType);
|
---|
1607 |
|
---|
1608 | AutoCaller autoCaller(this);
|
---|
1609 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1610 |
|
---|
1611 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1612 |
|
---|
1613 | *aType = m->type;
|
---|
1614 |
|
---|
1615 | return S_OK;
|
---|
1616 | }
|
---|
1617 |
|
---|
1618 | STDMETHODIMP Medium::COMSETTER(Type)(MediumType_T aType)
|
---|
1619 | {
|
---|
1620 | AutoCaller autoCaller(this);
|
---|
1621 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1622 |
|
---|
1623 | // we access mParent and members
|
---|
1624 | AutoMultiWriteLock2 mlock(&m->pVirtualBox->getMediaTreeLockHandle(), this->lockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1625 |
|
---|
1626 | switch (m->state)
|
---|
1627 | {
|
---|
1628 | case MediumState_Created:
|
---|
1629 | case MediumState_Inaccessible:
|
---|
1630 | break;
|
---|
1631 | default:
|
---|
1632 | return setStateError();
|
---|
1633 | }
|
---|
1634 |
|
---|
1635 | if (m->type == aType)
|
---|
1636 | {
|
---|
1637 | /* Nothing to do */
|
---|
1638 | return S_OK;
|
---|
1639 | }
|
---|
1640 |
|
---|
1641 | DeviceType_T devType = getDeviceType();
|
---|
1642 | // DVD media can only be readonly.
|
---|
1643 | if (devType == DeviceType_DVD && aType != MediumType_Readonly)
|
---|
1644 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1645 | tr("Cannot change the type of DVD medium '%s'"),
|
---|
1646 | m->strLocationFull.c_str());
|
---|
1647 | // Floppy media can only be writethrough or readonly.
|
---|
1648 | if ( devType == DeviceType_Floppy
|
---|
1649 | && aType != MediumType_Writethrough
|
---|
1650 | && aType != MediumType_Readonly)
|
---|
1651 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1652 | tr("Cannot change the type of floppy medium '%s'"),
|
---|
1653 | m->strLocationFull.c_str());
|
---|
1654 |
|
---|
1655 | /* cannot change the type of a differencing medium */
|
---|
1656 | if (m->pParent)
|
---|
1657 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1658 | tr("Cannot change the type of medium '%s' because it is a differencing medium"),
|
---|
1659 | m->strLocationFull.c_str());
|
---|
1660 |
|
---|
1661 | /* Cannot change the type of a medium being in use by more than one VM.
|
---|
1662 | * If the change is to Immutable or MultiAttach then it must not be
|
---|
1663 | * directly attached to any VM, otherwise the assumptions about indirect
|
---|
1664 | * attachment elsewhere are violated and the VM becomes inaccessible.
|
---|
1665 | * Attaching an immutable medium triggers the diff creation, and this is
|
---|
1666 | * vital for the correct operation. */
|
---|
1667 | if ( m->backRefs.size() > 1
|
---|
1668 | || ( ( aType == MediumType_Immutable
|
---|
1669 | || aType == MediumType_MultiAttach)
|
---|
1670 | && m->backRefs.size() > 0))
|
---|
1671 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1672 | tr("Cannot change the type of medium '%s' because it is attached to %d virtual machines"),
|
---|
1673 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
1674 |
|
---|
1675 | switch (aType)
|
---|
1676 | {
|
---|
1677 | case MediumType_Normal:
|
---|
1678 | case MediumType_Immutable:
|
---|
1679 | case MediumType_MultiAttach:
|
---|
1680 | {
|
---|
1681 | /* normal can be easily converted to immutable and vice versa even
|
---|
1682 | * if they have children as long as they are not attached to any
|
---|
1683 | * machine themselves */
|
---|
1684 | break;
|
---|
1685 | }
|
---|
1686 | case MediumType_Writethrough:
|
---|
1687 | case MediumType_Shareable:
|
---|
1688 | case MediumType_Readonly:
|
---|
1689 | {
|
---|
1690 | /* cannot change to writethrough, shareable or readonly
|
---|
1691 | * if there are children */
|
---|
1692 | if (getChildren().size() != 0)
|
---|
1693 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
1694 | tr("Cannot change type for medium '%s' since it has %d child media"),
|
---|
1695 | m->strLocationFull.c_str(), getChildren().size());
|
---|
1696 | if (aType == MediumType_Shareable)
|
---|
1697 | {
|
---|
1698 | if (m->state == MediumState_Inaccessible)
|
---|
1699 | {
|
---|
1700 | HRESULT rc = queryInfo(false /* fSetImageId */, false /* fSetParentId */);
|
---|
1701 | if (FAILED(rc))
|
---|
1702 | return setError(rc,
|
---|
1703 | tr("Cannot change type for medium '%s' to 'Shareable' because the medium is inaccessible"),
|
---|
1704 | m->strLocationFull.c_str());
|
---|
1705 | }
|
---|
1706 |
|
---|
1707 | MediumVariant_T variant = getVariant();
|
---|
1708 | if (!(variant & MediumVariant_Fixed))
|
---|
1709 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1710 | tr("Cannot change type for medium '%s' to 'Shareable' since it is a dynamic medium storage unit"),
|
---|
1711 | m->strLocationFull.c_str());
|
---|
1712 | }
|
---|
1713 | else if (aType == MediumType_Readonly && devType == DeviceType_HardDisk)
|
---|
1714 | {
|
---|
1715 | // Readonly hard disks are not allowed, this medium type is reserved for
|
---|
1716 | // DVDs and floppy images at the moment. Later we might allow readonly hard
|
---|
1717 | // disks, but that's extremely unusual and many guest OSes will have trouble.
|
---|
1718 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1719 | tr("Cannot change type for medium '%s' to 'Readonly' since it is a hard disk"),
|
---|
1720 | m->strLocationFull.c_str());
|
---|
1721 | }
|
---|
1722 | break;
|
---|
1723 | }
|
---|
1724 | default:
|
---|
1725 | AssertFailedReturn(E_FAIL);
|
---|
1726 | }
|
---|
1727 |
|
---|
1728 | if (aType == MediumType_MultiAttach)
|
---|
1729 | {
|
---|
1730 | // This type is new with VirtualBox 4.0 and therefore requires settings
|
---|
1731 | // version 1.11 in the settings backend. Unfortunately it is not enough to do
|
---|
1732 | // the usual routine in MachineConfigFile::bumpSettingsVersionIfNeeded() for
|
---|
1733 | // two reasons: The medium type is a property of the media registry tree, which
|
---|
1734 | // can reside in the global config file (for pre-4.0 media); we would therefore
|
---|
1735 | // possibly need to bump the global config version. We don't want to do that though
|
---|
1736 | // because that might make downgrading to pre-4.0 impossible.
|
---|
1737 | // As a result, we can only use these two new types if the medium is NOT in the
|
---|
1738 | // global registry:
|
---|
1739 | const Guid &uuidGlobalRegistry = m->pVirtualBox->getGlobalRegistryId();
|
---|
1740 | if (isInRegistry(uuidGlobalRegistry))
|
---|
1741 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1742 | tr("Cannot change type for medium '%s': the media type 'MultiAttach' can only be used "
|
---|
1743 | "on media registered with a machine that was created with VirtualBox 4.0 or later"),
|
---|
1744 | m->strLocationFull.c_str());
|
---|
1745 | }
|
---|
1746 |
|
---|
1747 | m->type = aType;
|
---|
1748 |
|
---|
1749 | // save the global settings; for that we should hold only the VirtualBox lock
|
---|
1750 | mlock.release();
|
---|
1751 | AutoWriteLock alock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
1752 | HRESULT rc = m->pVirtualBox->saveSettings();
|
---|
1753 |
|
---|
1754 | return rc;
|
---|
1755 | }
|
---|
1756 |
|
---|
1757 | STDMETHODIMP Medium::COMGETTER(Parent)(IMedium **aParent)
|
---|
1758 | {
|
---|
1759 | CheckComArgOutPointerValid(aParent);
|
---|
1760 |
|
---|
1761 | AutoCaller autoCaller(this);
|
---|
1762 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1763 |
|
---|
1764 | /* we access mParent */
|
---|
1765 | AutoReadLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1766 |
|
---|
1767 | m->pParent.queryInterfaceTo(aParent);
|
---|
1768 |
|
---|
1769 | return S_OK;
|
---|
1770 | }
|
---|
1771 |
|
---|
1772 | STDMETHODIMP Medium::COMGETTER(Children)(ComSafeArrayOut(IMedium *, aChildren))
|
---|
1773 | {
|
---|
1774 | CheckComArgOutSafeArrayPointerValid(aChildren);
|
---|
1775 |
|
---|
1776 | AutoCaller autoCaller(this);
|
---|
1777 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1778 |
|
---|
1779 | /* we access children */
|
---|
1780 | AutoReadLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1781 |
|
---|
1782 | SafeIfaceArray<IMedium> children(this->getChildren());
|
---|
1783 | children.detachTo(ComSafeArrayOutArg(aChildren));
|
---|
1784 |
|
---|
1785 | return S_OK;
|
---|
1786 | }
|
---|
1787 |
|
---|
1788 | STDMETHODIMP Medium::COMGETTER(Base)(IMedium **aBase)
|
---|
1789 | {
|
---|
1790 | CheckComArgOutPointerValid(aBase);
|
---|
1791 |
|
---|
1792 | /* base() will do callers/locking */
|
---|
1793 |
|
---|
1794 | getBase().queryInterfaceTo(aBase);
|
---|
1795 |
|
---|
1796 | return S_OK;
|
---|
1797 | }
|
---|
1798 |
|
---|
1799 | STDMETHODIMP Medium::COMGETTER(ReadOnly)(BOOL *aReadOnly)
|
---|
1800 | {
|
---|
1801 | CheckComArgOutPointerValid(aReadOnly);
|
---|
1802 |
|
---|
1803 | AutoCaller autoCaller(this);
|
---|
1804 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1805 |
|
---|
1806 | /* isRadOnly() will do locking */
|
---|
1807 |
|
---|
1808 | *aReadOnly = isReadOnly();
|
---|
1809 |
|
---|
1810 | return S_OK;
|
---|
1811 | }
|
---|
1812 |
|
---|
1813 | STDMETHODIMP Medium::COMGETTER(LogicalSize)(LONG64 *aLogicalSize)
|
---|
1814 | {
|
---|
1815 | CheckComArgOutPointerValid(aLogicalSize);
|
---|
1816 |
|
---|
1817 | {
|
---|
1818 | AutoCaller autoCaller(this);
|
---|
1819 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1820 |
|
---|
1821 | /* we access mParent */
|
---|
1822 | AutoReadLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1823 |
|
---|
1824 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1825 |
|
---|
1826 | if (m->pParent.isNull())
|
---|
1827 | {
|
---|
1828 | *aLogicalSize = m->logicalSize;
|
---|
1829 |
|
---|
1830 | return S_OK;
|
---|
1831 | }
|
---|
1832 | }
|
---|
1833 |
|
---|
1834 | /* We assume that some backend may decide to return a meaningless value in
|
---|
1835 | * response to VDGetSize() for differencing media and therefore always
|
---|
1836 | * ask the base medium ourselves. */
|
---|
1837 |
|
---|
1838 | /* base() will do callers/locking */
|
---|
1839 |
|
---|
1840 | return getBase()->COMGETTER(LogicalSize)(aLogicalSize);
|
---|
1841 | }
|
---|
1842 |
|
---|
1843 | STDMETHODIMP Medium::COMGETTER(AutoReset)(BOOL *aAutoReset)
|
---|
1844 | {
|
---|
1845 | CheckComArgOutPointerValid(aAutoReset);
|
---|
1846 |
|
---|
1847 | AutoCaller autoCaller(this);
|
---|
1848 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1849 |
|
---|
1850 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1851 |
|
---|
1852 | if (m->pParent.isNull())
|
---|
1853 | *aAutoReset = FALSE;
|
---|
1854 | else
|
---|
1855 | *aAutoReset = m->autoReset;
|
---|
1856 |
|
---|
1857 | return S_OK;
|
---|
1858 | }
|
---|
1859 |
|
---|
1860 | STDMETHODIMP Medium::COMSETTER(AutoReset)(BOOL aAutoReset)
|
---|
1861 | {
|
---|
1862 | AutoCaller autoCaller(this);
|
---|
1863 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1864 |
|
---|
1865 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1866 |
|
---|
1867 | if (m->pParent.isNull())
|
---|
1868 | return setError(VBOX_E_NOT_SUPPORTED,
|
---|
1869 | tr("Medium '%s' is not differencing"),
|
---|
1870 | m->strLocationFull.c_str());
|
---|
1871 |
|
---|
1872 | if (m->autoReset != !!aAutoReset)
|
---|
1873 | {
|
---|
1874 | m->autoReset = !!aAutoReset;
|
---|
1875 |
|
---|
1876 | // save the global settings; for that we should hold only the VirtualBox lock
|
---|
1877 | mlock.release();
|
---|
1878 | AutoWriteLock alock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
1879 | return m->pVirtualBox->saveSettings();
|
---|
1880 | }
|
---|
1881 |
|
---|
1882 | return S_OK;
|
---|
1883 | }
|
---|
1884 | STDMETHODIMP Medium::COMGETTER(LastAccessError)(BSTR *aLastAccessError)
|
---|
1885 | {
|
---|
1886 | CheckComArgOutPointerValid(aLastAccessError);
|
---|
1887 |
|
---|
1888 | AutoCaller autoCaller(this);
|
---|
1889 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1890 |
|
---|
1891 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1892 |
|
---|
1893 | m->strLastAccessError.cloneTo(aLastAccessError);
|
---|
1894 |
|
---|
1895 | return S_OK;
|
---|
1896 | }
|
---|
1897 |
|
---|
1898 | STDMETHODIMP Medium::COMGETTER(MachineIds)(ComSafeArrayOut(BSTR,aMachineIds))
|
---|
1899 | {
|
---|
1900 | CheckComArgOutSafeArrayPointerValid(aMachineIds);
|
---|
1901 |
|
---|
1902 | AutoCaller autoCaller(this);
|
---|
1903 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1904 |
|
---|
1905 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1906 |
|
---|
1907 | com::SafeArray<BSTR> machineIds;
|
---|
1908 |
|
---|
1909 | if (m->backRefs.size() != 0)
|
---|
1910 | {
|
---|
1911 | machineIds.reset(m->backRefs.size());
|
---|
1912 |
|
---|
1913 | size_t i = 0;
|
---|
1914 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
1915 | it != m->backRefs.end(); ++it, ++i)
|
---|
1916 | {
|
---|
1917 | it->machineId.toUtf16().detachTo(&machineIds[i]);
|
---|
1918 | }
|
---|
1919 | }
|
---|
1920 |
|
---|
1921 | machineIds.detachTo(ComSafeArrayOutArg(aMachineIds));
|
---|
1922 |
|
---|
1923 | return S_OK;
|
---|
1924 | }
|
---|
1925 |
|
---|
1926 | STDMETHODIMP Medium::SetIDs(BOOL aSetImageId,
|
---|
1927 | IN_BSTR aImageId,
|
---|
1928 | BOOL aSetParentId,
|
---|
1929 | IN_BSTR aParentId)
|
---|
1930 | {
|
---|
1931 | AutoCaller autoCaller(this);
|
---|
1932 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1933 |
|
---|
1934 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1935 |
|
---|
1936 | switch (m->state)
|
---|
1937 | {
|
---|
1938 | case MediumState_Created:
|
---|
1939 | break;
|
---|
1940 | default:
|
---|
1941 | return setStateError();
|
---|
1942 | }
|
---|
1943 |
|
---|
1944 | Guid imageId, parentId;
|
---|
1945 | if (aSetImageId)
|
---|
1946 | {
|
---|
1947 | imageId = Guid(aImageId);
|
---|
1948 | if (imageId.isEmpty())
|
---|
1949 | return setError(E_INVALIDARG, tr("Argument %s is empty"), "aImageId");
|
---|
1950 | }
|
---|
1951 | if (aSetParentId)
|
---|
1952 | parentId = Guid(aParentId);
|
---|
1953 |
|
---|
1954 | unconst(m->uuidImage) = imageId;
|
---|
1955 | unconst(m->uuidParentImage) = parentId;
|
---|
1956 |
|
---|
1957 | HRESULT rc = queryInfo(!!aSetImageId /* fSetImageId */,
|
---|
1958 | !!aSetParentId /* fSetParentId */);
|
---|
1959 |
|
---|
1960 | return rc;
|
---|
1961 | }
|
---|
1962 |
|
---|
1963 | STDMETHODIMP Medium::RefreshState(MediumState_T *aState)
|
---|
1964 | {
|
---|
1965 | CheckComArgOutPointerValid(aState);
|
---|
1966 |
|
---|
1967 | AutoCaller autoCaller(this);
|
---|
1968 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1969 |
|
---|
1970 | /* queryInfo() locks this for writing. */
|
---|
1971 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1972 |
|
---|
1973 | HRESULT rc = S_OK;
|
---|
1974 |
|
---|
1975 | switch (m->state)
|
---|
1976 | {
|
---|
1977 | case MediumState_Created:
|
---|
1978 | case MediumState_Inaccessible:
|
---|
1979 | case MediumState_LockedRead:
|
---|
1980 | {
|
---|
1981 | rc = queryInfo(false /* fSetImageId */, false /* fSetParentId */);
|
---|
1982 | break;
|
---|
1983 | }
|
---|
1984 | default:
|
---|
1985 | break;
|
---|
1986 | }
|
---|
1987 |
|
---|
1988 | *aState = m->state;
|
---|
1989 |
|
---|
1990 | return rc;
|
---|
1991 | }
|
---|
1992 |
|
---|
1993 | STDMETHODIMP Medium::GetSnapshotIds(IN_BSTR aMachineId,
|
---|
1994 | ComSafeArrayOut(BSTR, aSnapshotIds))
|
---|
1995 | {
|
---|
1996 | CheckComArgExpr(aMachineId, Guid(aMachineId).isEmpty() == false);
|
---|
1997 | CheckComArgOutSafeArrayPointerValid(aSnapshotIds);
|
---|
1998 |
|
---|
1999 | AutoCaller autoCaller(this);
|
---|
2000 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2001 |
|
---|
2002 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2003 |
|
---|
2004 | com::SafeArray<BSTR> snapshotIds;
|
---|
2005 |
|
---|
2006 | Guid id(aMachineId);
|
---|
2007 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
2008 | it != m->backRefs.end(); ++it)
|
---|
2009 | {
|
---|
2010 | if (it->machineId == id)
|
---|
2011 | {
|
---|
2012 | size_t size = it->llSnapshotIds.size();
|
---|
2013 |
|
---|
2014 | /* if the medium is attached to the machine in the current state, we
|
---|
2015 | * return its ID as the first element of the array */
|
---|
2016 | if (it->fInCurState)
|
---|
2017 | ++size;
|
---|
2018 |
|
---|
2019 | if (size > 0)
|
---|
2020 | {
|
---|
2021 | snapshotIds.reset(size);
|
---|
2022 |
|
---|
2023 | size_t j = 0;
|
---|
2024 | if (it->fInCurState)
|
---|
2025 | it->machineId.toUtf16().detachTo(&snapshotIds[j++]);
|
---|
2026 |
|
---|
2027 | for (GuidList::const_iterator jt = it->llSnapshotIds.begin();
|
---|
2028 | jt != it->llSnapshotIds.end();
|
---|
2029 | ++jt, ++j)
|
---|
2030 | {
|
---|
2031 | (*jt).toUtf16().detachTo(&snapshotIds[j]);
|
---|
2032 | }
|
---|
2033 | }
|
---|
2034 |
|
---|
2035 | break;
|
---|
2036 | }
|
---|
2037 | }
|
---|
2038 |
|
---|
2039 | snapshotIds.detachTo(ComSafeArrayOutArg(aSnapshotIds));
|
---|
2040 |
|
---|
2041 | return S_OK;
|
---|
2042 | }
|
---|
2043 |
|
---|
2044 | /**
|
---|
2045 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
2046 | * in-process calls).
|
---|
2047 | */
|
---|
2048 | STDMETHODIMP Medium::LockRead(MediumState_T *aState)
|
---|
2049 | {
|
---|
2050 | AutoCaller autoCaller(this);
|
---|
2051 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2052 |
|
---|
2053 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2054 |
|
---|
2055 | /* Wait for a concurrently running queryInfo() to complete */
|
---|
2056 | while (m->queryInfoRunning)
|
---|
2057 | {
|
---|
2058 | alock.leave();
|
---|
2059 | RTSemEventMultiWait(m->queryInfoSem, RT_INDEFINITE_WAIT);
|
---|
2060 | alock.enter();
|
---|
2061 | }
|
---|
2062 |
|
---|
2063 | /* return the current state before */
|
---|
2064 | if (aState)
|
---|
2065 | *aState = m->state;
|
---|
2066 |
|
---|
2067 | HRESULT rc = S_OK;
|
---|
2068 |
|
---|
2069 | switch (m->state)
|
---|
2070 | {
|
---|
2071 | case MediumState_Created:
|
---|
2072 | case MediumState_Inaccessible:
|
---|
2073 | case MediumState_LockedRead:
|
---|
2074 | {
|
---|
2075 | ++m->readers;
|
---|
2076 |
|
---|
2077 | ComAssertMsgBreak(m->readers != 0, ("Counter overflow"), rc = E_FAIL);
|
---|
2078 |
|
---|
2079 | /* Remember pre-lock state */
|
---|
2080 | if (m->state != MediumState_LockedRead)
|
---|
2081 | m->preLockState = m->state;
|
---|
2082 |
|
---|
2083 | LogFlowThisFunc(("Okay - prev state=%d readers=%d\n", m->state, m->readers));
|
---|
2084 | m->state = MediumState_LockedRead;
|
---|
2085 |
|
---|
2086 | break;
|
---|
2087 | }
|
---|
2088 | default:
|
---|
2089 | {
|
---|
2090 | LogFlowThisFunc(("Failing - state=%d\n", m->state));
|
---|
2091 | rc = setStateError();
|
---|
2092 | break;
|
---|
2093 | }
|
---|
2094 | }
|
---|
2095 |
|
---|
2096 | return rc;
|
---|
2097 | }
|
---|
2098 |
|
---|
2099 | /**
|
---|
2100 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
2101 | * in-process calls).
|
---|
2102 | */
|
---|
2103 | STDMETHODIMP Medium::UnlockRead(MediumState_T *aState)
|
---|
2104 | {
|
---|
2105 | AutoCaller autoCaller(this);
|
---|
2106 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2107 |
|
---|
2108 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2109 |
|
---|
2110 | HRESULT rc = S_OK;
|
---|
2111 |
|
---|
2112 | switch (m->state)
|
---|
2113 | {
|
---|
2114 | case MediumState_LockedRead:
|
---|
2115 | {
|
---|
2116 | Assert(m->readers != 0);
|
---|
2117 | --m->readers;
|
---|
2118 |
|
---|
2119 | /* Reset the state after the last reader */
|
---|
2120 | if (m->readers == 0)
|
---|
2121 | {
|
---|
2122 | m->state = m->preLockState;
|
---|
2123 | /* There are cases where we inject the deleting state into
|
---|
2124 | * a medium locked for reading. Make sure #unmarkForDeletion()
|
---|
2125 | * gets the right state afterwards. */
|
---|
2126 | if (m->preLockState == MediumState_Deleting)
|
---|
2127 | m->preLockState = MediumState_Created;
|
---|
2128 | }
|
---|
2129 |
|
---|
2130 | LogFlowThisFunc(("new state=%d\n", m->state));
|
---|
2131 | break;
|
---|
2132 | }
|
---|
2133 | default:
|
---|
2134 | {
|
---|
2135 | LogFlowThisFunc(("Failing - state=%d\n", m->state));
|
---|
2136 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2137 | tr("Medium '%s' is not locked for reading"),
|
---|
2138 | m->strLocationFull.c_str());
|
---|
2139 | break;
|
---|
2140 | }
|
---|
2141 | }
|
---|
2142 |
|
---|
2143 | /* return the current state after */
|
---|
2144 | if (aState)
|
---|
2145 | *aState = m->state;
|
---|
2146 |
|
---|
2147 | return rc;
|
---|
2148 | }
|
---|
2149 |
|
---|
2150 | /**
|
---|
2151 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
2152 | * in-process calls).
|
---|
2153 | */
|
---|
2154 | STDMETHODIMP Medium::LockWrite(MediumState_T *aState)
|
---|
2155 | {
|
---|
2156 | AutoCaller autoCaller(this);
|
---|
2157 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2158 |
|
---|
2159 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2160 |
|
---|
2161 | /* Wait for a concurrently running queryInfo() to complete */
|
---|
2162 | while (m->queryInfoRunning)
|
---|
2163 | {
|
---|
2164 | alock.leave();
|
---|
2165 | RTSemEventMultiWait(m->queryInfoSem, RT_INDEFINITE_WAIT);
|
---|
2166 | alock.enter();
|
---|
2167 | }
|
---|
2168 |
|
---|
2169 | /* return the current state before */
|
---|
2170 | if (aState)
|
---|
2171 | *aState = m->state;
|
---|
2172 |
|
---|
2173 | HRESULT rc = S_OK;
|
---|
2174 |
|
---|
2175 | switch (m->state)
|
---|
2176 | {
|
---|
2177 | case MediumState_Created:
|
---|
2178 | case MediumState_Inaccessible:
|
---|
2179 | {
|
---|
2180 | m->preLockState = m->state;
|
---|
2181 |
|
---|
2182 | LogFlowThisFunc(("Okay - prev state=%d locationFull=%s\n", m->state, getLocationFull().c_str()));
|
---|
2183 | m->state = MediumState_LockedWrite;
|
---|
2184 | break;
|
---|
2185 | }
|
---|
2186 | default:
|
---|
2187 | {
|
---|
2188 | LogFlowThisFunc(("Failing - state=%d locationFull=%s\n", m->state, getLocationFull().c_str()));
|
---|
2189 | rc = setStateError();
|
---|
2190 | break;
|
---|
2191 | }
|
---|
2192 | }
|
---|
2193 |
|
---|
2194 | return rc;
|
---|
2195 | }
|
---|
2196 |
|
---|
2197 | /**
|
---|
2198 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
2199 | * in-process calls).
|
---|
2200 | */
|
---|
2201 | STDMETHODIMP Medium::UnlockWrite(MediumState_T *aState)
|
---|
2202 | {
|
---|
2203 | AutoCaller autoCaller(this);
|
---|
2204 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2205 |
|
---|
2206 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2207 |
|
---|
2208 | HRESULT rc = S_OK;
|
---|
2209 |
|
---|
2210 | switch (m->state)
|
---|
2211 | {
|
---|
2212 | case MediumState_LockedWrite:
|
---|
2213 | {
|
---|
2214 | m->state = m->preLockState;
|
---|
2215 | /* There are cases where we inject the deleting state into
|
---|
2216 | * a medium locked for writing. Make sure #unmarkForDeletion()
|
---|
2217 | * gets the right state afterwards. */
|
---|
2218 | if (m->preLockState == MediumState_Deleting)
|
---|
2219 | m->preLockState = MediumState_Created;
|
---|
2220 | LogFlowThisFunc(("new state=%d locationFull=%s\n", m->state, getLocationFull().c_str()));
|
---|
2221 | break;
|
---|
2222 | }
|
---|
2223 | default:
|
---|
2224 | {
|
---|
2225 | LogFlowThisFunc(("Failing - state=%d locationFull=%s\n", m->state, getLocationFull().c_str()));
|
---|
2226 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2227 | tr("Medium '%s' is not locked for writing"),
|
---|
2228 | m->strLocationFull.c_str());
|
---|
2229 | break;
|
---|
2230 | }
|
---|
2231 | }
|
---|
2232 |
|
---|
2233 | /* return the current state after */
|
---|
2234 | if (aState)
|
---|
2235 | *aState = m->state;
|
---|
2236 |
|
---|
2237 | return rc;
|
---|
2238 | }
|
---|
2239 |
|
---|
2240 | STDMETHODIMP Medium::Close()
|
---|
2241 | {
|
---|
2242 | AutoCaller autoCaller(this);
|
---|
2243 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2244 |
|
---|
2245 | // make a copy of VirtualBox pointer which gets nulled by uninit()
|
---|
2246 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
2247 |
|
---|
2248 | GuidList llRegistriesThatNeedSaving;
|
---|
2249 | HRESULT rc = close(&llRegistriesThatNeedSaving, autoCaller);
|
---|
2250 |
|
---|
2251 | pVirtualBox->saveRegistries(llRegistriesThatNeedSaving);
|
---|
2252 |
|
---|
2253 | return rc;
|
---|
2254 | }
|
---|
2255 |
|
---|
2256 | STDMETHODIMP Medium::GetProperty(IN_BSTR aName, BSTR *aValue)
|
---|
2257 | {
|
---|
2258 | CheckComArgStrNotEmptyOrNull(aName);
|
---|
2259 | CheckComArgOutPointerValid(aValue);
|
---|
2260 |
|
---|
2261 | AutoCaller autoCaller(this);
|
---|
2262 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2263 |
|
---|
2264 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2265 |
|
---|
2266 | settings::StringsMap::const_iterator it = m->mapProperties.find(Utf8Str(aName));
|
---|
2267 | if (it == m->mapProperties.end())
|
---|
2268 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2269 | tr("Property '%ls' does not exist"), aName);
|
---|
2270 |
|
---|
2271 | it->second.cloneTo(aValue);
|
---|
2272 |
|
---|
2273 | return S_OK;
|
---|
2274 | }
|
---|
2275 |
|
---|
2276 | STDMETHODIMP Medium::SetProperty(IN_BSTR aName, IN_BSTR aValue)
|
---|
2277 | {
|
---|
2278 | CheckComArgStrNotEmptyOrNull(aName);
|
---|
2279 |
|
---|
2280 | AutoCaller autoCaller(this);
|
---|
2281 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2282 |
|
---|
2283 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2284 |
|
---|
2285 | switch (m->state)
|
---|
2286 | {
|
---|
2287 | case MediumState_Created:
|
---|
2288 | case MediumState_Inaccessible:
|
---|
2289 | break;
|
---|
2290 | default:
|
---|
2291 | return setStateError();
|
---|
2292 | }
|
---|
2293 |
|
---|
2294 | settings::StringsMap::iterator it = m->mapProperties.find(Utf8Str(aName));
|
---|
2295 | if (it == m->mapProperties.end())
|
---|
2296 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2297 | tr("Property '%ls' does not exist"),
|
---|
2298 | aName);
|
---|
2299 |
|
---|
2300 | it->second = aValue;
|
---|
2301 |
|
---|
2302 | // save the global settings; for that we should hold only the VirtualBox lock
|
---|
2303 | mlock.release();
|
---|
2304 | AutoWriteLock alock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
2305 | HRESULT rc = m->pVirtualBox->saveSettings();
|
---|
2306 |
|
---|
2307 | return rc;
|
---|
2308 | }
|
---|
2309 |
|
---|
2310 | STDMETHODIMP Medium::GetProperties(IN_BSTR aNames,
|
---|
2311 | ComSafeArrayOut(BSTR, aReturnNames),
|
---|
2312 | ComSafeArrayOut(BSTR, aReturnValues))
|
---|
2313 | {
|
---|
2314 | CheckComArgOutSafeArrayPointerValid(aReturnNames);
|
---|
2315 | CheckComArgOutSafeArrayPointerValid(aReturnValues);
|
---|
2316 |
|
---|
2317 | AutoCaller autoCaller(this);
|
---|
2318 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2319 |
|
---|
2320 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2321 |
|
---|
2322 | /// @todo make use of aNames according to the documentation
|
---|
2323 | NOREF(aNames);
|
---|
2324 |
|
---|
2325 | com::SafeArray<BSTR> names(m->mapProperties.size());
|
---|
2326 | com::SafeArray<BSTR> values(m->mapProperties.size());
|
---|
2327 | size_t i = 0;
|
---|
2328 |
|
---|
2329 | for (settings::StringsMap::const_iterator it = m->mapProperties.begin();
|
---|
2330 | it != m->mapProperties.end();
|
---|
2331 | ++it)
|
---|
2332 | {
|
---|
2333 | it->first.cloneTo(&names[i]);
|
---|
2334 | it->second.cloneTo(&values[i]);
|
---|
2335 | ++i;
|
---|
2336 | }
|
---|
2337 |
|
---|
2338 | names.detachTo(ComSafeArrayOutArg(aReturnNames));
|
---|
2339 | values.detachTo(ComSafeArrayOutArg(aReturnValues));
|
---|
2340 |
|
---|
2341 | return S_OK;
|
---|
2342 | }
|
---|
2343 |
|
---|
2344 | STDMETHODIMP Medium::SetProperties(ComSafeArrayIn(IN_BSTR, aNames),
|
---|
2345 | ComSafeArrayIn(IN_BSTR, aValues))
|
---|
2346 | {
|
---|
2347 | CheckComArgSafeArrayNotNull(aNames);
|
---|
2348 | CheckComArgSafeArrayNotNull(aValues);
|
---|
2349 |
|
---|
2350 | AutoCaller autoCaller(this);
|
---|
2351 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2352 |
|
---|
2353 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2354 |
|
---|
2355 | com::SafeArray<IN_BSTR> names(ComSafeArrayInArg(aNames));
|
---|
2356 | com::SafeArray<IN_BSTR> values(ComSafeArrayInArg(aValues));
|
---|
2357 |
|
---|
2358 | /* first pass: validate names */
|
---|
2359 | for (size_t i = 0;
|
---|
2360 | i < names.size();
|
---|
2361 | ++i)
|
---|
2362 | {
|
---|
2363 | if (m->mapProperties.find(Utf8Str(names[i])) == m->mapProperties.end())
|
---|
2364 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2365 | tr("Property '%ls' does not exist"), names[i]);
|
---|
2366 | }
|
---|
2367 |
|
---|
2368 | /* second pass: assign */
|
---|
2369 | for (size_t i = 0;
|
---|
2370 | i < names.size();
|
---|
2371 | ++i)
|
---|
2372 | {
|
---|
2373 | settings::StringsMap::iterator it = m->mapProperties.find(Utf8Str(names[i]));
|
---|
2374 | AssertReturn(it != m->mapProperties.end(), E_FAIL);
|
---|
2375 |
|
---|
2376 | it->second = Utf8Str(values[i]);
|
---|
2377 | }
|
---|
2378 |
|
---|
2379 | mlock.release();
|
---|
2380 |
|
---|
2381 | // saveSettings needs vbox lock
|
---|
2382 | AutoWriteLock alock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
2383 | HRESULT rc = m->pVirtualBox->saveSettings();
|
---|
2384 |
|
---|
2385 | return rc;
|
---|
2386 | }
|
---|
2387 |
|
---|
2388 | STDMETHODIMP Medium::CreateBaseStorage(LONG64 aLogicalSize,
|
---|
2389 | MediumVariant_T aVariant,
|
---|
2390 | IProgress **aProgress)
|
---|
2391 | {
|
---|
2392 | CheckComArgOutPointerValid(aProgress);
|
---|
2393 | if (aLogicalSize < 0)
|
---|
2394 | return setError(E_INVALIDARG, tr("The medium size argument (%lld) is negative"), aLogicalSize);
|
---|
2395 |
|
---|
2396 | AutoCaller autoCaller(this);
|
---|
2397 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2398 |
|
---|
2399 | HRESULT rc = S_OK;
|
---|
2400 | ComObjPtr <Progress> pProgress;
|
---|
2401 | Medium::Task *pTask = NULL;
|
---|
2402 |
|
---|
2403 | try
|
---|
2404 | {
|
---|
2405 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2406 |
|
---|
2407 | aVariant = (MediumVariant_T)((unsigned)aVariant & (unsigned)~MediumVariant_Diff);
|
---|
2408 | if ( !(aVariant & MediumVariant_Fixed)
|
---|
2409 | && !(m->formatObj->getCapabilities() & MediumFormatCapabilities_CreateDynamic))
|
---|
2410 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2411 | tr("Medium format '%s' does not support dynamic storage creation"),
|
---|
2412 | m->strFormat.c_str());
|
---|
2413 | if ( (aVariant & MediumVariant_Fixed)
|
---|
2414 | && !(m->formatObj->getCapabilities() & MediumFormatCapabilities_CreateDynamic))
|
---|
2415 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2416 | tr("Medium format '%s' does not support fixed storage creation"),
|
---|
2417 | m->strFormat.c_str());
|
---|
2418 |
|
---|
2419 | if (m->state != MediumState_NotCreated)
|
---|
2420 | throw setStateError();
|
---|
2421 |
|
---|
2422 | pProgress.createObject();
|
---|
2423 | rc = pProgress->init(m->pVirtualBox,
|
---|
2424 | static_cast<IMedium*>(this),
|
---|
2425 | (aVariant & MediumVariant_Fixed)
|
---|
2426 | ? BstrFmt(tr("Creating fixed medium storage unit '%s'"), m->strLocationFull.c_str()).raw()
|
---|
2427 | : BstrFmt(tr("Creating dynamic medium storage unit '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
2428 | TRUE /* aCancelable */);
|
---|
2429 | if (FAILED(rc))
|
---|
2430 | throw rc;
|
---|
2431 |
|
---|
2432 | /* setup task object to carry out the operation asynchronously */
|
---|
2433 | pTask = new Medium::CreateBaseTask(this, pProgress, aLogicalSize,
|
---|
2434 | aVariant);
|
---|
2435 | rc = pTask->rc();
|
---|
2436 | AssertComRC(rc);
|
---|
2437 | if (FAILED(rc))
|
---|
2438 | throw rc;
|
---|
2439 |
|
---|
2440 | m->state = MediumState_Creating;
|
---|
2441 | }
|
---|
2442 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2443 |
|
---|
2444 | if (SUCCEEDED(rc))
|
---|
2445 | {
|
---|
2446 | rc = startThread(pTask);
|
---|
2447 |
|
---|
2448 | if (SUCCEEDED(rc))
|
---|
2449 | pProgress.queryInterfaceTo(aProgress);
|
---|
2450 | }
|
---|
2451 | else if (pTask != NULL)
|
---|
2452 | delete pTask;
|
---|
2453 |
|
---|
2454 | return rc;
|
---|
2455 | }
|
---|
2456 |
|
---|
2457 | STDMETHODIMP Medium::DeleteStorage(IProgress **aProgress)
|
---|
2458 | {
|
---|
2459 | CheckComArgOutPointerValid(aProgress);
|
---|
2460 |
|
---|
2461 | AutoCaller autoCaller(this);
|
---|
2462 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2463 |
|
---|
2464 | ComObjPtr<Progress> pProgress;
|
---|
2465 |
|
---|
2466 | GuidList llRegistriesThatNeedSaving;
|
---|
2467 | HRESULT rc = deleteStorage(&pProgress,
|
---|
2468 | false /* aWait */,
|
---|
2469 | &llRegistriesThatNeedSaving);
|
---|
2470 | m->pVirtualBox->saveRegistries(llRegistriesThatNeedSaving);
|
---|
2471 |
|
---|
2472 | if (SUCCEEDED(rc))
|
---|
2473 | pProgress.queryInterfaceTo(aProgress);
|
---|
2474 |
|
---|
2475 | return rc;
|
---|
2476 | }
|
---|
2477 |
|
---|
2478 | STDMETHODIMP Medium::CreateDiffStorage(IMedium *aTarget,
|
---|
2479 | MediumVariant_T aVariant,
|
---|
2480 | IProgress **aProgress)
|
---|
2481 | {
|
---|
2482 | CheckComArgNotNull(aTarget);
|
---|
2483 | CheckComArgOutPointerValid(aProgress);
|
---|
2484 |
|
---|
2485 | AutoCaller autoCaller(this);
|
---|
2486 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2487 |
|
---|
2488 | ComObjPtr<Medium> diff = static_cast<Medium*>(aTarget);
|
---|
2489 |
|
---|
2490 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2491 |
|
---|
2492 | if (m->type == MediumType_Writethrough)
|
---|
2493 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2494 | tr("Medium type of '%s' is Writethrough"),
|
---|
2495 | m->strLocationFull.c_str());
|
---|
2496 | else if (m->type == MediumType_Shareable)
|
---|
2497 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2498 | tr("Medium type of '%s' is Shareable"),
|
---|
2499 | m->strLocationFull.c_str());
|
---|
2500 | else if (m->type == MediumType_Readonly)
|
---|
2501 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2502 | tr("Medium type of '%s' is Readonly"),
|
---|
2503 | m->strLocationFull.c_str());
|
---|
2504 |
|
---|
2505 | /* Apply the normal locking logic to the entire chain. */
|
---|
2506 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
2507 | HRESULT rc = diff->createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2508 | true /* fMediumLockWrite */,
|
---|
2509 | this,
|
---|
2510 | *pMediumLockList);
|
---|
2511 | if (FAILED(rc))
|
---|
2512 | {
|
---|
2513 | delete pMediumLockList;
|
---|
2514 | return rc;
|
---|
2515 | }
|
---|
2516 |
|
---|
2517 | ComObjPtr <Progress> pProgress;
|
---|
2518 |
|
---|
2519 | rc = createDiffStorage(diff, aVariant, pMediumLockList, &pProgress,
|
---|
2520 | false /* aWait */, NULL /* pfNeedsGlobalSaveSettings*/);
|
---|
2521 | if (FAILED(rc))
|
---|
2522 | delete pMediumLockList;
|
---|
2523 | else
|
---|
2524 | pProgress.queryInterfaceTo(aProgress);
|
---|
2525 |
|
---|
2526 | return rc;
|
---|
2527 | }
|
---|
2528 |
|
---|
2529 | STDMETHODIMP Medium::MergeTo(IMedium *aTarget, IProgress **aProgress)
|
---|
2530 | {
|
---|
2531 | CheckComArgNotNull(aTarget);
|
---|
2532 | CheckComArgOutPointerValid(aProgress);
|
---|
2533 | ComAssertRet(aTarget != this, E_INVALIDARG);
|
---|
2534 |
|
---|
2535 | AutoCaller autoCaller(this);
|
---|
2536 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2537 |
|
---|
2538 | ComObjPtr<Medium> pTarget = static_cast<Medium*>(aTarget);
|
---|
2539 |
|
---|
2540 | bool fMergeForward = false;
|
---|
2541 | ComObjPtr<Medium> pParentForTarget;
|
---|
2542 | MediaList childrenToReparent;
|
---|
2543 | MediumLockList *pMediumLockList = NULL;
|
---|
2544 |
|
---|
2545 | HRESULT rc = S_OK;
|
---|
2546 |
|
---|
2547 | rc = prepareMergeTo(pTarget, NULL, NULL, true, fMergeForward,
|
---|
2548 | pParentForTarget, childrenToReparent, pMediumLockList);
|
---|
2549 | if (FAILED(rc)) return rc;
|
---|
2550 |
|
---|
2551 | ComObjPtr <Progress> pProgress;
|
---|
2552 |
|
---|
2553 | rc = mergeTo(pTarget, fMergeForward, pParentForTarget, childrenToReparent,
|
---|
2554 | pMediumLockList, &pProgress, false /* aWait */,
|
---|
2555 | NULL /* pfNeedsGlobalSaveSettings */);
|
---|
2556 | if (FAILED(rc))
|
---|
2557 | cancelMergeTo(childrenToReparent, pMediumLockList);
|
---|
2558 | else
|
---|
2559 | pProgress.queryInterfaceTo(aProgress);
|
---|
2560 |
|
---|
2561 | return rc;
|
---|
2562 | }
|
---|
2563 |
|
---|
2564 | STDMETHODIMP Medium::CloneTo(IMedium *aTarget,
|
---|
2565 | MediumVariant_T aVariant,
|
---|
2566 | IMedium *aParent,
|
---|
2567 | IProgress **aProgress)
|
---|
2568 | {
|
---|
2569 | CheckComArgNotNull(aTarget);
|
---|
2570 | CheckComArgOutPointerValid(aProgress);
|
---|
2571 | ComAssertRet(aTarget != this, E_INVALIDARG);
|
---|
2572 |
|
---|
2573 | AutoCaller autoCaller(this);
|
---|
2574 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2575 |
|
---|
2576 | ComObjPtr<Medium> pTarget = static_cast<Medium*>(aTarget);
|
---|
2577 | ComObjPtr<Medium> pParent;
|
---|
2578 | if (aParent)
|
---|
2579 | pParent = static_cast<Medium*>(aParent);
|
---|
2580 |
|
---|
2581 | HRESULT rc = S_OK;
|
---|
2582 | ComObjPtr<Progress> pProgress;
|
---|
2583 | Medium::Task *pTask = NULL;
|
---|
2584 |
|
---|
2585 | try
|
---|
2586 | {
|
---|
2587 | // locking: we need the tree lock first because we access parent pointers
|
---|
2588 | AutoReadLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
2589 | // and we need to write-lock the media involved
|
---|
2590 | AutoMultiWriteLock3 alock(this, pTarget, pParent COMMA_LOCKVAL_SRC_POS);
|
---|
2591 |
|
---|
2592 | if ( pTarget->m->state != MediumState_NotCreated
|
---|
2593 | && pTarget->m->state != MediumState_Created)
|
---|
2594 | throw pTarget->setStateError();
|
---|
2595 |
|
---|
2596 | /* Build the source lock list. */
|
---|
2597 | MediumLockList *pSourceMediumLockList(new MediumLockList());
|
---|
2598 | rc = createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2599 | false /* fMediumLockWrite */,
|
---|
2600 | NULL,
|
---|
2601 | *pSourceMediumLockList);
|
---|
2602 | if (FAILED(rc))
|
---|
2603 | {
|
---|
2604 | delete pSourceMediumLockList;
|
---|
2605 | throw rc;
|
---|
2606 | }
|
---|
2607 |
|
---|
2608 | /* Build the target lock list (including the to-be parent chain). */
|
---|
2609 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
2610 | rc = pTarget->createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2611 | true /* fMediumLockWrite */,
|
---|
2612 | pParent,
|
---|
2613 | *pTargetMediumLockList);
|
---|
2614 | if (FAILED(rc))
|
---|
2615 | {
|
---|
2616 | delete pSourceMediumLockList;
|
---|
2617 | delete pTargetMediumLockList;
|
---|
2618 | throw rc;
|
---|
2619 | }
|
---|
2620 |
|
---|
2621 | rc = pSourceMediumLockList->Lock();
|
---|
2622 | if (FAILED(rc))
|
---|
2623 | {
|
---|
2624 | delete pSourceMediumLockList;
|
---|
2625 | delete pTargetMediumLockList;
|
---|
2626 | throw setError(rc,
|
---|
2627 | tr("Failed to lock source media '%s'"),
|
---|
2628 | getLocationFull().c_str());
|
---|
2629 | }
|
---|
2630 | rc = pTargetMediumLockList->Lock();
|
---|
2631 | if (FAILED(rc))
|
---|
2632 | {
|
---|
2633 | delete pSourceMediumLockList;
|
---|
2634 | delete pTargetMediumLockList;
|
---|
2635 | throw setError(rc,
|
---|
2636 | tr("Failed to lock target media '%s'"),
|
---|
2637 | pTarget->getLocationFull().c_str());
|
---|
2638 | }
|
---|
2639 |
|
---|
2640 | pProgress.createObject();
|
---|
2641 | rc = pProgress->init(m->pVirtualBox,
|
---|
2642 | static_cast <IMedium *>(this),
|
---|
2643 | BstrFmt(tr("Creating clone medium '%s'"), pTarget->m->strLocationFull.c_str()).raw(),
|
---|
2644 | TRUE /* aCancelable */);
|
---|
2645 | if (FAILED(rc))
|
---|
2646 | {
|
---|
2647 | delete pSourceMediumLockList;
|
---|
2648 | delete pTargetMediumLockList;
|
---|
2649 | throw rc;
|
---|
2650 | }
|
---|
2651 |
|
---|
2652 | /* setup task object to carry out the operation asynchronously */
|
---|
2653 | pTask = new Medium::CloneTask(this, pProgress, pTarget, aVariant,
|
---|
2654 | pParent, pSourceMediumLockList,
|
---|
2655 | pTargetMediumLockList);
|
---|
2656 | rc = pTask->rc();
|
---|
2657 | AssertComRC(rc);
|
---|
2658 | if (FAILED(rc))
|
---|
2659 | throw rc;
|
---|
2660 |
|
---|
2661 | if (pTarget->m->state == MediumState_NotCreated)
|
---|
2662 | pTarget->m->state = MediumState_Creating;
|
---|
2663 | }
|
---|
2664 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2665 |
|
---|
2666 | if (SUCCEEDED(rc))
|
---|
2667 | {
|
---|
2668 | rc = startThread(pTask);
|
---|
2669 |
|
---|
2670 | if (SUCCEEDED(rc))
|
---|
2671 | pProgress.queryInterfaceTo(aProgress);
|
---|
2672 | }
|
---|
2673 | else if (pTask != NULL)
|
---|
2674 | delete pTask;
|
---|
2675 |
|
---|
2676 | return rc;
|
---|
2677 | }
|
---|
2678 |
|
---|
2679 | STDMETHODIMP Medium::Compact(IProgress **aProgress)
|
---|
2680 | {
|
---|
2681 | CheckComArgOutPointerValid(aProgress);
|
---|
2682 |
|
---|
2683 | AutoCaller autoCaller(this);
|
---|
2684 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2685 |
|
---|
2686 | HRESULT rc = S_OK;
|
---|
2687 | ComObjPtr <Progress> pProgress;
|
---|
2688 | Medium::Task *pTask = NULL;
|
---|
2689 |
|
---|
2690 | try
|
---|
2691 | {
|
---|
2692 | /* We need to lock both the current object, and the tree lock (would
|
---|
2693 | * cause a lock order violation otherwise) for createMediumLockList. */
|
---|
2694 | AutoMultiWriteLock2 multilock(&m->pVirtualBox->getMediaTreeLockHandle(),
|
---|
2695 | this->lockHandle()
|
---|
2696 | COMMA_LOCKVAL_SRC_POS);
|
---|
2697 |
|
---|
2698 | /* Build the medium lock list. */
|
---|
2699 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
2700 | rc = createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
2701 | true /* fMediumLockWrite */,
|
---|
2702 | NULL,
|
---|
2703 | *pMediumLockList);
|
---|
2704 | if (FAILED(rc))
|
---|
2705 | {
|
---|
2706 | delete pMediumLockList;
|
---|
2707 | throw rc;
|
---|
2708 | }
|
---|
2709 |
|
---|
2710 | rc = pMediumLockList->Lock();
|
---|
2711 | if (FAILED(rc))
|
---|
2712 | {
|
---|
2713 | delete pMediumLockList;
|
---|
2714 | throw setError(rc,
|
---|
2715 | tr("Failed to lock media when compacting '%s'"),
|
---|
2716 | getLocationFull().c_str());
|
---|
2717 | }
|
---|
2718 |
|
---|
2719 | pProgress.createObject();
|
---|
2720 | rc = pProgress->init(m->pVirtualBox,
|
---|
2721 | static_cast <IMedium *>(this),
|
---|
2722 | BstrFmt(tr("Compacting medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
2723 | TRUE /* aCancelable */);
|
---|
2724 | if (FAILED(rc))
|
---|
2725 | {
|
---|
2726 | delete pMediumLockList;
|
---|
2727 | throw rc;
|
---|
2728 | }
|
---|
2729 |
|
---|
2730 | /* setup task object to carry out the operation asynchronously */
|
---|
2731 | pTask = new Medium::CompactTask(this, pProgress, pMediumLockList);
|
---|
2732 | rc = pTask->rc();
|
---|
2733 | AssertComRC(rc);
|
---|
2734 | if (FAILED(rc))
|
---|
2735 | throw rc;
|
---|
2736 | }
|
---|
2737 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2738 |
|
---|
2739 | if (SUCCEEDED(rc))
|
---|
2740 | {
|
---|
2741 | rc = startThread(pTask);
|
---|
2742 |
|
---|
2743 | if (SUCCEEDED(rc))
|
---|
2744 | pProgress.queryInterfaceTo(aProgress);
|
---|
2745 | }
|
---|
2746 | else if (pTask != NULL)
|
---|
2747 | delete pTask;
|
---|
2748 |
|
---|
2749 | return rc;
|
---|
2750 | }
|
---|
2751 |
|
---|
2752 | STDMETHODIMP Medium::Resize(LONG64 aLogicalSize, IProgress **aProgress)
|
---|
2753 | {
|
---|
2754 | CheckComArgOutPointerValid(aProgress);
|
---|
2755 |
|
---|
2756 | AutoCaller autoCaller(this);
|
---|
2757 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2758 |
|
---|
2759 | HRESULT rc = S_OK;
|
---|
2760 | ComObjPtr <Progress> pProgress;
|
---|
2761 | Medium::Task *pTask = NULL;
|
---|
2762 |
|
---|
2763 | try
|
---|
2764 | {
|
---|
2765 | /* We need to lock both the current object, and the tree lock (would
|
---|
2766 | * cause a lock order violation otherwise) for createMediumLockList. */
|
---|
2767 | AutoMultiWriteLock2 multilock(&m->pVirtualBox->getMediaTreeLockHandle(),
|
---|
2768 | this->lockHandle()
|
---|
2769 | COMMA_LOCKVAL_SRC_POS);
|
---|
2770 |
|
---|
2771 | /* Build the medium lock list. */
|
---|
2772 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
2773 | rc = createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
2774 | true /* fMediumLockWrite */,
|
---|
2775 | NULL,
|
---|
2776 | *pMediumLockList);
|
---|
2777 | if (FAILED(rc))
|
---|
2778 | {
|
---|
2779 | delete pMediumLockList;
|
---|
2780 | throw rc;
|
---|
2781 | }
|
---|
2782 |
|
---|
2783 | rc = pMediumLockList->Lock();
|
---|
2784 | if (FAILED(rc))
|
---|
2785 | {
|
---|
2786 | delete pMediumLockList;
|
---|
2787 | throw setError(rc,
|
---|
2788 | tr("Failed to lock media when compacting '%s'"),
|
---|
2789 | getLocationFull().c_str());
|
---|
2790 | }
|
---|
2791 |
|
---|
2792 | pProgress.createObject();
|
---|
2793 | rc = pProgress->init(m->pVirtualBox,
|
---|
2794 | static_cast <IMedium *>(this),
|
---|
2795 | BstrFmt(tr("Compacting medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
2796 | TRUE /* aCancelable */);
|
---|
2797 | if (FAILED(rc))
|
---|
2798 | {
|
---|
2799 | delete pMediumLockList;
|
---|
2800 | throw rc;
|
---|
2801 | }
|
---|
2802 |
|
---|
2803 | /* setup task object to carry out the operation asynchronously */
|
---|
2804 | pTask = new Medium::ResizeTask(this, aLogicalSize, pProgress, pMediumLockList);
|
---|
2805 | rc = pTask->rc();
|
---|
2806 | AssertComRC(rc);
|
---|
2807 | if (FAILED(rc))
|
---|
2808 | throw rc;
|
---|
2809 | }
|
---|
2810 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2811 |
|
---|
2812 | if (SUCCEEDED(rc))
|
---|
2813 | {
|
---|
2814 | rc = startThread(pTask);
|
---|
2815 |
|
---|
2816 | if (SUCCEEDED(rc))
|
---|
2817 | pProgress.queryInterfaceTo(aProgress);
|
---|
2818 | }
|
---|
2819 | else if (pTask != NULL)
|
---|
2820 | delete pTask;
|
---|
2821 |
|
---|
2822 | return rc;
|
---|
2823 | }
|
---|
2824 |
|
---|
2825 | STDMETHODIMP Medium::Reset(IProgress **aProgress)
|
---|
2826 | {
|
---|
2827 | CheckComArgOutPointerValid(aProgress);
|
---|
2828 |
|
---|
2829 | AutoCaller autoCaller(this);
|
---|
2830 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2831 |
|
---|
2832 | HRESULT rc = S_OK;
|
---|
2833 | ComObjPtr <Progress> pProgress;
|
---|
2834 | Medium::Task *pTask = NULL;
|
---|
2835 |
|
---|
2836 | try
|
---|
2837 | {
|
---|
2838 | /* canClose() needs the tree lock */
|
---|
2839 | AutoMultiWriteLock2 multilock(&m->pVirtualBox->getMediaTreeLockHandle(),
|
---|
2840 | this->lockHandle()
|
---|
2841 | COMMA_LOCKVAL_SRC_POS);
|
---|
2842 |
|
---|
2843 | LogFlowThisFunc(("ENTER for medium %s\n", m->strLocationFull.c_str()));
|
---|
2844 |
|
---|
2845 | if (m->pParent.isNull())
|
---|
2846 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2847 | tr("Medium type of '%s' is not differencing"),
|
---|
2848 | m->strLocationFull.c_str());
|
---|
2849 |
|
---|
2850 | rc = canClose();
|
---|
2851 | if (FAILED(rc))
|
---|
2852 | throw rc;
|
---|
2853 |
|
---|
2854 | /* Build the medium lock list. */
|
---|
2855 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
2856 | rc = createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2857 | true /* fMediumLockWrite */,
|
---|
2858 | NULL,
|
---|
2859 | *pMediumLockList);
|
---|
2860 | if (FAILED(rc))
|
---|
2861 | {
|
---|
2862 | delete pMediumLockList;
|
---|
2863 | throw rc;
|
---|
2864 | }
|
---|
2865 |
|
---|
2866 | rc = pMediumLockList->Lock();
|
---|
2867 | if (FAILED(rc))
|
---|
2868 | {
|
---|
2869 | delete pMediumLockList;
|
---|
2870 | throw setError(rc,
|
---|
2871 | tr("Failed to lock media when resetting '%s'"),
|
---|
2872 | getLocationFull().c_str());
|
---|
2873 | }
|
---|
2874 |
|
---|
2875 | pProgress.createObject();
|
---|
2876 | rc = pProgress->init(m->pVirtualBox,
|
---|
2877 | static_cast<IMedium*>(this),
|
---|
2878 | BstrFmt(tr("Resetting differencing medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
2879 | FALSE /* aCancelable */);
|
---|
2880 | if (FAILED(rc))
|
---|
2881 | throw rc;
|
---|
2882 |
|
---|
2883 | /* setup task object to carry out the operation asynchronously */
|
---|
2884 | pTask = new Medium::ResetTask(this, pProgress, pMediumLockList);
|
---|
2885 | rc = pTask->rc();
|
---|
2886 | AssertComRC(rc);
|
---|
2887 | if (FAILED(rc))
|
---|
2888 | throw rc;
|
---|
2889 | }
|
---|
2890 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2891 |
|
---|
2892 | if (SUCCEEDED(rc))
|
---|
2893 | {
|
---|
2894 | rc = startThread(pTask);
|
---|
2895 |
|
---|
2896 | if (SUCCEEDED(rc))
|
---|
2897 | pProgress.queryInterfaceTo(aProgress);
|
---|
2898 | }
|
---|
2899 | else
|
---|
2900 | {
|
---|
2901 | /* Note: on success, the task will unlock this */
|
---|
2902 | {
|
---|
2903 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2904 | HRESULT rc2 = UnlockWrite(NULL);
|
---|
2905 | AssertComRC(rc2);
|
---|
2906 | }
|
---|
2907 | if (pTask != NULL)
|
---|
2908 | delete pTask;
|
---|
2909 | }
|
---|
2910 |
|
---|
2911 | LogFlowThisFunc(("LEAVE, rc=%Rhrc\n", rc));
|
---|
2912 |
|
---|
2913 | return rc;
|
---|
2914 | }
|
---|
2915 |
|
---|
2916 | ////////////////////////////////////////////////////////////////////////////////
|
---|
2917 | //
|
---|
2918 | // Medium public internal methods
|
---|
2919 | //
|
---|
2920 | ////////////////////////////////////////////////////////////////////////////////
|
---|
2921 |
|
---|
2922 | /**
|
---|
2923 | * Internal method to return the medium's parent medium. Must have caller + locking!
|
---|
2924 | * @return
|
---|
2925 | */
|
---|
2926 | const ComObjPtr<Medium>& Medium::getParent() const
|
---|
2927 | {
|
---|
2928 | return m->pParent;
|
---|
2929 | }
|
---|
2930 |
|
---|
2931 | /**
|
---|
2932 | * Internal method to return the medium's list of child media. Must have caller + locking!
|
---|
2933 | * @return
|
---|
2934 | */
|
---|
2935 | const MediaList& Medium::getChildren() const
|
---|
2936 | {
|
---|
2937 | return m->llChildren;
|
---|
2938 | }
|
---|
2939 |
|
---|
2940 | /**
|
---|
2941 | * Internal method to return the medium's GUID. Must have caller + locking!
|
---|
2942 | * @return
|
---|
2943 | */
|
---|
2944 | const Guid& Medium::getId() const
|
---|
2945 | {
|
---|
2946 | return m->id;
|
---|
2947 | }
|
---|
2948 |
|
---|
2949 | /**
|
---|
2950 | * Internal method to return the medium's state. Must have caller + locking!
|
---|
2951 | * @return
|
---|
2952 | */
|
---|
2953 | MediumState_T Medium::getState() const
|
---|
2954 | {
|
---|
2955 | return m->state;
|
---|
2956 | }
|
---|
2957 |
|
---|
2958 | /**
|
---|
2959 | * Internal method to return the medium's variant. Must have caller + locking!
|
---|
2960 | * @return
|
---|
2961 | */
|
---|
2962 | MediumVariant_T Medium::getVariant() const
|
---|
2963 | {
|
---|
2964 | return m->variant;
|
---|
2965 | }
|
---|
2966 |
|
---|
2967 | /**
|
---|
2968 | * Internal method which returns true if this medium represents a host drive.
|
---|
2969 | * @return
|
---|
2970 | */
|
---|
2971 | bool Medium::isHostDrive() const
|
---|
2972 | {
|
---|
2973 | return m->hostDrive;
|
---|
2974 | }
|
---|
2975 |
|
---|
2976 | /**
|
---|
2977 | * Internal method to return the medium's full location. Must have caller + locking!
|
---|
2978 | * @return
|
---|
2979 | */
|
---|
2980 | const Utf8Str& Medium::getLocationFull() const
|
---|
2981 | {
|
---|
2982 | return m->strLocationFull;
|
---|
2983 | }
|
---|
2984 |
|
---|
2985 | /**
|
---|
2986 | * Internal method to return the medium's format string. Must have caller + locking!
|
---|
2987 | * @return
|
---|
2988 | */
|
---|
2989 | const Utf8Str& Medium::getFormat() const
|
---|
2990 | {
|
---|
2991 | return m->strFormat;
|
---|
2992 | }
|
---|
2993 |
|
---|
2994 | /**
|
---|
2995 | * Internal method to return the medium's format object. Must have caller + locking!
|
---|
2996 | * @return
|
---|
2997 | */
|
---|
2998 | const ComObjPtr<MediumFormat>& Medium::getMediumFormat() const
|
---|
2999 | {
|
---|
3000 | return m->formatObj;
|
---|
3001 | }
|
---|
3002 |
|
---|
3003 | /**
|
---|
3004 | * Internal method that returns true if the medium is represented by a file on the host disk
|
---|
3005 | * (and not iSCSI or something).
|
---|
3006 | * @return
|
---|
3007 | */
|
---|
3008 | bool Medium::isMediumFormatFile() const
|
---|
3009 | {
|
---|
3010 | if ( m->formatObj
|
---|
3011 | && (m->formatObj->getCapabilities() & MediumFormatCapabilities_File)
|
---|
3012 | )
|
---|
3013 | return true;
|
---|
3014 | return false;
|
---|
3015 | }
|
---|
3016 |
|
---|
3017 | /**
|
---|
3018 | * Internal method to return the medium's size. Must have caller + locking!
|
---|
3019 | * @return
|
---|
3020 | */
|
---|
3021 | uint64_t Medium::getSize() const
|
---|
3022 | {
|
---|
3023 | return m->size;
|
---|
3024 | }
|
---|
3025 |
|
---|
3026 | /**
|
---|
3027 | * Returns the medium device type. Must have caller + locking!
|
---|
3028 | * @return
|
---|
3029 | */
|
---|
3030 | DeviceType_T Medium::getDeviceType() const
|
---|
3031 | {
|
---|
3032 | return m->devType;
|
---|
3033 | }
|
---|
3034 |
|
---|
3035 | /**
|
---|
3036 | * Returns the medium type. Must have caller + locking!
|
---|
3037 | * @return
|
---|
3038 | */
|
---|
3039 | MediumType_T Medium::getType() const
|
---|
3040 | {
|
---|
3041 | return m->type;
|
---|
3042 | }
|
---|
3043 |
|
---|
3044 | /**
|
---|
3045 | * Returns a short version of the location attribute.
|
---|
3046 | *
|
---|
3047 | * @note Must be called from under this object's read or write lock.
|
---|
3048 | */
|
---|
3049 | Utf8Str Medium::getName()
|
---|
3050 | {
|
---|
3051 | Utf8Str name = RTPathFilename(m->strLocationFull.c_str());
|
---|
3052 | return name;
|
---|
3053 | }
|
---|
3054 |
|
---|
3055 | /**
|
---|
3056 | * This adds the given UUID to the list of media registries in which this
|
---|
3057 | * medium should be registered. The UUID can either be a machine UUID,
|
---|
3058 | * to add a machine registry, or the global registry UUID as returned by
|
---|
3059 | * VirtualBox::getGlobalRegistryId().
|
---|
3060 | *
|
---|
3061 | * Note that for hard disks, this method does nothing if the medium is
|
---|
3062 | * already in another registry to avoid having hard disks in more than
|
---|
3063 | * one registry, which causes trouble with keeping diff images in sync.
|
---|
3064 | * See getFirstRegistryMachineId() for details.
|
---|
3065 | *
|
---|
3066 | * @param id
|
---|
3067 | * @return true if the registry was added; false if the given id was already on the list.
|
---|
3068 | */
|
---|
3069 | bool Medium::addRegistry(const Guid& id)
|
---|
3070 | {
|
---|
3071 | AutoCaller autoCaller(this);
|
---|
3072 | if (FAILED(autoCaller.rc())) return false;
|
---|
3073 |
|
---|
3074 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3075 |
|
---|
3076 | if ( m->devType == DeviceType_HardDisk
|
---|
3077 | && m->llRegistryIDs.size() > 0
|
---|
3078 | )
|
---|
3079 | return false;
|
---|
3080 |
|
---|
3081 | // no need to add the UUID twice
|
---|
3082 | for (GuidList::const_iterator it = m->llRegistryIDs.begin();
|
---|
3083 | it != m->llRegistryIDs.end();
|
---|
3084 | ++it)
|
---|
3085 | {
|
---|
3086 | if ((*it) == id)
|
---|
3087 | return false;
|
---|
3088 | }
|
---|
3089 |
|
---|
3090 | m->llRegistryIDs.push_back(id);
|
---|
3091 | return true;
|
---|
3092 | }
|
---|
3093 |
|
---|
3094 | /**
|
---|
3095 | * Returns true if id is in the list of media registries for this medium.
|
---|
3096 | * @param id
|
---|
3097 | * @return
|
---|
3098 | */
|
---|
3099 | bool Medium::isInRegistry(const Guid& id)
|
---|
3100 | {
|
---|
3101 | AutoCaller autoCaller(this);
|
---|
3102 | if (FAILED(autoCaller.rc())) return false;
|
---|
3103 |
|
---|
3104 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3105 |
|
---|
3106 | for (GuidList::const_iterator it = m->llRegistryIDs.begin();
|
---|
3107 | it != m->llRegistryIDs.end();
|
---|
3108 | ++it)
|
---|
3109 | {
|
---|
3110 | if (*it == id)
|
---|
3111 | return true;
|
---|
3112 | }
|
---|
3113 |
|
---|
3114 | return false;
|
---|
3115 | }
|
---|
3116 |
|
---|
3117 | /**
|
---|
3118 | * Internal method to return the medium's first registry machine (i.e. the machine in whose
|
---|
3119 | * machine XML this medium is listed).
|
---|
3120 | *
|
---|
3121 | * Every medium must now (4.0) reside in at least one media registry, which is identified by
|
---|
3122 | * a UUID. This is either a machine UUID if the machine is from 4.0 or newer, in which case
|
---|
3123 | * machines have their own media registries, or it is the pseudo-UUID of the VirtualBox
|
---|
3124 | * object if the machine is old and still needs the global registry in VirtualBox.xml.
|
---|
3125 | *
|
---|
3126 | * By definition, hard disks may only be in one media registry, in which all its children
|
---|
3127 | * will be stored as well. Otherwise we run into problems with having keep multiple registries
|
---|
3128 | * in sync. (This is the "cloned VM" case in which VM1 may link to the disks of VM2; in this
|
---|
3129 | * case, only VM2's registry is used for the disk in question.)
|
---|
3130 | *
|
---|
3131 | * ISOs and RAWs, by contrast, can be in more than one repository to make things easier for
|
---|
3132 | * the user.
|
---|
3133 | *
|
---|
3134 | * Must have caller + locking!
|
---|
3135 | *
|
---|
3136 | * @return
|
---|
3137 | */
|
---|
3138 | const Guid& Medium::getFirstRegistryMachineId() const
|
---|
3139 | {
|
---|
3140 | return m->llRegistryIDs.front();
|
---|
3141 | }
|
---|
3142 |
|
---|
3143 | /**
|
---|
3144 | * Adds all the IDs of the registries in which this medium is registered to the given list
|
---|
3145 | * of UUIDs, but only if they are not on the list yet.
|
---|
3146 | * @param llRegistryIDs
|
---|
3147 | */
|
---|
3148 | HRESULT Medium::addToRegistryIDList(GuidList &llRegistryIDs)
|
---|
3149 | {
|
---|
3150 | AutoCaller autoCaller(this);
|
---|
3151 | if (FAILED(autoCaller.rc())) return false;
|
---|
3152 |
|
---|
3153 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3154 |
|
---|
3155 | for (GuidList::const_iterator it = m->llRegistryIDs.begin();
|
---|
3156 | it != m->llRegistryIDs.end();
|
---|
3157 | ++it)
|
---|
3158 | {
|
---|
3159 | m->pVirtualBox->addGuidToListUniquely(llRegistryIDs, *it);
|
---|
3160 | }
|
---|
3161 |
|
---|
3162 | return S_OK;
|
---|
3163 | }
|
---|
3164 |
|
---|
3165 | /**
|
---|
3166 | * Adds the given machine and optionally the snapshot to the list of the objects
|
---|
3167 | * this medium is attached to.
|
---|
3168 | *
|
---|
3169 | * @param aMachineId Machine ID.
|
---|
3170 | * @param aSnapshotId Snapshot ID; when non-empty, adds a snapshot attachment.
|
---|
3171 | */
|
---|
3172 | HRESULT Medium::addBackReference(const Guid &aMachineId,
|
---|
3173 | const Guid &aSnapshotId /*= Guid::Empty*/)
|
---|
3174 | {
|
---|
3175 | AssertReturn(!aMachineId.isEmpty(), E_FAIL);
|
---|
3176 |
|
---|
3177 | LogFlowThisFunc(("ENTER, aMachineId: {%RTuuid}, aSnapshotId: {%RTuuid}\n", aMachineId.raw(), aSnapshotId.raw()));
|
---|
3178 |
|
---|
3179 | AutoCaller autoCaller(this);
|
---|
3180 | AssertComRCReturnRC(autoCaller.rc());
|
---|
3181 |
|
---|
3182 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3183 |
|
---|
3184 | switch (m->state)
|
---|
3185 | {
|
---|
3186 | case MediumState_Created:
|
---|
3187 | case MediumState_Inaccessible:
|
---|
3188 | case MediumState_LockedRead:
|
---|
3189 | case MediumState_LockedWrite:
|
---|
3190 | break;
|
---|
3191 |
|
---|
3192 | default:
|
---|
3193 | return setStateError();
|
---|
3194 | }
|
---|
3195 |
|
---|
3196 | if (m->numCreateDiffTasks > 0)
|
---|
3197 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
3198 | tr("Cannot attach medium '%s' {%RTuuid}: %u differencing child media are being created"),
|
---|
3199 | m->strLocationFull.c_str(),
|
---|
3200 | m->id.raw(),
|
---|
3201 | m->numCreateDiffTasks);
|
---|
3202 |
|
---|
3203 | BackRefList::iterator it = std::find_if(m->backRefs.begin(),
|
---|
3204 | m->backRefs.end(),
|
---|
3205 | BackRef::EqualsTo(aMachineId));
|
---|
3206 | if (it == m->backRefs.end())
|
---|
3207 | {
|
---|
3208 | BackRef ref(aMachineId, aSnapshotId);
|
---|
3209 | m->backRefs.push_back(ref);
|
---|
3210 |
|
---|
3211 | return S_OK;
|
---|
3212 | }
|
---|
3213 |
|
---|
3214 | // if the caller has not supplied a snapshot ID, then we're attaching
|
---|
3215 | // to a machine a medium which represents the machine's current state,
|
---|
3216 | // so set the flag
|
---|
3217 | if (aSnapshotId.isEmpty())
|
---|
3218 | {
|
---|
3219 | /* sanity: no duplicate attachments */
|
---|
3220 | AssertReturn(!it->fInCurState, E_FAIL);
|
---|
3221 | it->fInCurState = true;
|
---|
3222 |
|
---|
3223 | return S_OK;
|
---|
3224 | }
|
---|
3225 |
|
---|
3226 | // otherwise: a snapshot medium is being attached
|
---|
3227 |
|
---|
3228 | /* sanity: no duplicate attachments */
|
---|
3229 | for (GuidList::const_iterator jt = it->llSnapshotIds.begin();
|
---|
3230 | jt != it->llSnapshotIds.end();
|
---|
3231 | ++jt)
|
---|
3232 | {
|
---|
3233 | const Guid &idOldSnapshot = *jt;
|
---|
3234 |
|
---|
3235 | if (idOldSnapshot == aSnapshotId)
|
---|
3236 | {
|
---|
3237 | #ifdef DEBUG
|
---|
3238 | dumpBackRefs();
|
---|
3239 | #endif
|
---|
3240 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
3241 | tr("Cannot attach medium '%s' {%RTuuid} from snapshot '%RTuuid': medium is already in use by this snapshot!"),
|
---|
3242 | m->strLocationFull.c_str(),
|
---|
3243 | m->id.raw(),
|
---|
3244 | aSnapshotId.raw(),
|
---|
3245 | idOldSnapshot.raw());
|
---|
3246 | }
|
---|
3247 | }
|
---|
3248 |
|
---|
3249 | it->llSnapshotIds.push_back(aSnapshotId);
|
---|
3250 | it->fInCurState = false;
|
---|
3251 |
|
---|
3252 | LogFlowThisFuncLeave();
|
---|
3253 |
|
---|
3254 | return S_OK;
|
---|
3255 | }
|
---|
3256 |
|
---|
3257 | /**
|
---|
3258 | * Removes the given machine and optionally the snapshot from the list of the
|
---|
3259 | * objects this medium is attached to.
|
---|
3260 | *
|
---|
3261 | * @param aMachineId Machine ID.
|
---|
3262 | * @param aSnapshotId Snapshot ID; when non-empty, removes the snapshot
|
---|
3263 | * attachment.
|
---|
3264 | */
|
---|
3265 | HRESULT Medium::removeBackReference(const Guid &aMachineId,
|
---|
3266 | const Guid &aSnapshotId /*= Guid::Empty*/)
|
---|
3267 | {
|
---|
3268 | AssertReturn(!aMachineId.isEmpty(), E_FAIL);
|
---|
3269 |
|
---|
3270 | AutoCaller autoCaller(this);
|
---|
3271 | AssertComRCReturnRC(autoCaller.rc());
|
---|
3272 |
|
---|
3273 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3274 |
|
---|
3275 | BackRefList::iterator it =
|
---|
3276 | std::find_if(m->backRefs.begin(), m->backRefs.end(),
|
---|
3277 | BackRef::EqualsTo(aMachineId));
|
---|
3278 | AssertReturn(it != m->backRefs.end(), E_FAIL);
|
---|
3279 |
|
---|
3280 | if (aSnapshotId.isEmpty())
|
---|
3281 | {
|
---|
3282 | /* remove the current state attachment */
|
---|
3283 | it->fInCurState = false;
|
---|
3284 | }
|
---|
3285 | else
|
---|
3286 | {
|
---|
3287 | /* remove the snapshot attachment */
|
---|
3288 | GuidList::iterator jt = std::find(it->llSnapshotIds.begin(),
|
---|
3289 | it->llSnapshotIds.end(),
|
---|
3290 | aSnapshotId);
|
---|
3291 |
|
---|
3292 | AssertReturn(jt != it->llSnapshotIds.end(), E_FAIL);
|
---|
3293 | it->llSnapshotIds.erase(jt);
|
---|
3294 | }
|
---|
3295 |
|
---|
3296 | /* if the backref becomes empty, remove it */
|
---|
3297 | if (it->fInCurState == false && it->llSnapshotIds.size() == 0)
|
---|
3298 | m->backRefs.erase(it);
|
---|
3299 |
|
---|
3300 | return S_OK;
|
---|
3301 | }
|
---|
3302 |
|
---|
3303 | /**
|
---|
3304 | * Internal method to return the medium's list of backrefs. Must have caller + locking!
|
---|
3305 | * @return
|
---|
3306 | */
|
---|
3307 | const Guid* Medium::getFirstMachineBackrefId() const
|
---|
3308 | {
|
---|
3309 | if (!m->backRefs.size())
|
---|
3310 | return NULL;
|
---|
3311 |
|
---|
3312 | return &m->backRefs.front().machineId;
|
---|
3313 | }
|
---|
3314 |
|
---|
3315 | const Guid* Medium::getFirstMachineBackrefSnapshotId() const
|
---|
3316 | {
|
---|
3317 | if (!m->backRefs.size())
|
---|
3318 | return NULL;
|
---|
3319 |
|
---|
3320 | const BackRef &ref = m->backRefs.front();
|
---|
3321 | if (!ref.llSnapshotIds.size())
|
---|
3322 | return NULL;
|
---|
3323 |
|
---|
3324 | return &ref.llSnapshotIds.front();
|
---|
3325 | }
|
---|
3326 |
|
---|
3327 | #ifdef DEBUG
|
---|
3328 | /**
|
---|
3329 | * Debugging helper that gets called after VirtualBox initialization that writes all
|
---|
3330 | * machine backreferences to the debug log.
|
---|
3331 | */
|
---|
3332 | void Medium::dumpBackRefs()
|
---|
3333 | {
|
---|
3334 | AutoCaller autoCaller(this);
|
---|
3335 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3336 |
|
---|
3337 | LogFlowThisFunc(("Dumping backrefs for medium '%s':\n", m->strLocationFull.c_str()));
|
---|
3338 |
|
---|
3339 | for (BackRefList::iterator it2 = m->backRefs.begin();
|
---|
3340 | it2 != m->backRefs.end();
|
---|
3341 | ++it2)
|
---|
3342 | {
|
---|
3343 | const BackRef &ref = *it2;
|
---|
3344 | LogFlowThisFunc((" Backref from machine {%RTuuid} (fInCurState: %d)\n", ref.machineId.raw(), ref.fInCurState));
|
---|
3345 |
|
---|
3346 | for (GuidList::const_iterator jt2 = it2->llSnapshotIds.begin();
|
---|
3347 | jt2 != it2->llSnapshotIds.end();
|
---|
3348 | ++jt2)
|
---|
3349 | {
|
---|
3350 | const Guid &id = *jt2;
|
---|
3351 | LogFlowThisFunc((" Backref from snapshot {%RTuuid}\n", id.raw()));
|
---|
3352 | }
|
---|
3353 | }
|
---|
3354 | }
|
---|
3355 | #endif
|
---|
3356 |
|
---|
3357 | /**
|
---|
3358 | * Checks if the given change of \a aOldPath to \a aNewPath affects the location
|
---|
3359 | * of this media and updates it if necessary to reflect the new location.
|
---|
3360 | *
|
---|
3361 | * @param aOldPath Old path (full).
|
---|
3362 | * @param aNewPath New path (full).
|
---|
3363 | *
|
---|
3364 | * @note Locks this object for writing.
|
---|
3365 | */
|
---|
3366 | HRESULT Medium::updatePath(const Utf8Str &strOldPath, const Utf8Str &strNewPath)
|
---|
3367 | {
|
---|
3368 | AssertReturn(!strOldPath.isEmpty(), E_FAIL);
|
---|
3369 | AssertReturn(!strNewPath.isEmpty(), E_FAIL);
|
---|
3370 |
|
---|
3371 | AutoCaller autoCaller(this);
|
---|
3372 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
3373 |
|
---|
3374 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3375 |
|
---|
3376 | LogFlowThisFunc(("locationFull.before='%s'\n", m->strLocationFull.c_str()));
|
---|
3377 |
|
---|
3378 | const char *pcszMediumPath = m->strLocationFull.c_str();
|
---|
3379 |
|
---|
3380 | if (RTPathStartsWith(pcszMediumPath, strOldPath.c_str()))
|
---|
3381 | {
|
---|
3382 | Utf8Str newPath(strNewPath);
|
---|
3383 | newPath.append(pcszMediumPath + strOldPath.length());
|
---|
3384 | unconst(m->strLocationFull) = newPath;
|
---|
3385 |
|
---|
3386 | LogFlowThisFunc(("locationFull.after='%s'\n", m->strLocationFull.c_str()));
|
---|
3387 | }
|
---|
3388 |
|
---|
3389 | return S_OK;
|
---|
3390 | }
|
---|
3391 |
|
---|
3392 | /**
|
---|
3393 | * Returns the base medium of the media chain this medium is part of.
|
---|
3394 | *
|
---|
3395 | * The base medium is found by walking up the parent-child relationship axis.
|
---|
3396 | * If the medium doesn't have a parent (i.e. it's a base medium), it
|
---|
3397 | * returns itself in response to this method.
|
---|
3398 | *
|
---|
3399 | * @param aLevel Where to store the number of ancestors of this medium
|
---|
3400 | * (zero for the base), may be @c NULL.
|
---|
3401 | *
|
---|
3402 | * @note Locks medium tree for reading.
|
---|
3403 | */
|
---|
3404 | ComObjPtr<Medium> Medium::getBase(uint32_t *aLevel /*= NULL*/)
|
---|
3405 | {
|
---|
3406 | ComObjPtr<Medium> pBase;
|
---|
3407 | uint32_t level;
|
---|
3408 |
|
---|
3409 | AutoCaller autoCaller(this);
|
---|
3410 | AssertReturn(autoCaller.isOk(), pBase);
|
---|
3411 |
|
---|
3412 | /* we access mParent */
|
---|
3413 | AutoReadLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3414 |
|
---|
3415 | pBase = this;
|
---|
3416 | level = 0;
|
---|
3417 |
|
---|
3418 | if (m->pParent)
|
---|
3419 | {
|
---|
3420 | for (;;)
|
---|
3421 | {
|
---|
3422 | AutoCaller baseCaller(pBase);
|
---|
3423 | AssertReturn(baseCaller.isOk(), pBase);
|
---|
3424 |
|
---|
3425 | if (pBase->m->pParent.isNull())
|
---|
3426 | break;
|
---|
3427 |
|
---|
3428 | pBase = pBase->m->pParent;
|
---|
3429 | ++level;
|
---|
3430 | }
|
---|
3431 | }
|
---|
3432 |
|
---|
3433 | if (aLevel != NULL)
|
---|
3434 | *aLevel = level;
|
---|
3435 |
|
---|
3436 | return pBase;
|
---|
3437 | }
|
---|
3438 |
|
---|
3439 | /**
|
---|
3440 | * Returns @c true if this medium cannot be modified because it has
|
---|
3441 | * dependents (children) or is part of the snapshot. Related to the medium
|
---|
3442 | * type and posterity, not to the current media state.
|
---|
3443 | *
|
---|
3444 | * @note Locks this object and medium tree for reading.
|
---|
3445 | */
|
---|
3446 | bool Medium::isReadOnly()
|
---|
3447 | {
|
---|
3448 | AutoCaller autoCaller(this);
|
---|
3449 | AssertComRCReturn(autoCaller.rc(), false);
|
---|
3450 |
|
---|
3451 | /* we access children */
|
---|
3452 | AutoReadLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3453 |
|
---|
3454 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3455 |
|
---|
3456 | switch (m->type)
|
---|
3457 | {
|
---|
3458 | case MediumType_Normal:
|
---|
3459 | {
|
---|
3460 | if (getChildren().size() != 0)
|
---|
3461 | return true;
|
---|
3462 |
|
---|
3463 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
3464 | it != m->backRefs.end(); ++it)
|
---|
3465 | if (it->llSnapshotIds.size() != 0)
|
---|
3466 | return true;
|
---|
3467 |
|
---|
3468 | return false;
|
---|
3469 | }
|
---|
3470 | case MediumType_Immutable:
|
---|
3471 | case MediumType_MultiAttach:
|
---|
3472 | return true;
|
---|
3473 | case MediumType_Writethrough:
|
---|
3474 | case MediumType_Shareable:
|
---|
3475 | case MediumType_Readonly: /* explicit readonly media has no diffs */
|
---|
3476 | return false;
|
---|
3477 | default:
|
---|
3478 | break;
|
---|
3479 | }
|
---|
3480 |
|
---|
3481 | AssertFailedReturn(false);
|
---|
3482 | }
|
---|
3483 |
|
---|
3484 | /**
|
---|
3485 | * Saves medium data by appending a new child node to the given
|
---|
3486 | * parent XML settings node.
|
---|
3487 | *
|
---|
3488 | * @param data Settings struct to be updated.
|
---|
3489 | * @param strHardDiskFolder Folder for which paths should be relative.
|
---|
3490 | *
|
---|
3491 | * @note Locks this object, medium tree and children for reading.
|
---|
3492 | */
|
---|
3493 | HRESULT Medium::saveSettings(settings::Medium &data,
|
---|
3494 | const Utf8Str &strHardDiskFolder)
|
---|
3495 | {
|
---|
3496 | AutoCaller autoCaller(this);
|
---|
3497 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
3498 |
|
---|
3499 | /* we access mParent */
|
---|
3500 | AutoReadLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3501 |
|
---|
3502 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3503 |
|
---|
3504 | data.uuid = m->id;
|
---|
3505 |
|
---|
3506 | // make path relative if needed
|
---|
3507 | if ( !strHardDiskFolder.isEmpty()
|
---|
3508 | && RTPathStartsWith(m->strLocationFull.c_str(), strHardDiskFolder.c_str())
|
---|
3509 | )
|
---|
3510 | data.strLocation = m->strLocationFull.substr(strHardDiskFolder.length() + 1);
|
---|
3511 | else
|
---|
3512 | data.strLocation = m->strLocationFull;
|
---|
3513 | data.strFormat = m->strFormat;
|
---|
3514 |
|
---|
3515 | /* optional, only for diffs, default is false */
|
---|
3516 | if (m->pParent)
|
---|
3517 | data.fAutoReset = m->autoReset;
|
---|
3518 | else
|
---|
3519 | data.fAutoReset = false;
|
---|
3520 |
|
---|
3521 | /* optional */
|
---|
3522 | data.strDescription = m->strDescription;
|
---|
3523 |
|
---|
3524 | /* optional properties */
|
---|
3525 | data.properties.clear();
|
---|
3526 | for (settings::StringsMap::const_iterator it = m->mapProperties.begin();
|
---|
3527 | it != m->mapProperties.end();
|
---|
3528 | ++it)
|
---|
3529 | {
|
---|
3530 | /* only save properties that have non-default values */
|
---|
3531 | if (!it->second.isEmpty())
|
---|
3532 | {
|
---|
3533 | const Utf8Str &name = it->first;
|
---|
3534 | const Utf8Str &value = it->second;
|
---|
3535 | data.properties[name] = value;
|
---|
3536 | }
|
---|
3537 | }
|
---|
3538 |
|
---|
3539 | /* only for base media */
|
---|
3540 | if (m->pParent.isNull())
|
---|
3541 | data.hdType = m->type;
|
---|
3542 |
|
---|
3543 | /* save all children */
|
---|
3544 | for (MediaList::const_iterator it = getChildren().begin();
|
---|
3545 | it != getChildren().end();
|
---|
3546 | ++it)
|
---|
3547 | {
|
---|
3548 | settings::Medium med;
|
---|
3549 | HRESULT rc = (*it)->saveSettings(med, strHardDiskFolder);
|
---|
3550 | AssertComRCReturnRC(rc);
|
---|
3551 | data.llChildren.push_back(med);
|
---|
3552 | }
|
---|
3553 |
|
---|
3554 | return S_OK;
|
---|
3555 | }
|
---|
3556 |
|
---|
3557 | /**
|
---|
3558 | * Constructs a medium lock list for this medium. The lock is not taken.
|
---|
3559 | *
|
---|
3560 | * @note Locks the medium tree for reading.
|
---|
3561 | *
|
---|
3562 | * @param fFailIfInaccessible If true, this fails with an error if a medium is inaccessible. If false,
|
---|
3563 | * inaccessible media are silently skipped and not locked (i.e. their state remains "Inaccessible");
|
---|
3564 | * this is necessary for a VM's removable media VM startup for which we do not want to fail.
|
---|
3565 | * @param fMediumLockWrite Whether to associate a write lock with this medium.
|
---|
3566 | * @param pToBeParent Medium which will become the parent of this medium.
|
---|
3567 | * @param mediumLockList Where to store the resulting list.
|
---|
3568 | */
|
---|
3569 | HRESULT Medium::createMediumLockList(bool fFailIfInaccessible,
|
---|
3570 | bool fMediumLockWrite,
|
---|
3571 | Medium *pToBeParent,
|
---|
3572 | MediumLockList &mediumLockList)
|
---|
3573 | {
|
---|
3574 | AutoCaller autoCaller(this);
|
---|
3575 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
3576 |
|
---|
3577 | HRESULT rc = S_OK;
|
---|
3578 |
|
---|
3579 | /* we access parent medium objects */
|
---|
3580 | AutoReadLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3581 |
|
---|
3582 | /* paranoid sanity checking if the medium has a to-be parent medium */
|
---|
3583 | if (pToBeParent)
|
---|
3584 | {
|
---|
3585 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3586 | ComAssertRet(getParent().isNull(), E_FAIL);
|
---|
3587 | ComAssertRet(getChildren().size() == 0, E_FAIL);
|
---|
3588 | }
|
---|
3589 |
|
---|
3590 | ErrorInfoKeeper eik;
|
---|
3591 | MultiResult mrc(S_OK);
|
---|
3592 |
|
---|
3593 | ComObjPtr<Medium> pMedium = this;
|
---|
3594 | while (!pMedium.isNull())
|
---|
3595 | {
|
---|
3596 | // need write lock for RefreshState if medium is inaccessible
|
---|
3597 | AutoWriteLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
3598 |
|
---|
3599 | /* Accessibility check must be first, otherwise locking interferes
|
---|
3600 | * with getting the medium state. Lock lists are not created for
|
---|
3601 | * fun, and thus getting the medium status is no luxury. */
|
---|
3602 | MediumState_T mediumState = pMedium->getState();
|
---|
3603 | if (mediumState == MediumState_Inaccessible)
|
---|
3604 | {
|
---|
3605 | rc = pMedium->RefreshState(&mediumState);
|
---|
3606 | if (FAILED(rc)) return rc;
|
---|
3607 |
|
---|
3608 | if (mediumState == MediumState_Inaccessible)
|
---|
3609 | {
|
---|
3610 | // ignore inaccessible ISO media and silently return S_OK,
|
---|
3611 | // otherwise VM startup (esp. restore) may fail without good reason
|
---|
3612 | if (!fFailIfInaccessible)
|
---|
3613 | return S_OK;
|
---|
3614 |
|
---|
3615 | // otherwise report an error
|
---|
3616 | Bstr error;
|
---|
3617 | rc = pMedium->COMGETTER(LastAccessError)(error.asOutParam());
|
---|
3618 | if (FAILED(rc)) return rc;
|
---|
3619 |
|
---|
3620 | /* collect multiple errors */
|
---|
3621 | eik.restore();
|
---|
3622 | Assert(!error.isEmpty());
|
---|
3623 | mrc = setError(E_FAIL,
|
---|
3624 | "%ls",
|
---|
3625 | error.raw());
|
---|
3626 | // error message will be something like
|
---|
3627 | // "Could not open the medium ... VD: error VERR_FILE_NOT_FOUND opening image file ... (VERR_FILE_NOT_FOUND).
|
---|
3628 | eik.fetch();
|
---|
3629 | }
|
---|
3630 | }
|
---|
3631 |
|
---|
3632 | if (pMedium == this)
|
---|
3633 | mediumLockList.Prepend(pMedium, fMediumLockWrite);
|
---|
3634 | else
|
---|
3635 | mediumLockList.Prepend(pMedium, false);
|
---|
3636 |
|
---|
3637 | pMedium = pMedium->getParent();
|
---|
3638 | if (pMedium.isNull() && pToBeParent)
|
---|
3639 | {
|
---|
3640 | pMedium = pToBeParent;
|
---|
3641 | pToBeParent = NULL;
|
---|
3642 | }
|
---|
3643 | }
|
---|
3644 |
|
---|
3645 | return mrc;
|
---|
3646 | }
|
---|
3647 |
|
---|
3648 | /**
|
---|
3649 | * Creates a new differencing storage unit using the format of the given target
|
---|
3650 | * medium and the location. Note that @c aTarget must be NotCreated.
|
---|
3651 | *
|
---|
3652 | * The @a aMediumLockList parameter contains the associated medium lock list,
|
---|
3653 | * which must be in locked state. If @a aWait is @c true then the caller is
|
---|
3654 | * responsible for unlocking.
|
---|
3655 | *
|
---|
3656 | * If @a aProgress is not NULL but the object it points to is @c null then a
|
---|
3657 | * new progress object will be created and assigned to @a *aProgress on
|
---|
3658 | * success, otherwise the existing progress object is used. If @a aProgress is
|
---|
3659 | * NULL, then no progress object is created/used at all.
|
---|
3660 | *
|
---|
3661 | * When @a aWait is @c false, this method will create a thread to perform the
|
---|
3662 | * create operation asynchronously and will return immediately. Otherwise, it
|
---|
3663 | * will perform the operation on the calling thread and will not return to the
|
---|
3664 | * caller until the operation is completed. Note that @a aProgress cannot be
|
---|
3665 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
3666 | *
|
---|
3667 | * @param aTarget Target medium.
|
---|
3668 | * @param aVariant Precise medium variant to create.
|
---|
3669 | * @param aMediumLockList List of media which should be locked.
|
---|
3670 | * @param aProgress Where to find/store a Progress object to track
|
---|
3671 | * operation completion.
|
---|
3672 | * @param aWait @c true if this method should block instead of
|
---|
3673 | * creating an asynchronous thread.
|
---|
3674 | * @param pllRegistriesThatNeedSaving Optional pointer to a list of UUIDs that will receive the registry IDs that need saving.
|
---|
3675 | * This only works in "wait" mode; otherwise saveSettings is called automatically by the thread that
|
---|
3676 | * was created, and this parameter is ignored.
|
---|
3677 | *
|
---|
3678 | * @note Locks this object and @a aTarget for writing.
|
---|
3679 | */
|
---|
3680 | HRESULT Medium::createDiffStorage(ComObjPtr<Medium> &aTarget,
|
---|
3681 | MediumVariant_T aVariant,
|
---|
3682 | MediumLockList *aMediumLockList,
|
---|
3683 | ComObjPtr<Progress> *aProgress,
|
---|
3684 | bool aWait,
|
---|
3685 | GuidList *pllRegistriesThatNeedSaving)
|
---|
3686 | {
|
---|
3687 | AssertReturn(!aTarget.isNull(), E_FAIL);
|
---|
3688 | AssertReturn(aMediumLockList, E_FAIL);
|
---|
3689 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
3690 |
|
---|
3691 | AutoCaller autoCaller(this);
|
---|
3692 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
3693 |
|
---|
3694 | AutoCaller targetCaller(aTarget);
|
---|
3695 | if (FAILED(targetCaller.rc())) return targetCaller.rc();
|
---|
3696 |
|
---|
3697 | HRESULT rc = S_OK;
|
---|
3698 | ComObjPtr<Progress> pProgress;
|
---|
3699 | Medium::Task *pTask = NULL;
|
---|
3700 |
|
---|
3701 | try
|
---|
3702 | {
|
---|
3703 | AutoMultiWriteLock2 alock(this, aTarget COMMA_LOCKVAL_SRC_POS);
|
---|
3704 |
|
---|
3705 | ComAssertThrow( m->type != MediumType_Writethrough
|
---|
3706 | && m->type != MediumType_Shareable
|
---|
3707 | && m->type != MediumType_Readonly, E_FAIL);
|
---|
3708 | ComAssertThrow(m->state == MediumState_LockedRead, E_FAIL);
|
---|
3709 |
|
---|
3710 | if (aTarget->m->state != MediumState_NotCreated)
|
---|
3711 | throw aTarget->setStateError();
|
---|
3712 |
|
---|
3713 | /* Check that the medium is not attached to the current state of
|
---|
3714 | * any VM referring to it. */
|
---|
3715 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
3716 | it != m->backRefs.end();
|
---|
3717 | ++it)
|
---|
3718 | {
|
---|
3719 | if (it->fInCurState)
|
---|
3720 | {
|
---|
3721 | /* Note: when a VM snapshot is being taken, all normal media
|
---|
3722 | * attached to the VM in the current state will be, as an
|
---|
3723 | * exception, also associated with the snapshot which is about
|
---|
3724 | * to create (see SnapshotMachine::init()) before deassociating
|
---|
3725 | * them from the current state (which takes place only on
|
---|
3726 | * success in Machine::fixupHardDisks()), so that the size of
|
---|
3727 | * snapshotIds will be 1 in this case. The extra condition is
|
---|
3728 | * used to filter out this legal situation. */
|
---|
3729 | if (it->llSnapshotIds.size() == 0)
|
---|
3730 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3731 | 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"),
|
---|
3732 | m->strLocationFull.c_str(), it->machineId.raw());
|
---|
3733 |
|
---|
3734 | Assert(it->llSnapshotIds.size() == 1);
|
---|
3735 | }
|
---|
3736 | }
|
---|
3737 |
|
---|
3738 | if (aProgress != NULL)
|
---|
3739 | {
|
---|
3740 | /* use the existing progress object... */
|
---|
3741 | pProgress = *aProgress;
|
---|
3742 |
|
---|
3743 | /* ...but create a new one if it is null */
|
---|
3744 | if (pProgress.isNull())
|
---|
3745 | {
|
---|
3746 | pProgress.createObject();
|
---|
3747 | rc = pProgress->init(m->pVirtualBox,
|
---|
3748 | static_cast<IMedium*>(this),
|
---|
3749 | BstrFmt(tr("Creating differencing medium storage unit '%s'"), aTarget->m->strLocationFull.c_str()).raw(),
|
---|
3750 | TRUE /* aCancelable */);
|
---|
3751 | if (FAILED(rc))
|
---|
3752 | throw rc;
|
---|
3753 | }
|
---|
3754 | }
|
---|
3755 |
|
---|
3756 | /* setup task object to carry out the operation sync/async */
|
---|
3757 | pTask = new Medium::CreateDiffTask(this, pProgress, aTarget, aVariant,
|
---|
3758 | aMediumLockList,
|
---|
3759 | aWait /* fKeepMediumLockList */);
|
---|
3760 | rc = pTask->rc();
|
---|
3761 | AssertComRC(rc);
|
---|
3762 | if (FAILED(rc))
|
---|
3763 | throw rc;
|
---|
3764 |
|
---|
3765 | /* register a task (it will deregister itself when done) */
|
---|
3766 | ++m->numCreateDiffTasks;
|
---|
3767 | Assert(m->numCreateDiffTasks != 0); /* overflow? */
|
---|
3768 |
|
---|
3769 | aTarget->m->state = MediumState_Creating;
|
---|
3770 | }
|
---|
3771 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3772 |
|
---|
3773 | if (SUCCEEDED(rc))
|
---|
3774 | {
|
---|
3775 | if (aWait)
|
---|
3776 | rc = runNow(pTask, pllRegistriesThatNeedSaving);
|
---|
3777 | else
|
---|
3778 | rc = startThread(pTask);
|
---|
3779 |
|
---|
3780 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
3781 | *aProgress = pProgress;
|
---|
3782 | }
|
---|
3783 | else if (pTask != NULL)
|
---|
3784 | delete pTask;
|
---|
3785 |
|
---|
3786 | return rc;
|
---|
3787 | }
|
---|
3788 |
|
---|
3789 | /**
|
---|
3790 | * Returns a preferred format for differencing media.
|
---|
3791 | */
|
---|
3792 | Utf8Str Medium::getPreferredDiffFormat()
|
---|
3793 | {
|
---|
3794 | AutoCaller autoCaller(this);
|
---|
3795 | AssertComRCReturn(autoCaller.rc(), Utf8Str::Empty);
|
---|
3796 |
|
---|
3797 | /* check that our own format supports diffs */
|
---|
3798 | if (!(m->formatObj->getCapabilities() & MediumFormatCapabilities_Differencing))
|
---|
3799 | {
|
---|
3800 | /* use the default format if not */
|
---|
3801 | Utf8Str tmp;
|
---|
3802 | m->pVirtualBox->getDefaultHardDiskFormat(tmp);
|
---|
3803 | return tmp;
|
---|
3804 | }
|
---|
3805 |
|
---|
3806 | /* m->strFormat is const, no need to lock */
|
---|
3807 | return m->strFormat;
|
---|
3808 | }
|
---|
3809 |
|
---|
3810 | /**
|
---|
3811 | * Implementation for the public Medium::Close() with the exception of calling
|
---|
3812 | * VirtualBox::saveSettings(), in case someone wants to call this for several
|
---|
3813 | * media.
|
---|
3814 | *
|
---|
3815 | * After this returns with success, uninit() has been called on the medium, and
|
---|
3816 | * the object is no longer usable ("not ready" state).
|
---|
3817 | *
|
---|
3818 | * @param pllRegistriesThatNeedSaving Optional pointer to a list of UUIDs that will receive the registry IDs that need saving.
|
---|
3819 | * @param autoCaller AutoCaller instance which must have been created on the caller's stack for this medium. This gets released here
|
---|
3820 | * upon which the Medium instance gets uninitialized.
|
---|
3821 | * @return
|
---|
3822 | */
|
---|
3823 | HRESULT Medium::close(GuidList *pllRegistriesThatNeedSaving,
|
---|
3824 | AutoCaller &autoCaller)
|
---|
3825 | {
|
---|
3826 | // we're accessing parent/child and backrefs, so lock the tree first, then ourselves
|
---|
3827 | AutoMultiWriteLock2 multilock(&m->pVirtualBox->getMediaTreeLockHandle(),
|
---|
3828 | this->lockHandle()
|
---|
3829 | COMMA_LOCKVAL_SRC_POS);
|
---|
3830 |
|
---|
3831 | LogFlowFunc(("ENTER for %s\n", getLocationFull().c_str()));
|
---|
3832 |
|
---|
3833 | bool wasCreated = true;
|
---|
3834 |
|
---|
3835 | switch (m->state)
|
---|
3836 | {
|
---|
3837 | case MediumState_NotCreated:
|
---|
3838 | wasCreated = false;
|
---|
3839 | break;
|
---|
3840 | case MediumState_Created:
|
---|
3841 | case MediumState_Inaccessible:
|
---|
3842 | break;
|
---|
3843 | default:
|
---|
3844 | return setStateError();
|
---|
3845 | }
|
---|
3846 |
|
---|
3847 | if (m->backRefs.size() != 0)
|
---|
3848 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
3849 | tr("Medium '%s' cannot be closed because it is still attached to %d virtual machines"),
|
---|
3850 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
3851 |
|
---|
3852 | // perform extra media-dependent close checks
|
---|
3853 | HRESULT rc = canClose();
|
---|
3854 | if (FAILED(rc)) return rc;
|
---|
3855 |
|
---|
3856 | if (wasCreated)
|
---|
3857 | {
|
---|
3858 | // remove from the list of known media before performing actual
|
---|
3859 | // uninitialization (to keep the media registry consistent on
|
---|
3860 | // failure to do so)
|
---|
3861 | rc = unregisterWithVirtualBox(pllRegistriesThatNeedSaving);
|
---|
3862 | if (FAILED(rc)) return rc;
|
---|
3863 | }
|
---|
3864 |
|
---|
3865 | // leave the AutoCaller, as otherwise uninit() will simply hang
|
---|
3866 | autoCaller.release();
|
---|
3867 |
|
---|
3868 | // Keep the locks held until after uninit, as otherwise the consistency
|
---|
3869 | // of the medium tree cannot be guaranteed.
|
---|
3870 | uninit();
|
---|
3871 |
|
---|
3872 | LogFlowFuncLeave();
|
---|
3873 |
|
---|
3874 | return rc;
|
---|
3875 | }
|
---|
3876 |
|
---|
3877 | /**
|
---|
3878 | * Deletes the medium storage unit.
|
---|
3879 | *
|
---|
3880 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
3881 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
3882 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
3883 | * progress object is created/used at all.
|
---|
3884 | *
|
---|
3885 | * When @a aWait is @c false, this method will create a thread to perform the
|
---|
3886 | * delete operation asynchronously and will return immediately. Otherwise, it
|
---|
3887 | * will perform the operation on the calling thread and will not return to the
|
---|
3888 | * caller until the operation is completed. Note that @a aProgress cannot be
|
---|
3889 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
3890 | *
|
---|
3891 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
3892 | * completion.
|
---|
3893 | * @param aWait @c true if this method should block instead of creating
|
---|
3894 | * an asynchronous thread.
|
---|
3895 | * @param pfNeedsGlobalSaveSettings Optional pointer to a bool that must have been initialized to false and that will be set to true
|
---|
3896 | * by this function if the caller should invoke VirtualBox::saveSettings() because the global settings have changed.
|
---|
3897 | * This only works in "wait" mode; otherwise saveSettings gets called automatically by the thread that was created,
|
---|
3898 | * and this parameter is ignored.
|
---|
3899 | *
|
---|
3900 | * @note Locks mVirtualBox and this object for writing. Locks medium tree for
|
---|
3901 | * writing.
|
---|
3902 | */
|
---|
3903 | HRESULT Medium::deleteStorage(ComObjPtr<Progress> *aProgress,
|
---|
3904 | bool aWait,
|
---|
3905 | GuidList *pllRegistriesThatNeedSaving)
|
---|
3906 | {
|
---|
3907 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
3908 |
|
---|
3909 | AutoCaller autoCaller(this);
|
---|
3910 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
3911 |
|
---|
3912 | HRESULT rc = S_OK;
|
---|
3913 | ComObjPtr<Progress> pProgress;
|
---|
3914 | Medium::Task *pTask = NULL;
|
---|
3915 |
|
---|
3916 | try
|
---|
3917 | {
|
---|
3918 | /* we're accessing the media tree, and canClose() needs it too */
|
---|
3919 | AutoMultiWriteLock2 multilock(&m->pVirtualBox->getMediaTreeLockHandle(),
|
---|
3920 | this->lockHandle()
|
---|
3921 | COMMA_LOCKVAL_SRC_POS);
|
---|
3922 | LogFlowThisFunc(("aWait=%RTbool locationFull=%s\n", aWait, getLocationFull().c_str() ));
|
---|
3923 |
|
---|
3924 | if ( !(m->formatObj->getCapabilities() & ( MediumFormatCapabilities_CreateDynamic
|
---|
3925 | | MediumFormatCapabilities_CreateFixed)))
|
---|
3926 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3927 | tr("Medium format '%s' does not support storage deletion"),
|
---|
3928 | m->strFormat.c_str());
|
---|
3929 |
|
---|
3930 | /* Note that we are fine with Inaccessible state too: a) for symmetry
|
---|
3931 | * with create calls and b) because it doesn't really harm to try, if
|
---|
3932 | * it is really inaccessible, the delete operation will fail anyway.
|
---|
3933 | * Accepting Inaccessible state is especially important because all
|
---|
3934 | * registered media are initially Inaccessible upon VBoxSVC startup
|
---|
3935 | * until COMGETTER(RefreshState) is called. Accept Deleting state
|
---|
3936 | * because some callers need to put the medium in this state early
|
---|
3937 | * to prevent races. */
|
---|
3938 | switch (m->state)
|
---|
3939 | {
|
---|
3940 | case MediumState_Created:
|
---|
3941 | case MediumState_Deleting:
|
---|
3942 | case MediumState_Inaccessible:
|
---|
3943 | break;
|
---|
3944 | default:
|
---|
3945 | throw setStateError();
|
---|
3946 | }
|
---|
3947 |
|
---|
3948 | if (m->backRefs.size() != 0)
|
---|
3949 | {
|
---|
3950 | Utf8Str strMachines;
|
---|
3951 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
3952 | it != m->backRefs.end();
|
---|
3953 | ++it)
|
---|
3954 | {
|
---|
3955 | const BackRef &b = *it;
|
---|
3956 | if (strMachines.length())
|
---|
3957 | strMachines.append(", ");
|
---|
3958 | strMachines.append(b.machineId.toString().c_str());
|
---|
3959 | }
|
---|
3960 | #ifdef DEBUG
|
---|
3961 | dumpBackRefs();
|
---|
3962 | #endif
|
---|
3963 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
3964 | tr("Cannot delete storage: medium '%s' is still attached to the following %d virtual machine(s): %s"),
|
---|
3965 | m->strLocationFull.c_str(),
|
---|
3966 | m->backRefs.size(),
|
---|
3967 | strMachines.c_str());
|
---|
3968 | }
|
---|
3969 |
|
---|
3970 | rc = canClose();
|
---|
3971 | if (FAILED(rc))
|
---|
3972 | throw rc;
|
---|
3973 |
|
---|
3974 | /* go to Deleting state, so that the medium is not actually locked */
|
---|
3975 | if (m->state != MediumState_Deleting)
|
---|
3976 | {
|
---|
3977 | rc = markForDeletion();
|
---|
3978 | if (FAILED(rc))
|
---|
3979 | throw rc;
|
---|
3980 | }
|
---|
3981 |
|
---|
3982 | /* Build the medium lock list. */
|
---|
3983 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3984 | rc = createMediumLockList(true /* fFailIfInaccessible */,
|
---|
3985 | true /* fMediumLockWrite */,
|
---|
3986 | NULL,
|
---|
3987 | *pMediumLockList);
|
---|
3988 | if (FAILED(rc))
|
---|
3989 | {
|
---|
3990 | delete pMediumLockList;
|
---|
3991 | throw rc;
|
---|
3992 | }
|
---|
3993 |
|
---|
3994 | rc = pMediumLockList->Lock();
|
---|
3995 | if (FAILED(rc))
|
---|
3996 | {
|
---|
3997 | delete pMediumLockList;
|
---|
3998 | throw setError(rc,
|
---|
3999 | tr("Failed to lock media when deleting '%s'"),
|
---|
4000 | getLocationFull().c_str());
|
---|
4001 | }
|
---|
4002 |
|
---|
4003 | /* try to remove from the list of known media before performing
|
---|
4004 | * actual deletion (we favor the consistency of the media registry
|
---|
4005 | * which would have been broken if unregisterWithVirtualBox() failed
|
---|
4006 | * after we successfully deleted the storage) */
|
---|
4007 | rc = unregisterWithVirtualBox(pllRegistriesThatNeedSaving);
|
---|
4008 | if (FAILED(rc))
|
---|
4009 | throw rc;
|
---|
4010 | // no longer need lock
|
---|
4011 | multilock.release();
|
---|
4012 |
|
---|
4013 | if (aProgress != NULL)
|
---|
4014 | {
|
---|
4015 | /* use the existing progress object... */
|
---|
4016 | pProgress = *aProgress;
|
---|
4017 |
|
---|
4018 | /* ...but create a new one if it is null */
|
---|
4019 | if (pProgress.isNull())
|
---|
4020 | {
|
---|
4021 | pProgress.createObject();
|
---|
4022 | rc = pProgress->init(m->pVirtualBox,
|
---|
4023 | static_cast<IMedium*>(this),
|
---|
4024 | BstrFmt(tr("Deleting medium storage unit '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
4025 | FALSE /* aCancelable */);
|
---|
4026 | if (FAILED(rc))
|
---|
4027 | throw rc;
|
---|
4028 | }
|
---|
4029 | }
|
---|
4030 |
|
---|
4031 | /* setup task object to carry out the operation sync/async */
|
---|
4032 | pTask = new Medium::DeleteTask(this, pProgress, pMediumLockList);
|
---|
4033 | rc = pTask->rc();
|
---|
4034 | AssertComRC(rc);
|
---|
4035 | if (FAILED(rc))
|
---|
4036 | throw rc;
|
---|
4037 | }
|
---|
4038 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4039 |
|
---|
4040 | if (SUCCEEDED(rc))
|
---|
4041 | {
|
---|
4042 | if (aWait)
|
---|
4043 | rc = runNow(pTask, NULL /* pfNeedsGlobalSaveSettings*/);
|
---|
4044 | else
|
---|
4045 | rc = startThread(pTask);
|
---|
4046 |
|
---|
4047 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
4048 | *aProgress = pProgress;
|
---|
4049 |
|
---|
4050 | }
|
---|
4051 | else
|
---|
4052 | {
|
---|
4053 | if (pTask)
|
---|
4054 | delete pTask;
|
---|
4055 |
|
---|
4056 | /* Undo deleting state if necessary. */
|
---|
4057 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4058 | unmarkForDeletion();
|
---|
4059 | }
|
---|
4060 |
|
---|
4061 | return rc;
|
---|
4062 | }
|
---|
4063 |
|
---|
4064 | /**
|
---|
4065 | * Mark a medium for deletion.
|
---|
4066 | *
|
---|
4067 | * @note Caller must hold the write lock on this medium!
|
---|
4068 | */
|
---|
4069 | HRESULT Medium::markForDeletion()
|
---|
4070 | {
|
---|
4071 | ComAssertRet(this->lockHandle()->isWriteLockOnCurrentThread(), E_FAIL);
|
---|
4072 | switch (m->state)
|
---|
4073 | {
|
---|
4074 | case MediumState_Created:
|
---|
4075 | case MediumState_Inaccessible:
|
---|
4076 | m->preLockState = m->state;
|
---|
4077 | m->state = MediumState_Deleting;
|
---|
4078 | return S_OK;
|
---|
4079 | default:
|
---|
4080 | return setStateError();
|
---|
4081 | }
|
---|
4082 | }
|
---|
4083 |
|
---|
4084 | /**
|
---|
4085 | * Removes the "mark for deletion".
|
---|
4086 | *
|
---|
4087 | * @note Caller must hold the write lock on this medium!
|
---|
4088 | */
|
---|
4089 | HRESULT Medium::unmarkForDeletion()
|
---|
4090 | {
|
---|
4091 | ComAssertRet(this->lockHandle()->isWriteLockOnCurrentThread(), E_FAIL);
|
---|
4092 | switch (m->state)
|
---|
4093 | {
|
---|
4094 | case MediumState_Deleting:
|
---|
4095 | m->state = m->preLockState;
|
---|
4096 | return S_OK;
|
---|
4097 | default:
|
---|
4098 | return setStateError();
|
---|
4099 | }
|
---|
4100 | }
|
---|
4101 |
|
---|
4102 | /**
|
---|
4103 | * Mark a medium for deletion which is in locked state.
|
---|
4104 | *
|
---|
4105 | * @note Caller must hold the write lock on this medium!
|
---|
4106 | */
|
---|
4107 | HRESULT Medium::markLockedForDeletion()
|
---|
4108 | {
|
---|
4109 | ComAssertRet(this->lockHandle()->isWriteLockOnCurrentThread(), E_FAIL);
|
---|
4110 | if ( ( m->state == MediumState_LockedRead
|
---|
4111 | || m->state == MediumState_LockedWrite)
|
---|
4112 | && m->preLockState == MediumState_Created)
|
---|
4113 | {
|
---|
4114 | m->preLockState = MediumState_Deleting;
|
---|
4115 | return S_OK;
|
---|
4116 | }
|
---|
4117 | else
|
---|
4118 | return setStateError();
|
---|
4119 | }
|
---|
4120 |
|
---|
4121 | /**
|
---|
4122 | * Removes the "mark for deletion" for a medium in locked state.
|
---|
4123 | *
|
---|
4124 | * @note Caller must hold the write lock on this medium!
|
---|
4125 | */
|
---|
4126 | HRESULT Medium::unmarkLockedForDeletion()
|
---|
4127 | {
|
---|
4128 | ComAssertRet(this->lockHandle()->isWriteLockOnCurrentThread(), E_FAIL);
|
---|
4129 | if ( ( m->state == MediumState_LockedRead
|
---|
4130 | || m->state == MediumState_LockedWrite)
|
---|
4131 | && m->preLockState == MediumState_Deleting)
|
---|
4132 | {
|
---|
4133 | m->preLockState = MediumState_Created;
|
---|
4134 | return S_OK;
|
---|
4135 | }
|
---|
4136 | else
|
---|
4137 | return setStateError();
|
---|
4138 | }
|
---|
4139 |
|
---|
4140 | /**
|
---|
4141 | * Prepares this (source) medium, target medium and all intermediate media
|
---|
4142 | * for the merge operation.
|
---|
4143 | *
|
---|
4144 | * This method is to be called prior to calling the #mergeTo() to perform
|
---|
4145 | * necessary consistency checks and place involved media to appropriate
|
---|
4146 | * states. If #mergeTo() is not called or fails, the state modifications
|
---|
4147 | * performed by this method must be undone by #cancelMergeTo().
|
---|
4148 | *
|
---|
4149 | * See #mergeTo() for more information about merging.
|
---|
4150 | *
|
---|
4151 | * @param pTarget Target medium.
|
---|
4152 | * @param aMachineId Allowed machine attachment. NULL means do not check.
|
---|
4153 | * @param aSnapshotId Allowed snapshot attachment. NULL or empty UUID means
|
---|
4154 | * do not check.
|
---|
4155 | * @param fLockMedia Flag whether to lock the medium lock list or not.
|
---|
4156 | * If set to false and the medium lock list locking fails
|
---|
4157 | * later you must call #cancelMergeTo().
|
---|
4158 | * @param fMergeForward Resulting merge direction (out).
|
---|
4159 | * @param pParentForTarget New parent for target medium after merge (out).
|
---|
4160 | * @param aChildrenToReparent List of children of the source which will have
|
---|
4161 | * to be reparented to the target after merge (out).
|
---|
4162 | * @param aMediumLockList Medium locking information (out).
|
---|
4163 | *
|
---|
4164 | * @note Locks medium tree for reading. Locks this object, aTarget and all
|
---|
4165 | * intermediate media for writing.
|
---|
4166 | */
|
---|
4167 | HRESULT Medium::prepareMergeTo(const ComObjPtr<Medium> &pTarget,
|
---|
4168 | const Guid *aMachineId,
|
---|
4169 | const Guid *aSnapshotId,
|
---|
4170 | bool fLockMedia,
|
---|
4171 | bool &fMergeForward,
|
---|
4172 | ComObjPtr<Medium> &pParentForTarget,
|
---|
4173 | MediaList &aChildrenToReparent,
|
---|
4174 | MediumLockList * &aMediumLockList)
|
---|
4175 | {
|
---|
4176 | AssertReturn(pTarget != NULL, E_FAIL);
|
---|
4177 | AssertReturn(pTarget != this, E_FAIL);
|
---|
4178 |
|
---|
4179 | AutoCaller autoCaller(this);
|
---|
4180 | AssertComRCReturnRC(autoCaller.rc());
|
---|
4181 |
|
---|
4182 | AutoCaller targetCaller(pTarget);
|
---|
4183 | AssertComRCReturnRC(targetCaller.rc());
|
---|
4184 |
|
---|
4185 | HRESULT rc = S_OK;
|
---|
4186 | fMergeForward = false;
|
---|
4187 | pParentForTarget.setNull();
|
---|
4188 | aChildrenToReparent.clear();
|
---|
4189 | Assert(aMediumLockList == NULL);
|
---|
4190 | aMediumLockList = NULL;
|
---|
4191 |
|
---|
4192 | try
|
---|
4193 | {
|
---|
4194 | // locking: we need the tree lock first because we access parent pointers
|
---|
4195 | AutoReadLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4196 |
|
---|
4197 | /* more sanity checking and figuring out the merge direction */
|
---|
4198 | ComObjPtr<Medium> pMedium = getParent();
|
---|
4199 | while (!pMedium.isNull() && pMedium != pTarget)
|
---|
4200 | pMedium = pMedium->getParent();
|
---|
4201 | if (pMedium == pTarget)
|
---|
4202 | fMergeForward = false;
|
---|
4203 | else
|
---|
4204 | {
|
---|
4205 | pMedium = pTarget->getParent();
|
---|
4206 | while (!pMedium.isNull() && pMedium != this)
|
---|
4207 | pMedium = pMedium->getParent();
|
---|
4208 | if (pMedium == this)
|
---|
4209 | fMergeForward = true;
|
---|
4210 | else
|
---|
4211 | {
|
---|
4212 | Utf8Str tgtLoc;
|
---|
4213 | {
|
---|
4214 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
4215 | tgtLoc = pTarget->getLocationFull();
|
---|
4216 | }
|
---|
4217 |
|
---|
4218 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4219 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4220 | tr("Media '%s' and '%s' are unrelated"),
|
---|
4221 | m->strLocationFull.c_str(), tgtLoc.c_str());
|
---|
4222 | }
|
---|
4223 | }
|
---|
4224 |
|
---|
4225 | /* Build the lock list. */
|
---|
4226 | aMediumLockList = new MediumLockList();
|
---|
4227 | if (fMergeForward)
|
---|
4228 | rc = pTarget->createMediumLockList(true /* fFailIfInaccessible */,
|
---|
4229 | true /* fMediumLockWrite */,
|
---|
4230 | NULL,
|
---|
4231 | *aMediumLockList);
|
---|
4232 | else
|
---|
4233 | rc = createMediumLockList(true /* fFailIfInaccessible */,
|
---|
4234 | false /* fMediumLockWrite */,
|
---|
4235 | NULL,
|
---|
4236 | *aMediumLockList);
|
---|
4237 | if (FAILED(rc))
|
---|
4238 | throw rc;
|
---|
4239 |
|
---|
4240 | /* Sanity checking, must be after lock list creation as it depends on
|
---|
4241 | * valid medium states. The medium objects must be accessible. Only
|
---|
4242 | * do this if immediate locking is requested, otherwise it fails when
|
---|
4243 | * we construct a medium lock list for an already running VM. Snapshot
|
---|
4244 | * deletion uses this to simplify its life. */
|
---|
4245 | if (fLockMedia)
|
---|
4246 | {
|
---|
4247 | {
|
---|
4248 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4249 | if (m->state != MediumState_Created)
|
---|
4250 | throw setStateError();
|
---|
4251 | }
|
---|
4252 | {
|
---|
4253 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
4254 | if (pTarget->m->state != MediumState_Created)
|
---|
4255 | throw pTarget->setStateError();
|
---|
4256 | }
|
---|
4257 | }
|
---|
4258 |
|
---|
4259 | /* check medium attachment and other sanity conditions */
|
---|
4260 | if (fMergeForward)
|
---|
4261 | {
|
---|
4262 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4263 | if (getChildren().size() > 1)
|
---|
4264 | {
|
---|
4265 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4266 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
4267 | m->strLocationFull.c_str(), getChildren().size());
|
---|
4268 | }
|
---|
4269 | /* One backreference is only allowed if the machine ID is not empty
|
---|
4270 | * and it matches the machine the medium is attached to (including
|
---|
4271 | * the snapshot ID if not empty). */
|
---|
4272 | if ( m->backRefs.size() != 0
|
---|
4273 | && ( !aMachineId
|
---|
4274 | || m->backRefs.size() != 1
|
---|
4275 | || aMachineId->isEmpty()
|
---|
4276 | || *getFirstMachineBackrefId() != *aMachineId
|
---|
4277 | || ( (!aSnapshotId || !aSnapshotId->isEmpty())
|
---|
4278 | && *getFirstMachineBackrefSnapshotId() != *aSnapshotId)))
|
---|
4279 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
4280 | tr("Medium '%s' is attached to %d virtual machines"),
|
---|
4281 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
4282 | if (m->type == MediumType_Immutable)
|
---|
4283 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4284 | tr("Medium '%s' is immutable"),
|
---|
4285 | m->strLocationFull.c_str());
|
---|
4286 | if (m->type == MediumType_MultiAttach)
|
---|
4287 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4288 | tr("Medium '%s' is multi-attach"),
|
---|
4289 | m->strLocationFull.c_str());
|
---|
4290 | }
|
---|
4291 | else
|
---|
4292 | {
|
---|
4293 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
4294 | if (pTarget->getChildren().size() > 1)
|
---|
4295 | {
|
---|
4296 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
4297 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
4298 | pTarget->m->strLocationFull.c_str(),
|
---|
4299 | pTarget->getChildren().size());
|
---|
4300 | }
|
---|
4301 | if (pTarget->m->type == MediumType_Immutable)
|
---|
4302 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4303 | tr("Medium '%s' is immutable"),
|
---|
4304 | pTarget->m->strLocationFull.c_str());
|
---|
4305 | if (pTarget->m->type == MediumType_MultiAttach)
|
---|
4306 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4307 | tr("Medium '%s' is multi-attach"),
|
---|
4308 | pTarget->m->strLocationFull.c_str());
|
---|
4309 | }
|
---|
4310 | ComObjPtr<Medium> pLast(fMergeForward ? (Medium *)pTarget : this);
|
---|
4311 | ComObjPtr<Medium> pLastIntermediate = pLast->getParent();
|
---|
4312 | for (pLast = pLastIntermediate;
|
---|
4313 | !pLast.isNull() && pLast != pTarget && pLast != this;
|
---|
4314 | pLast = pLast->getParent())
|
---|
4315 | {
|
---|
4316 | AutoReadLock alock(pLast COMMA_LOCKVAL_SRC_POS);
|
---|
4317 | if (pLast->getChildren().size() > 1)
|
---|
4318 | {
|
---|
4319 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
4320 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
4321 | pLast->m->strLocationFull.c_str(),
|
---|
4322 | pLast->getChildren().size());
|
---|
4323 | }
|
---|
4324 | if (pLast->m->backRefs.size() != 0)
|
---|
4325 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
4326 | tr("Medium '%s' is attached to %d virtual machines"),
|
---|
4327 | pLast->m->strLocationFull.c_str(),
|
---|
4328 | pLast->m->backRefs.size());
|
---|
4329 |
|
---|
4330 | }
|
---|
4331 |
|
---|
4332 | /* Update medium states appropriately */
|
---|
4333 | if (m->state == MediumState_Created)
|
---|
4334 | {
|
---|
4335 | rc = markForDeletion();
|
---|
4336 | if (FAILED(rc))
|
---|
4337 | throw rc;
|
---|
4338 | }
|
---|
4339 | else
|
---|
4340 | {
|
---|
4341 | if (fLockMedia)
|
---|
4342 | throw setStateError();
|
---|
4343 | else if ( m->state == MediumState_LockedWrite
|
---|
4344 | || m->state == MediumState_LockedRead)
|
---|
4345 | {
|
---|
4346 | /* Either mark it for deletion in locked state or allow
|
---|
4347 | * others to have done so. */
|
---|
4348 | if (m->preLockState == MediumState_Created)
|
---|
4349 | markLockedForDeletion();
|
---|
4350 | else if (m->preLockState != MediumState_Deleting)
|
---|
4351 | throw setStateError();
|
---|
4352 | }
|
---|
4353 | else
|
---|
4354 | throw setStateError();
|
---|
4355 | }
|
---|
4356 |
|
---|
4357 | if (fMergeForward)
|
---|
4358 | {
|
---|
4359 | /* we will need parent to reparent target */
|
---|
4360 | pParentForTarget = m->pParent;
|
---|
4361 | }
|
---|
4362 | else
|
---|
4363 | {
|
---|
4364 | /* we will need to reparent children of the source */
|
---|
4365 | for (MediaList::const_iterator it = getChildren().begin();
|
---|
4366 | it != getChildren().end();
|
---|
4367 | ++it)
|
---|
4368 | {
|
---|
4369 | pMedium = *it;
|
---|
4370 | if (fLockMedia)
|
---|
4371 | {
|
---|
4372 | rc = pMedium->LockWrite(NULL);
|
---|
4373 | if (FAILED(rc))
|
---|
4374 | throw rc;
|
---|
4375 | }
|
---|
4376 |
|
---|
4377 | aChildrenToReparent.push_back(pMedium);
|
---|
4378 | }
|
---|
4379 | }
|
---|
4380 | for (pLast = pLastIntermediate;
|
---|
4381 | !pLast.isNull() && pLast != pTarget && pLast != this;
|
---|
4382 | pLast = pLast->getParent())
|
---|
4383 | {
|
---|
4384 | AutoWriteLock alock(pLast COMMA_LOCKVAL_SRC_POS);
|
---|
4385 | if (pLast->m->state == MediumState_Created)
|
---|
4386 | {
|
---|
4387 | rc = pLast->markForDeletion();
|
---|
4388 | if (FAILED(rc))
|
---|
4389 | throw rc;
|
---|
4390 | }
|
---|
4391 | else
|
---|
4392 | throw pLast->setStateError();
|
---|
4393 | }
|
---|
4394 |
|
---|
4395 | /* Tweak the lock list in the backward merge case, as the target
|
---|
4396 | * isn't marked to be locked for writing yet. */
|
---|
4397 | if (!fMergeForward)
|
---|
4398 | {
|
---|
4399 | MediumLockList::Base::iterator lockListBegin =
|
---|
4400 | aMediumLockList->GetBegin();
|
---|
4401 | MediumLockList::Base::iterator lockListEnd =
|
---|
4402 | aMediumLockList->GetEnd();
|
---|
4403 | lockListEnd--;
|
---|
4404 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
4405 | it != lockListEnd;
|
---|
4406 | ++it)
|
---|
4407 | {
|
---|
4408 | MediumLock &mediumLock = *it;
|
---|
4409 | if (mediumLock.GetMedium() == pTarget)
|
---|
4410 | {
|
---|
4411 | HRESULT rc2 = mediumLock.UpdateLock(true);
|
---|
4412 | AssertComRC(rc2);
|
---|
4413 | break;
|
---|
4414 | }
|
---|
4415 | }
|
---|
4416 | }
|
---|
4417 |
|
---|
4418 | if (fLockMedia)
|
---|
4419 | {
|
---|
4420 | rc = aMediumLockList->Lock();
|
---|
4421 | if (FAILED(rc))
|
---|
4422 | {
|
---|
4423 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
4424 | throw setError(rc,
|
---|
4425 | tr("Failed to lock media when merging to '%s'"),
|
---|
4426 | pTarget->getLocationFull().c_str());
|
---|
4427 | }
|
---|
4428 | }
|
---|
4429 | }
|
---|
4430 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4431 |
|
---|
4432 | if (FAILED(rc))
|
---|
4433 | {
|
---|
4434 | delete aMediumLockList;
|
---|
4435 | aMediumLockList = NULL;
|
---|
4436 | }
|
---|
4437 |
|
---|
4438 | return rc;
|
---|
4439 | }
|
---|
4440 |
|
---|
4441 | /**
|
---|
4442 | * Merges this medium to the specified medium which must be either its
|
---|
4443 | * direct ancestor or descendant.
|
---|
4444 | *
|
---|
4445 | * Given this medium is SOURCE and the specified medium is TARGET, we will
|
---|
4446 | * get two variants of the merge operation:
|
---|
4447 | *
|
---|
4448 | * forward merge
|
---|
4449 | * ------------------------->
|
---|
4450 | * [Extra] <- SOURCE <- Intermediate <- TARGET
|
---|
4451 | * Any Del Del LockWr
|
---|
4452 | *
|
---|
4453 | *
|
---|
4454 | * backward merge
|
---|
4455 | * <-------------------------
|
---|
4456 | * TARGET <- Intermediate <- SOURCE <- [Extra]
|
---|
4457 | * LockWr Del Del LockWr
|
---|
4458 | *
|
---|
4459 | * Each diagram shows the involved media on the media chain where
|
---|
4460 | * SOURCE and TARGET belong. Under each medium there is a state value which
|
---|
4461 | * the medium must have at a time of the mergeTo() call.
|
---|
4462 | *
|
---|
4463 | * The media in the square braces may be absent (e.g. when the forward
|
---|
4464 | * operation takes place and SOURCE is the base medium, or when the backward
|
---|
4465 | * merge operation takes place and TARGET is the last child in the chain) but if
|
---|
4466 | * they present they are involved too as shown.
|
---|
4467 | *
|
---|
4468 | * Neither the source medium nor intermediate media may be attached to
|
---|
4469 | * any VM directly or in the snapshot, otherwise this method will assert.
|
---|
4470 | *
|
---|
4471 | * The #prepareMergeTo() method must be called prior to this method to place all
|
---|
4472 | * involved to necessary states and perform other consistency checks.
|
---|
4473 | *
|
---|
4474 | * If @a aWait is @c true then this method will perform the operation on the
|
---|
4475 | * calling thread and will not return to the caller until the operation is
|
---|
4476 | * completed. When this method succeeds, all intermediate medium objects in
|
---|
4477 | * the chain will be uninitialized, the state of the target medium (and all
|
---|
4478 | * involved extra media) will be restored. @a aMediumLockList will not be
|
---|
4479 | * deleted, whether the operation is successful or not. The caller has to do
|
---|
4480 | * this if appropriate. Note that this (source) medium is not uninitialized
|
---|
4481 | * because of possible AutoCaller instances held by the caller of this method
|
---|
4482 | * on the current thread. It's therefore the responsibility of the caller to
|
---|
4483 | * call Medium::uninit() after releasing all callers.
|
---|
4484 | *
|
---|
4485 | * If @a aWait is @c false then this method will create a thread to perform the
|
---|
4486 | * operation asynchronously and will return immediately. If the operation
|
---|
4487 | * succeeds, the thread will uninitialize the source medium object and all
|
---|
4488 | * intermediate medium objects in the chain, reset the state of the target
|
---|
4489 | * medium (and all involved extra media) and delete @a aMediumLockList.
|
---|
4490 | * If the operation fails, the thread will only reset the states of all
|
---|
4491 | * involved media and delete @a aMediumLockList.
|
---|
4492 | *
|
---|
4493 | * When this method fails (regardless of the @a aWait mode), it is a caller's
|
---|
4494 | * responsibility to undo state changes and delete @a aMediumLockList using
|
---|
4495 | * #cancelMergeTo().
|
---|
4496 | *
|
---|
4497 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
4498 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
4499 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
4500 | * progress object is created/used at all. Note that @a aProgress cannot be
|
---|
4501 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
4502 | *
|
---|
4503 | * @param pTarget Target medium.
|
---|
4504 | * @param fMergeForward Merge direction.
|
---|
4505 | * @param pParentForTarget New parent for target medium after merge.
|
---|
4506 | * @param aChildrenToReparent List of children of the source which will have
|
---|
4507 | * to be reparented to the target after merge.
|
---|
4508 | * @param aMediumLockList Medium locking information.
|
---|
4509 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
4510 | * completion.
|
---|
4511 | * @param aWait @c true if this method should block instead of creating
|
---|
4512 | * an asynchronous thread.
|
---|
4513 | * @param pfNeedsGlobalSaveSettings Optional pointer to a bool that must have been initialized to false and that will be set to true
|
---|
4514 | * by this function if the caller should invoke VirtualBox::saveSettings() because the global settings have changed.
|
---|
4515 | * This only works in "wait" mode; otherwise saveSettings gets called automatically by the thread that was created,
|
---|
4516 | * and this parameter is ignored.
|
---|
4517 | *
|
---|
4518 | * @note Locks the tree lock for writing. Locks the media from the chain
|
---|
4519 | * for writing.
|
---|
4520 | */
|
---|
4521 | HRESULT Medium::mergeTo(const ComObjPtr<Medium> &pTarget,
|
---|
4522 | bool fMergeForward,
|
---|
4523 | const ComObjPtr<Medium> &pParentForTarget,
|
---|
4524 | const MediaList &aChildrenToReparent,
|
---|
4525 | MediumLockList *aMediumLockList,
|
---|
4526 | ComObjPtr <Progress> *aProgress,
|
---|
4527 | bool aWait,
|
---|
4528 | GuidList *pllRegistriesThatNeedSaving)
|
---|
4529 | {
|
---|
4530 | AssertReturn(pTarget != NULL, E_FAIL);
|
---|
4531 | AssertReturn(pTarget != this, E_FAIL);
|
---|
4532 | AssertReturn(aMediumLockList != NULL, E_FAIL);
|
---|
4533 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
4534 |
|
---|
4535 | AutoCaller autoCaller(this);
|
---|
4536 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4537 |
|
---|
4538 | AutoCaller targetCaller(pTarget);
|
---|
4539 | AssertComRCReturnRC(targetCaller.rc());
|
---|
4540 |
|
---|
4541 | HRESULT rc = S_OK;
|
---|
4542 | ComObjPtr <Progress> pProgress;
|
---|
4543 | Medium::Task *pTask = NULL;
|
---|
4544 |
|
---|
4545 | try
|
---|
4546 | {
|
---|
4547 | if (aProgress != NULL)
|
---|
4548 | {
|
---|
4549 | /* use the existing progress object... */
|
---|
4550 | pProgress = *aProgress;
|
---|
4551 |
|
---|
4552 | /* ...but create a new one if it is null */
|
---|
4553 | if (pProgress.isNull())
|
---|
4554 | {
|
---|
4555 | Utf8Str tgtName;
|
---|
4556 | {
|
---|
4557 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
4558 | tgtName = pTarget->getName();
|
---|
4559 | }
|
---|
4560 |
|
---|
4561 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4562 |
|
---|
4563 | pProgress.createObject();
|
---|
4564 | rc = pProgress->init(m->pVirtualBox,
|
---|
4565 | static_cast<IMedium*>(this),
|
---|
4566 | BstrFmt(tr("Merging medium '%s' to '%s'"),
|
---|
4567 | getName().c_str(),
|
---|
4568 | tgtName.c_str()).raw(),
|
---|
4569 | TRUE /* aCancelable */);
|
---|
4570 | if (FAILED(rc))
|
---|
4571 | throw rc;
|
---|
4572 | }
|
---|
4573 | }
|
---|
4574 |
|
---|
4575 | /* setup task object to carry out the operation sync/async */
|
---|
4576 | pTask = new Medium::MergeTask(this, pTarget, fMergeForward,
|
---|
4577 | pParentForTarget, aChildrenToReparent,
|
---|
4578 | pProgress, aMediumLockList,
|
---|
4579 | aWait /* fKeepMediumLockList */);
|
---|
4580 | rc = pTask->rc();
|
---|
4581 | AssertComRC(rc);
|
---|
4582 | if (FAILED(rc))
|
---|
4583 | throw rc;
|
---|
4584 | }
|
---|
4585 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4586 |
|
---|
4587 | if (SUCCEEDED(rc))
|
---|
4588 | {
|
---|
4589 | if (aWait)
|
---|
4590 | rc = runNow(pTask, pllRegistriesThatNeedSaving);
|
---|
4591 | else
|
---|
4592 | rc = startThread(pTask);
|
---|
4593 |
|
---|
4594 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
4595 | *aProgress = pProgress;
|
---|
4596 | }
|
---|
4597 | else if (pTask != NULL)
|
---|
4598 | delete pTask;
|
---|
4599 |
|
---|
4600 | return rc;
|
---|
4601 | }
|
---|
4602 |
|
---|
4603 | /**
|
---|
4604 | * Undoes what #prepareMergeTo() did. Must be called if #mergeTo() is not
|
---|
4605 | * called or fails. Frees memory occupied by @a aMediumLockList and unlocks
|
---|
4606 | * the medium objects in @a aChildrenToReparent.
|
---|
4607 | *
|
---|
4608 | * @param aChildrenToReparent List of children of the source which will have
|
---|
4609 | * to be reparented to the target after merge.
|
---|
4610 | * @param aMediumLockList Medium locking information.
|
---|
4611 | *
|
---|
4612 | * @note Locks the media from the chain for writing.
|
---|
4613 | */
|
---|
4614 | void Medium::cancelMergeTo(const MediaList &aChildrenToReparent,
|
---|
4615 | MediumLockList *aMediumLockList)
|
---|
4616 | {
|
---|
4617 | AutoCaller autoCaller(this);
|
---|
4618 | AssertComRCReturnVoid(autoCaller.rc());
|
---|
4619 |
|
---|
4620 | AssertReturnVoid(aMediumLockList != NULL);
|
---|
4621 |
|
---|
4622 | /* Revert media marked for deletion to previous state. */
|
---|
4623 | HRESULT rc;
|
---|
4624 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
4625 | aMediumLockList->GetBegin();
|
---|
4626 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
4627 | aMediumLockList->GetEnd();
|
---|
4628 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
4629 | it != mediumListEnd;
|
---|
4630 | ++it)
|
---|
4631 | {
|
---|
4632 | const MediumLock &mediumLock = *it;
|
---|
4633 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
4634 | AutoWriteLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
4635 |
|
---|
4636 | if (pMedium->m->state == MediumState_Deleting)
|
---|
4637 | {
|
---|
4638 | rc = pMedium->unmarkForDeletion();
|
---|
4639 | AssertComRC(rc);
|
---|
4640 | }
|
---|
4641 | }
|
---|
4642 |
|
---|
4643 | /* the destructor will do the work */
|
---|
4644 | delete aMediumLockList;
|
---|
4645 |
|
---|
4646 | /* unlock the children which had to be reparented */
|
---|
4647 | for (MediaList::const_iterator it = aChildrenToReparent.begin();
|
---|
4648 | it != aChildrenToReparent.end();
|
---|
4649 | ++it)
|
---|
4650 | {
|
---|
4651 | const ComObjPtr<Medium> &pMedium = *it;
|
---|
4652 |
|
---|
4653 | AutoWriteLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
4654 | pMedium->UnlockWrite(NULL);
|
---|
4655 | }
|
---|
4656 | }
|
---|
4657 |
|
---|
4658 | /**
|
---|
4659 | * Fix the parent UUID of all children to point to this medium as their
|
---|
4660 | * parent.
|
---|
4661 | */
|
---|
4662 | HRESULT Medium::fixParentUuidOfChildren(const MediaList &childrenToReparent)
|
---|
4663 | {
|
---|
4664 | MediumLockList mediumLockList;
|
---|
4665 | HRESULT rc = createMediumLockList(true /* fFailIfInaccessible */,
|
---|
4666 | false /* fMediumLockWrite */,
|
---|
4667 | this,
|
---|
4668 | mediumLockList);
|
---|
4669 | AssertComRCReturnRC(rc);
|
---|
4670 |
|
---|
4671 | try
|
---|
4672 | {
|
---|
4673 | PVBOXHDD hdd;
|
---|
4674 | int vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &hdd);
|
---|
4675 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
4676 |
|
---|
4677 | try
|
---|
4678 | {
|
---|
4679 | MediumLockList::Base::iterator lockListBegin =
|
---|
4680 | mediumLockList.GetBegin();
|
---|
4681 | MediumLockList::Base::iterator lockListEnd =
|
---|
4682 | mediumLockList.GetEnd();
|
---|
4683 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
4684 | it != lockListEnd;
|
---|
4685 | ++it)
|
---|
4686 | {
|
---|
4687 | MediumLock &mediumLock = *it;
|
---|
4688 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
4689 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
4690 |
|
---|
4691 | // open the medium
|
---|
4692 | vrc = VDOpen(hdd,
|
---|
4693 | pMedium->m->strFormat.c_str(),
|
---|
4694 | pMedium->m->strLocationFull.c_str(),
|
---|
4695 | VD_OPEN_FLAGS_READONLY,
|
---|
4696 | pMedium->m->vdImageIfaces);
|
---|
4697 | if (RT_FAILURE(vrc))
|
---|
4698 | throw vrc;
|
---|
4699 | }
|
---|
4700 |
|
---|
4701 | for (MediaList::const_iterator it = childrenToReparent.begin();
|
---|
4702 | it != childrenToReparent.end();
|
---|
4703 | ++it)
|
---|
4704 | {
|
---|
4705 | /* VD_OPEN_FLAGS_INFO since UUID is wrong yet */
|
---|
4706 | vrc = VDOpen(hdd,
|
---|
4707 | (*it)->m->strFormat.c_str(),
|
---|
4708 | (*it)->m->strLocationFull.c_str(),
|
---|
4709 | VD_OPEN_FLAGS_INFO,
|
---|
4710 | (*it)->m->vdImageIfaces);
|
---|
4711 | if (RT_FAILURE(vrc))
|
---|
4712 | throw vrc;
|
---|
4713 |
|
---|
4714 | vrc = VDSetParentUuid(hdd, VD_LAST_IMAGE, m->id.raw());
|
---|
4715 | if (RT_FAILURE(vrc))
|
---|
4716 | throw vrc;
|
---|
4717 |
|
---|
4718 | vrc = VDClose(hdd, false /* fDelete */);
|
---|
4719 | if (RT_FAILURE(vrc))
|
---|
4720 | throw vrc;
|
---|
4721 |
|
---|
4722 | (*it)->UnlockWrite(NULL);
|
---|
4723 | }
|
---|
4724 | }
|
---|
4725 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4726 | catch (int aVRC)
|
---|
4727 | {
|
---|
4728 | throw setError(E_FAIL,
|
---|
4729 | tr("Could not update medium UUID references to parent '%s' (%s)"),
|
---|
4730 | m->strLocationFull.c_str(),
|
---|
4731 | vdError(aVRC).c_str());
|
---|
4732 | }
|
---|
4733 |
|
---|
4734 | VDDestroy(hdd);
|
---|
4735 | }
|
---|
4736 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4737 |
|
---|
4738 | return rc;
|
---|
4739 | }
|
---|
4740 |
|
---|
4741 | /**
|
---|
4742 | * Used by IAppliance to export disk images.
|
---|
4743 | *
|
---|
4744 | * @param aFilename Filename to create (UTF8).
|
---|
4745 | * @param aFormat Medium format for creating @a aFilename.
|
---|
4746 | * @param aVariant Which exact image format variant to use
|
---|
4747 | * for the destination image.
|
---|
4748 | * @param aVDImageIOCallbacks Pointer to the callback table for a
|
---|
4749 | * VDINTERFACEIO interface. May be NULL.
|
---|
4750 | * @param aVDImageIOUser Opaque data for the callbacks.
|
---|
4751 | * @param aProgress Progress object to use.
|
---|
4752 | * @return
|
---|
4753 | * @note The source format is defined by the Medium instance.
|
---|
4754 | */
|
---|
4755 | HRESULT Medium::exportFile(const char *aFilename,
|
---|
4756 | const ComObjPtr<MediumFormat> &aFormat,
|
---|
4757 | MediumVariant_T aVariant,
|
---|
4758 | void *aVDImageIOCallbacks, void *aVDImageIOUser,
|
---|
4759 | const ComObjPtr<Progress> &aProgress)
|
---|
4760 | {
|
---|
4761 | AssertPtrReturn(aFilename, E_INVALIDARG);
|
---|
4762 | AssertReturn(!aFormat.isNull(), E_INVALIDARG);
|
---|
4763 | AssertReturn(!aProgress.isNull(), E_INVALIDARG);
|
---|
4764 |
|
---|
4765 | AutoCaller autoCaller(this);
|
---|
4766 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4767 |
|
---|
4768 | HRESULT rc = S_OK;
|
---|
4769 | Medium::Task *pTask = NULL;
|
---|
4770 |
|
---|
4771 | try
|
---|
4772 | {
|
---|
4773 | // locking: we need the tree lock first because we access parent pointers
|
---|
4774 | AutoReadLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4775 | // and we need to write-lock the media involved
|
---|
4776 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4777 |
|
---|
4778 | /* Build the source lock list. */
|
---|
4779 | MediumLockList *pSourceMediumLockList(new MediumLockList());
|
---|
4780 | rc = createMediumLockList(true /* fFailIfInaccessible */,
|
---|
4781 | false /* fMediumLockWrite */,
|
---|
4782 | NULL,
|
---|
4783 | *pSourceMediumLockList);
|
---|
4784 | if (FAILED(rc))
|
---|
4785 | {
|
---|
4786 | delete pSourceMediumLockList;
|
---|
4787 | throw rc;
|
---|
4788 | }
|
---|
4789 |
|
---|
4790 | rc = pSourceMediumLockList->Lock();
|
---|
4791 | if (FAILED(rc))
|
---|
4792 | {
|
---|
4793 | delete pSourceMediumLockList;
|
---|
4794 | throw setError(rc,
|
---|
4795 | tr("Failed to lock source media '%s'"),
|
---|
4796 | getLocationFull().c_str());
|
---|
4797 | }
|
---|
4798 |
|
---|
4799 | /* setup task object to carry out the operation asynchronously */
|
---|
4800 | pTask = new Medium::ExportTask(this, aProgress, aFilename, aFormat,
|
---|
4801 | aVariant, aVDImageIOCallbacks,
|
---|
4802 | aVDImageIOUser, pSourceMediumLockList);
|
---|
4803 | rc = pTask->rc();
|
---|
4804 | AssertComRC(rc);
|
---|
4805 | if (FAILED(rc))
|
---|
4806 | throw rc;
|
---|
4807 | }
|
---|
4808 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4809 |
|
---|
4810 | if (SUCCEEDED(rc))
|
---|
4811 | rc = startThread(pTask);
|
---|
4812 | else if (pTask != NULL)
|
---|
4813 | delete pTask;
|
---|
4814 |
|
---|
4815 | return rc;
|
---|
4816 | }
|
---|
4817 |
|
---|
4818 | /**
|
---|
4819 | * Used by IAppliance to import disk images.
|
---|
4820 | *
|
---|
4821 | * @param aFilename Filename to read (UTF8).
|
---|
4822 | * @param aFormat Medium format for reading @a aFilename.
|
---|
4823 | * @param aVariant Which exact image format variant to use
|
---|
4824 | * for the destination image.
|
---|
4825 | * @param aVDImageIOCallbacks Pointer to the callback table for a
|
---|
4826 | * VDINTERFACEIO interface. May be NULL.
|
---|
4827 | * @param aVDImageIOUser Opaque data for the callbacks.
|
---|
4828 | * @param aParent Parent medium. May be NULL.
|
---|
4829 | * @param aProgress Progress object to use.
|
---|
4830 | * @return
|
---|
4831 | * @note The destination format is defined by the Medium instance.
|
---|
4832 | */
|
---|
4833 | HRESULT Medium::importFile(const char *aFilename,
|
---|
4834 | const ComObjPtr<MediumFormat> &aFormat,
|
---|
4835 | MediumVariant_T aVariant,
|
---|
4836 | void *aVDImageIOCallbacks, void *aVDImageIOUser,
|
---|
4837 | const ComObjPtr<Medium> &aParent,
|
---|
4838 | const ComObjPtr<Progress> &aProgress)
|
---|
4839 | {
|
---|
4840 | AssertPtrReturn(aFilename, E_INVALIDARG);
|
---|
4841 | AssertReturn(!aFormat.isNull(), E_INVALIDARG);
|
---|
4842 | AssertReturn(!aProgress.isNull(), E_INVALIDARG);
|
---|
4843 |
|
---|
4844 | AutoCaller autoCaller(this);
|
---|
4845 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4846 |
|
---|
4847 | HRESULT rc = S_OK;
|
---|
4848 | Medium::Task *pTask = NULL;
|
---|
4849 |
|
---|
4850 | try
|
---|
4851 | {
|
---|
4852 | // locking: we need the tree lock first because we access parent pointers
|
---|
4853 | AutoReadLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4854 | // and we need to write-lock the media involved
|
---|
4855 | AutoMultiWriteLock2 alock(this, aParent COMMA_LOCKVAL_SRC_POS);
|
---|
4856 |
|
---|
4857 | if ( m->state != MediumState_NotCreated
|
---|
4858 | && m->state != MediumState_Created)
|
---|
4859 | throw setStateError();
|
---|
4860 |
|
---|
4861 | /* Build the target lock list. */
|
---|
4862 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
4863 | rc = createMediumLockList(true /* fFailIfInaccessible */,
|
---|
4864 | true /* fMediumLockWrite */,
|
---|
4865 | aParent,
|
---|
4866 | *pTargetMediumLockList);
|
---|
4867 | if (FAILED(rc))
|
---|
4868 | {
|
---|
4869 | delete pTargetMediumLockList;
|
---|
4870 | throw rc;
|
---|
4871 | }
|
---|
4872 |
|
---|
4873 | rc = pTargetMediumLockList->Lock();
|
---|
4874 | if (FAILED(rc))
|
---|
4875 | {
|
---|
4876 | delete pTargetMediumLockList;
|
---|
4877 | throw setError(rc,
|
---|
4878 | tr("Failed to lock target media '%s'"),
|
---|
4879 | getLocationFull().c_str());
|
---|
4880 | }
|
---|
4881 |
|
---|
4882 | /* setup task object to carry out the operation asynchronously */
|
---|
4883 | pTask = new Medium::ImportTask(this, aProgress, aFilename, aFormat,
|
---|
4884 | aVariant, aVDImageIOCallbacks,
|
---|
4885 | aVDImageIOUser, aParent,
|
---|
4886 | pTargetMediumLockList);
|
---|
4887 | rc = pTask->rc();
|
---|
4888 | AssertComRC(rc);
|
---|
4889 | if (FAILED(rc))
|
---|
4890 | throw rc;
|
---|
4891 |
|
---|
4892 | if (m->state == MediumState_NotCreated)
|
---|
4893 | m->state = MediumState_Creating;
|
---|
4894 | }
|
---|
4895 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4896 |
|
---|
4897 | if (SUCCEEDED(rc))
|
---|
4898 | rc = startThread(pTask);
|
---|
4899 | else if (pTask != NULL)
|
---|
4900 | delete pTask;
|
---|
4901 |
|
---|
4902 | return rc;
|
---|
4903 | }
|
---|
4904 |
|
---|
4905 | ////////////////////////////////////////////////////////////////////////////////
|
---|
4906 | //
|
---|
4907 | // Private methods
|
---|
4908 | //
|
---|
4909 | ////////////////////////////////////////////////////////////////////////////////
|
---|
4910 |
|
---|
4911 | /**
|
---|
4912 | * Queries information from the medium.
|
---|
4913 | *
|
---|
4914 | * As a result of this call, the accessibility state and data members such as
|
---|
4915 | * size and description will be updated with the current information.
|
---|
4916 | *
|
---|
4917 | * @note This method may block during a system I/O call that checks storage
|
---|
4918 | * accessibility.
|
---|
4919 | *
|
---|
4920 | * @note Locks medium tree for reading and writing (for new diff media checked
|
---|
4921 | * for the first time). Locks mParent for reading. Locks this object for
|
---|
4922 | * writing.
|
---|
4923 | *
|
---|
4924 | * @param fSetImageId Whether to reset the UUID contained in the image file to the UUID in the medium instance data (see SetIDs())
|
---|
4925 | * @param fSetParentId Whether to reset the parent UUID contained in the image file to the parent UUID in the medium instance data (see SetIDs())
|
---|
4926 | * @return
|
---|
4927 | */
|
---|
4928 | HRESULT Medium::queryInfo(bool fSetImageId, bool fSetParentId)
|
---|
4929 | {
|
---|
4930 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4931 |
|
---|
4932 | if ( m->state != MediumState_Created
|
---|
4933 | && m->state != MediumState_Inaccessible
|
---|
4934 | && m->state != MediumState_LockedRead)
|
---|
4935 | return E_FAIL;
|
---|
4936 |
|
---|
4937 | HRESULT rc = S_OK;
|
---|
4938 |
|
---|
4939 | int vrc = VINF_SUCCESS;
|
---|
4940 |
|
---|
4941 | /* check if a blocking queryInfo() call is in progress on some other thread,
|
---|
4942 | * and wait for it to finish if so instead of querying data ourselves */
|
---|
4943 | if (m->queryInfoRunning)
|
---|
4944 | {
|
---|
4945 | Assert( m->state == MediumState_LockedRead
|
---|
4946 | || m->state == MediumState_LockedWrite);
|
---|
4947 |
|
---|
4948 | alock.leave();
|
---|
4949 | vrc = RTSemEventMultiWait(m->queryInfoSem, RT_INDEFINITE_WAIT);
|
---|
4950 | alock.enter();
|
---|
4951 |
|
---|
4952 | AssertRC(vrc);
|
---|
4953 |
|
---|
4954 | return S_OK;
|
---|
4955 | }
|
---|
4956 |
|
---|
4957 | bool success = false;
|
---|
4958 | Utf8Str lastAccessError;
|
---|
4959 |
|
---|
4960 | /* are we dealing with a new medium constructed using the existing
|
---|
4961 | * location? */
|
---|
4962 | bool isImport = m->id.isEmpty();
|
---|
4963 | unsigned uOpenFlags = VD_OPEN_FLAGS_INFO;
|
---|
4964 |
|
---|
4965 | /* Note that we don't use VD_OPEN_FLAGS_READONLY when opening new
|
---|
4966 | * media because that would prevent necessary modifications
|
---|
4967 | * when opening media of some third-party formats for the first
|
---|
4968 | * time in VirtualBox (such as VMDK for which VDOpen() needs to
|
---|
4969 | * generate an UUID if it is missing) */
|
---|
4970 | if ( (m->hddOpenMode == OpenReadOnly)
|
---|
4971 | || m->type == MediumType_Readonly
|
---|
4972 | || !isImport
|
---|
4973 | )
|
---|
4974 | uOpenFlags |= VD_OPEN_FLAGS_READONLY;
|
---|
4975 |
|
---|
4976 | /* Open shareable medium with the appropriate flags */
|
---|
4977 | if (m->type == MediumType_Shareable)
|
---|
4978 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
4979 |
|
---|
4980 | /* Lock the medium, which makes the behavior much more consistent */
|
---|
4981 | if (uOpenFlags & (VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SHAREABLE))
|
---|
4982 | rc = LockRead(NULL);
|
---|
4983 | else
|
---|
4984 | rc = LockWrite(NULL);
|
---|
4985 | if (FAILED(rc)) return rc;
|
---|
4986 |
|
---|
4987 | /* Copies of the input state fields which are not read-only,
|
---|
4988 | * as we're dropping the lock. CAUTION: be extremely careful what
|
---|
4989 | * you do with the contents of this medium object, as you will
|
---|
4990 | * create races if there are concurrent changes. */
|
---|
4991 | Utf8Str format(m->strFormat);
|
---|
4992 | Utf8Str location(m->strLocationFull);
|
---|
4993 | ComObjPtr<MediumFormat> formatObj = m->formatObj;
|
---|
4994 |
|
---|
4995 | /* "Output" values which can't be set because the lock isn't held
|
---|
4996 | * at the time the values are determined. */
|
---|
4997 | Guid mediumId = m->id;
|
---|
4998 | uint64_t mediumSize = 0;
|
---|
4999 | uint64_t mediumLogicalSize = 0;
|
---|
5000 |
|
---|
5001 | /* Flag whether a base image has a non-zero parent UUID and thus
|
---|
5002 | * need repairing after it was closed again. */
|
---|
5003 | bool fRepairImageZeroParentUuid = false;
|
---|
5004 |
|
---|
5005 | /* leave the lock before a lengthy operation */
|
---|
5006 | vrc = RTSemEventMultiReset(m->queryInfoSem);
|
---|
5007 | AssertRCReturn(vrc, E_FAIL);
|
---|
5008 | m->queryInfoRunning = true;
|
---|
5009 | alock.leave();
|
---|
5010 |
|
---|
5011 | try
|
---|
5012 | {
|
---|
5013 | /* skip accessibility checks for host drives */
|
---|
5014 | if (m->hostDrive)
|
---|
5015 | {
|
---|
5016 | success = true;
|
---|
5017 | throw S_OK;
|
---|
5018 | }
|
---|
5019 |
|
---|
5020 | PVBOXHDD hdd;
|
---|
5021 | vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &hdd);
|
---|
5022 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
5023 |
|
---|
5024 | try
|
---|
5025 | {
|
---|
5026 | /** @todo This kind of opening of media is assuming that diff
|
---|
5027 | * media can be opened as base media. Should be documented that
|
---|
5028 | * it must work for all medium format backends. */
|
---|
5029 | vrc = VDOpen(hdd,
|
---|
5030 | format.c_str(),
|
---|
5031 | location.c_str(),
|
---|
5032 | uOpenFlags,
|
---|
5033 | m->vdImageIfaces);
|
---|
5034 | if (RT_FAILURE(vrc))
|
---|
5035 | {
|
---|
5036 | lastAccessError = Utf8StrFmt(tr("Could not open the medium '%s'%s"),
|
---|
5037 | location.c_str(), vdError(vrc).c_str());
|
---|
5038 | throw S_OK;
|
---|
5039 | }
|
---|
5040 |
|
---|
5041 | if (formatObj->getCapabilities() & MediumFormatCapabilities_Uuid)
|
---|
5042 | {
|
---|
5043 | /* Modify the UUIDs if necessary. The associated fields are
|
---|
5044 | * not modified by other code, so no need to copy. */
|
---|
5045 | if (fSetImageId)
|
---|
5046 | {
|
---|
5047 | vrc = VDSetUuid(hdd, 0, m->uuidImage.raw());
|
---|
5048 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
5049 | }
|
---|
5050 | if (fSetParentId)
|
---|
5051 | {
|
---|
5052 | vrc = VDSetParentUuid(hdd, 0, m->uuidParentImage.raw());
|
---|
5053 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
5054 | }
|
---|
5055 | /* zap the information, these are no long-term members */
|
---|
5056 | unconst(m->uuidImage).clear();
|
---|
5057 | unconst(m->uuidParentImage).clear();
|
---|
5058 |
|
---|
5059 | /* check the UUID */
|
---|
5060 | RTUUID uuid;
|
---|
5061 | vrc = VDGetUuid(hdd, 0, &uuid);
|
---|
5062 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
5063 |
|
---|
5064 | if (isImport)
|
---|
5065 | {
|
---|
5066 | mediumId = uuid;
|
---|
5067 |
|
---|
5068 | if (mediumId.isEmpty() && (m->hddOpenMode == OpenReadOnly))
|
---|
5069 | // only when importing a VDMK that has no UUID, create one in memory
|
---|
5070 | mediumId.create();
|
---|
5071 | }
|
---|
5072 | else
|
---|
5073 | {
|
---|
5074 | Assert(!mediumId.isEmpty());
|
---|
5075 |
|
---|
5076 | if (mediumId != uuid)
|
---|
5077 | {
|
---|
5078 | lastAccessError = Utf8StrFmt(
|
---|
5079 | tr("UUID {%RTuuid} of the medium '%s' does not match the value {%RTuuid} stored in the media registry ('%s')"),
|
---|
5080 | &uuid,
|
---|
5081 | location.c_str(),
|
---|
5082 | mediumId.raw(),
|
---|
5083 | m->pVirtualBox->settingsFilePath().c_str());
|
---|
5084 | throw S_OK;
|
---|
5085 | }
|
---|
5086 | }
|
---|
5087 | }
|
---|
5088 | else
|
---|
5089 | {
|
---|
5090 | /* the backend does not support storing UUIDs within the
|
---|
5091 | * underlying storage so use what we store in XML */
|
---|
5092 |
|
---|
5093 | /* generate an UUID for an imported UUID-less medium */
|
---|
5094 | if (isImport)
|
---|
5095 | {
|
---|
5096 | if (fSetImageId)
|
---|
5097 | mediumId = m->uuidImage;
|
---|
5098 | else
|
---|
5099 | mediumId.create();
|
---|
5100 | }
|
---|
5101 | }
|
---|
5102 |
|
---|
5103 | /* get the medium variant */
|
---|
5104 | unsigned uImageFlags;
|
---|
5105 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
5106 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
5107 | m->variant = (MediumVariant_T)uImageFlags;
|
---|
5108 |
|
---|
5109 | /* check/get the parent uuid and update corresponding state */
|
---|
5110 | if (uImageFlags & VD_IMAGE_FLAGS_DIFF)
|
---|
5111 | {
|
---|
5112 | RTUUID parentId;
|
---|
5113 | vrc = VDGetParentUuid(hdd, 0, &parentId);
|
---|
5114 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
5115 |
|
---|
5116 | /* streamOptimized VMDK images are only accepted as base
|
---|
5117 | * images, as this allows automatic repair of OVF appliances.
|
---|
5118 | * Since such images don't support random writes they will not
|
---|
5119 | * be created for diff images. Only an overly smart user might
|
---|
5120 | * manually create this case. Too bad for him. */
|
---|
5121 | if ( isImport
|
---|
5122 | && !(uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
|
---|
5123 | {
|
---|
5124 | /* the parent must be known to us. Note that we freely
|
---|
5125 | * call locking methods of mVirtualBox and parent, as all
|
---|
5126 | * relevant locks must be already held. There may be no
|
---|
5127 | * concurrent access to the just opened medium on other
|
---|
5128 | * threads yet (and init() will fail if this method reports
|
---|
5129 | * MediumState_Inaccessible) */
|
---|
5130 |
|
---|
5131 | Guid id = parentId;
|
---|
5132 | ComObjPtr<Medium> pParent;
|
---|
5133 | rc = m->pVirtualBox->findHardDiskById(id, false /* aSetError */, &pParent);
|
---|
5134 | if (FAILED(rc))
|
---|
5135 | {
|
---|
5136 | lastAccessError = Utf8StrFmt(
|
---|
5137 | tr("Parent medium with UUID {%RTuuid} of the medium '%s' is not found in the media registry ('%s')"),
|
---|
5138 | &parentId, location.c_str(),
|
---|
5139 | m->pVirtualBox->settingsFilePath().c_str());
|
---|
5140 | throw S_OK;
|
---|
5141 | }
|
---|
5142 |
|
---|
5143 | /* we set mParent & children() */
|
---|
5144 | AutoWriteLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5145 |
|
---|
5146 | Assert(m->pParent.isNull());
|
---|
5147 | m->pParent = pParent;
|
---|
5148 | m->pParent->m->llChildren.push_back(this);
|
---|
5149 | }
|
---|
5150 | else
|
---|
5151 | {
|
---|
5152 | /* we access mParent */
|
---|
5153 | AutoReadLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5154 |
|
---|
5155 | /* check that parent UUIDs match. Note that there's no need
|
---|
5156 | * for the parent's AutoCaller (our lifetime is bound to
|
---|
5157 | * it) */
|
---|
5158 |
|
---|
5159 | if (m->pParent.isNull())
|
---|
5160 | {
|
---|
5161 | /* Due to a bug in VDCopy() in VirtualBox 3.0.0-3.0.14
|
---|
5162 | * and 3.1.0-3.1.8 there are base images out there
|
---|
5163 | * which have a non-zero parent UUID. No point in
|
---|
5164 | * complaining about them, instead automatically
|
---|
5165 | * repair the problem. Later we can bring back the
|
---|
5166 | * error message, but we should wait until really
|
---|
5167 | * most users have repaired their images, either with
|
---|
5168 | * VBoxFixHdd or this way. */
|
---|
5169 | #if 1
|
---|
5170 | fRepairImageZeroParentUuid = true;
|
---|
5171 | #else /* 0 */
|
---|
5172 | lastAccessError = Utf8StrFmt(
|
---|
5173 | tr("Medium type of '%s' is differencing but it is not associated with any parent medium in the media registry ('%s')"),
|
---|
5174 | location.c_str(),
|
---|
5175 | m->pVirtualBox->settingsFilePath().c_str());
|
---|
5176 | throw S_OK;
|
---|
5177 | #endif /* 0 */
|
---|
5178 | }
|
---|
5179 |
|
---|
5180 | AutoReadLock parentLock(m->pParent COMMA_LOCKVAL_SRC_POS);
|
---|
5181 | if ( !fRepairImageZeroParentUuid
|
---|
5182 | && m->pParent->getState() != MediumState_Inaccessible
|
---|
5183 | && m->pParent->getId() != parentId)
|
---|
5184 | {
|
---|
5185 | lastAccessError = Utf8StrFmt(
|
---|
5186 | tr("Parent UUID {%RTuuid} of the medium '%s' does not match UUID {%RTuuid} of its parent medium stored in the media registry ('%s')"),
|
---|
5187 | &parentId, location.c_str(),
|
---|
5188 | m->pParent->getId().raw(),
|
---|
5189 | m->pVirtualBox->settingsFilePath().c_str());
|
---|
5190 | throw S_OK;
|
---|
5191 | }
|
---|
5192 |
|
---|
5193 | /// @todo NEWMEDIA what to do if the parent is not
|
---|
5194 | /// accessible while the diff is? Probably nothing. The
|
---|
5195 | /// real code will detect the mismatch anyway.
|
---|
5196 | }
|
---|
5197 | }
|
---|
5198 |
|
---|
5199 | mediumSize = VDGetFileSize(hdd, 0);
|
---|
5200 | mediumLogicalSize = VDGetSize(hdd, 0);
|
---|
5201 |
|
---|
5202 | success = true;
|
---|
5203 | }
|
---|
5204 | catch (HRESULT aRC)
|
---|
5205 | {
|
---|
5206 | rc = aRC;
|
---|
5207 | }
|
---|
5208 |
|
---|
5209 | VDDestroy(hdd);
|
---|
5210 | }
|
---|
5211 | catch (HRESULT aRC)
|
---|
5212 | {
|
---|
5213 | rc = aRC;
|
---|
5214 | }
|
---|
5215 |
|
---|
5216 | alock.enter();
|
---|
5217 |
|
---|
5218 | if (isImport)
|
---|
5219 | unconst(m->id) = mediumId;
|
---|
5220 |
|
---|
5221 | if (success)
|
---|
5222 | {
|
---|
5223 | m->size = mediumSize;
|
---|
5224 | m->logicalSize = mediumLogicalSize;
|
---|
5225 | m->strLastAccessError.setNull();
|
---|
5226 | }
|
---|
5227 | else
|
---|
5228 | {
|
---|
5229 | m->strLastAccessError = lastAccessError;
|
---|
5230 | LogWarningFunc(("'%s' is not accessible (error='%s', rc=%Rhrc, vrc=%Rrc)\n",
|
---|
5231 | location.c_str(), m->strLastAccessError.c_str(),
|
---|
5232 | rc, vrc));
|
---|
5233 | }
|
---|
5234 |
|
---|
5235 | /* inform other callers if there are any */
|
---|
5236 | RTSemEventMultiSignal(m->queryInfoSem);
|
---|
5237 | m->queryInfoRunning = false;
|
---|
5238 |
|
---|
5239 | /* Set the proper state according to the result of the check */
|
---|
5240 | if (success)
|
---|
5241 | m->preLockState = MediumState_Created;
|
---|
5242 | else
|
---|
5243 | m->preLockState = MediumState_Inaccessible;
|
---|
5244 |
|
---|
5245 | HRESULT rc2;
|
---|
5246 | if (uOpenFlags & (VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SHAREABLE))
|
---|
5247 | rc2 = UnlockRead(NULL);
|
---|
5248 | else
|
---|
5249 | rc2 = UnlockWrite(NULL);
|
---|
5250 | if (SUCCEEDED(rc) && FAILED(rc2))
|
---|
5251 | rc = rc2;
|
---|
5252 | if (FAILED(rc)) return rc;
|
---|
5253 |
|
---|
5254 | /* If this is a base image which incorrectly has a parent UUID set,
|
---|
5255 | * repair the image now by zeroing the parent UUID. This is only done
|
---|
5256 | * when we have structural information from a config file, on import
|
---|
5257 | * this is not possible. If someone would accidentally call openMedium
|
---|
5258 | * with a diff image before the base is registered this would destroy
|
---|
5259 | * the diff. Not acceptable. */
|
---|
5260 | if (fRepairImageZeroParentUuid)
|
---|
5261 | {
|
---|
5262 | rc = LockWrite(NULL);
|
---|
5263 | if (FAILED(rc)) return rc;
|
---|
5264 |
|
---|
5265 | alock.leave();
|
---|
5266 |
|
---|
5267 | try
|
---|
5268 | {
|
---|
5269 | PVBOXHDD hdd;
|
---|
5270 | vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &hdd);
|
---|
5271 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
5272 |
|
---|
5273 | try
|
---|
5274 | {
|
---|
5275 | vrc = VDOpen(hdd,
|
---|
5276 | format.c_str(),
|
---|
5277 | location.c_str(),
|
---|
5278 | uOpenFlags & ~VD_OPEN_FLAGS_READONLY,
|
---|
5279 | m->vdImageIfaces);
|
---|
5280 | if (RT_FAILURE(vrc))
|
---|
5281 | throw S_OK;
|
---|
5282 |
|
---|
5283 | RTUUID zeroParentUuid;
|
---|
5284 | RTUuidClear(&zeroParentUuid);
|
---|
5285 | vrc = VDSetParentUuid(hdd, 0, &zeroParentUuid);
|
---|
5286 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
5287 | }
|
---|
5288 | catch (HRESULT aRC)
|
---|
5289 | {
|
---|
5290 | rc = aRC;
|
---|
5291 | }
|
---|
5292 |
|
---|
5293 | VDDestroy(hdd);
|
---|
5294 | }
|
---|
5295 | catch (HRESULT aRC)
|
---|
5296 | {
|
---|
5297 | rc = aRC;
|
---|
5298 | }
|
---|
5299 |
|
---|
5300 | alock.enter();
|
---|
5301 |
|
---|
5302 | rc = UnlockWrite(NULL);
|
---|
5303 | if (SUCCEEDED(rc) && FAILED(rc2))
|
---|
5304 | rc = rc2;
|
---|
5305 | if (FAILED(rc)) return rc;
|
---|
5306 | }
|
---|
5307 |
|
---|
5308 | return rc;
|
---|
5309 | }
|
---|
5310 |
|
---|
5311 | /**
|
---|
5312 | * Performs extra checks if the medium can be closed and returns S_OK in
|
---|
5313 | * this case. Otherwise, returns a respective error message. Called by
|
---|
5314 | * Close() under the medium tree lock and the medium lock.
|
---|
5315 | *
|
---|
5316 | * @note Also reused by Medium::Reset().
|
---|
5317 | *
|
---|
5318 | * @note Caller must hold the media tree write lock!
|
---|
5319 | */
|
---|
5320 | HRESULT Medium::canClose()
|
---|
5321 | {
|
---|
5322 | Assert(m->pVirtualBox->getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
5323 |
|
---|
5324 | if (getChildren().size() != 0)
|
---|
5325 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
5326 | tr("Cannot close medium '%s' because it has %d child media"),
|
---|
5327 | m->strLocationFull.c_str(), getChildren().size());
|
---|
5328 |
|
---|
5329 | return S_OK;
|
---|
5330 | }
|
---|
5331 |
|
---|
5332 | /**
|
---|
5333 | * Unregisters this medium with mVirtualBox. Called by close() under the medium tree lock.
|
---|
5334 | *
|
---|
5335 | * This calls either VirtualBox::unregisterImage or VirtualBox::unregisterHardDisk depending
|
---|
5336 | * on the device type of this medium.
|
---|
5337 | *
|
---|
5338 | * @param pllRegistriesThatNeedSaving Optional pointer to a list of UUIDs that will receive the registry IDs that need saving.
|
---|
5339 | *
|
---|
5340 | * @note Caller must have locked the media tree lock for writing!
|
---|
5341 | */
|
---|
5342 | HRESULT Medium::unregisterWithVirtualBox(GuidList *pllRegistriesThatNeedSaving)
|
---|
5343 | {
|
---|
5344 | /* Note that we need to de-associate ourselves from the parent to let
|
---|
5345 | * unregisterHardDisk() properly save the registry */
|
---|
5346 |
|
---|
5347 | /* we modify mParent and access children */
|
---|
5348 | Assert(m->pVirtualBox->getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
5349 |
|
---|
5350 | Medium *pParentBackup = m->pParent;
|
---|
5351 | AssertReturn(getChildren().size() == 0, E_FAIL);
|
---|
5352 | if (m->pParent)
|
---|
5353 | deparent();
|
---|
5354 |
|
---|
5355 | HRESULT rc = E_FAIL;
|
---|
5356 | switch (m->devType)
|
---|
5357 | {
|
---|
5358 | case DeviceType_DVD:
|
---|
5359 | case DeviceType_Floppy:
|
---|
5360 | rc = m->pVirtualBox->unregisterImage(this,
|
---|
5361 | m->devType,
|
---|
5362 | pllRegistriesThatNeedSaving);
|
---|
5363 | break;
|
---|
5364 |
|
---|
5365 | case DeviceType_HardDisk:
|
---|
5366 | rc = m->pVirtualBox->unregisterHardDisk(this, pllRegistriesThatNeedSaving);
|
---|
5367 | break;
|
---|
5368 |
|
---|
5369 | default:
|
---|
5370 | break;
|
---|
5371 | }
|
---|
5372 |
|
---|
5373 | if (FAILED(rc))
|
---|
5374 | {
|
---|
5375 | if (pParentBackup)
|
---|
5376 | {
|
---|
5377 | // re-associate with the parent as we are still relatives in the registry
|
---|
5378 | m->pParent = pParentBackup;
|
---|
5379 | m->pParent->m->llChildren.push_back(this);
|
---|
5380 | }
|
---|
5381 | }
|
---|
5382 |
|
---|
5383 | return rc;
|
---|
5384 | }
|
---|
5385 |
|
---|
5386 | /**
|
---|
5387 | * Sets the extended error info according to the current media state.
|
---|
5388 | *
|
---|
5389 | * @note Must be called from under this object's write or read lock.
|
---|
5390 | */
|
---|
5391 | HRESULT Medium::setStateError()
|
---|
5392 | {
|
---|
5393 | HRESULT rc = E_FAIL;
|
---|
5394 |
|
---|
5395 | switch (m->state)
|
---|
5396 | {
|
---|
5397 | case MediumState_NotCreated:
|
---|
5398 | {
|
---|
5399 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5400 | tr("Storage for the medium '%s' is not created"),
|
---|
5401 | m->strLocationFull.c_str());
|
---|
5402 | break;
|
---|
5403 | }
|
---|
5404 | case MediumState_Created:
|
---|
5405 | {
|
---|
5406 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5407 | tr("Storage for the medium '%s' is already created"),
|
---|
5408 | m->strLocationFull.c_str());
|
---|
5409 | break;
|
---|
5410 | }
|
---|
5411 | case MediumState_LockedRead:
|
---|
5412 | {
|
---|
5413 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5414 | tr("Medium '%s' is locked for reading by another task"),
|
---|
5415 | m->strLocationFull.c_str());
|
---|
5416 | break;
|
---|
5417 | }
|
---|
5418 | case MediumState_LockedWrite:
|
---|
5419 | {
|
---|
5420 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5421 | tr("Medium '%s' is locked for writing by another task"),
|
---|
5422 | m->strLocationFull.c_str());
|
---|
5423 | break;
|
---|
5424 | }
|
---|
5425 | case MediumState_Inaccessible:
|
---|
5426 | {
|
---|
5427 | /* be in sync with Console::powerUpThread() */
|
---|
5428 | if (!m->strLastAccessError.isEmpty())
|
---|
5429 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5430 | tr("Medium '%s' is not accessible. %s"),
|
---|
5431 | m->strLocationFull.c_str(), m->strLastAccessError.c_str());
|
---|
5432 | else
|
---|
5433 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5434 | tr("Medium '%s' is not accessible"),
|
---|
5435 | m->strLocationFull.c_str());
|
---|
5436 | break;
|
---|
5437 | }
|
---|
5438 | case MediumState_Creating:
|
---|
5439 | {
|
---|
5440 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5441 | tr("Storage for the medium '%s' is being created"),
|
---|
5442 | m->strLocationFull.c_str());
|
---|
5443 | break;
|
---|
5444 | }
|
---|
5445 | case MediumState_Deleting:
|
---|
5446 | {
|
---|
5447 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5448 | tr("Storage for the medium '%s' is being deleted"),
|
---|
5449 | m->strLocationFull.c_str());
|
---|
5450 | break;
|
---|
5451 | }
|
---|
5452 | default:
|
---|
5453 | {
|
---|
5454 | AssertFailed();
|
---|
5455 | break;
|
---|
5456 | }
|
---|
5457 | }
|
---|
5458 |
|
---|
5459 | return rc;
|
---|
5460 | }
|
---|
5461 |
|
---|
5462 | /**
|
---|
5463 | * Sets the value of m->strLocationFull. The given location must be a fully
|
---|
5464 | * qualified path; relative paths are not supported here.
|
---|
5465 | *
|
---|
5466 | * As a special exception, if the specified location is a file path that ends with '/'
|
---|
5467 | * then the file name part will be generated by this method automatically in the format
|
---|
5468 | * '{<uuid>}.<ext>' where <uuid> is a fresh UUID that this method will generate
|
---|
5469 | * and assign to this medium, and <ext> is the default extension for this
|
---|
5470 | * medium's storage format. Note that this procedure requires the media state to
|
---|
5471 | * be NotCreated and will return a failure otherwise.
|
---|
5472 | *
|
---|
5473 | * @param aLocation Location of the storage unit. If the location is a FS-path,
|
---|
5474 | * then it can be relative to the VirtualBox home directory.
|
---|
5475 | * @param aFormat Optional fallback format if it is an import and the format
|
---|
5476 | * cannot be determined.
|
---|
5477 | *
|
---|
5478 | * @note Must be called from under this object's write lock.
|
---|
5479 | */
|
---|
5480 | HRESULT Medium::setLocation(const Utf8Str &aLocation,
|
---|
5481 | const Utf8Str &aFormat /* = Utf8Str::Empty */)
|
---|
5482 | {
|
---|
5483 | AssertReturn(!aLocation.isEmpty(), E_FAIL);
|
---|
5484 |
|
---|
5485 | AutoCaller autoCaller(this);
|
---|
5486 | AssertComRCReturnRC(autoCaller.rc());
|
---|
5487 |
|
---|
5488 | /* formatObj may be null only when initializing from an existing path and
|
---|
5489 | * no format is known yet */
|
---|
5490 | AssertReturn( (!m->strFormat.isEmpty() && !m->formatObj.isNull())
|
---|
5491 | || ( autoCaller.state() == InInit
|
---|
5492 | && m->state != MediumState_NotCreated
|
---|
5493 | && m->id.isEmpty()
|
---|
5494 | && m->strFormat.isEmpty()
|
---|
5495 | && m->formatObj.isNull()),
|
---|
5496 | E_FAIL);
|
---|
5497 |
|
---|
5498 | /* are we dealing with a new medium constructed using the existing
|
---|
5499 | * location? */
|
---|
5500 | bool isImport = m->strFormat.isEmpty();
|
---|
5501 |
|
---|
5502 | if ( isImport
|
---|
5503 | || ( (m->formatObj->getCapabilities() & MediumFormatCapabilities_File)
|
---|
5504 | && !m->hostDrive))
|
---|
5505 | {
|
---|
5506 | Guid id;
|
---|
5507 |
|
---|
5508 | Utf8Str locationFull(aLocation);
|
---|
5509 |
|
---|
5510 | if (m->state == MediumState_NotCreated)
|
---|
5511 | {
|
---|
5512 | /* must be a file (formatObj must be already known) */
|
---|
5513 | Assert(m->formatObj->getCapabilities() & MediumFormatCapabilities_File);
|
---|
5514 |
|
---|
5515 | if (RTPathFilename(aLocation.c_str()) == NULL)
|
---|
5516 | {
|
---|
5517 | /* no file name is given (either an empty string or ends with a
|
---|
5518 | * slash), generate a new UUID + file name if the state allows
|
---|
5519 | * this */
|
---|
5520 |
|
---|
5521 | ComAssertMsgRet(!m->formatObj->getFileExtensions().empty(),
|
---|
5522 | ("Must be at least one extension if it is MediumFormatCapabilities_File\n"),
|
---|
5523 | E_FAIL);
|
---|
5524 |
|
---|
5525 | Utf8Str strExt = m->formatObj->getFileExtensions().front();
|
---|
5526 | ComAssertMsgRet(!strExt.isEmpty(),
|
---|
5527 | ("Default extension must not be empty\n"),
|
---|
5528 | E_FAIL);
|
---|
5529 |
|
---|
5530 | id.create();
|
---|
5531 |
|
---|
5532 | locationFull = Utf8StrFmt("%s{%RTuuid}.%s",
|
---|
5533 | aLocation.c_str(), id.raw(), strExt.c_str());
|
---|
5534 | }
|
---|
5535 | }
|
---|
5536 |
|
---|
5537 | // we must always have full paths now
|
---|
5538 | if (!RTPathStartsWithRoot(locationFull.c_str()))
|
---|
5539 | return setError(VBOX_E_FILE_ERROR,
|
---|
5540 | tr("The given path '%s' is not fully qualified"),
|
---|
5541 | locationFull.c_str());
|
---|
5542 |
|
---|
5543 | /* detect the backend from the storage unit if importing */
|
---|
5544 | if (isImport)
|
---|
5545 | {
|
---|
5546 | VDTYPE enmType = VDTYPE_INVALID;
|
---|
5547 | char *backendName = NULL;
|
---|
5548 |
|
---|
5549 | int vrc = VINF_SUCCESS;
|
---|
5550 |
|
---|
5551 | /* is it a file? */
|
---|
5552 | {
|
---|
5553 | RTFILE file;
|
---|
5554 | vrc = RTFileOpen(&file, locationFull.c_str(), RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
5555 | if (RT_SUCCESS(vrc))
|
---|
5556 | RTFileClose(file);
|
---|
5557 | }
|
---|
5558 | if (RT_SUCCESS(vrc))
|
---|
5559 | {
|
---|
5560 | vrc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
|
---|
5561 | locationFull.c_str(), &backendName, &enmType);
|
---|
5562 | }
|
---|
5563 | else if (vrc != VERR_FILE_NOT_FOUND && vrc != VERR_PATH_NOT_FOUND)
|
---|
5564 | {
|
---|
5565 | /* assume it's not a file, restore the original location */
|
---|
5566 | locationFull = aLocation;
|
---|
5567 | vrc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
|
---|
5568 | locationFull.c_str(), &backendName, &enmType);
|
---|
5569 | }
|
---|
5570 |
|
---|
5571 | if (RT_FAILURE(vrc))
|
---|
5572 | {
|
---|
5573 | if (vrc == VERR_FILE_NOT_FOUND || vrc == VERR_PATH_NOT_FOUND)
|
---|
5574 | return setError(VBOX_E_FILE_ERROR,
|
---|
5575 | tr("Could not find file for the medium '%s' (%Rrc)"),
|
---|
5576 | locationFull.c_str(), vrc);
|
---|
5577 | else if (aFormat.isEmpty())
|
---|
5578 | return setError(VBOX_E_IPRT_ERROR,
|
---|
5579 | tr("Could not get the storage format of the medium '%s' (%Rrc)"),
|
---|
5580 | locationFull.c_str(), vrc);
|
---|
5581 | else
|
---|
5582 | {
|
---|
5583 | HRESULT rc = setFormat(aFormat);
|
---|
5584 | /* setFormat() must not fail since we've just used the backend so
|
---|
5585 | * the format object must be there */
|
---|
5586 | AssertComRCReturnRC(rc);
|
---|
5587 | }
|
---|
5588 | }
|
---|
5589 | else if ( enmType == VDTYPE_INVALID
|
---|
5590 | || m->devType != convertToDeviceType(enmType))
|
---|
5591 | {
|
---|
5592 | /*
|
---|
5593 | * The user tried to use a image as a device which is not supported
|
---|
5594 | * by the backend.
|
---|
5595 | */
|
---|
5596 | return setError(E_FAIL,
|
---|
5597 | tr("The medium '%s' can't be used as the requested device type"),
|
---|
5598 | locationFull.c_str());
|
---|
5599 | }
|
---|
5600 | else
|
---|
5601 | {
|
---|
5602 | ComAssertRet(backendName != NULL && *backendName != '\0', E_FAIL);
|
---|
5603 |
|
---|
5604 | HRESULT rc = setFormat(backendName);
|
---|
5605 | RTStrFree(backendName);
|
---|
5606 |
|
---|
5607 | /* setFormat() must not fail since we've just used the backend so
|
---|
5608 | * the format object must be there */
|
---|
5609 | AssertComRCReturnRC(rc);
|
---|
5610 | }
|
---|
5611 | }
|
---|
5612 |
|
---|
5613 | m->strLocationFull = locationFull;
|
---|
5614 |
|
---|
5615 | /* is it still a file? */
|
---|
5616 | if ( (m->formatObj->getCapabilities() & MediumFormatCapabilities_File)
|
---|
5617 | && (m->state == MediumState_NotCreated)
|
---|
5618 | )
|
---|
5619 | /* assign a new UUID (this UUID will be used when calling
|
---|
5620 | * VDCreateBase/VDCreateDiff as a wanted UUID). Note that we
|
---|
5621 | * also do that if we didn't generate it to make sure it is
|
---|
5622 | * either generated by us or reset to null */
|
---|
5623 | unconst(m->id) = id;
|
---|
5624 | }
|
---|
5625 | else
|
---|
5626 | m->strLocationFull = aLocation;
|
---|
5627 |
|
---|
5628 | return S_OK;
|
---|
5629 | }
|
---|
5630 |
|
---|
5631 | /**
|
---|
5632 | * Checks that the format ID is valid and sets it on success.
|
---|
5633 | *
|
---|
5634 | * Note that this method will caller-reference the format object on success!
|
---|
5635 | * This reference must be released somewhere to let the MediumFormat object be
|
---|
5636 | * uninitialized.
|
---|
5637 | *
|
---|
5638 | * @note Must be called from under this object's write lock.
|
---|
5639 | */
|
---|
5640 | HRESULT Medium::setFormat(const Utf8Str &aFormat)
|
---|
5641 | {
|
---|
5642 | /* get the format object first */
|
---|
5643 | {
|
---|
5644 | SystemProperties *pSysProps = m->pVirtualBox->getSystemProperties();
|
---|
5645 | AutoReadLock propsLock(pSysProps COMMA_LOCKVAL_SRC_POS);
|
---|
5646 |
|
---|
5647 | unconst(m->formatObj) = pSysProps->mediumFormat(aFormat);
|
---|
5648 | if (m->formatObj.isNull())
|
---|
5649 | return setError(E_INVALIDARG,
|
---|
5650 | tr("Invalid medium storage format '%s'"),
|
---|
5651 | aFormat.c_str());
|
---|
5652 |
|
---|
5653 | /* reference the format permanently to prevent its unexpected
|
---|
5654 | * uninitialization */
|
---|
5655 | HRESULT rc = m->formatObj->addCaller();
|
---|
5656 | AssertComRCReturnRC(rc);
|
---|
5657 |
|
---|
5658 | /* get properties (preinsert them as keys in the map). Note that the
|
---|
5659 | * map doesn't grow over the object life time since the set of
|
---|
5660 | * properties is meant to be constant. */
|
---|
5661 |
|
---|
5662 | Assert(m->mapProperties.empty());
|
---|
5663 |
|
---|
5664 | for (MediumFormat::PropertyList::const_iterator it = m->formatObj->getProperties().begin();
|
---|
5665 | it != m->formatObj->getProperties().end();
|
---|
5666 | ++it)
|
---|
5667 | {
|
---|
5668 | m->mapProperties.insert(std::make_pair(it->strName, Utf8Str::Empty));
|
---|
5669 | }
|
---|
5670 | }
|
---|
5671 |
|
---|
5672 | unconst(m->strFormat) = aFormat;
|
---|
5673 |
|
---|
5674 | return S_OK;
|
---|
5675 | }
|
---|
5676 |
|
---|
5677 | /**
|
---|
5678 | * Converts the Medium device type to the VD type.
|
---|
5679 | */
|
---|
5680 | VDTYPE Medium::convertDeviceType()
|
---|
5681 | {
|
---|
5682 | VDTYPE enmType;
|
---|
5683 |
|
---|
5684 | switch (m->devType)
|
---|
5685 | {
|
---|
5686 | case DeviceType_HardDisk:
|
---|
5687 | enmType = VDTYPE_HDD;
|
---|
5688 | break;
|
---|
5689 | case DeviceType_DVD:
|
---|
5690 | enmType = VDTYPE_DVD;
|
---|
5691 | break;
|
---|
5692 | case DeviceType_Floppy:
|
---|
5693 | enmType = VDTYPE_FLOPPY;
|
---|
5694 | break;
|
---|
5695 | default:
|
---|
5696 | ComAssertFailedRet(VDTYPE_INVALID);
|
---|
5697 | }
|
---|
5698 |
|
---|
5699 | return enmType;
|
---|
5700 | }
|
---|
5701 |
|
---|
5702 | /**
|
---|
5703 | * Converts from the VD type to the medium type.
|
---|
5704 | */
|
---|
5705 | DeviceType_T Medium::convertToDeviceType(VDTYPE enmType)
|
---|
5706 | {
|
---|
5707 | DeviceType_T devType;
|
---|
5708 |
|
---|
5709 | switch (enmType)
|
---|
5710 | {
|
---|
5711 | case VDTYPE_HDD:
|
---|
5712 | devType = DeviceType_HardDisk;
|
---|
5713 | break;
|
---|
5714 | case VDTYPE_DVD:
|
---|
5715 | devType = DeviceType_DVD;
|
---|
5716 | break;
|
---|
5717 | case VDTYPE_FLOPPY:
|
---|
5718 | devType = DeviceType_Floppy;
|
---|
5719 | break;
|
---|
5720 | default:
|
---|
5721 | ComAssertFailedRet(DeviceType_Null);
|
---|
5722 | }
|
---|
5723 |
|
---|
5724 | return devType;
|
---|
5725 | }
|
---|
5726 |
|
---|
5727 | /**
|
---|
5728 | * Returns the last error message collected by the vdErrorCall callback and
|
---|
5729 | * resets it.
|
---|
5730 | *
|
---|
5731 | * The error message is returned prepended with a dot and a space, like this:
|
---|
5732 | * <code>
|
---|
5733 | * ". <error_text> (%Rrc)"
|
---|
5734 | * </code>
|
---|
5735 | * to make it easily appendable to a more general error message. The @c %Rrc
|
---|
5736 | * format string is given @a aVRC as an argument.
|
---|
5737 | *
|
---|
5738 | * If there is no last error message collected by vdErrorCall or if it is a
|
---|
5739 | * null or empty string, then this function returns the following text:
|
---|
5740 | * <code>
|
---|
5741 | * " (%Rrc)"
|
---|
5742 | * </code>
|
---|
5743 | *
|
---|
5744 | * @note Doesn't do any object locking; it is assumed that the caller makes sure
|
---|
5745 | * the callback isn't called by more than one thread at a time.
|
---|
5746 | *
|
---|
5747 | * @param aVRC VBox error code to use when no error message is provided.
|
---|
5748 | */
|
---|
5749 | Utf8Str Medium::vdError(int aVRC)
|
---|
5750 | {
|
---|
5751 | Utf8Str error;
|
---|
5752 |
|
---|
5753 | if (m->vdError.isEmpty())
|
---|
5754 | error = Utf8StrFmt(" (%Rrc)", aVRC);
|
---|
5755 | else
|
---|
5756 | error = Utf8StrFmt(".\n%s", m->vdError.c_str());
|
---|
5757 |
|
---|
5758 | m->vdError.setNull();
|
---|
5759 |
|
---|
5760 | return error;
|
---|
5761 | }
|
---|
5762 |
|
---|
5763 | /**
|
---|
5764 | * Error message callback.
|
---|
5765 | *
|
---|
5766 | * Puts the reported error message to the m->vdError field.
|
---|
5767 | *
|
---|
5768 | * @note Doesn't do any object locking; it is assumed that the caller makes sure
|
---|
5769 | * the callback isn't called by more than one thread at a time.
|
---|
5770 | *
|
---|
5771 | * @param pvUser The opaque data passed on container creation.
|
---|
5772 | * @param rc The VBox error code.
|
---|
5773 | * @param RT_SRC_POS_DECL Use RT_SRC_POS.
|
---|
5774 | * @param pszFormat Error message format string.
|
---|
5775 | * @param va Error message arguments.
|
---|
5776 | */
|
---|
5777 | /*static*/
|
---|
5778 | DECLCALLBACK(void) Medium::vdErrorCall(void *pvUser, int rc, RT_SRC_POS_DECL,
|
---|
5779 | const char *pszFormat, va_list va)
|
---|
5780 | {
|
---|
5781 | NOREF(pszFile); NOREF(iLine); NOREF(pszFunction); /* RT_SRC_POS_DECL */
|
---|
5782 |
|
---|
5783 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
5784 | AssertReturnVoid(that != NULL);
|
---|
5785 |
|
---|
5786 | if (that->m->vdError.isEmpty())
|
---|
5787 | that->m->vdError =
|
---|
5788 | Utf8StrFmt("%s (%Rrc)", Utf8Str(pszFormat, va).c_str(), rc);
|
---|
5789 | else
|
---|
5790 | that->m->vdError =
|
---|
5791 | Utf8StrFmt("%s.\n%s (%Rrc)", that->m->vdError.c_str(),
|
---|
5792 | Utf8Str(pszFormat, va).c_str(), rc);
|
---|
5793 | }
|
---|
5794 |
|
---|
5795 | /* static */
|
---|
5796 | DECLCALLBACK(bool) Medium::vdConfigAreKeysValid(void *pvUser,
|
---|
5797 | const char * /* pszzValid */)
|
---|
5798 | {
|
---|
5799 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
5800 | AssertReturn(that != NULL, false);
|
---|
5801 |
|
---|
5802 | /* we always return true since the only keys we have are those found in
|
---|
5803 | * VDBACKENDINFO */
|
---|
5804 | return true;
|
---|
5805 | }
|
---|
5806 |
|
---|
5807 | /* static */
|
---|
5808 | DECLCALLBACK(int) Medium::vdConfigQuerySize(void *pvUser,
|
---|
5809 | const char *pszName,
|
---|
5810 | size_t *pcbValue)
|
---|
5811 | {
|
---|
5812 | AssertReturn(VALID_PTR(pcbValue), VERR_INVALID_POINTER);
|
---|
5813 |
|
---|
5814 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
5815 | AssertReturn(that != NULL, VERR_GENERAL_FAILURE);
|
---|
5816 |
|
---|
5817 | settings::StringsMap::const_iterator it = that->m->mapProperties.find(Utf8Str(pszName));
|
---|
5818 | if (it == that->m->mapProperties.end())
|
---|
5819 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
5820 |
|
---|
5821 | /* we interpret null values as "no value" in Medium */
|
---|
5822 | if (it->second.isEmpty())
|
---|
5823 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
5824 |
|
---|
5825 | *pcbValue = it->second.length() + 1 /* include terminator */;
|
---|
5826 |
|
---|
5827 | return VINF_SUCCESS;
|
---|
5828 | }
|
---|
5829 |
|
---|
5830 | /* static */
|
---|
5831 | DECLCALLBACK(int) Medium::vdConfigQuery(void *pvUser,
|
---|
5832 | const char *pszName,
|
---|
5833 | char *pszValue,
|
---|
5834 | size_t cchValue)
|
---|
5835 | {
|
---|
5836 | AssertReturn(VALID_PTR(pszValue), VERR_INVALID_POINTER);
|
---|
5837 |
|
---|
5838 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
5839 | AssertReturn(that != NULL, VERR_GENERAL_FAILURE);
|
---|
5840 |
|
---|
5841 | settings::StringsMap::const_iterator it = that->m->mapProperties.find(Utf8Str(pszName));
|
---|
5842 | if (it == that->m->mapProperties.end())
|
---|
5843 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
5844 |
|
---|
5845 | /* we interpret null values as "no value" in Medium */
|
---|
5846 | if (it->second.isEmpty())
|
---|
5847 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
5848 |
|
---|
5849 | const Utf8Str &value = it->second;
|
---|
5850 | if (value.length() >= cchValue)
|
---|
5851 | return VERR_CFGM_NOT_ENOUGH_SPACE;
|
---|
5852 |
|
---|
5853 | memcpy(pszValue, value.c_str(), value.length() + 1);
|
---|
5854 |
|
---|
5855 | return VINF_SUCCESS;
|
---|
5856 | }
|
---|
5857 |
|
---|
5858 | DECLCALLBACK(int) Medium::vdTcpSocketCreate(uint32_t fFlags, PVDSOCKET pSock)
|
---|
5859 | {
|
---|
5860 | PVDSOCKETINT pSocketInt = NULL;
|
---|
5861 |
|
---|
5862 | if ((fFlags & VD_INTERFACETCPNET_CONNECT_EXTENDED_SELECT) != 0)
|
---|
5863 | return VERR_NOT_SUPPORTED;
|
---|
5864 |
|
---|
5865 | pSocketInt = (PVDSOCKETINT)RTMemAllocZ(sizeof(VDSOCKETINT));
|
---|
5866 | if (!pSocketInt)
|
---|
5867 | return VERR_NO_MEMORY;
|
---|
5868 |
|
---|
5869 | pSocketInt->hSocket = NIL_RTSOCKET;
|
---|
5870 | *pSock = pSocketInt;
|
---|
5871 | return VINF_SUCCESS;
|
---|
5872 | }
|
---|
5873 |
|
---|
5874 | DECLCALLBACK(int) Medium::vdTcpSocketDestroy(VDSOCKET Sock)
|
---|
5875 | {
|
---|
5876 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
5877 |
|
---|
5878 | if (pSocketInt->hSocket != NIL_RTSOCKET)
|
---|
5879 | RTTcpClientCloseEx(pSocketInt->hSocket, false /*fGracefulShutdown*/);
|
---|
5880 |
|
---|
5881 | RTMemFree(pSocketInt);
|
---|
5882 |
|
---|
5883 | return VINF_SUCCESS;
|
---|
5884 | }
|
---|
5885 |
|
---|
5886 | DECLCALLBACK(int) Medium::vdTcpClientConnect(VDSOCKET Sock, const char *pszAddress, uint32_t uPort)
|
---|
5887 | {
|
---|
5888 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
5889 |
|
---|
5890 | return RTTcpClientConnect(pszAddress, uPort, &pSocketInt->hSocket);
|
---|
5891 | }
|
---|
5892 |
|
---|
5893 | DECLCALLBACK(int) Medium::vdTcpClientClose(VDSOCKET Sock)
|
---|
5894 | {
|
---|
5895 | int rc = VINF_SUCCESS;
|
---|
5896 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
5897 |
|
---|
5898 | rc = RTTcpClientCloseEx(pSocketInt->hSocket, false /*fGracefulShutdown*/);
|
---|
5899 | pSocketInt->hSocket = NIL_RTSOCKET;
|
---|
5900 | return rc;
|
---|
5901 | }
|
---|
5902 |
|
---|
5903 | DECLCALLBACK(bool) Medium::vdTcpIsClientConnected(VDSOCKET Sock)
|
---|
5904 | {
|
---|
5905 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
5906 | return pSocketInt->hSocket != NIL_RTSOCKET;
|
---|
5907 | }
|
---|
5908 |
|
---|
5909 | DECLCALLBACK(int) Medium::vdTcpSelectOne(VDSOCKET Sock, RTMSINTERVAL cMillies)
|
---|
5910 | {
|
---|
5911 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
5912 | return RTTcpSelectOne(pSocketInt->hSocket, cMillies);
|
---|
5913 | }
|
---|
5914 |
|
---|
5915 | DECLCALLBACK(int) Medium::vdTcpRead(VDSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead)
|
---|
5916 | {
|
---|
5917 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
5918 | return RTTcpRead(pSocketInt->hSocket, pvBuffer, cbBuffer, pcbRead);
|
---|
5919 | }
|
---|
5920 |
|
---|
5921 | DECLCALLBACK(int) Medium::vdTcpWrite(VDSOCKET Sock, const void *pvBuffer, size_t cbBuffer)
|
---|
5922 | {
|
---|
5923 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
5924 | return RTTcpWrite(pSocketInt->hSocket, pvBuffer, cbBuffer);
|
---|
5925 | }
|
---|
5926 |
|
---|
5927 | DECLCALLBACK(int) Medium::vdTcpSgWrite(VDSOCKET Sock, PCRTSGBUF pSgBuf)
|
---|
5928 | {
|
---|
5929 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
5930 | return RTTcpSgWrite(pSocketInt->hSocket, pSgBuf);
|
---|
5931 | }
|
---|
5932 |
|
---|
5933 | DECLCALLBACK(int) Medium::vdTcpFlush(VDSOCKET Sock)
|
---|
5934 | {
|
---|
5935 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
5936 | return RTTcpFlush(pSocketInt->hSocket);
|
---|
5937 | }
|
---|
5938 |
|
---|
5939 | DECLCALLBACK(int) Medium::vdTcpSetSendCoalescing(VDSOCKET Sock, bool fEnable)
|
---|
5940 | {
|
---|
5941 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
5942 | return RTTcpSetSendCoalescing(pSocketInt->hSocket, fEnable);
|
---|
5943 | }
|
---|
5944 |
|
---|
5945 | DECLCALLBACK(int) Medium::vdTcpGetLocalAddress(VDSOCKET Sock, PRTNETADDR pAddr)
|
---|
5946 | {
|
---|
5947 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
5948 | return RTTcpGetLocalAddress(pSocketInt->hSocket, pAddr);
|
---|
5949 | }
|
---|
5950 |
|
---|
5951 | DECLCALLBACK(int) Medium::vdTcpGetPeerAddress(VDSOCKET Sock, PRTNETADDR pAddr)
|
---|
5952 | {
|
---|
5953 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
5954 | return RTTcpGetPeerAddress(pSocketInt->hSocket, pAddr);
|
---|
5955 | }
|
---|
5956 |
|
---|
5957 | /**
|
---|
5958 | * Starts a new thread driven by the appropriate Medium::Task::handler() method.
|
---|
5959 | *
|
---|
5960 | * @note When the task is executed by this method, IProgress::notifyComplete()
|
---|
5961 | * is automatically called for the progress object associated with this
|
---|
5962 | * task when the task is finished to signal the operation completion for
|
---|
5963 | * other threads asynchronously waiting for it.
|
---|
5964 | */
|
---|
5965 | HRESULT Medium::startThread(Medium::Task *pTask)
|
---|
5966 | {
|
---|
5967 | #ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
|
---|
5968 | /* Extreme paranoia: The calling thread should not hold the medium
|
---|
5969 | * tree lock or any medium lock. Since there is no separate lock class
|
---|
5970 | * for medium objects be even more strict: no other object locks. */
|
---|
5971 | Assert(!AutoLockHoldsLocksInClass(LOCKCLASS_LISTOFMEDIA));
|
---|
5972 | Assert(!AutoLockHoldsLocksInClass(getLockingClass()));
|
---|
5973 | #endif
|
---|
5974 |
|
---|
5975 | /// @todo use a more descriptive task name
|
---|
5976 | int vrc = RTThreadCreate(NULL, Medium::Task::fntMediumTask, pTask,
|
---|
5977 | 0, RTTHREADTYPE_MAIN_HEAVY_WORKER, 0,
|
---|
5978 | "Medium::Task");
|
---|
5979 | if (RT_FAILURE(vrc))
|
---|
5980 | {
|
---|
5981 | delete pTask;
|
---|
5982 | return setError(E_FAIL, "Could not create Medium::Task thread (%Rrc)\n", vrc);
|
---|
5983 | }
|
---|
5984 |
|
---|
5985 | return S_OK;
|
---|
5986 | }
|
---|
5987 |
|
---|
5988 | /**
|
---|
5989 | * Runs Medium::Task::handler() on the current thread instead of creating
|
---|
5990 | * a new one.
|
---|
5991 | *
|
---|
5992 | * This call implies that it is made on another temporary thread created for
|
---|
5993 | * some asynchronous task. Avoid calling it from a normal thread since the task
|
---|
5994 | * operations are potentially lengthy and will block the calling thread in this
|
---|
5995 | * case.
|
---|
5996 | *
|
---|
5997 | * @note When the task is executed by this method, IProgress::notifyComplete()
|
---|
5998 | * is not called for the progress object associated with this task when
|
---|
5999 | * the task is finished. Instead, the result of the operation is returned
|
---|
6000 | * by this method directly and it's the caller's responsibility to
|
---|
6001 | * complete the progress object in this case.
|
---|
6002 | */
|
---|
6003 | HRESULT Medium::runNow(Medium::Task *pTask,
|
---|
6004 | GuidList *pllRegistriesThatNeedSaving)
|
---|
6005 | {
|
---|
6006 | #ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
|
---|
6007 | /* Extreme paranoia: The calling thread should not hold the medium
|
---|
6008 | * tree lock or any medium lock. Since there is no separate lock class
|
---|
6009 | * for medium objects be even more strict: no other object locks. */
|
---|
6010 | Assert(!AutoLockHoldsLocksInClass(LOCKCLASS_LISTOFMEDIA));
|
---|
6011 | Assert(!AutoLockHoldsLocksInClass(getLockingClass()));
|
---|
6012 | #endif
|
---|
6013 |
|
---|
6014 | pTask->m_pllRegistriesThatNeedSaving = pllRegistriesThatNeedSaving;
|
---|
6015 |
|
---|
6016 | /* NIL_RTTHREAD indicates synchronous call. */
|
---|
6017 | return (HRESULT)Medium::Task::fntMediumTask(NIL_RTTHREAD, pTask);
|
---|
6018 | }
|
---|
6019 |
|
---|
6020 | /**
|
---|
6021 | * Implementation code for the "create base" task.
|
---|
6022 | *
|
---|
6023 | * This only gets started from Medium::CreateBaseStorage() and always runs
|
---|
6024 | * asynchronously. As a result, we always save the VirtualBox.xml file when
|
---|
6025 | * we're done here.
|
---|
6026 | *
|
---|
6027 | * @param task
|
---|
6028 | * @return
|
---|
6029 | */
|
---|
6030 | HRESULT Medium::taskCreateBaseHandler(Medium::CreateBaseTask &task)
|
---|
6031 | {
|
---|
6032 | HRESULT rc = S_OK;
|
---|
6033 |
|
---|
6034 | /* these parameters we need after creation */
|
---|
6035 | uint64_t size = 0, logicalSize = 0;
|
---|
6036 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
6037 | bool fGenerateUuid = false;
|
---|
6038 |
|
---|
6039 | try
|
---|
6040 | {
|
---|
6041 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6042 |
|
---|
6043 | /* The object may request a specific UUID (through a special form of
|
---|
6044 | * the setLocation() argument). Otherwise we have to generate it */
|
---|
6045 | Guid id = m->id;
|
---|
6046 | fGenerateUuid = id.isEmpty();
|
---|
6047 | if (fGenerateUuid)
|
---|
6048 | {
|
---|
6049 | id.create();
|
---|
6050 | /* VirtualBox::registerHardDisk() will need UUID */
|
---|
6051 | unconst(m->id) = id;
|
---|
6052 | }
|
---|
6053 |
|
---|
6054 | Utf8Str format(m->strFormat);
|
---|
6055 | Utf8Str location(m->strLocationFull);
|
---|
6056 | uint64_t capabilities = m->formatObj->getCapabilities();
|
---|
6057 | ComAssertThrow(capabilities & ( VD_CAP_CREATE_FIXED
|
---|
6058 | | VD_CAP_CREATE_DYNAMIC), E_FAIL);
|
---|
6059 | Assert(m->state == MediumState_Creating);
|
---|
6060 |
|
---|
6061 | PVBOXHDD hdd;
|
---|
6062 | int vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &hdd);
|
---|
6063 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6064 |
|
---|
6065 | /* unlock before the potentially lengthy operation */
|
---|
6066 | thisLock.release();
|
---|
6067 |
|
---|
6068 | try
|
---|
6069 | {
|
---|
6070 | /* ensure the directory exists */
|
---|
6071 | rc = VirtualBox::ensureFilePathExists(location);
|
---|
6072 | if (FAILED(rc))
|
---|
6073 | throw rc;
|
---|
6074 |
|
---|
6075 | VDGEOMETRY geo = { 0, 0, 0 }; /* auto-detect */
|
---|
6076 |
|
---|
6077 | vrc = VDCreateBase(hdd,
|
---|
6078 | format.c_str(),
|
---|
6079 | location.c_str(),
|
---|
6080 | task.mSize,
|
---|
6081 | task.mVariant,
|
---|
6082 | NULL,
|
---|
6083 | &geo,
|
---|
6084 | &geo,
|
---|
6085 | id.raw(),
|
---|
6086 | VD_OPEN_FLAGS_NORMAL,
|
---|
6087 | m->vdImageIfaces,
|
---|
6088 | task.mVDOperationIfaces);
|
---|
6089 | if (RT_FAILURE(vrc))
|
---|
6090 | throw setError(VBOX_E_FILE_ERROR,
|
---|
6091 | tr("Could not create the medium storage unit '%s'%s"),
|
---|
6092 | location.c_str(), vdError(vrc).c_str());
|
---|
6093 |
|
---|
6094 | size = VDGetFileSize(hdd, 0);
|
---|
6095 | logicalSize = VDGetSize(hdd, 0);
|
---|
6096 | unsigned uImageFlags;
|
---|
6097 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
6098 | if (RT_SUCCESS(vrc))
|
---|
6099 | variant = (MediumVariant_T)uImageFlags;
|
---|
6100 | }
|
---|
6101 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6102 |
|
---|
6103 | VDDestroy(hdd);
|
---|
6104 | }
|
---|
6105 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6106 |
|
---|
6107 | if (SUCCEEDED(rc))
|
---|
6108 | {
|
---|
6109 | /* register with mVirtualBox as the last step and move to
|
---|
6110 | * Created state only on success (leaving an orphan file is
|
---|
6111 | * better than breaking media registry consistency) */
|
---|
6112 | AutoWriteLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
6113 | rc = m->pVirtualBox->registerHardDisk(this, NULL /* pllRegistriesThatNeedSaving */);
|
---|
6114 | }
|
---|
6115 |
|
---|
6116 | // reenter the lock before changing state
|
---|
6117 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6118 |
|
---|
6119 | if (SUCCEEDED(rc))
|
---|
6120 | {
|
---|
6121 | m->state = MediumState_Created;
|
---|
6122 |
|
---|
6123 | m->size = size;
|
---|
6124 | m->logicalSize = logicalSize;
|
---|
6125 | m->variant = variant;
|
---|
6126 | }
|
---|
6127 | else
|
---|
6128 | {
|
---|
6129 | /* back to NotCreated on failure */
|
---|
6130 | m->state = MediumState_NotCreated;
|
---|
6131 |
|
---|
6132 | /* reset UUID to prevent it from being reused next time */
|
---|
6133 | if (fGenerateUuid)
|
---|
6134 | unconst(m->id).clear();
|
---|
6135 | }
|
---|
6136 |
|
---|
6137 | return rc;
|
---|
6138 | }
|
---|
6139 |
|
---|
6140 | /**
|
---|
6141 | * Implementation code for the "create diff" task.
|
---|
6142 | *
|
---|
6143 | * This task always gets started from Medium::createDiffStorage() and can run
|
---|
6144 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
6145 | * that function. If we run synchronously, the caller expects the bool
|
---|
6146 | * *pfNeedsGlobalSaveSettings to be set before returning; otherwise (in asynchronous
|
---|
6147 | * mode), we save the settings ourselves.
|
---|
6148 | *
|
---|
6149 | * @param task
|
---|
6150 | * @return
|
---|
6151 | */
|
---|
6152 | HRESULT Medium::taskCreateDiffHandler(Medium::CreateDiffTask &task)
|
---|
6153 | {
|
---|
6154 | HRESULT rc = S_OK;
|
---|
6155 |
|
---|
6156 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
6157 |
|
---|
6158 | uint64_t size = 0, logicalSize = 0;
|
---|
6159 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
6160 | bool fGenerateUuid = false;
|
---|
6161 |
|
---|
6162 | GuidList llRegistriesThatNeedSaving;
|
---|
6163 |
|
---|
6164 | try
|
---|
6165 | {
|
---|
6166 | /* Lock both in {parent,child} order. */
|
---|
6167 | AutoMultiWriteLock2 mediaLock(this, pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
6168 |
|
---|
6169 | /* The object may request a specific UUID (through a special form of
|
---|
6170 | * the setLocation() argument). Otherwise we have to generate it */
|
---|
6171 | Guid targetId = pTarget->m->id;
|
---|
6172 | fGenerateUuid = targetId.isEmpty();
|
---|
6173 | if (fGenerateUuid)
|
---|
6174 | {
|
---|
6175 | targetId.create();
|
---|
6176 | /* VirtualBox::registerHardDisk() will need UUID */
|
---|
6177 | unconst(pTarget->m->id) = targetId;
|
---|
6178 | }
|
---|
6179 |
|
---|
6180 | Guid id = m->id;
|
---|
6181 |
|
---|
6182 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
6183 | Utf8Str targetLocation(pTarget->m->strLocationFull);
|
---|
6184 | uint64_t capabilities = m->formatObj->getCapabilities();
|
---|
6185 | ComAssertThrow(capabilities & VD_CAP_CREATE_DYNAMIC, E_FAIL);
|
---|
6186 |
|
---|
6187 | Assert(pTarget->m->state == MediumState_Creating);
|
---|
6188 | Assert(m->state == MediumState_LockedRead);
|
---|
6189 |
|
---|
6190 | PVBOXHDD hdd;
|
---|
6191 | int vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &hdd);
|
---|
6192 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6193 |
|
---|
6194 | /* the two media are now protected by their non-default states;
|
---|
6195 | * unlock the media before the potentially lengthy operation */
|
---|
6196 | mediaLock.release();
|
---|
6197 |
|
---|
6198 | try
|
---|
6199 | {
|
---|
6200 | /* Open all media in the target chain but the last. */
|
---|
6201 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
6202 | task.mpMediumLockList->GetBegin();
|
---|
6203 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
6204 | task.mpMediumLockList->GetEnd();
|
---|
6205 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
6206 | it != targetListEnd;
|
---|
6207 | ++it)
|
---|
6208 | {
|
---|
6209 | const MediumLock &mediumLock = *it;
|
---|
6210 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
6211 |
|
---|
6212 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
6213 |
|
---|
6214 | /* Skip over the target diff medium */
|
---|
6215 | if (pMedium->m->state == MediumState_Creating)
|
---|
6216 | continue;
|
---|
6217 |
|
---|
6218 | /* sanity check */
|
---|
6219 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
6220 |
|
---|
6221 | /* Open all media in appropriate mode. */
|
---|
6222 | vrc = VDOpen(hdd,
|
---|
6223 | pMedium->m->strFormat.c_str(),
|
---|
6224 | pMedium->m->strLocationFull.c_str(),
|
---|
6225 | VD_OPEN_FLAGS_READONLY,
|
---|
6226 | pMedium->m->vdImageIfaces);
|
---|
6227 | if (RT_FAILURE(vrc))
|
---|
6228 | throw setError(VBOX_E_FILE_ERROR,
|
---|
6229 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
6230 | pMedium->m->strLocationFull.c_str(),
|
---|
6231 | vdError(vrc).c_str());
|
---|
6232 | }
|
---|
6233 |
|
---|
6234 | /* ensure the target directory exists */
|
---|
6235 | rc = VirtualBox::ensureFilePathExists(targetLocation);
|
---|
6236 | if (FAILED(rc))
|
---|
6237 | throw rc;
|
---|
6238 |
|
---|
6239 | vrc = VDCreateDiff(hdd,
|
---|
6240 | targetFormat.c_str(),
|
---|
6241 | targetLocation.c_str(),
|
---|
6242 | task.mVariant | VD_IMAGE_FLAGS_DIFF,
|
---|
6243 | NULL,
|
---|
6244 | targetId.raw(),
|
---|
6245 | id.raw(),
|
---|
6246 | VD_OPEN_FLAGS_NORMAL,
|
---|
6247 | pTarget->m->vdImageIfaces,
|
---|
6248 | task.mVDOperationIfaces);
|
---|
6249 | if (RT_FAILURE(vrc))
|
---|
6250 | throw setError(VBOX_E_FILE_ERROR,
|
---|
6251 | tr("Could not create the differencing medium storage unit '%s'%s"),
|
---|
6252 | targetLocation.c_str(), vdError(vrc).c_str());
|
---|
6253 |
|
---|
6254 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
6255 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
6256 | unsigned uImageFlags;
|
---|
6257 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
6258 | if (RT_SUCCESS(vrc))
|
---|
6259 | variant = (MediumVariant_T)uImageFlags;
|
---|
6260 | }
|
---|
6261 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6262 |
|
---|
6263 | VDDestroy(hdd);
|
---|
6264 | }
|
---|
6265 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6266 |
|
---|
6267 | if (SUCCEEDED(rc))
|
---|
6268 | {
|
---|
6269 | AutoWriteLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
6270 |
|
---|
6271 | Assert(pTarget->m->pParent.isNull());
|
---|
6272 |
|
---|
6273 | /* associate the child with the parent */
|
---|
6274 | pTarget->m->pParent = this;
|
---|
6275 | m->llChildren.push_back(pTarget);
|
---|
6276 |
|
---|
6277 | /** @todo r=klaus neither target nor base() are locked,
|
---|
6278 | * potential race! */
|
---|
6279 | /* diffs for immutable media are auto-reset by default */
|
---|
6280 | pTarget->m->autoReset = (getBase()->m->type == MediumType_Immutable);
|
---|
6281 |
|
---|
6282 | /* register with mVirtualBox as the last step and move to
|
---|
6283 | * Created state only on success (leaving an orphan file is
|
---|
6284 | * better than breaking media registry consistency) */
|
---|
6285 | rc = m->pVirtualBox->registerHardDisk(pTarget, &llRegistriesThatNeedSaving);
|
---|
6286 |
|
---|
6287 | if (FAILED(rc))
|
---|
6288 | /* break the parent association on failure to register */
|
---|
6289 | deparent();
|
---|
6290 | }
|
---|
6291 |
|
---|
6292 | AutoMultiWriteLock2 mediaLock(this, pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
6293 |
|
---|
6294 | if (SUCCEEDED(rc))
|
---|
6295 | {
|
---|
6296 | pTarget->m->state = MediumState_Created;
|
---|
6297 |
|
---|
6298 | pTarget->m->size = size;
|
---|
6299 | pTarget->m->logicalSize = logicalSize;
|
---|
6300 | pTarget->m->variant = variant;
|
---|
6301 | }
|
---|
6302 | else
|
---|
6303 | {
|
---|
6304 | /* back to NotCreated on failure */
|
---|
6305 | pTarget->m->state = MediumState_NotCreated;
|
---|
6306 |
|
---|
6307 | pTarget->m->autoReset = false;
|
---|
6308 |
|
---|
6309 | /* reset UUID to prevent it from being reused next time */
|
---|
6310 | if (fGenerateUuid)
|
---|
6311 | unconst(pTarget->m->id).clear();
|
---|
6312 | }
|
---|
6313 |
|
---|
6314 | // deregister the task registered in createDiffStorage()
|
---|
6315 | Assert(m->numCreateDiffTasks != 0);
|
---|
6316 | --m->numCreateDiffTasks;
|
---|
6317 |
|
---|
6318 | if (task.isAsync())
|
---|
6319 | {
|
---|
6320 | mediaLock.release();
|
---|
6321 | m->pVirtualBox->saveRegistries(llRegistriesThatNeedSaving);
|
---|
6322 | }
|
---|
6323 | else
|
---|
6324 | // synchronous mode: report save settings result to caller
|
---|
6325 | if (task.m_pllRegistriesThatNeedSaving)
|
---|
6326 | *task.m_pllRegistriesThatNeedSaving = llRegistriesThatNeedSaving;
|
---|
6327 |
|
---|
6328 | /* Note that in sync mode, it's the caller's responsibility to
|
---|
6329 | * unlock the medium. */
|
---|
6330 |
|
---|
6331 | return rc;
|
---|
6332 | }
|
---|
6333 |
|
---|
6334 | /**
|
---|
6335 | * Implementation code for the "merge" task.
|
---|
6336 | *
|
---|
6337 | * This task always gets started from Medium::mergeTo() and can run
|
---|
6338 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
6339 | * that function. If we run synchronously, the caller expects the bool
|
---|
6340 | * *pfNeedsGlobalSaveSettings to be set before returning; otherwise (in asynchronous
|
---|
6341 | * mode), we save the settings ourselves.
|
---|
6342 | *
|
---|
6343 | * @param task
|
---|
6344 | * @return
|
---|
6345 | */
|
---|
6346 | HRESULT Medium::taskMergeHandler(Medium::MergeTask &task)
|
---|
6347 | {
|
---|
6348 | HRESULT rc = S_OK;
|
---|
6349 |
|
---|
6350 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
6351 |
|
---|
6352 | try
|
---|
6353 | {
|
---|
6354 | PVBOXHDD hdd;
|
---|
6355 | int vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &hdd);
|
---|
6356 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6357 |
|
---|
6358 | try
|
---|
6359 | {
|
---|
6360 | // Similar code appears in SessionMachine::onlineMergeMedium, so
|
---|
6361 | // if you make any changes below check whether they are applicable
|
---|
6362 | // in that context as well.
|
---|
6363 |
|
---|
6364 | unsigned uTargetIdx = VD_LAST_IMAGE;
|
---|
6365 | unsigned uSourceIdx = VD_LAST_IMAGE;
|
---|
6366 | /* Open all media in the chain. */
|
---|
6367 | MediumLockList::Base::iterator lockListBegin =
|
---|
6368 | task.mpMediumLockList->GetBegin();
|
---|
6369 | MediumLockList::Base::iterator lockListEnd =
|
---|
6370 | task.mpMediumLockList->GetEnd();
|
---|
6371 | unsigned i = 0;
|
---|
6372 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
6373 | it != lockListEnd;
|
---|
6374 | ++it)
|
---|
6375 | {
|
---|
6376 | MediumLock &mediumLock = *it;
|
---|
6377 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
6378 |
|
---|
6379 | if (pMedium == this)
|
---|
6380 | uSourceIdx = i;
|
---|
6381 | else if (pMedium == pTarget)
|
---|
6382 | uTargetIdx = i;
|
---|
6383 |
|
---|
6384 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
6385 |
|
---|
6386 | /*
|
---|
6387 | * complex sanity (sane complexity)
|
---|
6388 | *
|
---|
6389 | * The current medium must be in the Deleting (medium is merged)
|
---|
6390 | * or LockedRead (parent medium) state if it is not the target.
|
---|
6391 | * If it is the target it must be in the LockedWrite state.
|
---|
6392 | */
|
---|
6393 | Assert( ( pMedium != pTarget
|
---|
6394 | && ( pMedium->m->state == MediumState_Deleting
|
---|
6395 | || pMedium->m->state == MediumState_LockedRead))
|
---|
6396 | || ( pMedium == pTarget
|
---|
6397 | && pMedium->m->state == MediumState_LockedWrite));
|
---|
6398 |
|
---|
6399 | /*
|
---|
6400 | * Medium must be the target, in the LockedRead state
|
---|
6401 | * or Deleting state where it is not allowed to be attached
|
---|
6402 | * to a virtual machine.
|
---|
6403 | */
|
---|
6404 | Assert( pMedium == pTarget
|
---|
6405 | || pMedium->m->state == MediumState_LockedRead
|
---|
6406 | || ( pMedium->m->backRefs.size() == 0
|
---|
6407 | && pMedium->m->state == MediumState_Deleting));
|
---|
6408 | /* The source medium must be in Deleting state. */
|
---|
6409 | Assert( pMedium != this
|
---|
6410 | || pMedium->m->state == MediumState_Deleting);
|
---|
6411 |
|
---|
6412 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
6413 |
|
---|
6414 | if ( pMedium->m->state == MediumState_LockedRead
|
---|
6415 | || pMedium->m->state == MediumState_Deleting)
|
---|
6416 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
6417 | if (pMedium->m->type == MediumType_Shareable)
|
---|
6418 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
6419 |
|
---|
6420 | /* Open the medium */
|
---|
6421 | vrc = VDOpen(hdd,
|
---|
6422 | pMedium->m->strFormat.c_str(),
|
---|
6423 | pMedium->m->strLocationFull.c_str(),
|
---|
6424 | uOpenFlags,
|
---|
6425 | pMedium->m->vdImageIfaces);
|
---|
6426 | if (RT_FAILURE(vrc))
|
---|
6427 | throw vrc;
|
---|
6428 |
|
---|
6429 | i++;
|
---|
6430 | }
|
---|
6431 |
|
---|
6432 | ComAssertThrow( uSourceIdx != VD_LAST_IMAGE
|
---|
6433 | && uTargetIdx != VD_LAST_IMAGE, E_FAIL);
|
---|
6434 |
|
---|
6435 | vrc = VDMerge(hdd, uSourceIdx, uTargetIdx,
|
---|
6436 | task.mVDOperationIfaces);
|
---|
6437 | if (RT_FAILURE(vrc))
|
---|
6438 | throw vrc;
|
---|
6439 |
|
---|
6440 | /* update parent UUIDs */
|
---|
6441 | if (!task.mfMergeForward)
|
---|
6442 | {
|
---|
6443 | /* we need to update UUIDs of all source's children
|
---|
6444 | * which cannot be part of the container at once so
|
---|
6445 | * add each one in there individually */
|
---|
6446 | if (task.mChildrenToReparent.size() > 0)
|
---|
6447 | {
|
---|
6448 | for (MediaList::const_iterator it = task.mChildrenToReparent.begin();
|
---|
6449 | it != task.mChildrenToReparent.end();
|
---|
6450 | ++it)
|
---|
6451 | {
|
---|
6452 | /* VD_OPEN_FLAGS_INFO since UUID is wrong yet */
|
---|
6453 | vrc = VDOpen(hdd,
|
---|
6454 | (*it)->m->strFormat.c_str(),
|
---|
6455 | (*it)->m->strLocationFull.c_str(),
|
---|
6456 | VD_OPEN_FLAGS_INFO,
|
---|
6457 | (*it)->m->vdImageIfaces);
|
---|
6458 | if (RT_FAILURE(vrc))
|
---|
6459 | throw vrc;
|
---|
6460 |
|
---|
6461 | vrc = VDSetParentUuid(hdd, VD_LAST_IMAGE,
|
---|
6462 | pTarget->m->id.raw());
|
---|
6463 | if (RT_FAILURE(vrc))
|
---|
6464 | throw vrc;
|
---|
6465 |
|
---|
6466 | vrc = VDClose(hdd, false /* fDelete */);
|
---|
6467 | if (RT_FAILURE(vrc))
|
---|
6468 | throw vrc;
|
---|
6469 |
|
---|
6470 | (*it)->UnlockWrite(NULL);
|
---|
6471 | }
|
---|
6472 | }
|
---|
6473 | }
|
---|
6474 | }
|
---|
6475 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6476 | catch (int aVRC)
|
---|
6477 | {
|
---|
6478 | throw setError(VBOX_E_FILE_ERROR,
|
---|
6479 | tr("Could not merge the medium '%s' to '%s'%s"),
|
---|
6480 | m->strLocationFull.c_str(),
|
---|
6481 | pTarget->m->strLocationFull.c_str(),
|
---|
6482 | vdError(aVRC).c_str());
|
---|
6483 | }
|
---|
6484 |
|
---|
6485 | VDDestroy(hdd);
|
---|
6486 | }
|
---|
6487 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6488 |
|
---|
6489 | HRESULT rc2;
|
---|
6490 |
|
---|
6491 | if (SUCCEEDED(rc))
|
---|
6492 | {
|
---|
6493 | /* all media but the target were successfully deleted by
|
---|
6494 | * VDMerge; reparent the last one and uninitialize deleted media. */
|
---|
6495 |
|
---|
6496 | AutoWriteLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
6497 |
|
---|
6498 | if (task.mfMergeForward)
|
---|
6499 | {
|
---|
6500 | /* first, unregister the target since it may become a base
|
---|
6501 | * medium which needs re-registration */
|
---|
6502 | rc2 = m->pVirtualBox->unregisterHardDisk(pTarget, NULL /*&fNeedsGlobalSaveSettings*/);
|
---|
6503 | AssertComRC(rc2);
|
---|
6504 |
|
---|
6505 | /* then, reparent it and disconnect the deleted branch at
|
---|
6506 | * both ends (chain->parent() is source's parent) */
|
---|
6507 | pTarget->deparent();
|
---|
6508 | pTarget->m->pParent = task.mParentForTarget;
|
---|
6509 | if (pTarget->m->pParent)
|
---|
6510 | {
|
---|
6511 | pTarget->m->pParent->m->llChildren.push_back(pTarget);
|
---|
6512 | deparent();
|
---|
6513 | }
|
---|
6514 |
|
---|
6515 | /* then, register again */
|
---|
6516 | rc2 = m->pVirtualBox->registerHardDisk(pTarget, NULL /* pllRegistriesThatNeedSaving */ );
|
---|
6517 | AssertComRC(rc2);
|
---|
6518 | }
|
---|
6519 | else
|
---|
6520 | {
|
---|
6521 | Assert(pTarget->getChildren().size() == 1);
|
---|
6522 | Medium *targetChild = pTarget->getChildren().front();
|
---|
6523 |
|
---|
6524 | /* disconnect the deleted branch at the elder end */
|
---|
6525 | targetChild->deparent();
|
---|
6526 |
|
---|
6527 | /* reparent source's children and disconnect the deleted
|
---|
6528 | * branch at the younger end */
|
---|
6529 | if (task.mChildrenToReparent.size() > 0)
|
---|
6530 | {
|
---|
6531 | /* obey {parent,child} lock order */
|
---|
6532 | AutoWriteLock sourceLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6533 |
|
---|
6534 | for (MediaList::const_iterator it = task.mChildrenToReparent.begin();
|
---|
6535 | it != task.mChildrenToReparent.end();
|
---|
6536 | it++)
|
---|
6537 | {
|
---|
6538 | Medium *pMedium = *it;
|
---|
6539 | AutoWriteLock childLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
6540 |
|
---|
6541 | pMedium->deparent(); // removes pMedium from source
|
---|
6542 | pMedium->setParent(pTarget);
|
---|
6543 | }
|
---|
6544 | }
|
---|
6545 | }
|
---|
6546 |
|
---|
6547 | /* unregister and uninitialize all media removed by the merge */
|
---|
6548 | MediumLockList::Base::iterator lockListBegin =
|
---|
6549 | task.mpMediumLockList->GetBegin();
|
---|
6550 | MediumLockList::Base::iterator lockListEnd =
|
---|
6551 | task.mpMediumLockList->GetEnd();
|
---|
6552 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
6553 | it != lockListEnd;
|
---|
6554 | )
|
---|
6555 | {
|
---|
6556 | MediumLock &mediumLock = *it;
|
---|
6557 | /* Create a real copy of the medium pointer, as the medium
|
---|
6558 | * lock deletion below would invalidate the referenced object. */
|
---|
6559 | const ComObjPtr<Medium> pMedium = mediumLock.GetMedium();
|
---|
6560 |
|
---|
6561 | /* The target and all media not merged (readonly) are skipped */
|
---|
6562 | if ( pMedium == pTarget
|
---|
6563 | || pMedium->m->state == MediumState_LockedRead)
|
---|
6564 | {
|
---|
6565 | ++it;
|
---|
6566 | continue;
|
---|
6567 | }
|
---|
6568 |
|
---|
6569 | rc2 = pMedium->m->pVirtualBox->unregisterHardDisk(pMedium,
|
---|
6570 | NULL /*pfNeedsGlobalSaveSettings*/);
|
---|
6571 | AssertComRC(rc2);
|
---|
6572 |
|
---|
6573 | /* now, uninitialize the deleted medium (note that
|
---|
6574 | * due to the Deleting state, uninit() will not touch
|
---|
6575 | * the parent-child relationship so we need to
|
---|
6576 | * uninitialize each disk individually) */
|
---|
6577 |
|
---|
6578 | /* note that the operation initiator medium (which is
|
---|
6579 | * normally also the source medium) is a special case
|
---|
6580 | * -- there is one more caller added by Task to it which
|
---|
6581 | * we must release. Also, if we are in sync mode, the
|
---|
6582 | * caller may still hold an AutoCaller instance for it
|
---|
6583 | * and therefore we cannot uninit() it (it's therefore
|
---|
6584 | * the caller's responsibility) */
|
---|
6585 | if (pMedium == this)
|
---|
6586 | {
|
---|
6587 | Assert(getChildren().size() == 0);
|
---|
6588 | Assert(m->backRefs.size() == 0);
|
---|
6589 | task.mMediumCaller.release();
|
---|
6590 | }
|
---|
6591 |
|
---|
6592 | /* Delete the medium lock list entry, which also releases the
|
---|
6593 | * caller added by MergeChain before uninit() and updates the
|
---|
6594 | * iterator to point to the right place. */
|
---|
6595 | rc2 = task.mpMediumLockList->RemoveByIterator(it);
|
---|
6596 | AssertComRC(rc2);
|
---|
6597 |
|
---|
6598 | if (task.isAsync() || pMedium != this)
|
---|
6599 | pMedium->uninit();
|
---|
6600 | }
|
---|
6601 | }
|
---|
6602 |
|
---|
6603 | if (task.isAsync())
|
---|
6604 | {
|
---|
6605 | // in asynchronous mode, save settings now
|
---|
6606 | // for that we should hold only the VirtualBox lock
|
---|
6607 | AutoWriteLock vboxlock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
6608 | m->pVirtualBox->saveSettings();
|
---|
6609 | }
|
---|
6610 | else
|
---|
6611 | // synchronous mode: report save settings result to caller
|
---|
6612 | if (task.m_pllRegistriesThatNeedSaving)
|
---|
6613 | pTarget->addToRegistryIDList(*task.m_pllRegistriesThatNeedSaving);
|
---|
6614 |
|
---|
6615 | if (FAILED(rc))
|
---|
6616 | {
|
---|
6617 | /* Here we come if either VDMerge() failed (in which case we
|
---|
6618 | * assume that it tried to do everything to make a further
|
---|
6619 | * retry possible -- e.g. not deleted intermediate media
|
---|
6620 | * and so on) or VirtualBox::saveSettings() failed (where we
|
---|
6621 | * should have the original tree but with intermediate storage
|
---|
6622 | * units deleted by VDMerge()). We have to only restore states
|
---|
6623 | * (through the MergeChain dtor) unless we are run synchronously
|
---|
6624 | * in which case it's the responsibility of the caller as stated
|
---|
6625 | * in the mergeTo() docs. The latter also implies that we
|
---|
6626 | * don't own the merge chain, so release it in this case. */
|
---|
6627 | if (task.isAsync())
|
---|
6628 | {
|
---|
6629 | Assert(task.mChildrenToReparent.size() == 0);
|
---|
6630 | cancelMergeTo(task.mChildrenToReparent, task.mpMediumLockList);
|
---|
6631 | }
|
---|
6632 | }
|
---|
6633 |
|
---|
6634 | return rc;
|
---|
6635 | }
|
---|
6636 |
|
---|
6637 | /**
|
---|
6638 | * Implementation code for the "clone" task.
|
---|
6639 | *
|
---|
6640 | * This only gets started from Medium::CloneTo() and always runs asynchronously.
|
---|
6641 | * As a result, we always save the VirtualBox.xml file when we're done here.
|
---|
6642 | *
|
---|
6643 | * @param task
|
---|
6644 | * @return
|
---|
6645 | */
|
---|
6646 | HRESULT Medium::taskCloneHandler(Medium::CloneTask &task)
|
---|
6647 | {
|
---|
6648 | HRESULT rc = S_OK;
|
---|
6649 |
|
---|
6650 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
6651 | const ComObjPtr<Medium> &pParent = task.mParent;
|
---|
6652 |
|
---|
6653 | bool fCreatingTarget = false;
|
---|
6654 |
|
---|
6655 | uint64_t size = 0, logicalSize = 0;
|
---|
6656 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
6657 | bool fGenerateUuid = false;
|
---|
6658 |
|
---|
6659 | try
|
---|
6660 | {
|
---|
6661 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
6662 | * signal from the task initiator (which releases it only after
|
---|
6663 | * RTThreadCreate()) that we can start the job. */
|
---|
6664 | AutoMultiWriteLock3 thisLock(this, pTarget, pParent COMMA_LOCKVAL_SRC_POS);
|
---|
6665 |
|
---|
6666 | fCreatingTarget = pTarget->m->state == MediumState_Creating;
|
---|
6667 |
|
---|
6668 | /* The object may request a specific UUID (through a special form of
|
---|
6669 | * the setLocation() argument). Otherwise we have to generate it */
|
---|
6670 | Guid targetId = pTarget->m->id;
|
---|
6671 | fGenerateUuid = targetId.isEmpty();
|
---|
6672 | if (fGenerateUuid)
|
---|
6673 | {
|
---|
6674 | targetId.create();
|
---|
6675 | /* VirtualBox::registerHardDisk() will need UUID */
|
---|
6676 | unconst(pTarget->m->id) = targetId;
|
---|
6677 | }
|
---|
6678 |
|
---|
6679 | PVBOXHDD hdd;
|
---|
6680 | int vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &hdd);
|
---|
6681 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6682 |
|
---|
6683 | try
|
---|
6684 | {
|
---|
6685 | /* Open all media in the source chain. */
|
---|
6686 | MediumLockList::Base::const_iterator sourceListBegin =
|
---|
6687 | task.mpSourceMediumLockList->GetBegin();
|
---|
6688 | MediumLockList::Base::const_iterator sourceListEnd =
|
---|
6689 | task.mpSourceMediumLockList->GetEnd();
|
---|
6690 | for (MediumLockList::Base::const_iterator it = sourceListBegin;
|
---|
6691 | it != sourceListEnd;
|
---|
6692 | ++it)
|
---|
6693 | {
|
---|
6694 | const MediumLock &mediumLock = *it;
|
---|
6695 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
6696 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
6697 |
|
---|
6698 | /* sanity check */
|
---|
6699 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
6700 |
|
---|
6701 | /** Open all media in read-only mode. */
|
---|
6702 | vrc = VDOpen(hdd,
|
---|
6703 | pMedium->m->strFormat.c_str(),
|
---|
6704 | pMedium->m->strLocationFull.c_str(),
|
---|
6705 | VD_OPEN_FLAGS_READONLY,
|
---|
6706 | pMedium->m->vdImageIfaces);
|
---|
6707 | if (RT_FAILURE(vrc))
|
---|
6708 | throw setError(VBOX_E_FILE_ERROR,
|
---|
6709 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
6710 | pMedium->m->strLocationFull.c_str(),
|
---|
6711 | vdError(vrc).c_str());
|
---|
6712 | }
|
---|
6713 |
|
---|
6714 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
6715 | Utf8Str targetLocation(pTarget->m->strLocationFull);
|
---|
6716 |
|
---|
6717 | Assert( pTarget->m->state == MediumState_Creating
|
---|
6718 | || pTarget->m->state == MediumState_LockedWrite);
|
---|
6719 | Assert(m->state == MediumState_LockedRead);
|
---|
6720 | Assert( pParent.isNull()
|
---|
6721 | || pParent->m->state == MediumState_LockedRead);
|
---|
6722 |
|
---|
6723 | /* unlock before the potentially lengthy operation */
|
---|
6724 | thisLock.release();
|
---|
6725 |
|
---|
6726 | /* ensure the target directory exists */
|
---|
6727 | rc = VirtualBox::ensureFilePathExists(targetLocation);
|
---|
6728 | if (FAILED(rc))
|
---|
6729 | throw rc;
|
---|
6730 |
|
---|
6731 | PVBOXHDD targetHdd;
|
---|
6732 | vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &targetHdd);
|
---|
6733 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6734 |
|
---|
6735 | try
|
---|
6736 | {
|
---|
6737 | /* Open all media in the target chain. */
|
---|
6738 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
6739 | task.mpTargetMediumLockList->GetBegin();
|
---|
6740 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
6741 | task.mpTargetMediumLockList->GetEnd();
|
---|
6742 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
6743 | it != targetListEnd;
|
---|
6744 | ++it)
|
---|
6745 | {
|
---|
6746 | const MediumLock &mediumLock = *it;
|
---|
6747 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
6748 |
|
---|
6749 | /* If the target medium is not created yet there's no
|
---|
6750 | * reason to open it. */
|
---|
6751 | if (pMedium == pTarget && fCreatingTarget)
|
---|
6752 | continue;
|
---|
6753 |
|
---|
6754 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
6755 |
|
---|
6756 | /* sanity check */
|
---|
6757 | Assert( pMedium->m->state == MediumState_LockedRead
|
---|
6758 | || pMedium->m->state == MediumState_LockedWrite);
|
---|
6759 |
|
---|
6760 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
6761 | if (pMedium->m->state != MediumState_LockedWrite)
|
---|
6762 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
6763 | if (pMedium->m->type == MediumType_Shareable)
|
---|
6764 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
6765 |
|
---|
6766 | /* Open all media in appropriate mode. */
|
---|
6767 | vrc = VDOpen(targetHdd,
|
---|
6768 | pMedium->m->strFormat.c_str(),
|
---|
6769 | pMedium->m->strLocationFull.c_str(),
|
---|
6770 | uOpenFlags,
|
---|
6771 | pMedium->m->vdImageIfaces);
|
---|
6772 | if (RT_FAILURE(vrc))
|
---|
6773 | throw setError(VBOX_E_FILE_ERROR,
|
---|
6774 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
6775 | pMedium->m->strLocationFull.c_str(),
|
---|
6776 | vdError(vrc).c_str());
|
---|
6777 | }
|
---|
6778 |
|
---|
6779 | /** @todo r=klaus target isn't locked, race getting the state */
|
---|
6780 | vrc = VDCopy(hdd,
|
---|
6781 | VD_LAST_IMAGE,
|
---|
6782 | targetHdd,
|
---|
6783 | targetFormat.c_str(),
|
---|
6784 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
6785 | false /* fMoveByRename */,
|
---|
6786 | 0 /* cbSize */,
|
---|
6787 | task.mVariant,
|
---|
6788 | targetId.raw(),
|
---|
6789 | VD_OPEN_FLAGS_NORMAL,
|
---|
6790 | NULL /* pVDIfsOperation */,
|
---|
6791 | pTarget->m->vdImageIfaces,
|
---|
6792 | task.mVDOperationIfaces);
|
---|
6793 | if (RT_FAILURE(vrc))
|
---|
6794 | throw setError(VBOX_E_FILE_ERROR,
|
---|
6795 | tr("Could not create the clone medium '%s'%s"),
|
---|
6796 | targetLocation.c_str(), vdError(vrc).c_str());
|
---|
6797 |
|
---|
6798 | size = VDGetFileSize(targetHdd, VD_LAST_IMAGE);
|
---|
6799 | logicalSize = VDGetSize(targetHdd, VD_LAST_IMAGE);
|
---|
6800 | unsigned uImageFlags;
|
---|
6801 | vrc = VDGetImageFlags(targetHdd, 0, &uImageFlags);
|
---|
6802 | if (RT_SUCCESS(vrc))
|
---|
6803 | variant = (MediumVariant_T)uImageFlags;
|
---|
6804 | }
|
---|
6805 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6806 |
|
---|
6807 | VDDestroy(targetHdd);
|
---|
6808 | }
|
---|
6809 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6810 |
|
---|
6811 | VDDestroy(hdd);
|
---|
6812 | }
|
---|
6813 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6814 |
|
---|
6815 | /* Only do the parent changes for newly created media. */
|
---|
6816 | if (SUCCEEDED(rc) && fCreatingTarget)
|
---|
6817 | {
|
---|
6818 | /* we set mParent & children() */
|
---|
6819 | AutoWriteLock alock2(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
6820 |
|
---|
6821 | Assert(pTarget->m->pParent.isNull());
|
---|
6822 |
|
---|
6823 | if (pParent)
|
---|
6824 | {
|
---|
6825 | /* associate the clone with the parent and deassociate
|
---|
6826 | * from VirtualBox */
|
---|
6827 | pTarget->m->pParent = pParent;
|
---|
6828 | pParent->m->llChildren.push_back(pTarget);
|
---|
6829 |
|
---|
6830 | /* register with mVirtualBox as the last step and move to
|
---|
6831 | * Created state only on success (leaving an orphan file is
|
---|
6832 | * better than breaking media registry consistency) */
|
---|
6833 | rc = pParent->m->pVirtualBox->registerHardDisk(pTarget, NULL /* pllRegistriesThatNeedSaving */);
|
---|
6834 |
|
---|
6835 | if (FAILED(rc))
|
---|
6836 | /* break parent association on failure to register */
|
---|
6837 | pTarget->deparent(); // removes target from parent
|
---|
6838 | }
|
---|
6839 | else
|
---|
6840 | {
|
---|
6841 | /* just register */
|
---|
6842 | rc = m->pVirtualBox->registerHardDisk(pTarget, NULL /* pllRegistriesThatNeedSaving */);
|
---|
6843 | }
|
---|
6844 | }
|
---|
6845 |
|
---|
6846 | if (fCreatingTarget)
|
---|
6847 | {
|
---|
6848 | AutoWriteLock mLock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
6849 |
|
---|
6850 | if (SUCCEEDED(rc))
|
---|
6851 | {
|
---|
6852 | pTarget->m->state = MediumState_Created;
|
---|
6853 |
|
---|
6854 | pTarget->m->size = size;
|
---|
6855 | pTarget->m->logicalSize = logicalSize;
|
---|
6856 | pTarget->m->variant = variant;
|
---|
6857 | }
|
---|
6858 | else
|
---|
6859 | {
|
---|
6860 | /* back to NotCreated on failure */
|
---|
6861 | pTarget->m->state = MediumState_NotCreated;
|
---|
6862 |
|
---|
6863 | /* reset UUID to prevent it from being reused next time */
|
---|
6864 | if (fGenerateUuid)
|
---|
6865 | unconst(pTarget->m->id).clear();
|
---|
6866 | }
|
---|
6867 | }
|
---|
6868 |
|
---|
6869 | // now, at the end of this task (always asynchronous), save the settings
|
---|
6870 | {
|
---|
6871 | AutoWriteLock vboxlock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
6872 | m->pVirtualBox->saveSettings();
|
---|
6873 | }
|
---|
6874 |
|
---|
6875 | /* Everything is explicitly unlocked when the task exits,
|
---|
6876 | * as the task destruction also destroys the source chain. */
|
---|
6877 |
|
---|
6878 | /* Make sure the source chain is released early. It could happen
|
---|
6879 | * that we get a deadlock in Appliance::Import when Medium::Close
|
---|
6880 | * is called & the source chain is released at the same time. */
|
---|
6881 | task.mpSourceMediumLockList->Clear();
|
---|
6882 |
|
---|
6883 | return rc;
|
---|
6884 | }
|
---|
6885 |
|
---|
6886 | /**
|
---|
6887 | * Implementation code for the "delete" task.
|
---|
6888 | *
|
---|
6889 | * This task always gets started from Medium::deleteStorage() and can run
|
---|
6890 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
6891 | * that function.
|
---|
6892 | *
|
---|
6893 | * @param task
|
---|
6894 | * @return
|
---|
6895 | */
|
---|
6896 | HRESULT Medium::taskDeleteHandler(Medium::DeleteTask &task)
|
---|
6897 | {
|
---|
6898 | NOREF(task);
|
---|
6899 | HRESULT rc = S_OK;
|
---|
6900 |
|
---|
6901 | try
|
---|
6902 | {
|
---|
6903 | /* The lock is also used as a signal from the task initiator (which
|
---|
6904 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
6905 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6906 |
|
---|
6907 | PVBOXHDD hdd;
|
---|
6908 | int vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &hdd);
|
---|
6909 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6910 |
|
---|
6911 | Utf8Str format(m->strFormat);
|
---|
6912 | Utf8Str location(m->strLocationFull);
|
---|
6913 |
|
---|
6914 | /* unlock before the potentially lengthy operation */
|
---|
6915 | Assert(m->state == MediumState_Deleting);
|
---|
6916 | thisLock.release();
|
---|
6917 |
|
---|
6918 | try
|
---|
6919 | {
|
---|
6920 | vrc = VDOpen(hdd,
|
---|
6921 | format.c_str(),
|
---|
6922 | location.c_str(),
|
---|
6923 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO,
|
---|
6924 | m->vdImageIfaces);
|
---|
6925 | if (RT_SUCCESS(vrc))
|
---|
6926 | vrc = VDClose(hdd, true /* fDelete */);
|
---|
6927 |
|
---|
6928 | if (RT_FAILURE(vrc))
|
---|
6929 | throw setError(VBOX_E_FILE_ERROR,
|
---|
6930 | tr("Could not delete the medium storage unit '%s'%s"),
|
---|
6931 | location.c_str(), vdError(vrc).c_str());
|
---|
6932 |
|
---|
6933 | }
|
---|
6934 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6935 |
|
---|
6936 | VDDestroy(hdd);
|
---|
6937 | }
|
---|
6938 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6939 |
|
---|
6940 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6941 |
|
---|
6942 | /* go to the NotCreated state even on failure since the storage
|
---|
6943 | * may have been already partially deleted and cannot be used any
|
---|
6944 | * more. One will be able to manually re-open the storage if really
|
---|
6945 | * needed to re-register it. */
|
---|
6946 | m->state = MediumState_NotCreated;
|
---|
6947 |
|
---|
6948 | /* Reset UUID to prevent Create* from reusing it again */
|
---|
6949 | unconst(m->id).clear();
|
---|
6950 |
|
---|
6951 | return rc;
|
---|
6952 | }
|
---|
6953 |
|
---|
6954 | /**
|
---|
6955 | * Implementation code for the "reset" task.
|
---|
6956 | *
|
---|
6957 | * This always gets started asynchronously from Medium::Reset().
|
---|
6958 | *
|
---|
6959 | * @param task
|
---|
6960 | * @return
|
---|
6961 | */
|
---|
6962 | HRESULT Medium::taskResetHandler(Medium::ResetTask &task)
|
---|
6963 | {
|
---|
6964 | HRESULT rc = S_OK;
|
---|
6965 |
|
---|
6966 | uint64_t size = 0, logicalSize = 0;
|
---|
6967 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
6968 |
|
---|
6969 | try
|
---|
6970 | {
|
---|
6971 | /* The lock is also used as a signal from the task initiator (which
|
---|
6972 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
6973 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6974 |
|
---|
6975 | /// @todo Below we use a pair of delete/create operations to reset
|
---|
6976 | /// the diff contents but the most efficient way will of course be
|
---|
6977 | /// to add a VDResetDiff() API call
|
---|
6978 |
|
---|
6979 | PVBOXHDD hdd;
|
---|
6980 | int vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &hdd);
|
---|
6981 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6982 |
|
---|
6983 | Guid id = m->id;
|
---|
6984 | Utf8Str format(m->strFormat);
|
---|
6985 | Utf8Str location(m->strLocationFull);
|
---|
6986 |
|
---|
6987 | Medium *pParent = m->pParent;
|
---|
6988 | Guid parentId = pParent->m->id;
|
---|
6989 | Utf8Str parentFormat(pParent->m->strFormat);
|
---|
6990 | Utf8Str parentLocation(pParent->m->strLocationFull);
|
---|
6991 |
|
---|
6992 | Assert(m->state == MediumState_LockedWrite);
|
---|
6993 |
|
---|
6994 | /* unlock before the potentially lengthy operation */
|
---|
6995 | thisLock.release();
|
---|
6996 |
|
---|
6997 | try
|
---|
6998 | {
|
---|
6999 | /* Open all media in the target chain but the last. */
|
---|
7000 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
7001 | task.mpMediumLockList->GetBegin();
|
---|
7002 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
7003 | task.mpMediumLockList->GetEnd();
|
---|
7004 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
7005 | it != targetListEnd;
|
---|
7006 | ++it)
|
---|
7007 | {
|
---|
7008 | const MediumLock &mediumLock = *it;
|
---|
7009 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
7010 |
|
---|
7011 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
7012 |
|
---|
7013 | /* sanity check, "this" is checked above */
|
---|
7014 | Assert( pMedium == this
|
---|
7015 | || pMedium->m->state == MediumState_LockedRead);
|
---|
7016 |
|
---|
7017 | /* Open all media in appropriate mode. */
|
---|
7018 | vrc = VDOpen(hdd,
|
---|
7019 | pMedium->m->strFormat.c_str(),
|
---|
7020 | pMedium->m->strLocationFull.c_str(),
|
---|
7021 | VD_OPEN_FLAGS_READONLY,
|
---|
7022 | pMedium->m->vdImageIfaces);
|
---|
7023 | if (RT_FAILURE(vrc))
|
---|
7024 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7025 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
7026 | pMedium->m->strLocationFull.c_str(),
|
---|
7027 | vdError(vrc).c_str());
|
---|
7028 |
|
---|
7029 | /* Done when we hit the media which should be reset */
|
---|
7030 | if (pMedium == this)
|
---|
7031 | break;
|
---|
7032 | }
|
---|
7033 |
|
---|
7034 | /* first, delete the storage unit */
|
---|
7035 | vrc = VDClose(hdd, true /* fDelete */);
|
---|
7036 | if (RT_FAILURE(vrc))
|
---|
7037 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7038 | tr("Could not delete the medium storage unit '%s'%s"),
|
---|
7039 | location.c_str(), vdError(vrc).c_str());
|
---|
7040 |
|
---|
7041 | /* next, create it again */
|
---|
7042 | vrc = VDOpen(hdd,
|
---|
7043 | parentFormat.c_str(),
|
---|
7044 | parentLocation.c_str(),
|
---|
7045 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO,
|
---|
7046 | m->vdImageIfaces);
|
---|
7047 | if (RT_FAILURE(vrc))
|
---|
7048 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7049 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
7050 | parentLocation.c_str(), vdError(vrc).c_str());
|
---|
7051 |
|
---|
7052 | vrc = VDCreateDiff(hdd,
|
---|
7053 | format.c_str(),
|
---|
7054 | location.c_str(),
|
---|
7055 | /// @todo use the same medium variant as before
|
---|
7056 | VD_IMAGE_FLAGS_NONE,
|
---|
7057 | NULL,
|
---|
7058 | id.raw(),
|
---|
7059 | parentId.raw(),
|
---|
7060 | VD_OPEN_FLAGS_NORMAL,
|
---|
7061 | m->vdImageIfaces,
|
---|
7062 | task.mVDOperationIfaces);
|
---|
7063 | if (RT_FAILURE(vrc))
|
---|
7064 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7065 | tr("Could not create the differencing medium storage unit '%s'%s"),
|
---|
7066 | location.c_str(), vdError(vrc).c_str());
|
---|
7067 |
|
---|
7068 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
7069 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
7070 | unsigned uImageFlags;
|
---|
7071 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
7072 | if (RT_SUCCESS(vrc))
|
---|
7073 | variant = (MediumVariant_T)uImageFlags;
|
---|
7074 | }
|
---|
7075 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7076 |
|
---|
7077 | VDDestroy(hdd);
|
---|
7078 | }
|
---|
7079 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7080 |
|
---|
7081 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7082 |
|
---|
7083 | m->size = size;
|
---|
7084 | m->logicalSize = logicalSize;
|
---|
7085 | m->variant = variant;
|
---|
7086 |
|
---|
7087 | if (task.isAsync())
|
---|
7088 | {
|
---|
7089 | /* unlock ourselves when done */
|
---|
7090 | HRESULT rc2 = UnlockWrite(NULL);
|
---|
7091 | AssertComRC(rc2);
|
---|
7092 | }
|
---|
7093 |
|
---|
7094 | /* Note that in sync mode, it's the caller's responsibility to
|
---|
7095 | * unlock the medium. */
|
---|
7096 |
|
---|
7097 | return rc;
|
---|
7098 | }
|
---|
7099 |
|
---|
7100 | /**
|
---|
7101 | * Implementation code for the "compact" task.
|
---|
7102 | *
|
---|
7103 | * @param task
|
---|
7104 | * @return
|
---|
7105 | */
|
---|
7106 | HRESULT Medium::taskCompactHandler(Medium::CompactTask &task)
|
---|
7107 | {
|
---|
7108 | HRESULT rc = S_OK;
|
---|
7109 |
|
---|
7110 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
7111 | * signal from the task initiator (which releases it only after
|
---|
7112 | * RTThreadCreate()) that we can start the job. */
|
---|
7113 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7114 |
|
---|
7115 | try
|
---|
7116 | {
|
---|
7117 | PVBOXHDD hdd;
|
---|
7118 | int vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &hdd);
|
---|
7119 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7120 |
|
---|
7121 | try
|
---|
7122 | {
|
---|
7123 | /* Open all media in the chain. */
|
---|
7124 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
7125 | task.mpMediumLockList->GetBegin();
|
---|
7126 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
7127 | task.mpMediumLockList->GetEnd();
|
---|
7128 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
7129 | mediumListEnd;
|
---|
7130 | mediumListLast--;
|
---|
7131 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
7132 | it != mediumListEnd;
|
---|
7133 | ++it)
|
---|
7134 | {
|
---|
7135 | const MediumLock &mediumLock = *it;
|
---|
7136 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
7137 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
7138 |
|
---|
7139 | /* sanity check */
|
---|
7140 | if (it == mediumListLast)
|
---|
7141 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
7142 | else
|
---|
7143 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
7144 |
|
---|
7145 | /* Open all media but last in read-only mode. Do not handle
|
---|
7146 | * shareable media, as compaction and sharing are mutually
|
---|
7147 | * exclusive. */
|
---|
7148 | vrc = VDOpen(hdd,
|
---|
7149 | pMedium->m->strFormat.c_str(),
|
---|
7150 | pMedium->m->strLocationFull.c_str(),
|
---|
7151 | (it == mediumListLast) ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY,
|
---|
7152 | pMedium->m->vdImageIfaces);
|
---|
7153 | if (RT_FAILURE(vrc))
|
---|
7154 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7155 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
7156 | pMedium->m->strLocationFull.c_str(),
|
---|
7157 | vdError(vrc).c_str());
|
---|
7158 | }
|
---|
7159 |
|
---|
7160 | Assert(m->state == MediumState_LockedWrite);
|
---|
7161 |
|
---|
7162 | Utf8Str location(m->strLocationFull);
|
---|
7163 |
|
---|
7164 | /* unlock before the potentially lengthy operation */
|
---|
7165 | thisLock.release();
|
---|
7166 |
|
---|
7167 | vrc = VDCompact(hdd, VD_LAST_IMAGE, task.mVDOperationIfaces);
|
---|
7168 | if (RT_FAILURE(vrc))
|
---|
7169 | {
|
---|
7170 | if (vrc == VERR_NOT_SUPPORTED)
|
---|
7171 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
7172 | tr("Compacting is not yet supported for medium '%s'"),
|
---|
7173 | location.c_str());
|
---|
7174 | else if (vrc == VERR_NOT_IMPLEMENTED)
|
---|
7175 | throw setError(E_NOTIMPL,
|
---|
7176 | tr("Compacting is not implemented, medium '%s'"),
|
---|
7177 | location.c_str());
|
---|
7178 | else
|
---|
7179 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7180 | tr("Could not compact medium '%s'%s"),
|
---|
7181 | location.c_str(),
|
---|
7182 | vdError(vrc).c_str());
|
---|
7183 | }
|
---|
7184 | }
|
---|
7185 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7186 |
|
---|
7187 | VDDestroy(hdd);
|
---|
7188 | }
|
---|
7189 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7190 |
|
---|
7191 | /* Everything is explicitly unlocked when the task exits,
|
---|
7192 | * as the task destruction also destroys the media chain. */
|
---|
7193 |
|
---|
7194 | return rc;
|
---|
7195 | }
|
---|
7196 |
|
---|
7197 | /**
|
---|
7198 | * Implementation code for the "resize" task.
|
---|
7199 | *
|
---|
7200 | * @param task
|
---|
7201 | * @return
|
---|
7202 | */
|
---|
7203 | HRESULT Medium::taskResizeHandler(Medium::ResizeTask &task)
|
---|
7204 | {
|
---|
7205 | HRESULT rc = S_OK;
|
---|
7206 |
|
---|
7207 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
7208 | * signal from the task initiator (which releases it only after
|
---|
7209 | * RTThreadCreate()) that we can start the job. */
|
---|
7210 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7211 |
|
---|
7212 | try
|
---|
7213 | {
|
---|
7214 | PVBOXHDD hdd;
|
---|
7215 | int vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &hdd);
|
---|
7216 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7217 |
|
---|
7218 | try
|
---|
7219 | {
|
---|
7220 | /* Open all media in the chain. */
|
---|
7221 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
7222 | task.mpMediumLockList->GetBegin();
|
---|
7223 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
7224 | task.mpMediumLockList->GetEnd();
|
---|
7225 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
7226 | mediumListEnd;
|
---|
7227 | mediumListLast--;
|
---|
7228 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
7229 | it != mediumListEnd;
|
---|
7230 | ++it)
|
---|
7231 | {
|
---|
7232 | const MediumLock &mediumLock = *it;
|
---|
7233 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
7234 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
7235 |
|
---|
7236 | /* sanity check */
|
---|
7237 | if (it == mediumListLast)
|
---|
7238 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
7239 | else
|
---|
7240 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
7241 |
|
---|
7242 | /* Open all media but last in read-only mode. Do not handle
|
---|
7243 | * shareable media, as compaction and sharing are mutually
|
---|
7244 | * exclusive. */
|
---|
7245 | vrc = VDOpen(hdd,
|
---|
7246 | pMedium->m->strFormat.c_str(),
|
---|
7247 | pMedium->m->strLocationFull.c_str(),
|
---|
7248 | (it == mediumListLast) ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY,
|
---|
7249 | pMedium->m->vdImageIfaces);
|
---|
7250 | if (RT_FAILURE(vrc))
|
---|
7251 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7252 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
7253 | pMedium->m->strLocationFull.c_str(),
|
---|
7254 | vdError(vrc).c_str());
|
---|
7255 | }
|
---|
7256 |
|
---|
7257 | Assert(m->state == MediumState_LockedWrite);
|
---|
7258 |
|
---|
7259 | Utf8Str location(m->strLocationFull);
|
---|
7260 |
|
---|
7261 | /* unlock before the potentially lengthy operation */
|
---|
7262 | thisLock.release();
|
---|
7263 |
|
---|
7264 | VDGEOMETRY geo = {0, 0, 0}; /* auto */
|
---|
7265 | vrc = VDResize(hdd, task.mSize, &geo, &geo, task.mVDOperationIfaces);
|
---|
7266 | if (RT_FAILURE(vrc))
|
---|
7267 | {
|
---|
7268 | if (vrc == VERR_NOT_SUPPORTED)
|
---|
7269 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
7270 | tr("Compacting is not yet supported for medium '%s'"),
|
---|
7271 | location.c_str());
|
---|
7272 | else if (vrc == VERR_NOT_IMPLEMENTED)
|
---|
7273 | throw setError(E_NOTIMPL,
|
---|
7274 | tr("Compacting is not implemented, medium '%s'"),
|
---|
7275 | location.c_str());
|
---|
7276 | else
|
---|
7277 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7278 | tr("Could not compact medium '%s'%s"),
|
---|
7279 | location.c_str(),
|
---|
7280 | vdError(vrc).c_str());
|
---|
7281 | }
|
---|
7282 | }
|
---|
7283 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7284 |
|
---|
7285 | VDDestroy(hdd);
|
---|
7286 | }
|
---|
7287 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7288 |
|
---|
7289 | /* Everything is explicitly unlocked when the task exits,
|
---|
7290 | * as the task destruction also destroys the media chain. */
|
---|
7291 |
|
---|
7292 | return rc;
|
---|
7293 | }
|
---|
7294 |
|
---|
7295 | /**
|
---|
7296 | * Implementation code for the "export" task.
|
---|
7297 | *
|
---|
7298 | * This only gets started from Medium::exportFile() and always runs
|
---|
7299 | * asynchronously. It doesn't touch anything configuration related, so
|
---|
7300 | * we never save the VirtualBox.xml file here.
|
---|
7301 | *
|
---|
7302 | * @param task
|
---|
7303 | * @return
|
---|
7304 | */
|
---|
7305 | HRESULT Medium::taskExportHandler(Medium::ExportTask &task)
|
---|
7306 | {
|
---|
7307 | HRESULT rc = S_OK;
|
---|
7308 |
|
---|
7309 | try
|
---|
7310 | {
|
---|
7311 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
7312 | * signal from the task initiator (which releases it only after
|
---|
7313 | * RTThreadCreate()) that we can start the job. */
|
---|
7314 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7315 |
|
---|
7316 | PVBOXHDD hdd;
|
---|
7317 | int vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &hdd);
|
---|
7318 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7319 |
|
---|
7320 | try
|
---|
7321 | {
|
---|
7322 | /* Open all media in the source chain. */
|
---|
7323 | MediumLockList::Base::const_iterator sourceListBegin =
|
---|
7324 | task.mpSourceMediumLockList->GetBegin();
|
---|
7325 | MediumLockList::Base::const_iterator sourceListEnd =
|
---|
7326 | task.mpSourceMediumLockList->GetEnd();
|
---|
7327 | for (MediumLockList::Base::const_iterator it = sourceListBegin;
|
---|
7328 | it != sourceListEnd;
|
---|
7329 | ++it)
|
---|
7330 | {
|
---|
7331 | const MediumLock &mediumLock = *it;
|
---|
7332 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
7333 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
7334 |
|
---|
7335 | /* sanity check */
|
---|
7336 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
7337 |
|
---|
7338 | /* Open all media in read-only mode. */
|
---|
7339 | vrc = VDOpen(hdd,
|
---|
7340 | pMedium->m->strFormat.c_str(),
|
---|
7341 | pMedium->m->strLocationFull.c_str(),
|
---|
7342 | VD_OPEN_FLAGS_READONLY,
|
---|
7343 | pMedium->m->vdImageIfaces);
|
---|
7344 | if (RT_FAILURE(vrc))
|
---|
7345 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7346 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
7347 | pMedium->m->strLocationFull.c_str(),
|
---|
7348 | vdError(vrc).c_str());
|
---|
7349 | }
|
---|
7350 |
|
---|
7351 | Utf8Str targetFormat(task.mFormat->getId());
|
---|
7352 | Utf8Str targetLocation(task.mFilename);
|
---|
7353 |
|
---|
7354 | Assert(m->state == MediumState_LockedRead);
|
---|
7355 |
|
---|
7356 | /* unlock before the potentially lengthy operation */
|
---|
7357 | thisLock.release();
|
---|
7358 |
|
---|
7359 | /* ensure the target directory exists */
|
---|
7360 | rc = VirtualBox::ensureFilePathExists(targetLocation);
|
---|
7361 | if (FAILED(rc))
|
---|
7362 | throw rc;
|
---|
7363 |
|
---|
7364 | PVBOXHDD targetHdd;
|
---|
7365 | vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &targetHdd);
|
---|
7366 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7367 |
|
---|
7368 | try
|
---|
7369 | {
|
---|
7370 | vrc = VDCopy(hdd,
|
---|
7371 | VD_LAST_IMAGE,
|
---|
7372 | targetHdd,
|
---|
7373 | targetFormat.c_str(),
|
---|
7374 | targetLocation.c_str(),
|
---|
7375 | false /* fMoveByRename */,
|
---|
7376 | 0 /* cbSize */,
|
---|
7377 | task.mVariant,
|
---|
7378 | NULL /* pDstUuid */,
|
---|
7379 | VD_OPEN_FLAGS_NORMAL,
|
---|
7380 | NULL /* pVDIfsOperation */,
|
---|
7381 | task.mVDImageIfaces,
|
---|
7382 | task.mVDOperationIfaces);
|
---|
7383 | if (RT_FAILURE(vrc))
|
---|
7384 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7385 | tr("Could not create the clone medium '%s'%s"),
|
---|
7386 | targetLocation.c_str(), vdError(vrc).c_str());
|
---|
7387 | }
|
---|
7388 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7389 |
|
---|
7390 | VDDestroy(targetHdd);
|
---|
7391 | }
|
---|
7392 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7393 |
|
---|
7394 | VDDestroy(hdd);
|
---|
7395 | }
|
---|
7396 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7397 |
|
---|
7398 | /* Everything is explicitly unlocked when the task exits,
|
---|
7399 | * as the task destruction also destroys the source chain. */
|
---|
7400 |
|
---|
7401 | /* Make sure the source chain is released early, otherwise it can
|
---|
7402 | * lead to deadlocks with concurrent IAppliance activities. */
|
---|
7403 | task.mpSourceMediumLockList->Clear();
|
---|
7404 |
|
---|
7405 | return rc;
|
---|
7406 | }
|
---|
7407 |
|
---|
7408 | /**
|
---|
7409 | * Implementation code for the "import" task.
|
---|
7410 | *
|
---|
7411 | * This only gets started from Medium::importFile() and always runs
|
---|
7412 | * asynchronously. It potentially touches the media registry, so we
|
---|
7413 | * always save the VirtualBox.xml file when we're done here.
|
---|
7414 | *
|
---|
7415 | * @param task
|
---|
7416 | * @return
|
---|
7417 | */
|
---|
7418 | HRESULT Medium::taskImportHandler(Medium::ImportTask &task)
|
---|
7419 | {
|
---|
7420 | HRESULT rc = S_OK;
|
---|
7421 |
|
---|
7422 | const ComObjPtr<Medium> &pParent = task.mParent;
|
---|
7423 |
|
---|
7424 | bool fCreatingTarget = false;
|
---|
7425 |
|
---|
7426 | uint64_t size = 0, logicalSize = 0;
|
---|
7427 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
7428 | bool fGenerateUuid = false;
|
---|
7429 |
|
---|
7430 | try
|
---|
7431 | {
|
---|
7432 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
7433 | * signal from the task initiator (which releases it only after
|
---|
7434 | * RTThreadCreate()) that we can start the job. */
|
---|
7435 | AutoMultiWriteLock2 thisLock(this, pParent COMMA_LOCKVAL_SRC_POS);
|
---|
7436 |
|
---|
7437 | fCreatingTarget = m->state == MediumState_Creating;
|
---|
7438 |
|
---|
7439 | /* The object may request a specific UUID (through a special form of
|
---|
7440 | * the setLocation() argument). Otherwise we have to generate it */
|
---|
7441 | Guid targetId = m->id;
|
---|
7442 | fGenerateUuid = targetId.isEmpty();
|
---|
7443 | if (fGenerateUuid)
|
---|
7444 | {
|
---|
7445 | targetId.create();
|
---|
7446 | /* VirtualBox::registerHardDisk() will need UUID */
|
---|
7447 | unconst(m->id) = targetId;
|
---|
7448 | }
|
---|
7449 |
|
---|
7450 |
|
---|
7451 | PVBOXHDD hdd;
|
---|
7452 | int vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &hdd);
|
---|
7453 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7454 |
|
---|
7455 | try
|
---|
7456 | {
|
---|
7457 | /* Open source medium. */
|
---|
7458 | vrc = VDOpen(hdd,
|
---|
7459 | task.mFormat->getId().c_str(),
|
---|
7460 | task.mFilename.c_str(),
|
---|
7461 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SEQUENTIAL,
|
---|
7462 | task.mVDImageIfaces);
|
---|
7463 | if (RT_FAILURE(vrc))
|
---|
7464 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7465 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
7466 | task.mFilename.c_str(),
|
---|
7467 | vdError(vrc).c_str());
|
---|
7468 |
|
---|
7469 | Utf8Str targetFormat(m->strFormat);
|
---|
7470 | Utf8Str targetLocation(m->strLocationFull);
|
---|
7471 |
|
---|
7472 | Assert( m->state == MediumState_Creating
|
---|
7473 | || m->state == MediumState_LockedWrite);
|
---|
7474 | Assert( pParent.isNull()
|
---|
7475 | || pParent->m->state == MediumState_LockedRead);
|
---|
7476 |
|
---|
7477 | /* unlock before the potentially lengthy operation */
|
---|
7478 | thisLock.release();
|
---|
7479 |
|
---|
7480 | /* ensure the target directory exists */
|
---|
7481 | rc = VirtualBox::ensureFilePathExists(targetLocation);
|
---|
7482 | if (FAILED(rc))
|
---|
7483 | throw rc;
|
---|
7484 |
|
---|
7485 | PVBOXHDD targetHdd;
|
---|
7486 | vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &targetHdd);
|
---|
7487 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7488 |
|
---|
7489 | try
|
---|
7490 | {
|
---|
7491 | /* Open all media in the target chain. */
|
---|
7492 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
7493 | task.mpTargetMediumLockList->GetBegin();
|
---|
7494 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
7495 | task.mpTargetMediumLockList->GetEnd();
|
---|
7496 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
7497 | it != targetListEnd;
|
---|
7498 | ++it)
|
---|
7499 | {
|
---|
7500 | const MediumLock &mediumLock = *it;
|
---|
7501 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
7502 |
|
---|
7503 | /* If the target medium is not created yet there's no
|
---|
7504 | * reason to open it. */
|
---|
7505 | if (pMedium == this && fCreatingTarget)
|
---|
7506 | continue;
|
---|
7507 |
|
---|
7508 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
7509 |
|
---|
7510 | /* sanity check */
|
---|
7511 | Assert( pMedium->m->state == MediumState_LockedRead
|
---|
7512 | || pMedium->m->state == MediumState_LockedWrite);
|
---|
7513 |
|
---|
7514 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
7515 | if (pMedium->m->state != MediumState_LockedWrite)
|
---|
7516 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
7517 | if (pMedium->m->type == MediumType_Shareable)
|
---|
7518 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
7519 |
|
---|
7520 | /* Open all media in appropriate mode. */
|
---|
7521 | vrc = VDOpen(targetHdd,
|
---|
7522 | pMedium->m->strFormat.c_str(),
|
---|
7523 | pMedium->m->strLocationFull.c_str(),
|
---|
7524 | uOpenFlags,
|
---|
7525 | pMedium->m->vdImageIfaces);
|
---|
7526 | if (RT_FAILURE(vrc))
|
---|
7527 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7528 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
7529 | pMedium->m->strLocationFull.c_str(),
|
---|
7530 | vdError(vrc).c_str());
|
---|
7531 | }
|
---|
7532 |
|
---|
7533 | /** @todo r=klaus target isn't locked, race getting the state */
|
---|
7534 | vrc = VDCopy(hdd,
|
---|
7535 | VD_LAST_IMAGE,
|
---|
7536 | targetHdd,
|
---|
7537 | targetFormat.c_str(),
|
---|
7538 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
7539 | false /* fMoveByRename */,
|
---|
7540 | 0 /* cbSize */,
|
---|
7541 | task.mVariant,
|
---|
7542 | targetId.raw(),
|
---|
7543 | VD_OPEN_FLAGS_NORMAL,
|
---|
7544 | NULL /* pVDIfsOperation */,
|
---|
7545 | m->vdImageIfaces,
|
---|
7546 | task.mVDOperationIfaces);
|
---|
7547 | if (RT_FAILURE(vrc))
|
---|
7548 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7549 | tr("Could not create the clone medium '%s'%s"),
|
---|
7550 | targetLocation.c_str(), vdError(vrc).c_str());
|
---|
7551 |
|
---|
7552 | size = VDGetFileSize(targetHdd, VD_LAST_IMAGE);
|
---|
7553 | logicalSize = VDGetSize(targetHdd, VD_LAST_IMAGE);
|
---|
7554 | unsigned uImageFlags;
|
---|
7555 | vrc = VDGetImageFlags(targetHdd, 0, &uImageFlags);
|
---|
7556 | if (RT_SUCCESS(vrc))
|
---|
7557 | variant = (MediumVariant_T)uImageFlags;
|
---|
7558 | }
|
---|
7559 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7560 |
|
---|
7561 | VDDestroy(targetHdd);
|
---|
7562 | }
|
---|
7563 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7564 |
|
---|
7565 | VDDestroy(hdd);
|
---|
7566 | }
|
---|
7567 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7568 |
|
---|
7569 | /* Only do the parent changes for newly created media. */
|
---|
7570 | if (SUCCEEDED(rc) && fCreatingTarget)
|
---|
7571 | {
|
---|
7572 | /* we set mParent & children() */
|
---|
7573 | AutoWriteLock alock2(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
7574 |
|
---|
7575 | Assert(m->pParent.isNull());
|
---|
7576 |
|
---|
7577 | if (pParent)
|
---|
7578 | {
|
---|
7579 | /* associate the clone with the parent and deassociate
|
---|
7580 | * from VirtualBox */
|
---|
7581 | m->pParent = pParent;
|
---|
7582 | pParent->m->llChildren.push_back(this);
|
---|
7583 |
|
---|
7584 | /* register with mVirtualBox as the last step and move to
|
---|
7585 | * Created state only on success (leaving an orphan file is
|
---|
7586 | * better than breaking media registry consistency) */
|
---|
7587 | rc = pParent->m->pVirtualBox->registerHardDisk(this, NULL /* llRegistriesThatNeedSaving */);
|
---|
7588 |
|
---|
7589 | if (FAILED(rc))
|
---|
7590 | /* break parent association on failure to register */
|
---|
7591 | this->deparent(); // removes target from parent
|
---|
7592 | }
|
---|
7593 | else
|
---|
7594 | {
|
---|
7595 | /* just register */
|
---|
7596 | rc = m->pVirtualBox->registerHardDisk(this, NULL /* pllRegistriesThatNeedSaving */);
|
---|
7597 | }
|
---|
7598 | }
|
---|
7599 |
|
---|
7600 | if (fCreatingTarget)
|
---|
7601 | {
|
---|
7602 | AutoWriteLock mLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7603 |
|
---|
7604 | if (SUCCEEDED(rc))
|
---|
7605 | {
|
---|
7606 | m->state = MediumState_Created;
|
---|
7607 |
|
---|
7608 | m->size = size;
|
---|
7609 | m->logicalSize = logicalSize;
|
---|
7610 | m->variant = variant;
|
---|
7611 | }
|
---|
7612 | else
|
---|
7613 | {
|
---|
7614 | /* back to NotCreated on failure */
|
---|
7615 | m->state = MediumState_NotCreated;
|
---|
7616 |
|
---|
7617 | /* reset UUID to prevent it from being reused next time */
|
---|
7618 | if (fGenerateUuid)
|
---|
7619 | unconst(m->id).clear();
|
---|
7620 | }
|
---|
7621 | }
|
---|
7622 |
|
---|
7623 | // now, at the end of this task (always asynchronous), save the settings
|
---|
7624 | {
|
---|
7625 | AutoWriteLock vboxlock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
7626 | m->pVirtualBox->saveSettings();
|
---|
7627 | }
|
---|
7628 |
|
---|
7629 | /* Everything is explicitly unlocked when the task exits,
|
---|
7630 | * as the task destruction also destroys the target chain. */
|
---|
7631 |
|
---|
7632 | /* Make sure the target chain is released early, otherwise it can
|
---|
7633 | * lead to deadlocks with concurrent IAppliance activities. */
|
---|
7634 | task.mpTargetMediumLockList->Clear();
|
---|
7635 |
|
---|
7636 | return rc;
|
---|
7637 | }
|
---|
7638 |
|
---|
7639 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|