1 | /* $Id: MediumImpl.cpp 31568 2010-08-11 13:35:59Z 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/VBoxHDD.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 | typedef std::list<Guid> GuidList;
|
---|
66 |
|
---|
67 | BackRef(const Guid &aMachineId,
|
---|
68 | const Guid &aSnapshotId = Guid::Empty)
|
---|
69 | : machineId(aMachineId),
|
---|
70 | fInCurState(aSnapshotId.isEmpty())
|
---|
71 | {
|
---|
72 | if (!aSnapshotId.isEmpty())
|
---|
73 | llSnapshotIds.push_back(aSnapshotId);
|
---|
74 | }
|
---|
75 |
|
---|
76 | Guid machineId;
|
---|
77 | bool fInCurState : 1;
|
---|
78 | GuidList llSnapshotIds;
|
---|
79 | };
|
---|
80 |
|
---|
81 | typedef std::list<BackRef> BackRefList;
|
---|
82 |
|
---|
83 | struct Medium::Data
|
---|
84 | {
|
---|
85 | Data()
|
---|
86 | : pVirtualBox(NULL),
|
---|
87 | state(MediumState_NotCreated),
|
---|
88 | variant(MediumVariant_Standard),
|
---|
89 | size(0),
|
---|
90 | readers(0),
|
---|
91 | preLockState(MediumState_NotCreated),
|
---|
92 | queryInfoSem(NIL_RTSEMEVENTMULTI),
|
---|
93 | queryInfoRunning(false),
|
---|
94 | type(MediumType_Normal),
|
---|
95 | devType(DeviceType_HardDisk),
|
---|
96 | logicalSize(0),
|
---|
97 | hddOpenMode(OpenReadWrite),
|
---|
98 | autoReset(false),
|
---|
99 | hostDrive(false),
|
---|
100 | implicit(false),
|
---|
101 | numCreateDiffTasks(0),
|
---|
102 | vdDiskIfaces(NULL)
|
---|
103 | { }
|
---|
104 |
|
---|
105 | /** weak VirtualBox parent */
|
---|
106 | VirtualBox * const pVirtualBox;
|
---|
107 |
|
---|
108 | // pParent and llChildren are protected by VirtualBox::getMediaTreeLockHandle()
|
---|
109 | ComObjPtr<Medium> pParent;
|
---|
110 | MediaList llChildren; // to add a child, just call push_back; to remove a child, call child->deparent() which does a lookup
|
---|
111 |
|
---|
112 | Guid uuidRegistryMachine; // machine in whose registry this medium is listed or NULL; see getRegistryMachine()
|
---|
113 |
|
---|
114 | const Guid id;
|
---|
115 | Utf8Str strDescription;
|
---|
116 | MediumState_T state;
|
---|
117 | MediumVariant_T variant;
|
---|
118 | Utf8Str strLocation;
|
---|
119 | Utf8Str strLocationFull;
|
---|
120 | uint64_t size;
|
---|
121 | Utf8Str strLastAccessError;
|
---|
122 |
|
---|
123 | BackRefList backRefs;
|
---|
124 |
|
---|
125 | size_t readers;
|
---|
126 | MediumState_T preLockState;
|
---|
127 |
|
---|
128 | RTSEMEVENTMULTI queryInfoSem;
|
---|
129 | bool queryInfoRunning : 1;
|
---|
130 |
|
---|
131 | const Utf8Str strFormat;
|
---|
132 | ComObjPtr<MediumFormat> formatObj;
|
---|
133 |
|
---|
134 | MediumType_T type;
|
---|
135 | DeviceType_T devType;
|
---|
136 | uint64_t logicalSize; /*< In MBytes. */
|
---|
137 |
|
---|
138 | HDDOpenMode hddOpenMode;
|
---|
139 |
|
---|
140 | bool autoReset : 1;
|
---|
141 |
|
---|
142 | /** the following members are invalid after changing UUID on open */
|
---|
143 | const Guid uuidImage;
|
---|
144 | const Guid uuidParentImage;
|
---|
145 |
|
---|
146 | bool hostDrive : 1;
|
---|
147 |
|
---|
148 | settings::StringsMap mapProperties;
|
---|
149 |
|
---|
150 | bool implicit : 1;
|
---|
151 |
|
---|
152 | uint32_t numCreateDiffTasks;
|
---|
153 |
|
---|
154 | Utf8Str vdError; /*< Error remembered by the VD error callback. */
|
---|
155 |
|
---|
156 | VDINTERFACE vdIfError;
|
---|
157 | VDINTERFACEERROR vdIfCallsError;
|
---|
158 |
|
---|
159 | VDINTERFACE vdIfConfig;
|
---|
160 | VDINTERFACECONFIG vdIfCallsConfig;
|
---|
161 |
|
---|
162 | VDINTERFACE vdIfTcpNet;
|
---|
163 | VDINTERFACETCPNET vdIfCallsTcpNet;
|
---|
164 |
|
---|
165 | PVDINTERFACE vdDiskIfaces;
|
---|
166 | };
|
---|
167 |
|
---|
168 | typedef struct VDSOCKETINT
|
---|
169 | {
|
---|
170 | /** Socket handle. */
|
---|
171 | RTSOCKET hSocket;
|
---|
172 | } VDSOCKETINT, *PVDSOCKETINT;
|
---|
173 |
|
---|
174 | ////////////////////////////////////////////////////////////////////////////////
|
---|
175 | //
|
---|
176 | // Globals
|
---|
177 | //
|
---|
178 | ////////////////////////////////////////////////////////////////////////////////
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Medium::Task class for asynchronous operations.
|
---|
182 | *
|
---|
183 | * @note Instances of this class must be created using new() because the
|
---|
184 | * task thread function will delete them when the task is complete.
|
---|
185 | *
|
---|
186 | * @note The constructor of this class adds a caller on the managed Medium
|
---|
187 | * object which is automatically released upon destruction.
|
---|
188 | */
|
---|
189 | class Medium::Task
|
---|
190 | {
|
---|
191 | public:
|
---|
192 | Task(Medium *aMedium, Progress *aProgress)
|
---|
193 | : mVDOperationIfaces(NULL),
|
---|
194 | m_pfNeedsSaveSettings(NULL),
|
---|
195 | mMedium(aMedium),
|
---|
196 | mMediumCaller(aMedium),
|
---|
197 | mThread(NIL_RTTHREAD),
|
---|
198 | mProgress(aProgress)
|
---|
199 | {
|
---|
200 | AssertReturnVoidStmt(aMedium, mRC = E_FAIL);
|
---|
201 | mRC = mMediumCaller.rc();
|
---|
202 | if (FAILED(mRC))
|
---|
203 | return;
|
---|
204 |
|
---|
205 | /* Set up a per-operation progress interface, can be used freely (for
|
---|
206 | * binary operations you can use it either on the source or target). */
|
---|
207 | mVDIfCallsProgress.cbSize = sizeof(VDINTERFACEPROGRESS);
|
---|
208 | mVDIfCallsProgress.enmInterface = VDINTERFACETYPE_PROGRESS;
|
---|
209 | mVDIfCallsProgress.pfnProgress = vdProgressCall;
|
---|
210 | int vrc = VDInterfaceAdd(&mVDIfProgress,
|
---|
211 | "Medium::Task::vdInterfaceProgress",
|
---|
212 | VDINTERFACETYPE_PROGRESS,
|
---|
213 | &mVDIfCallsProgress,
|
---|
214 | mProgress,
|
---|
215 | &mVDOperationIfaces);
|
---|
216 | AssertRC(vrc);
|
---|
217 | if (RT_FAILURE(vrc))
|
---|
218 | mRC = E_FAIL;
|
---|
219 | }
|
---|
220 |
|
---|
221 | // Make all destructors virtual. Just in case.
|
---|
222 | virtual ~Task()
|
---|
223 | {}
|
---|
224 |
|
---|
225 | HRESULT rc() const { return mRC; }
|
---|
226 | bool isOk() const { return SUCCEEDED(rc()); }
|
---|
227 |
|
---|
228 | static int fntMediumTask(RTTHREAD aThread, void *pvUser);
|
---|
229 |
|
---|
230 | bool isAsync() { return mThread != NIL_RTTHREAD; }
|
---|
231 |
|
---|
232 | PVDINTERFACE mVDOperationIfaces;
|
---|
233 |
|
---|
234 | // Whether the caller needs to call VirtualBox::saveSettings() after
|
---|
235 | // the task function returns. Only used in synchronous (wait) mode;
|
---|
236 | // otherwise the task will save the settings itself.
|
---|
237 | bool *m_pfNeedsSaveSettings;
|
---|
238 |
|
---|
239 | const ComObjPtr<Medium> mMedium;
|
---|
240 | AutoCaller mMediumCaller;
|
---|
241 |
|
---|
242 | friend HRESULT Medium::runNow(Medium::Task*, bool*);
|
---|
243 |
|
---|
244 | protected:
|
---|
245 | HRESULT mRC;
|
---|
246 | RTTHREAD mThread;
|
---|
247 |
|
---|
248 | private:
|
---|
249 | virtual HRESULT handler() = 0;
|
---|
250 |
|
---|
251 | const ComObjPtr<Progress> mProgress;
|
---|
252 |
|
---|
253 | static DECLCALLBACK(int) vdProgressCall(void *pvUser, unsigned uPercent);
|
---|
254 |
|
---|
255 | VDINTERFACE mVDIfProgress;
|
---|
256 | VDINTERFACEPROGRESS mVDIfCallsProgress;
|
---|
257 | };
|
---|
258 |
|
---|
259 | class Medium::CreateBaseTask : public Medium::Task
|
---|
260 | {
|
---|
261 | public:
|
---|
262 | CreateBaseTask(Medium *aMedium,
|
---|
263 | Progress *aProgress,
|
---|
264 | uint64_t aSize,
|
---|
265 | MediumVariant_T aVariant)
|
---|
266 | : Medium::Task(aMedium, aProgress),
|
---|
267 | mSize(aSize),
|
---|
268 | mVariant(aVariant)
|
---|
269 | {}
|
---|
270 |
|
---|
271 | uint64_t mSize;
|
---|
272 | MediumVariant_T mVariant;
|
---|
273 |
|
---|
274 | private:
|
---|
275 | virtual HRESULT handler();
|
---|
276 | };
|
---|
277 |
|
---|
278 | class Medium::CreateDiffTask : public Medium::Task
|
---|
279 | {
|
---|
280 | public:
|
---|
281 | CreateDiffTask(Medium *aMedium,
|
---|
282 | Progress *aProgress,
|
---|
283 | Medium *aTarget,
|
---|
284 | MediumVariant_T aVariant,
|
---|
285 | MediumLockList *aMediumLockList,
|
---|
286 | bool fKeepMediumLockList = false)
|
---|
287 | : Medium::Task(aMedium, aProgress),
|
---|
288 | mpMediumLockList(aMediumLockList),
|
---|
289 | mTarget(aTarget),
|
---|
290 | mVariant(aVariant),
|
---|
291 | mTargetCaller(aTarget),
|
---|
292 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
293 | {
|
---|
294 | AssertReturnVoidStmt(aTarget != NULL, mRC = E_FAIL);
|
---|
295 | mRC = mTargetCaller.rc();
|
---|
296 | if (FAILED(mRC))
|
---|
297 | return;
|
---|
298 | }
|
---|
299 |
|
---|
300 | ~CreateDiffTask()
|
---|
301 | {
|
---|
302 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
303 | delete mpMediumLockList;
|
---|
304 | }
|
---|
305 |
|
---|
306 | MediumLockList *mpMediumLockList;
|
---|
307 |
|
---|
308 | const ComObjPtr<Medium> mTarget;
|
---|
309 | MediumVariant_T mVariant;
|
---|
310 |
|
---|
311 | private:
|
---|
312 | virtual HRESULT handler();
|
---|
313 |
|
---|
314 | AutoCaller mTargetCaller;
|
---|
315 | bool mfKeepMediumLockList;
|
---|
316 | };
|
---|
317 |
|
---|
318 | class Medium::CloneTask : public Medium::Task
|
---|
319 | {
|
---|
320 | public:
|
---|
321 | CloneTask(Medium *aMedium,
|
---|
322 | Progress *aProgress,
|
---|
323 | Medium *aTarget,
|
---|
324 | MediumVariant_T aVariant,
|
---|
325 | Medium *aParent,
|
---|
326 | MediumLockList *aSourceMediumLockList,
|
---|
327 | MediumLockList *aTargetMediumLockList,
|
---|
328 | bool fKeepSourceMediumLockList = false,
|
---|
329 | bool fKeepTargetMediumLockList = false)
|
---|
330 | : Medium::Task(aMedium, aProgress),
|
---|
331 | mTarget(aTarget),
|
---|
332 | mParent(aParent),
|
---|
333 | mpSourceMediumLockList(aSourceMediumLockList),
|
---|
334 | mpTargetMediumLockList(aTargetMediumLockList),
|
---|
335 | mVariant(aVariant),
|
---|
336 | mTargetCaller(aTarget),
|
---|
337 | mParentCaller(aParent),
|
---|
338 | mfKeepSourceMediumLockList(fKeepSourceMediumLockList),
|
---|
339 | mfKeepTargetMediumLockList(fKeepTargetMediumLockList)
|
---|
340 | {
|
---|
341 | AssertReturnVoidStmt(aTarget != NULL, mRC = E_FAIL);
|
---|
342 | mRC = mTargetCaller.rc();
|
---|
343 | if (FAILED(mRC))
|
---|
344 | return;
|
---|
345 | /* aParent may be NULL */
|
---|
346 | mRC = mParentCaller.rc();
|
---|
347 | if (FAILED(mRC))
|
---|
348 | return;
|
---|
349 | AssertReturnVoidStmt(aSourceMediumLockList != NULL, mRC = E_FAIL);
|
---|
350 | AssertReturnVoidStmt(aTargetMediumLockList != NULL, mRC = E_FAIL);
|
---|
351 | }
|
---|
352 |
|
---|
353 | ~CloneTask()
|
---|
354 | {
|
---|
355 | if (!mfKeepSourceMediumLockList && mpSourceMediumLockList)
|
---|
356 | delete mpSourceMediumLockList;
|
---|
357 | if (!mfKeepTargetMediumLockList && mpTargetMediumLockList)
|
---|
358 | delete mpTargetMediumLockList;
|
---|
359 | }
|
---|
360 |
|
---|
361 | const ComObjPtr<Medium> mTarget;
|
---|
362 | const ComObjPtr<Medium> mParent;
|
---|
363 | MediumLockList *mpSourceMediumLockList;
|
---|
364 | MediumLockList *mpTargetMediumLockList;
|
---|
365 | MediumVariant_T mVariant;
|
---|
366 |
|
---|
367 | private:
|
---|
368 | virtual HRESULT handler();
|
---|
369 |
|
---|
370 | AutoCaller mTargetCaller;
|
---|
371 | AutoCaller mParentCaller;
|
---|
372 | bool mfKeepSourceMediumLockList;
|
---|
373 | bool mfKeepTargetMediumLockList;
|
---|
374 | };
|
---|
375 |
|
---|
376 | class Medium::CompactTask : public Medium::Task
|
---|
377 | {
|
---|
378 | public:
|
---|
379 | CompactTask(Medium *aMedium,
|
---|
380 | Progress *aProgress,
|
---|
381 | MediumLockList *aMediumLockList,
|
---|
382 | bool fKeepMediumLockList = false)
|
---|
383 | : Medium::Task(aMedium, aProgress),
|
---|
384 | mpMediumLockList(aMediumLockList),
|
---|
385 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
386 | {
|
---|
387 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
388 | }
|
---|
389 |
|
---|
390 | ~CompactTask()
|
---|
391 | {
|
---|
392 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
393 | delete mpMediumLockList;
|
---|
394 | }
|
---|
395 |
|
---|
396 | MediumLockList *mpMediumLockList;
|
---|
397 |
|
---|
398 | private:
|
---|
399 | virtual HRESULT handler();
|
---|
400 |
|
---|
401 | bool mfKeepMediumLockList;
|
---|
402 | };
|
---|
403 |
|
---|
404 | class Medium::ResetTask : public Medium::Task
|
---|
405 | {
|
---|
406 | public:
|
---|
407 | ResetTask(Medium *aMedium,
|
---|
408 | Progress *aProgress,
|
---|
409 | MediumLockList *aMediumLockList,
|
---|
410 | bool fKeepMediumLockList = false)
|
---|
411 | : Medium::Task(aMedium, aProgress),
|
---|
412 | mpMediumLockList(aMediumLockList),
|
---|
413 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
414 | {}
|
---|
415 |
|
---|
416 | ~ResetTask()
|
---|
417 | {
|
---|
418 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
419 | delete mpMediumLockList;
|
---|
420 | }
|
---|
421 |
|
---|
422 | MediumLockList *mpMediumLockList;
|
---|
423 |
|
---|
424 | private:
|
---|
425 | virtual HRESULT handler();
|
---|
426 |
|
---|
427 | bool mfKeepMediumLockList;
|
---|
428 | };
|
---|
429 |
|
---|
430 | class Medium::DeleteTask : public Medium::Task
|
---|
431 | {
|
---|
432 | public:
|
---|
433 | DeleteTask(Medium *aMedium,
|
---|
434 | Progress *aProgress,
|
---|
435 | MediumLockList *aMediumLockList,
|
---|
436 | bool fKeepMediumLockList = false)
|
---|
437 | : Medium::Task(aMedium, aProgress),
|
---|
438 | mpMediumLockList(aMediumLockList),
|
---|
439 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
440 | {}
|
---|
441 |
|
---|
442 | ~DeleteTask()
|
---|
443 | {
|
---|
444 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
445 | delete mpMediumLockList;
|
---|
446 | }
|
---|
447 |
|
---|
448 | MediumLockList *mpMediumLockList;
|
---|
449 |
|
---|
450 | private:
|
---|
451 | virtual HRESULT handler();
|
---|
452 |
|
---|
453 | bool mfKeepMediumLockList;
|
---|
454 | };
|
---|
455 |
|
---|
456 | class Medium::MergeTask : public Medium::Task
|
---|
457 | {
|
---|
458 | public:
|
---|
459 | MergeTask(Medium *aMedium,
|
---|
460 | Medium *aTarget,
|
---|
461 | bool fMergeForward,
|
---|
462 | Medium *aParentForTarget,
|
---|
463 | const MediaList &aChildrenToReparent,
|
---|
464 | Progress *aProgress,
|
---|
465 | MediumLockList *aMediumLockList,
|
---|
466 | bool fKeepMediumLockList = false)
|
---|
467 | : Medium::Task(aMedium, aProgress),
|
---|
468 | mTarget(aTarget),
|
---|
469 | mfMergeForward(fMergeForward),
|
---|
470 | mParentForTarget(aParentForTarget),
|
---|
471 | mChildrenToReparent(aChildrenToReparent),
|
---|
472 | mpMediumLockList(aMediumLockList),
|
---|
473 | mTargetCaller(aTarget),
|
---|
474 | mParentForTargetCaller(aParentForTarget),
|
---|
475 | mfChildrenCaller(false),
|
---|
476 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
477 | {
|
---|
478 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
479 | for (MediaList::const_iterator it = mChildrenToReparent.begin();
|
---|
480 | it != mChildrenToReparent.end();
|
---|
481 | ++it)
|
---|
482 | {
|
---|
483 | HRESULT rc2 = (*it)->addCaller();
|
---|
484 | if (FAILED(rc2))
|
---|
485 | {
|
---|
486 | mRC = E_FAIL;
|
---|
487 | for (MediaList::const_iterator it2 = mChildrenToReparent.begin();
|
---|
488 | it2 != it;
|
---|
489 | --it2)
|
---|
490 | {
|
---|
491 | (*it2)->releaseCaller();
|
---|
492 | }
|
---|
493 | return;
|
---|
494 | }
|
---|
495 | }
|
---|
496 | mfChildrenCaller = true;
|
---|
497 | }
|
---|
498 |
|
---|
499 | ~MergeTask()
|
---|
500 | {
|
---|
501 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
502 | delete mpMediumLockList;
|
---|
503 | if (mfChildrenCaller)
|
---|
504 | {
|
---|
505 | for (MediaList::const_iterator it = mChildrenToReparent.begin();
|
---|
506 | it != mChildrenToReparent.end();
|
---|
507 | ++it)
|
---|
508 | {
|
---|
509 | (*it)->releaseCaller();
|
---|
510 | }
|
---|
511 | }
|
---|
512 | }
|
---|
513 |
|
---|
514 | const ComObjPtr<Medium> mTarget;
|
---|
515 | bool mfMergeForward;
|
---|
516 | /* When mChildrenToReparent is empty then mParentForTarget is non-null.
|
---|
517 | * In other words: they are used in different cases. */
|
---|
518 | const ComObjPtr<Medium> mParentForTarget;
|
---|
519 | MediaList mChildrenToReparent;
|
---|
520 | MediumLockList *mpMediumLockList;
|
---|
521 |
|
---|
522 | private:
|
---|
523 | virtual HRESULT handler();
|
---|
524 |
|
---|
525 | AutoCaller mTargetCaller;
|
---|
526 | AutoCaller mParentForTargetCaller;
|
---|
527 | bool mfChildrenCaller;
|
---|
528 | bool mfKeepMediumLockList;
|
---|
529 | };
|
---|
530 |
|
---|
531 | /**
|
---|
532 | * Thread function for time-consuming medium tasks.
|
---|
533 | *
|
---|
534 | * @param pvUser Pointer to the Medium::Task instance.
|
---|
535 | */
|
---|
536 | /* static */
|
---|
537 | DECLCALLBACK(int) Medium::Task::fntMediumTask(RTTHREAD aThread, void *pvUser)
|
---|
538 | {
|
---|
539 | LogFlowFuncEnter();
|
---|
540 | AssertReturn(pvUser, (int)E_INVALIDARG);
|
---|
541 | Medium::Task *pTask = static_cast<Medium::Task *>(pvUser);
|
---|
542 |
|
---|
543 | pTask->mThread = aThread;
|
---|
544 |
|
---|
545 | HRESULT rc = pTask->handler();
|
---|
546 |
|
---|
547 | /* complete the progress if run asynchronously */
|
---|
548 | if (pTask->isAsync())
|
---|
549 | {
|
---|
550 | if (!pTask->mProgress.isNull())
|
---|
551 | pTask->mProgress->notifyComplete(rc);
|
---|
552 | }
|
---|
553 |
|
---|
554 | /* pTask is no longer needed, delete it. */
|
---|
555 | delete pTask;
|
---|
556 |
|
---|
557 | LogFlowFunc(("rc=%Rhrc\n", rc));
|
---|
558 | LogFlowFuncLeave();
|
---|
559 |
|
---|
560 | return (int)rc;
|
---|
561 | }
|
---|
562 |
|
---|
563 | /**
|
---|
564 | * PFNVDPROGRESS callback handler for Task operations.
|
---|
565 | *
|
---|
566 | * @param pvUser Pointer to the Progress instance.
|
---|
567 | * @param uPercent Completetion precentage (0-100).
|
---|
568 | */
|
---|
569 | /*static*/
|
---|
570 | DECLCALLBACK(int) Medium::Task::vdProgressCall(void *pvUser, unsigned uPercent)
|
---|
571 | {
|
---|
572 | Progress *that = static_cast<Progress *>(pvUser);
|
---|
573 |
|
---|
574 | if (that != NULL)
|
---|
575 | {
|
---|
576 | /* update the progress object, capping it at 99% as the final percent
|
---|
577 | * is used for additional operations like setting the UUIDs and similar. */
|
---|
578 | HRESULT rc = that->SetCurrentOperationProgress(uPercent * 99 / 100);
|
---|
579 | if (FAILED(rc))
|
---|
580 | {
|
---|
581 | if (rc == E_FAIL)
|
---|
582 | return VERR_CANCELLED;
|
---|
583 | else
|
---|
584 | return VERR_INVALID_STATE;
|
---|
585 | }
|
---|
586 | }
|
---|
587 |
|
---|
588 | return VINF_SUCCESS;
|
---|
589 | }
|
---|
590 |
|
---|
591 | /**
|
---|
592 | * Implementation code for the "create base" task.
|
---|
593 | */
|
---|
594 | HRESULT Medium::CreateBaseTask::handler()
|
---|
595 | {
|
---|
596 | return mMedium->taskCreateBaseHandler(*this);
|
---|
597 | }
|
---|
598 |
|
---|
599 | /**
|
---|
600 | * Implementation code for the "create diff" task.
|
---|
601 | */
|
---|
602 | HRESULT Medium::CreateDiffTask::handler()
|
---|
603 | {
|
---|
604 | return mMedium->taskCreateDiffHandler(*this);
|
---|
605 | }
|
---|
606 |
|
---|
607 | /**
|
---|
608 | * Implementation code for the "clone" task.
|
---|
609 | */
|
---|
610 | HRESULT Medium::CloneTask::handler()
|
---|
611 | {
|
---|
612 | return mMedium->taskCloneHandler(*this);
|
---|
613 | }
|
---|
614 |
|
---|
615 | /**
|
---|
616 | * Implementation code for the "compact" task.
|
---|
617 | */
|
---|
618 | HRESULT Medium::CompactTask::handler()
|
---|
619 | {
|
---|
620 | return mMedium->taskCompactHandler(*this);
|
---|
621 | }
|
---|
622 |
|
---|
623 | /**
|
---|
624 | * Implementation code for the "reset" task.
|
---|
625 | */
|
---|
626 | HRESULT Medium::ResetTask::handler()
|
---|
627 | {
|
---|
628 | return mMedium->taskResetHandler(*this);
|
---|
629 | }
|
---|
630 |
|
---|
631 | /**
|
---|
632 | * Implementation code for the "delete" task.
|
---|
633 | */
|
---|
634 | HRESULT Medium::DeleteTask::handler()
|
---|
635 | {
|
---|
636 | return mMedium->taskDeleteHandler(*this);
|
---|
637 | }
|
---|
638 |
|
---|
639 | /**
|
---|
640 | * Implementation code for the "merge" task.
|
---|
641 | */
|
---|
642 | HRESULT Medium::MergeTask::handler()
|
---|
643 | {
|
---|
644 | return mMedium->taskMergeHandler(*this);
|
---|
645 | }
|
---|
646 |
|
---|
647 |
|
---|
648 | ////////////////////////////////////////////////////////////////////////////////
|
---|
649 | //
|
---|
650 | // Medium constructor / destructor
|
---|
651 | //
|
---|
652 | ////////////////////////////////////////////////////////////////////////////////
|
---|
653 |
|
---|
654 | DEFINE_EMPTY_CTOR_DTOR(Medium)
|
---|
655 |
|
---|
656 | HRESULT Medium::FinalConstruct()
|
---|
657 | {
|
---|
658 | m = new Data;
|
---|
659 |
|
---|
660 | /* Initialize the callbacks of the VD error interface */
|
---|
661 | m->vdIfCallsError.cbSize = sizeof(VDINTERFACEERROR);
|
---|
662 | m->vdIfCallsError.enmInterface = VDINTERFACETYPE_ERROR;
|
---|
663 | m->vdIfCallsError.pfnError = vdErrorCall;
|
---|
664 | m->vdIfCallsError.pfnMessage = NULL;
|
---|
665 |
|
---|
666 | /* Initialize the callbacks of the VD config interface */
|
---|
667 | m->vdIfCallsConfig.cbSize = sizeof(VDINTERFACECONFIG);
|
---|
668 | m->vdIfCallsConfig.enmInterface = VDINTERFACETYPE_CONFIG;
|
---|
669 | m->vdIfCallsConfig.pfnAreKeysValid = vdConfigAreKeysValid;
|
---|
670 | m->vdIfCallsConfig.pfnQuerySize = vdConfigQuerySize;
|
---|
671 | m->vdIfCallsConfig.pfnQuery = vdConfigQuery;
|
---|
672 |
|
---|
673 | /* Initialize the callbacks of the VD TCP interface (we always use the host
|
---|
674 | * IP stack for now) */
|
---|
675 | m->vdIfCallsTcpNet.cbSize = sizeof(VDINTERFACETCPNET);
|
---|
676 | m->vdIfCallsTcpNet.enmInterface = VDINTERFACETYPE_TCPNET;
|
---|
677 | m->vdIfCallsTcpNet.pfnSocketCreate = vdTcpSocketCreate;
|
---|
678 | m->vdIfCallsTcpNet.pfnSocketDestroy = vdTcpSocketDestroy;
|
---|
679 | m->vdIfCallsTcpNet.pfnClientConnect = vdTcpClientConnect;
|
---|
680 | m->vdIfCallsTcpNet.pfnClientClose = vdTcpClientClose;
|
---|
681 | m->vdIfCallsTcpNet.pfnIsClientConnected = vdTcpIsClientConnected;
|
---|
682 | m->vdIfCallsTcpNet.pfnSelectOne = vdTcpSelectOne;
|
---|
683 | m->vdIfCallsTcpNet.pfnRead = vdTcpRead;
|
---|
684 | m->vdIfCallsTcpNet.pfnWrite = vdTcpWrite;
|
---|
685 | m->vdIfCallsTcpNet.pfnSgWrite = vdTcpSgWrite;
|
---|
686 | m->vdIfCallsTcpNet.pfnFlush = vdTcpFlush;
|
---|
687 | m->vdIfCallsTcpNet.pfnSetSendCoalescing = vdTcpSetSendCoalescing;
|
---|
688 | m->vdIfCallsTcpNet.pfnGetLocalAddress = vdTcpGetLocalAddress;
|
---|
689 | m->vdIfCallsTcpNet.pfnGetPeerAddress = vdTcpGetPeerAddress;
|
---|
690 | m->vdIfCallsTcpNet.pfnSelectOneEx = NULL;
|
---|
691 | m->vdIfCallsTcpNet.pfnPoke = NULL;
|
---|
692 |
|
---|
693 | /* Initialize the per-disk interface chain */
|
---|
694 | int vrc;
|
---|
695 | vrc = VDInterfaceAdd(&m->vdIfError,
|
---|
696 | "Medium::vdInterfaceError",
|
---|
697 | VDINTERFACETYPE_ERROR,
|
---|
698 | &m->vdIfCallsError, this, &m->vdDiskIfaces);
|
---|
699 | AssertRCReturn(vrc, E_FAIL);
|
---|
700 |
|
---|
701 | vrc = VDInterfaceAdd(&m->vdIfConfig,
|
---|
702 | "Medium::vdInterfaceConfig",
|
---|
703 | VDINTERFACETYPE_CONFIG,
|
---|
704 | &m->vdIfCallsConfig, this, &m->vdDiskIfaces);
|
---|
705 | AssertRCReturn(vrc, E_FAIL);
|
---|
706 |
|
---|
707 | vrc = VDInterfaceAdd(&m->vdIfTcpNet,
|
---|
708 | "Medium::vdInterfaceTcpNet",
|
---|
709 | VDINTERFACETYPE_TCPNET,
|
---|
710 | &m->vdIfCallsTcpNet, this, &m->vdDiskIfaces);
|
---|
711 | AssertRCReturn(vrc, E_FAIL);
|
---|
712 |
|
---|
713 | vrc = RTSemEventMultiCreate(&m->queryInfoSem);
|
---|
714 | AssertRCReturn(vrc, E_FAIL);
|
---|
715 | vrc = RTSemEventMultiSignal(m->queryInfoSem);
|
---|
716 | AssertRCReturn(vrc, E_FAIL);
|
---|
717 |
|
---|
718 | return S_OK;
|
---|
719 | }
|
---|
720 |
|
---|
721 | void Medium::FinalRelease()
|
---|
722 | {
|
---|
723 | uninit();
|
---|
724 |
|
---|
725 | delete m;
|
---|
726 | }
|
---|
727 |
|
---|
728 | /**
|
---|
729 | * Initializes an empty hard disk object without creating or opening an associated
|
---|
730 | * storage unit.
|
---|
731 | *
|
---|
732 | * This gets called by VirtualBox::CreateHardDisk().
|
---|
733 | *
|
---|
734 | * For hard disks that don't have the VD_CAP_CREATE_FIXED or
|
---|
735 | * VD_CAP_CREATE_DYNAMIC capability (and therefore cannot be created or deleted
|
---|
736 | * with the means of VirtualBox) the associated storage unit is assumed to be
|
---|
737 | * ready for use so the state of the hard disk object will be set to Created.
|
---|
738 | *
|
---|
739 | * @param aVirtualBox VirtualBox object.
|
---|
740 | * @param aLocation Storage unit location.
|
---|
741 | * @param pfNeedsSaveSettings Optional pointer to a bool that must have been initialized to false and that will be set to true
|
---|
742 | * by this function if the caller should invoke VirtualBox::saveSettings() because the global settings have changed.
|
---|
743 | */
|
---|
744 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
745 | const Utf8Str &aFormat,
|
---|
746 | const Utf8Str &aLocation,
|
---|
747 | bool *pfNeedsSaveSettings)
|
---|
748 | {
|
---|
749 | AssertReturn(aVirtualBox != NULL, E_FAIL);
|
---|
750 | AssertReturn(!aFormat.isEmpty(), E_FAIL);
|
---|
751 |
|
---|
752 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
753 | AutoInitSpan autoInitSpan(this);
|
---|
754 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
755 |
|
---|
756 | HRESULT rc = S_OK;
|
---|
757 |
|
---|
758 | /* share VirtualBox weakly (parent remains NULL so far) */
|
---|
759 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
760 |
|
---|
761 | /* no storage yet */
|
---|
762 | m->state = MediumState_NotCreated;
|
---|
763 |
|
---|
764 | /* cannot be a host drive */
|
---|
765 | m->hostDrive = false;
|
---|
766 |
|
---|
767 | /* No storage unit is created yet, no need to queryInfo() */
|
---|
768 |
|
---|
769 | rc = setFormat(aFormat);
|
---|
770 | if (FAILED(rc)) return rc;
|
---|
771 |
|
---|
772 | if (m->formatObj->getCapabilities() & MediumFormatCapabilities_File)
|
---|
773 | {
|
---|
774 | rc = setLocation(aLocation);
|
---|
775 | if (FAILED(rc)) return rc;
|
---|
776 | }
|
---|
777 | else
|
---|
778 | {
|
---|
779 | rc = setLocation(aLocation);
|
---|
780 | if (FAILED(rc)) return rc;
|
---|
781 | }
|
---|
782 |
|
---|
783 | if (!(m->formatObj->getCapabilities() & ( MediumFormatCapabilities_CreateFixed
|
---|
784 | | MediumFormatCapabilities_CreateDynamic))
|
---|
785 | )
|
---|
786 | {
|
---|
787 | /* storage for hard disks of this format can neither be explicitly
|
---|
788 | * created by VirtualBox nor deleted, so we place the hard disk to
|
---|
789 | * Created state here and also add it to the registry */
|
---|
790 | m->state = MediumState_Created;
|
---|
791 | unconst(m->id).create();
|
---|
792 |
|
---|
793 | AutoWriteLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
794 | rc = m->pVirtualBox->registerHardDisk(this, pfNeedsSaveSettings);
|
---|
795 | }
|
---|
796 |
|
---|
797 | /* Confirm a successful initialization when it's the case */
|
---|
798 | if (SUCCEEDED(rc))
|
---|
799 | autoInitSpan.setSucceeded();
|
---|
800 |
|
---|
801 | return rc;
|
---|
802 | }
|
---|
803 |
|
---|
804 | /**
|
---|
805 | * Initializes the medium object by opening the storage unit at the specified
|
---|
806 | * location. The enOpenMode parameter defines whether the medium will be opened
|
---|
807 | * read/write or read-only.
|
---|
808 | *
|
---|
809 | * This gets called by VirtualBox::OpenHardDisk(), OpenDVDImage and OpenFloppyImage();
|
---|
810 | * this also gets called by Machine::AttachDevice() and createImplicitDiffs()
|
---|
811 | * when new diff images are created.
|
---|
812 | *
|
---|
813 | * Note that the UUID, format and the parent of this medium will be
|
---|
814 | * determined when reading the medium storage unit, unless new values are
|
---|
815 | * specified by the parameters. If the detected or set parent is
|
---|
816 | * not known to VirtualBox, then this method will fail.
|
---|
817 | *
|
---|
818 | * @param aVirtualBox VirtualBox object.
|
---|
819 | * @param aLocation Storage unit location.
|
---|
820 | * @param enOpenMode Whether to open the medium read/write or read-only.
|
---|
821 | * @param aDeviceType Device type of medium.
|
---|
822 | */
|
---|
823 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
824 | const Utf8Str &aLocation,
|
---|
825 | HDDOpenMode enOpenMode,
|
---|
826 | DeviceType_T aDeviceType)
|
---|
827 | {
|
---|
828 | AssertReturn(aVirtualBox, E_INVALIDARG);
|
---|
829 | AssertReturn(!aLocation.isEmpty(), E_INVALIDARG);
|
---|
830 |
|
---|
831 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
832 | AutoInitSpan autoInitSpan(this);
|
---|
833 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
834 |
|
---|
835 | HRESULT rc = S_OK;
|
---|
836 |
|
---|
837 | /* share VirtualBox weakly (parent remains NULL so far) */
|
---|
838 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
839 |
|
---|
840 | /* there must be a storage unit */
|
---|
841 | m->state = MediumState_Created;
|
---|
842 |
|
---|
843 | /* remember device type for correct unregistering later */
|
---|
844 | m->devType = aDeviceType;
|
---|
845 |
|
---|
846 | /* cannot be a host drive */
|
---|
847 | m->hostDrive = false;
|
---|
848 |
|
---|
849 | /* remember the open mode (defaults to ReadWrite) */
|
---|
850 | m->hddOpenMode = enOpenMode;
|
---|
851 |
|
---|
852 | if (aDeviceType == DeviceType_HardDisk)
|
---|
853 | rc = setLocation(aLocation);
|
---|
854 | else
|
---|
855 | rc = setLocation(aLocation, "RAW");
|
---|
856 | if (FAILED(rc)) return rc;
|
---|
857 |
|
---|
858 | /* get all the information about the medium from the storage unit */
|
---|
859 | rc = queryInfo(false /* fSetImageId */, false /* fSetParentId */);
|
---|
860 |
|
---|
861 | if (SUCCEEDED(rc))
|
---|
862 | {
|
---|
863 | /* if the storage unit is not accessible, it's not acceptable for the
|
---|
864 | * newly opened media so convert this into an error */
|
---|
865 | if (m->state == MediumState_Inaccessible)
|
---|
866 | {
|
---|
867 | Assert(!m->strLastAccessError.isEmpty());
|
---|
868 | rc = setError(E_FAIL, "%s", m->strLastAccessError.c_str());
|
---|
869 | }
|
---|
870 | else
|
---|
871 | {
|
---|
872 | AssertReturn(!m->id.isEmpty(), E_FAIL);
|
---|
873 |
|
---|
874 | /* storage format must be detected by queryInfo() if the medium is accessible */
|
---|
875 | AssertReturn(!m->strFormat.isEmpty(), E_FAIL);
|
---|
876 | }
|
---|
877 | }
|
---|
878 |
|
---|
879 | /* Confirm a successful initialization when it's the case */
|
---|
880 | if (SUCCEEDED(rc))
|
---|
881 | autoInitSpan.setSucceeded();
|
---|
882 |
|
---|
883 | return rc;
|
---|
884 | }
|
---|
885 |
|
---|
886 | /**
|
---|
887 | * Initializes the medium object by loading its data from the given settings
|
---|
888 | * node. In this mode, the medium will always be opened read/write.
|
---|
889 | *
|
---|
890 | * @param aVirtualBox VirtualBox object.
|
---|
891 | * @param aParent Parent medium disk or NULL for a root (base) medium.
|
---|
892 | * @param aDeviceType Device type of the medium.
|
---|
893 | * @param aNode Configuration settings.
|
---|
894 | *
|
---|
895 | * @note Locks VirtualBox for writing, the medium tree for writing.
|
---|
896 | */
|
---|
897 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
898 | Medium *aParent,
|
---|
899 | DeviceType_T aDeviceType,
|
---|
900 | const settings::Medium &data)
|
---|
901 | {
|
---|
902 | using namespace settings;
|
---|
903 |
|
---|
904 | AssertReturn(aVirtualBox, E_INVALIDARG);
|
---|
905 |
|
---|
906 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
907 | AutoInitSpan autoInitSpan(this);
|
---|
908 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
909 |
|
---|
910 | HRESULT rc = S_OK;
|
---|
911 |
|
---|
912 | /* share VirtualBox and parent weakly */
|
---|
913 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
914 |
|
---|
915 | /* register with VirtualBox/parent early, since uninit() will
|
---|
916 | * unconditionally unregister on failure */
|
---|
917 | if (aParent)
|
---|
918 | {
|
---|
919 | // differencing medium: add to parent
|
---|
920 | AutoWriteLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
921 | m->pParent = aParent;
|
---|
922 | aParent->m->llChildren.push_back(this);
|
---|
923 | }
|
---|
924 |
|
---|
925 | /* see below why we don't call queryInfo() (and therefore treat the medium
|
---|
926 | * as inaccessible for now */
|
---|
927 | m->state = MediumState_Inaccessible;
|
---|
928 | m->strLastAccessError = tr("Accessibility check was not yet performed");
|
---|
929 |
|
---|
930 | /* required */
|
---|
931 | unconst(m->id) = data.uuid;
|
---|
932 |
|
---|
933 | /* assume not a host drive */
|
---|
934 | m->hostDrive = false;
|
---|
935 |
|
---|
936 | /* optional */
|
---|
937 | m->strDescription = data.strDescription;
|
---|
938 |
|
---|
939 | /* required */
|
---|
940 | if (aDeviceType == DeviceType_HardDisk)
|
---|
941 | {
|
---|
942 | AssertReturn(!data.strFormat.isEmpty(), E_FAIL);
|
---|
943 | rc = setFormat(data.strFormat);
|
---|
944 | if (FAILED(rc)) return rc;
|
---|
945 | }
|
---|
946 | else
|
---|
947 | {
|
---|
948 | /// @todo handle host drive settings here as well?
|
---|
949 | if (!data.strFormat.isEmpty())
|
---|
950 | rc = setFormat(data.strFormat);
|
---|
951 | else
|
---|
952 | rc = setFormat("RAW");
|
---|
953 | if (FAILED(rc)) return rc;
|
---|
954 | }
|
---|
955 |
|
---|
956 | /* optional, only for diffs, default is false; we can only auto-reset
|
---|
957 | * diff media so they must have a parent */
|
---|
958 | if (aParent != NULL)
|
---|
959 | m->autoReset = data.fAutoReset;
|
---|
960 | else
|
---|
961 | m->autoReset = false;
|
---|
962 |
|
---|
963 | /* properties (after setting the format as it populates the map). Note that
|
---|
964 | * if some properties are not supported but preseint in the settings file,
|
---|
965 | * they will still be read and accessible (for possible backward
|
---|
966 | * compatibility; we can also clean them up from the XML upon next
|
---|
967 | * XML format version change if we wish) */
|
---|
968 | for (settings::StringsMap::const_iterator it = data.properties.begin();
|
---|
969 | it != data.properties.end();
|
---|
970 | ++it)
|
---|
971 | {
|
---|
972 | const Utf8Str &name = it->first;
|
---|
973 | const Utf8Str &value = it->second;
|
---|
974 | m->mapProperties[name] = value;
|
---|
975 | }
|
---|
976 |
|
---|
977 | /* required */
|
---|
978 | rc = setLocation(data.strLocation);
|
---|
979 | if (FAILED(rc)) return rc;
|
---|
980 |
|
---|
981 | if (aDeviceType == DeviceType_HardDisk)
|
---|
982 | {
|
---|
983 | /* type is only for base hard disks */
|
---|
984 | if (m->pParent.isNull())
|
---|
985 | m->type = data.hdType;
|
---|
986 | }
|
---|
987 | else
|
---|
988 | m->type = MediumType_Writethrough;
|
---|
989 |
|
---|
990 | /* remember device type for correct unregistering later */
|
---|
991 | m->devType = aDeviceType;
|
---|
992 |
|
---|
993 | LogFlowThisFunc(("m->strLocationFull='%s', m->strFormat=%s, m->id={%RTuuid}\n",
|
---|
994 | m->strLocationFull.c_str(), m->strFormat.c_str(), m->id.raw()));
|
---|
995 |
|
---|
996 | /* Don't call queryInfo() for registered media to prevent the calling
|
---|
997 | * thread (i.e. the VirtualBox server startup thread) from an unexpected
|
---|
998 | * freeze but mark it as initially inaccessible instead. The vital UUID,
|
---|
999 | * location and format properties are read from the registry file above; to
|
---|
1000 | * get the actual state and the rest of the data, the user will have to call
|
---|
1001 | * COMGETTER(State). */
|
---|
1002 |
|
---|
1003 | AutoWriteLock treeLock(aVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1004 |
|
---|
1005 | /* load all children */
|
---|
1006 | for (settings::MediaList::const_iterator it = data.llChildren.begin();
|
---|
1007 | it != data.llChildren.end();
|
---|
1008 | ++it)
|
---|
1009 | {
|
---|
1010 | const settings::Medium &med = *it;
|
---|
1011 |
|
---|
1012 | ComObjPtr<Medium> pHD;
|
---|
1013 | pHD.createObject();
|
---|
1014 | rc = pHD->init(aVirtualBox,
|
---|
1015 | this, // parent
|
---|
1016 | aDeviceType,
|
---|
1017 | med); // child data
|
---|
1018 | if (FAILED(rc)) break;
|
---|
1019 |
|
---|
1020 | rc = m->pVirtualBox->registerHardDisk(pHD, NULL /*pfNeedsSaveSettings*/);
|
---|
1021 | if (FAILED(rc)) break;
|
---|
1022 | }
|
---|
1023 |
|
---|
1024 | /* Confirm a successful initialization when it's the case */
|
---|
1025 | if (SUCCEEDED(rc))
|
---|
1026 | autoInitSpan.setSucceeded();
|
---|
1027 |
|
---|
1028 | return rc;
|
---|
1029 | }
|
---|
1030 |
|
---|
1031 | /**
|
---|
1032 | * Initializes the medium object by providing the host drive information.
|
---|
1033 | * Not used for anything but the host floppy/host DVD case.
|
---|
1034 | *
|
---|
1035 | * @param aVirtualBox VirtualBox object.
|
---|
1036 | * @param aDeviceType Device type of the medium.
|
---|
1037 | * @param aLocation Location of the host drive.
|
---|
1038 | * @param aDescription Comment for this host drive.
|
---|
1039 | *
|
---|
1040 | * @note Locks VirtualBox lock for writing.
|
---|
1041 | */
|
---|
1042 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1043 | DeviceType_T aDeviceType,
|
---|
1044 | const Utf8Str &aLocation,
|
---|
1045 | const Utf8Str &aDescription /* = Utf8Str::Empty */)
|
---|
1046 | {
|
---|
1047 | ComAssertRet(aDeviceType == DeviceType_DVD || aDeviceType == DeviceType_Floppy, E_INVALIDARG);
|
---|
1048 | ComAssertRet(!aLocation.isEmpty(), E_INVALIDARG);
|
---|
1049 |
|
---|
1050 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1051 | AutoInitSpan autoInitSpan(this);
|
---|
1052 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1053 |
|
---|
1054 | /* share VirtualBox weakly (parent remains NULL so far) */
|
---|
1055 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1056 |
|
---|
1057 | /* fake up a UUID which is unique, but also reproducible */
|
---|
1058 | RTUUID uuid;
|
---|
1059 | RTUuidClear(&uuid);
|
---|
1060 | if (aDeviceType == DeviceType_DVD)
|
---|
1061 | memcpy(&uuid.au8[0], "DVD", 3);
|
---|
1062 | else
|
---|
1063 | memcpy(&uuid.au8[0], "FD", 2);
|
---|
1064 | /* use device name, adjusted to the end of uuid, shortened if necessary */
|
---|
1065 | size_t lenLocation = aLocation.length();
|
---|
1066 | if (lenLocation > 12)
|
---|
1067 | memcpy(&uuid.au8[4], aLocation.c_str() + (lenLocation - 12), 12);
|
---|
1068 | else
|
---|
1069 | memcpy(&uuid.au8[4 + 12 - lenLocation], aLocation.c_str(), lenLocation);
|
---|
1070 | unconst(m->id) = uuid;
|
---|
1071 |
|
---|
1072 | m->type = MediumType_Writethrough;
|
---|
1073 | m->devType = aDeviceType;
|
---|
1074 | m->state = MediumState_Created;
|
---|
1075 | m->hostDrive = true;
|
---|
1076 | HRESULT rc = setFormat("RAW");
|
---|
1077 | if (FAILED(rc)) return rc;
|
---|
1078 | rc = setLocation(aLocation);
|
---|
1079 | if (FAILED(rc)) return rc;
|
---|
1080 | m->strDescription = aDescription;
|
---|
1081 |
|
---|
1082 | /// @todo generate uuid (similarly to host network interface uuid) from location and device type
|
---|
1083 |
|
---|
1084 | autoInitSpan.setSucceeded();
|
---|
1085 | return S_OK;
|
---|
1086 | }
|
---|
1087 |
|
---|
1088 | /**
|
---|
1089 | * Uninitializes the instance.
|
---|
1090 | *
|
---|
1091 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
1092 | *
|
---|
1093 | * @note All children of this medium get uninitialized by calling their
|
---|
1094 | * uninit() methods.
|
---|
1095 | *
|
---|
1096 | * @note Caller must hold the tree lock of the medium tree this medium is on.
|
---|
1097 | */
|
---|
1098 | void Medium::uninit()
|
---|
1099 | {
|
---|
1100 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
1101 | AutoUninitSpan autoUninitSpan(this);
|
---|
1102 | if (autoUninitSpan.uninitDone())
|
---|
1103 | return;
|
---|
1104 |
|
---|
1105 | if (!m->formatObj.isNull())
|
---|
1106 | {
|
---|
1107 | /* remove the caller reference we added in setFormat() */
|
---|
1108 | m->formatObj->releaseCaller();
|
---|
1109 | m->formatObj.setNull();
|
---|
1110 | }
|
---|
1111 |
|
---|
1112 | if (m->state == MediumState_Deleting)
|
---|
1113 | {
|
---|
1114 | /* we are being uninitialized after've been deleted by merge.
|
---|
1115 | * Reparenting has already been done so don't touch it here (we are
|
---|
1116 | * now orphans and removeDependentChild() will assert) */
|
---|
1117 | Assert(m->pParent.isNull());
|
---|
1118 | }
|
---|
1119 | else
|
---|
1120 | {
|
---|
1121 | MediaList::iterator it;
|
---|
1122 | for (it = m->llChildren.begin();
|
---|
1123 | it != m->llChildren.end();
|
---|
1124 | ++it)
|
---|
1125 | {
|
---|
1126 | Medium *pChild = *it;
|
---|
1127 | pChild->m->pParent.setNull();
|
---|
1128 | pChild->uninit();
|
---|
1129 | }
|
---|
1130 | m->llChildren.clear(); // this unsets all the ComPtrs and probably calls delete
|
---|
1131 |
|
---|
1132 | if (m->pParent)
|
---|
1133 | {
|
---|
1134 | // this is a differencing disk: then remove it from the parent's children list
|
---|
1135 | deparent();
|
---|
1136 | }
|
---|
1137 | }
|
---|
1138 |
|
---|
1139 | RTSemEventMultiSignal(m->queryInfoSem);
|
---|
1140 | RTSemEventMultiDestroy(m->queryInfoSem);
|
---|
1141 | m->queryInfoSem = NIL_RTSEMEVENTMULTI;
|
---|
1142 |
|
---|
1143 | unconst(m->pVirtualBox) = NULL;
|
---|
1144 | }
|
---|
1145 |
|
---|
1146 | /**
|
---|
1147 | * Internal helper that removes "this" from the list of children of its
|
---|
1148 | * parent. Used in uninit() and other places when reparenting is necessary.
|
---|
1149 | *
|
---|
1150 | * The caller must hold the medium tree lock!
|
---|
1151 | */
|
---|
1152 | void Medium::deparent()
|
---|
1153 | {
|
---|
1154 | MediaList &llParent = m->pParent->m->llChildren;
|
---|
1155 | for (MediaList::iterator it = llParent.begin();
|
---|
1156 | it != llParent.end();
|
---|
1157 | ++it)
|
---|
1158 | {
|
---|
1159 | Medium *pParentsChild = *it;
|
---|
1160 | if (this == pParentsChild)
|
---|
1161 | {
|
---|
1162 | llParent.erase(it);
|
---|
1163 | break;
|
---|
1164 | }
|
---|
1165 | }
|
---|
1166 | m->pParent.setNull();
|
---|
1167 | }
|
---|
1168 |
|
---|
1169 | /**
|
---|
1170 | * Internal helper that removes "this" from the list of children of its
|
---|
1171 | * parent. Used in uninit() and other places when reparenting is necessary.
|
---|
1172 | *
|
---|
1173 | * The caller must hold the medium tree lock!
|
---|
1174 | */
|
---|
1175 | void Medium::setParent(const ComObjPtr<Medium> &pParent)
|
---|
1176 | {
|
---|
1177 | m->pParent = pParent;
|
---|
1178 | if (pParent)
|
---|
1179 | pParent->m->llChildren.push_back(this);
|
---|
1180 | }
|
---|
1181 |
|
---|
1182 |
|
---|
1183 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1184 | //
|
---|
1185 | // IMedium public methods
|
---|
1186 | //
|
---|
1187 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1188 |
|
---|
1189 | STDMETHODIMP Medium::COMGETTER(Id)(BSTR *aId)
|
---|
1190 | {
|
---|
1191 | CheckComArgOutPointerValid(aId);
|
---|
1192 |
|
---|
1193 | AutoCaller autoCaller(this);
|
---|
1194 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1195 |
|
---|
1196 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1197 |
|
---|
1198 | m->id.toUtf16().cloneTo(aId);
|
---|
1199 |
|
---|
1200 | return S_OK;
|
---|
1201 | }
|
---|
1202 |
|
---|
1203 | STDMETHODIMP Medium::COMGETTER(Description)(BSTR *aDescription)
|
---|
1204 | {
|
---|
1205 | CheckComArgOutPointerValid(aDescription);
|
---|
1206 |
|
---|
1207 | AutoCaller autoCaller(this);
|
---|
1208 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1209 |
|
---|
1210 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1211 |
|
---|
1212 | m->strDescription.cloneTo(aDescription);
|
---|
1213 |
|
---|
1214 | return S_OK;
|
---|
1215 | }
|
---|
1216 |
|
---|
1217 | STDMETHODIMP Medium::COMSETTER(Description)(IN_BSTR aDescription)
|
---|
1218 | {
|
---|
1219 | AutoCaller autoCaller(this);
|
---|
1220 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1221 |
|
---|
1222 | // AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1223 |
|
---|
1224 | /// @todo update m->description and save the global registry (and local
|
---|
1225 | /// registries of portable VMs referring to this medium), this will also
|
---|
1226 | /// require to add the mRegistered flag to data
|
---|
1227 |
|
---|
1228 | NOREF(aDescription);
|
---|
1229 |
|
---|
1230 | ReturnComNotImplemented();
|
---|
1231 | }
|
---|
1232 |
|
---|
1233 | STDMETHODIMP Medium::COMGETTER(State)(MediumState_T *aState)
|
---|
1234 | {
|
---|
1235 | CheckComArgOutPointerValid(aState);
|
---|
1236 |
|
---|
1237 | AutoCaller autoCaller(this);
|
---|
1238 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1239 |
|
---|
1240 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1241 | *aState = m->state;
|
---|
1242 |
|
---|
1243 | return S_OK;
|
---|
1244 | }
|
---|
1245 |
|
---|
1246 | STDMETHODIMP Medium::COMGETTER(Variant)(MediumVariant_T *aVariant)
|
---|
1247 | {
|
---|
1248 | CheckComArgOutPointerValid(aVariant);
|
---|
1249 |
|
---|
1250 | AutoCaller autoCaller(this);
|
---|
1251 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1252 |
|
---|
1253 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1254 | *aVariant = m->variant;
|
---|
1255 |
|
---|
1256 | return S_OK;
|
---|
1257 | }
|
---|
1258 |
|
---|
1259 |
|
---|
1260 | STDMETHODIMP Medium::COMGETTER(Location)(BSTR *aLocation)
|
---|
1261 | {
|
---|
1262 | CheckComArgOutPointerValid(aLocation);
|
---|
1263 |
|
---|
1264 | AutoCaller autoCaller(this);
|
---|
1265 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1266 |
|
---|
1267 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1268 |
|
---|
1269 | m->strLocationFull.cloneTo(aLocation);
|
---|
1270 |
|
---|
1271 | return S_OK;
|
---|
1272 | }
|
---|
1273 |
|
---|
1274 | STDMETHODIMP Medium::COMSETTER(Location)(IN_BSTR aLocation)
|
---|
1275 | {
|
---|
1276 | CheckComArgStrNotEmptyOrNull(aLocation);
|
---|
1277 |
|
---|
1278 | AutoCaller autoCaller(this);
|
---|
1279 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1280 |
|
---|
1281 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1282 |
|
---|
1283 | /// @todo NEWMEDIA for file names, add the default extension if no extension
|
---|
1284 | /// is present (using the information from the VD backend which also implies
|
---|
1285 | /// that one more parameter should be passed to setLocation() requesting
|
---|
1286 | /// that functionality since it is only allwed when called from this method
|
---|
1287 |
|
---|
1288 | /// @todo NEWMEDIA rename the file and set m->location on success, then save
|
---|
1289 | /// the global registry (and local registries of portable VMs referring to
|
---|
1290 | /// this medium), this will also require to add the mRegistered flag to data
|
---|
1291 |
|
---|
1292 | ReturnComNotImplemented();
|
---|
1293 | }
|
---|
1294 |
|
---|
1295 | STDMETHODIMP Medium::COMGETTER(Name)(BSTR *aName)
|
---|
1296 | {
|
---|
1297 | CheckComArgOutPointerValid(aName);
|
---|
1298 |
|
---|
1299 | AutoCaller autoCaller(this);
|
---|
1300 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1301 |
|
---|
1302 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1303 |
|
---|
1304 | getName().cloneTo(aName);
|
---|
1305 |
|
---|
1306 | return S_OK;
|
---|
1307 | }
|
---|
1308 |
|
---|
1309 | STDMETHODIMP Medium::COMGETTER(DeviceType)(DeviceType_T *aDeviceType)
|
---|
1310 | {
|
---|
1311 | CheckComArgOutPointerValid(aDeviceType);
|
---|
1312 |
|
---|
1313 | AutoCaller autoCaller(this);
|
---|
1314 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1315 |
|
---|
1316 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1317 |
|
---|
1318 | *aDeviceType = m->devType;
|
---|
1319 |
|
---|
1320 | return S_OK;
|
---|
1321 | }
|
---|
1322 |
|
---|
1323 | STDMETHODIMP Medium::COMGETTER(HostDrive)(BOOL *aHostDrive)
|
---|
1324 | {
|
---|
1325 | CheckComArgOutPointerValid(aHostDrive);
|
---|
1326 |
|
---|
1327 | AutoCaller autoCaller(this);
|
---|
1328 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1329 |
|
---|
1330 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1331 |
|
---|
1332 | *aHostDrive = m->hostDrive;
|
---|
1333 |
|
---|
1334 | return S_OK;
|
---|
1335 | }
|
---|
1336 |
|
---|
1337 | STDMETHODIMP Medium::COMGETTER(Size)(ULONG64 *aSize)
|
---|
1338 | {
|
---|
1339 | CheckComArgOutPointerValid(aSize);
|
---|
1340 |
|
---|
1341 | AutoCaller autoCaller(this);
|
---|
1342 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1343 |
|
---|
1344 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1345 |
|
---|
1346 | *aSize = m->size;
|
---|
1347 |
|
---|
1348 | return S_OK;
|
---|
1349 | }
|
---|
1350 |
|
---|
1351 | STDMETHODIMP Medium::COMGETTER(Format)(BSTR *aFormat)
|
---|
1352 | {
|
---|
1353 | CheckComArgOutPointerValid(aFormat);
|
---|
1354 |
|
---|
1355 | AutoCaller autoCaller(this);
|
---|
1356 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1357 |
|
---|
1358 | /* no need to lock, m->strFormat is const */
|
---|
1359 | m->strFormat.cloneTo(aFormat);
|
---|
1360 |
|
---|
1361 | return S_OK;
|
---|
1362 | }
|
---|
1363 |
|
---|
1364 | STDMETHODIMP Medium::COMGETTER(MediumFormat)(IMediumFormat **aMediumFormat)
|
---|
1365 | {
|
---|
1366 | CheckComArgOutPointerValid(aMediumFormat);
|
---|
1367 |
|
---|
1368 | AutoCaller autoCaller(this);
|
---|
1369 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1370 |
|
---|
1371 | /* no need to lock, m->formatObj is const */
|
---|
1372 | m->formatObj.queryInterfaceTo(aMediumFormat);
|
---|
1373 |
|
---|
1374 | return S_OK;
|
---|
1375 | }
|
---|
1376 |
|
---|
1377 | STDMETHODIMP Medium::COMGETTER(Type)(MediumType_T *aType)
|
---|
1378 | {
|
---|
1379 | CheckComArgOutPointerValid(aType);
|
---|
1380 |
|
---|
1381 | AutoCaller autoCaller(this);
|
---|
1382 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1383 |
|
---|
1384 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1385 |
|
---|
1386 | *aType = m->type;
|
---|
1387 |
|
---|
1388 | return S_OK;
|
---|
1389 | }
|
---|
1390 |
|
---|
1391 | STDMETHODIMP Medium::COMSETTER(Type)(MediumType_T aType)
|
---|
1392 | {
|
---|
1393 | AutoCaller autoCaller(this);
|
---|
1394 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1395 |
|
---|
1396 | // we access mParent and members
|
---|
1397 | AutoMultiWriteLock2 mlock(&m->pVirtualBox->getMediaTreeLockHandle(), this->lockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1398 |
|
---|
1399 | switch (m->state)
|
---|
1400 | {
|
---|
1401 | case MediumState_Created:
|
---|
1402 | case MediumState_Inaccessible:
|
---|
1403 | break;
|
---|
1404 | default:
|
---|
1405 | return setStateError();
|
---|
1406 | }
|
---|
1407 |
|
---|
1408 | if (m->type == aType)
|
---|
1409 | {
|
---|
1410 | /* Nothing to do */
|
---|
1411 | return S_OK;
|
---|
1412 | }
|
---|
1413 |
|
---|
1414 | /* cannot change the type of a differencing medium */
|
---|
1415 | if (m->pParent)
|
---|
1416 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1417 | tr("Cannot change the type of medium '%s' because it is a differencing medium"),
|
---|
1418 | m->strLocationFull.c_str());
|
---|
1419 |
|
---|
1420 | /* cannot change the type of a medium being in use by more than one VM */
|
---|
1421 | if (m->backRefs.size() > 1)
|
---|
1422 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1423 | tr("Cannot change the type of medium '%s' because it is attached to %d virtual machines"),
|
---|
1424 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
1425 |
|
---|
1426 | switch (aType)
|
---|
1427 | {
|
---|
1428 | case MediumType_Normal:
|
---|
1429 | case MediumType_Immutable:
|
---|
1430 | {
|
---|
1431 | /* normal can be easily converted to immutable and vice versa even
|
---|
1432 | * if they have children as long as they are not attached to any
|
---|
1433 | * machine themselves */
|
---|
1434 | break;
|
---|
1435 | }
|
---|
1436 | case MediumType_Writethrough:
|
---|
1437 | case MediumType_Shareable:
|
---|
1438 | {
|
---|
1439 | /* cannot change to writethrough or shareable if there are children */
|
---|
1440 | if (getChildren().size() != 0)
|
---|
1441 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
1442 | tr("Cannot change type for medium '%s' since it has %d child media"),
|
---|
1443 | m->strLocationFull.c_str(), getChildren().size());
|
---|
1444 | if (aType == MediumType_Shareable)
|
---|
1445 | {
|
---|
1446 | MediumVariant_T variant = getVariant();
|
---|
1447 | if (!(variant & MediumVariant_Fixed))
|
---|
1448 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1449 | tr("Cannot change type for medium '%s' to 'Shareable' since it is a dynamic medium storage unit"),
|
---|
1450 | m->strLocationFull.c_str());
|
---|
1451 | }
|
---|
1452 | break;
|
---|
1453 | }
|
---|
1454 | default:
|
---|
1455 | AssertFailedReturn(E_FAIL);
|
---|
1456 | }
|
---|
1457 |
|
---|
1458 | m->type = aType;
|
---|
1459 |
|
---|
1460 | // save the global settings; for that we should hold only the VirtualBox lock
|
---|
1461 | mlock.release();
|
---|
1462 | AutoWriteLock alock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
1463 | HRESULT rc = m->pVirtualBox->saveSettings();
|
---|
1464 |
|
---|
1465 | return rc;
|
---|
1466 | }
|
---|
1467 |
|
---|
1468 | STDMETHODIMP Medium::COMGETTER(Parent)(IMedium **aParent)
|
---|
1469 | {
|
---|
1470 | CheckComArgOutPointerValid(aParent);
|
---|
1471 |
|
---|
1472 | AutoCaller autoCaller(this);
|
---|
1473 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1474 |
|
---|
1475 | /* we access mParent */
|
---|
1476 | AutoReadLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1477 |
|
---|
1478 | m->pParent.queryInterfaceTo(aParent);
|
---|
1479 |
|
---|
1480 | return S_OK;
|
---|
1481 | }
|
---|
1482 |
|
---|
1483 | STDMETHODIMP Medium::COMGETTER(Children)(ComSafeArrayOut(IMedium *, aChildren))
|
---|
1484 | {
|
---|
1485 | CheckComArgOutSafeArrayPointerValid(aChildren);
|
---|
1486 |
|
---|
1487 | AutoCaller autoCaller(this);
|
---|
1488 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1489 |
|
---|
1490 | /* we access children */
|
---|
1491 | AutoReadLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1492 |
|
---|
1493 | SafeIfaceArray<IMedium> children(this->getChildren());
|
---|
1494 | children.detachTo(ComSafeArrayOutArg(aChildren));
|
---|
1495 |
|
---|
1496 | return S_OK;
|
---|
1497 | }
|
---|
1498 |
|
---|
1499 | STDMETHODIMP Medium::COMGETTER(Base)(IMedium **aBase)
|
---|
1500 | {
|
---|
1501 | CheckComArgOutPointerValid(aBase);
|
---|
1502 |
|
---|
1503 | /* base() will do callers/locking */
|
---|
1504 |
|
---|
1505 | getBase().queryInterfaceTo(aBase);
|
---|
1506 |
|
---|
1507 | return S_OK;
|
---|
1508 | }
|
---|
1509 |
|
---|
1510 | STDMETHODIMP Medium::COMGETTER(ReadOnly)(BOOL *aReadOnly)
|
---|
1511 | {
|
---|
1512 | CheckComArgOutPointerValid(aReadOnly);
|
---|
1513 |
|
---|
1514 | AutoCaller autoCaller(this);
|
---|
1515 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1516 |
|
---|
1517 | /* isRadOnly() will do locking */
|
---|
1518 |
|
---|
1519 | *aReadOnly = isReadOnly();
|
---|
1520 |
|
---|
1521 | return S_OK;
|
---|
1522 | }
|
---|
1523 |
|
---|
1524 | STDMETHODIMP Medium::COMGETTER(LogicalSize)(ULONG64 *aLogicalSize)
|
---|
1525 | {
|
---|
1526 | CheckComArgOutPointerValid(aLogicalSize);
|
---|
1527 |
|
---|
1528 | {
|
---|
1529 | AutoCaller autoCaller(this);
|
---|
1530 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1531 |
|
---|
1532 | /* we access mParent */
|
---|
1533 | AutoReadLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1534 |
|
---|
1535 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1536 |
|
---|
1537 | if (m->pParent.isNull())
|
---|
1538 | {
|
---|
1539 | *aLogicalSize = m->logicalSize;
|
---|
1540 |
|
---|
1541 | return S_OK;
|
---|
1542 | }
|
---|
1543 | }
|
---|
1544 |
|
---|
1545 | /* We assume that some backend may decide to return a meaningless value in
|
---|
1546 | * response to VDGetSize() for differencing media and therefore always
|
---|
1547 | * ask the base medium ourselves. */
|
---|
1548 |
|
---|
1549 | /* base() will do callers/locking */
|
---|
1550 |
|
---|
1551 | return getBase()->COMGETTER(LogicalSize)(aLogicalSize);
|
---|
1552 | }
|
---|
1553 |
|
---|
1554 | STDMETHODIMP Medium::COMGETTER(AutoReset)(BOOL *aAutoReset)
|
---|
1555 | {
|
---|
1556 | CheckComArgOutPointerValid(aAutoReset);
|
---|
1557 |
|
---|
1558 | AutoCaller autoCaller(this);
|
---|
1559 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1560 |
|
---|
1561 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1562 |
|
---|
1563 | if (m->pParent.isNull())
|
---|
1564 | *aAutoReset = FALSE;
|
---|
1565 | else
|
---|
1566 | *aAutoReset = m->autoReset;
|
---|
1567 |
|
---|
1568 | return S_OK;
|
---|
1569 | }
|
---|
1570 |
|
---|
1571 | STDMETHODIMP Medium::COMSETTER(AutoReset)(BOOL aAutoReset)
|
---|
1572 | {
|
---|
1573 | AutoCaller autoCaller(this);
|
---|
1574 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1575 |
|
---|
1576 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1577 |
|
---|
1578 | if (m->pParent.isNull())
|
---|
1579 | return setError(VBOX_E_NOT_SUPPORTED,
|
---|
1580 | tr("Medium '%s' is not differencing"),
|
---|
1581 | m->strLocationFull.c_str());
|
---|
1582 |
|
---|
1583 | if (m->autoReset != !!aAutoReset)
|
---|
1584 | {
|
---|
1585 | m->autoReset = !!aAutoReset;
|
---|
1586 |
|
---|
1587 | // save the global settings; for that we should hold only the VirtualBox lock
|
---|
1588 | mlock.release();
|
---|
1589 | AutoWriteLock alock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
1590 | return m->pVirtualBox->saveSettings();
|
---|
1591 | }
|
---|
1592 |
|
---|
1593 | return S_OK;
|
---|
1594 | }
|
---|
1595 | STDMETHODIMP Medium::COMGETTER(LastAccessError)(BSTR *aLastAccessError)
|
---|
1596 | {
|
---|
1597 | CheckComArgOutPointerValid(aLastAccessError);
|
---|
1598 |
|
---|
1599 | AutoCaller autoCaller(this);
|
---|
1600 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1601 |
|
---|
1602 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1603 |
|
---|
1604 | m->strLastAccessError.cloneTo(aLastAccessError);
|
---|
1605 |
|
---|
1606 | return S_OK;
|
---|
1607 | }
|
---|
1608 |
|
---|
1609 | STDMETHODIMP Medium::COMGETTER(MachineIds)(ComSafeArrayOut(BSTR,aMachineIds))
|
---|
1610 | {
|
---|
1611 | CheckComArgOutSafeArrayPointerValid(aMachineIds);
|
---|
1612 |
|
---|
1613 | AutoCaller autoCaller(this);
|
---|
1614 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1615 |
|
---|
1616 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1617 |
|
---|
1618 | com::SafeArray<BSTR> machineIds;
|
---|
1619 |
|
---|
1620 | if (m->backRefs.size() != 0)
|
---|
1621 | {
|
---|
1622 | machineIds.reset(m->backRefs.size());
|
---|
1623 |
|
---|
1624 | size_t i = 0;
|
---|
1625 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
1626 | it != m->backRefs.end(); ++it, ++i)
|
---|
1627 | {
|
---|
1628 | it->machineId.toUtf16().detachTo(&machineIds[i]);
|
---|
1629 | }
|
---|
1630 | }
|
---|
1631 |
|
---|
1632 | machineIds.detachTo(ComSafeArrayOutArg(aMachineIds));
|
---|
1633 |
|
---|
1634 | return S_OK;
|
---|
1635 | }
|
---|
1636 |
|
---|
1637 | STDMETHODIMP Medium::SetIDs(BOOL aSetImageId,
|
---|
1638 | IN_BSTR aImageId,
|
---|
1639 | BOOL aSetParentId,
|
---|
1640 | IN_BSTR aParentId)
|
---|
1641 | {
|
---|
1642 | AutoCaller autoCaller(this);
|
---|
1643 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1644 |
|
---|
1645 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1646 |
|
---|
1647 | Guid imageId, parentId;
|
---|
1648 | if (aSetImageId)
|
---|
1649 | {
|
---|
1650 | imageId = Guid(aImageId);
|
---|
1651 | if (imageId.isEmpty())
|
---|
1652 | return setError(E_INVALIDARG, tr("Argument %s is empty"), "aImageId");
|
---|
1653 | }
|
---|
1654 | if (aSetParentId)
|
---|
1655 | parentId = Guid(aParentId);
|
---|
1656 |
|
---|
1657 | unconst(m->uuidImage) = imageId;
|
---|
1658 | unconst(m->uuidParentImage) = parentId;
|
---|
1659 |
|
---|
1660 | HRESULT rc = queryInfo(!!aSetImageId /* fSetImageId */,
|
---|
1661 | !!aSetParentId /* fSetParentId */);
|
---|
1662 |
|
---|
1663 | return rc;
|
---|
1664 | }
|
---|
1665 |
|
---|
1666 | STDMETHODIMP Medium::RefreshState(MediumState_T *aState)
|
---|
1667 | {
|
---|
1668 | CheckComArgOutPointerValid(aState);
|
---|
1669 |
|
---|
1670 | AutoCaller autoCaller(this);
|
---|
1671 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1672 |
|
---|
1673 | /* queryInfo() locks this for writing. */
|
---|
1674 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1675 |
|
---|
1676 | HRESULT rc = S_OK;
|
---|
1677 |
|
---|
1678 | switch (m->state)
|
---|
1679 | {
|
---|
1680 | case MediumState_Created:
|
---|
1681 | case MediumState_Inaccessible:
|
---|
1682 | case MediumState_LockedRead:
|
---|
1683 | {
|
---|
1684 | rc = queryInfo(false /* fSetImageId */, false /* fSetParentId */);
|
---|
1685 | break;
|
---|
1686 | }
|
---|
1687 | default:
|
---|
1688 | break;
|
---|
1689 | }
|
---|
1690 |
|
---|
1691 | *aState = m->state;
|
---|
1692 |
|
---|
1693 | return rc;
|
---|
1694 | }
|
---|
1695 |
|
---|
1696 | STDMETHODIMP Medium::GetSnapshotIds(IN_BSTR aMachineId,
|
---|
1697 | ComSafeArrayOut(BSTR, aSnapshotIds))
|
---|
1698 | {
|
---|
1699 | CheckComArgExpr(aMachineId, Guid(aMachineId).isEmpty() == false);
|
---|
1700 | CheckComArgOutSafeArrayPointerValid(aSnapshotIds);
|
---|
1701 |
|
---|
1702 | AutoCaller autoCaller(this);
|
---|
1703 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1704 |
|
---|
1705 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1706 |
|
---|
1707 | com::SafeArray<BSTR> snapshotIds;
|
---|
1708 |
|
---|
1709 | Guid id(aMachineId);
|
---|
1710 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
1711 | it != m->backRefs.end(); ++it)
|
---|
1712 | {
|
---|
1713 | if (it->machineId == id)
|
---|
1714 | {
|
---|
1715 | size_t size = it->llSnapshotIds.size();
|
---|
1716 |
|
---|
1717 | /* if the medium is attached to the machine in the current state, we
|
---|
1718 | * return its ID as the first element of the array */
|
---|
1719 | if (it->fInCurState)
|
---|
1720 | ++size;
|
---|
1721 |
|
---|
1722 | if (size > 0)
|
---|
1723 | {
|
---|
1724 | snapshotIds.reset(size);
|
---|
1725 |
|
---|
1726 | size_t j = 0;
|
---|
1727 | if (it->fInCurState)
|
---|
1728 | it->machineId.toUtf16().detachTo(&snapshotIds[j++]);
|
---|
1729 |
|
---|
1730 | for (BackRef::GuidList::const_iterator jt = it->llSnapshotIds.begin();
|
---|
1731 | jt != it->llSnapshotIds.end();
|
---|
1732 | ++jt, ++j)
|
---|
1733 | {
|
---|
1734 | (*jt).toUtf16().detachTo(&snapshotIds[j]);
|
---|
1735 | }
|
---|
1736 | }
|
---|
1737 |
|
---|
1738 | break;
|
---|
1739 | }
|
---|
1740 | }
|
---|
1741 |
|
---|
1742 | snapshotIds.detachTo(ComSafeArrayOutArg(aSnapshotIds));
|
---|
1743 |
|
---|
1744 | return S_OK;
|
---|
1745 | }
|
---|
1746 |
|
---|
1747 | /**
|
---|
1748 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
1749 | * in-process calls).
|
---|
1750 | */
|
---|
1751 | STDMETHODIMP Medium::LockRead(MediumState_T *aState)
|
---|
1752 | {
|
---|
1753 | AutoCaller autoCaller(this);
|
---|
1754 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1755 |
|
---|
1756 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1757 |
|
---|
1758 | /* Wait for a concurrently running queryInfo() to complete */
|
---|
1759 | while (m->queryInfoRunning)
|
---|
1760 | {
|
---|
1761 | alock.leave();
|
---|
1762 | RTSemEventMultiWait(m->queryInfoSem, RT_INDEFINITE_WAIT);
|
---|
1763 | alock.enter();
|
---|
1764 | }
|
---|
1765 |
|
---|
1766 | /* return the current state before */
|
---|
1767 | if (aState)
|
---|
1768 | *aState = m->state;
|
---|
1769 |
|
---|
1770 | HRESULT rc = S_OK;
|
---|
1771 |
|
---|
1772 | switch (m->state)
|
---|
1773 | {
|
---|
1774 | case MediumState_Created:
|
---|
1775 | case MediumState_Inaccessible:
|
---|
1776 | case MediumState_LockedRead:
|
---|
1777 | {
|
---|
1778 | ++m->readers;
|
---|
1779 |
|
---|
1780 | ComAssertMsgBreak(m->readers != 0, ("Counter overflow"), rc = E_FAIL);
|
---|
1781 |
|
---|
1782 | /* Remember pre-lock state */
|
---|
1783 | if (m->state != MediumState_LockedRead)
|
---|
1784 | m->preLockState = m->state;
|
---|
1785 |
|
---|
1786 | LogFlowThisFunc(("Okay - prev state=%d readers=%d\n", m->state, m->readers));
|
---|
1787 | m->state = MediumState_LockedRead;
|
---|
1788 |
|
---|
1789 | break;
|
---|
1790 | }
|
---|
1791 | default:
|
---|
1792 | {
|
---|
1793 | LogFlowThisFunc(("Failing - state=%d\n", m->state));
|
---|
1794 | rc = setStateError();
|
---|
1795 | break;
|
---|
1796 | }
|
---|
1797 | }
|
---|
1798 |
|
---|
1799 | return rc;
|
---|
1800 | }
|
---|
1801 |
|
---|
1802 | /**
|
---|
1803 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
1804 | * in-process calls).
|
---|
1805 | */
|
---|
1806 | STDMETHODIMP Medium::UnlockRead(MediumState_T *aState)
|
---|
1807 | {
|
---|
1808 | AutoCaller autoCaller(this);
|
---|
1809 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1810 |
|
---|
1811 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1812 |
|
---|
1813 | HRESULT rc = S_OK;
|
---|
1814 |
|
---|
1815 | switch (m->state)
|
---|
1816 | {
|
---|
1817 | case MediumState_LockedRead:
|
---|
1818 | {
|
---|
1819 | Assert(m->readers != 0);
|
---|
1820 | --m->readers;
|
---|
1821 |
|
---|
1822 | /* Reset the state after the last reader */
|
---|
1823 | if (m->readers == 0)
|
---|
1824 | {
|
---|
1825 | m->state = m->preLockState;
|
---|
1826 | /* There are cases where we inject the deleting state into
|
---|
1827 | * a medium locked for reading. Make sure #unmarkForDeletion()
|
---|
1828 | * gets the right state afterwards. */
|
---|
1829 | if (m->preLockState == MediumState_Deleting)
|
---|
1830 | m->preLockState = MediumState_Created;
|
---|
1831 | }
|
---|
1832 |
|
---|
1833 | LogFlowThisFunc(("new state=%d\n", m->state));
|
---|
1834 | break;
|
---|
1835 | }
|
---|
1836 | default:
|
---|
1837 | {
|
---|
1838 | LogFlowThisFunc(("Failing - state=%d\n", m->state));
|
---|
1839 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1840 | tr("Medium '%s' is not locked for reading"),
|
---|
1841 | m->strLocationFull.c_str());
|
---|
1842 | break;
|
---|
1843 | }
|
---|
1844 | }
|
---|
1845 |
|
---|
1846 | /* return the current state after */
|
---|
1847 | if (aState)
|
---|
1848 | *aState = m->state;
|
---|
1849 |
|
---|
1850 | return rc;
|
---|
1851 | }
|
---|
1852 |
|
---|
1853 | /**
|
---|
1854 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
1855 | * in-process calls).
|
---|
1856 | */
|
---|
1857 | STDMETHODIMP Medium::LockWrite(MediumState_T *aState)
|
---|
1858 | {
|
---|
1859 | AutoCaller autoCaller(this);
|
---|
1860 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1861 |
|
---|
1862 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1863 |
|
---|
1864 | /* Wait for a concurrently running queryInfo() to complete */
|
---|
1865 | while (m->queryInfoRunning)
|
---|
1866 | {
|
---|
1867 | alock.leave();
|
---|
1868 | RTSemEventMultiWait(m->queryInfoSem, RT_INDEFINITE_WAIT);
|
---|
1869 | alock.enter();
|
---|
1870 | }
|
---|
1871 |
|
---|
1872 | /* return the current state before */
|
---|
1873 | if (aState)
|
---|
1874 | *aState = m->state;
|
---|
1875 |
|
---|
1876 | HRESULT rc = S_OK;
|
---|
1877 |
|
---|
1878 | switch (m->state)
|
---|
1879 | {
|
---|
1880 | case MediumState_Created:
|
---|
1881 | case MediumState_Inaccessible:
|
---|
1882 | {
|
---|
1883 | m->preLockState = m->state;
|
---|
1884 |
|
---|
1885 | LogFlowThisFunc(("Okay - prev state=%d locationFull=%s\n", m->state, getLocationFull().c_str()));
|
---|
1886 | m->state = MediumState_LockedWrite;
|
---|
1887 | break;
|
---|
1888 | }
|
---|
1889 | default:
|
---|
1890 | {
|
---|
1891 | LogFlowThisFunc(("Failing - state=%d locationFull=%s\n", m->state, getLocationFull().c_str()));
|
---|
1892 | rc = setStateError();
|
---|
1893 | break;
|
---|
1894 | }
|
---|
1895 | }
|
---|
1896 |
|
---|
1897 | return rc;
|
---|
1898 | }
|
---|
1899 |
|
---|
1900 | /**
|
---|
1901 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
1902 | * in-process calls).
|
---|
1903 | */
|
---|
1904 | STDMETHODIMP Medium::UnlockWrite(MediumState_T *aState)
|
---|
1905 | {
|
---|
1906 | AutoCaller autoCaller(this);
|
---|
1907 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1908 |
|
---|
1909 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1910 |
|
---|
1911 | HRESULT rc = S_OK;
|
---|
1912 |
|
---|
1913 | switch (m->state)
|
---|
1914 | {
|
---|
1915 | case MediumState_LockedWrite:
|
---|
1916 | {
|
---|
1917 | m->state = m->preLockState;
|
---|
1918 | /* There are cases where we inject the deleting state into
|
---|
1919 | * a medium locked for writing. Make sure #unmarkForDeletion()
|
---|
1920 | * gets the right state afterwards. */
|
---|
1921 | if (m->preLockState == MediumState_Deleting)
|
---|
1922 | m->preLockState = MediumState_Created;
|
---|
1923 | LogFlowThisFunc(("new state=%d locationFull=%s\n", m->state, getLocationFull().c_str()));
|
---|
1924 | break;
|
---|
1925 | }
|
---|
1926 | default:
|
---|
1927 | {
|
---|
1928 | LogFlowThisFunc(("Failing - state=%d locationFull=%s\n", m->state, getLocationFull().c_str()));
|
---|
1929 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1930 | tr("Medium '%s' is not locked for writing"),
|
---|
1931 | m->strLocationFull.c_str());
|
---|
1932 | break;
|
---|
1933 | }
|
---|
1934 | }
|
---|
1935 |
|
---|
1936 | /* return the current state after */
|
---|
1937 | if (aState)
|
---|
1938 | *aState = m->state;
|
---|
1939 |
|
---|
1940 | return rc;
|
---|
1941 | }
|
---|
1942 |
|
---|
1943 | STDMETHODIMP Medium::Close()
|
---|
1944 | {
|
---|
1945 | AutoCaller autoCaller(this);
|
---|
1946 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1947 |
|
---|
1948 | // make a copy of VirtualBox pointer which gets nulled by uninit()
|
---|
1949 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
1950 |
|
---|
1951 | bool fNeedsSaveSettings = false;
|
---|
1952 | HRESULT rc = close(&fNeedsSaveSettings, autoCaller);
|
---|
1953 |
|
---|
1954 | if (fNeedsSaveSettings)
|
---|
1955 | {
|
---|
1956 | AutoWriteLock vboxlock(pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
1957 | pVirtualBox->saveSettings();
|
---|
1958 | }
|
---|
1959 |
|
---|
1960 | return rc;
|
---|
1961 | }
|
---|
1962 |
|
---|
1963 | STDMETHODIMP Medium::GetProperty(IN_BSTR aName, BSTR *aValue)
|
---|
1964 | {
|
---|
1965 | CheckComArgStrNotEmptyOrNull(aName);
|
---|
1966 | CheckComArgOutPointerValid(aValue);
|
---|
1967 |
|
---|
1968 | AutoCaller autoCaller(this);
|
---|
1969 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1970 |
|
---|
1971 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1972 |
|
---|
1973 | settings::StringsMap::const_iterator it = m->mapProperties.find(Utf8Str(aName));
|
---|
1974 | if (it == m->mapProperties.end())
|
---|
1975 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
1976 | tr("Property '%ls' does not exist"), aName);
|
---|
1977 |
|
---|
1978 | it->second.cloneTo(aValue);
|
---|
1979 |
|
---|
1980 | return S_OK;
|
---|
1981 | }
|
---|
1982 |
|
---|
1983 | STDMETHODIMP Medium::SetProperty(IN_BSTR aName, IN_BSTR aValue)
|
---|
1984 | {
|
---|
1985 | CheckComArgStrNotEmptyOrNull(aName);
|
---|
1986 |
|
---|
1987 | AutoCaller autoCaller(this);
|
---|
1988 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1989 |
|
---|
1990 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1991 |
|
---|
1992 | switch (m->state)
|
---|
1993 | {
|
---|
1994 | case MediumState_Created:
|
---|
1995 | case MediumState_Inaccessible:
|
---|
1996 | break;
|
---|
1997 | default:
|
---|
1998 | return setStateError();
|
---|
1999 | }
|
---|
2000 |
|
---|
2001 | settings::StringsMap::iterator it = m->mapProperties.find(Utf8Str(aName));
|
---|
2002 | if (it == m->mapProperties.end())
|
---|
2003 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2004 | tr("Property '%ls' does not exist"),
|
---|
2005 | aName);
|
---|
2006 |
|
---|
2007 | it->second = aValue;
|
---|
2008 |
|
---|
2009 | // save the global settings; for that we should hold only the VirtualBox lock
|
---|
2010 | mlock.release();
|
---|
2011 | AutoWriteLock alock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
2012 | HRESULT rc = m->pVirtualBox->saveSettings();
|
---|
2013 |
|
---|
2014 | return rc;
|
---|
2015 | }
|
---|
2016 |
|
---|
2017 | STDMETHODIMP Medium::GetProperties(IN_BSTR aNames,
|
---|
2018 | ComSafeArrayOut(BSTR, aReturnNames),
|
---|
2019 | ComSafeArrayOut(BSTR, aReturnValues))
|
---|
2020 | {
|
---|
2021 | CheckComArgOutSafeArrayPointerValid(aReturnNames);
|
---|
2022 | CheckComArgOutSafeArrayPointerValid(aReturnValues);
|
---|
2023 |
|
---|
2024 | AutoCaller autoCaller(this);
|
---|
2025 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2026 |
|
---|
2027 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2028 |
|
---|
2029 | /// @todo make use of aNames according to the documentation
|
---|
2030 | NOREF(aNames);
|
---|
2031 |
|
---|
2032 | com::SafeArray<BSTR> names(m->mapProperties.size());
|
---|
2033 | com::SafeArray<BSTR> values(m->mapProperties.size());
|
---|
2034 | size_t i = 0;
|
---|
2035 |
|
---|
2036 | for (settings::StringsMap::const_iterator it = m->mapProperties.begin();
|
---|
2037 | it != m->mapProperties.end();
|
---|
2038 | ++it)
|
---|
2039 | {
|
---|
2040 | it->first.cloneTo(&names[i]);
|
---|
2041 | it->second.cloneTo(&values[i]);
|
---|
2042 | ++i;
|
---|
2043 | }
|
---|
2044 |
|
---|
2045 | names.detachTo(ComSafeArrayOutArg(aReturnNames));
|
---|
2046 | values.detachTo(ComSafeArrayOutArg(aReturnValues));
|
---|
2047 |
|
---|
2048 | return S_OK;
|
---|
2049 | }
|
---|
2050 |
|
---|
2051 | STDMETHODIMP Medium::SetProperties(ComSafeArrayIn(IN_BSTR, aNames),
|
---|
2052 | ComSafeArrayIn(IN_BSTR, aValues))
|
---|
2053 | {
|
---|
2054 | CheckComArgSafeArrayNotNull(aNames);
|
---|
2055 | CheckComArgSafeArrayNotNull(aValues);
|
---|
2056 |
|
---|
2057 | AutoCaller autoCaller(this);
|
---|
2058 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2059 |
|
---|
2060 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2061 |
|
---|
2062 | com::SafeArray<IN_BSTR> names(ComSafeArrayInArg(aNames));
|
---|
2063 | com::SafeArray<IN_BSTR> values(ComSafeArrayInArg(aValues));
|
---|
2064 |
|
---|
2065 | /* first pass: validate names */
|
---|
2066 | for (size_t i = 0;
|
---|
2067 | i < names.size();
|
---|
2068 | ++i)
|
---|
2069 | {
|
---|
2070 | if (m->mapProperties.find(Utf8Str(names[i])) == m->mapProperties.end())
|
---|
2071 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2072 | tr("Property '%ls' does not exist"), names[i]);
|
---|
2073 | }
|
---|
2074 |
|
---|
2075 | /* second pass: assign */
|
---|
2076 | for (size_t i = 0;
|
---|
2077 | i < names.size();
|
---|
2078 | ++i)
|
---|
2079 | {
|
---|
2080 | settings::StringsMap::iterator it = m->mapProperties.find(Utf8Str(names[i]));
|
---|
2081 | AssertReturn(it != m->mapProperties.end(), E_FAIL);
|
---|
2082 |
|
---|
2083 | it->second = Utf8Str(values[i]);
|
---|
2084 | }
|
---|
2085 |
|
---|
2086 | mlock.release();
|
---|
2087 |
|
---|
2088 | // saveSettings needs vbox lock
|
---|
2089 | AutoWriteLock alock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
2090 | HRESULT rc = m->pVirtualBox->saveSettings();
|
---|
2091 |
|
---|
2092 | return rc;
|
---|
2093 | }
|
---|
2094 |
|
---|
2095 | STDMETHODIMP Medium::CreateBaseStorage(ULONG64 aLogicalSize,
|
---|
2096 | MediumVariant_T aVariant,
|
---|
2097 | IProgress **aProgress)
|
---|
2098 | {
|
---|
2099 | CheckComArgOutPointerValid(aProgress);
|
---|
2100 |
|
---|
2101 | AutoCaller autoCaller(this);
|
---|
2102 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2103 |
|
---|
2104 | HRESULT rc = S_OK;
|
---|
2105 | ComObjPtr <Progress> pProgress;
|
---|
2106 | Medium::Task *pTask = NULL;
|
---|
2107 |
|
---|
2108 | try
|
---|
2109 | {
|
---|
2110 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2111 |
|
---|
2112 | aVariant = (MediumVariant_T)((unsigned)aVariant & (unsigned)~MediumVariant_Diff);
|
---|
2113 | if ( !(aVariant & MediumVariant_Fixed)
|
---|
2114 | && !(m->formatObj->getCapabilities() & MediumFormatCapabilities_CreateDynamic))
|
---|
2115 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2116 | tr("Medium format '%s' does not support dynamic storage creation"),
|
---|
2117 | m->strFormat.c_str());
|
---|
2118 | if ( (aVariant & MediumVariant_Fixed)
|
---|
2119 | && !(m->formatObj->getCapabilities() & MediumFormatCapabilities_CreateDynamic))
|
---|
2120 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2121 | tr("Medium format '%s' does not support fixed storage creation"),
|
---|
2122 | m->strFormat.c_str());
|
---|
2123 |
|
---|
2124 | if (m->state != MediumState_NotCreated)
|
---|
2125 | throw setStateError();
|
---|
2126 |
|
---|
2127 | pProgress.createObject();
|
---|
2128 | rc = pProgress->init(m->pVirtualBox,
|
---|
2129 | static_cast<IMedium*>(this),
|
---|
2130 | (aVariant & MediumVariant_Fixed)
|
---|
2131 | ? BstrFmt(tr("Creating fixed medium storage unit '%s'"), m->strLocationFull.c_str())
|
---|
2132 | : BstrFmt(tr("Creating dynamic medium storage unit '%s'"), m->strLocationFull.c_str()),
|
---|
2133 | TRUE /* aCancelable */);
|
---|
2134 | if (FAILED(rc))
|
---|
2135 | throw rc;
|
---|
2136 |
|
---|
2137 | /* setup task object to carry out the operation asynchronously */
|
---|
2138 | pTask = new Medium::CreateBaseTask(this, pProgress, aLogicalSize,
|
---|
2139 | aVariant);
|
---|
2140 | rc = pTask->rc();
|
---|
2141 | AssertComRC(rc);
|
---|
2142 | if (FAILED(rc))
|
---|
2143 | throw rc;
|
---|
2144 |
|
---|
2145 | m->state = MediumState_Creating;
|
---|
2146 | }
|
---|
2147 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2148 |
|
---|
2149 | if (SUCCEEDED(rc))
|
---|
2150 | {
|
---|
2151 | rc = startThread(pTask);
|
---|
2152 |
|
---|
2153 | if (SUCCEEDED(rc))
|
---|
2154 | pProgress.queryInterfaceTo(aProgress);
|
---|
2155 | }
|
---|
2156 | else if (pTask != NULL)
|
---|
2157 | delete pTask;
|
---|
2158 |
|
---|
2159 | return rc;
|
---|
2160 | }
|
---|
2161 |
|
---|
2162 | STDMETHODIMP Medium::DeleteStorage(IProgress **aProgress)
|
---|
2163 | {
|
---|
2164 | CheckComArgOutPointerValid(aProgress);
|
---|
2165 |
|
---|
2166 | AutoCaller autoCaller(this);
|
---|
2167 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2168 |
|
---|
2169 | bool fNeedsSaveSettings = false;
|
---|
2170 | ComObjPtr<Progress> pProgress;
|
---|
2171 |
|
---|
2172 | HRESULT rc = deleteStorage(&pProgress,
|
---|
2173 | false /* aWait */,
|
---|
2174 | &fNeedsSaveSettings);
|
---|
2175 | if (fNeedsSaveSettings)
|
---|
2176 | {
|
---|
2177 | AutoWriteLock vboxlock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
2178 | m->pVirtualBox->saveSettings();
|
---|
2179 | }
|
---|
2180 |
|
---|
2181 | if (SUCCEEDED(rc))
|
---|
2182 | pProgress.queryInterfaceTo(aProgress);
|
---|
2183 |
|
---|
2184 | return rc;
|
---|
2185 | }
|
---|
2186 |
|
---|
2187 | STDMETHODIMP Medium::CreateDiffStorage(IMedium *aTarget,
|
---|
2188 | MediumVariant_T aVariant,
|
---|
2189 | IProgress **aProgress)
|
---|
2190 | {
|
---|
2191 | CheckComArgNotNull(aTarget);
|
---|
2192 | CheckComArgOutPointerValid(aProgress);
|
---|
2193 |
|
---|
2194 | AutoCaller autoCaller(this);
|
---|
2195 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2196 |
|
---|
2197 | ComObjPtr<Medium> diff = static_cast<Medium*>(aTarget);
|
---|
2198 |
|
---|
2199 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2200 |
|
---|
2201 | if (m->type == MediumType_Writethrough)
|
---|
2202 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2203 | tr("Medium type of '%s' is Writethrough"),
|
---|
2204 | m->strLocationFull.c_str());
|
---|
2205 | else if (m->type == MediumType_Shareable)
|
---|
2206 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2207 | tr("Medium type of '%s' is Shareable"),
|
---|
2208 | m->strLocationFull.c_str());
|
---|
2209 |
|
---|
2210 | /* Apply the normal locking logic to the entire chain. */
|
---|
2211 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
2212 | HRESULT rc = diff->createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2213 | true /* fMediumLockWrite */,
|
---|
2214 | this,
|
---|
2215 | *pMediumLockList);
|
---|
2216 | if (FAILED(rc))
|
---|
2217 | {
|
---|
2218 | delete pMediumLockList;
|
---|
2219 | return rc;
|
---|
2220 | }
|
---|
2221 |
|
---|
2222 | ComObjPtr <Progress> pProgress;
|
---|
2223 |
|
---|
2224 | rc = createDiffStorage(diff, aVariant, pMediumLockList, &pProgress,
|
---|
2225 | false /* aWait */, NULL /* pfNeedsSaveSettings*/);
|
---|
2226 | if (FAILED(rc))
|
---|
2227 | delete pMediumLockList;
|
---|
2228 | else
|
---|
2229 | pProgress.queryInterfaceTo(aProgress);
|
---|
2230 |
|
---|
2231 | return rc;
|
---|
2232 | }
|
---|
2233 |
|
---|
2234 | STDMETHODIMP Medium::MergeTo(IMedium *aTarget, IProgress **aProgress)
|
---|
2235 | {
|
---|
2236 | CheckComArgNotNull(aTarget);
|
---|
2237 | CheckComArgOutPointerValid(aProgress);
|
---|
2238 | ComAssertRet(aTarget != this, E_INVALIDARG);
|
---|
2239 |
|
---|
2240 | AutoCaller autoCaller(this);
|
---|
2241 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2242 |
|
---|
2243 | ComObjPtr<Medium> pTarget = static_cast<Medium*>(aTarget);
|
---|
2244 |
|
---|
2245 | bool fMergeForward = false;
|
---|
2246 | ComObjPtr<Medium> pParentForTarget;
|
---|
2247 | MediaList childrenToReparent;
|
---|
2248 | MediumLockList *pMediumLockList = NULL;
|
---|
2249 |
|
---|
2250 | HRESULT rc = S_OK;
|
---|
2251 |
|
---|
2252 | rc = prepareMergeTo(pTarget, NULL, NULL, true, fMergeForward,
|
---|
2253 | pParentForTarget, childrenToReparent, pMediumLockList);
|
---|
2254 | if (FAILED(rc)) return rc;
|
---|
2255 |
|
---|
2256 | ComObjPtr <Progress> pProgress;
|
---|
2257 |
|
---|
2258 | rc = mergeTo(pTarget, fMergeForward, pParentForTarget, childrenToReparent,
|
---|
2259 | pMediumLockList, &pProgress, false /* aWait */,
|
---|
2260 | NULL /* pfNeedsSaveSettings */);
|
---|
2261 | if (FAILED(rc))
|
---|
2262 | cancelMergeTo(childrenToReparent, pMediumLockList);
|
---|
2263 | else
|
---|
2264 | pProgress.queryInterfaceTo(aProgress);
|
---|
2265 |
|
---|
2266 | return rc;
|
---|
2267 | }
|
---|
2268 |
|
---|
2269 | STDMETHODIMP Medium::CloneTo(IMedium *aTarget,
|
---|
2270 | MediumVariant_T aVariant,
|
---|
2271 | IMedium *aParent,
|
---|
2272 | IProgress **aProgress)
|
---|
2273 | {
|
---|
2274 | CheckComArgNotNull(aTarget);
|
---|
2275 | CheckComArgOutPointerValid(aProgress);
|
---|
2276 | ComAssertRet(aTarget != this, E_INVALIDARG);
|
---|
2277 |
|
---|
2278 | AutoCaller autoCaller(this);
|
---|
2279 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2280 |
|
---|
2281 | ComObjPtr<Medium> pTarget = static_cast<Medium*>(aTarget);
|
---|
2282 | ComObjPtr<Medium> pParent;
|
---|
2283 | if (aParent)
|
---|
2284 | pParent = static_cast<Medium*>(aParent);
|
---|
2285 |
|
---|
2286 | HRESULT rc = S_OK;
|
---|
2287 | ComObjPtr<Progress> pProgress;
|
---|
2288 | Medium::Task *pTask = NULL;
|
---|
2289 |
|
---|
2290 | try
|
---|
2291 | {
|
---|
2292 | // locking: we need the tree lock first because we access parent pointers
|
---|
2293 | AutoReadLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
2294 | // and we need to write-lock the media involved
|
---|
2295 | AutoMultiWriteLock3 alock(this, pTarget, pParent COMMA_LOCKVAL_SRC_POS);
|
---|
2296 |
|
---|
2297 | if ( pTarget->m->state != MediumState_NotCreated
|
---|
2298 | && pTarget->m->state != MediumState_Created)
|
---|
2299 | throw pTarget->setStateError();
|
---|
2300 |
|
---|
2301 | /* Build the source lock list. */
|
---|
2302 | MediumLockList *pSourceMediumLockList(new MediumLockList());
|
---|
2303 | rc = createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2304 | false /* fMediumLockWrite */,
|
---|
2305 | NULL,
|
---|
2306 | *pSourceMediumLockList);
|
---|
2307 | if (FAILED(rc))
|
---|
2308 | {
|
---|
2309 | delete pSourceMediumLockList;
|
---|
2310 | throw rc;
|
---|
2311 | }
|
---|
2312 |
|
---|
2313 | /* Build the target lock list (including the to-be parent chain). */
|
---|
2314 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
2315 | rc = pTarget->createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2316 | true /* fMediumLockWrite */,
|
---|
2317 | pParent,
|
---|
2318 | *pTargetMediumLockList);
|
---|
2319 | if (FAILED(rc))
|
---|
2320 | {
|
---|
2321 | delete pSourceMediumLockList;
|
---|
2322 | delete pTargetMediumLockList;
|
---|
2323 | throw rc;
|
---|
2324 | }
|
---|
2325 |
|
---|
2326 | rc = pSourceMediumLockList->Lock();
|
---|
2327 | if (FAILED(rc))
|
---|
2328 | {
|
---|
2329 | delete pSourceMediumLockList;
|
---|
2330 | delete pTargetMediumLockList;
|
---|
2331 | throw setError(rc,
|
---|
2332 | tr("Failed to lock source media '%s'"),
|
---|
2333 | getLocationFull().c_str());
|
---|
2334 | }
|
---|
2335 | rc = pTargetMediumLockList->Lock();
|
---|
2336 | if (FAILED(rc))
|
---|
2337 | {
|
---|
2338 | delete pSourceMediumLockList;
|
---|
2339 | delete pTargetMediumLockList;
|
---|
2340 | throw setError(rc,
|
---|
2341 | tr("Failed to lock target media '%s'"),
|
---|
2342 | pTarget->getLocationFull().c_str());
|
---|
2343 | }
|
---|
2344 |
|
---|
2345 | pProgress.createObject();
|
---|
2346 | rc = pProgress->init(m->pVirtualBox,
|
---|
2347 | static_cast <IMedium *>(this),
|
---|
2348 | BstrFmt(tr("Creating clone medium '%s'"), pTarget->m->strLocationFull.c_str()),
|
---|
2349 | TRUE /* aCancelable */);
|
---|
2350 | if (FAILED(rc))
|
---|
2351 | {
|
---|
2352 | delete pSourceMediumLockList;
|
---|
2353 | delete pTargetMediumLockList;
|
---|
2354 | throw rc;
|
---|
2355 | }
|
---|
2356 |
|
---|
2357 | /* setup task object to carry out the operation asynchronously */
|
---|
2358 | pTask = new Medium::CloneTask(this, pProgress, pTarget, aVariant,
|
---|
2359 | pParent, pSourceMediumLockList,
|
---|
2360 | pTargetMediumLockList);
|
---|
2361 | rc = pTask->rc();
|
---|
2362 | AssertComRC(rc);
|
---|
2363 | if (FAILED(rc))
|
---|
2364 | throw rc;
|
---|
2365 |
|
---|
2366 | if (pTarget->m->state == MediumState_NotCreated)
|
---|
2367 | pTarget->m->state = MediumState_Creating;
|
---|
2368 | }
|
---|
2369 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2370 |
|
---|
2371 | if (SUCCEEDED(rc))
|
---|
2372 | {
|
---|
2373 | rc = startThread(pTask);
|
---|
2374 |
|
---|
2375 | if (SUCCEEDED(rc))
|
---|
2376 | pProgress.queryInterfaceTo(aProgress);
|
---|
2377 | }
|
---|
2378 | else if (pTask != NULL)
|
---|
2379 | delete pTask;
|
---|
2380 |
|
---|
2381 | return rc;
|
---|
2382 | }
|
---|
2383 |
|
---|
2384 | STDMETHODIMP Medium::Compact(IProgress **aProgress)
|
---|
2385 | {
|
---|
2386 | CheckComArgOutPointerValid(aProgress);
|
---|
2387 |
|
---|
2388 | AutoCaller autoCaller(this);
|
---|
2389 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2390 |
|
---|
2391 | HRESULT rc = S_OK;
|
---|
2392 | ComObjPtr <Progress> pProgress;
|
---|
2393 | Medium::Task *pTask = NULL;
|
---|
2394 |
|
---|
2395 | try
|
---|
2396 | {
|
---|
2397 | /* We need to lock both the current object, and the tree lock (would
|
---|
2398 | * cause a lock order violation otherwise) for createMediumLockList. */
|
---|
2399 | AutoMultiWriteLock2 multilock(&m->pVirtualBox->getMediaTreeLockHandle(),
|
---|
2400 | this->lockHandle()
|
---|
2401 | COMMA_LOCKVAL_SRC_POS);
|
---|
2402 |
|
---|
2403 | /* Build the medium lock list. */
|
---|
2404 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
2405 | rc = createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
2406 | true /* fMediumLockWrite */,
|
---|
2407 | NULL,
|
---|
2408 | *pMediumLockList);
|
---|
2409 | if (FAILED(rc))
|
---|
2410 | {
|
---|
2411 | delete pMediumLockList;
|
---|
2412 | throw rc;
|
---|
2413 | }
|
---|
2414 |
|
---|
2415 | rc = pMediumLockList->Lock();
|
---|
2416 | if (FAILED(rc))
|
---|
2417 | {
|
---|
2418 | delete pMediumLockList;
|
---|
2419 | throw setError(rc,
|
---|
2420 | tr("Failed to lock media when compacting '%s'"),
|
---|
2421 | getLocationFull().c_str());
|
---|
2422 | }
|
---|
2423 |
|
---|
2424 | pProgress.createObject();
|
---|
2425 | rc = pProgress->init(m->pVirtualBox,
|
---|
2426 | static_cast <IMedium *>(this),
|
---|
2427 | BstrFmt(tr("Compacting medium '%s'"), m->strLocationFull.c_str()),
|
---|
2428 | TRUE /* aCancelable */);
|
---|
2429 | if (FAILED(rc))
|
---|
2430 | {
|
---|
2431 | delete pMediumLockList;
|
---|
2432 | throw rc;
|
---|
2433 | }
|
---|
2434 |
|
---|
2435 | /* setup task object to carry out the operation asynchronously */
|
---|
2436 | pTask = new Medium::CompactTask(this, pProgress, pMediumLockList);
|
---|
2437 | rc = pTask->rc();
|
---|
2438 | AssertComRC(rc);
|
---|
2439 | if (FAILED(rc))
|
---|
2440 | throw rc;
|
---|
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::Resize(ULONG64 aLogicalSize, IProgress **aProgress)
|
---|
2458 | {
|
---|
2459 | CheckComArgOutPointerValid(aProgress);
|
---|
2460 |
|
---|
2461 | AutoCaller autoCaller(this);
|
---|
2462 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2463 |
|
---|
2464 | NOREF(aLogicalSize);
|
---|
2465 | NOREF(aProgress);
|
---|
2466 | ReturnComNotImplemented();
|
---|
2467 | }
|
---|
2468 |
|
---|
2469 | STDMETHODIMP Medium::Reset(IProgress **aProgress)
|
---|
2470 | {
|
---|
2471 | CheckComArgOutPointerValid(aProgress);
|
---|
2472 |
|
---|
2473 | AutoCaller autoCaller(this);
|
---|
2474 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2475 |
|
---|
2476 | HRESULT rc = S_OK;
|
---|
2477 | ComObjPtr <Progress> pProgress;
|
---|
2478 | Medium::Task *pTask = NULL;
|
---|
2479 |
|
---|
2480 | try
|
---|
2481 | {
|
---|
2482 | /* canClose() needs the tree lock */
|
---|
2483 | AutoMultiWriteLock2 multilock(&m->pVirtualBox->getMediaTreeLockHandle(),
|
---|
2484 | this->lockHandle()
|
---|
2485 | COMMA_LOCKVAL_SRC_POS);
|
---|
2486 |
|
---|
2487 | LogFlowThisFunc(("ENTER for medium %s\n", m->strLocationFull.c_str()));
|
---|
2488 |
|
---|
2489 | if (m->pParent.isNull())
|
---|
2490 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2491 | tr("Medium type of '%s' is not differencing"),
|
---|
2492 | m->strLocationFull.c_str());
|
---|
2493 |
|
---|
2494 | rc = canClose();
|
---|
2495 | if (FAILED(rc))
|
---|
2496 | throw rc;
|
---|
2497 |
|
---|
2498 | /* Build the medium lock list. */
|
---|
2499 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
2500 | rc = createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2501 | true /* fMediumLockWrite */,
|
---|
2502 | NULL,
|
---|
2503 | *pMediumLockList);
|
---|
2504 | if (FAILED(rc))
|
---|
2505 | {
|
---|
2506 | delete pMediumLockList;
|
---|
2507 | throw rc;
|
---|
2508 | }
|
---|
2509 |
|
---|
2510 | rc = pMediumLockList->Lock();
|
---|
2511 | if (FAILED(rc))
|
---|
2512 | {
|
---|
2513 | delete pMediumLockList;
|
---|
2514 | throw setError(rc,
|
---|
2515 | tr("Failed to lock media when resetting '%s'"),
|
---|
2516 | getLocationFull().c_str());
|
---|
2517 | }
|
---|
2518 |
|
---|
2519 | pProgress.createObject();
|
---|
2520 | rc = pProgress->init(m->pVirtualBox,
|
---|
2521 | static_cast<IMedium*>(this),
|
---|
2522 | BstrFmt(tr("Resetting differencing medium '%s'"), m->strLocationFull.c_str()),
|
---|
2523 | FALSE /* aCancelable */);
|
---|
2524 | if (FAILED(rc))
|
---|
2525 | throw rc;
|
---|
2526 |
|
---|
2527 | /* setup task object to carry out the operation asynchronously */
|
---|
2528 | pTask = new Medium::ResetTask(this, pProgress, pMediumLockList);
|
---|
2529 | rc = pTask->rc();
|
---|
2530 | AssertComRC(rc);
|
---|
2531 | if (FAILED(rc))
|
---|
2532 | throw rc;
|
---|
2533 | }
|
---|
2534 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2535 |
|
---|
2536 | if (SUCCEEDED(rc))
|
---|
2537 | {
|
---|
2538 | rc = startThread(pTask);
|
---|
2539 |
|
---|
2540 | if (SUCCEEDED(rc))
|
---|
2541 | pProgress.queryInterfaceTo(aProgress);
|
---|
2542 | }
|
---|
2543 | else
|
---|
2544 | {
|
---|
2545 | /* Note: on success, the task will unlock this */
|
---|
2546 | {
|
---|
2547 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2548 | HRESULT rc2 = UnlockWrite(NULL);
|
---|
2549 | AssertComRC(rc2);
|
---|
2550 | }
|
---|
2551 | if (pTask != NULL)
|
---|
2552 | delete pTask;
|
---|
2553 | }
|
---|
2554 |
|
---|
2555 | LogFlowThisFunc(("LEAVE, rc=%Rhrc\n", rc));
|
---|
2556 |
|
---|
2557 | return rc;
|
---|
2558 | }
|
---|
2559 |
|
---|
2560 | ////////////////////////////////////////////////////////////////////////////////
|
---|
2561 | //
|
---|
2562 | // Medium public internal methods
|
---|
2563 | //
|
---|
2564 | ////////////////////////////////////////////////////////////////////////////////
|
---|
2565 |
|
---|
2566 | /**
|
---|
2567 | * Internal method to return the medium's parent medium. Must have caller + locking!
|
---|
2568 | * @return
|
---|
2569 | */
|
---|
2570 | const ComObjPtr<Medium>& Medium::getParent() const
|
---|
2571 | {
|
---|
2572 | return m->pParent;
|
---|
2573 | }
|
---|
2574 |
|
---|
2575 | /**
|
---|
2576 | * Internal method to return the medium's list of child media. Must have caller + locking!
|
---|
2577 | * @return
|
---|
2578 | */
|
---|
2579 | const MediaList& Medium::getChildren() const
|
---|
2580 | {
|
---|
2581 | return m->llChildren;
|
---|
2582 | }
|
---|
2583 |
|
---|
2584 | /**
|
---|
2585 | * Internal method to return the medium's registry machine (i.e. the machine in whose
|
---|
2586 | * machine XML this medium is listed), or NULL if the medium is registered globally.
|
---|
2587 | *
|
---|
2588 | * Must have caller + locking!
|
---|
2589 | *
|
---|
2590 | * @return
|
---|
2591 | */
|
---|
2592 | const Guid& Medium::getRegistryMachineId() const
|
---|
2593 | {
|
---|
2594 | return m->uuidRegistryMachine;
|
---|
2595 | }
|
---|
2596 |
|
---|
2597 | /**
|
---|
2598 | * Internal method to return the medium's GUID. Must have caller + locking!
|
---|
2599 | * @return
|
---|
2600 | */
|
---|
2601 | const Guid& Medium::getId() const
|
---|
2602 | {
|
---|
2603 | return m->id;
|
---|
2604 | }
|
---|
2605 |
|
---|
2606 | /**
|
---|
2607 | * Internal method to return the medium's state. Must have caller + locking!
|
---|
2608 | * @return
|
---|
2609 | */
|
---|
2610 | MediumState_T Medium::getState() const
|
---|
2611 | {
|
---|
2612 | return m->state;
|
---|
2613 | }
|
---|
2614 |
|
---|
2615 | /**
|
---|
2616 | * Internal method to return the medium's variant. Must have caller + locking!
|
---|
2617 | * @return
|
---|
2618 | */
|
---|
2619 | MediumVariant_T Medium::getVariant() const
|
---|
2620 | {
|
---|
2621 | return m->variant;
|
---|
2622 | }
|
---|
2623 |
|
---|
2624 | /**
|
---|
2625 | * Internal method which returns true if this medium represents a host drive.
|
---|
2626 | * @return
|
---|
2627 | */
|
---|
2628 | bool Medium::isHostDrive() const
|
---|
2629 | {
|
---|
2630 | return m->hostDrive;
|
---|
2631 | }
|
---|
2632 |
|
---|
2633 | /**
|
---|
2634 | * Internal method to return the medium's location. Must have caller + locking!
|
---|
2635 | * @return
|
---|
2636 | */
|
---|
2637 | const Utf8Str& Medium::getLocation() const
|
---|
2638 | {
|
---|
2639 | return m->strLocation;
|
---|
2640 | }
|
---|
2641 |
|
---|
2642 | /**
|
---|
2643 | * Internal method to return the medium's full location. Must have caller + locking!
|
---|
2644 | * @return
|
---|
2645 | */
|
---|
2646 | const Utf8Str& Medium::getLocationFull() const
|
---|
2647 | {
|
---|
2648 | return m->strLocationFull;
|
---|
2649 | }
|
---|
2650 |
|
---|
2651 | /**
|
---|
2652 | * Internal method to return the medium's format string. Must have caller + locking!
|
---|
2653 | * @return
|
---|
2654 | */
|
---|
2655 | const Utf8Str& Medium::getFormat() const
|
---|
2656 | {
|
---|
2657 | return m->strFormat;
|
---|
2658 | }
|
---|
2659 |
|
---|
2660 | /**
|
---|
2661 | * Internal method to return the medium's format object. Must have caller + locking!
|
---|
2662 | * @return
|
---|
2663 | */
|
---|
2664 | const ComObjPtr<MediumFormat>& Medium::getMediumFormat() const
|
---|
2665 | {
|
---|
2666 | return m->formatObj;
|
---|
2667 | }
|
---|
2668 |
|
---|
2669 | /**
|
---|
2670 | * Internal method to return the medium's size. Must have caller + locking!
|
---|
2671 | * @return
|
---|
2672 | */
|
---|
2673 | uint64_t Medium::getSize() const
|
---|
2674 | {
|
---|
2675 | return m->size;
|
---|
2676 | }
|
---|
2677 |
|
---|
2678 | /**
|
---|
2679 | * Adds the given machine and optionally the snapshot to the list of the objects
|
---|
2680 | * this medium is attached to.
|
---|
2681 | *
|
---|
2682 | * @param aMachineId Machine ID.
|
---|
2683 | * @param aSnapshotId Snapshot ID; when non-empty, adds a snapshot attachment.
|
---|
2684 | */
|
---|
2685 | HRESULT Medium::addBackReference(const Guid &aMachineId,
|
---|
2686 | const Guid &aSnapshotId /*= Guid::Empty*/)
|
---|
2687 | {
|
---|
2688 | AssertReturn(!aMachineId.isEmpty(), E_FAIL);
|
---|
2689 |
|
---|
2690 | LogFlowThisFunc(("ENTER, aMachineId: {%RTuuid}, aSnapshotId: {%RTuuid}\n", aMachineId.raw(), aSnapshotId.raw()));
|
---|
2691 |
|
---|
2692 | AutoCaller autoCaller(this);
|
---|
2693 | AssertComRCReturnRC(autoCaller.rc());
|
---|
2694 |
|
---|
2695 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2696 |
|
---|
2697 | switch (m->state)
|
---|
2698 | {
|
---|
2699 | case MediumState_Created:
|
---|
2700 | case MediumState_Inaccessible:
|
---|
2701 | case MediumState_LockedRead:
|
---|
2702 | case MediumState_LockedWrite:
|
---|
2703 | break;
|
---|
2704 |
|
---|
2705 | default:
|
---|
2706 | return setStateError();
|
---|
2707 | }
|
---|
2708 |
|
---|
2709 | if (m->numCreateDiffTasks > 0)
|
---|
2710 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
2711 | tr("Cannot attach medium '%s' {%RTuuid}: %u differencing child media are being created"),
|
---|
2712 | m->strLocationFull.c_str(),
|
---|
2713 | m->id.raw(),
|
---|
2714 | m->numCreateDiffTasks);
|
---|
2715 |
|
---|
2716 | BackRefList::iterator it = std::find_if(m->backRefs.begin(),
|
---|
2717 | m->backRefs.end(),
|
---|
2718 | BackRef::EqualsTo(aMachineId));
|
---|
2719 | if (it == m->backRefs.end())
|
---|
2720 | {
|
---|
2721 | BackRef ref(aMachineId, aSnapshotId);
|
---|
2722 | m->backRefs.push_back(ref);
|
---|
2723 |
|
---|
2724 | return S_OK;
|
---|
2725 | }
|
---|
2726 |
|
---|
2727 | // if the caller has not supplied a snapshot ID, then we're attaching
|
---|
2728 | // to a machine a medium which represents the machine's current state,
|
---|
2729 | // so set the flag
|
---|
2730 | if (aSnapshotId.isEmpty())
|
---|
2731 | {
|
---|
2732 | /* sanity: no duplicate attachments */
|
---|
2733 | AssertReturn(!it->fInCurState, E_FAIL);
|
---|
2734 | it->fInCurState = true;
|
---|
2735 |
|
---|
2736 | return S_OK;
|
---|
2737 | }
|
---|
2738 |
|
---|
2739 | // otherwise: a snapshot medium is being attached
|
---|
2740 |
|
---|
2741 | /* sanity: no duplicate attachments */
|
---|
2742 | for (BackRef::GuidList::const_iterator jt = it->llSnapshotIds.begin();
|
---|
2743 | jt != it->llSnapshotIds.end();
|
---|
2744 | ++jt)
|
---|
2745 | {
|
---|
2746 | const Guid &idOldSnapshot = *jt;
|
---|
2747 |
|
---|
2748 | if (idOldSnapshot == aSnapshotId)
|
---|
2749 | {
|
---|
2750 | #ifdef DEBUG
|
---|
2751 | dumpBackRefs();
|
---|
2752 | #endif
|
---|
2753 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
2754 | tr("Cannot attach medium '%s' {%RTuuid} from snapshot '%RTuuid': medium is already in use by this snapshot!"),
|
---|
2755 | m->strLocationFull.c_str(),
|
---|
2756 | m->id.raw(),
|
---|
2757 | aSnapshotId.raw(),
|
---|
2758 | idOldSnapshot.raw());
|
---|
2759 | }
|
---|
2760 | }
|
---|
2761 |
|
---|
2762 | it->llSnapshotIds.push_back(aSnapshotId);
|
---|
2763 | it->fInCurState = false;
|
---|
2764 |
|
---|
2765 | LogFlowThisFuncLeave();
|
---|
2766 |
|
---|
2767 | return S_OK;
|
---|
2768 | }
|
---|
2769 |
|
---|
2770 | /**
|
---|
2771 | * Removes the given machine and optionally the snapshot from the list of the
|
---|
2772 | * objects this medium is attached to.
|
---|
2773 | *
|
---|
2774 | * @param aMachineId Machine ID.
|
---|
2775 | * @param aSnapshotId Snapshot ID; when non-empty, removes the snapshot
|
---|
2776 | * attachment.
|
---|
2777 | */
|
---|
2778 | HRESULT Medium::removeBackReference(const Guid &aMachineId,
|
---|
2779 | const Guid &aSnapshotId /*= Guid::Empty*/)
|
---|
2780 | {
|
---|
2781 | AssertReturn(!aMachineId.isEmpty(), E_FAIL);
|
---|
2782 |
|
---|
2783 | AutoCaller autoCaller(this);
|
---|
2784 | AssertComRCReturnRC(autoCaller.rc());
|
---|
2785 |
|
---|
2786 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2787 |
|
---|
2788 | BackRefList::iterator it =
|
---|
2789 | std::find_if(m->backRefs.begin(), m->backRefs.end(),
|
---|
2790 | BackRef::EqualsTo(aMachineId));
|
---|
2791 | AssertReturn(it != m->backRefs.end(), E_FAIL);
|
---|
2792 |
|
---|
2793 | if (aSnapshotId.isEmpty())
|
---|
2794 | {
|
---|
2795 | /* remove the current state attachment */
|
---|
2796 | it->fInCurState = false;
|
---|
2797 | }
|
---|
2798 | else
|
---|
2799 | {
|
---|
2800 | /* remove the snapshot attachment */
|
---|
2801 | BackRef::GuidList::iterator jt =
|
---|
2802 | std::find(it->llSnapshotIds.begin(), it->llSnapshotIds.end(), aSnapshotId);
|
---|
2803 |
|
---|
2804 | AssertReturn(jt != it->llSnapshotIds.end(), E_FAIL);
|
---|
2805 | it->llSnapshotIds.erase(jt);
|
---|
2806 | }
|
---|
2807 |
|
---|
2808 | /* if the backref becomes empty, remove it */
|
---|
2809 | if (it->fInCurState == false && it->llSnapshotIds.size() == 0)
|
---|
2810 | m->backRefs.erase(it);
|
---|
2811 |
|
---|
2812 | return S_OK;
|
---|
2813 | }
|
---|
2814 |
|
---|
2815 | /**
|
---|
2816 | * Internal method to return the medium's list of backrefs. Must have caller + locking!
|
---|
2817 | * @return
|
---|
2818 | */
|
---|
2819 | const Guid* Medium::getFirstMachineBackrefId() const
|
---|
2820 | {
|
---|
2821 | if (!m->backRefs.size())
|
---|
2822 | return NULL;
|
---|
2823 |
|
---|
2824 | return &m->backRefs.front().machineId;
|
---|
2825 | }
|
---|
2826 |
|
---|
2827 | const Guid* Medium::getFirstMachineBackrefSnapshotId() const
|
---|
2828 | {
|
---|
2829 | if (!m->backRefs.size())
|
---|
2830 | return NULL;
|
---|
2831 |
|
---|
2832 | const BackRef &ref = m->backRefs.front();
|
---|
2833 | if (!ref.llSnapshotIds.size())
|
---|
2834 | return NULL;
|
---|
2835 |
|
---|
2836 | return &ref.llSnapshotIds.front();
|
---|
2837 | }
|
---|
2838 |
|
---|
2839 | #ifdef DEBUG
|
---|
2840 | /**
|
---|
2841 | * Debugging helper that gets called after VirtualBox initialization that writes all
|
---|
2842 | * machine backreferences to the debug log.
|
---|
2843 | */
|
---|
2844 | void Medium::dumpBackRefs()
|
---|
2845 | {
|
---|
2846 | AutoCaller autoCaller(this);
|
---|
2847 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2848 |
|
---|
2849 | LogFlowThisFunc(("Dumping backrefs for medium '%s':\n", m->strLocationFull.c_str()));
|
---|
2850 |
|
---|
2851 | for (BackRefList::iterator it2 = m->backRefs.begin();
|
---|
2852 | it2 != m->backRefs.end();
|
---|
2853 | ++it2)
|
---|
2854 | {
|
---|
2855 | const BackRef &ref = *it2;
|
---|
2856 | LogFlowThisFunc((" Backref from machine {%RTuuid} (fInCurState: %d)\n", ref.machineId.raw(), ref.fInCurState));
|
---|
2857 |
|
---|
2858 | for (BackRef::GuidList::const_iterator jt2 = it2->llSnapshotIds.begin();
|
---|
2859 | jt2 != it2->llSnapshotIds.end();
|
---|
2860 | ++jt2)
|
---|
2861 | {
|
---|
2862 | const Guid &id = *jt2;
|
---|
2863 | LogFlowThisFunc((" Backref from snapshot {%RTuuid}\n", id.raw()));
|
---|
2864 | }
|
---|
2865 | }
|
---|
2866 | }
|
---|
2867 | #endif
|
---|
2868 |
|
---|
2869 | /**
|
---|
2870 | * Checks if the given change of \a aOldPath to \a aNewPath affects the location
|
---|
2871 | * of this media and updates it if necessary to reflect the new location.
|
---|
2872 | *
|
---|
2873 | * @param aOldPath Old path (full).
|
---|
2874 | * @param aNewPath New path (full).
|
---|
2875 | *
|
---|
2876 | * @note Locks this object for writing.
|
---|
2877 | */
|
---|
2878 | HRESULT Medium::updatePath(const Utf8Str &strOldPath, const Utf8Str &strNewPath)
|
---|
2879 | {
|
---|
2880 | AssertReturn(!strOldPath.isEmpty(), E_FAIL);
|
---|
2881 | AssertReturn(!strNewPath.isEmpty(), E_FAIL);
|
---|
2882 |
|
---|
2883 | AutoCaller autoCaller(this);
|
---|
2884 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2885 |
|
---|
2886 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2887 |
|
---|
2888 | LogFlowThisFunc(("locationFull.before='%s'\n", m->strLocationFull.c_str()));
|
---|
2889 |
|
---|
2890 | const char *pcszMediumPath = m->strLocationFull.c_str();
|
---|
2891 |
|
---|
2892 | if (RTPathStartsWith(pcszMediumPath, strOldPath.c_str()))
|
---|
2893 | {
|
---|
2894 | Utf8Str newPath(strNewPath);
|
---|
2895 | newPath.append(pcszMediumPath + strOldPath.length());
|
---|
2896 | unconst(m->strLocationFull) = newPath;
|
---|
2897 |
|
---|
2898 | Utf8Str path;
|
---|
2899 | m->pVirtualBox->copyPathRelativeToConfig(newPath, path);
|
---|
2900 | unconst(m->strLocation) = path;
|
---|
2901 |
|
---|
2902 | LogFlowThisFunc(("locationFull.after='%s'\n", m->strLocationFull.c_str()));
|
---|
2903 | }
|
---|
2904 |
|
---|
2905 | return S_OK;
|
---|
2906 | }
|
---|
2907 |
|
---|
2908 | /**
|
---|
2909 | * Returns the base medium of the media chain this medium is part of.
|
---|
2910 | *
|
---|
2911 | * The base medium is found by walking up the parent-child relationship axis.
|
---|
2912 | * If the medium doesn't have a parent (i.e. it's a base medium), it
|
---|
2913 | * returns itself in response to this method.
|
---|
2914 | *
|
---|
2915 | * @param aLevel Where to store the number of ancestors of this medium
|
---|
2916 | * (zero for the base), may be @c NULL.
|
---|
2917 | *
|
---|
2918 | * @note Locks medium tree for reading.
|
---|
2919 | */
|
---|
2920 | ComObjPtr<Medium> Medium::getBase(uint32_t *aLevel /*= NULL*/)
|
---|
2921 | {
|
---|
2922 | ComObjPtr<Medium> pBase;
|
---|
2923 | uint32_t level;
|
---|
2924 |
|
---|
2925 | AutoCaller autoCaller(this);
|
---|
2926 | AssertReturn(autoCaller.isOk(), pBase);
|
---|
2927 |
|
---|
2928 | /* we access mParent */
|
---|
2929 | AutoReadLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
2930 |
|
---|
2931 | pBase = this;
|
---|
2932 | level = 0;
|
---|
2933 |
|
---|
2934 | if (m->pParent)
|
---|
2935 | {
|
---|
2936 | for (;;)
|
---|
2937 | {
|
---|
2938 | AutoCaller baseCaller(pBase);
|
---|
2939 | AssertReturn(baseCaller.isOk(), pBase);
|
---|
2940 |
|
---|
2941 | if (pBase->m->pParent.isNull())
|
---|
2942 | break;
|
---|
2943 |
|
---|
2944 | pBase = pBase->m->pParent;
|
---|
2945 | ++level;
|
---|
2946 | }
|
---|
2947 | }
|
---|
2948 |
|
---|
2949 | if (aLevel != NULL)
|
---|
2950 | *aLevel = level;
|
---|
2951 |
|
---|
2952 | return pBase;
|
---|
2953 | }
|
---|
2954 |
|
---|
2955 | /**
|
---|
2956 | * Returns @c true if this medium cannot be modified because it has
|
---|
2957 | * dependants (children) or is part of the snapshot. Related to the medium
|
---|
2958 | * type and posterity, not to the current media state.
|
---|
2959 | *
|
---|
2960 | * @note Locks this object and medium tree for reading.
|
---|
2961 | */
|
---|
2962 | bool Medium::isReadOnly()
|
---|
2963 | {
|
---|
2964 | AutoCaller autoCaller(this);
|
---|
2965 | AssertComRCReturn(autoCaller.rc(), false);
|
---|
2966 |
|
---|
2967 | /* we access children */
|
---|
2968 | AutoReadLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
2969 |
|
---|
2970 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2971 |
|
---|
2972 | switch (m->type)
|
---|
2973 | {
|
---|
2974 | case MediumType_Normal:
|
---|
2975 | {
|
---|
2976 | if (getChildren().size() != 0)
|
---|
2977 | return true;
|
---|
2978 |
|
---|
2979 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
2980 | it != m->backRefs.end(); ++it)
|
---|
2981 | if (it->llSnapshotIds.size() != 0)
|
---|
2982 | return true;
|
---|
2983 |
|
---|
2984 | return false;
|
---|
2985 | }
|
---|
2986 | case MediumType_Immutable:
|
---|
2987 | return true;
|
---|
2988 | case MediumType_Writethrough:
|
---|
2989 | case MediumType_Shareable:
|
---|
2990 | return false;
|
---|
2991 | default:
|
---|
2992 | break;
|
---|
2993 | }
|
---|
2994 |
|
---|
2995 | AssertFailedReturn(false);
|
---|
2996 | }
|
---|
2997 |
|
---|
2998 | /**
|
---|
2999 | * Saves medium data by appending a new child node to the given
|
---|
3000 | * parent XML settings node.
|
---|
3001 | *
|
---|
3002 | * @param data Settings struct to be updated.
|
---|
3003 | *
|
---|
3004 | * @note Locks this object, medium tree and children for reading.
|
---|
3005 | */
|
---|
3006 | HRESULT Medium::saveSettings(settings::Medium &data)
|
---|
3007 | {
|
---|
3008 | AutoCaller autoCaller(this);
|
---|
3009 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
3010 |
|
---|
3011 | /* we access mParent */
|
---|
3012 | AutoReadLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3013 |
|
---|
3014 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3015 |
|
---|
3016 | data.uuid = m->id;
|
---|
3017 | data.strLocation = m->strLocation;
|
---|
3018 | data.strFormat = m->strFormat;
|
---|
3019 |
|
---|
3020 | /* optional, only for diffs, default is false */
|
---|
3021 | if (m->pParent)
|
---|
3022 | data.fAutoReset = m->autoReset;
|
---|
3023 | else
|
---|
3024 | data.fAutoReset = false;
|
---|
3025 |
|
---|
3026 | /* optional */
|
---|
3027 | data.strDescription = m->strDescription;
|
---|
3028 |
|
---|
3029 | /* optional properties */
|
---|
3030 | data.properties.clear();
|
---|
3031 | for (settings::StringsMap::const_iterator it = m->mapProperties.begin();
|
---|
3032 | it != m->mapProperties.end();
|
---|
3033 | ++it)
|
---|
3034 | {
|
---|
3035 | /* only save properties that have non-default values */
|
---|
3036 | if (!it->second.isEmpty())
|
---|
3037 | {
|
---|
3038 | const Utf8Str &name = it->first;
|
---|
3039 | const Utf8Str &value = it->second;
|
---|
3040 | data.properties[name] = value;
|
---|
3041 | }
|
---|
3042 | }
|
---|
3043 |
|
---|
3044 | /* only for base media */
|
---|
3045 | if (m->pParent.isNull())
|
---|
3046 | data.hdType = m->type;
|
---|
3047 |
|
---|
3048 | /* save all children */
|
---|
3049 | for (MediaList::const_iterator it = getChildren().begin();
|
---|
3050 | it != getChildren().end();
|
---|
3051 | ++it)
|
---|
3052 | {
|
---|
3053 | settings::Medium med;
|
---|
3054 | HRESULT rc = (*it)->saveSettings(med);
|
---|
3055 | AssertComRCReturnRC(rc);
|
---|
3056 | data.llChildren.push_back(med);
|
---|
3057 | }
|
---|
3058 |
|
---|
3059 | return S_OK;
|
---|
3060 | }
|
---|
3061 |
|
---|
3062 | /**
|
---|
3063 | * Compares the location of this medium to the given location.
|
---|
3064 | *
|
---|
3065 | * The comparison takes the location details into account. For example, if the
|
---|
3066 | * location is a file in the host's filesystem, a case insensitive comparison
|
---|
3067 | * will be performed for case insensitive filesystems.
|
---|
3068 | *
|
---|
3069 | * @param aLocation Location to compare to (as is).
|
---|
3070 | * @param aResult Where to store the result of comparison: 0 if locations
|
---|
3071 | * are equal, 1 if this object's location is greater than
|
---|
3072 | * the specified location, and -1 otherwise.
|
---|
3073 | */
|
---|
3074 | HRESULT Medium::compareLocationTo(const Utf8Str &strLocation, int &aResult)
|
---|
3075 | {
|
---|
3076 | AutoCaller autoCaller(this);
|
---|
3077 | AssertComRCReturnRC(autoCaller.rc());
|
---|
3078 |
|
---|
3079 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3080 |
|
---|
3081 | Utf8Str locationFull(m->strLocationFull);
|
---|
3082 |
|
---|
3083 | /// @todo NEWMEDIA delegate the comparison to the backend?
|
---|
3084 |
|
---|
3085 | if (m->formatObj->getCapabilities() & MediumFormatCapabilities_File)
|
---|
3086 | {
|
---|
3087 | Utf8Str location;
|
---|
3088 |
|
---|
3089 | /* For locations represented by files, append the default path if
|
---|
3090 | * only the name is given, and then get the full path. */
|
---|
3091 | if (!RTPathHavePath(strLocation.c_str()))
|
---|
3092 | {
|
---|
3093 | m->pVirtualBox->getDefaultHardDiskFolder(location);
|
---|
3094 | location.append(RTPATH_DELIMITER);
|
---|
3095 | location.append(strLocation);
|
---|
3096 | }
|
---|
3097 | else
|
---|
3098 | location = strLocation;
|
---|
3099 |
|
---|
3100 | int vrc = m->pVirtualBox->calculateFullPath(location, location);
|
---|
3101 | if (RT_FAILURE(vrc))
|
---|
3102 | return setError(VBOX_E_FILE_ERROR,
|
---|
3103 | tr("Invalid medium storage file location '%s' (%Rrc)"),
|
---|
3104 | location.c_str(),
|
---|
3105 | vrc);
|
---|
3106 |
|
---|
3107 | aResult = RTPathCompare(locationFull.c_str(), location.c_str());
|
---|
3108 | }
|
---|
3109 | else
|
---|
3110 | aResult = locationFull.compare(strLocation);
|
---|
3111 |
|
---|
3112 | return S_OK;
|
---|
3113 | }
|
---|
3114 |
|
---|
3115 | /**
|
---|
3116 | * Constructs a medium lock list for this medium. The lock is not taken.
|
---|
3117 | *
|
---|
3118 | * @note Locks the medium tree for reading.
|
---|
3119 | *
|
---|
3120 | * @param fFailIfInaccessible If true, this fails with an error if a medium is inaccessible. If false,
|
---|
3121 | * inaccessible media are silently skipped and not locked (i.e. their state remains "Inaccessible");
|
---|
3122 | * this is necessary for a VM's removable media VM startup for which we do not want to fail.
|
---|
3123 | * @param fMediumLockWrite Whether to associate a write lock with this medium.
|
---|
3124 | * @param pToBeParent Medium which will become the parent of this medium.
|
---|
3125 | * @param mediumLockList Where to store the resulting list.
|
---|
3126 | */
|
---|
3127 | HRESULT Medium::createMediumLockList(bool fFailIfInaccessible,
|
---|
3128 | bool fMediumLockWrite,
|
---|
3129 | Medium *pToBeParent,
|
---|
3130 | MediumLockList &mediumLockList)
|
---|
3131 | {
|
---|
3132 | AutoCaller autoCaller(this);
|
---|
3133 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
3134 |
|
---|
3135 | HRESULT rc = S_OK;
|
---|
3136 |
|
---|
3137 | /* we access parent medium objects */
|
---|
3138 | AutoReadLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3139 |
|
---|
3140 | /* paranoid sanity checking if the medium has a to-be parent medium */
|
---|
3141 | if (pToBeParent)
|
---|
3142 | {
|
---|
3143 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3144 | ComAssertRet(getParent().isNull(), E_FAIL);
|
---|
3145 | ComAssertRet(getChildren().size() == 0, E_FAIL);
|
---|
3146 | }
|
---|
3147 |
|
---|
3148 | ErrorInfoKeeper eik;
|
---|
3149 | MultiResult mrc(S_OK);
|
---|
3150 |
|
---|
3151 | ComObjPtr<Medium> pMedium = this;
|
---|
3152 | while (!pMedium.isNull())
|
---|
3153 | {
|
---|
3154 | // need write lock for RefreshState if medium is inaccessible
|
---|
3155 | AutoWriteLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
3156 |
|
---|
3157 | /* Accessibility check must be first, otherwise locking interferes
|
---|
3158 | * with getting the medium state. Lock lists are not created for
|
---|
3159 | * fun, and thus getting the medium status is no luxury. */
|
---|
3160 | MediumState_T mediumState = pMedium->getState();
|
---|
3161 | if (mediumState == MediumState_Inaccessible)
|
---|
3162 | {
|
---|
3163 | rc = pMedium->RefreshState(&mediumState);
|
---|
3164 | if (FAILED(rc)) return rc;
|
---|
3165 |
|
---|
3166 | if (mediumState == MediumState_Inaccessible)
|
---|
3167 | {
|
---|
3168 | // ignore inaccessible ISO media and silently return S_OK,
|
---|
3169 | // otherwise VM startup (esp. restore) may fail without good reason
|
---|
3170 | if (!fFailIfInaccessible)
|
---|
3171 | return S_OK;
|
---|
3172 |
|
---|
3173 | // otherwise report an error
|
---|
3174 | Bstr error;
|
---|
3175 | rc = pMedium->COMGETTER(LastAccessError)(error.asOutParam());
|
---|
3176 | if (FAILED(rc)) return rc;
|
---|
3177 |
|
---|
3178 | /* collect multiple errors */
|
---|
3179 | eik.restore();
|
---|
3180 | Assert(!error.isEmpty());
|
---|
3181 | mrc = setError(E_FAIL,
|
---|
3182 | "%ls",
|
---|
3183 | error.raw());
|
---|
3184 | // error message will be something like
|
---|
3185 | // "Could not open the medium ... VD: error VERR_FILE_NOT_FOUND opening image file ... (VERR_FILE_NOT_FOUND).
|
---|
3186 | eik.fetch();
|
---|
3187 | }
|
---|
3188 | }
|
---|
3189 |
|
---|
3190 | if (pMedium == this)
|
---|
3191 | mediumLockList.Prepend(pMedium, fMediumLockWrite);
|
---|
3192 | else
|
---|
3193 | mediumLockList.Prepend(pMedium, false);
|
---|
3194 |
|
---|
3195 | pMedium = pMedium->getParent();
|
---|
3196 | if (pMedium.isNull() && pToBeParent)
|
---|
3197 | {
|
---|
3198 | pMedium = pToBeParent;
|
---|
3199 | pToBeParent = NULL;
|
---|
3200 | }
|
---|
3201 | }
|
---|
3202 |
|
---|
3203 | return mrc;
|
---|
3204 | }
|
---|
3205 |
|
---|
3206 | /**
|
---|
3207 | * Returns a preferred format for differencing media.
|
---|
3208 | */
|
---|
3209 | Utf8Str Medium::getPreferredDiffFormat()
|
---|
3210 | {
|
---|
3211 | AutoCaller autoCaller(this);
|
---|
3212 | AssertComRCReturn(autoCaller.rc(), Utf8Str::Empty);
|
---|
3213 |
|
---|
3214 | /* check that our own format supports diffs */
|
---|
3215 | if (!(m->formatObj->getCapabilities() & MediumFormatCapabilities_Differencing))
|
---|
3216 | {
|
---|
3217 | /* use the default format if not */
|
---|
3218 | Utf8Str tmp;
|
---|
3219 | m->pVirtualBox->getDefaultHardDiskFormat(tmp);
|
---|
3220 | return tmp;
|
---|
3221 | }
|
---|
3222 |
|
---|
3223 | /* m->strFormat is const, no need to lock */
|
---|
3224 | return m->strFormat;
|
---|
3225 | }
|
---|
3226 |
|
---|
3227 | /**
|
---|
3228 | * Returns the medium device type. Must have caller + locking!
|
---|
3229 | * @return
|
---|
3230 | */
|
---|
3231 | DeviceType_T Medium::getDeviceType() const
|
---|
3232 | {
|
---|
3233 | return m->devType;
|
---|
3234 | }
|
---|
3235 |
|
---|
3236 | /**
|
---|
3237 | * Returns the medium type. Must have caller + locking!
|
---|
3238 | * @return
|
---|
3239 | */
|
---|
3240 | MediumType_T Medium::getType() const
|
---|
3241 | {
|
---|
3242 | return m->type;
|
---|
3243 | }
|
---|
3244 |
|
---|
3245 | /**
|
---|
3246 | * Returns a short version of the location attribute.
|
---|
3247 | *
|
---|
3248 | * @note Must be called from under this object's read or write lock.
|
---|
3249 | */
|
---|
3250 | Utf8Str Medium::getName()
|
---|
3251 | {
|
---|
3252 | Utf8Str name = RTPathFilename(m->strLocationFull.c_str());
|
---|
3253 | return name;
|
---|
3254 | }
|
---|
3255 |
|
---|
3256 | /**
|
---|
3257 | * Sets the value of m->strLocation and calculates the value of m->strLocationFull.
|
---|
3258 | *
|
---|
3259 | * Treats non-FS-path locations specially, and prepends the default medium
|
---|
3260 | * folder if the given location string does not contain any path information
|
---|
3261 | * at all.
|
---|
3262 | *
|
---|
3263 | * Also, if the specified location is a file path that ends with '/' then the
|
---|
3264 | * file name part will be generated by this method automatically in the format
|
---|
3265 | * '{<uuid>}.<ext>' where <uuid> is a fresh UUID that this method will generate
|
---|
3266 | * and assign to this medium, and <ext> is the default extension for this
|
---|
3267 | * medium's storage format. Note that this procedure requires the media state to
|
---|
3268 | * be NotCreated and will return a failure otherwise.
|
---|
3269 | *
|
---|
3270 | * @param aLocation Location of the storage unit. If the location is a FS-path,
|
---|
3271 | * then it can be relative to the VirtualBox home directory.
|
---|
3272 | * @param aFormat Optional fallback format if it is an import and the format
|
---|
3273 | * cannot be determined.
|
---|
3274 | *
|
---|
3275 | * @note Must be called from under this object's write lock.
|
---|
3276 | */
|
---|
3277 | HRESULT Medium::setLocation(const Utf8Str &aLocation,
|
---|
3278 | const Utf8Str &aFormat /* = Utf8Str::Empty */)
|
---|
3279 | {
|
---|
3280 | AssertReturn(!aLocation.isEmpty(), E_FAIL);
|
---|
3281 |
|
---|
3282 | AutoCaller autoCaller(this);
|
---|
3283 | AssertComRCReturnRC(autoCaller.rc());
|
---|
3284 |
|
---|
3285 | /* formatObj may be null only when initializing from an existing path and
|
---|
3286 | * no format is known yet */
|
---|
3287 | AssertReturn( (!m->strFormat.isEmpty() && !m->formatObj.isNull())
|
---|
3288 | || ( autoCaller.state() == InInit
|
---|
3289 | && m->state != MediumState_NotCreated
|
---|
3290 | && m->id.isEmpty()
|
---|
3291 | && m->strFormat.isEmpty()
|
---|
3292 | && m->formatObj.isNull()),
|
---|
3293 | E_FAIL);
|
---|
3294 |
|
---|
3295 | /* are we dealing with a new medium constructed using the existing
|
---|
3296 | * location? */
|
---|
3297 | bool isImport = m->strFormat.isEmpty();
|
---|
3298 |
|
---|
3299 | if ( isImport
|
---|
3300 | || ( (m->formatObj->getCapabilities() & MediumFormatCapabilities_File)
|
---|
3301 | && !m->hostDrive))
|
---|
3302 | {
|
---|
3303 | Guid id;
|
---|
3304 |
|
---|
3305 | Utf8Str location(aLocation);
|
---|
3306 |
|
---|
3307 | if (m->state == MediumState_NotCreated)
|
---|
3308 | {
|
---|
3309 | /* must be a file (formatObj must be already known) */
|
---|
3310 | Assert(m->formatObj->getCapabilities() & MediumFormatCapabilities_File);
|
---|
3311 |
|
---|
3312 | if (RTPathFilename(location.c_str()) == NULL)
|
---|
3313 | {
|
---|
3314 | /* no file name is given (either an empty string or ends with a
|
---|
3315 | * slash), generate a new UUID + file name if the state allows
|
---|
3316 | * this */
|
---|
3317 |
|
---|
3318 | ComAssertMsgRet(!m->formatObj->getFileExtensions().empty(),
|
---|
3319 | ("Must be at least one extension if it is MediumFormatCapabilities_File\n"),
|
---|
3320 | E_FAIL);
|
---|
3321 |
|
---|
3322 | Utf8Str strExt = m->formatObj->getFileExtensions().front();
|
---|
3323 | ComAssertMsgRet(!strExt.isEmpty(),
|
---|
3324 | ("Default extension must not be empty\n"),
|
---|
3325 | E_FAIL);
|
---|
3326 |
|
---|
3327 | id.create();
|
---|
3328 |
|
---|
3329 | location = Utf8StrFmt("%s{%RTuuid}.%s",
|
---|
3330 | location.c_str(), id.raw(), strExt.c_str());
|
---|
3331 | }
|
---|
3332 | }
|
---|
3333 |
|
---|
3334 | /* append the default folder if no path is given */
|
---|
3335 | if (!RTPathHavePath(location.c_str()))
|
---|
3336 | {
|
---|
3337 | Utf8Str tmp;
|
---|
3338 | m->pVirtualBox->getDefaultHardDiskFolder(tmp);
|
---|
3339 | tmp.append(RTPATH_DELIMITER);
|
---|
3340 | tmp.append(location);
|
---|
3341 | location = tmp;
|
---|
3342 | }
|
---|
3343 |
|
---|
3344 | /* get the full file name */
|
---|
3345 | Utf8Str locationFull;
|
---|
3346 | int vrc = m->pVirtualBox->calculateFullPath(location, locationFull);
|
---|
3347 | if (RT_FAILURE(vrc))
|
---|
3348 | return setError(VBOX_E_FILE_ERROR,
|
---|
3349 | tr("Invalid medium storage file location '%s' (%Rrc)"),
|
---|
3350 | location.c_str(), vrc);
|
---|
3351 |
|
---|
3352 | /* detect the backend from the storage unit if importing */
|
---|
3353 | if (isImport)
|
---|
3354 | {
|
---|
3355 | char *backendName = NULL;
|
---|
3356 |
|
---|
3357 | /* is it a file? */
|
---|
3358 | {
|
---|
3359 | RTFILE file;
|
---|
3360 | vrc = RTFileOpen(&file, locationFull.c_str(), RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
3361 | if (RT_SUCCESS(vrc))
|
---|
3362 | RTFileClose(file);
|
---|
3363 | }
|
---|
3364 | if (RT_SUCCESS(vrc))
|
---|
3365 | {
|
---|
3366 | vrc = VDGetFormat(NULL, locationFull.c_str(), &backendName);
|
---|
3367 | }
|
---|
3368 | else if (vrc != VERR_FILE_NOT_FOUND && vrc != VERR_PATH_NOT_FOUND)
|
---|
3369 | {
|
---|
3370 | /* assume it's not a file, restore the original location */
|
---|
3371 | location = locationFull = aLocation;
|
---|
3372 | vrc = VDGetFormat(NULL, locationFull.c_str(), &backendName);
|
---|
3373 | }
|
---|
3374 |
|
---|
3375 | if (RT_FAILURE(vrc))
|
---|
3376 | {
|
---|
3377 | if (vrc == VERR_FILE_NOT_FOUND || vrc == VERR_PATH_NOT_FOUND)
|
---|
3378 | return setError(VBOX_E_FILE_ERROR,
|
---|
3379 | tr("Could not find file for the medium '%s' (%Rrc)"),
|
---|
3380 | locationFull.c_str(), vrc);
|
---|
3381 | else if (aFormat.isEmpty())
|
---|
3382 | return setError(VBOX_E_IPRT_ERROR,
|
---|
3383 | tr("Could not get the storage format of the medium '%s' (%Rrc)"),
|
---|
3384 | locationFull.c_str(), vrc);
|
---|
3385 | else
|
---|
3386 | {
|
---|
3387 | HRESULT rc = setFormat(aFormat);
|
---|
3388 | /* setFormat() must not fail since we've just used the backend so
|
---|
3389 | * the format object must be there */
|
---|
3390 | AssertComRCReturnRC(rc);
|
---|
3391 | }
|
---|
3392 | }
|
---|
3393 | else
|
---|
3394 | {
|
---|
3395 | ComAssertRet(backendName != NULL && *backendName != '\0', E_FAIL);
|
---|
3396 |
|
---|
3397 | HRESULT rc = setFormat(backendName);
|
---|
3398 | RTStrFree(backendName);
|
---|
3399 |
|
---|
3400 | /* setFormat() must not fail since we've just used the backend so
|
---|
3401 | * the format object must be there */
|
---|
3402 | AssertComRCReturnRC(rc);
|
---|
3403 | }
|
---|
3404 | }
|
---|
3405 |
|
---|
3406 | /* is it still a file? */
|
---|
3407 | if (m->formatObj->getCapabilities() & MediumFormatCapabilities_File)
|
---|
3408 | {
|
---|
3409 | m->strLocation = location;
|
---|
3410 | m->strLocationFull = locationFull;
|
---|
3411 |
|
---|
3412 | if (m->state == MediumState_NotCreated)
|
---|
3413 | {
|
---|
3414 | /* assign a new UUID (this UUID will be used when calling
|
---|
3415 | * VDCreateBase/VDCreateDiff as a wanted UUID). Note that we
|
---|
3416 | * also do that if we didn't generate it to make sure it is
|
---|
3417 | * either generated by us or reset to null */
|
---|
3418 | unconst(m->id) = id;
|
---|
3419 | }
|
---|
3420 | }
|
---|
3421 | else
|
---|
3422 | {
|
---|
3423 | m->strLocation = locationFull;
|
---|
3424 | m->strLocationFull = locationFull;
|
---|
3425 | }
|
---|
3426 | }
|
---|
3427 | else
|
---|
3428 | {
|
---|
3429 | m->strLocation = aLocation;
|
---|
3430 | m->strLocationFull = aLocation;
|
---|
3431 | }
|
---|
3432 |
|
---|
3433 | return S_OK;
|
---|
3434 | }
|
---|
3435 |
|
---|
3436 | /**
|
---|
3437 | * Queries information from the medium.
|
---|
3438 | *
|
---|
3439 | * As a result of this call, the accessibility state and data members such as
|
---|
3440 | * size and description will be updated with the current information.
|
---|
3441 | *
|
---|
3442 | * @note This method may block during a system I/O call that checks storage
|
---|
3443 | * accessibility.
|
---|
3444 | *
|
---|
3445 | * @note Locks medium tree for reading and writing (for new diff media checked
|
---|
3446 | * for the first time). Locks mParent for reading. Locks this object for
|
---|
3447 | * writing.
|
---|
3448 | *
|
---|
3449 | * @param fSetImageId Whether to reset the UUID contained in the image file to the UUID in the medium instance data (see SetIDs())
|
---|
3450 | * @param fSetParentId Whether to reset the parent UUID contained in the image file to the parent UUID in the medium instance data (see SetIDs())
|
---|
3451 | * @return
|
---|
3452 | */
|
---|
3453 | HRESULT Medium::queryInfo(bool fSetImageId, bool fSetParentId)
|
---|
3454 | {
|
---|
3455 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3456 |
|
---|
3457 | if ( m->state != MediumState_Created
|
---|
3458 | && m->state != MediumState_Inaccessible
|
---|
3459 | && m->state != MediumState_LockedRead)
|
---|
3460 | return E_FAIL;
|
---|
3461 |
|
---|
3462 | HRESULT rc = S_OK;
|
---|
3463 |
|
---|
3464 | int vrc = VINF_SUCCESS;
|
---|
3465 |
|
---|
3466 | /* check if a blocking queryInfo() call is in progress on some other thread,
|
---|
3467 | * and wait for it to finish if so instead of querying data ourselves */
|
---|
3468 | if (m->queryInfoRunning)
|
---|
3469 | {
|
---|
3470 | Assert( m->state == MediumState_LockedRead
|
---|
3471 | || m->state == MediumState_LockedWrite);
|
---|
3472 |
|
---|
3473 | alock.leave();
|
---|
3474 | vrc = RTSemEventMultiWait(m->queryInfoSem, RT_INDEFINITE_WAIT);
|
---|
3475 | alock.enter();
|
---|
3476 |
|
---|
3477 | AssertRC(vrc);
|
---|
3478 |
|
---|
3479 | return S_OK;
|
---|
3480 | }
|
---|
3481 |
|
---|
3482 | bool success = false;
|
---|
3483 | Utf8Str lastAccessError;
|
---|
3484 |
|
---|
3485 | /* are we dealing with a new medium constructed using the existing
|
---|
3486 | * location? */
|
---|
3487 | bool isImport = m->id.isEmpty();
|
---|
3488 | unsigned uOpenFlags = VD_OPEN_FLAGS_INFO;
|
---|
3489 |
|
---|
3490 | /* Note that we don't use VD_OPEN_FLAGS_READONLY when opening new
|
---|
3491 | * media because that would prevent necessary modifications
|
---|
3492 | * when opening media of some third-party formats for the first
|
---|
3493 | * time in VirtualBox (such as VMDK for which VDOpen() needs to
|
---|
3494 | * generate an UUID if it is missing) */
|
---|
3495 | if ( (m->hddOpenMode == OpenReadOnly)
|
---|
3496 | || !isImport
|
---|
3497 | )
|
---|
3498 | uOpenFlags |= VD_OPEN_FLAGS_READONLY;
|
---|
3499 |
|
---|
3500 | /* Open shareable medium with the appropriate flags */
|
---|
3501 | if (m->type == MediumType_Shareable)
|
---|
3502 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
3503 |
|
---|
3504 | /* Lock the medium, which makes the behavior much more consistent */
|
---|
3505 | if (uOpenFlags & (VD_OPEN_FLAGS_READONLY || VD_OPEN_FLAGS_SHAREABLE))
|
---|
3506 | rc = LockRead(NULL);
|
---|
3507 | else
|
---|
3508 | rc = LockWrite(NULL);
|
---|
3509 | if (FAILED(rc)) return rc;
|
---|
3510 |
|
---|
3511 | /* Copies of the input state fields which are not read-only,
|
---|
3512 | * as we're dropping the lock. CAUTION: be extremely careful what
|
---|
3513 | * you do with the contents of this medium object, as you will
|
---|
3514 | * create races if there are concurrent changes. */
|
---|
3515 | Utf8Str format(m->strFormat);
|
---|
3516 | Utf8Str location(m->strLocationFull);
|
---|
3517 | ComObjPtr<MediumFormat> formatObj = m->formatObj;
|
---|
3518 |
|
---|
3519 | /* "Output" values which can't be set because the lock isn't held
|
---|
3520 | * at the time the values are determined. */
|
---|
3521 | Guid mediumId = m->id;
|
---|
3522 | uint64_t mediumSize = 0;
|
---|
3523 | uint64_t mediumLogicalSize = 0;
|
---|
3524 |
|
---|
3525 | /* leave the lock before a lengthy operation */
|
---|
3526 | vrc = RTSemEventMultiReset(m->queryInfoSem);
|
---|
3527 | AssertRCReturn(vrc, E_FAIL);
|
---|
3528 | m->queryInfoRunning = true;
|
---|
3529 | alock.leave();
|
---|
3530 |
|
---|
3531 | try
|
---|
3532 | {
|
---|
3533 | /* skip accessibility checks for host drives */
|
---|
3534 | if (m->hostDrive)
|
---|
3535 | {
|
---|
3536 | success = true;
|
---|
3537 | throw S_OK;
|
---|
3538 | }
|
---|
3539 |
|
---|
3540 | PVBOXHDD hdd;
|
---|
3541 | vrc = VDCreate(m->vdDiskIfaces, &hdd);
|
---|
3542 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
3543 |
|
---|
3544 | try
|
---|
3545 | {
|
---|
3546 | /** @todo This kind of opening of media is assuming that diff
|
---|
3547 | * media can be opened as base media. Should be documented that
|
---|
3548 | * it must work for all medium format backends. */
|
---|
3549 | vrc = VDOpen(hdd,
|
---|
3550 | format.c_str(),
|
---|
3551 | location.c_str(),
|
---|
3552 | uOpenFlags,
|
---|
3553 | m->vdDiskIfaces);
|
---|
3554 | if (RT_FAILURE(vrc))
|
---|
3555 | {
|
---|
3556 | lastAccessError = Utf8StrFmt(tr("Could not open the medium '%s'%s"),
|
---|
3557 | location.c_str(), vdError(vrc).c_str());
|
---|
3558 | throw S_OK;
|
---|
3559 | }
|
---|
3560 |
|
---|
3561 | if (formatObj->getCapabilities() & MediumFormatCapabilities_Uuid)
|
---|
3562 | {
|
---|
3563 | /* Modify the UUIDs if necessary. The associated fields are
|
---|
3564 | * not modified by other code, so no need to copy. */
|
---|
3565 | if (fSetImageId)
|
---|
3566 | {
|
---|
3567 | vrc = VDSetUuid(hdd, 0, m->uuidImage);
|
---|
3568 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
3569 | }
|
---|
3570 | if (fSetParentId)
|
---|
3571 | {
|
---|
3572 | vrc = VDSetParentUuid(hdd, 0, m->uuidParentImage);
|
---|
3573 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
3574 | }
|
---|
3575 | /* zap the information, these are no long-term members */
|
---|
3576 | unconst(m->uuidImage).clear();
|
---|
3577 | unconst(m->uuidParentImage).clear();
|
---|
3578 |
|
---|
3579 | /* check the UUID */
|
---|
3580 | RTUUID uuid;
|
---|
3581 | vrc = VDGetUuid(hdd, 0, &uuid);
|
---|
3582 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
3583 |
|
---|
3584 | if (isImport)
|
---|
3585 | {
|
---|
3586 | mediumId = uuid;
|
---|
3587 |
|
---|
3588 | if (mediumId.isEmpty() && (m->hddOpenMode == OpenReadOnly))
|
---|
3589 | // only when importing a VDMK that has no UUID, create one in memory
|
---|
3590 | mediumId.create();
|
---|
3591 | }
|
---|
3592 | else
|
---|
3593 | {
|
---|
3594 | Assert(!mediumId.isEmpty());
|
---|
3595 |
|
---|
3596 | if (mediumId != uuid)
|
---|
3597 | {
|
---|
3598 | lastAccessError = Utf8StrFmt(
|
---|
3599 | tr("UUID {%RTuuid} of the medium '%s' does not match the value {%RTuuid} stored in the media registry ('%s')"),
|
---|
3600 | &uuid,
|
---|
3601 | location.c_str(),
|
---|
3602 | mediumId.raw(),
|
---|
3603 | m->pVirtualBox->settingsFilePath().c_str());
|
---|
3604 | throw S_OK;
|
---|
3605 | }
|
---|
3606 | }
|
---|
3607 | }
|
---|
3608 | else
|
---|
3609 | {
|
---|
3610 | /* the backend does not support storing UUIDs within the
|
---|
3611 | * underlying storage so use what we store in XML */
|
---|
3612 |
|
---|
3613 | /* generate an UUID for an imported UUID-less medium */
|
---|
3614 | if (isImport)
|
---|
3615 | {
|
---|
3616 | if (fSetImageId)
|
---|
3617 | mediumId = m->uuidImage;
|
---|
3618 | else
|
---|
3619 | mediumId.create();
|
---|
3620 | }
|
---|
3621 | }
|
---|
3622 |
|
---|
3623 | /* get the medium variant */
|
---|
3624 | unsigned uImageFlags;
|
---|
3625 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
3626 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
3627 | m->variant = (MediumVariant_T)uImageFlags;
|
---|
3628 |
|
---|
3629 | /* check/get the parent uuid and update corresponding state */
|
---|
3630 | if (uImageFlags & VD_IMAGE_FLAGS_DIFF)
|
---|
3631 | {
|
---|
3632 | RTUUID parentId;
|
---|
3633 | vrc = VDGetParentUuid(hdd, 0, &parentId);
|
---|
3634 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
3635 |
|
---|
3636 | if (isImport)
|
---|
3637 | {
|
---|
3638 | /* the parent must be known to us. Note that we freely
|
---|
3639 | * call locking methods of mVirtualBox and parent, as all
|
---|
3640 | * relevant locks must be already held. There may be no
|
---|
3641 | * concurrent access to the just opened medium on other
|
---|
3642 | * threads yet (and init() will fail if this method reports
|
---|
3643 | * MediumState_Inaccessible) */
|
---|
3644 |
|
---|
3645 | Guid id = parentId;
|
---|
3646 | ComObjPtr<Medium> pParent;
|
---|
3647 | rc = m->pVirtualBox->findHardDisk(&id, Utf8Str::Empty, false /* aSetError */, &pParent);
|
---|
3648 | if (FAILED(rc))
|
---|
3649 | {
|
---|
3650 | lastAccessError = Utf8StrFmt(
|
---|
3651 | tr("Parent medium with UUID {%RTuuid} of the medium '%s' is not found in the media registry ('%s')"),
|
---|
3652 | &parentId, location.c_str(),
|
---|
3653 | m->pVirtualBox->settingsFilePath().c_str());
|
---|
3654 | throw S_OK;
|
---|
3655 | }
|
---|
3656 |
|
---|
3657 | /* we set mParent & children() */
|
---|
3658 | AutoWriteLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3659 |
|
---|
3660 | Assert(m->pParent.isNull());
|
---|
3661 | m->pParent = pParent;
|
---|
3662 | m->pParent->m->llChildren.push_back(this);
|
---|
3663 | }
|
---|
3664 | else
|
---|
3665 | {
|
---|
3666 | /* we access mParent */
|
---|
3667 | AutoReadLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3668 |
|
---|
3669 | /* check that parent UUIDs match. Note that there's no need
|
---|
3670 | * for the parent's AutoCaller (our lifetime is bound to
|
---|
3671 | * it) */
|
---|
3672 |
|
---|
3673 | if (m->pParent.isNull())
|
---|
3674 | {
|
---|
3675 | lastAccessError = Utf8StrFmt(
|
---|
3676 | tr("Medium type of '%s' is differencing but it is not associated with any parent medium in the media registry ('%s')"),
|
---|
3677 | location.c_str(),
|
---|
3678 | m->pVirtualBox->settingsFilePath().c_str());
|
---|
3679 | throw S_OK;
|
---|
3680 | }
|
---|
3681 |
|
---|
3682 | AutoReadLock parentLock(m->pParent COMMA_LOCKVAL_SRC_POS);
|
---|
3683 | if ( m->pParent->getState() != MediumState_Inaccessible
|
---|
3684 | && m->pParent->getId() != parentId)
|
---|
3685 | {
|
---|
3686 | lastAccessError = Utf8StrFmt(
|
---|
3687 | tr("Parent UUID {%RTuuid} of the medium '%s' does not match UUID {%RTuuid} of its parent medium stored in the media registry ('%s')"),
|
---|
3688 | &parentId, location.c_str(),
|
---|
3689 | m->pParent->getId().raw(),
|
---|
3690 | m->pVirtualBox->settingsFilePath().c_str());
|
---|
3691 | throw S_OK;
|
---|
3692 | }
|
---|
3693 |
|
---|
3694 | /// @todo NEWMEDIA what to do if the parent is not
|
---|
3695 | /// accessible while the diff is? Probably nothing. The
|
---|
3696 | /// real code will detect the mismatch anyway.
|
---|
3697 | }
|
---|
3698 | }
|
---|
3699 |
|
---|
3700 | mediumSize = VDGetFileSize(hdd, 0);
|
---|
3701 | mediumLogicalSize = VDGetSize(hdd, 0) / _1M;
|
---|
3702 |
|
---|
3703 | success = true;
|
---|
3704 | }
|
---|
3705 | catch (HRESULT aRC)
|
---|
3706 | {
|
---|
3707 | rc = aRC;
|
---|
3708 | }
|
---|
3709 |
|
---|
3710 | VDDestroy(hdd);
|
---|
3711 |
|
---|
3712 | }
|
---|
3713 | catch (HRESULT aRC)
|
---|
3714 | {
|
---|
3715 | rc = aRC;
|
---|
3716 | }
|
---|
3717 |
|
---|
3718 | alock.enter();
|
---|
3719 |
|
---|
3720 | if (isImport)
|
---|
3721 | unconst(m->id) = mediumId;
|
---|
3722 |
|
---|
3723 | if (success)
|
---|
3724 | {
|
---|
3725 | m->size = mediumSize;
|
---|
3726 | m->logicalSize = mediumLogicalSize;
|
---|
3727 | m->strLastAccessError.setNull();
|
---|
3728 | }
|
---|
3729 | else
|
---|
3730 | {
|
---|
3731 | m->strLastAccessError = lastAccessError;
|
---|
3732 | LogWarningFunc(("'%s' is not accessible (error='%s', rc=%Rhrc, vrc=%Rrc)\n",
|
---|
3733 | location.c_str(), m->strLastAccessError.c_str(),
|
---|
3734 | rc, vrc));
|
---|
3735 | }
|
---|
3736 |
|
---|
3737 | /* inform other callers if there are any */
|
---|
3738 | RTSemEventMultiSignal(m->queryInfoSem);
|
---|
3739 | m->queryInfoRunning = false;
|
---|
3740 |
|
---|
3741 | /* Set the proper state according to the result of the check */
|
---|
3742 | if (success)
|
---|
3743 | m->preLockState = MediumState_Created;
|
---|
3744 | else
|
---|
3745 | m->preLockState = MediumState_Inaccessible;
|
---|
3746 |
|
---|
3747 | if (uOpenFlags & (VD_OPEN_FLAGS_READONLY || VD_OPEN_FLAGS_SHAREABLE))
|
---|
3748 | rc = UnlockRead(NULL);
|
---|
3749 | else
|
---|
3750 | rc = UnlockWrite(NULL);
|
---|
3751 | if (FAILED(rc)) return rc;
|
---|
3752 |
|
---|
3753 | return rc;
|
---|
3754 | }
|
---|
3755 |
|
---|
3756 | /**
|
---|
3757 | * Sets the extended error info according to the current media state.
|
---|
3758 | *
|
---|
3759 | * @note Must be called from under this object's write or read lock.
|
---|
3760 | */
|
---|
3761 | HRESULT Medium::setStateError()
|
---|
3762 | {
|
---|
3763 | HRESULT rc = E_FAIL;
|
---|
3764 |
|
---|
3765 | switch (m->state)
|
---|
3766 | {
|
---|
3767 | case MediumState_NotCreated:
|
---|
3768 | {
|
---|
3769 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3770 | tr("Storage for the medium '%s' is not created"),
|
---|
3771 | m->strLocationFull.c_str());
|
---|
3772 | break;
|
---|
3773 | }
|
---|
3774 | case MediumState_Created:
|
---|
3775 | {
|
---|
3776 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3777 | tr("Storage for the medium '%s' is already created"),
|
---|
3778 | m->strLocationFull.c_str());
|
---|
3779 | break;
|
---|
3780 | }
|
---|
3781 | case MediumState_LockedRead:
|
---|
3782 | {
|
---|
3783 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3784 | tr("Medium '%s' is locked for reading by another task"),
|
---|
3785 | m->strLocationFull.c_str());
|
---|
3786 | break;
|
---|
3787 | }
|
---|
3788 | case MediumState_LockedWrite:
|
---|
3789 | {
|
---|
3790 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3791 | tr("Medium '%s' is locked for writing by another task"),
|
---|
3792 | m->strLocationFull.c_str());
|
---|
3793 | break;
|
---|
3794 | }
|
---|
3795 | case MediumState_Inaccessible:
|
---|
3796 | {
|
---|
3797 | /* be in sync with Console::powerUpThread() */
|
---|
3798 | if (!m->strLastAccessError.isEmpty())
|
---|
3799 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3800 | tr("Medium '%s' is not accessible. %s"),
|
---|
3801 | m->strLocationFull.c_str(), m->strLastAccessError.c_str());
|
---|
3802 | else
|
---|
3803 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3804 | tr("Medium '%s' is not accessible"),
|
---|
3805 | m->strLocationFull.c_str());
|
---|
3806 | break;
|
---|
3807 | }
|
---|
3808 | case MediumState_Creating:
|
---|
3809 | {
|
---|
3810 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3811 | tr("Storage for the medium '%s' is being created"),
|
---|
3812 | m->strLocationFull.c_str());
|
---|
3813 | break;
|
---|
3814 | }
|
---|
3815 | case MediumState_Deleting:
|
---|
3816 | {
|
---|
3817 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3818 | tr("Storage for the medium '%s' is being deleted"),
|
---|
3819 | m->strLocationFull.c_str());
|
---|
3820 | break;
|
---|
3821 | }
|
---|
3822 | default:
|
---|
3823 | {
|
---|
3824 | AssertFailed();
|
---|
3825 | break;
|
---|
3826 | }
|
---|
3827 | }
|
---|
3828 |
|
---|
3829 | return rc;
|
---|
3830 | }
|
---|
3831 |
|
---|
3832 | /**
|
---|
3833 | * Implementation for the public Medium::Close() with the exception of calling
|
---|
3834 | * VirtualBox::saveSettings(), in case someone wants to call this for several
|
---|
3835 | * media.
|
---|
3836 | *
|
---|
3837 | * After this returns with success, uninit() has been called on the medium, and
|
---|
3838 | * the object is no longer usable ("not ready" state).
|
---|
3839 | *
|
---|
3840 | * @param pfNeedsSaveSettings Optional pointer to a bool that must have been initialized to false and that will be set to true
|
---|
3841 | * by this function if the caller should invoke VirtualBox::saveSettings() because the global settings have changed.
|
---|
3842 | * This only works in "wait" mode; otherwise saveSettings gets called automatically by the thread that was created,
|
---|
3843 | * and this parameter is ignored.
|
---|
3844 | * @param autoCaller AutoCaller instance which must have been created on the caller's stack for this medium. This gets released here
|
---|
3845 | * upon which the Medium instance gets uninitialized.
|
---|
3846 | * @return
|
---|
3847 | */
|
---|
3848 | HRESULT Medium::close(bool *pfNeedsSaveSettings, AutoCaller &autoCaller)
|
---|
3849 | {
|
---|
3850 | // we're accessing parent/child and backrefs, so lock the tree first, then ourselves
|
---|
3851 | AutoMultiWriteLock2 multilock(&m->pVirtualBox->getMediaTreeLockHandle(),
|
---|
3852 | this->lockHandle()
|
---|
3853 | COMMA_LOCKVAL_SRC_POS);
|
---|
3854 |
|
---|
3855 | LogFlowFunc(("ENTER for %s\n", getLocationFull().c_str()));
|
---|
3856 |
|
---|
3857 | bool wasCreated = true;
|
---|
3858 |
|
---|
3859 | switch (m->state)
|
---|
3860 | {
|
---|
3861 | case MediumState_NotCreated:
|
---|
3862 | wasCreated = false;
|
---|
3863 | break;
|
---|
3864 | case MediumState_Created:
|
---|
3865 | case MediumState_Inaccessible:
|
---|
3866 | break;
|
---|
3867 | default:
|
---|
3868 | return setStateError();
|
---|
3869 | }
|
---|
3870 |
|
---|
3871 | if (m->backRefs.size() != 0)
|
---|
3872 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
3873 | tr("Medium '%s' cannot be closed because it is still attached to %d virtual machines"),
|
---|
3874 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
3875 |
|
---|
3876 | // perform extra media-dependent close checks
|
---|
3877 | HRESULT rc = canClose();
|
---|
3878 | if (FAILED(rc)) return rc;
|
---|
3879 |
|
---|
3880 | if (wasCreated)
|
---|
3881 | {
|
---|
3882 | // remove from the list of known media before performing actual
|
---|
3883 | // uninitialization (to keep the media registry consistent on
|
---|
3884 | // failure to do so)
|
---|
3885 | rc = unregisterWithVirtualBox(pfNeedsSaveSettings);
|
---|
3886 | if (FAILED(rc)) return rc;
|
---|
3887 | }
|
---|
3888 |
|
---|
3889 | // leave the AutoCaller, as otherwise uninit() will simply hang
|
---|
3890 | autoCaller.release();
|
---|
3891 |
|
---|
3892 | // Keep the locks held until after uninit, as otherwise the consistency
|
---|
3893 | // of the medium tree cannot be guaranteed.
|
---|
3894 | uninit();
|
---|
3895 |
|
---|
3896 | LogFlowFuncLeave();
|
---|
3897 |
|
---|
3898 | return rc;
|
---|
3899 | }
|
---|
3900 |
|
---|
3901 | /**
|
---|
3902 | * Deletes the medium storage unit.
|
---|
3903 | *
|
---|
3904 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
3905 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
3906 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
3907 | * progress object is created/used at all.
|
---|
3908 | *
|
---|
3909 | * When @a aWait is @c false, this method will create a thread to perform the
|
---|
3910 | * delete operation asynchronously and will return immediately. Otherwise, it
|
---|
3911 | * will perform the operation on the calling thread and will not return to the
|
---|
3912 | * caller until the operation is completed. Note that @a aProgress cannot be
|
---|
3913 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
3914 | *
|
---|
3915 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
3916 | * completion.
|
---|
3917 | * @param aWait @c true if this method should block instead of creating
|
---|
3918 | * an asynchronous thread.
|
---|
3919 | * @param pfNeedsSaveSettings Optional pointer to a bool that must have been initialized to false and that will be set to true
|
---|
3920 | * by this function if the caller should invoke VirtualBox::saveSettings() because the global settings have changed.
|
---|
3921 | * This only works in "wait" mode; otherwise saveSettings gets called automatically by the thread that was created,
|
---|
3922 | * and this parameter is ignored.
|
---|
3923 | *
|
---|
3924 | * @note Locks mVirtualBox and this object for writing. Locks medium tree for
|
---|
3925 | * writing.
|
---|
3926 | */
|
---|
3927 | HRESULT Medium::deleteStorage(ComObjPtr<Progress> *aProgress,
|
---|
3928 | bool aWait,
|
---|
3929 | bool *pfNeedsSaveSettings)
|
---|
3930 | {
|
---|
3931 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
3932 |
|
---|
3933 | AutoCaller autoCaller(this);
|
---|
3934 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
3935 |
|
---|
3936 | HRESULT rc = S_OK;
|
---|
3937 | ComObjPtr<Progress> pProgress;
|
---|
3938 | Medium::Task *pTask = NULL;
|
---|
3939 |
|
---|
3940 | try
|
---|
3941 | {
|
---|
3942 | /* we're accessing the media tree, and canClose() needs it too */
|
---|
3943 | AutoMultiWriteLock2 multilock(&m->pVirtualBox->getMediaTreeLockHandle(),
|
---|
3944 | this->lockHandle()
|
---|
3945 | COMMA_LOCKVAL_SRC_POS);
|
---|
3946 | LogFlowThisFunc(("aWait=%RTbool locationFull=%s\n", aWait, getLocationFull().c_str() ));
|
---|
3947 |
|
---|
3948 | if ( !(m->formatObj->getCapabilities() & ( MediumFormatCapabilities_CreateDynamic
|
---|
3949 | | MediumFormatCapabilities_CreateFixed)))
|
---|
3950 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3951 | tr("Medium format '%s' does not support storage deletion"),
|
---|
3952 | m->strFormat.c_str());
|
---|
3953 |
|
---|
3954 | /* Note that we are fine with Inaccessible state too: a) for symmetry
|
---|
3955 | * with create calls and b) because it doesn't really harm to try, if
|
---|
3956 | * it is really inaccessible, the delete operation will fail anyway.
|
---|
3957 | * Accepting Inaccessible state is especially important because all
|
---|
3958 | * registered media are initially Inaccessible upon VBoxSVC startup
|
---|
3959 | * until COMGETTER(RefreshState) is called. Accept Deleting state
|
---|
3960 | * because some callers need to put the medium in this state early
|
---|
3961 | * to prevent races. */
|
---|
3962 | switch (m->state)
|
---|
3963 | {
|
---|
3964 | case MediumState_Created:
|
---|
3965 | case MediumState_Deleting:
|
---|
3966 | case MediumState_Inaccessible:
|
---|
3967 | break;
|
---|
3968 | default:
|
---|
3969 | throw setStateError();
|
---|
3970 | }
|
---|
3971 |
|
---|
3972 | if (m->backRefs.size() != 0)
|
---|
3973 | {
|
---|
3974 | Utf8Str strMachines;
|
---|
3975 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
3976 | it != m->backRefs.end();
|
---|
3977 | ++it)
|
---|
3978 | {
|
---|
3979 | const BackRef &b = *it;
|
---|
3980 | if (strMachines.length())
|
---|
3981 | strMachines.append(", ");
|
---|
3982 | strMachines.append(b.machineId.toString().c_str());
|
---|
3983 | }
|
---|
3984 | #ifdef DEBUG
|
---|
3985 | dumpBackRefs();
|
---|
3986 | #endif
|
---|
3987 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
3988 | tr("Cannot delete storage: medium '%s' is still attached to the following %d virtual machine(s): %s"),
|
---|
3989 | m->strLocationFull.c_str(),
|
---|
3990 | m->backRefs.size(),
|
---|
3991 | strMachines.c_str());
|
---|
3992 | }
|
---|
3993 |
|
---|
3994 | rc = canClose();
|
---|
3995 | if (FAILED(rc))
|
---|
3996 | throw rc;
|
---|
3997 |
|
---|
3998 | /* go to Deleting state, so that the medium is not actually locked */
|
---|
3999 | if (m->state != MediumState_Deleting)
|
---|
4000 | {
|
---|
4001 | rc = markForDeletion();
|
---|
4002 | if (FAILED(rc))
|
---|
4003 | throw rc;
|
---|
4004 | }
|
---|
4005 |
|
---|
4006 | /* Build the medium lock list. */
|
---|
4007 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
4008 | rc = createMediumLockList(true /* fFailIfInaccessible */,
|
---|
4009 | true /* fMediumLockWrite */,
|
---|
4010 | NULL,
|
---|
4011 | *pMediumLockList);
|
---|
4012 | if (FAILED(rc))
|
---|
4013 | {
|
---|
4014 | delete pMediumLockList;
|
---|
4015 | throw rc;
|
---|
4016 | }
|
---|
4017 |
|
---|
4018 | rc = pMediumLockList->Lock();
|
---|
4019 | if (FAILED(rc))
|
---|
4020 | {
|
---|
4021 | delete pMediumLockList;
|
---|
4022 | throw setError(rc,
|
---|
4023 | tr("Failed to lock media when deleting '%s'"),
|
---|
4024 | getLocationFull().c_str());
|
---|
4025 | }
|
---|
4026 |
|
---|
4027 | /* try to remove from the list of known media before performing
|
---|
4028 | * actual deletion (we favor the consistency of the media registry
|
---|
4029 | * which would have been broken if unregisterWithVirtualBox() failed
|
---|
4030 | * after we successfully deleted the storage) */
|
---|
4031 | rc = unregisterWithVirtualBox(pfNeedsSaveSettings);
|
---|
4032 | if (FAILED(rc))
|
---|
4033 | throw rc;
|
---|
4034 | // no longer need lock
|
---|
4035 | multilock.release();
|
---|
4036 |
|
---|
4037 | if (aProgress != NULL)
|
---|
4038 | {
|
---|
4039 | /* use the existing progress object... */
|
---|
4040 | pProgress = *aProgress;
|
---|
4041 |
|
---|
4042 | /* ...but create a new one if it is null */
|
---|
4043 | if (pProgress.isNull())
|
---|
4044 | {
|
---|
4045 | pProgress.createObject();
|
---|
4046 | rc = pProgress->init(m->pVirtualBox,
|
---|
4047 | static_cast<IMedium*>(this),
|
---|
4048 | BstrFmt(tr("Deleting medium storage unit '%s'"), m->strLocationFull.c_str()),
|
---|
4049 | FALSE /* aCancelable */);
|
---|
4050 | if (FAILED(rc))
|
---|
4051 | throw rc;
|
---|
4052 | }
|
---|
4053 | }
|
---|
4054 |
|
---|
4055 | /* setup task object to carry out the operation sync/async */
|
---|
4056 | pTask = new Medium::DeleteTask(this, pProgress, pMediumLockList);
|
---|
4057 | rc = pTask->rc();
|
---|
4058 | AssertComRC(rc);
|
---|
4059 | if (FAILED(rc))
|
---|
4060 | throw rc;
|
---|
4061 | }
|
---|
4062 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4063 |
|
---|
4064 | if (SUCCEEDED(rc))
|
---|
4065 | {
|
---|
4066 | if (aWait)
|
---|
4067 | rc = runNow(pTask, NULL /* pfNeedsSaveSettings*/);
|
---|
4068 | else
|
---|
4069 | rc = startThread(pTask);
|
---|
4070 |
|
---|
4071 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
4072 | *aProgress = pProgress;
|
---|
4073 |
|
---|
4074 | }
|
---|
4075 | else
|
---|
4076 | {
|
---|
4077 | if (pTask)
|
---|
4078 | delete pTask;
|
---|
4079 |
|
---|
4080 | /* Undo deleting state if necessary. */
|
---|
4081 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4082 | unmarkForDeletion();
|
---|
4083 | }
|
---|
4084 |
|
---|
4085 | return rc;
|
---|
4086 | }
|
---|
4087 |
|
---|
4088 | /**
|
---|
4089 | * Mark a medium for deletion.
|
---|
4090 | *
|
---|
4091 | * @note Caller must hold the write lock on this medium!
|
---|
4092 | */
|
---|
4093 | HRESULT Medium::markForDeletion()
|
---|
4094 | {
|
---|
4095 | ComAssertRet(this->lockHandle()->isWriteLockOnCurrentThread(), E_FAIL);
|
---|
4096 | switch (m->state)
|
---|
4097 | {
|
---|
4098 | case MediumState_Created:
|
---|
4099 | case MediumState_Inaccessible:
|
---|
4100 | m->preLockState = m->state;
|
---|
4101 | m->state = MediumState_Deleting;
|
---|
4102 | return S_OK;
|
---|
4103 | default:
|
---|
4104 | return setStateError();
|
---|
4105 | }
|
---|
4106 | }
|
---|
4107 |
|
---|
4108 | /**
|
---|
4109 | * Removes the "mark for deletion".
|
---|
4110 | *
|
---|
4111 | * @note Caller must hold the write lock on this medium!
|
---|
4112 | */
|
---|
4113 | HRESULT Medium::unmarkForDeletion()
|
---|
4114 | {
|
---|
4115 | ComAssertRet(this->lockHandle()->isWriteLockOnCurrentThread(), E_FAIL);
|
---|
4116 | switch (m->state)
|
---|
4117 | {
|
---|
4118 | case MediumState_Deleting:
|
---|
4119 | m->state = m->preLockState;
|
---|
4120 | return S_OK;
|
---|
4121 | default:
|
---|
4122 | return setStateError();
|
---|
4123 | }
|
---|
4124 | }
|
---|
4125 |
|
---|
4126 | /**
|
---|
4127 | * Mark a medium for deletion which is in locked state.
|
---|
4128 | *
|
---|
4129 | * @note Caller must hold the write lock on this medium!
|
---|
4130 | */
|
---|
4131 | HRESULT Medium::markLockedForDeletion()
|
---|
4132 | {
|
---|
4133 | ComAssertRet(this->lockHandle()->isWriteLockOnCurrentThread(), E_FAIL);
|
---|
4134 | if ( ( m->state == MediumState_LockedRead
|
---|
4135 | || m->state == MediumState_LockedWrite)
|
---|
4136 | && m->preLockState == MediumState_Created)
|
---|
4137 | {
|
---|
4138 | m->preLockState = MediumState_Deleting;
|
---|
4139 | return S_OK;
|
---|
4140 | }
|
---|
4141 | else
|
---|
4142 | return setStateError();
|
---|
4143 | }
|
---|
4144 |
|
---|
4145 | /**
|
---|
4146 | * Removes the "mark for deletion" for a medium in locked state.
|
---|
4147 | *
|
---|
4148 | * @note Caller must hold the write lock on this medium!
|
---|
4149 | */
|
---|
4150 | HRESULT Medium::unmarkLockedForDeletion()
|
---|
4151 | {
|
---|
4152 | ComAssertRet(this->lockHandle()->isWriteLockOnCurrentThread(), E_FAIL);
|
---|
4153 | if ( ( m->state == MediumState_LockedRead
|
---|
4154 | || m->state == MediumState_LockedWrite)
|
---|
4155 | && m->preLockState == MediumState_Deleting)
|
---|
4156 | {
|
---|
4157 | m->preLockState = MediumState_Created;
|
---|
4158 | return S_OK;
|
---|
4159 | }
|
---|
4160 | else
|
---|
4161 | return setStateError();
|
---|
4162 | }
|
---|
4163 |
|
---|
4164 | /**
|
---|
4165 | * Creates a new differencing storage unit using the format of the given target
|
---|
4166 | * medium and the location. Note that @c aTarget must be NotCreated.
|
---|
4167 | *
|
---|
4168 | * The @a aMediumLockList parameter contains the associated medium lock list,
|
---|
4169 | * which must be in locked state. If @a aWait is @c true then the caller is
|
---|
4170 | * responsible for unlocking.
|
---|
4171 | *
|
---|
4172 | * If @a aProgress is not NULL but the object it points to is @c null then a
|
---|
4173 | * new progress object will be created and assigned to @a *aProgress on
|
---|
4174 | * success, otherwise the existing progress object is used. If @a aProgress is
|
---|
4175 | * NULL, then no progress object is created/used at all.
|
---|
4176 | *
|
---|
4177 | * When @a aWait is @c false, this method will create a thread to perform the
|
---|
4178 | * create operation asynchronously and will return immediately. Otherwise, it
|
---|
4179 | * will perform the operation on the calling thread and will not return to the
|
---|
4180 | * caller until the operation is completed. Note that @a aProgress cannot be
|
---|
4181 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
4182 | *
|
---|
4183 | * @param aTarget Target medium.
|
---|
4184 | * @param aVariant Precise medium variant to create.
|
---|
4185 | * @param aMediumLockList List of media which should be locked.
|
---|
4186 | * @param aProgress Where to find/store a Progress object to track
|
---|
4187 | * operation completion.
|
---|
4188 | * @param aWait @c true if this method should block instead of
|
---|
4189 | * creating an asynchronous thread.
|
---|
4190 | * @param pfNeedsSaveSettings Optional pointer to a bool that must have been
|
---|
4191 | * initialized to false and that will be set to true
|
---|
4192 | * by this function if the caller should invoke
|
---|
4193 | * VirtualBox::saveSettings() because the global
|
---|
4194 | * settings have changed. This only works in "wait"
|
---|
4195 | * mode; otherwise saveSettings is called
|
---|
4196 | * automatically by the thread that was created,
|
---|
4197 | * and this parameter is ignored.
|
---|
4198 | *
|
---|
4199 | * @note Locks this object and @a aTarget for writing.
|
---|
4200 | */
|
---|
4201 | HRESULT Medium::createDiffStorage(ComObjPtr<Medium> &aTarget,
|
---|
4202 | MediumVariant_T aVariant,
|
---|
4203 | MediumLockList *aMediumLockList,
|
---|
4204 | ComObjPtr<Progress> *aProgress,
|
---|
4205 | bool aWait,
|
---|
4206 | bool *pfNeedsSaveSettings)
|
---|
4207 | {
|
---|
4208 | AssertReturn(!aTarget.isNull(), E_FAIL);
|
---|
4209 | AssertReturn(aMediumLockList, E_FAIL);
|
---|
4210 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
4211 |
|
---|
4212 | AutoCaller autoCaller(this);
|
---|
4213 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4214 |
|
---|
4215 | AutoCaller targetCaller(aTarget);
|
---|
4216 | if (FAILED(targetCaller.rc())) return targetCaller.rc();
|
---|
4217 |
|
---|
4218 | HRESULT rc = S_OK;
|
---|
4219 | ComObjPtr<Progress> pProgress;
|
---|
4220 | Medium::Task *pTask = NULL;
|
---|
4221 |
|
---|
4222 | try
|
---|
4223 | {
|
---|
4224 | AutoMultiWriteLock2 alock(this, aTarget COMMA_LOCKVAL_SRC_POS);
|
---|
4225 |
|
---|
4226 | ComAssertThrow( m->type != MediumType_Writethrough
|
---|
4227 | && m->type != MediumType_Shareable, E_FAIL);
|
---|
4228 | ComAssertThrow(m->state == MediumState_LockedRead, E_FAIL);
|
---|
4229 |
|
---|
4230 | if (aTarget->m->state != MediumState_NotCreated)
|
---|
4231 | throw aTarget->setStateError();
|
---|
4232 |
|
---|
4233 | /* Check that the medium is not attached to the current state of
|
---|
4234 | * any VM referring to it. */
|
---|
4235 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
4236 | it != m->backRefs.end();
|
---|
4237 | ++it)
|
---|
4238 | {
|
---|
4239 | if (it->fInCurState)
|
---|
4240 | {
|
---|
4241 | /* Note: when a VM snapshot is being taken, all normal media
|
---|
4242 | * attached to the VM in the current state will be, as an
|
---|
4243 | * exception, also associated with the snapshot which is about
|
---|
4244 | * to create (see SnapshotMachine::init()) before deassociating
|
---|
4245 | * them from the current state (which takes place only on
|
---|
4246 | * success in Machine::fixupHardDisks()), so that the size of
|
---|
4247 | * snapshotIds will be 1 in this case. The extra condition is
|
---|
4248 | * used to filter out this legal situation. */
|
---|
4249 | if (it->llSnapshotIds.size() == 0)
|
---|
4250 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4251 | 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"),
|
---|
4252 | m->strLocationFull.c_str(), it->machineId.raw());
|
---|
4253 |
|
---|
4254 | Assert(it->llSnapshotIds.size() == 1);
|
---|
4255 | }
|
---|
4256 | }
|
---|
4257 |
|
---|
4258 | if (aProgress != NULL)
|
---|
4259 | {
|
---|
4260 | /* use the existing progress object... */
|
---|
4261 | pProgress = *aProgress;
|
---|
4262 |
|
---|
4263 | /* ...but create a new one if it is null */
|
---|
4264 | if (pProgress.isNull())
|
---|
4265 | {
|
---|
4266 | pProgress.createObject();
|
---|
4267 | rc = pProgress->init(m->pVirtualBox,
|
---|
4268 | static_cast<IMedium*>(this),
|
---|
4269 | BstrFmt(tr("Creating differencing medium storage unit '%s'"), aTarget->m->strLocationFull.c_str()),
|
---|
4270 | TRUE /* aCancelable */);
|
---|
4271 | if (FAILED(rc))
|
---|
4272 | throw rc;
|
---|
4273 | }
|
---|
4274 | }
|
---|
4275 |
|
---|
4276 | /* setup task object to carry out the operation sync/async */
|
---|
4277 | pTask = new Medium::CreateDiffTask(this, pProgress, aTarget, aVariant,
|
---|
4278 | aMediumLockList,
|
---|
4279 | aWait /* fKeepMediumLockList */);
|
---|
4280 | rc = pTask->rc();
|
---|
4281 | AssertComRC(rc);
|
---|
4282 | if (FAILED(rc))
|
---|
4283 | throw rc;
|
---|
4284 |
|
---|
4285 | /* register a task (it will deregister itself when done) */
|
---|
4286 | ++m->numCreateDiffTasks;
|
---|
4287 | Assert(m->numCreateDiffTasks != 0); /* overflow? */
|
---|
4288 |
|
---|
4289 | aTarget->m->state = MediumState_Creating;
|
---|
4290 | }
|
---|
4291 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4292 |
|
---|
4293 | if (SUCCEEDED(rc))
|
---|
4294 | {
|
---|
4295 | if (aWait)
|
---|
4296 | rc = runNow(pTask, pfNeedsSaveSettings);
|
---|
4297 | else
|
---|
4298 | rc = startThread(pTask);
|
---|
4299 |
|
---|
4300 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
4301 | *aProgress = pProgress;
|
---|
4302 | }
|
---|
4303 | else if (pTask != NULL)
|
---|
4304 | delete pTask;
|
---|
4305 |
|
---|
4306 | return rc;
|
---|
4307 | }
|
---|
4308 |
|
---|
4309 | /**
|
---|
4310 | * Prepares this (source) medium, target medium and all intermediate media
|
---|
4311 | * for the merge operation.
|
---|
4312 | *
|
---|
4313 | * This method is to be called prior to calling the #mergeTo() to perform
|
---|
4314 | * necessary consistency checks and place involved media to appropriate
|
---|
4315 | * states. If #mergeTo() is not called or fails, the state modifications
|
---|
4316 | * performed by this method must be undone by #cancelMergeTo().
|
---|
4317 | *
|
---|
4318 | * See #mergeTo() for more information about merging.
|
---|
4319 | *
|
---|
4320 | * @param pTarget Target medium.
|
---|
4321 | * @param aMachineId Allowed machine attachment. NULL means do not check.
|
---|
4322 | * @param aSnapshotId Allowed snapshot attachment. NULL or empty UUID means
|
---|
4323 | * do not check.
|
---|
4324 | * @param fLockMedia Flag whether to lock the medium lock list or not.
|
---|
4325 | * If set to false and the medium lock list locking fails
|
---|
4326 | * later you must call #cancelMergeTo().
|
---|
4327 | * @param fMergeForward Resulting merge direction (out).
|
---|
4328 | * @param pParentForTarget New parent for target medium after merge (out).
|
---|
4329 | * @param aChildrenToReparent List of children of the source which will have
|
---|
4330 | * to be reparented to the target after merge (out).
|
---|
4331 | * @param aMediumLockList Medium locking information (out).
|
---|
4332 | *
|
---|
4333 | * @note Locks medium tree for reading. Locks this object, aTarget and all
|
---|
4334 | * intermediate media for writing.
|
---|
4335 | */
|
---|
4336 | HRESULT Medium::prepareMergeTo(const ComObjPtr<Medium> &pTarget,
|
---|
4337 | const Guid *aMachineId,
|
---|
4338 | const Guid *aSnapshotId,
|
---|
4339 | bool fLockMedia,
|
---|
4340 | bool &fMergeForward,
|
---|
4341 | ComObjPtr<Medium> &pParentForTarget,
|
---|
4342 | MediaList &aChildrenToReparent,
|
---|
4343 | MediumLockList * &aMediumLockList)
|
---|
4344 | {
|
---|
4345 | AssertReturn(pTarget != NULL, E_FAIL);
|
---|
4346 | AssertReturn(pTarget != this, E_FAIL);
|
---|
4347 |
|
---|
4348 | AutoCaller autoCaller(this);
|
---|
4349 | AssertComRCReturnRC(autoCaller.rc());
|
---|
4350 |
|
---|
4351 | AutoCaller targetCaller(pTarget);
|
---|
4352 | AssertComRCReturnRC(targetCaller.rc());
|
---|
4353 |
|
---|
4354 | HRESULT rc = S_OK;
|
---|
4355 | fMergeForward = false;
|
---|
4356 | pParentForTarget.setNull();
|
---|
4357 | aChildrenToReparent.clear();
|
---|
4358 | Assert(aMediumLockList == NULL);
|
---|
4359 | aMediumLockList = NULL;
|
---|
4360 |
|
---|
4361 | try
|
---|
4362 | {
|
---|
4363 | // locking: we need the tree lock first because we access parent pointers
|
---|
4364 | AutoReadLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4365 |
|
---|
4366 | /* more sanity checking and figuring out the merge direction */
|
---|
4367 | ComObjPtr<Medium> pMedium = getParent();
|
---|
4368 | while (!pMedium.isNull() && pMedium != pTarget)
|
---|
4369 | pMedium = pMedium->getParent();
|
---|
4370 | if (pMedium == pTarget)
|
---|
4371 | fMergeForward = false;
|
---|
4372 | else
|
---|
4373 | {
|
---|
4374 | pMedium = pTarget->getParent();
|
---|
4375 | while (!pMedium.isNull() && pMedium != this)
|
---|
4376 | pMedium = pMedium->getParent();
|
---|
4377 | if (pMedium == this)
|
---|
4378 | fMergeForward = true;
|
---|
4379 | else
|
---|
4380 | {
|
---|
4381 | Utf8Str tgtLoc;
|
---|
4382 | {
|
---|
4383 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
4384 | tgtLoc = pTarget->getLocationFull();
|
---|
4385 | }
|
---|
4386 |
|
---|
4387 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4388 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4389 | tr("Media '%s' and '%s' are unrelated"),
|
---|
4390 | m->strLocationFull.c_str(), tgtLoc.c_str());
|
---|
4391 | }
|
---|
4392 | }
|
---|
4393 |
|
---|
4394 | /* Build the lock list. */
|
---|
4395 | aMediumLockList = new MediumLockList();
|
---|
4396 | if (fMergeForward)
|
---|
4397 | rc = pTarget->createMediumLockList(true /* fFailIfInaccessible */,
|
---|
4398 | true /* fMediumLockWrite */,
|
---|
4399 | NULL,
|
---|
4400 | *aMediumLockList);
|
---|
4401 | else
|
---|
4402 | rc = createMediumLockList(true /* fFailIfInaccessible */,
|
---|
4403 | false /* fMediumLockWrite */,
|
---|
4404 | NULL,
|
---|
4405 | *aMediumLockList);
|
---|
4406 | if (FAILED(rc))
|
---|
4407 | throw rc;
|
---|
4408 |
|
---|
4409 | /* Sanity checking, must be after lock list creation as it depends on
|
---|
4410 | * valid medium states. The medium objects must be accessible. Only
|
---|
4411 | * do this if immediate locking is requested, otherwise it fails when
|
---|
4412 | * we construct a medium lock list for an already running VM. Snapshot
|
---|
4413 | * deletion uses this to simplify its life. */
|
---|
4414 | if (fLockMedia)
|
---|
4415 | {
|
---|
4416 | {
|
---|
4417 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4418 | if (m->state != MediumState_Created)
|
---|
4419 | throw setStateError();
|
---|
4420 | }
|
---|
4421 | {
|
---|
4422 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
4423 | if (pTarget->m->state != MediumState_Created)
|
---|
4424 | throw pTarget->setStateError();
|
---|
4425 | }
|
---|
4426 | }
|
---|
4427 |
|
---|
4428 | /* check medium attachment and other sanity conditions */
|
---|
4429 | if (fMergeForward)
|
---|
4430 | {
|
---|
4431 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4432 | if (getChildren().size() > 1)
|
---|
4433 | {
|
---|
4434 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4435 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
4436 | m->strLocationFull.c_str(), getChildren().size());
|
---|
4437 | }
|
---|
4438 | /* One backreference is only allowed if the machine ID is not empty
|
---|
4439 | * and it matches the machine the medium is attached to (including
|
---|
4440 | * the snapshot ID if not empty). */
|
---|
4441 | if ( m->backRefs.size() != 0
|
---|
4442 | && ( !aMachineId
|
---|
4443 | || m->backRefs.size() != 1
|
---|
4444 | || aMachineId->isEmpty()
|
---|
4445 | || *getFirstMachineBackrefId() != *aMachineId
|
---|
4446 | || ( (!aSnapshotId || !aSnapshotId->isEmpty())
|
---|
4447 | && *getFirstMachineBackrefSnapshotId() != *aSnapshotId)))
|
---|
4448 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
4449 | tr("Medium '%s' is attached to %d virtual machines"),
|
---|
4450 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
4451 | if (m->type == MediumType_Immutable)
|
---|
4452 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4453 | tr("Medium '%s' is immutable"),
|
---|
4454 | m->strLocationFull.c_str());
|
---|
4455 | }
|
---|
4456 | else
|
---|
4457 | {
|
---|
4458 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
4459 | if (pTarget->getChildren().size() > 1)
|
---|
4460 | {
|
---|
4461 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
4462 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
4463 | pTarget->m->strLocationFull.c_str(),
|
---|
4464 | pTarget->getChildren().size());
|
---|
4465 | }
|
---|
4466 | if (pTarget->m->type == MediumType_Immutable)
|
---|
4467 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4468 | tr("Medium '%s' is immutable"),
|
---|
4469 | pTarget->m->strLocationFull.c_str());
|
---|
4470 | }
|
---|
4471 | ComObjPtr<Medium> pLast(fMergeForward ? (Medium *)pTarget : this);
|
---|
4472 | ComObjPtr<Medium> pLastIntermediate = pLast->getParent();
|
---|
4473 | for (pLast = pLastIntermediate;
|
---|
4474 | !pLast.isNull() && pLast != pTarget && pLast != this;
|
---|
4475 | pLast = pLast->getParent())
|
---|
4476 | {
|
---|
4477 | AutoReadLock alock(pLast COMMA_LOCKVAL_SRC_POS);
|
---|
4478 | if (pLast->getChildren().size() > 1)
|
---|
4479 | {
|
---|
4480 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
4481 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
4482 | pLast->m->strLocationFull.c_str(),
|
---|
4483 | pLast->getChildren().size());
|
---|
4484 | }
|
---|
4485 | if (pLast->m->backRefs.size() != 0)
|
---|
4486 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
4487 | tr("Medium '%s' is attached to %d virtual machines"),
|
---|
4488 | pLast->m->strLocationFull.c_str(),
|
---|
4489 | pLast->m->backRefs.size());
|
---|
4490 |
|
---|
4491 | }
|
---|
4492 |
|
---|
4493 | /* Update medium states appropriately */
|
---|
4494 | if (m->state == MediumState_Created)
|
---|
4495 | {
|
---|
4496 | rc = markForDeletion();
|
---|
4497 | if (FAILED(rc))
|
---|
4498 | throw rc;
|
---|
4499 | }
|
---|
4500 | else
|
---|
4501 | {
|
---|
4502 | if (fLockMedia)
|
---|
4503 | throw setStateError();
|
---|
4504 | else if ( m->state == MediumState_LockedWrite
|
---|
4505 | || m->state == MediumState_LockedRead)
|
---|
4506 | {
|
---|
4507 | /* Either mark it for deletiion in locked state or allow
|
---|
4508 | * others to have done so. */
|
---|
4509 | if (m->preLockState == MediumState_Created)
|
---|
4510 | markLockedForDeletion();
|
---|
4511 | else if (m->preLockState != MediumState_Deleting)
|
---|
4512 | throw setStateError();
|
---|
4513 | }
|
---|
4514 | else
|
---|
4515 | throw setStateError();
|
---|
4516 | }
|
---|
4517 |
|
---|
4518 | if (fMergeForward)
|
---|
4519 | {
|
---|
4520 | /* we will need parent to reparent target */
|
---|
4521 | pParentForTarget = m->pParent;
|
---|
4522 | }
|
---|
4523 | else
|
---|
4524 | {
|
---|
4525 | /* we will need to reparent children of the source */
|
---|
4526 | for (MediaList::const_iterator it = getChildren().begin();
|
---|
4527 | it != getChildren().end();
|
---|
4528 | ++it)
|
---|
4529 | {
|
---|
4530 | pMedium = *it;
|
---|
4531 | if (fLockMedia)
|
---|
4532 | {
|
---|
4533 | rc = pMedium->LockWrite(NULL);
|
---|
4534 | if (FAILED(rc))
|
---|
4535 | throw rc;
|
---|
4536 | }
|
---|
4537 |
|
---|
4538 | aChildrenToReparent.push_back(pMedium);
|
---|
4539 | }
|
---|
4540 | }
|
---|
4541 | for (pLast = pLastIntermediate;
|
---|
4542 | !pLast.isNull() && pLast != pTarget && pLast != this;
|
---|
4543 | pLast = pLast->getParent())
|
---|
4544 | {
|
---|
4545 | AutoWriteLock alock(pLast COMMA_LOCKVAL_SRC_POS);
|
---|
4546 | if (pLast->m->state == MediumState_Created)
|
---|
4547 | {
|
---|
4548 | rc = pLast->markForDeletion();
|
---|
4549 | if (FAILED(rc))
|
---|
4550 | throw rc;
|
---|
4551 | }
|
---|
4552 | else
|
---|
4553 | throw pLast->setStateError();
|
---|
4554 | }
|
---|
4555 |
|
---|
4556 | /* Tweak the lock list in the backward merge case, as the target
|
---|
4557 | * isn't marked to be locked for writing yet. */
|
---|
4558 | if (!fMergeForward)
|
---|
4559 | {
|
---|
4560 | MediumLockList::Base::iterator lockListBegin =
|
---|
4561 | aMediumLockList->GetBegin();
|
---|
4562 | MediumLockList::Base::iterator lockListEnd =
|
---|
4563 | aMediumLockList->GetEnd();
|
---|
4564 | lockListEnd--;
|
---|
4565 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
4566 | it != lockListEnd;
|
---|
4567 | ++it)
|
---|
4568 | {
|
---|
4569 | MediumLock &mediumLock = *it;
|
---|
4570 | if (mediumLock.GetMedium() == pTarget)
|
---|
4571 | {
|
---|
4572 | HRESULT rc2 = mediumLock.UpdateLock(true);
|
---|
4573 | AssertComRC(rc2);
|
---|
4574 | break;
|
---|
4575 | }
|
---|
4576 | }
|
---|
4577 | }
|
---|
4578 |
|
---|
4579 | if (fLockMedia)
|
---|
4580 | {
|
---|
4581 | rc = aMediumLockList->Lock();
|
---|
4582 | if (FAILED(rc))
|
---|
4583 | {
|
---|
4584 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
4585 | throw setError(rc,
|
---|
4586 | tr("Failed to lock media when merging to '%s'"),
|
---|
4587 | pTarget->getLocationFull().c_str());
|
---|
4588 | }
|
---|
4589 | }
|
---|
4590 | }
|
---|
4591 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4592 |
|
---|
4593 | if (FAILED(rc))
|
---|
4594 | {
|
---|
4595 | delete aMediumLockList;
|
---|
4596 | aMediumLockList = NULL;
|
---|
4597 | }
|
---|
4598 |
|
---|
4599 | return rc;
|
---|
4600 | }
|
---|
4601 |
|
---|
4602 | /**
|
---|
4603 | * Merges this medium to the specified medium which must be either its
|
---|
4604 | * direct ancestor or descendant.
|
---|
4605 | *
|
---|
4606 | * Given this medium is SOURCE and the specified medium is TARGET, we will
|
---|
4607 | * get two variants of the merge operation:
|
---|
4608 | *
|
---|
4609 | * forward merge
|
---|
4610 | * ------------------------->
|
---|
4611 | * [Extra] <- SOURCE <- Intermediate <- TARGET
|
---|
4612 | * Any Del Del LockWr
|
---|
4613 | *
|
---|
4614 | *
|
---|
4615 | * backward merge
|
---|
4616 | * <-------------------------
|
---|
4617 | * TARGET <- Intermediate <- SOURCE <- [Extra]
|
---|
4618 | * LockWr Del Del LockWr
|
---|
4619 | *
|
---|
4620 | * Each diagram shows the involved media on the media chain where
|
---|
4621 | * SOURCE and TARGET belong. Under each medium there is a state value which
|
---|
4622 | * the medium must have at a time of the mergeTo() call.
|
---|
4623 | *
|
---|
4624 | * The media in the square braces may be absent (e.g. when the forward
|
---|
4625 | * operation takes place and SOURCE is the base medium, or when the backward
|
---|
4626 | * merge operation takes place and TARGET is the last child in the chain) but if
|
---|
4627 | * they present they are involved too as shown.
|
---|
4628 | *
|
---|
4629 | * Neither the source medium nor intermediate media may be attached to
|
---|
4630 | * any VM directly or in the snapshot, otherwise this method will assert.
|
---|
4631 | *
|
---|
4632 | * The #prepareMergeTo() method must be called prior to this method to place all
|
---|
4633 | * involved to necessary states and perform other consistency checks.
|
---|
4634 | *
|
---|
4635 | * If @a aWait is @c true then this method will perform the operation on the
|
---|
4636 | * calling thread and will not return to the caller until the operation is
|
---|
4637 | * completed. When this method succeeds, all intermediate medium objects in
|
---|
4638 | * the chain will be uninitialized, the state of the target medium (and all
|
---|
4639 | * involved extra media) will be restored. @a aMediumLockList will not be
|
---|
4640 | * deleted, whether the operation is successful or not. The caller has to do
|
---|
4641 | * this if appropriate. Note that this (source) medium is not uninitialized
|
---|
4642 | * because of possible AutoCaller instances held by the caller of this method
|
---|
4643 | * on the current thread. It's therefore the responsibility of the caller to
|
---|
4644 | * call Medium::uninit() after releasing all callers.
|
---|
4645 | *
|
---|
4646 | * If @a aWait is @c false then this method will create a thread to perform the
|
---|
4647 | * operation asynchronously and will return immediately. If the operation
|
---|
4648 | * succeeds, the thread will uninitialize the source medium object and all
|
---|
4649 | * intermediate medium objects in the chain, reset the state of the target
|
---|
4650 | * medium (and all involved extra media) and delete @a aMediumLockList.
|
---|
4651 | * If the operation fails, the thread will only reset the states of all
|
---|
4652 | * involved media and delete @a aMediumLockList.
|
---|
4653 | *
|
---|
4654 | * When this method fails (regardless of the @a aWait mode), it is a caller's
|
---|
4655 | * responsiblity to undo state changes and delete @a aMediumLockList using
|
---|
4656 | * #cancelMergeTo().
|
---|
4657 | *
|
---|
4658 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
4659 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
4660 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
4661 | * progress object is created/used at all. Note that @a aProgress cannot be
|
---|
4662 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
4663 | *
|
---|
4664 | * @param pTarget Target medium.
|
---|
4665 | * @param fMergeForward Merge direction.
|
---|
4666 | * @param pParentForTarget New parent for target medium after merge.
|
---|
4667 | * @param aChildrenToReparent List of children of the source which will have
|
---|
4668 | * to be reparented to the target after merge.
|
---|
4669 | * @param aMediumLockList Medium locking information.
|
---|
4670 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
4671 | * completion.
|
---|
4672 | * @param aWait @c true if this method should block instead of creating
|
---|
4673 | * an asynchronous thread.
|
---|
4674 | * @param pfNeedsSaveSettings Optional pointer to a bool that must have been initialized to false and that will be set to true
|
---|
4675 | * by this function if the caller should invoke VirtualBox::saveSettings() because the global settings have changed.
|
---|
4676 | * This only works in "wait" mode; otherwise saveSettings gets called automatically by the thread that was created,
|
---|
4677 | * and this parameter is ignored.
|
---|
4678 | *
|
---|
4679 | * @note Locks the tree lock for writing. Locks the media from the chain
|
---|
4680 | * for writing.
|
---|
4681 | */
|
---|
4682 | HRESULT Medium::mergeTo(const ComObjPtr<Medium> &pTarget,
|
---|
4683 | bool fMergeForward,
|
---|
4684 | const ComObjPtr<Medium> &pParentForTarget,
|
---|
4685 | const MediaList &aChildrenToReparent,
|
---|
4686 | MediumLockList *aMediumLockList,
|
---|
4687 | ComObjPtr <Progress> *aProgress,
|
---|
4688 | bool aWait,
|
---|
4689 | bool *pfNeedsSaveSettings)
|
---|
4690 | {
|
---|
4691 | AssertReturn(pTarget != NULL, E_FAIL);
|
---|
4692 | AssertReturn(pTarget != this, E_FAIL);
|
---|
4693 | AssertReturn(aMediumLockList != NULL, E_FAIL);
|
---|
4694 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
4695 |
|
---|
4696 | AutoCaller autoCaller(this);
|
---|
4697 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4698 |
|
---|
4699 | AutoCaller targetCaller(pTarget);
|
---|
4700 | AssertComRCReturnRC(targetCaller.rc());
|
---|
4701 |
|
---|
4702 | HRESULT rc = S_OK;
|
---|
4703 | ComObjPtr <Progress> pProgress;
|
---|
4704 | Medium::Task *pTask = NULL;
|
---|
4705 |
|
---|
4706 | try
|
---|
4707 | {
|
---|
4708 | if (aProgress != NULL)
|
---|
4709 | {
|
---|
4710 | /* use the existing progress object... */
|
---|
4711 | pProgress = *aProgress;
|
---|
4712 |
|
---|
4713 | /* ...but create a new one if it is null */
|
---|
4714 | if (pProgress.isNull())
|
---|
4715 | {
|
---|
4716 | Utf8Str tgtName;
|
---|
4717 | {
|
---|
4718 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
4719 | tgtName = pTarget->getName();
|
---|
4720 | }
|
---|
4721 |
|
---|
4722 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4723 |
|
---|
4724 | pProgress.createObject();
|
---|
4725 | rc = pProgress->init(m->pVirtualBox,
|
---|
4726 | static_cast<IMedium*>(this),
|
---|
4727 | BstrFmt(tr("Merging medium '%s' to '%s'"),
|
---|
4728 | getName().c_str(),
|
---|
4729 | tgtName.c_str()),
|
---|
4730 | TRUE /* aCancelable */);
|
---|
4731 | if (FAILED(rc))
|
---|
4732 | throw rc;
|
---|
4733 | }
|
---|
4734 | }
|
---|
4735 |
|
---|
4736 | /* setup task object to carry out the operation sync/async */
|
---|
4737 | pTask = new Medium::MergeTask(this, pTarget, fMergeForward,
|
---|
4738 | pParentForTarget, aChildrenToReparent,
|
---|
4739 | pProgress, aMediumLockList,
|
---|
4740 | aWait /* fKeepMediumLockList */);
|
---|
4741 | rc = pTask->rc();
|
---|
4742 | AssertComRC(rc);
|
---|
4743 | if (FAILED(rc))
|
---|
4744 | throw rc;
|
---|
4745 | }
|
---|
4746 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4747 |
|
---|
4748 | if (SUCCEEDED(rc))
|
---|
4749 | {
|
---|
4750 | if (aWait)
|
---|
4751 | rc = runNow(pTask, pfNeedsSaveSettings);
|
---|
4752 | else
|
---|
4753 | rc = startThread(pTask);
|
---|
4754 |
|
---|
4755 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
4756 | *aProgress = pProgress;
|
---|
4757 | }
|
---|
4758 | else if (pTask != NULL)
|
---|
4759 | delete pTask;
|
---|
4760 |
|
---|
4761 | return rc;
|
---|
4762 | }
|
---|
4763 |
|
---|
4764 | /**
|
---|
4765 | * Undoes what #prepareMergeTo() did. Must be called if #mergeTo() is not
|
---|
4766 | * called or fails. Frees memory occupied by @a aMediumLockList and unlocks
|
---|
4767 | * the medium objects in @a aChildrenToReparent.
|
---|
4768 | *
|
---|
4769 | * @param aChildrenToReparent List of children of the source which will have
|
---|
4770 | * to be reparented to the target after merge.
|
---|
4771 | * @param aMediumLockList Medium locking information.
|
---|
4772 | *
|
---|
4773 | * @note Locks the media from the chain for writing.
|
---|
4774 | */
|
---|
4775 | void Medium::cancelMergeTo(const MediaList &aChildrenToReparent,
|
---|
4776 | MediumLockList *aMediumLockList)
|
---|
4777 | {
|
---|
4778 | AutoCaller autoCaller(this);
|
---|
4779 | AssertComRCReturnVoid(autoCaller.rc());
|
---|
4780 |
|
---|
4781 | AssertReturnVoid(aMediumLockList != NULL);
|
---|
4782 |
|
---|
4783 | /* Revert media marked for deletion to previous state. */
|
---|
4784 | HRESULT rc;
|
---|
4785 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
4786 | aMediumLockList->GetBegin();
|
---|
4787 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
4788 | aMediumLockList->GetEnd();
|
---|
4789 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
4790 | it != mediumListEnd;
|
---|
4791 | ++it)
|
---|
4792 | {
|
---|
4793 | const MediumLock &mediumLock = *it;
|
---|
4794 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
4795 | AutoWriteLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
4796 |
|
---|
4797 | if (pMedium->m->state == MediumState_Deleting)
|
---|
4798 | {
|
---|
4799 | rc = pMedium->unmarkForDeletion();
|
---|
4800 | AssertComRC(rc);
|
---|
4801 | }
|
---|
4802 | }
|
---|
4803 |
|
---|
4804 | /* the destructor will do the work */
|
---|
4805 | delete aMediumLockList;
|
---|
4806 |
|
---|
4807 | /* unlock the children which had to be reparented */
|
---|
4808 | for (MediaList::const_iterator it = aChildrenToReparent.begin();
|
---|
4809 | it != aChildrenToReparent.end();
|
---|
4810 | ++it)
|
---|
4811 | {
|
---|
4812 | const ComObjPtr<Medium> &pMedium = *it;
|
---|
4813 |
|
---|
4814 | AutoWriteLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
4815 | pMedium->UnlockWrite(NULL);
|
---|
4816 | }
|
---|
4817 | }
|
---|
4818 |
|
---|
4819 | ////////////////////////////////////////////////////////////////////////////////
|
---|
4820 | //
|
---|
4821 | // Private methods
|
---|
4822 | //
|
---|
4823 | ////////////////////////////////////////////////////////////////////////////////
|
---|
4824 |
|
---|
4825 | /**
|
---|
4826 | * Performs extra checks if the medium can be closed and returns S_OK in
|
---|
4827 | * this case. Otherwise, returns a respective error message. Called by
|
---|
4828 | * Close() under the medium tree lock and the medium lock.
|
---|
4829 | *
|
---|
4830 | * @note Also reused by Medium::Reset().
|
---|
4831 | *
|
---|
4832 | * @note Caller must hold the media tree write lock!
|
---|
4833 | */
|
---|
4834 | HRESULT Medium::canClose()
|
---|
4835 | {
|
---|
4836 | Assert(m->pVirtualBox->getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
4837 |
|
---|
4838 | if (getChildren().size() != 0)
|
---|
4839 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4840 | tr("Cannot close medium '%s' because it has %d child media"),
|
---|
4841 | m->strLocationFull.c_str(), getChildren().size());
|
---|
4842 |
|
---|
4843 | return S_OK;
|
---|
4844 | }
|
---|
4845 |
|
---|
4846 | /**
|
---|
4847 | * Unregisters this medium with mVirtualBox. Called by close() under the medium tree lock.
|
---|
4848 | *
|
---|
4849 | * This calls either VirtualBox::unregisterImage or VirtualBox::unregisterHardDisk depending
|
---|
4850 | * on the device type of this medium.
|
---|
4851 | *
|
---|
4852 | * @param pfNeedsSaveSettings Optional pointer to a bool that must have been initialized to false and that will be set to true
|
---|
4853 | * by this function if the caller should invoke VirtualBox::saveSettings() because the global settings have changed.
|
---|
4854 | *
|
---|
4855 | * @note Caller must have locked the media tree lock for writing!
|
---|
4856 | */
|
---|
4857 | HRESULT Medium::unregisterWithVirtualBox(bool *pfNeedsSaveSettings)
|
---|
4858 | {
|
---|
4859 | /* Note that we need to de-associate ourselves from the parent to let
|
---|
4860 | * unregisterHardDisk() properly save the registry */
|
---|
4861 |
|
---|
4862 | /* we modify mParent and access children */
|
---|
4863 | Assert(m->pVirtualBox->getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
4864 |
|
---|
4865 | Medium *pParentBackup = m->pParent;
|
---|
4866 | AssertReturn(getChildren().size() == 0, E_FAIL);
|
---|
4867 | if (m->pParent)
|
---|
4868 | deparent();
|
---|
4869 |
|
---|
4870 | HRESULT rc = E_FAIL;
|
---|
4871 | switch (m->devType)
|
---|
4872 | {
|
---|
4873 | case DeviceType_DVD:
|
---|
4874 | rc = m->pVirtualBox->unregisterImage(this, DeviceType_DVD, pfNeedsSaveSettings);
|
---|
4875 | break;
|
---|
4876 |
|
---|
4877 | case DeviceType_Floppy:
|
---|
4878 | rc = m->pVirtualBox->unregisterImage(this, DeviceType_Floppy, pfNeedsSaveSettings);
|
---|
4879 | break;
|
---|
4880 |
|
---|
4881 | case DeviceType_HardDisk:
|
---|
4882 | rc = m->pVirtualBox->unregisterHardDisk(this, pfNeedsSaveSettings);
|
---|
4883 | break;
|
---|
4884 |
|
---|
4885 | default:
|
---|
4886 | break;
|
---|
4887 | }
|
---|
4888 |
|
---|
4889 | if (FAILED(rc))
|
---|
4890 | {
|
---|
4891 | if (pParentBackup)
|
---|
4892 | {
|
---|
4893 | // re-associate with the parent as we are still relatives in the registry
|
---|
4894 | m->pParent = pParentBackup;
|
---|
4895 | m->pParent->m->llChildren.push_back(this);
|
---|
4896 | }
|
---|
4897 | }
|
---|
4898 |
|
---|
4899 | return rc;
|
---|
4900 | }
|
---|
4901 |
|
---|
4902 | /**
|
---|
4903 | * Checks that the format ID is valid and sets it on success.
|
---|
4904 | *
|
---|
4905 | * Note that this method will caller-reference the format object on success!
|
---|
4906 | * This reference must be released somewhere to let the MediumFormat object be
|
---|
4907 | * uninitialized.
|
---|
4908 | *
|
---|
4909 | * @note Must be called from under this object's write lock.
|
---|
4910 | */
|
---|
4911 | HRESULT Medium::setFormat(const Utf8Str &aFormat)
|
---|
4912 | {
|
---|
4913 | /* get the format object first */
|
---|
4914 | {
|
---|
4915 | SystemProperties *pSysProps = m->pVirtualBox->getSystemProperties();
|
---|
4916 | AutoReadLock propsLock(pSysProps COMMA_LOCKVAL_SRC_POS);
|
---|
4917 |
|
---|
4918 | unconst(m->formatObj) = pSysProps->mediumFormat(aFormat);
|
---|
4919 | if (m->formatObj.isNull())
|
---|
4920 | return setError(E_INVALIDARG,
|
---|
4921 | tr("Invalid medium storage format '%s'"),
|
---|
4922 | aFormat.c_str());
|
---|
4923 |
|
---|
4924 | /* reference the format permanently to prevent its unexpected
|
---|
4925 | * uninitialization */
|
---|
4926 | HRESULT rc = m->formatObj->addCaller();
|
---|
4927 | AssertComRCReturnRC(rc);
|
---|
4928 |
|
---|
4929 | /* get properties (preinsert them as keys in the map). Note that the
|
---|
4930 | * map doesn't grow over the object life time since the set of
|
---|
4931 | * properties is meant to be constant. */
|
---|
4932 |
|
---|
4933 | Assert(m->mapProperties.empty());
|
---|
4934 |
|
---|
4935 | for (MediumFormat::PropertyList::const_iterator it = m->formatObj->getProperties().begin();
|
---|
4936 | it != m->formatObj->getProperties().end();
|
---|
4937 | ++it)
|
---|
4938 | {
|
---|
4939 | m->mapProperties.insert(std::make_pair(it->strName, Utf8Str::Empty));
|
---|
4940 | }
|
---|
4941 | }
|
---|
4942 |
|
---|
4943 | unconst(m->strFormat) = aFormat;
|
---|
4944 |
|
---|
4945 | return S_OK;
|
---|
4946 | }
|
---|
4947 |
|
---|
4948 | /**
|
---|
4949 | * Returns the last error message collected by the vdErrorCall callback and
|
---|
4950 | * resets it.
|
---|
4951 | *
|
---|
4952 | * The error message is returned prepended with a dot and a space, like this:
|
---|
4953 | * <code>
|
---|
4954 | * ". <error_text> (%Rrc)"
|
---|
4955 | * </code>
|
---|
4956 | * to make it easily appendable to a more general error message. The @c %Rrc
|
---|
4957 | * format string is given @a aVRC as an argument.
|
---|
4958 | *
|
---|
4959 | * If there is no last error message collected by vdErrorCall or if it is a
|
---|
4960 | * null or empty string, then this function returns the following text:
|
---|
4961 | * <code>
|
---|
4962 | * " (%Rrc)"
|
---|
4963 | * </code>
|
---|
4964 | *
|
---|
4965 | * @note Doesn't do any object locking; it is assumed that the caller makes sure
|
---|
4966 | * the callback isn't called by more than one thread at a time.
|
---|
4967 | *
|
---|
4968 | * @param aVRC VBox error code to use when no error message is provided.
|
---|
4969 | */
|
---|
4970 | Utf8Str Medium::vdError(int aVRC)
|
---|
4971 | {
|
---|
4972 | Utf8Str error;
|
---|
4973 |
|
---|
4974 | if (m->vdError.isEmpty())
|
---|
4975 | error = Utf8StrFmt(" (%Rrc)", aVRC);
|
---|
4976 | else
|
---|
4977 | error = Utf8StrFmt(".\n%s", m->vdError.c_str());
|
---|
4978 |
|
---|
4979 | m->vdError.setNull();
|
---|
4980 |
|
---|
4981 | return error;
|
---|
4982 | }
|
---|
4983 |
|
---|
4984 | /**
|
---|
4985 | * Error message callback.
|
---|
4986 | *
|
---|
4987 | * Puts the reported error message to the m->vdError field.
|
---|
4988 | *
|
---|
4989 | * @note Doesn't do any object locking; it is assumed that the caller makes sure
|
---|
4990 | * the callback isn't called by more than one thread at a time.
|
---|
4991 | *
|
---|
4992 | * @param pvUser The opaque data passed on container creation.
|
---|
4993 | * @param rc The VBox error code.
|
---|
4994 | * @param RT_SRC_POS_DECL Use RT_SRC_POS.
|
---|
4995 | * @param pszFormat Error message format string.
|
---|
4996 | * @param va Error message arguments.
|
---|
4997 | */
|
---|
4998 | /*static*/
|
---|
4999 | DECLCALLBACK(void) Medium::vdErrorCall(void *pvUser, int rc, RT_SRC_POS_DECL,
|
---|
5000 | const char *pszFormat, va_list va)
|
---|
5001 | {
|
---|
5002 | NOREF(pszFile); NOREF(iLine); NOREF(pszFunction); /* RT_SRC_POS_DECL */
|
---|
5003 |
|
---|
5004 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
5005 | AssertReturnVoid(that != NULL);
|
---|
5006 |
|
---|
5007 | if (that->m->vdError.isEmpty())
|
---|
5008 | that->m->vdError =
|
---|
5009 | Utf8StrFmt("%s (%Rrc)", Utf8StrFmtVA(pszFormat, va).c_str(), rc);
|
---|
5010 | else
|
---|
5011 | that->m->vdError =
|
---|
5012 | Utf8StrFmt("%s.\n%s (%Rrc)", that->m->vdError.c_str(),
|
---|
5013 | Utf8StrFmtVA(pszFormat, va).c_str(), rc);
|
---|
5014 | }
|
---|
5015 |
|
---|
5016 | /* static */
|
---|
5017 | DECLCALLBACK(bool) Medium::vdConfigAreKeysValid(void *pvUser,
|
---|
5018 | const char * /* pszzValid */)
|
---|
5019 | {
|
---|
5020 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
5021 | AssertReturn(that != NULL, false);
|
---|
5022 |
|
---|
5023 | /* we always return true since the only keys we have are those found in
|
---|
5024 | * VDBACKENDINFO */
|
---|
5025 | return true;
|
---|
5026 | }
|
---|
5027 |
|
---|
5028 | /* static */
|
---|
5029 | DECLCALLBACK(int) Medium::vdConfigQuerySize(void *pvUser,
|
---|
5030 | const char *pszName,
|
---|
5031 | size_t *pcbValue)
|
---|
5032 | {
|
---|
5033 | AssertReturn(VALID_PTR(pcbValue), VERR_INVALID_POINTER);
|
---|
5034 |
|
---|
5035 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
5036 | AssertReturn(that != NULL, VERR_GENERAL_FAILURE);
|
---|
5037 |
|
---|
5038 | settings::StringsMap::const_iterator it = that->m->mapProperties.find(Utf8Str(pszName));
|
---|
5039 | if (it == that->m->mapProperties.end())
|
---|
5040 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
5041 |
|
---|
5042 | /* we interpret null values as "no value" in Medium */
|
---|
5043 | if (it->second.isEmpty())
|
---|
5044 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
5045 |
|
---|
5046 | *pcbValue = it->second.length() + 1 /* include terminator */;
|
---|
5047 |
|
---|
5048 | return VINF_SUCCESS;
|
---|
5049 | }
|
---|
5050 |
|
---|
5051 | /* static */
|
---|
5052 | DECLCALLBACK(int) Medium::vdConfigQuery(void *pvUser,
|
---|
5053 | const char *pszName,
|
---|
5054 | char *pszValue,
|
---|
5055 | size_t cchValue)
|
---|
5056 | {
|
---|
5057 | AssertReturn(VALID_PTR(pszValue), VERR_INVALID_POINTER);
|
---|
5058 |
|
---|
5059 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
5060 | AssertReturn(that != NULL, VERR_GENERAL_FAILURE);
|
---|
5061 |
|
---|
5062 | settings::StringsMap::const_iterator it = that->m->mapProperties.find(Utf8Str(pszName));
|
---|
5063 | if (it == that->m->mapProperties.end())
|
---|
5064 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
5065 |
|
---|
5066 | /* we interpret null values as "no value" in Medium */
|
---|
5067 | if (it->second.isEmpty())
|
---|
5068 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
5069 |
|
---|
5070 | const Utf8Str &value = it->second;
|
---|
5071 | if (value.length() >= cchValue)
|
---|
5072 | return VERR_CFGM_NOT_ENOUGH_SPACE;
|
---|
5073 |
|
---|
5074 | memcpy(pszValue, value.c_str(), value.length() + 1);
|
---|
5075 |
|
---|
5076 | return VINF_SUCCESS;
|
---|
5077 | }
|
---|
5078 |
|
---|
5079 | DECLCALLBACK(int) Medium::vdTcpSocketCreate(uint32_t fFlags, PVDSOCKET pSock)
|
---|
5080 | {
|
---|
5081 | PVDSOCKETINT pSocketInt = NULL;
|
---|
5082 |
|
---|
5083 | if ((fFlags & VD_INTERFACETCPNET_CONNECT_EXTENDED_SELECT) != 0)
|
---|
5084 | return VERR_NOT_SUPPORTED;
|
---|
5085 |
|
---|
5086 | pSocketInt = (PVDSOCKETINT)RTMemAllocZ(sizeof(VDSOCKETINT));
|
---|
5087 | if (!pSocketInt)
|
---|
5088 | return VERR_NO_MEMORY;
|
---|
5089 |
|
---|
5090 | pSocketInt->hSocket = NIL_RTSOCKET;
|
---|
5091 | *pSock = pSocketInt;
|
---|
5092 | return VINF_SUCCESS;
|
---|
5093 | }
|
---|
5094 |
|
---|
5095 | DECLCALLBACK(int) Medium::vdTcpSocketDestroy(VDSOCKET Sock)
|
---|
5096 | {
|
---|
5097 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
5098 |
|
---|
5099 | if (pSocketInt->hSocket != NIL_RTSOCKET)
|
---|
5100 | RTTcpClientClose(pSocketInt->hSocket);
|
---|
5101 |
|
---|
5102 | RTMemFree(pSocketInt);
|
---|
5103 |
|
---|
5104 | return VINF_SUCCESS;
|
---|
5105 | }
|
---|
5106 |
|
---|
5107 | DECLCALLBACK(int) Medium::vdTcpClientConnect(VDSOCKET Sock, const char *pszAddress, uint32_t uPort)
|
---|
5108 | {
|
---|
5109 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
5110 |
|
---|
5111 | return RTTcpClientConnect(pszAddress, uPort, &pSocketInt->hSocket);
|
---|
5112 | }
|
---|
5113 |
|
---|
5114 | DECLCALLBACK(int) Medium::vdTcpClientClose(VDSOCKET Sock)
|
---|
5115 | {
|
---|
5116 | int rc = VINF_SUCCESS;
|
---|
5117 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
5118 |
|
---|
5119 | rc = RTTcpClientClose(pSocketInt->hSocket);
|
---|
5120 | pSocketInt->hSocket = NIL_RTSOCKET;
|
---|
5121 | return rc;
|
---|
5122 | }
|
---|
5123 |
|
---|
5124 | DECLCALLBACK(bool) Medium::vdTcpIsClientConnected(VDSOCKET Sock)
|
---|
5125 | {
|
---|
5126 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
5127 | return pSocketInt->hSocket != NIL_RTSOCKET;
|
---|
5128 | }
|
---|
5129 |
|
---|
5130 | DECLCALLBACK(int) Medium::vdTcpSelectOne(VDSOCKET Sock, RTMSINTERVAL cMillies)
|
---|
5131 | {
|
---|
5132 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
5133 | return RTTcpSelectOne(pSocketInt->hSocket, cMillies);
|
---|
5134 | }
|
---|
5135 |
|
---|
5136 | DECLCALLBACK(int) Medium::vdTcpRead(VDSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead)
|
---|
5137 | {
|
---|
5138 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
5139 | return RTTcpRead(pSocketInt->hSocket, pvBuffer, cbBuffer, pcbRead);
|
---|
5140 | }
|
---|
5141 |
|
---|
5142 | DECLCALLBACK(int) Medium::vdTcpWrite(VDSOCKET Sock, const void *pvBuffer, size_t cbBuffer)
|
---|
5143 | {
|
---|
5144 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
5145 | return RTTcpWrite(pSocketInt->hSocket, pvBuffer, cbBuffer);
|
---|
5146 | }
|
---|
5147 |
|
---|
5148 | DECLCALLBACK(int) Medium::vdTcpSgWrite(VDSOCKET Sock, PCRTSGBUF pSgBuf)
|
---|
5149 | {
|
---|
5150 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
5151 | return RTTcpSgWrite(pSocketInt->hSocket, pSgBuf);
|
---|
5152 | }
|
---|
5153 |
|
---|
5154 | DECLCALLBACK(int) Medium::vdTcpFlush(VDSOCKET Sock)
|
---|
5155 | {
|
---|
5156 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
5157 | return RTTcpFlush(pSocketInt->hSocket);
|
---|
5158 | }
|
---|
5159 |
|
---|
5160 | DECLCALLBACK(int) Medium::vdTcpSetSendCoalescing(VDSOCKET Sock, bool fEnable)
|
---|
5161 | {
|
---|
5162 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
5163 | return RTTcpSetSendCoalescing(pSocketInt->hSocket, fEnable);
|
---|
5164 | }
|
---|
5165 |
|
---|
5166 | DECLCALLBACK(int) Medium::vdTcpGetLocalAddress(VDSOCKET Sock, PRTNETADDR pAddr)
|
---|
5167 | {
|
---|
5168 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
5169 | return RTTcpGetLocalAddress(pSocketInt->hSocket, pAddr);
|
---|
5170 | }
|
---|
5171 |
|
---|
5172 | DECLCALLBACK(int) Medium::vdTcpGetPeerAddress(VDSOCKET Sock, PRTNETADDR pAddr)
|
---|
5173 | {
|
---|
5174 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
5175 | return RTTcpGetPeerAddress(pSocketInt->hSocket, pAddr);
|
---|
5176 | }
|
---|
5177 |
|
---|
5178 |
|
---|
5179 | /**
|
---|
5180 | * Starts a new thread driven by the appropriate Medium::Task::handler() method.
|
---|
5181 | *
|
---|
5182 | * @note When the task is executed by this method, IProgress::notifyComplete()
|
---|
5183 | * is automatically called for the progress object associated with this
|
---|
5184 | * task when the task is finished to signal the operation completion for
|
---|
5185 | * other threads asynchronously waiting for it.
|
---|
5186 | */
|
---|
5187 | HRESULT Medium::startThread(Medium::Task *pTask)
|
---|
5188 | {
|
---|
5189 | #ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
|
---|
5190 | /* Extreme paranoia: The calling thread should not hold the medium
|
---|
5191 | * tree lock or any medium lock. Since there is no separate lock class
|
---|
5192 | * for medium objects be even more strict: no other object locks. */
|
---|
5193 | Assert(!AutoLockHoldsLocksInClass(LOCKCLASS_LISTOFMEDIA));
|
---|
5194 | Assert(!AutoLockHoldsLocksInClass(getLockingClass()));
|
---|
5195 | #endif
|
---|
5196 |
|
---|
5197 | /// @todo use a more descriptive task name
|
---|
5198 | int vrc = RTThreadCreate(NULL, Medium::Task::fntMediumTask, pTask,
|
---|
5199 | 0, RTTHREADTYPE_MAIN_HEAVY_WORKER, 0,
|
---|
5200 | "Medium::Task");
|
---|
5201 | if (RT_FAILURE(vrc))
|
---|
5202 | {
|
---|
5203 | delete pTask;
|
---|
5204 | return setError(E_FAIL, "Could not create Medium::Task thread (%Rrc)\n", vrc);
|
---|
5205 | }
|
---|
5206 |
|
---|
5207 | return S_OK;
|
---|
5208 | }
|
---|
5209 |
|
---|
5210 | /**
|
---|
5211 | * Fix the parent UUID of all children to point to this medium as their
|
---|
5212 | * parent.
|
---|
5213 | */
|
---|
5214 | HRESULT Medium::fixParentUuidOfChildren(const MediaList &childrenToReparent)
|
---|
5215 | {
|
---|
5216 | MediumLockList mediumLockList;
|
---|
5217 | HRESULT rc = createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5218 | false /* fMediumLockWrite */,
|
---|
5219 | this,
|
---|
5220 | mediumLockList);
|
---|
5221 | AssertComRCReturnRC(rc);
|
---|
5222 |
|
---|
5223 | try
|
---|
5224 | {
|
---|
5225 | PVBOXHDD hdd;
|
---|
5226 | int vrc = VDCreate(m->vdDiskIfaces, &hdd);
|
---|
5227 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
5228 |
|
---|
5229 | try
|
---|
5230 | {
|
---|
5231 | MediumLockList::Base::iterator lockListBegin =
|
---|
5232 | mediumLockList.GetBegin();
|
---|
5233 | MediumLockList::Base::iterator lockListEnd =
|
---|
5234 | mediumLockList.GetEnd();
|
---|
5235 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
5236 | it != lockListEnd;
|
---|
5237 | ++it)
|
---|
5238 | {
|
---|
5239 | MediumLock &mediumLock = *it;
|
---|
5240 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
5241 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
5242 |
|
---|
5243 | // open the medium
|
---|
5244 | vrc = VDOpen(hdd,
|
---|
5245 | pMedium->m->strFormat.c_str(),
|
---|
5246 | pMedium->m->strLocationFull.c_str(),
|
---|
5247 | VD_OPEN_FLAGS_READONLY,
|
---|
5248 | pMedium->m->vdDiskIfaces);
|
---|
5249 | if (RT_FAILURE(vrc))
|
---|
5250 | throw vrc;
|
---|
5251 | }
|
---|
5252 |
|
---|
5253 | for (MediaList::const_iterator it = childrenToReparent.begin();
|
---|
5254 | it != childrenToReparent.end();
|
---|
5255 | ++it)
|
---|
5256 | {
|
---|
5257 | /* VD_OPEN_FLAGS_INFO since UUID is wrong yet */
|
---|
5258 | vrc = VDOpen(hdd,
|
---|
5259 | (*it)->m->strFormat.c_str(),
|
---|
5260 | (*it)->m->strLocationFull.c_str(),
|
---|
5261 | VD_OPEN_FLAGS_INFO,
|
---|
5262 | (*it)->m->vdDiskIfaces);
|
---|
5263 | if (RT_FAILURE(vrc))
|
---|
5264 | throw vrc;
|
---|
5265 |
|
---|
5266 | vrc = VDSetParentUuid(hdd, VD_LAST_IMAGE, m->id);
|
---|
5267 | if (RT_FAILURE(vrc))
|
---|
5268 | throw vrc;
|
---|
5269 |
|
---|
5270 | vrc = VDClose(hdd, false /* fDelete */);
|
---|
5271 | if (RT_FAILURE(vrc))
|
---|
5272 | throw vrc;
|
---|
5273 |
|
---|
5274 | (*it)->UnlockWrite(NULL);
|
---|
5275 | }
|
---|
5276 | }
|
---|
5277 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5278 | catch (int aVRC)
|
---|
5279 | {
|
---|
5280 | throw setError(E_FAIL,
|
---|
5281 | tr("Could not update medium UUID references to parent '%s' (%s)"),
|
---|
5282 | m->strLocationFull.c_str(),
|
---|
5283 | vdError(aVRC).c_str());
|
---|
5284 | }
|
---|
5285 |
|
---|
5286 | VDDestroy(hdd);
|
---|
5287 | }
|
---|
5288 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5289 |
|
---|
5290 | return rc;
|
---|
5291 | }
|
---|
5292 |
|
---|
5293 | /**
|
---|
5294 | * Runs Medium::Task::handler() on the current thread instead of creating
|
---|
5295 | * a new one.
|
---|
5296 | *
|
---|
5297 | * This call implies that it is made on another temporary thread created for
|
---|
5298 | * some asynchronous task. Avoid calling it from a normal thread since the task
|
---|
5299 | * operations are potentially lengthy and will block the calling thread in this
|
---|
5300 | * case.
|
---|
5301 | *
|
---|
5302 | * @note When the task is executed by this method, IProgress::notifyComplete()
|
---|
5303 | * is not called for the progress object associated with this task when
|
---|
5304 | * the task is finished. Instead, the result of the operation is returned
|
---|
5305 | * by this method directly and it's the caller's responsibility to
|
---|
5306 | * complete the progress object in this case.
|
---|
5307 | */
|
---|
5308 | HRESULT Medium::runNow(Medium::Task *pTask,
|
---|
5309 | bool *pfNeedsSaveSettings)
|
---|
5310 | {
|
---|
5311 | #ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
|
---|
5312 | /* Extreme paranoia: The calling thread should not hold the medium
|
---|
5313 | * tree lock or any medium lock. Since there is no separate lock class
|
---|
5314 | * for medium objects be even more strict: no other object locks. */
|
---|
5315 | Assert(!AutoLockHoldsLocksInClass(LOCKCLASS_LISTOFMEDIA));
|
---|
5316 | Assert(!AutoLockHoldsLocksInClass(getLockingClass()));
|
---|
5317 | #endif
|
---|
5318 |
|
---|
5319 | pTask->m_pfNeedsSaveSettings = pfNeedsSaveSettings;
|
---|
5320 |
|
---|
5321 | /* NIL_RTTHREAD indicates synchronous call. */
|
---|
5322 | return (HRESULT)Medium::Task::fntMediumTask(NIL_RTTHREAD, pTask);
|
---|
5323 | }
|
---|
5324 |
|
---|
5325 | /**
|
---|
5326 | * Implementation code for the "create base" task.
|
---|
5327 | *
|
---|
5328 | * This only gets started from Medium::CreateBaseStorage() and always runs
|
---|
5329 | * asynchronously. As a result, we always save the VirtualBox.xml file when
|
---|
5330 | * we're done here.
|
---|
5331 | *
|
---|
5332 | * @param task
|
---|
5333 | * @return
|
---|
5334 | */
|
---|
5335 | HRESULT Medium::taskCreateBaseHandler(Medium::CreateBaseTask &task)
|
---|
5336 | {
|
---|
5337 | HRESULT rc = S_OK;
|
---|
5338 |
|
---|
5339 | /* these parameters we need after creation */
|
---|
5340 | uint64_t size = 0, logicalSize = 0;
|
---|
5341 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
5342 | bool fGenerateUuid = false;
|
---|
5343 |
|
---|
5344 | try
|
---|
5345 | {
|
---|
5346 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5347 |
|
---|
5348 | /* The object may request a specific UUID (through a special form of
|
---|
5349 | * the setLocation() argument). Otherwise we have to generate it */
|
---|
5350 | Guid id = m->id;
|
---|
5351 | fGenerateUuid = id.isEmpty();
|
---|
5352 | if (fGenerateUuid)
|
---|
5353 | {
|
---|
5354 | id.create();
|
---|
5355 | /* VirtualBox::registerHardDisk() will need UUID */
|
---|
5356 | unconst(m->id) = id;
|
---|
5357 | }
|
---|
5358 |
|
---|
5359 | Utf8Str format(m->strFormat);
|
---|
5360 | Utf8Str location(m->strLocationFull);
|
---|
5361 | uint64_t capabilities = m->formatObj->getCapabilities();
|
---|
5362 | ComAssertThrow(capabilities & ( VD_CAP_CREATE_FIXED
|
---|
5363 | | VD_CAP_CREATE_DYNAMIC), E_FAIL);
|
---|
5364 | Assert(m->state == MediumState_Creating);
|
---|
5365 |
|
---|
5366 | PVBOXHDD hdd;
|
---|
5367 | int vrc = VDCreate(m->vdDiskIfaces, &hdd);
|
---|
5368 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
5369 |
|
---|
5370 | /* unlock before the potentially lengthy operation */
|
---|
5371 | thisLock.release();
|
---|
5372 |
|
---|
5373 | try
|
---|
5374 | {
|
---|
5375 | /* ensure the directory exists */
|
---|
5376 | rc = VirtualBox::ensureFilePathExists(location);
|
---|
5377 | if (FAILED(rc))
|
---|
5378 | throw rc;
|
---|
5379 |
|
---|
5380 | PDMMEDIAGEOMETRY geo = { 0, 0, 0 }; /* auto-detect */
|
---|
5381 |
|
---|
5382 | vrc = VDCreateBase(hdd,
|
---|
5383 | format.c_str(),
|
---|
5384 | location.c_str(),
|
---|
5385 | task.mSize * _1M,
|
---|
5386 | task.mVariant,
|
---|
5387 | NULL,
|
---|
5388 | &geo,
|
---|
5389 | &geo,
|
---|
5390 | id.raw(),
|
---|
5391 | VD_OPEN_FLAGS_NORMAL,
|
---|
5392 | NULL,
|
---|
5393 | task.mVDOperationIfaces);
|
---|
5394 | if (RT_FAILURE(vrc))
|
---|
5395 | throw setError(VBOX_E_FILE_ERROR,
|
---|
5396 | tr("Could not create the medium storage unit '%s'%s"),
|
---|
5397 | location.c_str(), vdError(vrc).c_str());
|
---|
5398 |
|
---|
5399 | size = VDGetFileSize(hdd, 0);
|
---|
5400 | logicalSize = VDGetSize(hdd, 0) / _1M;
|
---|
5401 | unsigned uImageFlags;
|
---|
5402 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
5403 | if (RT_SUCCESS(vrc))
|
---|
5404 | variant = (MediumVariant_T)uImageFlags;
|
---|
5405 | }
|
---|
5406 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5407 |
|
---|
5408 | VDDestroy(hdd);
|
---|
5409 | }
|
---|
5410 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5411 |
|
---|
5412 | if (SUCCEEDED(rc))
|
---|
5413 | {
|
---|
5414 | /* register with mVirtualBox as the last step and move to
|
---|
5415 | * Created state only on success (leaving an orphan file is
|
---|
5416 | * better than breaking media registry consistency) */
|
---|
5417 | bool fNeedsSaveSettings = false;
|
---|
5418 | AutoWriteLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5419 | rc = m->pVirtualBox->registerHardDisk(this, &fNeedsSaveSettings);
|
---|
5420 | treeLock.release();
|
---|
5421 |
|
---|
5422 | if (fNeedsSaveSettings)
|
---|
5423 | {
|
---|
5424 | AutoWriteLock vboxlock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
5425 | m->pVirtualBox->saveSettings();
|
---|
5426 | }
|
---|
5427 | }
|
---|
5428 |
|
---|
5429 | // reenter the lock before changing state
|
---|
5430 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5431 |
|
---|
5432 | if (SUCCEEDED(rc))
|
---|
5433 | {
|
---|
5434 | m->state = MediumState_Created;
|
---|
5435 |
|
---|
5436 | m->size = size;
|
---|
5437 | m->logicalSize = logicalSize;
|
---|
5438 | m->variant = variant;
|
---|
5439 | }
|
---|
5440 | else
|
---|
5441 | {
|
---|
5442 | /* back to NotCreated on failure */
|
---|
5443 | m->state = MediumState_NotCreated;
|
---|
5444 |
|
---|
5445 | /* reset UUID to prevent it from being reused next time */
|
---|
5446 | if (fGenerateUuid)
|
---|
5447 | unconst(m->id).clear();
|
---|
5448 | }
|
---|
5449 |
|
---|
5450 | return rc;
|
---|
5451 | }
|
---|
5452 |
|
---|
5453 | /**
|
---|
5454 | * Implementation code for the "create diff" task.
|
---|
5455 | *
|
---|
5456 | * This task always gets started from Medium::createDiffStorage() and can run
|
---|
5457 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
5458 | * that function. If we run synchronously, the caller expects the bool
|
---|
5459 | * *pfNeedsSaveSettings to be set before returning; otherwise (in asynchronous
|
---|
5460 | * mode), we save the settings ourselves.
|
---|
5461 | *
|
---|
5462 | * @param task
|
---|
5463 | * @return
|
---|
5464 | */
|
---|
5465 | HRESULT Medium::taskCreateDiffHandler(Medium::CreateDiffTask &task)
|
---|
5466 | {
|
---|
5467 | HRESULT rc = S_OK;
|
---|
5468 |
|
---|
5469 | bool fNeedsSaveSettings = false;
|
---|
5470 |
|
---|
5471 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
5472 |
|
---|
5473 | uint64_t size = 0, logicalSize = 0;
|
---|
5474 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
5475 | bool fGenerateUuid = false;
|
---|
5476 |
|
---|
5477 | try
|
---|
5478 | {
|
---|
5479 | /* Lock both in {parent,child} order. */
|
---|
5480 | AutoMultiWriteLock2 mediaLock(this, pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5481 |
|
---|
5482 | /* The object may request a specific UUID (through a special form of
|
---|
5483 | * the setLocation() argument). Otherwise we have to generate it */
|
---|
5484 | Guid targetId = pTarget->m->id;
|
---|
5485 | fGenerateUuid = targetId.isEmpty();
|
---|
5486 | if (fGenerateUuid)
|
---|
5487 | {
|
---|
5488 | targetId.create();
|
---|
5489 | /* VirtualBox::registerHardDisk() will need UUID */
|
---|
5490 | unconst(pTarget->m->id) = targetId;
|
---|
5491 | }
|
---|
5492 |
|
---|
5493 | Guid id = m->id;
|
---|
5494 |
|
---|
5495 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
5496 | Utf8Str targetLocation(pTarget->m->strLocationFull);
|
---|
5497 | uint64_t capabilities = m->formatObj->getCapabilities();
|
---|
5498 | ComAssertThrow(capabilities & VD_CAP_CREATE_DYNAMIC, E_FAIL);
|
---|
5499 |
|
---|
5500 | Assert(pTarget->m->state == MediumState_Creating);
|
---|
5501 | Assert(m->state == MediumState_LockedRead);
|
---|
5502 |
|
---|
5503 | PVBOXHDD hdd;
|
---|
5504 | int vrc = VDCreate(m->vdDiskIfaces, &hdd);
|
---|
5505 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
5506 |
|
---|
5507 | /* the two media are now protected by their non-default states;
|
---|
5508 | * unlock the media before the potentially lengthy operation */
|
---|
5509 | mediaLock.release();
|
---|
5510 |
|
---|
5511 | try
|
---|
5512 | {
|
---|
5513 | /* Open all media in the target chain but the last. */
|
---|
5514 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
5515 | task.mpMediumLockList->GetBegin();
|
---|
5516 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
5517 | task.mpMediumLockList->GetEnd();
|
---|
5518 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
5519 | it != targetListEnd;
|
---|
5520 | ++it)
|
---|
5521 | {
|
---|
5522 | const MediumLock &mediumLock = *it;
|
---|
5523 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
5524 |
|
---|
5525 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
5526 |
|
---|
5527 | /* Skip over the target diff medium */
|
---|
5528 | if (pMedium->m->state == MediumState_Creating)
|
---|
5529 | continue;
|
---|
5530 |
|
---|
5531 | /* sanity check */
|
---|
5532 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
5533 |
|
---|
5534 | /* Open all media in appropriate mode. */
|
---|
5535 | vrc = VDOpen(hdd,
|
---|
5536 | pMedium->m->strFormat.c_str(),
|
---|
5537 | pMedium->m->strLocationFull.c_str(),
|
---|
5538 | VD_OPEN_FLAGS_READONLY,
|
---|
5539 | pMedium->m->vdDiskIfaces);
|
---|
5540 | if (RT_FAILURE(vrc))
|
---|
5541 | throw setError(VBOX_E_FILE_ERROR,
|
---|
5542 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
5543 | pMedium->m->strLocationFull.c_str(),
|
---|
5544 | vdError(vrc).c_str());
|
---|
5545 | }
|
---|
5546 |
|
---|
5547 | /* ensure the target directory exists */
|
---|
5548 | rc = VirtualBox::ensureFilePathExists(targetLocation);
|
---|
5549 | if (FAILED(rc))
|
---|
5550 | throw rc;
|
---|
5551 |
|
---|
5552 | vrc = VDCreateDiff(hdd,
|
---|
5553 | targetFormat.c_str(),
|
---|
5554 | targetLocation.c_str(),
|
---|
5555 | task.mVariant | VD_IMAGE_FLAGS_DIFF,
|
---|
5556 | NULL,
|
---|
5557 | targetId.raw(),
|
---|
5558 | id.raw(),
|
---|
5559 | VD_OPEN_FLAGS_NORMAL,
|
---|
5560 | pTarget->m->vdDiskIfaces,
|
---|
5561 | task.mVDOperationIfaces);
|
---|
5562 | if (RT_FAILURE(vrc))
|
---|
5563 | throw setError(VBOX_E_FILE_ERROR,
|
---|
5564 | tr("Could not create the differencing medium storage unit '%s'%s"),
|
---|
5565 | targetLocation.c_str(), vdError(vrc).c_str());
|
---|
5566 |
|
---|
5567 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
5568 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE) / _1M;
|
---|
5569 | unsigned uImageFlags;
|
---|
5570 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
5571 | if (RT_SUCCESS(vrc))
|
---|
5572 | variant = (MediumVariant_T)uImageFlags;
|
---|
5573 | }
|
---|
5574 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5575 |
|
---|
5576 | VDDestroy(hdd);
|
---|
5577 | }
|
---|
5578 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5579 |
|
---|
5580 | if (SUCCEEDED(rc))
|
---|
5581 | {
|
---|
5582 | AutoWriteLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5583 |
|
---|
5584 | Assert(pTarget->m->pParent.isNull());
|
---|
5585 |
|
---|
5586 | /* associate the child with the parent */
|
---|
5587 | pTarget->m->pParent = this;
|
---|
5588 | m->llChildren.push_back(pTarget);
|
---|
5589 |
|
---|
5590 | /** @todo r=klaus neither target nor base() are locked,
|
---|
5591 | * potential race! */
|
---|
5592 | /* diffs for immutable media are auto-reset by default */
|
---|
5593 | pTarget->m->autoReset = (getBase()->m->type == MediumType_Immutable);
|
---|
5594 |
|
---|
5595 | /* register with mVirtualBox as the last step and move to
|
---|
5596 | * Created state only on success (leaving an orphan file is
|
---|
5597 | * better than breaking media registry consistency) */
|
---|
5598 | rc = m->pVirtualBox->registerHardDisk(pTarget, &fNeedsSaveSettings);
|
---|
5599 |
|
---|
5600 | if (FAILED(rc))
|
---|
5601 | /* break the parent association on failure to register */
|
---|
5602 | deparent();
|
---|
5603 | }
|
---|
5604 |
|
---|
5605 | AutoMultiWriteLock2 mediaLock(this, pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5606 |
|
---|
5607 | if (SUCCEEDED(rc))
|
---|
5608 | {
|
---|
5609 | pTarget->m->state = MediumState_Created;
|
---|
5610 |
|
---|
5611 | pTarget->m->size = size;
|
---|
5612 | pTarget->m->logicalSize = logicalSize;
|
---|
5613 | pTarget->m->variant = variant;
|
---|
5614 | }
|
---|
5615 | else
|
---|
5616 | {
|
---|
5617 | /* back to NotCreated on failure */
|
---|
5618 | pTarget->m->state = MediumState_NotCreated;
|
---|
5619 |
|
---|
5620 | pTarget->m->autoReset = false;
|
---|
5621 |
|
---|
5622 | /* reset UUID to prevent it from being reused next time */
|
---|
5623 | if (fGenerateUuid)
|
---|
5624 | unconst(pTarget->m->id).clear();
|
---|
5625 | }
|
---|
5626 |
|
---|
5627 | // deregister the task registered in createDiffStorage()
|
---|
5628 | Assert(m->numCreateDiffTasks != 0);
|
---|
5629 | --m->numCreateDiffTasks;
|
---|
5630 |
|
---|
5631 | if (task.isAsync())
|
---|
5632 | {
|
---|
5633 | if (fNeedsSaveSettings)
|
---|
5634 | {
|
---|
5635 | // save the global settings; for that we should hold only the VirtualBox lock
|
---|
5636 | mediaLock.release();
|
---|
5637 | AutoWriteLock vboxlock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
5638 | m->pVirtualBox->saveSettings();
|
---|
5639 | }
|
---|
5640 | }
|
---|
5641 | else
|
---|
5642 | // synchronous mode: report save settings result to caller
|
---|
5643 | if (task.m_pfNeedsSaveSettings)
|
---|
5644 | *task.m_pfNeedsSaveSettings = fNeedsSaveSettings;
|
---|
5645 |
|
---|
5646 | /* Note that in sync mode, it's the caller's responsibility to
|
---|
5647 | * unlock the medium. */
|
---|
5648 |
|
---|
5649 | return rc;
|
---|
5650 | }
|
---|
5651 |
|
---|
5652 | /**
|
---|
5653 | * Implementation code for the "merge" task.
|
---|
5654 | *
|
---|
5655 | * This task always gets started from Medium::mergeTo() and can run
|
---|
5656 | * synchronously or asynchrously depending on the "wait" parameter passed to
|
---|
5657 | * that function. If we run synchronously, the caller expects the bool
|
---|
5658 | * *pfNeedsSaveSettings to be set before returning; otherwise (in asynchronous
|
---|
5659 | * mode), we save the settings ourselves.
|
---|
5660 | *
|
---|
5661 | * @param task
|
---|
5662 | * @return
|
---|
5663 | */
|
---|
5664 | HRESULT Medium::taskMergeHandler(Medium::MergeTask &task)
|
---|
5665 | {
|
---|
5666 | HRESULT rc = S_OK;
|
---|
5667 |
|
---|
5668 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
5669 |
|
---|
5670 | try
|
---|
5671 | {
|
---|
5672 | PVBOXHDD hdd;
|
---|
5673 | int vrc = VDCreate(m->vdDiskIfaces, &hdd);
|
---|
5674 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
5675 |
|
---|
5676 | try
|
---|
5677 | {
|
---|
5678 | // Similar code appears in SessionMachine::onlineMergeMedium, so
|
---|
5679 | // if you make any changes below check whether they are applicable
|
---|
5680 | // in that context as well.
|
---|
5681 |
|
---|
5682 | unsigned uTargetIdx = VD_LAST_IMAGE;
|
---|
5683 | unsigned uSourceIdx = VD_LAST_IMAGE;
|
---|
5684 | /* Open all media in the chain. */
|
---|
5685 | MediumLockList::Base::iterator lockListBegin =
|
---|
5686 | task.mpMediumLockList->GetBegin();
|
---|
5687 | MediumLockList::Base::iterator lockListEnd =
|
---|
5688 | task.mpMediumLockList->GetEnd();
|
---|
5689 | unsigned i = 0;
|
---|
5690 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
5691 | it != lockListEnd;
|
---|
5692 | ++it)
|
---|
5693 | {
|
---|
5694 | MediumLock &mediumLock = *it;
|
---|
5695 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
5696 |
|
---|
5697 | if (pMedium == this)
|
---|
5698 | uSourceIdx = i;
|
---|
5699 | else if (pMedium == pTarget)
|
---|
5700 | uTargetIdx = i;
|
---|
5701 |
|
---|
5702 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
5703 |
|
---|
5704 | /*
|
---|
5705 | * complex sanity (sane complexity)
|
---|
5706 | *
|
---|
5707 | * The current medium must be in the Deleting (medium is merged)
|
---|
5708 | * or LockedRead (parent medium) state if it is not the target.
|
---|
5709 | * If it is the target it must be in the LockedWrite state.
|
---|
5710 | */
|
---|
5711 | Assert( ( pMedium != pTarget
|
---|
5712 | && ( pMedium->m->state == MediumState_Deleting
|
---|
5713 | || pMedium->m->state == MediumState_LockedRead))
|
---|
5714 | || ( pMedium == pTarget
|
---|
5715 | && pMedium->m->state == MediumState_LockedWrite));
|
---|
5716 |
|
---|
5717 | /*
|
---|
5718 | * Medium must be the target, in the LockedRead state
|
---|
5719 | * or Deleting state where it is not allowed to be attached
|
---|
5720 | * to a virtual machine.
|
---|
5721 | */
|
---|
5722 | Assert( pMedium == pTarget
|
---|
5723 | || pMedium->m->state == MediumState_LockedRead
|
---|
5724 | || ( pMedium->m->backRefs.size() == 0
|
---|
5725 | && pMedium->m->state == MediumState_Deleting));
|
---|
5726 | /* The source medium must be in Deleting state. */
|
---|
5727 | Assert( pMedium != this
|
---|
5728 | || pMedium->m->state == MediumState_Deleting);
|
---|
5729 |
|
---|
5730 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
5731 |
|
---|
5732 | if ( pMedium->m->state == MediumState_LockedRead
|
---|
5733 | || pMedium->m->state == MediumState_Deleting)
|
---|
5734 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
5735 | if (pMedium->m->type == MediumType_Shareable)
|
---|
5736 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
5737 |
|
---|
5738 | /* Open the medium */
|
---|
5739 | vrc = VDOpen(hdd,
|
---|
5740 | pMedium->m->strFormat.c_str(),
|
---|
5741 | pMedium->m->strLocationFull.c_str(),
|
---|
5742 | uOpenFlags,
|
---|
5743 | pMedium->m->vdDiskIfaces);
|
---|
5744 | if (RT_FAILURE(vrc))
|
---|
5745 | throw vrc;
|
---|
5746 |
|
---|
5747 | i++;
|
---|
5748 | }
|
---|
5749 |
|
---|
5750 | ComAssertThrow( uSourceIdx != VD_LAST_IMAGE
|
---|
5751 | && uTargetIdx != VD_LAST_IMAGE, E_FAIL);
|
---|
5752 |
|
---|
5753 | vrc = VDMerge(hdd, uSourceIdx, uTargetIdx,
|
---|
5754 | task.mVDOperationIfaces);
|
---|
5755 | if (RT_FAILURE(vrc))
|
---|
5756 | throw vrc;
|
---|
5757 |
|
---|
5758 | /* update parent UUIDs */
|
---|
5759 | if (!task.mfMergeForward)
|
---|
5760 | {
|
---|
5761 | /* we need to update UUIDs of all source's children
|
---|
5762 | * which cannot be part of the container at once so
|
---|
5763 | * add each one in there individually */
|
---|
5764 | if (task.mChildrenToReparent.size() > 0)
|
---|
5765 | {
|
---|
5766 | for (MediaList::const_iterator it = task.mChildrenToReparent.begin();
|
---|
5767 | it != task.mChildrenToReparent.end();
|
---|
5768 | ++it)
|
---|
5769 | {
|
---|
5770 | /* VD_OPEN_FLAGS_INFO since UUID is wrong yet */
|
---|
5771 | vrc = VDOpen(hdd,
|
---|
5772 | (*it)->m->strFormat.c_str(),
|
---|
5773 | (*it)->m->strLocationFull.c_str(),
|
---|
5774 | VD_OPEN_FLAGS_INFO,
|
---|
5775 | (*it)->m->vdDiskIfaces);
|
---|
5776 | if (RT_FAILURE(vrc))
|
---|
5777 | throw vrc;
|
---|
5778 |
|
---|
5779 | vrc = VDSetParentUuid(hdd, VD_LAST_IMAGE,
|
---|
5780 | pTarget->m->id);
|
---|
5781 | if (RT_FAILURE(vrc))
|
---|
5782 | throw vrc;
|
---|
5783 |
|
---|
5784 | vrc = VDClose(hdd, false /* fDelete */);
|
---|
5785 | if (RT_FAILURE(vrc))
|
---|
5786 | throw vrc;
|
---|
5787 |
|
---|
5788 | (*it)->UnlockWrite(NULL);
|
---|
5789 | }
|
---|
5790 | }
|
---|
5791 | }
|
---|
5792 | }
|
---|
5793 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5794 | catch (int aVRC)
|
---|
5795 | {
|
---|
5796 | throw setError(VBOX_E_FILE_ERROR,
|
---|
5797 | tr("Could not merge the medium '%s' to '%s'%s"),
|
---|
5798 | m->strLocationFull.c_str(),
|
---|
5799 | pTarget->m->strLocationFull.c_str(),
|
---|
5800 | vdError(aVRC).c_str());
|
---|
5801 | }
|
---|
5802 |
|
---|
5803 | VDDestroy(hdd);
|
---|
5804 | }
|
---|
5805 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5806 |
|
---|
5807 | HRESULT rc2;
|
---|
5808 |
|
---|
5809 | if (SUCCEEDED(rc))
|
---|
5810 | {
|
---|
5811 | /* all media but the target were successfully deleted by
|
---|
5812 | * VDMerge; reparent the last one and uninitialize deleted media. */
|
---|
5813 |
|
---|
5814 | AutoWriteLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5815 |
|
---|
5816 | if (task.mfMergeForward)
|
---|
5817 | {
|
---|
5818 | /* first, unregister the target since it may become a base
|
---|
5819 | * medium which needs re-registration */
|
---|
5820 | rc2 = m->pVirtualBox->unregisterHardDisk(pTarget, NULL /*&fNeedsSaveSettings*/);
|
---|
5821 | AssertComRC(rc2);
|
---|
5822 |
|
---|
5823 | /* then, reparent it and disconnect the deleted branch at
|
---|
5824 | * both ends (chain->parent() is source's parent) */
|
---|
5825 | pTarget->deparent();
|
---|
5826 | pTarget->m->pParent = task.mParentForTarget;
|
---|
5827 | if (pTarget->m->pParent)
|
---|
5828 | {
|
---|
5829 | pTarget->m->pParent->m->llChildren.push_back(pTarget);
|
---|
5830 | deparent();
|
---|
5831 | }
|
---|
5832 |
|
---|
5833 | /* then, register again */
|
---|
5834 | rc2 = m->pVirtualBox->registerHardDisk(pTarget, NULL /*&fNeedsSaveSettings*/);
|
---|
5835 | AssertComRC(rc2);
|
---|
5836 | }
|
---|
5837 | else
|
---|
5838 | {
|
---|
5839 | Assert(pTarget->getChildren().size() == 1);
|
---|
5840 | Medium *targetChild = pTarget->getChildren().front();
|
---|
5841 |
|
---|
5842 | /* disconnect the deleted branch at the elder end */
|
---|
5843 | targetChild->deparent();
|
---|
5844 |
|
---|
5845 | /* reparent source's children and disconnect the deleted
|
---|
5846 | * branch at the younger end */
|
---|
5847 | if (task.mChildrenToReparent.size() > 0)
|
---|
5848 | {
|
---|
5849 | /* obey {parent,child} lock order */
|
---|
5850 | AutoWriteLock sourceLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5851 |
|
---|
5852 | for (MediaList::const_iterator it = task.mChildrenToReparent.begin();
|
---|
5853 | it != task.mChildrenToReparent.end();
|
---|
5854 | it++)
|
---|
5855 | {
|
---|
5856 | Medium *pMedium = *it;
|
---|
5857 | AutoWriteLock childLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
5858 |
|
---|
5859 | pMedium->deparent(); // removes pMedium from source
|
---|
5860 | pMedium->setParent(pTarget);
|
---|
5861 | }
|
---|
5862 | }
|
---|
5863 | }
|
---|
5864 |
|
---|
5865 | /* unregister and uninitialize all media removed by the merge */
|
---|
5866 | MediumLockList::Base::iterator lockListBegin =
|
---|
5867 | task.mpMediumLockList->GetBegin();
|
---|
5868 | MediumLockList::Base::iterator lockListEnd =
|
---|
5869 | task.mpMediumLockList->GetEnd();
|
---|
5870 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
5871 | it != lockListEnd;
|
---|
5872 | )
|
---|
5873 | {
|
---|
5874 | MediumLock &mediumLock = *it;
|
---|
5875 | /* Create a real copy of the medium pointer, as the medium
|
---|
5876 | * lock deletion below would invalidate the referenced object. */
|
---|
5877 | const ComObjPtr<Medium> pMedium = mediumLock.GetMedium();
|
---|
5878 |
|
---|
5879 | /* The target and all media not merged (readonly) are skipped */
|
---|
5880 | if ( pMedium == pTarget
|
---|
5881 | || pMedium->m->state == MediumState_LockedRead)
|
---|
5882 | {
|
---|
5883 | ++it;
|
---|
5884 | continue;
|
---|
5885 | }
|
---|
5886 |
|
---|
5887 | rc2 = pMedium->m->pVirtualBox->unregisterHardDisk(pMedium,
|
---|
5888 | NULL /*pfNeedsSaveSettings*/);
|
---|
5889 | AssertComRC(rc2);
|
---|
5890 |
|
---|
5891 | /* now, uninitialize the deleted medium (note that
|
---|
5892 | * due to the Deleting state, uninit() will not touch
|
---|
5893 | * the parent-child relationship so we need to
|
---|
5894 | * uninitialize each disk individually) */
|
---|
5895 |
|
---|
5896 | /* note that the operation initiator medium (which is
|
---|
5897 | * normally also the source medium) is a special case
|
---|
5898 | * -- there is one more caller added by Task to it which
|
---|
5899 | * we must release. Also, if we are in sync mode, the
|
---|
5900 | * caller may still hold an AutoCaller instance for it
|
---|
5901 | * and therefore we cannot uninit() it (it's therefore
|
---|
5902 | * the caller's responsibility) */
|
---|
5903 | if (pMedium == this)
|
---|
5904 | {
|
---|
5905 | Assert(getChildren().size() == 0);
|
---|
5906 | Assert(m->backRefs.size() == 0);
|
---|
5907 | task.mMediumCaller.release();
|
---|
5908 | }
|
---|
5909 |
|
---|
5910 | /* Delete the medium lock list entry, which also releases the
|
---|
5911 | * caller added by MergeChain before uninit() and updates the
|
---|
5912 | * iterator to point to the right place. */
|
---|
5913 | rc2 = task.mpMediumLockList->RemoveByIterator(it);
|
---|
5914 | AssertComRC(rc2);
|
---|
5915 |
|
---|
5916 | if (task.isAsync() || pMedium != this)
|
---|
5917 | pMedium->uninit();
|
---|
5918 | }
|
---|
5919 | }
|
---|
5920 |
|
---|
5921 | if (task.isAsync())
|
---|
5922 | {
|
---|
5923 | // in asynchronous mode, save settings now
|
---|
5924 | // for that we should hold only the VirtualBox lock
|
---|
5925 | AutoWriteLock vboxlock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
5926 | m->pVirtualBox->saveSettings();
|
---|
5927 | }
|
---|
5928 | else
|
---|
5929 | // synchronous mode: report save settings result to caller
|
---|
5930 | if (task.m_pfNeedsSaveSettings)
|
---|
5931 | *task.m_pfNeedsSaveSettings = true;
|
---|
5932 |
|
---|
5933 | if (FAILED(rc))
|
---|
5934 | {
|
---|
5935 | /* Here we come if either VDMerge() failed (in which case we
|
---|
5936 | * assume that it tried to do everything to make a further
|
---|
5937 | * retry possible -- e.g. not deleted intermediate media
|
---|
5938 | * and so on) or VirtualBox::saveSettings() failed (where we
|
---|
5939 | * should have the original tree but with intermediate storage
|
---|
5940 | * units deleted by VDMerge()). We have to only restore states
|
---|
5941 | * (through the MergeChain dtor) unless we are run synchronously
|
---|
5942 | * in which case it's the responsibility of the caller as stated
|
---|
5943 | * in the mergeTo() docs. The latter also implies that we
|
---|
5944 | * don't own the merge chain, so release it in this case. */
|
---|
5945 | if (task.isAsync())
|
---|
5946 | {
|
---|
5947 | Assert(task.mChildrenToReparent.size() == 0);
|
---|
5948 | cancelMergeTo(task.mChildrenToReparent, task.mpMediumLockList);
|
---|
5949 | }
|
---|
5950 | }
|
---|
5951 |
|
---|
5952 | return rc;
|
---|
5953 | }
|
---|
5954 |
|
---|
5955 | /**
|
---|
5956 | * Implementation code for the "clone" task.
|
---|
5957 | *
|
---|
5958 | * This only gets started from Medium::CloneTo() and always runs asynchronously.
|
---|
5959 | * As a result, we always save the VirtualBox.xml file when we're done here.
|
---|
5960 | *
|
---|
5961 | * @param task
|
---|
5962 | * @return
|
---|
5963 | */
|
---|
5964 | HRESULT Medium::taskCloneHandler(Medium::CloneTask &task)
|
---|
5965 | {
|
---|
5966 | HRESULT rc = S_OK;
|
---|
5967 |
|
---|
5968 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
5969 | const ComObjPtr<Medium> &pParent = task.mParent;
|
---|
5970 |
|
---|
5971 | bool fCreatingTarget = false;
|
---|
5972 |
|
---|
5973 | uint64_t size = 0, logicalSize = 0;
|
---|
5974 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
5975 | bool fGenerateUuid = false;
|
---|
5976 |
|
---|
5977 | try
|
---|
5978 | {
|
---|
5979 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
5980 | * signal from the task initiator (which releases it only after
|
---|
5981 | * RTThreadCreate()) that we can start the job. */
|
---|
5982 | AutoMultiWriteLock3 thisLock(this, pTarget, pParent COMMA_LOCKVAL_SRC_POS);
|
---|
5983 |
|
---|
5984 | fCreatingTarget = pTarget->m->state == MediumState_Creating;
|
---|
5985 |
|
---|
5986 | /* The object may request a specific UUID (through a special form of
|
---|
5987 | * the setLocation() argument). Otherwise we have to generate it */
|
---|
5988 | Guid targetId = pTarget->m->id;
|
---|
5989 | fGenerateUuid = targetId.isEmpty();
|
---|
5990 | if (fGenerateUuid)
|
---|
5991 | {
|
---|
5992 | targetId.create();
|
---|
5993 | /* VirtualBox::registerHardDisk() will need UUID */
|
---|
5994 | unconst(pTarget->m->id) = targetId;
|
---|
5995 | }
|
---|
5996 |
|
---|
5997 | PVBOXHDD hdd;
|
---|
5998 | int vrc = VDCreate(m->vdDiskIfaces, &hdd);
|
---|
5999 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6000 |
|
---|
6001 | try
|
---|
6002 | {
|
---|
6003 | /* Open all media in the source chain. */
|
---|
6004 | MediumLockList::Base::const_iterator sourceListBegin =
|
---|
6005 | task.mpSourceMediumLockList->GetBegin();
|
---|
6006 | MediumLockList::Base::const_iterator sourceListEnd =
|
---|
6007 | task.mpSourceMediumLockList->GetEnd();
|
---|
6008 | for (MediumLockList::Base::const_iterator it = sourceListBegin;
|
---|
6009 | it != sourceListEnd;
|
---|
6010 | ++it)
|
---|
6011 | {
|
---|
6012 | const MediumLock &mediumLock = *it;
|
---|
6013 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
6014 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
6015 |
|
---|
6016 | /* sanity check */
|
---|
6017 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
6018 |
|
---|
6019 | /** Open all media in read-only mode. */
|
---|
6020 | vrc = VDOpen(hdd,
|
---|
6021 | pMedium->m->strFormat.c_str(),
|
---|
6022 | pMedium->m->strLocationFull.c_str(),
|
---|
6023 | VD_OPEN_FLAGS_READONLY,
|
---|
6024 | pMedium->m->vdDiskIfaces);
|
---|
6025 | if (RT_FAILURE(vrc))
|
---|
6026 | throw setError(VBOX_E_FILE_ERROR,
|
---|
6027 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
6028 | pMedium->m->strLocationFull.c_str(),
|
---|
6029 | vdError(vrc).c_str());
|
---|
6030 | }
|
---|
6031 |
|
---|
6032 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
6033 | Utf8Str targetLocation(pTarget->m->strLocationFull);
|
---|
6034 |
|
---|
6035 | Assert( pTarget->m->state == MediumState_Creating
|
---|
6036 | || pTarget->m->state == MediumState_LockedWrite);
|
---|
6037 | Assert(m->state == MediumState_LockedRead);
|
---|
6038 | Assert(pParent.isNull() || pParent->m->state == MediumState_LockedRead);
|
---|
6039 |
|
---|
6040 | /* unlock before the potentially lengthy operation */
|
---|
6041 | thisLock.release();
|
---|
6042 |
|
---|
6043 | /* ensure the target directory exists */
|
---|
6044 | rc = VirtualBox::ensureFilePathExists(targetLocation);
|
---|
6045 | if (FAILED(rc))
|
---|
6046 | throw rc;
|
---|
6047 |
|
---|
6048 | PVBOXHDD targetHdd;
|
---|
6049 | vrc = VDCreate(m->vdDiskIfaces, &targetHdd);
|
---|
6050 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6051 |
|
---|
6052 | try
|
---|
6053 | {
|
---|
6054 | /* Open all media in the target chain. */
|
---|
6055 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
6056 | task.mpTargetMediumLockList->GetBegin();
|
---|
6057 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
6058 | task.mpTargetMediumLockList->GetEnd();
|
---|
6059 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
6060 | it != targetListEnd;
|
---|
6061 | ++it)
|
---|
6062 | {
|
---|
6063 | const MediumLock &mediumLock = *it;
|
---|
6064 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
6065 |
|
---|
6066 | /* If the target medium is not created yet there's no
|
---|
6067 | * reason to open it. */
|
---|
6068 | if (pMedium == pTarget && fCreatingTarget)
|
---|
6069 | continue;
|
---|
6070 |
|
---|
6071 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
6072 |
|
---|
6073 | /* sanity check */
|
---|
6074 | Assert( pMedium->m->state == MediumState_LockedRead
|
---|
6075 | || pMedium->m->state == MediumState_LockedWrite);
|
---|
6076 |
|
---|
6077 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
6078 | if (pMedium->m->state != MediumState_LockedWrite)
|
---|
6079 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
6080 | if (pMedium->m->type == MediumType_Shareable)
|
---|
6081 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
6082 |
|
---|
6083 | /* Open all media in appropriate mode. */
|
---|
6084 | vrc = VDOpen(targetHdd,
|
---|
6085 | pMedium->m->strFormat.c_str(),
|
---|
6086 | pMedium->m->strLocationFull.c_str(),
|
---|
6087 | uOpenFlags,
|
---|
6088 | pMedium->m->vdDiskIfaces);
|
---|
6089 | if (RT_FAILURE(vrc))
|
---|
6090 | throw setError(VBOX_E_FILE_ERROR,
|
---|
6091 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
6092 | pMedium->m->strLocationFull.c_str(),
|
---|
6093 | vdError(vrc).c_str());
|
---|
6094 | }
|
---|
6095 |
|
---|
6096 | /** @todo r=klaus target isn't locked, race getting the state */
|
---|
6097 | vrc = VDCopy(hdd,
|
---|
6098 | VD_LAST_IMAGE,
|
---|
6099 | targetHdd,
|
---|
6100 | targetFormat.c_str(),
|
---|
6101 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
6102 | false,
|
---|
6103 | 0,
|
---|
6104 | task.mVariant,
|
---|
6105 | targetId.raw(),
|
---|
6106 | NULL,
|
---|
6107 | pTarget->m->vdDiskIfaces,
|
---|
6108 | task.mVDOperationIfaces);
|
---|
6109 | if (RT_FAILURE(vrc))
|
---|
6110 | throw setError(VBOX_E_FILE_ERROR,
|
---|
6111 | tr("Could not create the clone medium '%s'%s"),
|
---|
6112 | targetLocation.c_str(), vdError(vrc).c_str());
|
---|
6113 |
|
---|
6114 | size = VDGetFileSize(targetHdd, VD_LAST_IMAGE);
|
---|
6115 | logicalSize = VDGetSize(targetHdd, VD_LAST_IMAGE) / _1M;
|
---|
6116 | unsigned uImageFlags;
|
---|
6117 | vrc = VDGetImageFlags(targetHdd, 0, &uImageFlags);
|
---|
6118 | if (RT_SUCCESS(vrc))
|
---|
6119 | variant = (MediumVariant_T)uImageFlags;
|
---|
6120 | }
|
---|
6121 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6122 |
|
---|
6123 | VDDestroy(targetHdd);
|
---|
6124 | }
|
---|
6125 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6126 |
|
---|
6127 | VDDestroy(hdd);
|
---|
6128 | }
|
---|
6129 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6130 |
|
---|
6131 | /* Only do the parent changes for newly created media. */
|
---|
6132 | if (SUCCEEDED(rc) && fCreatingTarget)
|
---|
6133 | {
|
---|
6134 | /* we set mParent & children() */
|
---|
6135 | AutoWriteLock alock2(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
6136 |
|
---|
6137 | Assert(pTarget->m->pParent.isNull());
|
---|
6138 |
|
---|
6139 | if (pParent)
|
---|
6140 | {
|
---|
6141 | /* associate the clone with the parent and deassociate
|
---|
6142 | * from VirtualBox */
|
---|
6143 | pTarget->m->pParent = pParent;
|
---|
6144 | pParent->m->llChildren.push_back(pTarget);
|
---|
6145 |
|
---|
6146 | /* register with mVirtualBox as the last step and move to
|
---|
6147 | * Created state only on success (leaving an orphan file is
|
---|
6148 | * better than breaking media registry consistency) */
|
---|
6149 | rc = pParent->m->pVirtualBox->registerHardDisk(pTarget, NULL /* pfNeedsSaveSettings */);
|
---|
6150 |
|
---|
6151 | if (FAILED(rc))
|
---|
6152 | /* break parent association on failure to register */
|
---|
6153 | pTarget->deparent(); // removes target from parent
|
---|
6154 | }
|
---|
6155 | else
|
---|
6156 | {
|
---|
6157 | /* just register */
|
---|
6158 | rc = m->pVirtualBox->registerHardDisk(pTarget, NULL /* pfNeedsSaveSettings */);
|
---|
6159 | }
|
---|
6160 | }
|
---|
6161 |
|
---|
6162 | if (fCreatingTarget)
|
---|
6163 | {
|
---|
6164 | AutoWriteLock mLock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
6165 |
|
---|
6166 | if (SUCCEEDED(rc))
|
---|
6167 | {
|
---|
6168 | pTarget->m->state = MediumState_Created;
|
---|
6169 |
|
---|
6170 | pTarget->m->size = size;
|
---|
6171 | pTarget->m->logicalSize = logicalSize;
|
---|
6172 | pTarget->m->variant = variant;
|
---|
6173 | }
|
---|
6174 | else
|
---|
6175 | {
|
---|
6176 | /* back to NotCreated on failure */
|
---|
6177 | pTarget->m->state = MediumState_NotCreated;
|
---|
6178 |
|
---|
6179 | /* reset UUID to prevent it from being reused next time */
|
---|
6180 | if (fGenerateUuid)
|
---|
6181 | unconst(pTarget->m->id).clear();
|
---|
6182 | }
|
---|
6183 | }
|
---|
6184 |
|
---|
6185 | // now, at the end of this task (always asynchronous), save the settings
|
---|
6186 | {
|
---|
6187 | AutoWriteLock vboxlock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
6188 | m->pVirtualBox->saveSettings();
|
---|
6189 | }
|
---|
6190 |
|
---|
6191 | /* Everything is explicitly unlocked when the task exits,
|
---|
6192 | * as the task destruction also destroys the source chain. */
|
---|
6193 |
|
---|
6194 | /* Make sure the source chain is released early. It could happen
|
---|
6195 | * that we get a deadlock in Appliance::Import when Medium::Close
|
---|
6196 | * is called & the source chain is released at the same time. */
|
---|
6197 | task.mpSourceMediumLockList->Clear();
|
---|
6198 |
|
---|
6199 | return rc;
|
---|
6200 | }
|
---|
6201 |
|
---|
6202 | /**
|
---|
6203 | * Implementation code for the "delete" task.
|
---|
6204 | *
|
---|
6205 | * This task always gets started from Medium::deleteStorage() and can run
|
---|
6206 | * synchronously or asynchrously depending on the "wait" parameter passed to
|
---|
6207 | * that function.
|
---|
6208 | *
|
---|
6209 | * @param task
|
---|
6210 | * @return
|
---|
6211 | */
|
---|
6212 | HRESULT Medium::taskDeleteHandler(Medium::DeleteTask &task)
|
---|
6213 | {
|
---|
6214 | NOREF(task);
|
---|
6215 | HRESULT rc = S_OK;
|
---|
6216 |
|
---|
6217 | try
|
---|
6218 | {
|
---|
6219 | /* The lock is also used as a signal from the task initiator (which
|
---|
6220 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
6221 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6222 |
|
---|
6223 | PVBOXHDD hdd;
|
---|
6224 | int vrc = VDCreate(m->vdDiskIfaces, &hdd);
|
---|
6225 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6226 |
|
---|
6227 | Utf8Str format(m->strFormat);
|
---|
6228 | Utf8Str location(m->strLocationFull);
|
---|
6229 |
|
---|
6230 | /* unlock before the potentially lengthy operation */
|
---|
6231 | Assert(m->state == MediumState_Deleting);
|
---|
6232 | thisLock.release();
|
---|
6233 |
|
---|
6234 | try
|
---|
6235 | {
|
---|
6236 | vrc = VDOpen(hdd,
|
---|
6237 | format.c_str(),
|
---|
6238 | location.c_str(),
|
---|
6239 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO,
|
---|
6240 | m->vdDiskIfaces);
|
---|
6241 | if (RT_SUCCESS(vrc))
|
---|
6242 | vrc = VDClose(hdd, true /* fDelete */);
|
---|
6243 |
|
---|
6244 | if (RT_FAILURE(vrc))
|
---|
6245 | throw setError(VBOX_E_FILE_ERROR,
|
---|
6246 | tr("Could not delete the medium storage unit '%s'%s"),
|
---|
6247 | location.c_str(), vdError(vrc).c_str());
|
---|
6248 |
|
---|
6249 | }
|
---|
6250 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6251 |
|
---|
6252 | VDDestroy(hdd);
|
---|
6253 | }
|
---|
6254 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6255 |
|
---|
6256 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6257 |
|
---|
6258 | /* go to the NotCreated state even on failure since the storage
|
---|
6259 | * may have been already partially deleted and cannot be used any
|
---|
6260 | * more. One will be able to manually re-open the storage if really
|
---|
6261 | * needed to re-register it. */
|
---|
6262 | m->state = MediumState_NotCreated;
|
---|
6263 |
|
---|
6264 | /* Reset UUID to prevent Create* from reusing it again */
|
---|
6265 | unconst(m->id).clear();
|
---|
6266 |
|
---|
6267 | return rc;
|
---|
6268 | }
|
---|
6269 |
|
---|
6270 | /**
|
---|
6271 | * Implementation code for the "reset" task.
|
---|
6272 | *
|
---|
6273 | * This always gets started asynchronously from Medium::Reset().
|
---|
6274 | *
|
---|
6275 | * @param task
|
---|
6276 | * @return
|
---|
6277 | */
|
---|
6278 | HRESULT Medium::taskResetHandler(Medium::ResetTask &task)
|
---|
6279 | {
|
---|
6280 | HRESULT rc = S_OK;
|
---|
6281 |
|
---|
6282 | uint64_t size = 0, logicalSize = 0;
|
---|
6283 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
6284 |
|
---|
6285 | try
|
---|
6286 | {
|
---|
6287 | /* The lock is also used as a signal from the task initiator (which
|
---|
6288 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
6289 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6290 |
|
---|
6291 | /// @todo Below we use a pair of delete/create operations to reset
|
---|
6292 | /// the diff contents but the most efficient way will of course be
|
---|
6293 | /// to add a VDResetDiff() API call
|
---|
6294 |
|
---|
6295 | PVBOXHDD hdd;
|
---|
6296 | int vrc = VDCreate(m->vdDiskIfaces, &hdd);
|
---|
6297 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6298 |
|
---|
6299 | Guid id = m->id;
|
---|
6300 | Utf8Str format(m->strFormat);
|
---|
6301 | Utf8Str location(m->strLocationFull);
|
---|
6302 |
|
---|
6303 | Medium *pParent = m->pParent;
|
---|
6304 | Guid parentId = pParent->m->id;
|
---|
6305 | Utf8Str parentFormat(pParent->m->strFormat);
|
---|
6306 | Utf8Str parentLocation(pParent->m->strLocationFull);
|
---|
6307 |
|
---|
6308 | Assert(m->state == MediumState_LockedWrite);
|
---|
6309 |
|
---|
6310 | /* unlock before the potentially lengthy operation */
|
---|
6311 | thisLock.release();
|
---|
6312 |
|
---|
6313 | try
|
---|
6314 | {
|
---|
6315 | /* Open all media in the target chain but the last. */
|
---|
6316 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
6317 | task.mpMediumLockList->GetBegin();
|
---|
6318 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
6319 | task.mpMediumLockList->GetEnd();
|
---|
6320 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
6321 | it != targetListEnd;
|
---|
6322 | ++it)
|
---|
6323 | {
|
---|
6324 | const MediumLock &mediumLock = *it;
|
---|
6325 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
6326 |
|
---|
6327 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
6328 |
|
---|
6329 | /* sanity check, "this" is checked above */
|
---|
6330 | Assert( pMedium == this
|
---|
6331 | || pMedium->m->state == MediumState_LockedRead);
|
---|
6332 |
|
---|
6333 | /* Open all media in appropriate mode. */
|
---|
6334 | vrc = VDOpen(hdd,
|
---|
6335 | pMedium->m->strFormat.c_str(),
|
---|
6336 | pMedium->m->strLocationFull.c_str(),
|
---|
6337 | VD_OPEN_FLAGS_READONLY,
|
---|
6338 | pMedium->m->vdDiskIfaces);
|
---|
6339 | if (RT_FAILURE(vrc))
|
---|
6340 | throw setError(VBOX_E_FILE_ERROR,
|
---|
6341 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
6342 | pMedium->m->strLocationFull.c_str(),
|
---|
6343 | vdError(vrc).c_str());
|
---|
6344 |
|
---|
6345 | /* Done when we hit the media which should be reset */
|
---|
6346 | if (pMedium == this)
|
---|
6347 | break;
|
---|
6348 | }
|
---|
6349 |
|
---|
6350 | /* first, delete the storage unit */
|
---|
6351 | vrc = VDClose(hdd, true /* fDelete */);
|
---|
6352 | if (RT_FAILURE(vrc))
|
---|
6353 | throw setError(VBOX_E_FILE_ERROR,
|
---|
6354 | tr("Could not delete the medium storage unit '%s'%s"),
|
---|
6355 | location.c_str(), vdError(vrc).c_str());
|
---|
6356 |
|
---|
6357 | /* next, create it again */
|
---|
6358 | vrc = VDOpen(hdd,
|
---|
6359 | parentFormat.c_str(),
|
---|
6360 | parentLocation.c_str(),
|
---|
6361 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO,
|
---|
6362 | m->vdDiskIfaces);
|
---|
6363 | if (RT_FAILURE(vrc))
|
---|
6364 | throw setError(VBOX_E_FILE_ERROR,
|
---|
6365 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
6366 | parentLocation.c_str(), vdError(vrc).c_str());
|
---|
6367 |
|
---|
6368 | vrc = VDCreateDiff(hdd,
|
---|
6369 | format.c_str(),
|
---|
6370 | location.c_str(),
|
---|
6371 | /// @todo use the same medium variant as before
|
---|
6372 | VD_IMAGE_FLAGS_NONE,
|
---|
6373 | NULL,
|
---|
6374 | id.raw(),
|
---|
6375 | parentId.raw(),
|
---|
6376 | VD_OPEN_FLAGS_NORMAL,
|
---|
6377 | m->vdDiskIfaces,
|
---|
6378 | task.mVDOperationIfaces);
|
---|
6379 | if (RT_FAILURE(vrc))
|
---|
6380 | throw setError(VBOX_E_FILE_ERROR,
|
---|
6381 | tr("Could not create the differencing medium storage unit '%s'%s"),
|
---|
6382 | location.c_str(), vdError(vrc).c_str());
|
---|
6383 |
|
---|
6384 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
6385 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE) / _1M;
|
---|
6386 | unsigned uImageFlags;
|
---|
6387 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
6388 | if (RT_SUCCESS(vrc))
|
---|
6389 | variant = (MediumVariant_T)uImageFlags;
|
---|
6390 | }
|
---|
6391 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6392 |
|
---|
6393 | VDDestroy(hdd);
|
---|
6394 | }
|
---|
6395 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6396 |
|
---|
6397 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6398 |
|
---|
6399 | m->size = size;
|
---|
6400 | m->logicalSize = logicalSize;
|
---|
6401 | m->variant = variant;
|
---|
6402 |
|
---|
6403 | if (task.isAsync())
|
---|
6404 | {
|
---|
6405 | /* unlock ourselves when done */
|
---|
6406 | HRESULT rc2 = UnlockWrite(NULL);
|
---|
6407 | AssertComRC(rc2);
|
---|
6408 | }
|
---|
6409 |
|
---|
6410 | /* Note that in sync mode, it's the caller's responsibility to
|
---|
6411 | * unlock the medium. */
|
---|
6412 |
|
---|
6413 | return rc;
|
---|
6414 | }
|
---|
6415 |
|
---|
6416 | /**
|
---|
6417 | * Implementation code for the "compact" task.
|
---|
6418 | *
|
---|
6419 | * @param task
|
---|
6420 | * @return
|
---|
6421 | */
|
---|
6422 | HRESULT Medium::taskCompactHandler(Medium::CompactTask &task)
|
---|
6423 | {
|
---|
6424 | HRESULT rc = S_OK;
|
---|
6425 |
|
---|
6426 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
6427 | * signal from the task initiator (which releases it only after
|
---|
6428 | * RTThreadCreate()) that we can start the job. */
|
---|
6429 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6430 |
|
---|
6431 | try
|
---|
6432 | {
|
---|
6433 | PVBOXHDD hdd;
|
---|
6434 | int vrc = VDCreate(m->vdDiskIfaces, &hdd);
|
---|
6435 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6436 |
|
---|
6437 | try
|
---|
6438 | {
|
---|
6439 | /* Open all media in the chain. */
|
---|
6440 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
6441 | task.mpMediumLockList->GetBegin();
|
---|
6442 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
6443 | task.mpMediumLockList->GetEnd();
|
---|
6444 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
6445 | mediumListEnd;
|
---|
6446 | mediumListLast--;
|
---|
6447 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
6448 | it != mediumListEnd;
|
---|
6449 | ++it)
|
---|
6450 | {
|
---|
6451 | const MediumLock &mediumLock = *it;
|
---|
6452 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
6453 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
6454 |
|
---|
6455 | /* sanity check */
|
---|
6456 | if (it == mediumListLast)
|
---|
6457 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
6458 | else
|
---|
6459 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
6460 |
|
---|
6461 | /* Open all media but last in read-only mode. Do not handle
|
---|
6462 | * shareable media, as compaction and sharing are mutually
|
---|
6463 | * exclusive. */
|
---|
6464 | vrc = VDOpen(hdd,
|
---|
6465 | pMedium->m->strFormat.c_str(),
|
---|
6466 | pMedium->m->strLocationFull.c_str(),
|
---|
6467 | (it == mediumListLast) ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY,
|
---|
6468 | pMedium->m->vdDiskIfaces);
|
---|
6469 | if (RT_FAILURE(vrc))
|
---|
6470 | throw setError(VBOX_E_FILE_ERROR,
|
---|
6471 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
6472 | pMedium->m->strLocationFull.c_str(),
|
---|
6473 | vdError(vrc).c_str());
|
---|
6474 | }
|
---|
6475 |
|
---|
6476 | Assert(m->state == MediumState_LockedWrite);
|
---|
6477 |
|
---|
6478 | Utf8Str location(m->strLocationFull);
|
---|
6479 |
|
---|
6480 | /* unlock before the potentially lengthy operation */
|
---|
6481 | thisLock.release();
|
---|
6482 |
|
---|
6483 | vrc = VDCompact(hdd, VD_LAST_IMAGE, task.mVDOperationIfaces);
|
---|
6484 | if (RT_FAILURE(vrc))
|
---|
6485 | {
|
---|
6486 | if (vrc == VERR_NOT_SUPPORTED)
|
---|
6487 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
6488 | tr("Compacting is not yet supported for medium '%s'"),
|
---|
6489 | location.c_str());
|
---|
6490 | else if (vrc == VERR_NOT_IMPLEMENTED)
|
---|
6491 | throw setError(E_NOTIMPL,
|
---|
6492 | tr("Compacting is not implemented, medium '%s'"),
|
---|
6493 | location.c_str());
|
---|
6494 | else
|
---|
6495 | throw setError(VBOX_E_FILE_ERROR,
|
---|
6496 | tr("Could not compact medium '%s'%s"),
|
---|
6497 | location.c_str(),
|
---|
6498 | vdError(vrc).c_str());
|
---|
6499 | }
|
---|
6500 | }
|
---|
6501 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6502 |
|
---|
6503 | VDDestroy(hdd);
|
---|
6504 | }
|
---|
6505 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6506 |
|
---|
6507 | /* Everything is explicitly unlocked when the task exits,
|
---|
6508 | * as the task destruction also destroys the media chain. */
|
---|
6509 |
|
---|
6510 | return rc;
|
---|
6511 | }
|
---|
6512 |
|
---|
6513 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|