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