1 | /* $Id: MediumImpl.cpp 43478 2012-10-01 09:05:19Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox COM class implementation
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2012 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 | #include <list>
|
---|
43 |
|
---|
44 | typedef std::list<Guid> GuidList;
|
---|
45 |
|
---|
46 | ////////////////////////////////////////////////////////////////////////////////
|
---|
47 | //
|
---|
48 | // Medium data definition
|
---|
49 | //
|
---|
50 | ////////////////////////////////////////////////////////////////////////////////
|
---|
51 |
|
---|
52 | /** Describes how a machine refers to this medium. */
|
---|
53 | struct BackRef
|
---|
54 | {
|
---|
55 | /** Equality predicate for stdc++. */
|
---|
56 | struct EqualsTo : public std::unary_function <BackRef, bool>
|
---|
57 | {
|
---|
58 | explicit EqualsTo(const Guid &aMachineId) : machineId(aMachineId) {}
|
---|
59 |
|
---|
60 | bool operator()(const argument_type &aThat) const
|
---|
61 | {
|
---|
62 | return aThat.machineId == machineId;
|
---|
63 | }
|
---|
64 |
|
---|
65 | const Guid machineId;
|
---|
66 | };
|
---|
67 |
|
---|
68 | BackRef(const Guid &aMachineId,
|
---|
69 | const Guid &aSnapshotId = Guid::Empty)
|
---|
70 | : machineId(aMachineId),
|
---|
71 | fInCurState(aSnapshotId.isEmpty())
|
---|
72 | {
|
---|
73 | if (!aSnapshotId.isEmpty())
|
---|
74 | llSnapshotIds.push_back(aSnapshotId);
|
---|
75 | }
|
---|
76 |
|
---|
77 | Guid machineId;
|
---|
78 | bool fInCurState : 1;
|
---|
79 | GuidList llSnapshotIds;
|
---|
80 | };
|
---|
81 |
|
---|
82 | typedef std::list<BackRef> BackRefList;
|
---|
83 |
|
---|
84 | struct Medium::Data
|
---|
85 | {
|
---|
86 | Data()
|
---|
87 | : pVirtualBox(NULL),
|
---|
88 | state(MediumState_NotCreated),
|
---|
89 | variant(MediumVariant_Standard),
|
---|
90 | size(0),
|
---|
91 | readers(0),
|
---|
92 | preLockState(MediumState_NotCreated),
|
---|
93 | queryInfoSem(LOCKCLASS_MEDIUMQUERY),
|
---|
94 | queryInfoRunning(false),
|
---|
95 | type(MediumType_Normal),
|
---|
96 | devType(DeviceType_HardDisk),
|
---|
97 | logicalSize(0),
|
---|
98 | hddOpenMode(OpenReadWrite),
|
---|
99 | autoReset(false),
|
---|
100 | hostDrive(false),
|
---|
101 | implicit(false),
|
---|
102 | uOpenFlagsDef(VD_OPEN_FLAGS_IGNORE_FLUSH),
|
---|
103 | numCreateDiffTasks(0),
|
---|
104 | vdDiskIfaces(NULL),
|
---|
105 | vdImageIfaces(NULL)
|
---|
106 | { }
|
---|
107 |
|
---|
108 | /** weak VirtualBox parent */
|
---|
109 | VirtualBox * const pVirtualBox;
|
---|
110 |
|
---|
111 | // pParent and llChildren are protected by VirtualBox::getMediaTreeLockHandle()
|
---|
112 | ComObjPtr<Medium> pParent;
|
---|
113 | MediaList llChildren; // to add a child, just call push_back; to remove a child, call child->deparent() which does a lookup
|
---|
114 |
|
---|
115 | GuidList llRegistryIDs; // media registries in which this medium is listed
|
---|
116 |
|
---|
117 | const Guid id;
|
---|
118 | Utf8Str strDescription;
|
---|
119 | MediumState_T state;
|
---|
120 | MediumVariant_T variant;
|
---|
121 | Utf8Str strLocationFull;
|
---|
122 | uint64_t size;
|
---|
123 | Utf8Str strLastAccessError;
|
---|
124 |
|
---|
125 | BackRefList backRefs;
|
---|
126 |
|
---|
127 | size_t readers;
|
---|
128 | MediumState_T preLockState;
|
---|
129 |
|
---|
130 | /** Special synchronization for operations which must wait for
|
---|
131 | * Medium::queryInfo in another thread to complete. Using a SemRW is
|
---|
132 | * not quite ideal, but at least it is subject to the lock validator,
|
---|
133 | * unlike the SemEventMulti which we had here for many years. Catching
|
---|
134 | * possible deadlocks is more important than a tiny bit of efficiency. */
|
---|
135 | RWLockHandle queryInfoSem;
|
---|
136 | bool queryInfoRunning : 1;
|
---|
137 |
|
---|
138 | const Utf8Str strFormat;
|
---|
139 | ComObjPtr<MediumFormat> formatObj;
|
---|
140 |
|
---|
141 | MediumType_T type;
|
---|
142 | DeviceType_T devType;
|
---|
143 | uint64_t logicalSize;
|
---|
144 |
|
---|
145 | HDDOpenMode hddOpenMode;
|
---|
146 |
|
---|
147 | bool autoReset : 1;
|
---|
148 |
|
---|
149 | /** New UUID to be set on the next Medium::queryInfo call. */
|
---|
150 | const Guid uuidImage;
|
---|
151 | /** New parent UUID to be set on the next Medium::queryInfo call. */
|
---|
152 | const Guid uuidParentImage;
|
---|
153 |
|
---|
154 | bool hostDrive : 1;
|
---|
155 |
|
---|
156 | settings::StringsMap mapProperties;
|
---|
157 |
|
---|
158 | bool implicit : 1;
|
---|
159 |
|
---|
160 | /** Default flags passed to VDOpen(). */
|
---|
161 | unsigned uOpenFlagsDef;
|
---|
162 |
|
---|
163 | uint32_t numCreateDiffTasks;
|
---|
164 |
|
---|
165 | Utf8Str vdError; /*< Error remembered by the VD error callback. */
|
---|
166 |
|
---|
167 | VDINTERFACEERROR vdIfError;
|
---|
168 |
|
---|
169 | VDINTERFACECONFIG vdIfConfig;
|
---|
170 |
|
---|
171 | VDINTERFACETCPNET vdIfTcpNet;
|
---|
172 |
|
---|
173 | PVDINTERFACE vdDiskIfaces;
|
---|
174 | PVDINTERFACE vdImageIfaces;
|
---|
175 | };
|
---|
176 |
|
---|
177 | typedef struct VDSOCKETINT
|
---|
178 | {
|
---|
179 | /** Socket handle. */
|
---|
180 | RTSOCKET hSocket;
|
---|
181 | } VDSOCKETINT, *PVDSOCKETINT;
|
---|
182 |
|
---|
183 | ////////////////////////////////////////////////////////////////////////////////
|
---|
184 | //
|
---|
185 | // Globals
|
---|
186 | //
|
---|
187 | ////////////////////////////////////////////////////////////////////////////////
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Medium::Task class for asynchronous operations.
|
---|
191 | *
|
---|
192 | * @note Instances of this class must be created using new() because the
|
---|
193 | * task thread function will delete them when the task is complete.
|
---|
194 | *
|
---|
195 | * @note The constructor of this class adds a caller on the managed Medium
|
---|
196 | * object which is automatically released upon destruction.
|
---|
197 | */
|
---|
198 | class Medium::Task
|
---|
199 | {
|
---|
200 | public:
|
---|
201 | Task(Medium *aMedium, Progress *aProgress)
|
---|
202 | : mVDOperationIfaces(NULL),
|
---|
203 | mMedium(aMedium),
|
---|
204 | mMediumCaller(aMedium),
|
---|
205 | mThread(NIL_RTTHREAD),
|
---|
206 | mProgress(aProgress),
|
---|
207 | mVirtualBoxCaller(NULL)
|
---|
208 | {
|
---|
209 | AssertReturnVoidStmt(aMedium, mRC = E_FAIL);
|
---|
210 | mRC = mMediumCaller.rc();
|
---|
211 | if (FAILED(mRC))
|
---|
212 | return;
|
---|
213 |
|
---|
214 | /* Get strong VirtualBox reference, see below. */
|
---|
215 | VirtualBox *pVirtualBox = aMedium->m->pVirtualBox;
|
---|
216 | mVirtualBox = pVirtualBox;
|
---|
217 | mVirtualBoxCaller.attach(pVirtualBox);
|
---|
218 | mRC = mVirtualBoxCaller.rc();
|
---|
219 | if (FAILED(mRC))
|
---|
220 | return;
|
---|
221 |
|
---|
222 | /* Set up a per-operation progress interface, can be used freely (for
|
---|
223 | * binary operations you can use it either on the source or target). */
|
---|
224 | mVDIfProgress.pfnProgress = vdProgressCall;
|
---|
225 | int vrc = VDInterfaceAdd(&mVDIfProgress.Core,
|
---|
226 | "Medium::Task::vdInterfaceProgress",
|
---|
227 | VDINTERFACETYPE_PROGRESS,
|
---|
228 | mProgress,
|
---|
229 | sizeof(VDINTERFACEPROGRESS),
|
---|
230 | &mVDOperationIfaces);
|
---|
231 | AssertRC(vrc);
|
---|
232 | if (RT_FAILURE(vrc))
|
---|
233 | mRC = E_FAIL;
|
---|
234 | }
|
---|
235 |
|
---|
236 | // Make all destructors virtual. Just in case.
|
---|
237 | virtual ~Task()
|
---|
238 | {}
|
---|
239 |
|
---|
240 | HRESULT rc() const { return mRC; }
|
---|
241 | bool isOk() const { return SUCCEEDED(rc()); }
|
---|
242 |
|
---|
243 | static int fntMediumTask(RTTHREAD aThread, void *pvUser);
|
---|
244 |
|
---|
245 | bool isAsync() { return mThread != NIL_RTTHREAD; }
|
---|
246 |
|
---|
247 | PVDINTERFACE mVDOperationIfaces;
|
---|
248 |
|
---|
249 | const ComObjPtr<Medium> mMedium;
|
---|
250 | AutoCaller mMediumCaller;
|
---|
251 |
|
---|
252 | friend HRESULT Medium::runNow(Medium::Task*);
|
---|
253 |
|
---|
254 | protected:
|
---|
255 | HRESULT mRC;
|
---|
256 | RTTHREAD mThread;
|
---|
257 |
|
---|
258 | private:
|
---|
259 | virtual HRESULT handler() = 0;
|
---|
260 |
|
---|
261 | const ComObjPtr<Progress> mProgress;
|
---|
262 |
|
---|
263 | static DECLCALLBACK(int) vdProgressCall(void *pvUser, unsigned uPercent);
|
---|
264 |
|
---|
265 | VDINTERFACEPROGRESS mVDIfProgress;
|
---|
266 |
|
---|
267 | /* Must have a strong VirtualBox reference during a task otherwise the
|
---|
268 | * reference count might drop to 0 while a task is still running. This
|
---|
269 | * would result in weird behavior, including deadlocks due to uninit and
|
---|
270 | * locking order issues. The deadlock often is not detectable because the
|
---|
271 | * uninit uses event semaphores which sabotages deadlock detection. */
|
---|
272 | ComObjPtr<VirtualBox> mVirtualBox;
|
---|
273 | AutoCaller mVirtualBoxCaller;
|
---|
274 | };
|
---|
275 |
|
---|
276 | class Medium::CreateBaseTask : public Medium::Task
|
---|
277 | {
|
---|
278 | public:
|
---|
279 | CreateBaseTask(Medium *aMedium,
|
---|
280 | Progress *aProgress,
|
---|
281 | uint64_t aSize,
|
---|
282 | MediumVariant_T aVariant)
|
---|
283 | : Medium::Task(aMedium, aProgress),
|
---|
284 | mSize(aSize),
|
---|
285 | mVariant(aVariant)
|
---|
286 | {}
|
---|
287 |
|
---|
288 | uint64_t mSize;
|
---|
289 | MediumVariant_T mVariant;
|
---|
290 |
|
---|
291 | private:
|
---|
292 | virtual HRESULT handler();
|
---|
293 | };
|
---|
294 |
|
---|
295 | class Medium::CreateDiffTask : public Medium::Task
|
---|
296 | {
|
---|
297 | public:
|
---|
298 | CreateDiffTask(Medium *aMedium,
|
---|
299 | Progress *aProgress,
|
---|
300 | Medium *aTarget,
|
---|
301 | MediumVariant_T aVariant,
|
---|
302 | MediumLockList *aMediumLockList,
|
---|
303 | bool fKeepMediumLockList = false)
|
---|
304 | : Medium::Task(aMedium, aProgress),
|
---|
305 | mpMediumLockList(aMediumLockList),
|
---|
306 | mTarget(aTarget),
|
---|
307 | mVariant(aVariant),
|
---|
308 | mTargetCaller(aTarget),
|
---|
309 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
310 | {
|
---|
311 | AssertReturnVoidStmt(aTarget != NULL, mRC = E_FAIL);
|
---|
312 | mRC = mTargetCaller.rc();
|
---|
313 | if (FAILED(mRC))
|
---|
314 | return;
|
---|
315 | }
|
---|
316 |
|
---|
317 | ~CreateDiffTask()
|
---|
318 | {
|
---|
319 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
320 | delete mpMediumLockList;
|
---|
321 | }
|
---|
322 |
|
---|
323 | MediumLockList *mpMediumLockList;
|
---|
324 |
|
---|
325 | const ComObjPtr<Medium> mTarget;
|
---|
326 | MediumVariant_T mVariant;
|
---|
327 |
|
---|
328 | private:
|
---|
329 | virtual HRESULT handler();
|
---|
330 |
|
---|
331 | AutoCaller mTargetCaller;
|
---|
332 | bool mfKeepMediumLockList;
|
---|
333 | };
|
---|
334 |
|
---|
335 | class Medium::CloneTask : public Medium::Task
|
---|
336 | {
|
---|
337 | public:
|
---|
338 | CloneTask(Medium *aMedium,
|
---|
339 | Progress *aProgress,
|
---|
340 | Medium *aTarget,
|
---|
341 | MediumVariant_T aVariant,
|
---|
342 | Medium *aParent,
|
---|
343 | uint32_t idxSrcImageSame,
|
---|
344 | uint32_t idxDstImageSame,
|
---|
345 | MediumLockList *aSourceMediumLockList,
|
---|
346 | MediumLockList *aTargetMediumLockList,
|
---|
347 | bool fKeepSourceMediumLockList = false,
|
---|
348 | bool fKeepTargetMediumLockList = false)
|
---|
349 | : Medium::Task(aMedium, aProgress),
|
---|
350 | mTarget(aTarget),
|
---|
351 | mParent(aParent),
|
---|
352 | mpSourceMediumLockList(aSourceMediumLockList),
|
---|
353 | mpTargetMediumLockList(aTargetMediumLockList),
|
---|
354 | mVariant(aVariant),
|
---|
355 | midxSrcImageSame(idxSrcImageSame),
|
---|
356 | midxDstImageSame(idxDstImageSame),
|
---|
357 | mTargetCaller(aTarget),
|
---|
358 | mParentCaller(aParent),
|
---|
359 | mfKeepSourceMediumLockList(fKeepSourceMediumLockList),
|
---|
360 | mfKeepTargetMediumLockList(fKeepTargetMediumLockList)
|
---|
361 | {
|
---|
362 | AssertReturnVoidStmt(aTarget != NULL, mRC = E_FAIL);
|
---|
363 | mRC = mTargetCaller.rc();
|
---|
364 | if (FAILED(mRC))
|
---|
365 | return;
|
---|
366 | /* aParent may be NULL */
|
---|
367 | mRC = mParentCaller.rc();
|
---|
368 | if (FAILED(mRC))
|
---|
369 | return;
|
---|
370 | AssertReturnVoidStmt(aSourceMediumLockList != NULL, mRC = E_FAIL);
|
---|
371 | AssertReturnVoidStmt(aTargetMediumLockList != NULL, mRC = E_FAIL);
|
---|
372 | }
|
---|
373 |
|
---|
374 | ~CloneTask()
|
---|
375 | {
|
---|
376 | if (!mfKeepSourceMediumLockList && mpSourceMediumLockList)
|
---|
377 | delete mpSourceMediumLockList;
|
---|
378 | if (!mfKeepTargetMediumLockList && mpTargetMediumLockList)
|
---|
379 | delete mpTargetMediumLockList;
|
---|
380 | }
|
---|
381 |
|
---|
382 | const ComObjPtr<Medium> mTarget;
|
---|
383 | const ComObjPtr<Medium> mParent;
|
---|
384 | MediumLockList *mpSourceMediumLockList;
|
---|
385 | MediumLockList *mpTargetMediumLockList;
|
---|
386 | MediumVariant_T mVariant;
|
---|
387 | uint32_t midxSrcImageSame;
|
---|
388 | uint32_t midxDstImageSame;
|
---|
389 |
|
---|
390 | private:
|
---|
391 | virtual HRESULT handler();
|
---|
392 |
|
---|
393 | AutoCaller mTargetCaller;
|
---|
394 | AutoCaller mParentCaller;
|
---|
395 | bool mfKeepSourceMediumLockList;
|
---|
396 | bool mfKeepTargetMediumLockList;
|
---|
397 | };
|
---|
398 |
|
---|
399 | class Medium::CompactTask : public Medium::Task
|
---|
400 | {
|
---|
401 | public:
|
---|
402 | CompactTask(Medium *aMedium,
|
---|
403 | Progress *aProgress,
|
---|
404 | MediumLockList *aMediumLockList,
|
---|
405 | bool fKeepMediumLockList = false)
|
---|
406 | : Medium::Task(aMedium, aProgress),
|
---|
407 | mpMediumLockList(aMediumLockList),
|
---|
408 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
409 | {
|
---|
410 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
411 | }
|
---|
412 |
|
---|
413 | ~CompactTask()
|
---|
414 | {
|
---|
415 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
416 | delete mpMediumLockList;
|
---|
417 | }
|
---|
418 |
|
---|
419 | MediumLockList *mpMediumLockList;
|
---|
420 |
|
---|
421 | private:
|
---|
422 | virtual HRESULT handler();
|
---|
423 |
|
---|
424 | bool mfKeepMediumLockList;
|
---|
425 | };
|
---|
426 |
|
---|
427 | class Medium::ResizeTask : public Medium::Task
|
---|
428 | {
|
---|
429 | public:
|
---|
430 | ResizeTask(Medium *aMedium,
|
---|
431 | uint64_t aSize,
|
---|
432 | Progress *aProgress,
|
---|
433 | MediumLockList *aMediumLockList,
|
---|
434 | bool fKeepMediumLockList = false)
|
---|
435 | : Medium::Task(aMedium, aProgress),
|
---|
436 | mSize(aSize),
|
---|
437 | mpMediumLockList(aMediumLockList),
|
---|
438 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
439 | {
|
---|
440 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
441 | }
|
---|
442 |
|
---|
443 | ~ResizeTask()
|
---|
444 | {
|
---|
445 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
446 | delete mpMediumLockList;
|
---|
447 | }
|
---|
448 |
|
---|
449 | uint64_t mSize;
|
---|
450 | MediumLockList *mpMediumLockList;
|
---|
451 |
|
---|
452 | private:
|
---|
453 | virtual HRESULT handler();
|
---|
454 |
|
---|
455 | bool mfKeepMediumLockList;
|
---|
456 | };
|
---|
457 |
|
---|
458 | class Medium::ResetTask : public Medium::Task
|
---|
459 | {
|
---|
460 | public:
|
---|
461 | ResetTask(Medium *aMedium,
|
---|
462 | Progress *aProgress,
|
---|
463 | MediumLockList *aMediumLockList,
|
---|
464 | bool fKeepMediumLockList = false)
|
---|
465 | : Medium::Task(aMedium, aProgress),
|
---|
466 | mpMediumLockList(aMediumLockList),
|
---|
467 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
468 | {}
|
---|
469 |
|
---|
470 | ~ResetTask()
|
---|
471 | {
|
---|
472 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
473 | delete mpMediumLockList;
|
---|
474 | }
|
---|
475 |
|
---|
476 | MediumLockList *mpMediumLockList;
|
---|
477 |
|
---|
478 | private:
|
---|
479 | virtual HRESULT handler();
|
---|
480 |
|
---|
481 | bool mfKeepMediumLockList;
|
---|
482 | };
|
---|
483 |
|
---|
484 | class Medium::DeleteTask : public Medium::Task
|
---|
485 | {
|
---|
486 | public:
|
---|
487 | DeleteTask(Medium *aMedium,
|
---|
488 | Progress *aProgress,
|
---|
489 | MediumLockList *aMediumLockList,
|
---|
490 | bool fKeepMediumLockList = false)
|
---|
491 | : Medium::Task(aMedium, aProgress),
|
---|
492 | mpMediumLockList(aMediumLockList),
|
---|
493 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
494 | {}
|
---|
495 |
|
---|
496 | ~DeleteTask()
|
---|
497 | {
|
---|
498 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
499 | delete mpMediumLockList;
|
---|
500 | }
|
---|
501 |
|
---|
502 | MediumLockList *mpMediumLockList;
|
---|
503 |
|
---|
504 | private:
|
---|
505 | virtual HRESULT handler();
|
---|
506 |
|
---|
507 | bool mfKeepMediumLockList;
|
---|
508 | };
|
---|
509 |
|
---|
510 | class Medium::MergeTask : public Medium::Task
|
---|
511 | {
|
---|
512 | public:
|
---|
513 | MergeTask(Medium *aMedium,
|
---|
514 | Medium *aTarget,
|
---|
515 | bool fMergeForward,
|
---|
516 | Medium *aParentForTarget,
|
---|
517 | const MediaList &aChildrenToReparent,
|
---|
518 | Progress *aProgress,
|
---|
519 | MediumLockList *aMediumLockList,
|
---|
520 | bool fKeepMediumLockList = false)
|
---|
521 | : Medium::Task(aMedium, aProgress),
|
---|
522 | mTarget(aTarget),
|
---|
523 | mfMergeForward(fMergeForward),
|
---|
524 | mParentForTarget(aParentForTarget),
|
---|
525 | mChildrenToReparent(aChildrenToReparent),
|
---|
526 | mpMediumLockList(aMediumLockList),
|
---|
527 | mTargetCaller(aTarget),
|
---|
528 | mParentForTargetCaller(aParentForTarget),
|
---|
529 | mfChildrenCaller(false),
|
---|
530 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
531 | {
|
---|
532 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
533 | for (MediaList::const_iterator it = mChildrenToReparent.begin();
|
---|
534 | it != mChildrenToReparent.end();
|
---|
535 | ++it)
|
---|
536 | {
|
---|
537 | HRESULT rc2 = (*it)->addCaller();
|
---|
538 | if (FAILED(rc2))
|
---|
539 | {
|
---|
540 | mRC = E_FAIL;
|
---|
541 | for (MediaList::const_iterator it2 = mChildrenToReparent.begin();
|
---|
542 | it2 != it;
|
---|
543 | --it2)
|
---|
544 | {
|
---|
545 | (*it2)->releaseCaller();
|
---|
546 | }
|
---|
547 | return;
|
---|
548 | }
|
---|
549 | }
|
---|
550 | mfChildrenCaller = true;
|
---|
551 | }
|
---|
552 |
|
---|
553 | ~MergeTask()
|
---|
554 | {
|
---|
555 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
556 | delete mpMediumLockList;
|
---|
557 | if (mfChildrenCaller)
|
---|
558 | {
|
---|
559 | for (MediaList::const_iterator it = mChildrenToReparent.begin();
|
---|
560 | it != mChildrenToReparent.end();
|
---|
561 | ++it)
|
---|
562 | {
|
---|
563 | (*it)->releaseCaller();
|
---|
564 | }
|
---|
565 | }
|
---|
566 | }
|
---|
567 |
|
---|
568 | const ComObjPtr<Medium> mTarget;
|
---|
569 | bool mfMergeForward;
|
---|
570 | /* When mChildrenToReparent is empty then mParentForTarget is non-null.
|
---|
571 | * In other words: they are used in different cases. */
|
---|
572 | const ComObjPtr<Medium> mParentForTarget;
|
---|
573 | MediaList mChildrenToReparent;
|
---|
574 | MediumLockList *mpMediumLockList;
|
---|
575 |
|
---|
576 | private:
|
---|
577 | virtual HRESULT handler();
|
---|
578 |
|
---|
579 | AutoCaller mTargetCaller;
|
---|
580 | AutoCaller mParentForTargetCaller;
|
---|
581 | bool mfChildrenCaller;
|
---|
582 | bool mfKeepMediumLockList;
|
---|
583 | };
|
---|
584 |
|
---|
585 | class Medium::ExportTask : public Medium::Task
|
---|
586 | {
|
---|
587 | public:
|
---|
588 | ExportTask(Medium *aMedium,
|
---|
589 | Progress *aProgress,
|
---|
590 | const char *aFilename,
|
---|
591 | MediumFormat *aFormat,
|
---|
592 | MediumVariant_T aVariant,
|
---|
593 | VDINTERFACEIO *aVDImageIOIf,
|
---|
594 | void *aVDImageIOUser,
|
---|
595 | MediumLockList *aSourceMediumLockList,
|
---|
596 | bool fKeepSourceMediumLockList = false)
|
---|
597 | : Medium::Task(aMedium, aProgress),
|
---|
598 | mpSourceMediumLockList(aSourceMediumLockList),
|
---|
599 | mFilename(aFilename),
|
---|
600 | mFormat(aFormat),
|
---|
601 | mVariant(aVariant),
|
---|
602 | mfKeepSourceMediumLockList(fKeepSourceMediumLockList)
|
---|
603 | {
|
---|
604 | AssertReturnVoidStmt(aSourceMediumLockList != NULL, mRC = E_FAIL);
|
---|
605 |
|
---|
606 | mVDImageIfaces = aMedium->m->vdImageIfaces;
|
---|
607 | if (aVDImageIOIf)
|
---|
608 | {
|
---|
609 | int vrc = VDInterfaceAdd(&aVDImageIOIf->Core, "Medium::vdInterfaceIO",
|
---|
610 | VDINTERFACETYPE_IO, aVDImageIOUser,
|
---|
611 | sizeof(VDINTERFACEIO), &mVDImageIfaces);
|
---|
612 | AssertRCReturnVoidStmt(vrc, mRC = E_FAIL);
|
---|
613 | }
|
---|
614 | }
|
---|
615 |
|
---|
616 | ~ExportTask()
|
---|
617 | {
|
---|
618 | if (!mfKeepSourceMediumLockList && mpSourceMediumLockList)
|
---|
619 | delete mpSourceMediumLockList;
|
---|
620 | }
|
---|
621 |
|
---|
622 | MediumLockList *mpSourceMediumLockList;
|
---|
623 | Utf8Str mFilename;
|
---|
624 | ComObjPtr<MediumFormat> mFormat;
|
---|
625 | MediumVariant_T mVariant;
|
---|
626 | PVDINTERFACE mVDImageIfaces;
|
---|
627 |
|
---|
628 | private:
|
---|
629 | virtual HRESULT handler();
|
---|
630 |
|
---|
631 | bool mfKeepSourceMediumLockList;
|
---|
632 | };
|
---|
633 |
|
---|
634 | class Medium::ImportTask : public Medium::Task
|
---|
635 | {
|
---|
636 | public:
|
---|
637 | ImportTask(Medium *aMedium,
|
---|
638 | Progress *aProgress,
|
---|
639 | const char *aFilename,
|
---|
640 | MediumFormat *aFormat,
|
---|
641 | MediumVariant_T aVariant,
|
---|
642 | VDINTERFACEIO *aVDImageIOIf,
|
---|
643 | void *aVDImageIOUser,
|
---|
644 | Medium *aParent,
|
---|
645 | MediumLockList *aTargetMediumLockList,
|
---|
646 | bool fKeepTargetMediumLockList = false)
|
---|
647 | : Medium::Task(aMedium, aProgress),
|
---|
648 | mFilename(aFilename),
|
---|
649 | mFormat(aFormat),
|
---|
650 | mVariant(aVariant),
|
---|
651 | mParent(aParent),
|
---|
652 | mpTargetMediumLockList(aTargetMediumLockList),
|
---|
653 | mParentCaller(aParent),
|
---|
654 | mfKeepTargetMediumLockList(fKeepTargetMediumLockList)
|
---|
655 | {
|
---|
656 | AssertReturnVoidStmt(aTargetMediumLockList != NULL, mRC = E_FAIL);
|
---|
657 | /* aParent may be NULL */
|
---|
658 | mRC = mParentCaller.rc();
|
---|
659 | if (FAILED(mRC))
|
---|
660 | return;
|
---|
661 |
|
---|
662 | mVDImageIfaces = aMedium->m->vdImageIfaces;
|
---|
663 | if (aVDImageIOIf)
|
---|
664 | {
|
---|
665 | int vrc = VDInterfaceAdd(&aVDImageIOIf->Core, "Medium::vdInterfaceIO",
|
---|
666 | VDINTERFACETYPE_IO, aVDImageIOUser,
|
---|
667 | sizeof(VDINTERFACEIO), &mVDImageIfaces);
|
---|
668 | AssertRCReturnVoidStmt(vrc, mRC = E_FAIL);
|
---|
669 | }
|
---|
670 | }
|
---|
671 |
|
---|
672 | ~ImportTask()
|
---|
673 | {
|
---|
674 | if (!mfKeepTargetMediumLockList && mpTargetMediumLockList)
|
---|
675 | delete mpTargetMediumLockList;
|
---|
676 | }
|
---|
677 |
|
---|
678 | Utf8Str mFilename;
|
---|
679 | ComObjPtr<MediumFormat> mFormat;
|
---|
680 | MediumVariant_T mVariant;
|
---|
681 | const ComObjPtr<Medium> mParent;
|
---|
682 | MediumLockList *mpTargetMediumLockList;
|
---|
683 | PVDINTERFACE mVDImageIfaces;
|
---|
684 |
|
---|
685 | private:
|
---|
686 | virtual HRESULT handler();
|
---|
687 |
|
---|
688 | AutoCaller mParentCaller;
|
---|
689 | bool mfKeepTargetMediumLockList;
|
---|
690 | };
|
---|
691 |
|
---|
692 | /**
|
---|
693 | * Thread function for time-consuming medium tasks.
|
---|
694 | *
|
---|
695 | * @param pvUser Pointer to the Medium::Task instance.
|
---|
696 | */
|
---|
697 | /* static */
|
---|
698 | DECLCALLBACK(int) Medium::Task::fntMediumTask(RTTHREAD aThread, void *pvUser)
|
---|
699 | {
|
---|
700 | LogFlowFuncEnter();
|
---|
701 | AssertReturn(pvUser, (int)E_INVALIDARG);
|
---|
702 | Medium::Task *pTask = static_cast<Medium::Task *>(pvUser);
|
---|
703 |
|
---|
704 | pTask->mThread = aThread;
|
---|
705 |
|
---|
706 | HRESULT rc = pTask->handler();
|
---|
707 |
|
---|
708 | /* complete the progress if run asynchronously */
|
---|
709 | if (pTask->isAsync())
|
---|
710 | {
|
---|
711 | if (!pTask->mProgress.isNull())
|
---|
712 | pTask->mProgress->notifyComplete(rc);
|
---|
713 | }
|
---|
714 |
|
---|
715 | /* pTask is no longer needed, delete it. */
|
---|
716 | delete pTask;
|
---|
717 |
|
---|
718 | LogFlowFunc(("rc=%Rhrc\n", rc));
|
---|
719 | LogFlowFuncLeave();
|
---|
720 |
|
---|
721 | return (int)rc;
|
---|
722 | }
|
---|
723 |
|
---|
724 | /**
|
---|
725 | * PFNVDPROGRESS callback handler for Task operations.
|
---|
726 | *
|
---|
727 | * @param pvUser Pointer to the Progress instance.
|
---|
728 | * @param uPercent Completion percentage (0-100).
|
---|
729 | */
|
---|
730 | /*static*/
|
---|
731 | DECLCALLBACK(int) Medium::Task::vdProgressCall(void *pvUser, unsigned uPercent)
|
---|
732 | {
|
---|
733 | Progress *that = static_cast<Progress *>(pvUser);
|
---|
734 |
|
---|
735 | if (that != NULL)
|
---|
736 | {
|
---|
737 | /* update the progress object, capping it at 99% as the final percent
|
---|
738 | * is used for additional operations like setting the UUIDs and similar. */
|
---|
739 | HRESULT rc = that->SetCurrentOperationProgress(uPercent * 99 / 100);
|
---|
740 | if (FAILED(rc))
|
---|
741 | {
|
---|
742 | if (rc == E_FAIL)
|
---|
743 | return VERR_CANCELLED;
|
---|
744 | else
|
---|
745 | return VERR_INVALID_STATE;
|
---|
746 | }
|
---|
747 | }
|
---|
748 |
|
---|
749 | return VINF_SUCCESS;
|
---|
750 | }
|
---|
751 |
|
---|
752 | /**
|
---|
753 | * Implementation code for the "create base" task.
|
---|
754 | */
|
---|
755 | HRESULT Medium::CreateBaseTask::handler()
|
---|
756 | {
|
---|
757 | return mMedium->taskCreateBaseHandler(*this);
|
---|
758 | }
|
---|
759 |
|
---|
760 | /**
|
---|
761 | * Implementation code for the "create diff" task.
|
---|
762 | */
|
---|
763 | HRESULT Medium::CreateDiffTask::handler()
|
---|
764 | {
|
---|
765 | return mMedium->taskCreateDiffHandler(*this);
|
---|
766 | }
|
---|
767 |
|
---|
768 | /**
|
---|
769 | * Implementation code for the "clone" task.
|
---|
770 | */
|
---|
771 | HRESULT Medium::CloneTask::handler()
|
---|
772 | {
|
---|
773 | return mMedium->taskCloneHandler(*this);
|
---|
774 | }
|
---|
775 |
|
---|
776 | /**
|
---|
777 | * Implementation code for the "compact" task.
|
---|
778 | */
|
---|
779 | HRESULT Medium::CompactTask::handler()
|
---|
780 | {
|
---|
781 | return mMedium->taskCompactHandler(*this);
|
---|
782 | }
|
---|
783 |
|
---|
784 | /**
|
---|
785 | * Implementation code for the "resize" task.
|
---|
786 | */
|
---|
787 | HRESULT Medium::ResizeTask::handler()
|
---|
788 | {
|
---|
789 | return mMedium->taskResizeHandler(*this);
|
---|
790 | }
|
---|
791 |
|
---|
792 |
|
---|
793 | /**
|
---|
794 | * Implementation code for the "reset" task.
|
---|
795 | */
|
---|
796 | HRESULT Medium::ResetTask::handler()
|
---|
797 | {
|
---|
798 | return mMedium->taskResetHandler(*this);
|
---|
799 | }
|
---|
800 |
|
---|
801 | /**
|
---|
802 | * Implementation code for the "delete" task.
|
---|
803 | */
|
---|
804 | HRESULT Medium::DeleteTask::handler()
|
---|
805 | {
|
---|
806 | return mMedium->taskDeleteHandler(*this);
|
---|
807 | }
|
---|
808 |
|
---|
809 | /**
|
---|
810 | * Implementation code for the "merge" task.
|
---|
811 | */
|
---|
812 | HRESULT Medium::MergeTask::handler()
|
---|
813 | {
|
---|
814 | return mMedium->taskMergeHandler(*this);
|
---|
815 | }
|
---|
816 |
|
---|
817 | /**
|
---|
818 | * Implementation code for the "export" task.
|
---|
819 | */
|
---|
820 | HRESULT Medium::ExportTask::handler()
|
---|
821 | {
|
---|
822 | return mMedium->taskExportHandler(*this);
|
---|
823 | }
|
---|
824 |
|
---|
825 | /**
|
---|
826 | * Implementation code for the "import" task.
|
---|
827 | */
|
---|
828 | HRESULT Medium::ImportTask::handler()
|
---|
829 | {
|
---|
830 | return mMedium->taskImportHandler(*this);
|
---|
831 | }
|
---|
832 |
|
---|
833 | ////////////////////////////////////////////////////////////////////////////////
|
---|
834 | //
|
---|
835 | // Medium constructor / destructor
|
---|
836 | //
|
---|
837 | ////////////////////////////////////////////////////////////////////////////////
|
---|
838 |
|
---|
839 | DEFINE_EMPTY_CTOR_DTOR(Medium)
|
---|
840 |
|
---|
841 | HRESULT Medium::FinalConstruct()
|
---|
842 | {
|
---|
843 | m = new Data;
|
---|
844 |
|
---|
845 | /* Initialize the callbacks of the VD error interface */
|
---|
846 | m->vdIfError.pfnError = vdErrorCall;
|
---|
847 | m->vdIfError.pfnMessage = NULL;
|
---|
848 |
|
---|
849 | /* Initialize the callbacks of the VD config interface */
|
---|
850 | m->vdIfConfig.pfnAreKeysValid = vdConfigAreKeysValid;
|
---|
851 | m->vdIfConfig.pfnQuerySize = vdConfigQuerySize;
|
---|
852 | m->vdIfConfig.pfnQuery = vdConfigQuery;
|
---|
853 |
|
---|
854 | /* Initialize the callbacks of the VD TCP interface (we always use the host
|
---|
855 | * IP stack for now) */
|
---|
856 | m->vdIfTcpNet.pfnSocketCreate = vdTcpSocketCreate;
|
---|
857 | m->vdIfTcpNet.pfnSocketDestroy = vdTcpSocketDestroy;
|
---|
858 | m->vdIfTcpNet.pfnClientConnect = vdTcpClientConnect;
|
---|
859 | m->vdIfTcpNet.pfnClientClose = vdTcpClientClose;
|
---|
860 | m->vdIfTcpNet.pfnIsClientConnected = vdTcpIsClientConnected;
|
---|
861 | m->vdIfTcpNet.pfnSelectOne = vdTcpSelectOne;
|
---|
862 | m->vdIfTcpNet.pfnRead = vdTcpRead;
|
---|
863 | m->vdIfTcpNet.pfnWrite = vdTcpWrite;
|
---|
864 | m->vdIfTcpNet.pfnSgWrite = vdTcpSgWrite;
|
---|
865 | m->vdIfTcpNet.pfnFlush = vdTcpFlush;
|
---|
866 | m->vdIfTcpNet.pfnSetSendCoalescing = vdTcpSetSendCoalescing;
|
---|
867 | m->vdIfTcpNet.pfnGetLocalAddress = vdTcpGetLocalAddress;
|
---|
868 | m->vdIfTcpNet.pfnGetPeerAddress = vdTcpGetPeerAddress;
|
---|
869 | m->vdIfTcpNet.pfnSelectOneEx = NULL;
|
---|
870 | m->vdIfTcpNet.pfnPoke = NULL;
|
---|
871 |
|
---|
872 | /* Initialize the per-disk interface chain (could be done more globally,
|
---|
873 | * but it's not wasting much time or space so it's not worth it). */
|
---|
874 | int vrc;
|
---|
875 | vrc = VDInterfaceAdd(&m->vdIfError.Core,
|
---|
876 | "Medium::vdInterfaceError",
|
---|
877 | VDINTERFACETYPE_ERROR, this,
|
---|
878 | sizeof(VDINTERFACEERROR), &m->vdDiskIfaces);
|
---|
879 | AssertRCReturn(vrc, E_FAIL);
|
---|
880 |
|
---|
881 | /* Initialize the per-image interface chain */
|
---|
882 | vrc = VDInterfaceAdd(&m->vdIfConfig.Core,
|
---|
883 | "Medium::vdInterfaceConfig",
|
---|
884 | VDINTERFACETYPE_CONFIG, this,
|
---|
885 | sizeof(VDINTERFACECONFIG), &m->vdImageIfaces);
|
---|
886 | AssertRCReturn(vrc, E_FAIL);
|
---|
887 |
|
---|
888 | vrc = VDInterfaceAdd(&m->vdIfTcpNet.Core,
|
---|
889 | "Medium::vdInterfaceTcpNet",
|
---|
890 | VDINTERFACETYPE_TCPNET, this,
|
---|
891 | sizeof(VDINTERFACETCPNET), &m->vdImageIfaces);
|
---|
892 | AssertRCReturn(vrc, E_FAIL);
|
---|
893 |
|
---|
894 | return BaseFinalConstruct();
|
---|
895 | }
|
---|
896 |
|
---|
897 | void Medium::FinalRelease()
|
---|
898 | {
|
---|
899 | uninit();
|
---|
900 |
|
---|
901 | delete m;
|
---|
902 |
|
---|
903 | BaseFinalRelease();
|
---|
904 | }
|
---|
905 |
|
---|
906 | /**
|
---|
907 | * Initializes an empty hard disk object without creating or opening an associated
|
---|
908 | * storage unit.
|
---|
909 | *
|
---|
910 | * This gets called by VirtualBox::CreateHardDisk() in which case uuidMachineRegistry
|
---|
911 | * is empty since starting with VirtualBox 4.0, we no longer add opened media to a
|
---|
912 | * registry automatically (this is deferred until the medium is attached to a machine).
|
---|
913 | *
|
---|
914 | * This also gets called when VirtualBox creates diff images; in this case uuidMachineRegistry
|
---|
915 | * is set to the registry of the parent image to make sure they all end up in the same
|
---|
916 | * file.
|
---|
917 | *
|
---|
918 | * For hard disks that don't have the MediumFormatCapabilities_CreateFixed or
|
---|
919 | * MediumFormatCapabilities_CreateDynamic capability (and therefore cannot be created or deleted
|
---|
920 | * with the means of VirtualBox) the associated storage unit is assumed to be
|
---|
921 | * ready for use so the state of the hard disk object will be set to Created.
|
---|
922 | *
|
---|
923 | * @param aVirtualBox VirtualBox object.
|
---|
924 | * @param aFormat
|
---|
925 | * @param aLocation Storage unit location.
|
---|
926 | * @param uuidMachineRegistry The registry to which this medium should be added (global registry UUID or machine UUID or empty if none).
|
---|
927 | */
|
---|
928 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
929 | const Utf8Str &aFormat,
|
---|
930 | const Utf8Str &aLocation,
|
---|
931 | const Guid &uuidMachineRegistry)
|
---|
932 | {
|
---|
933 | AssertReturn(aVirtualBox != NULL, E_FAIL);
|
---|
934 | AssertReturn(!aFormat.isEmpty(), E_FAIL);
|
---|
935 |
|
---|
936 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
937 | AutoInitSpan autoInitSpan(this);
|
---|
938 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
939 |
|
---|
940 | HRESULT rc = S_OK;
|
---|
941 |
|
---|
942 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
943 |
|
---|
944 | if (!uuidMachineRegistry.isEmpty())
|
---|
945 | m->llRegistryIDs.push_back(uuidMachineRegistry);
|
---|
946 |
|
---|
947 | /* no storage yet */
|
---|
948 | m->state = MediumState_NotCreated;
|
---|
949 |
|
---|
950 | /* cannot be a host drive */
|
---|
951 | m->hostDrive = false;
|
---|
952 |
|
---|
953 | /* No storage unit is created yet, no need to call Medium::queryInfo */
|
---|
954 |
|
---|
955 | rc = setFormat(aFormat);
|
---|
956 | if (FAILED(rc)) return rc;
|
---|
957 |
|
---|
958 | rc = setLocation(aLocation);
|
---|
959 | if (FAILED(rc)) return rc;
|
---|
960 |
|
---|
961 | if (!(m->formatObj->getCapabilities() & ( MediumFormatCapabilities_CreateFixed
|
---|
962 | | MediumFormatCapabilities_CreateDynamic))
|
---|
963 | )
|
---|
964 | {
|
---|
965 | /* Storage for hard disks of this format can neither be explicitly
|
---|
966 | * created by VirtualBox nor deleted, so we place the hard disk to
|
---|
967 | * Inaccessible state here and also add it to the registry. The
|
---|
968 | * state means that one has to use RefreshState() to update the
|
---|
969 | * medium format specific fields. */
|
---|
970 | m->state = MediumState_Inaccessible;
|
---|
971 | // create new UUID
|
---|
972 | unconst(m->id).create();
|
---|
973 |
|
---|
974 | AutoWriteLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
975 | ComObjPtr<Medium> pMedium;
|
---|
976 | rc = m->pVirtualBox->registerMedium(this, &pMedium, DeviceType_HardDisk);
|
---|
977 | Assert(this == pMedium);
|
---|
978 | }
|
---|
979 |
|
---|
980 | /* Confirm a successful initialization when it's the case */
|
---|
981 | if (SUCCEEDED(rc))
|
---|
982 | autoInitSpan.setSucceeded();
|
---|
983 |
|
---|
984 | return rc;
|
---|
985 | }
|
---|
986 |
|
---|
987 | /**
|
---|
988 | * Initializes the medium object by opening the storage unit at the specified
|
---|
989 | * location. The enOpenMode parameter defines whether the medium will be opened
|
---|
990 | * read/write or read-only.
|
---|
991 | *
|
---|
992 | * This gets called by VirtualBox::OpenMedium() and also by
|
---|
993 | * Machine::AttachDevice() and createImplicitDiffs() when new diff
|
---|
994 | * images are created.
|
---|
995 | *
|
---|
996 | * There is no registry for this case since starting with VirtualBox 4.0, we
|
---|
997 | * no longer add opened media to a registry automatically (this is deferred
|
---|
998 | * until the medium is attached to a machine).
|
---|
999 | *
|
---|
1000 | * For hard disks, the UUID, format and the parent of this medium will be
|
---|
1001 | * determined when reading the medium storage unit. For DVD and floppy images,
|
---|
1002 | * which have no UUIDs in their storage units, new UUIDs are created.
|
---|
1003 | * If the detected or set parent is not known to VirtualBox, then this method
|
---|
1004 | * will fail.
|
---|
1005 | *
|
---|
1006 | * @param aVirtualBox VirtualBox object.
|
---|
1007 | * @param aLocation Storage unit location.
|
---|
1008 | * @param enOpenMode Whether to open the medium read/write or read-only.
|
---|
1009 | * @param fForceNewUuid Whether a new UUID should be set to avoid duplicates.
|
---|
1010 | * @param aDeviceType Device type of medium.
|
---|
1011 | */
|
---|
1012 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1013 | const Utf8Str &aLocation,
|
---|
1014 | HDDOpenMode enOpenMode,
|
---|
1015 | bool fForceNewUuid,
|
---|
1016 | DeviceType_T aDeviceType)
|
---|
1017 | {
|
---|
1018 | AssertReturn(aVirtualBox, E_INVALIDARG);
|
---|
1019 | AssertReturn(!aLocation.isEmpty(), E_INVALIDARG);
|
---|
1020 |
|
---|
1021 | HRESULT rc = S_OK;
|
---|
1022 |
|
---|
1023 | {
|
---|
1024 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1025 | AutoInitSpan autoInitSpan(this);
|
---|
1026 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1027 |
|
---|
1028 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1029 |
|
---|
1030 | /* there must be a storage unit */
|
---|
1031 | m->state = MediumState_Created;
|
---|
1032 |
|
---|
1033 | /* remember device type for correct unregistering later */
|
---|
1034 | m->devType = aDeviceType;
|
---|
1035 |
|
---|
1036 | /* cannot be a host drive */
|
---|
1037 | m->hostDrive = false;
|
---|
1038 |
|
---|
1039 | /* remember the open mode (defaults to ReadWrite) */
|
---|
1040 | m->hddOpenMode = enOpenMode;
|
---|
1041 |
|
---|
1042 | if (aDeviceType == DeviceType_DVD)
|
---|
1043 | m->type = MediumType_Readonly;
|
---|
1044 | else if (aDeviceType == DeviceType_Floppy)
|
---|
1045 | m->type = MediumType_Writethrough;
|
---|
1046 |
|
---|
1047 | rc = setLocation(aLocation);
|
---|
1048 | if (FAILED(rc)) return rc;
|
---|
1049 |
|
---|
1050 | /* get all the information about the medium from the storage unit */
|
---|
1051 | if (fForceNewUuid)
|
---|
1052 | unconst(m->uuidImage).create();
|
---|
1053 |
|
---|
1054 | m->state = MediumState_Inaccessible;
|
---|
1055 | m->strLastAccessError = tr("Accessibility check was not yet performed");
|
---|
1056 |
|
---|
1057 | /* Confirm a successful initialization before the call to queryInfo.
|
---|
1058 | * Otherwise we can end up with a AutoCaller deadlock because the
|
---|
1059 | * medium becomes visible but is not marked as initialized. Causes
|
---|
1060 | * locking trouble (e.g. trying to save media registries) which is
|
---|
1061 | * hard to solve. */
|
---|
1062 | autoInitSpan.setSucceeded();
|
---|
1063 | }
|
---|
1064 |
|
---|
1065 | /* we're normal code from now on, no longer init */
|
---|
1066 | AutoCaller autoCaller(this);
|
---|
1067 | if (FAILED(autoCaller.rc()))
|
---|
1068 | return autoCaller.rc();
|
---|
1069 |
|
---|
1070 | /* need to call queryInfo immediately to correctly place the medium in
|
---|
1071 | * the respective media tree and update other information such as uuid */
|
---|
1072 | rc = queryInfo(fForceNewUuid /* fSetImageId */, false /* fSetParentId */);
|
---|
1073 | if (SUCCEEDED(rc))
|
---|
1074 | {
|
---|
1075 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1076 |
|
---|
1077 | /* if the storage unit is not accessible, it's not acceptable for the
|
---|
1078 | * newly opened media so convert this into an error */
|
---|
1079 | if (m->state == MediumState_Inaccessible)
|
---|
1080 | {
|
---|
1081 | Assert(!m->strLastAccessError.isEmpty());
|
---|
1082 | rc = setError(E_FAIL, "%s", m->strLastAccessError.c_str());
|
---|
1083 | alock.release();
|
---|
1084 | autoCaller.release();
|
---|
1085 | uninit();
|
---|
1086 | }
|
---|
1087 | else
|
---|
1088 | {
|
---|
1089 | AssertStmt(!m->id.isEmpty(),
|
---|
1090 | alock.release(); autoCaller.release(); uninit(); return E_FAIL);
|
---|
1091 |
|
---|
1092 | /* storage format must be detected by Medium::queryInfo if the
|
---|
1093 | * medium is accessible */
|
---|
1094 | AssertStmt(!m->strFormat.isEmpty(),
|
---|
1095 | alock.release(); autoCaller.release(); uninit(); return E_FAIL);
|
---|
1096 | }
|
---|
1097 | }
|
---|
1098 | else
|
---|
1099 | {
|
---|
1100 | /* opening this image failed, mark the object as dead */
|
---|
1101 | autoCaller.release();
|
---|
1102 | uninit();
|
---|
1103 | }
|
---|
1104 |
|
---|
1105 | return rc;
|
---|
1106 | }
|
---|
1107 |
|
---|
1108 | /**
|
---|
1109 | * Initializes the medium object by loading its data from the given settings
|
---|
1110 | * node. In this mode, the medium will always be opened read/write.
|
---|
1111 | *
|
---|
1112 | * In this case, since we're loading from a registry, uuidMachineRegistry is
|
---|
1113 | * always set: it's either the global registry UUID or a machine UUID when
|
---|
1114 | * loading from a per-machine registry.
|
---|
1115 | *
|
---|
1116 | * @param aVirtualBox VirtualBox object.
|
---|
1117 | * @param aParent Parent medium disk or NULL for a root (base) medium.
|
---|
1118 | * @param aDeviceType Device type of the medium.
|
---|
1119 | * @param uuidMachineRegistry The registry to which this medium should be added (global registry UUID or machine UUID).
|
---|
1120 | * @param aNode Configuration settings.
|
---|
1121 | * @param strMachineFolder The machine folder with which to resolve relative paths; if empty, then we use the VirtualBox home directory
|
---|
1122 | *
|
---|
1123 | * @note Locks the medium tree for writing.
|
---|
1124 | */
|
---|
1125 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1126 | Medium *aParent,
|
---|
1127 | DeviceType_T aDeviceType,
|
---|
1128 | const Guid &uuidMachineRegistry,
|
---|
1129 | const settings::Medium &data,
|
---|
1130 | const Utf8Str &strMachineFolder)
|
---|
1131 | {
|
---|
1132 | using namespace settings;
|
---|
1133 |
|
---|
1134 | AssertReturn(aVirtualBox, E_INVALIDARG);
|
---|
1135 |
|
---|
1136 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1137 | AutoInitSpan autoInitSpan(this);
|
---|
1138 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1139 |
|
---|
1140 | HRESULT rc = S_OK;
|
---|
1141 |
|
---|
1142 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1143 |
|
---|
1144 | if (!uuidMachineRegistry.isEmpty())
|
---|
1145 | m->llRegistryIDs.push_back(uuidMachineRegistry);
|
---|
1146 |
|
---|
1147 | /* register with VirtualBox/parent early, since uninit() will
|
---|
1148 | * unconditionally unregister on failure */
|
---|
1149 | if (aParent)
|
---|
1150 | {
|
---|
1151 | // differencing medium: add to parent
|
---|
1152 | AutoWriteLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1153 | m->pParent = aParent;
|
---|
1154 | aParent->m->llChildren.push_back(this);
|
---|
1155 | }
|
---|
1156 |
|
---|
1157 | /* see below why we don't call Medium::queryInfo (and therefore treat
|
---|
1158 | * the medium as inaccessible for now */
|
---|
1159 | m->state = MediumState_Inaccessible;
|
---|
1160 | m->strLastAccessError = tr("Accessibility check was not yet performed");
|
---|
1161 |
|
---|
1162 | /* required */
|
---|
1163 | unconst(m->id) = data.uuid;
|
---|
1164 |
|
---|
1165 | /* assume not a host drive */
|
---|
1166 | m->hostDrive = false;
|
---|
1167 |
|
---|
1168 | /* optional */
|
---|
1169 | m->strDescription = data.strDescription;
|
---|
1170 |
|
---|
1171 | /* required */
|
---|
1172 | if (aDeviceType == DeviceType_HardDisk)
|
---|
1173 | {
|
---|
1174 | AssertReturn(!data.strFormat.isEmpty(), E_FAIL);
|
---|
1175 | rc = setFormat(data.strFormat);
|
---|
1176 | if (FAILED(rc)) return rc;
|
---|
1177 | }
|
---|
1178 | else
|
---|
1179 | {
|
---|
1180 | /// @todo handle host drive settings here as well?
|
---|
1181 | if (!data.strFormat.isEmpty())
|
---|
1182 | rc = setFormat(data.strFormat);
|
---|
1183 | else
|
---|
1184 | rc = setFormat("RAW");
|
---|
1185 | if (FAILED(rc)) return rc;
|
---|
1186 | }
|
---|
1187 |
|
---|
1188 | /* optional, only for diffs, default is false; we can only auto-reset
|
---|
1189 | * diff media so they must have a parent */
|
---|
1190 | if (aParent != NULL)
|
---|
1191 | m->autoReset = data.fAutoReset;
|
---|
1192 | else
|
---|
1193 | m->autoReset = false;
|
---|
1194 |
|
---|
1195 | /* properties (after setting the format as it populates the map). Note that
|
---|
1196 | * if some properties are not supported but present in the settings file,
|
---|
1197 | * they will still be read and accessible (for possible backward
|
---|
1198 | * compatibility; we can also clean them up from the XML upon next
|
---|
1199 | * XML format version change if we wish) */
|
---|
1200 | for (settings::StringsMap::const_iterator it = data.properties.begin();
|
---|
1201 | it != data.properties.end();
|
---|
1202 | ++it)
|
---|
1203 | {
|
---|
1204 | const Utf8Str &name = it->first;
|
---|
1205 | const Utf8Str &value = it->second;
|
---|
1206 | m->mapProperties[name] = value;
|
---|
1207 | }
|
---|
1208 |
|
---|
1209 | /* try to decrypt an optional iSCSI initiator secret */
|
---|
1210 | settings::StringsMap::const_iterator itCph = data.properties.find("InitiatorSecretEncrypted");
|
---|
1211 | if ( itCph != data.properties.end()
|
---|
1212 | && !itCph->second.isEmpty())
|
---|
1213 | {
|
---|
1214 | Utf8Str strPlaintext;
|
---|
1215 | int vrc = m->pVirtualBox->decryptSetting(&strPlaintext, itCph->second);
|
---|
1216 | if (RT_SUCCESS(vrc))
|
---|
1217 | m->mapProperties["InitiatorSecret"] = strPlaintext;
|
---|
1218 | }
|
---|
1219 |
|
---|
1220 | Utf8Str strFull;
|
---|
1221 | if (m->formatObj->getCapabilities() & MediumFormatCapabilities_File)
|
---|
1222 | {
|
---|
1223 | // compose full path of the medium, if it's not fully qualified...
|
---|
1224 | // slightly convoluted logic here. If the caller has given us a
|
---|
1225 | // machine folder, then a relative path will be relative to that:
|
---|
1226 | if ( !strMachineFolder.isEmpty()
|
---|
1227 | && !RTPathStartsWithRoot(data.strLocation.c_str())
|
---|
1228 | )
|
---|
1229 | {
|
---|
1230 | strFull = strMachineFolder;
|
---|
1231 | strFull += RTPATH_SLASH;
|
---|
1232 | strFull += data.strLocation;
|
---|
1233 | }
|
---|
1234 | else
|
---|
1235 | {
|
---|
1236 | // Otherwise use the old VirtualBox "make absolute path" logic:
|
---|
1237 | rc = m->pVirtualBox->calculateFullPath(data.strLocation, strFull);
|
---|
1238 | if (FAILED(rc)) return rc;
|
---|
1239 | }
|
---|
1240 | }
|
---|
1241 | else
|
---|
1242 | strFull = data.strLocation;
|
---|
1243 |
|
---|
1244 | rc = setLocation(strFull);
|
---|
1245 | if (FAILED(rc)) return rc;
|
---|
1246 |
|
---|
1247 | if (aDeviceType == DeviceType_HardDisk)
|
---|
1248 | {
|
---|
1249 | /* type is only for base hard disks */
|
---|
1250 | if (m->pParent.isNull())
|
---|
1251 | m->type = data.hdType;
|
---|
1252 | }
|
---|
1253 | else if (aDeviceType == DeviceType_DVD)
|
---|
1254 | m->type = MediumType_Readonly;
|
---|
1255 | else
|
---|
1256 | m->type = MediumType_Writethrough;
|
---|
1257 |
|
---|
1258 | /* remember device type for correct unregistering later */
|
---|
1259 | m->devType = aDeviceType;
|
---|
1260 |
|
---|
1261 | LogFlowThisFunc(("m->strLocationFull='%s', m->strFormat=%s, m->id={%RTuuid}\n",
|
---|
1262 | m->strLocationFull.c_str(), m->strFormat.c_str(), m->id.raw()));
|
---|
1263 |
|
---|
1264 | /* Don't call Medium::queryInfo for registered media to prevent the calling
|
---|
1265 | * thread (i.e. the VirtualBox server startup thread) from an unexpected
|
---|
1266 | * freeze but mark it as initially inaccessible instead. The vital UUID,
|
---|
1267 | * location and format properties are read from the registry file above; to
|
---|
1268 | * get the actual state and the rest of the data, the user will have to call
|
---|
1269 | * COMGETTER(State). */
|
---|
1270 |
|
---|
1271 | AutoWriteLock treeLock(aVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1272 |
|
---|
1273 | /* load all children */
|
---|
1274 | for (settings::MediaList::const_iterator it = data.llChildren.begin();
|
---|
1275 | it != data.llChildren.end();
|
---|
1276 | ++it)
|
---|
1277 | {
|
---|
1278 | const settings::Medium &med = *it;
|
---|
1279 |
|
---|
1280 | ComObjPtr<Medium> pHD;
|
---|
1281 | pHD.createObject();
|
---|
1282 | rc = pHD->init(aVirtualBox,
|
---|
1283 | this, // parent
|
---|
1284 | aDeviceType,
|
---|
1285 | uuidMachineRegistry,
|
---|
1286 | med, // child data
|
---|
1287 | strMachineFolder);
|
---|
1288 | if (FAILED(rc)) break;
|
---|
1289 |
|
---|
1290 | rc = m->pVirtualBox->registerMedium(pHD, &pHD, DeviceType_HardDisk);
|
---|
1291 | if (FAILED(rc)) break;
|
---|
1292 | }
|
---|
1293 |
|
---|
1294 | /* Confirm a successful initialization when it's the case */
|
---|
1295 | if (SUCCEEDED(rc))
|
---|
1296 | autoInitSpan.setSucceeded();
|
---|
1297 |
|
---|
1298 | return rc;
|
---|
1299 | }
|
---|
1300 |
|
---|
1301 | /**
|
---|
1302 | * Initializes the medium object by providing the host drive information.
|
---|
1303 | * Not used for anything but the host floppy/host DVD case.
|
---|
1304 | *
|
---|
1305 | * There is no registry for this case.
|
---|
1306 | *
|
---|
1307 | * @param aVirtualBox VirtualBox object.
|
---|
1308 | * @param aDeviceType Device type of the medium.
|
---|
1309 | * @param aLocation Location of the host drive.
|
---|
1310 | * @param aDescription Comment for this host drive.
|
---|
1311 | *
|
---|
1312 | * @note Locks VirtualBox lock for writing.
|
---|
1313 | */
|
---|
1314 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1315 | DeviceType_T aDeviceType,
|
---|
1316 | const Utf8Str &aLocation,
|
---|
1317 | const Utf8Str &aDescription /* = Utf8Str::Empty */)
|
---|
1318 | {
|
---|
1319 | ComAssertRet(aDeviceType == DeviceType_DVD || aDeviceType == DeviceType_Floppy, E_INVALIDARG);
|
---|
1320 | ComAssertRet(!aLocation.isEmpty(), E_INVALIDARG);
|
---|
1321 |
|
---|
1322 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1323 | AutoInitSpan autoInitSpan(this);
|
---|
1324 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1325 |
|
---|
1326 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1327 |
|
---|
1328 | // We do not store host drives in VirtualBox.xml or anywhere else, so if we want
|
---|
1329 | // host drives to be identifiable by UUID and not give the drive a different UUID
|
---|
1330 | // every time VirtualBox starts, we need to fake a reproducible UUID here:
|
---|
1331 | RTUUID uuid;
|
---|
1332 | RTUuidClear(&uuid);
|
---|
1333 | if (aDeviceType == DeviceType_DVD)
|
---|
1334 | memcpy(&uuid.au8[0], "DVD", 3);
|
---|
1335 | else
|
---|
1336 | memcpy(&uuid.au8[0], "FD", 2);
|
---|
1337 | /* use device name, adjusted to the end of uuid, shortened if necessary */
|
---|
1338 | size_t lenLocation = aLocation.length();
|
---|
1339 | if (lenLocation > 12)
|
---|
1340 | memcpy(&uuid.au8[4], aLocation.c_str() + (lenLocation - 12), 12);
|
---|
1341 | else
|
---|
1342 | memcpy(&uuid.au8[4 + 12 - lenLocation], aLocation.c_str(), lenLocation);
|
---|
1343 | unconst(m->id) = uuid;
|
---|
1344 |
|
---|
1345 | if (aDeviceType == DeviceType_DVD)
|
---|
1346 | m->type = MediumType_Readonly;
|
---|
1347 | else
|
---|
1348 | m->type = MediumType_Writethrough;
|
---|
1349 | m->devType = aDeviceType;
|
---|
1350 | m->state = MediumState_Created;
|
---|
1351 | m->hostDrive = true;
|
---|
1352 | HRESULT rc = setFormat("RAW");
|
---|
1353 | if (FAILED(rc)) return rc;
|
---|
1354 | rc = setLocation(aLocation);
|
---|
1355 | if (FAILED(rc)) return rc;
|
---|
1356 | m->strDescription = aDescription;
|
---|
1357 |
|
---|
1358 | autoInitSpan.setSucceeded();
|
---|
1359 | return S_OK;
|
---|
1360 | }
|
---|
1361 |
|
---|
1362 | /**
|
---|
1363 | * Uninitializes the instance.
|
---|
1364 | *
|
---|
1365 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
1366 | *
|
---|
1367 | * @note All children of this medium get uninitialized by calling their
|
---|
1368 | * uninit() methods.
|
---|
1369 | */
|
---|
1370 | void Medium::uninit()
|
---|
1371 | {
|
---|
1372 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
1373 | AutoUninitSpan autoUninitSpan(this);
|
---|
1374 | if (autoUninitSpan.uninitDone())
|
---|
1375 | return;
|
---|
1376 |
|
---|
1377 | if (!m->formatObj.isNull())
|
---|
1378 | {
|
---|
1379 | /* remove the caller reference we added in setFormat() */
|
---|
1380 | m->formatObj->releaseCaller();
|
---|
1381 | m->formatObj.setNull();
|
---|
1382 | }
|
---|
1383 |
|
---|
1384 | if (m->state == MediumState_Deleting)
|
---|
1385 | {
|
---|
1386 | /* This medium has been already deleted (directly or as part of a
|
---|
1387 | * merge). Reparenting has already been done. */
|
---|
1388 | Assert(m->pParent.isNull());
|
---|
1389 | }
|
---|
1390 | else
|
---|
1391 | {
|
---|
1392 | MediaList::iterator it;
|
---|
1393 | for (it = m->llChildren.begin();
|
---|
1394 | it != m->llChildren.end();
|
---|
1395 | ++it)
|
---|
1396 | {
|
---|
1397 | Medium *pChild = *it;
|
---|
1398 | pChild->m->pParent.setNull();
|
---|
1399 | pChild->uninit();
|
---|
1400 | }
|
---|
1401 | m->llChildren.clear(); // this unsets all the ComPtrs and probably calls delete
|
---|
1402 |
|
---|
1403 | if (m->pParent)
|
---|
1404 | {
|
---|
1405 | // this is a differencing disk: then remove it from the parent's children list
|
---|
1406 | deparent();
|
---|
1407 | }
|
---|
1408 | }
|
---|
1409 |
|
---|
1410 | unconst(m->pVirtualBox) = NULL;
|
---|
1411 | }
|
---|
1412 |
|
---|
1413 | /**
|
---|
1414 | * Internal helper that removes "this" from the list of children of its
|
---|
1415 | * parent. Used in uninit() and other places when reparenting is necessary.
|
---|
1416 | *
|
---|
1417 | * The caller must hold the medium tree lock!
|
---|
1418 | */
|
---|
1419 | void Medium::deparent()
|
---|
1420 | {
|
---|
1421 | MediaList &llParent = m->pParent->m->llChildren;
|
---|
1422 | for (MediaList::iterator it = llParent.begin();
|
---|
1423 | it != llParent.end();
|
---|
1424 | ++it)
|
---|
1425 | {
|
---|
1426 | Medium *pParentsChild = *it;
|
---|
1427 | if (this == pParentsChild)
|
---|
1428 | {
|
---|
1429 | llParent.erase(it);
|
---|
1430 | break;
|
---|
1431 | }
|
---|
1432 | }
|
---|
1433 | m->pParent.setNull();
|
---|
1434 | }
|
---|
1435 |
|
---|
1436 | /**
|
---|
1437 | * Internal helper that removes "this" from the list of children of its
|
---|
1438 | * parent. Used in uninit() and other places when reparenting is necessary.
|
---|
1439 | *
|
---|
1440 | * The caller must hold the medium tree lock!
|
---|
1441 | */
|
---|
1442 | void Medium::setParent(const ComObjPtr<Medium> &pParent)
|
---|
1443 | {
|
---|
1444 | m->pParent = pParent;
|
---|
1445 | if (pParent)
|
---|
1446 | pParent->m->llChildren.push_back(this);
|
---|
1447 | }
|
---|
1448 |
|
---|
1449 |
|
---|
1450 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1451 | //
|
---|
1452 | // IMedium public methods
|
---|
1453 | //
|
---|
1454 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1455 |
|
---|
1456 | STDMETHODIMP Medium::COMGETTER(Id)(BSTR *aId)
|
---|
1457 | {
|
---|
1458 | CheckComArgOutPointerValid(aId);
|
---|
1459 |
|
---|
1460 | AutoCaller autoCaller(this);
|
---|
1461 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1462 |
|
---|
1463 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1464 |
|
---|
1465 | m->id.toUtf16().cloneTo(aId);
|
---|
1466 |
|
---|
1467 | return S_OK;
|
---|
1468 | }
|
---|
1469 |
|
---|
1470 | STDMETHODIMP Medium::COMGETTER(Description)(BSTR *aDescription)
|
---|
1471 | {
|
---|
1472 | CheckComArgOutPointerValid(aDescription);
|
---|
1473 |
|
---|
1474 | AutoCaller autoCaller(this);
|
---|
1475 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1476 |
|
---|
1477 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1478 |
|
---|
1479 | m->strDescription.cloneTo(aDescription);
|
---|
1480 |
|
---|
1481 | return S_OK;
|
---|
1482 | }
|
---|
1483 |
|
---|
1484 | STDMETHODIMP Medium::COMSETTER(Description)(IN_BSTR aDescription)
|
---|
1485 | {
|
---|
1486 | AutoCaller autoCaller(this);
|
---|
1487 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1488 |
|
---|
1489 | // AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1490 |
|
---|
1491 | /// @todo update m->description and save the global registry (and local
|
---|
1492 | /// registries of portable VMs referring to this medium), this will also
|
---|
1493 | /// require to add the mRegistered flag to data
|
---|
1494 |
|
---|
1495 | NOREF(aDescription);
|
---|
1496 |
|
---|
1497 | ReturnComNotImplemented();
|
---|
1498 | }
|
---|
1499 |
|
---|
1500 | STDMETHODIMP Medium::COMGETTER(State)(MediumState_T *aState)
|
---|
1501 | {
|
---|
1502 | CheckComArgOutPointerValid(aState);
|
---|
1503 |
|
---|
1504 | AutoCaller autoCaller(this);
|
---|
1505 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1506 |
|
---|
1507 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1508 | *aState = m->state;
|
---|
1509 |
|
---|
1510 | return S_OK;
|
---|
1511 | }
|
---|
1512 |
|
---|
1513 | STDMETHODIMP Medium::COMGETTER(Variant)(ULONG *aVariant)
|
---|
1514 | {
|
---|
1515 | CheckComArgOutPointerValid(aVariant);
|
---|
1516 |
|
---|
1517 | AutoCaller autoCaller(this);
|
---|
1518 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1519 |
|
---|
1520 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1521 | *aVariant = m->variant;
|
---|
1522 |
|
---|
1523 | return S_OK;
|
---|
1524 | }
|
---|
1525 |
|
---|
1526 |
|
---|
1527 | STDMETHODIMP Medium::COMGETTER(Location)(BSTR *aLocation)
|
---|
1528 | {
|
---|
1529 | CheckComArgOutPointerValid(aLocation);
|
---|
1530 |
|
---|
1531 | AutoCaller autoCaller(this);
|
---|
1532 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1533 |
|
---|
1534 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1535 |
|
---|
1536 | m->strLocationFull.cloneTo(aLocation);
|
---|
1537 |
|
---|
1538 | return S_OK;
|
---|
1539 | }
|
---|
1540 |
|
---|
1541 | STDMETHODIMP Medium::COMSETTER(Location)(IN_BSTR aLocation)
|
---|
1542 | {
|
---|
1543 | CheckComArgStrNotEmptyOrNull(aLocation);
|
---|
1544 |
|
---|
1545 | AutoCaller autoCaller(this);
|
---|
1546 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1547 |
|
---|
1548 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1549 |
|
---|
1550 | /// @todo NEWMEDIA for file names, add the default extension if no extension
|
---|
1551 | /// is present (using the information from the VD backend which also implies
|
---|
1552 | /// that one more parameter should be passed to setLocation() requesting
|
---|
1553 | /// that functionality since it is only allowed when called from this method
|
---|
1554 |
|
---|
1555 | /// @todo NEWMEDIA rename the file and set m->location on success, then save
|
---|
1556 | /// the global registry (and local registries of portable VMs referring to
|
---|
1557 | /// this medium), this will also require to add the mRegistered flag to data
|
---|
1558 |
|
---|
1559 | ReturnComNotImplemented();
|
---|
1560 | }
|
---|
1561 |
|
---|
1562 | STDMETHODIMP Medium::COMGETTER(Name)(BSTR *aName)
|
---|
1563 | {
|
---|
1564 | CheckComArgOutPointerValid(aName);
|
---|
1565 |
|
---|
1566 | AutoCaller autoCaller(this);
|
---|
1567 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1568 |
|
---|
1569 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1570 |
|
---|
1571 | getName().cloneTo(aName);
|
---|
1572 |
|
---|
1573 | return S_OK;
|
---|
1574 | }
|
---|
1575 |
|
---|
1576 | STDMETHODIMP Medium::COMGETTER(DeviceType)(DeviceType_T *aDeviceType)
|
---|
1577 | {
|
---|
1578 | CheckComArgOutPointerValid(aDeviceType);
|
---|
1579 |
|
---|
1580 | AutoCaller autoCaller(this);
|
---|
1581 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1582 |
|
---|
1583 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1584 |
|
---|
1585 | *aDeviceType = m->devType;
|
---|
1586 |
|
---|
1587 | return S_OK;
|
---|
1588 | }
|
---|
1589 |
|
---|
1590 | STDMETHODIMP Medium::COMGETTER(HostDrive)(BOOL *aHostDrive)
|
---|
1591 | {
|
---|
1592 | CheckComArgOutPointerValid(aHostDrive);
|
---|
1593 |
|
---|
1594 | AutoCaller autoCaller(this);
|
---|
1595 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1596 |
|
---|
1597 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1598 |
|
---|
1599 | *aHostDrive = m->hostDrive;
|
---|
1600 |
|
---|
1601 | return S_OK;
|
---|
1602 | }
|
---|
1603 |
|
---|
1604 | STDMETHODIMP Medium::COMGETTER(Size)(LONG64 *aSize)
|
---|
1605 | {
|
---|
1606 | CheckComArgOutPointerValid(aSize);
|
---|
1607 |
|
---|
1608 | AutoCaller autoCaller(this);
|
---|
1609 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1610 |
|
---|
1611 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1612 |
|
---|
1613 | *aSize = m->size;
|
---|
1614 |
|
---|
1615 | return S_OK;
|
---|
1616 | }
|
---|
1617 |
|
---|
1618 | STDMETHODIMP Medium::COMGETTER(Format)(BSTR *aFormat)
|
---|
1619 | {
|
---|
1620 | CheckComArgOutPointerValid(aFormat);
|
---|
1621 |
|
---|
1622 | AutoCaller autoCaller(this);
|
---|
1623 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1624 |
|
---|
1625 | /* no need to lock, m->strFormat is const */
|
---|
1626 | m->strFormat.cloneTo(aFormat);
|
---|
1627 |
|
---|
1628 | return S_OK;
|
---|
1629 | }
|
---|
1630 |
|
---|
1631 | STDMETHODIMP Medium::COMGETTER(MediumFormat)(IMediumFormat **aMediumFormat)
|
---|
1632 | {
|
---|
1633 | CheckComArgOutPointerValid(aMediumFormat);
|
---|
1634 |
|
---|
1635 | AutoCaller autoCaller(this);
|
---|
1636 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1637 |
|
---|
1638 | /* no need to lock, m->formatObj is const */
|
---|
1639 | m->formatObj.queryInterfaceTo(aMediumFormat);
|
---|
1640 |
|
---|
1641 | return S_OK;
|
---|
1642 | }
|
---|
1643 |
|
---|
1644 | STDMETHODIMP Medium::COMGETTER(Type)(MediumType_T *aType)
|
---|
1645 | {
|
---|
1646 | CheckComArgOutPointerValid(aType);
|
---|
1647 |
|
---|
1648 | AutoCaller autoCaller(this);
|
---|
1649 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1650 |
|
---|
1651 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1652 |
|
---|
1653 | *aType = m->type;
|
---|
1654 |
|
---|
1655 | return S_OK;
|
---|
1656 | }
|
---|
1657 |
|
---|
1658 | STDMETHODIMP Medium::COMSETTER(Type)(MediumType_T aType)
|
---|
1659 | {
|
---|
1660 | AutoCaller autoCaller(this);
|
---|
1661 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1662 |
|
---|
1663 | // we access mParent and members
|
---|
1664 | AutoWriteLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1665 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1666 |
|
---|
1667 | switch (m->state)
|
---|
1668 | {
|
---|
1669 | case MediumState_Created:
|
---|
1670 | case MediumState_Inaccessible:
|
---|
1671 | break;
|
---|
1672 | default:
|
---|
1673 | return setStateError();
|
---|
1674 | }
|
---|
1675 |
|
---|
1676 | if (m->type == aType)
|
---|
1677 | {
|
---|
1678 | /* Nothing to do */
|
---|
1679 | return S_OK;
|
---|
1680 | }
|
---|
1681 |
|
---|
1682 | DeviceType_T devType = getDeviceType();
|
---|
1683 | // DVD media can only be readonly.
|
---|
1684 | if (devType == DeviceType_DVD && aType != MediumType_Readonly)
|
---|
1685 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1686 | tr("Cannot change the type of DVD medium '%s'"),
|
---|
1687 | m->strLocationFull.c_str());
|
---|
1688 | // Floppy media can only be writethrough or readonly.
|
---|
1689 | if ( devType == DeviceType_Floppy
|
---|
1690 | && aType != MediumType_Writethrough
|
---|
1691 | && aType != MediumType_Readonly)
|
---|
1692 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1693 | tr("Cannot change the type of floppy medium '%s'"),
|
---|
1694 | m->strLocationFull.c_str());
|
---|
1695 |
|
---|
1696 | /* cannot change the type of a differencing medium */
|
---|
1697 | if (m->pParent)
|
---|
1698 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1699 | tr("Cannot change the type of medium '%s' because it is a differencing medium"),
|
---|
1700 | m->strLocationFull.c_str());
|
---|
1701 |
|
---|
1702 | /* Cannot change the type of a medium being in use by more than one VM.
|
---|
1703 | * If the change is to Immutable or MultiAttach then it must not be
|
---|
1704 | * directly attached to any VM, otherwise the assumptions about indirect
|
---|
1705 | * attachment elsewhere are violated and the VM becomes inaccessible.
|
---|
1706 | * Attaching an immutable medium triggers the diff creation, and this is
|
---|
1707 | * vital for the correct operation. */
|
---|
1708 | if ( m->backRefs.size() > 1
|
---|
1709 | || ( ( aType == MediumType_Immutable
|
---|
1710 | || aType == MediumType_MultiAttach)
|
---|
1711 | && m->backRefs.size() > 0))
|
---|
1712 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1713 | tr("Cannot change the type of medium '%s' because it is attached to %d virtual machines"),
|
---|
1714 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
1715 |
|
---|
1716 | switch (aType)
|
---|
1717 | {
|
---|
1718 | case MediumType_Normal:
|
---|
1719 | case MediumType_Immutable:
|
---|
1720 | case MediumType_MultiAttach:
|
---|
1721 | {
|
---|
1722 | /* normal can be easily converted to immutable and vice versa even
|
---|
1723 | * if they have children as long as they are not attached to any
|
---|
1724 | * machine themselves */
|
---|
1725 | break;
|
---|
1726 | }
|
---|
1727 | case MediumType_Writethrough:
|
---|
1728 | case MediumType_Shareable:
|
---|
1729 | case MediumType_Readonly:
|
---|
1730 | {
|
---|
1731 | /* cannot change to writethrough, shareable or readonly
|
---|
1732 | * if there are children */
|
---|
1733 | if (getChildren().size() != 0)
|
---|
1734 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
1735 | tr("Cannot change type for medium '%s' since it has %d child media"),
|
---|
1736 | m->strLocationFull.c_str(), getChildren().size());
|
---|
1737 | if (aType == MediumType_Shareable)
|
---|
1738 | {
|
---|
1739 | MediumVariant_T variant = getVariant();
|
---|
1740 | if (!(variant & MediumVariant_Fixed))
|
---|
1741 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1742 | tr("Cannot change type for medium '%s' to 'Shareable' since it is a dynamic medium storage unit"),
|
---|
1743 | m->strLocationFull.c_str());
|
---|
1744 | }
|
---|
1745 | else if (aType == MediumType_Readonly && devType == DeviceType_HardDisk)
|
---|
1746 | {
|
---|
1747 | // Readonly hard disks are not allowed, this medium type is reserved for
|
---|
1748 | // DVDs and floppy images at the moment. Later we might allow readonly hard
|
---|
1749 | // disks, but that's extremely unusual and many guest OSes will have trouble.
|
---|
1750 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1751 | tr("Cannot change type for medium '%s' to 'Readonly' since it is a hard disk"),
|
---|
1752 | m->strLocationFull.c_str());
|
---|
1753 | }
|
---|
1754 | break;
|
---|
1755 | }
|
---|
1756 | default:
|
---|
1757 | AssertFailedReturn(E_FAIL);
|
---|
1758 | }
|
---|
1759 |
|
---|
1760 | if (aType == MediumType_MultiAttach)
|
---|
1761 | {
|
---|
1762 | // This type is new with VirtualBox 4.0 and therefore requires settings
|
---|
1763 | // version 1.11 in the settings backend. Unfortunately it is not enough to do
|
---|
1764 | // the usual routine in MachineConfigFile::bumpSettingsVersionIfNeeded() for
|
---|
1765 | // two reasons: The medium type is a property of the media registry tree, which
|
---|
1766 | // can reside in the global config file (for pre-4.0 media); we would therefore
|
---|
1767 | // possibly need to bump the global config version. We don't want to do that though
|
---|
1768 | // because that might make downgrading to pre-4.0 impossible.
|
---|
1769 | // As a result, we can only use these two new types if the medium is NOT in the
|
---|
1770 | // global registry:
|
---|
1771 | const Guid &uuidGlobalRegistry = m->pVirtualBox->getGlobalRegistryId();
|
---|
1772 | if (isInRegistry(uuidGlobalRegistry))
|
---|
1773 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1774 | tr("Cannot change type for medium '%s': the media type 'MultiAttach' can only be used "
|
---|
1775 | "on media registered with a machine that was created with VirtualBox 4.0 or later"),
|
---|
1776 | m->strLocationFull.c_str());
|
---|
1777 | }
|
---|
1778 |
|
---|
1779 | m->type = aType;
|
---|
1780 |
|
---|
1781 | // save the settings
|
---|
1782 | mlock.release();
|
---|
1783 | treeLock.release();
|
---|
1784 | markRegistriesModified();
|
---|
1785 | m->pVirtualBox->saveModifiedRegistries();
|
---|
1786 |
|
---|
1787 | return S_OK;
|
---|
1788 | }
|
---|
1789 |
|
---|
1790 | STDMETHODIMP Medium::COMGETTER(AllowedTypes)(ComSafeArrayOut(MediumType_T, aAllowedTypes))
|
---|
1791 | {
|
---|
1792 | CheckComArgOutSafeArrayPointerValid(aAllowedTypes);
|
---|
1793 | NOREF(aAllowedTypes);
|
---|
1794 | #ifndef RT_OS_WINDOWS
|
---|
1795 | NOREF(aAllowedTypesSize);
|
---|
1796 | #endif
|
---|
1797 |
|
---|
1798 | AutoCaller autoCaller(this);
|
---|
1799 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1800 |
|
---|
1801 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1802 |
|
---|
1803 | ReturnComNotImplemented();
|
---|
1804 | }
|
---|
1805 |
|
---|
1806 | STDMETHODIMP Medium::COMGETTER(Parent)(IMedium **aParent)
|
---|
1807 | {
|
---|
1808 | CheckComArgOutPointerValid(aParent);
|
---|
1809 |
|
---|
1810 | AutoCaller autoCaller(this);
|
---|
1811 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1812 |
|
---|
1813 | /* we access mParent */
|
---|
1814 | AutoReadLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1815 |
|
---|
1816 | m->pParent.queryInterfaceTo(aParent);
|
---|
1817 |
|
---|
1818 | return S_OK;
|
---|
1819 | }
|
---|
1820 |
|
---|
1821 | STDMETHODIMP Medium::COMGETTER(Children)(ComSafeArrayOut(IMedium *, aChildren))
|
---|
1822 | {
|
---|
1823 | CheckComArgOutSafeArrayPointerValid(aChildren);
|
---|
1824 |
|
---|
1825 | AutoCaller autoCaller(this);
|
---|
1826 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1827 |
|
---|
1828 | /* we access children */
|
---|
1829 | AutoReadLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1830 |
|
---|
1831 | SafeIfaceArray<IMedium> children(this->getChildren());
|
---|
1832 | children.detachTo(ComSafeArrayOutArg(aChildren));
|
---|
1833 |
|
---|
1834 | return S_OK;
|
---|
1835 | }
|
---|
1836 |
|
---|
1837 | STDMETHODIMP Medium::COMGETTER(Base)(IMedium **aBase)
|
---|
1838 | {
|
---|
1839 | CheckComArgOutPointerValid(aBase);
|
---|
1840 |
|
---|
1841 | /* base() will do callers/locking */
|
---|
1842 |
|
---|
1843 | getBase().queryInterfaceTo(aBase);
|
---|
1844 |
|
---|
1845 | return S_OK;
|
---|
1846 | }
|
---|
1847 |
|
---|
1848 | STDMETHODIMP Medium::COMGETTER(ReadOnly)(BOOL *aReadOnly)
|
---|
1849 | {
|
---|
1850 | CheckComArgOutPointerValid(aReadOnly);
|
---|
1851 |
|
---|
1852 | AutoCaller autoCaller(this);
|
---|
1853 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1854 |
|
---|
1855 | /* isReadOnly() will do locking */
|
---|
1856 |
|
---|
1857 | *aReadOnly = isReadOnly();
|
---|
1858 |
|
---|
1859 | return S_OK;
|
---|
1860 | }
|
---|
1861 |
|
---|
1862 | STDMETHODIMP Medium::COMGETTER(LogicalSize)(LONG64 *aLogicalSize)
|
---|
1863 | {
|
---|
1864 | CheckComArgOutPointerValid(aLogicalSize);
|
---|
1865 |
|
---|
1866 | {
|
---|
1867 | AutoCaller autoCaller(this);
|
---|
1868 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1869 |
|
---|
1870 | /* we access mParent */
|
---|
1871 | AutoReadLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1872 |
|
---|
1873 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1874 |
|
---|
1875 | if (m->pParent.isNull())
|
---|
1876 | {
|
---|
1877 | *aLogicalSize = m->logicalSize;
|
---|
1878 |
|
---|
1879 | return S_OK;
|
---|
1880 | }
|
---|
1881 | }
|
---|
1882 |
|
---|
1883 | /* We assume that some backend may decide to return a meaningless value in
|
---|
1884 | * response to VDGetSize() for differencing media and therefore always
|
---|
1885 | * ask the base medium ourselves. */
|
---|
1886 |
|
---|
1887 | /* base() will do callers/locking */
|
---|
1888 |
|
---|
1889 | return getBase()->COMGETTER(LogicalSize)(aLogicalSize);
|
---|
1890 | }
|
---|
1891 |
|
---|
1892 | STDMETHODIMP Medium::COMGETTER(AutoReset)(BOOL *aAutoReset)
|
---|
1893 | {
|
---|
1894 | CheckComArgOutPointerValid(aAutoReset);
|
---|
1895 |
|
---|
1896 | AutoCaller autoCaller(this);
|
---|
1897 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1898 |
|
---|
1899 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1900 |
|
---|
1901 | if (m->pParent.isNull())
|
---|
1902 | *aAutoReset = FALSE;
|
---|
1903 | else
|
---|
1904 | *aAutoReset = m->autoReset;
|
---|
1905 |
|
---|
1906 | return S_OK;
|
---|
1907 | }
|
---|
1908 |
|
---|
1909 | STDMETHODIMP Medium::COMSETTER(AutoReset)(BOOL aAutoReset)
|
---|
1910 | {
|
---|
1911 | AutoCaller autoCaller(this);
|
---|
1912 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1913 |
|
---|
1914 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1915 |
|
---|
1916 | if (m->pParent.isNull())
|
---|
1917 | return setError(VBOX_E_NOT_SUPPORTED,
|
---|
1918 | tr("Medium '%s' is not differencing"),
|
---|
1919 | m->strLocationFull.c_str());
|
---|
1920 |
|
---|
1921 | if (m->autoReset != !!aAutoReset)
|
---|
1922 | {
|
---|
1923 | m->autoReset = !!aAutoReset;
|
---|
1924 |
|
---|
1925 | // save the settings
|
---|
1926 | mlock.release();
|
---|
1927 | markRegistriesModified();
|
---|
1928 | m->pVirtualBox->saveModifiedRegistries();
|
---|
1929 | }
|
---|
1930 |
|
---|
1931 | return S_OK;
|
---|
1932 | }
|
---|
1933 |
|
---|
1934 | STDMETHODIMP Medium::COMGETTER(LastAccessError)(BSTR *aLastAccessError)
|
---|
1935 | {
|
---|
1936 | CheckComArgOutPointerValid(aLastAccessError);
|
---|
1937 |
|
---|
1938 | AutoCaller autoCaller(this);
|
---|
1939 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1940 |
|
---|
1941 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1942 |
|
---|
1943 | m->strLastAccessError.cloneTo(aLastAccessError);
|
---|
1944 |
|
---|
1945 | return S_OK;
|
---|
1946 | }
|
---|
1947 |
|
---|
1948 | STDMETHODIMP Medium::COMGETTER(MachineIds)(ComSafeArrayOut(BSTR,aMachineIds))
|
---|
1949 | {
|
---|
1950 | CheckComArgOutSafeArrayPointerValid(aMachineIds);
|
---|
1951 |
|
---|
1952 | AutoCaller autoCaller(this);
|
---|
1953 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1954 |
|
---|
1955 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1956 |
|
---|
1957 | com::SafeArray<BSTR> machineIds;
|
---|
1958 |
|
---|
1959 | if (m->backRefs.size() != 0)
|
---|
1960 | {
|
---|
1961 | machineIds.reset(m->backRefs.size());
|
---|
1962 |
|
---|
1963 | size_t i = 0;
|
---|
1964 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
1965 | it != m->backRefs.end(); ++it, ++i)
|
---|
1966 | {
|
---|
1967 | it->machineId.toUtf16().detachTo(&machineIds[i]);
|
---|
1968 | }
|
---|
1969 | }
|
---|
1970 |
|
---|
1971 | machineIds.detachTo(ComSafeArrayOutArg(aMachineIds));
|
---|
1972 |
|
---|
1973 | return S_OK;
|
---|
1974 | }
|
---|
1975 |
|
---|
1976 | STDMETHODIMP Medium::SetIds(BOOL aSetImageId,
|
---|
1977 | IN_BSTR aImageId,
|
---|
1978 | BOOL aSetParentId,
|
---|
1979 | IN_BSTR aParentId)
|
---|
1980 | {
|
---|
1981 | AutoCaller autoCaller(this);
|
---|
1982 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1983 |
|
---|
1984 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1985 |
|
---|
1986 | switch (m->state)
|
---|
1987 | {
|
---|
1988 | case MediumState_Created:
|
---|
1989 | break;
|
---|
1990 | default:
|
---|
1991 | return setStateError();
|
---|
1992 | }
|
---|
1993 |
|
---|
1994 | Guid imageId, parentId;
|
---|
1995 | if (aSetImageId)
|
---|
1996 | {
|
---|
1997 | if (Bstr(aImageId).isEmpty())
|
---|
1998 | imageId.create();
|
---|
1999 | else
|
---|
2000 | {
|
---|
2001 | imageId = Guid(aImageId);
|
---|
2002 | if (imageId.isEmpty())
|
---|
2003 | return setError(E_INVALIDARG, tr("Argument %s is empty"), "aImageId");
|
---|
2004 | }
|
---|
2005 | }
|
---|
2006 | if (aSetParentId)
|
---|
2007 | {
|
---|
2008 | if (Bstr(aParentId).isEmpty())
|
---|
2009 | parentId.create();
|
---|
2010 | else
|
---|
2011 | parentId = Guid(aParentId);
|
---|
2012 | }
|
---|
2013 |
|
---|
2014 | unconst(m->uuidImage) = imageId;
|
---|
2015 | unconst(m->uuidParentImage) = parentId;
|
---|
2016 |
|
---|
2017 | // must not hold any locks before calling Medium::queryInfo
|
---|
2018 | alock.release();
|
---|
2019 |
|
---|
2020 | HRESULT rc = queryInfo(!!aSetImageId /* fSetImageId */,
|
---|
2021 | !!aSetParentId /* fSetParentId */);
|
---|
2022 |
|
---|
2023 | return rc;
|
---|
2024 | }
|
---|
2025 |
|
---|
2026 | STDMETHODIMP Medium::RefreshState(MediumState_T *aState)
|
---|
2027 | {
|
---|
2028 | CheckComArgOutPointerValid(aState);
|
---|
2029 |
|
---|
2030 | AutoCaller autoCaller(this);
|
---|
2031 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2032 |
|
---|
2033 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2034 |
|
---|
2035 | HRESULT rc = S_OK;
|
---|
2036 |
|
---|
2037 | switch (m->state)
|
---|
2038 | {
|
---|
2039 | case MediumState_Created:
|
---|
2040 | case MediumState_Inaccessible:
|
---|
2041 | case MediumState_LockedRead:
|
---|
2042 | {
|
---|
2043 | // must not hold any locks before calling Medium::queryInfo
|
---|
2044 | alock.release();
|
---|
2045 |
|
---|
2046 | rc = queryInfo(false /* fSetImageId */, false /* fSetParentId */);
|
---|
2047 |
|
---|
2048 | alock.acquire();
|
---|
2049 | break;
|
---|
2050 | }
|
---|
2051 | default:
|
---|
2052 | break;
|
---|
2053 | }
|
---|
2054 |
|
---|
2055 | *aState = m->state;
|
---|
2056 |
|
---|
2057 | return rc;
|
---|
2058 | }
|
---|
2059 |
|
---|
2060 | STDMETHODIMP Medium::GetSnapshotIds(IN_BSTR aMachineId,
|
---|
2061 | ComSafeArrayOut(BSTR, aSnapshotIds))
|
---|
2062 | {
|
---|
2063 | CheckComArgExpr(aMachineId, Guid(aMachineId).isEmpty() == false);
|
---|
2064 | CheckComArgOutSafeArrayPointerValid(aSnapshotIds);
|
---|
2065 |
|
---|
2066 | AutoCaller autoCaller(this);
|
---|
2067 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2068 |
|
---|
2069 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2070 |
|
---|
2071 | com::SafeArray<BSTR> snapshotIds;
|
---|
2072 |
|
---|
2073 | Guid id(aMachineId);
|
---|
2074 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
2075 | it != m->backRefs.end(); ++it)
|
---|
2076 | {
|
---|
2077 | if (it->machineId == id)
|
---|
2078 | {
|
---|
2079 | size_t size = it->llSnapshotIds.size();
|
---|
2080 |
|
---|
2081 | /* if the medium is attached to the machine in the current state, we
|
---|
2082 | * return its ID as the first element of the array */
|
---|
2083 | if (it->fInCurState)
|
---|
2084 | ++size;
|
---|
2085 |
|
---|
2086 | if (size > 0)
|
---|
2087 | {
|
---|
2088 | snapshotIds.reset(size);
|
---|
2089 |
|
---|
2090 | size_t j = 0;
|
---|
2091 | if (it->fInCurState)
|
---|
2092 | it->machineId.toUtf16().detachTo(&snapshotIds[j++]);
|
---|
2093 |
|
---|
2094 | for (GuidList::const_iterator jt = it->llSnapshotIds.begin();
|
---|
2095 | jt != it->llSnapshotIds.end();
|
---|
2096 | ++jt, ++j)
|
---|
2097 | {
|
---|
2098 | (*jt).toUtf16().detachTo(&snapshotIds[j]);
|
---|
2099 | }
|
---|
2100 | }
|
---|
2101 |
|
---|
2102 | break;
|
---|
2103 | }
|
---|
2104 | }
|
---|
2105 |
|
---|
2106 | snapshotIds.detachTo(ComSafeArrayOutArg(aSnapshotIds));
|
---|
2107 |
|
---|
2108 | return S_OK;
|
---|
2109 | }
|
---|
2110 |
|
---|
2111 | /**
|
---|
2112 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
2113 | * in-process calls).
|
---|
2114 | */
|
---|
2115 | STDMETHODIMP Medium::LockRead(MediumState_T *aState)
|
---|
2116 | {
|
---|
2117 | AutoCaller autoCaller(this);
|
---|
2118 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2119 |
|
---|
2120 | /* Must not hold the object lock, as we need control over it below. */
|
---|
2121 | Assert(!isWriteLockOnCurrentThread());
|
---|
2122 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2123 |
|
---|
2124 | /* Wait for a concurrently running Medium::queryInfo to complete. */
|
---|
2125 | if (m->queryInfoRunning)
|
---|
2126 | {
|
---|
2127 | /* Must not hold the media tree lock, as Medium::queryInfo needs this
|
---|
2128 | * lock and thus we would run into a deadlock here. */
|
---|
2129 | Assert(!m->pVirtualBox->getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
2130 | while (m->queryInfoRunning)
|
---|
2131 | {
|
---|
2132 | alock.release();
|
---|
2133 | {
|
---|
2134 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
2135 | }
|
---|
2136 | alock.acquire();
|
---|
2137 | }
|
---|
2138 | }
|
---|
2139 |
|
---|
2140 | /* return the current state before */
|
---|
2141 | if (aState)
|
---|
2142 | *aState = m->state;
|
---|
2143 |
|
---|
2144 | HRESULT rc = S_OK;
|
---|
2145 |
|
---|
2146 | switch (m->state)
|
---|
2147 | {
|
---|
2148 | case MediumState_Created:
|
---|
2149 | case MediumState_Inaccessible:
|
---|
2150 | case MediumState_LockedRead:
|
---|
2151 | {
|
---|
2152 | ++m->readers;
|
---|
2153 |
|
---|
2154 | ComAssertMsgBreak(m->readers != 0, ("Counter overflow"), rc = E_FAIL);
|
---|
2155 |
|
---|
2156 | /* Remember pre-lock state */
|
---|
2157 | if (m->state != MediumState_LockedRead)
|
---|
2158 | m->preLockState = m->state;
|
---|
2159 |
|
---|
2160 | LogFlowThisFunc(("Okay - prev state=%d readers=%d\n", m->state, m->readers));
|
---|
2161 | m->state = MediumState_LockedRead;
|
---|
2162 |
|
---|
2163 | break;
|
---|
2164 | }
|
---|
2165 | default:
|
---|
2166 | {
|
---|
2167 | LogFlowThisFunc(("Failing - state=%d\n", m->state));
|
---|
2168 | rc = setStateError();
|
---|
2169 | break;
|
---|
2170 | }
|
---|
2171 | }
|
---|
2172 |
|
---|
2173 | return rc;
|
---|
2174 | }
|
---|
2175 |
|
---|
2176 | /**
|
---|
2177 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
2178 | * in-process calls).
|
---|
2179 | */
|
---|
2180 | STDMETHODIMP Medium::UnlockRead(MediumState_T *aState)
|
---|
2181 | {
|
---|
2182 | AutoCaller autoCaller(this);
|
---|
2183 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2184 |
|
---|
2185 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2186 |
|
---|
2187 | HRESULT rc = S_OK;
|
---|
2188 |
|
---|
2189 | switch (m->state)
|
---|
2190 | {
|
---|
2191 | case MediumState_LockedRead:
|
---|
2192 | {
|
---|
2193 | ComAssertMsgBreak(m->readers != 0, ("Counter underflow"), rc = E_FAIL);
|
---|
2194 | --m->readers;
|
---|
2195 |
|
---|
2196 | /* Reset the state after the last reader */
|
---|
2197 | if (m->readers == 0)
|
---|
2198 | {
|
---|
2199 | m->state = m->preLockState;
|
---|
2200 | /* There are cases where we inject the deleting state into
|
---|
2201 | * a medium locked for reading. Make sure #unmarkForDeletion()
|
---|
2202 | * gets the right state afterwards. */
|
---|
2203 | if (m->preLockState == MediumState_Deleting)
|
---|
2204 | m->preLockState = MediumState_Created;
|
---|
2205 | }
|
---|
2206 |
|
---|
2207 | LogFlowThisFunc(("new state=%d\n", m->state));
|
---|
2208 | break;
|
---|
2209 | }
|
---|
2210 | default:
|
---|
2211 | {
|
---|
2212 | LogFlowThisFunc(("Failing - state=%d\n", m->state));
|
---|
2213 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2214 | tr("Medium '%s' is not locked for reading"),
|
---|
2215 | m->strLocationFull.c_str());
|
---|
2216 | break;
|
---|
2217 | }
|
---|
2218 | }
|
---|
2219 |
|
---|
2220 | /* return the current state after */
|
---|
2221 | if (aState)
|
---|
2222 | *aState = m->state;
|
---|
2223 |
|
---|
2224 | return rc;
|
---|
2225 | }
|
---|
2226 |
|
---|
2227 | /**
|
---|
2228 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
2229 | * in-process calls).
|
---|
2230 | */
|
---|
2231 | STDMETHODIMP Medium::LockWrite(MediumState_T *aState)
|
---|
2232 | {
|
---|
2233 | AutoCaller autoCaller(this);
|
---|
2234 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2235 |
|
---|
2236 | /* Must not hold the object lock, as we need control over it below. */
|
---|
2237 | Assert(!isWriteLockOnCurrentThread());
|
---|
2238 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2239 |
|
---|
2240 | /* Wait for a concurrently running Medium::queryInfo to complete. */
|
---|
2241 | if (m->queryInfoRunning)
|
---|
2242 | {
|
---|
2243 | /* Must not hold the media tree lock, as Medium::queryInfo needs this
|
---|
2244 | * lock and thus we would run into a deadlock here. */
|
---|
2245 | Assert(!m->pVirtualBox->getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
2246 | while (m->queryInfoRunning)
|
---|
2247 | {
|
---|
2248 | alock.release();
|
---|
2249 | {
|
---|
2250 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
2251 | }
|
---|
2252 | alock.acquire();
|
---|
2253 | }
|
---|
2254 | }
|
---|
2255 |
|
---|
2256 | /* return the current state before */
|
---|
2257 | if (aState)
|
---|
2258 | *aState = m->state;
|
---|
2259 |
|
---|
2260 | HRESULT rc = S_OK;
|
---|
2261 |
|
---|
2262 | switch (m->state)
|
---|
2263 | {
|
---|
2264 | case MediumState_Created:
|
---|
2265 | case MediumState_Inaccessible:
|
---|
2266 | {
|
---|
2267 | m->preLockState = m->state;
|
---|
2268 |
|
---|
2269 | LogFlowThisFunc(("Okay - prev state=%d locationFull=%s\n", m->state, getLocationFull().c_str()));
|
---|
2270 | m->state = MediumState_LockedWrite;
|
---|
2271 | break;
|
---|
2272 | }
|
---|
2273 | default:
|
---|
2274 | {
|
---|
2275 | LogFlowThisFunc(("Failing - state=%d locationFull=%s\n", m->state, getLocationFull().c_str()));
|
---|
2276 | rc = setStateError();
|
---|
2277 | break;
|
---|
2278 | }
|
---|
2279 | }
|
---|
2280 |
|
---|
2281 | return rc;
|
---|
2282 | }
|
---|
2283 |
|
---|
2284 | /**
|
---|
2285 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
2286 | * in-process calls).
|
---|
2287 | */
|
---|
2288 | STDMETHODIMP Medium::UnlockWrite(MediumState_T *aState)
|
---|
2289 | {
|
---|
2290 | AutoCaller autoCaller(this);
|
---|
2291 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2292 |
|
---|
2293 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2294 |
|
---|
2295 | HRESULT rc = S_OK;
|
---|
2296 |
|
---|
2297 | switch (m->state)
|
---|
2298 | {
|
---|
2299 | case MediumState_LockedWrite:
|
---|
2300 | {
|
---|
2301 | m->state = m->preLockState;
|
---|
2302 | /* There are cases where we inject the deleting state into
|
---|
2303 | * a medium locked for writing. Make sure #unmarkForDeletion()
|
---|
2304 | * gets the right state afterwards. */
|
---|
2305 | if (m->preLockState == MediumState_Deleting)
|
---|
2306 | m->preLockState = MediumState_Created;
|
---|
2307 | LogFlowThisFunc(("new state=%d locationFull=%s\n", m->state, getLocationFull().c_str()));
|
---|
2308 | break;
|
---|
2309 | }
|
---|
2310 | default:
|
---|
2311 | {
|
---|
2312 | LogFlowThisFunc(("Failing - state=%d locationFull=%s\n", m->state, getLocationFull().c_str()));
|
---|
2313 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2314 | tr("Medium '%s' is not locked for writing"),
|
---|
2315 | m->strLocationFull.c_str());
|
---|
2316 | break;
|
---|
2317 | }
|
---|
2318 | }
|
---|
2319 |
|
---|
2320 | /* return the current state after */
|
---|
2321 | if (aState)
|
---|
2322 | *aState = m->state;
|
---|
2323 |
|
---|
2324 | return rc;
|
---|
2325 | }
|
---|
2326 |
|
---|
2327 | STDMETHODIMP Medium::Close()
|
---|
2328 | {
|
---|
2329 | AutoCaller autoCaller(this);
|
---|
2330 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2331 |
|
---|
2332 | // make a copy of VirtualBox pointer which gets nulled by uninit()
|
---|
2333 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
2334 |
|
---|
2335 | MultiResult mrc = close(autoCaller);
|
---|
2336 |
|
---|
2337 | pVirtualBox->saveModifiedRegistries();
|
---|
2338 |
|
---|
2339 | return mrc;
|
---|
2340 | }
|
---|
2341 |
|
---|
2342 | STDMETHODIMP Medium::GetProperty(IN_BSTR aName, BSTR *aValue)
|
---|
2343 | {
|
---|
2344 | CheckComArgStrNotEmptyOrNull(aName);
|
---|
2345 | CheckComArgOutPointerValid(aValue);
|
---|
2346 |
|
---|
2347 | AutoCaller autoCaller(this);
|
---|
2348 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2349 |
|
---|
2350 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2351 |
|
---|
2352 | settings::StringsMap::const_iterator it = m->mapProperties.find(Utf8Str(aName));
|
---|
2353 | if (it == m->mapProperties.end())
|
---|
2354 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2355 | tr("Property '%ls' does not exist"), aName);
|
---|
2356 |
|
---|
2357 | it->second.cloneTo(aValue);
|
---|
2358 |
|
---|
2359 | return S_OK;
|
---|
2360 | }
|
---|
2361 |
|
---|
2362 | STDMETHODIMP Medium::SetProperty(IN_BSTR aName, IN_BSTR aValue)
|
---|
2363 | {
|
---|
2364 | CheckComArgStrNotEmptyOrNull(aName);
|
---|
2365 |
|
---|
2366 | AutoCaller autoCaller(this);
|
---|
2367 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2368 |
|
---|
2369 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2370 |
|
---|
2371 | switch (m->state)
|
---|
2372 | {
|
---|
2373 | case MediumState_Created:
|
---|
2374 | case MediumState_Inaccessible:
|
---|
2375 | break;
|
---|
2376 | default:
|
---|
2377 | return setStateError();
|
---|
2378 | }
|
---|
2379 |
|
---|
2380 | settings::StringsMap::iterator it = m->mapProperties.find(Utf8Str(aName));
|
---|
2381 | if (it == m->mapProperties.end())
|
---|
2382 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2383 | tr("Property '%ls' does not exist"),
|
---|
2384 | aName);
|
---|
2385 |
|
---|
2386 | it->second = aValue;
|
---|
2387 |
|
---|
2388 | // save the settings
|
---|
2389 | mlock.release();
|
---|
2390 | markRegistriesModified();
|
---|
2391 | m->pVirtualBox->saveModifiedRegistries();
|
---|
2392 |
|
---|
2393 | return S_OK;
|
---|
2394 | }
|
---|
2395 |
|
---|
2396 | STDMETHODIMP Medium::GetProperties(IN_BSTR aNames,
|
---|
2397 | ComSafeArrayOut(BSTR, aReturnNames),
|
---|
2398 | ComSafeArrayOut(BSTR, aReturnValues))
|
---|
2399 | {
|
---|
2400 | CheckComArgOutSafeArrayPointerValid(aReturnNames);
|
---|
2401 | CheckComArgOutSafeArrayPointerValid(aReturnValues);
|
---|
2402 |
|
---|
2403 | AutoCaller autoCaller(this);
|
---|
2404 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2405 |
|
---|
2406 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2407 |
|
---|
2408 | /// @todo make use of aNames according to the documentation
|
---|
2409 | NOREF(aNames);
|
---|
2410 |
|
---|
2411 | com::SafeArray<BSTR> names(m->mapProperties.size());
|
---|
2412 | com::SafeArray<BSTR> values(m->mapProperties.size());
|
---|
2413 | size_t i = 0;
|
---|
2414 |
|
---|
2415 | for (settings::StringsMap::const_iterator it = m->mapProperties.begin();
|
---|
2416 | it != m->mapProperties.end();
|
---|
2417 | ++it)
|
---|
2418 | {
|
---|
2419 | it->first.cloneTo(&names[i]);
|
---|
2420 | it->second.cloneTo(&values[i]);
|
---|
2421 | ++i;
|
---|
2422 | }
|
---|
2423 |
|
---|
2424 | names.detachTo(ComSafeArrayOutArg(aReturnNames));
|
---|
2425 | values.detachTo(ComSafeArrayOutArg(aReturnValues));
|
---|
2426 |
|
---|
2427 | return S_OK;
|
---|
2428 | }
|
---|
2429 |
|
---|
2430 | STDMETHODIMP Medium::SetProperties(ComSafeArrayIn(IN_BSTR, aNames),
|
---|
2431 | ComSafeArrayIn(IN_BSTR, aValues))
|
---|
2432 | {
|
---|
2433 | CheckComArgSafeArrayNotNull(aNames);
|
---|
2434 | CheckComArgSafeArrayNotNull(aValues);
|
---|
2435 |
|
---|
2436 | AutoCaller autoCaller(this);
|
---|
2437 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2438 |
|
---|
2439 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2440 |
|
---|
2441 | com::SafeArray<IN_BSTR> names(ComSafeArrayInArg(aNames));
|
---|
2442 | com::SafeArray<IN_BSTR> values(ComSafeArrayInArg(aValues));
|
---|
2443 |
|
---|
2444 | /* first pass: validate names */
|
---|
2445 | for (size_t i = 0;
|
---|
2446 | i < names.size();
|
---|
2447 | ++i)
|
---|
2448 | {
|
---|
2449 | if (m->mapProperties.find(Utf8Str(names[i])) == m->mapProperties.end())
|
---|
2450 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2451 | tr("Property '%ls' does not exist"), names[i]);
|
---|
2452 | }
|
---|
2453 |
|
---|
2454 | /* second pass: assign */
|
---|
2455 | for (size_t i = 0;
|
---|
2456 | i < names.size();
|
---|
2457 | ++i)
|
---|
2458 | {
|
---|
2459 | settings::StringsMap::iterator it = m->mapProperties.find(Utf8Str(names[i]));
|
---|
2460 | AssertReturn(it != m->mapProperties.end(), E_FAIL);
|
---|
2461 |
|
---|
2462 | it->second = Utf8Str(values[i]);
|
---|
2463 | }
|
---|
2464 |
|
---|
2465 | // save the settings
|
---|
2466 | mlock.release();
|
---|
2467 | markRegistriesModified();
|
---|
2468 | m->pVirtualBox->saveModifiedRegistries();
|
---|
2469 |
|
---|
2470 | return S_OK;
|
---|
2471 | }
|
---|
2472 |
|
---|
2473 | STDMETHODIMP Medium::CreateBaseStorage(LONG64 aLogicalSize,
|
---|
2474 | ULONG aVariant,
|
---|
2475 | IProgress **aProgress)
|
---|
2476 | {
|
---|
2477 | CheckComArgOutPointerValid(aProgress);
|
---|
2478 | if (aLogicalSize < 0)
|
---|
2479 | return setError(E_INVALIDARG, tr("The medium size argument (%lld) is negative"), aLogicalSize);
|
---|
2480 |
|
---|
2481 | AutoCaller autoCaller(this);
|
---|
2482 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2483 |
|
---|
2484 | HRESULT rc = S_OK;
|
---|
2485 | ComObjPtr <Progress> pProgress;
|
---|
2486 | Medium::Task *pTask = NULL;
|
---|
2487 |
|
---|
2488 | try
|
---|
2489 | {
|
---|
2490 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2491 |
|
---|
2492 | aVariant = (MediumVariant_T)((unsigned)aVariant & (unsigned)~MediumVariant_Diff);
|
---|
2493 | if ( !(aVariant & MediumVariant_Fixed)
|
---|
2494 | && !(m->formatObj->getCapabilities() & MediumFormatCapabilities_CreateDynamic))
|
---|
2495 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2496 | tr("Medium format '%s' does not support dynamic storage creation"),
|
---|
2497 | m->strFormat.c_str());
|
---|
2498 | if ( (aVariant & MediumVariant_Fixed)
|
---|
2499 | && !(m->formatObj->getCapabilities() & MediumFormatCapabilities_CreateDynamic))
|
---|
2500 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2501 | tr("Medium format '%s' does not support fixed storage creation"),
|
---|
2502 | m->strFormat.c_str());
|
---|
2503 |
|
---|
2504 | if (m->state != MediumState_NotCreated)
|
---|
2505 | throw setStateError();
|
---|
2506 |
|
---|
2507 | pProgress.createObject();
|
---|
2508 | rc = pProgress->init(m->pVirtualBox,
|
---|
2509 | static_cast<IMedium*>(this),
|
---|
2510 | (aVariant & MediumVariant_Fixed)
|
---|
2511 | ? BstrFmt(tr("Creating fixed medium storage unit '%s'"), m->strLocationFull.c_str()).raw()
|
---|
2512 | : BstrFmt(tr("Creating dynamic medium storage unit '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
2513 | TRUE /* aCancelable */);
|
---|
2514 | if (FAILED(rc))
|
---|
2515 | throw rc;
|
---|
2516 |
|
---|
2517 | /* setup task object to carry out the operation asynchronously */
|
---|
2518 | pTask = new Medium::CreateBaseTask(this, pProgress, aLogicalSize,
|
---|
2519 | (MediumVariant_T)aVariant);
|
---|
2520 | rc = pTask->rc();
|
---|
2521 | AssertComRC(rc);
|
---|
2522 | if (FAILED(rc))
|
---|
2523 | throw rc;
|
---|
2524 |
|
---|
2525 | m->state = MediumState_Creating;
|
---|
2526 | }
|
---|
2527 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2528 |
|
---|
2529 | if (SUCCEEDED(rc))
|
---|
2530 | {
|
---|
2531 | rc = startThread(pTask);
|
---|
2532 |
|
---|
2533 | if (SUCCEEDED(rc))
|
---|
2534 | pProgress.queryInterfaceTo(aProgress);
|
---|
2535 | }
|
---|
2536 | else if (pTask != NULL)
|
---|
2537 | delete pTask;
|
---|
2538 |
|
---|
2539 | return rc;
|
---|
2540 | }
|
---|
2541 |
|
---|
2542 | STDMETHODIMP Medium::DeleteStorage(IProgress **aProgress)
|
---|
2543 | {
|
---|
2544 | CheckComArgOutPointerValid(aProgress);
|
---|
2545 |
|
---|
2546 | AutoCaller autoCaller(this);
|
---|
2547 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2548 |
|
---|
2549 | ComObjPtr<Progress> pProgress;
|
---|
2550 |
|
---|
2551 | MultiResult mrc = deleteStorage(&pProgress,
|
---|
2552 | false /* aWait */);
|
---|
2553 | /* Must save the registries in any case, since an entry was removed. */
|
---|
2554 | m->pVirtualBox->saveModifiedRegistries();
|
---|
2555 |
|
---|
2556 | if (SUCCEEDED(mrc))
|
---|
2557 | pProgress.queryInterfaceTo(aProgress);
|
---|
2558 |
|
---|
2559 | return mrc;
|
---|
2560 | }
|
---|
2561 |
|
---|
2562 | STDMETHODIMP Medium::CreateDiffStorage(IMedium *aTarget,
|
---|
2563 | ULONG aVariant,
|
---|
2564 | IProgress **aProgress)
|
---|
2565 | {
|
---|
2566 | CheckComArgNotNull(aTarget);
|
---|
2567 | CheckComArgOutPointerValid(aProgress);
|
---|
2568 |
|
---|
2569 | AutoCaller autoCaller(this);
|
---|
2570 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2571 |
|
---|
2572 | ComObjPtr<Medium> diff = static_cast<Medium*>(aTarget);
|
---|
2573 |
|
---|
2574 | // locking: we need the tree lock first because we access parent pointers
|
---|
2575 | AutoMultiWriteLock3 alock(&m->pVirtualBox->getMediaTreeLockHandle(),
|
---|
2576 | this->lockHandle(), diff->lockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
2577 |
|
---|
2578 | if (m->type == MediumType_Writethrough)
|
---|
2579 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2580 | tr("Medium type of '%s' is Writethrough"),
|
---|
2581 | m->strLocationFull.c_str());
|
---|
2582 | else if (m->type == MediumType_Shareable)
|
---|
2583 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2584 | tr("Medium type of '%s' is Shareable"),
|
---|
2585 | m->strLocationFull.c_str());
|
---|
2586 | else if (m->type == MediumType_Readonly)
|
---|
2587 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2588 | tr("Medium type of '%s' is Readonly"),
|
---|
2589 | m->strLocationFull.c_str());
|
---|
2590 |
|
---|
2591 | /* Apply the normal locking logic to the entire chain. */
|
---|
2592 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
2593 | alock.release();
|
---|
2594 | HRESULT rc = diff->createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2595 | true /* fMediumLockWrite */,
|
---|
2596 | this,
|
---|
2597 | *pMediumLockList);
|
---|
2598 | alock.acquire();
|
---|
2599 | if (FAILED(rc))
|
---|
2600 | {
|
---|
2601 | delete pMediumLockList;
|
---|
2602 | return rc;
|
---|
2603 | }
|
---|
2604 |
|
---|
2605 | alock.release();
|
---|
2606 | rc = pMediumLockList->Lock();
|
---|
2607 | alock.acquire();
|
---|
2608 | if (FAILED(rc))
|
---|
2609 | {
|
---|
2610 | delete pMediumLockList;
|
---|
2611 |
|
---|
2612 | return setError(rc, tr("Could not lock medium when creating diff '%s'"),
|
---|
2613 | diff->getLocationFull().c_str());
|
---|
2614 | }
|
---|
2615 |
|
---|
2616 | Guid parentMachineRegistry;
|
---|
2617 | if (getFirstRegistryMachineId(parentMachineRegistry))
|
---|
2618 | {
|
---|
2619 | /* since this medium has been just created it isn't associated yet */
|
---|
2620 | diff->m->llRegistryIDs.push_back(parentMachineRegistry);
|
---|
2621 | alock.release();
|
---|
2622 | diff->markRegistriesModified();
|
---|
2623 | alock.acquire();
|
---|
2624 | }
|
---|
2625 |
|
---|
2626 | alock.release();
|
---|
2627 |
|
---|
2628 | ComObjPtr <Progress> pProgress;
|
---|
2629 |
|
---|
2630 | rc = createDiffStorage(diff, (MediumVariant_T)aVariant, pMediumLockList,
|
---|
2631 | &pProgress, false /* aWait */);
|
---|
2632 | if (FAILED(rc))
|
---|
2633 | delete pMediumLockList;
|
---|
2634 | else
|
---|
2635 | pProgress.queryInterfaceTo(aProgress);
|
---|
2636 |
|
---|
2637 | return rc;
|
---|
2638 | }
|
---|
2639 |
|
---|
2640 | STDMETHODIMP Medium::MergeTo(IMedium *aTarget, IProgress **aProgress)
|
---|
2641 | {
|
---|
2642 | CheckComArgNotNull(aTarget);
|
---|
2643 | CheckComArgOutPointerValid(aProgress);
|
---|
2644 | ComAssertRet(aTarget != this, E_INVALIDARG);
|
---|
2645 |
|
---|
2646 | AutoCaller autoCaller(this);
|
---|
2647 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2648 |
|
---|
2649 | ComObjPtr<Medium> pTarget = static_cast<Medium*>(aTarget);
|
---|
2650 |
|
---|
2651 | bool fMergeForward = false;
|
---|
2652 | ComObjPtr<Medium> pParentForTarget;
|
---|
2653 | MediaList childrenToReparent;
|
---|
2654 | MediumLockList *pMediumLockList = NULL;
|
---|
2655 |
|
---|
2656 | HRESULT rc = S_OK;
|
---|
2657 |
|
---|
2658 | rc = prepareMergeTo(pTarget, NULL, NULL, true, fMergeForward,
|
---|
2659 | pParentForTarget, childrenToReparent, pMediumLockList);
|
---|
2660 | if (FAILED(rc)) return rc;
|
---|
2661 |
|
---|
2662 | ComObjPtr <Progress> pProgress;
|
---|
2663 |
|
---|
2664 | rc = mergeTo(pTarget, fMergeForward, pParentForTarget, childrenToReparent,
|
---|
2665 | pMediumLockList, &pProgress, false /* aWait */);
|
---|
2666 | if (FAILED(rc))
|
---|
2667 | cancelMergeTo(childrenToReparent, pMediumLockList);
|
---|
2668 | else
|
---|
2669 | pProgress.queryInterfaceTo(aProgress);
|
---|
2670 |
|
---|
2671 | return rc;
|
---|
2672 | }
|
---|
2673 |
|
---|
2674 | STDMETHODIMP Medium::CloneToBase(IMedium *aTarget,
|
---|
2675 | ULONG aVariant,
|
---|
2676 | IProgress **aProgress)
|
---|
2677 | {
|
---|
2678 | int rc = S_OK;
|
---|
2679 | CheckComArgNotNull(aTarget);
|
---|
2680 | CheckComArgOutPointerValid(aProgress);
|
---|
2681 | rc = CloneTo(aTarget, aVariant, NULL, aProgress);
|
---|
2682 | return rc;
|
---|
2683 | }
|
---|
2684 |
|
---|
2685 | STDMETHODIMP Medium::CloneTo(IMedium *aTarget,
|
---|
2686 | ULONG aVariant,
|
---|
2687 | IMedium *aParent,
|
---|
2688 | IProgress **aProgress)
|
---|
2689 | {
|
---|
2690 | CheckComArgNotNull(aTarget);
|
---|
2691 | CheckComArgOutPointerValid(aProgress);
|
---|
2692 | ComAssertRet(aTarget != this, E_INVALIDARG);
|
---|
2693 |
|
---|
2694 | AutoCaller autoCaller(this);
|
---|
2695 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2696 |
|
---|
2697 | ComObjPtr<Medium> pTarget = static_cast<Medium*>(aTarget);
|
---|
2698 | ComObjPtr<Medium> pParent;
|
---|
2699 | if (aParent)
|
---|
2700 | pParent = static_cast<Medium*>(aParent);
|
---|
2701 |
|
---|
2702 | HRESULT rc = S_OK;
|
---|
2703 | ComObjPtr<Progress> pProgress;
|
---|
2704 | Medium::Task *pTask = NULL;
|
---|
2705 |
|
---|
2706 | try
|
---|
2707 | {
|
---|
2708 | // locking: we need the tree lock first because we access parent pointers
|
---|
2709 | // and we need to write-lock the media involved
|
---|
2710 | uint32_t cHandles = 3;
|
---|
2711 | LockHandle* pHandles[4] = { &m->pVirtualBox->getMediaTreeLockHandle(),
|
---|
2712 | this->lockHandle(),
|
---|
2713 | pTarget->lockHandle() };
|
---|
2714 | /* Only add parent to the lock if it is not null */
|
---|
2715 | if (!pParent.isNull())
|
---|
2716 | pHandles[cHandles++] = pParent->lockHandle();
|
---|
2717 | AutoWriteLock alock(cHandles,
|
---|
2718 | pHandles
|
---|
2719 | COMMA_LOCKVAL_SRC_POS);
|
---|
2720 |
|
---|
2721 | if ( pTarget->m->state != MediumState_NotCreated
|
---|
2722 | && pTarget->m->state != MediumState_Created)
|
---|
2723 | throw pTarget->setStateError();
|
---|
2724 |
|
---|
2725 | /* Build the source lock list. */
|
---|
2726 | MediumLockList *pSourceMediumLockList(new MediumLockList());
|
---|
2727 | alock.release();
|
---|
2728 | rc = createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2729 | false /* fMediumLockWrite */,
|
---|
2730 | NULL,
|
---|
2731 | *pSourceMediumLockList);
|
---|
2732 | alock.acquire();
|
---|
2733 | if (FAILED(rc))
|
---|
2734 | {
|
---|
2735 | delete pSourceMediumLockList;
|
---|
2736 | throw rc;
|
---|
2737 | }
|
---|
2738 |
|
---|
2739 | /* Build the target lock list (including the to-be parent chain). */
|
---|
2740 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
2741 | alock.release();
|
---|
2742 | rc = pTarget->createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2743 | true /* fMediumLockWrite */,
|
---|
2744 | pParent,
|
---|
2745 | *pTargetMediumLockList);
|
---|
2746 | alock.acquire();
|
---|
2747 | if (FAILED(rc))
|
---|
2748 | {
|
---|
2749 | delete pSourceMediumLockList;
|
---|
2750 | delete pTargetMediumLockList;
|
---|
2751 | throw rc;
|
---|
2752 | }
|
---|
2753 |
|
---|
2754 | alock.release();
|
---|
2755 | rc = pSourceMediumLockList->Lock();
|
---|
2756 | alock.acquire();
|
---|
2757 | if (FAILED(rc))
|
---|
2758 | {
|
---|
2759 | delete pSourceMediumLockList;
|
---|
2760 | delete pTargetMediumLockList;
|
---|
2761 | throw setError(rc,
|
---|
2762 | tr("Failed to lock source media '%s'"),
|
---|
2763 | getLocationFull().c_str());
|
---|
2764 | }
|
---|
2765 | alock.release();
|
---|
2766 | rc = pTargetMediumLockList->Lock();
|
---|
2767 | alock.acquire();
|
---|
2768 | if (FAILED(rc))
|
---|
2769 | {
|
---|
2770 | delete pSourceMediumLockList;
|
---|
2771 | delete pTargetMediumLockList;
|
---|
2772 | throw setError(rc,
|
---|
2773 | tr("Failed to lock target media '%s'"),
|
---|
2774 | pTarget->getLocationFull().c_str());
|
---|
2775 | }
|
---|
2776 |
|
---|
2777 | pProgress.createObject();
|
---|
2778 | rc = pProgress->init(m->pVirtualBox,
|
---|
2779 | static_cast <IMedium *>(this),
|
---|
2780 | BstrFmt(tr("Creating clone medium '%s'"), pTarget->m->strLocationFull.c_str()).raw(),
|
---|
2781 | TRUE /* aCancelable */);
|
---|
2782 | if (FAILED(rc))
|
---|
2783 | {
|
---|
2784 | delete pSourceMediumLockList;
|
---|
2785 | delete pTargetMediumLockList;
|
---|
2786 | throw rc;
|
---|
2787 | }
|
---|
2788 |
|
---|
2789 | /* setup task object to carry out the operation asynchronously */
|
---|
2790 | pTask = new Medium::CloneTask(this, pProgress, pTarget,
|
---|
2791 | (MediumVariant_T)aVariant,
|
---|
2792 | pParent, UINT32_MAX, UINT32_MAX,
|
---|
2793 | pSourceMediumLockList, pTargetMediumLockList);
|
---|
2794 | rc = pTask->rc();
|
---|
2795 | AssertComRC(rc);
|
---|
2796 | if (FAILED(rc))
|
---|
2797 | throw rc;
|
---|
2798 |
|
---|
2799 | if (pTarget->m->state == MediumState_NotCreated)
|
---|
2800 | pTarget->m->state = MediumState_Creating;
|
---|
2801 | }
|
---|
2802 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2803 |
|
---|
2804 | if (SUCCEEDED(rc))
|
---|
2805 | {
|
---|
2806 | rc = startThread(pTask);
|
---|
2807 |
|
---|
2808 | if (SUCCEEDED(rc))
|
---|
2809 | pProgress.queryInterfaceTo(aProgress);
|
---|
2810 | }
|
---|
2811 | else if (pTask != NULL)
|
---|
2812 | delete pTask;
|
---|
2813 |
|
---|
2814 | return rc;
|
---|
2815 | }
|
---|
2816 |
|
---|
2817 | STDMETHODIMP Medium::Compact(IProgress **aProgress)
|
---|
2818 | {
|
---|
2819 | CheckComArgOutPointerValid(aProgress);
|
---|
2820 |
|
---|
2821 | AutoCaller autoCaller(this);
|
---|
2822 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2823 |
|
---|
2824 | HRESULT rc = S_OK;
|
---|
2825 | ComObjPtr <Progress> pProgress;
|
---|
2826 | Medium::Task *pTask = NULL;
|
---|
2827 |
|
---|
2828 | try
|
---|
2829 | {
|
---|
2830 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2831 |
|
---|
2832 | /* Build the medium lock list. */
|
---|
2833 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
2834 | alock.release();
|
---|
2835 | rc = createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
2836 | true /* fMediumLockWrite */,
|
---|
2837 | NULL,
|
---|
2838 | *pMediumLockList);
|
---|
2839 | alock.acquire();
|
---|
2840 | if (FAILED(rc))
|
---|
2841 | {
|
---|
2842 | delete pMediumLockList;
|
---|
2843 | throw rc;
|
---|
2844 | }
|
---|
2845 |
|
---|
2846 | alock.release();
|
---|
2847 | rc = pMediumLockList->Lock();
|
---|
2848 | alock.acquire();
|
---|
2849 | if (FAILED(rc))
|
---|
2850 | {
|
---|
2851 | delete pMediumLockList;
|
---|
2852 | throw setError(rc,
|
---|
2853 | tr("Failed to lock media when compacting '%s'"),
|
---|
2854 | getLocationFull().c_str());
|
---|
2855 | }
|
---|
2856 |
|
---|
2857 | pProgress.createObject();
|
---|
2858 | rc = pProgress->init(m->pVirtualBox,
|
---|
2859 | static_cast <IMedium *>(this),
|
---|
2860 | BstrFmt(tr("Compacting medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
2861 | TRUE /* aCancelable */);
|
---|
2862 | if (FAILED(rc))
|
---|
2863 | {
|
---|
2864 | delete pMediumLockList;
|
---|
2865 | throw rc;
|
---|
2866 | }
|
---|
2867 |
|
---|
2868 | /* setup task object to carry out the operation asynchronously */
|
---|
2869 | pTask = new Medium::CompactTask(this, pProgress, pMediumLockList);
|
---|
2870 | rc = pTask->rc();
|
---|
2871 | AssertComRC(rc);
|
---|
2872 | if (FAILED(rc))
|
---|
2873 | throw rc;
|
---|
2874 | }
|
---|
2875 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2876 |
|
---|
2877 | if (SUCCEEDED(rc))
|
---|
2878 | {
|
---|
2879 | rc = startThread(pTask);
|
---|
2880 |
|
---|
2881 | if (SUCCEEDED(rc))
|
---|
2882 | pProgress.queryInterfaceTo(aProgress);
|
---|
2883 | }
|
---|
2884 | else if (pTask != NULL)
|
---|
2885 | delete pTask;
|
---|
2886 |
|
---|
2887 | return rc;
|
---|
2888 | }
|
---|
2889 |
|
---|
2890 | STDMETHODIMP Medium::Resize(LONG64 aLogicalSize, IProgress **aProgress)
|
---|
2891 | {
|
---|
2892 | CheckComArgOutPointerValid(aProgress);
|
---|
2893 |
|
---|
2894 | AutoCaller autoCaller(this);
|
---|
2895 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2896 |
|
---|
2897 | HRESULT rc = S_OK;
|
---|
2898 | ComObjPtr <Progress> pProgress;
|
---|
2899 | Medium::Task *pTask = NULL;
|
---|
2900 |
|
---|
2901 | try
|
---|
2902 | {
|
---|
2903 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2904 |
|
---|
2905 | /* Build the medium lock list. */
|
---|
2906 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
2907 | alock.release();
|
---|
2908 | rc = createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
2909 | true /* fMediumLockWrite */,
|
---|
2910 | NULL,
|
---|
2911 | *pMediumLockList);
|
---|
2912 | alock.acquire();
|
---|
2913 | if (FAILED(rc))
|
---|
2914 | {
|
---|
2915 | delete pMediumLockList;
|
---|
2916 | throw rc;
|
---|
2917 | }
|
---|
2918 |
|
---|
2919 | alock.release();
|
---|
2920 | rc = pMediumLockList->Lock();
|
---|
2921 | alock.acquire();
|
---|
2922 | if (FAILED(rc))
|
---|
2923 | {
|
---|
2924 | delete pMediumLockList;
|
---|
2925 | throw setError(rc,
|
---|
2926 | tr("Failed to lock media when compacting '%s'"),
|
---|
2927 | getLocationFull().c_str());
|
---|
2928 | }
|
---|
2929 |
|
---|
2930 | pProgress.createObject();
|
---|
2931 | rc = pProgress->init(m->pVirtualBox,
|
---|
2932 | static_cast <IMedium *>(this),
|
---|
2933 | BstrFmt(tr("Compacting medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
2934 | TRUE /* aCancelable */);
|
---|
2935 | if (FAILED(rc))
|
---|
2936 | {
|
---|
2937 | delete pMediumLockList;
|
---|
2938 | throw rc;
|
---|
2939 | }
|
---|
2940 |
|
---|
2941 | /* setup task object to carry out the operation asynchronously */
|
---|
2942 | pTask = new Medium::ResizeTask(this, aLogicalSize, pProgress, pMediumLockList);
|
---|
2943 | rc = pTask->rc();
|
---|
2944 | AssertComRC(rc);
|
---|
2945 | if (FAILED(rc))
|
---|
2946 | throw rc;
|
---|
2947 | }
|
---|
2948 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2949 |
|
---|
2950 | if (SUCCEEDED(rc))
|
---|
2951 | {
|
---|
2952 | rc = startThread(pTask);
|
---|
2953 |
|
---|
2954 | if (SUCCEEDED(rc))
|
---|
2955 | pProgress.queryInterfaceTo(aProgress);
|
---|
2956 | }
|
---|
2957 | else if (pTask != NULL)
|
---|
2958 | delete pTask;
|
---|
2959 |
|
---|
2960 | return rc;
|
---|
2961 | }
|
---|
2962 |
|
---|
2963 | STDMETHODIMP Medium::Reset(IProgress **aProgress)
|
---|
2964 | {
|
---|
2965 | CheckComArgOutPointerValid(aProgress);
|
---|
2966 |
|
---|
2967 | AutoCaller autoCaller(this);
|
---|
2968 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2969 |
|
---|
2970 | HRESULT rc = S_OK;
|
---|
2971 | ComObjPtr <Progress> pProgress;
|
---|
2972 | Medium::Task *pTask = NULL;
|
---|
2973 |
|
---|
2974 | try
|
---|
2975 | {
|
---|
2976 | /* canClose() needs the tree lock */
|
---|
2977 | AutoMultiWriteLock2 multilock(&m->pVirtualBox->getMediaTreeLockHandle(),
|
---|
2978 | this->lockHandle()
|
---|
2979 | COMMA_LOCKVAL_SRC_POS);
|
---|
2980 |
|
---|
2981 | LogFlowThisFunc(("ENTER for medium %s\n", m->strLocationFull.c_str()));
|
---|
2982 |
|
---|
2983 | if (m->pParent.isNull())
|
---|
2984 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2985 | tr("Medium type of '%s' is not differencing"),
|
---|
2986 | m->strLocationFull.c_str());
|
---|
2987 |
|
---|
2988 | rc = canClose();
|
---|
2989 | if (FAILED(rc))
|
---|
2990 | throw rc;
|
---|
2991 |
|
---|
2992 | /* Build the medium lock list. */
|
---|
2993 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
2994 | multilock.release();
|
---|
2995 | rc = createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2996 | true /* fMediumLockWrite */,
|
---|
2997 | NULL,
|
---|
2998 | *pMediumLockList);
|
---|
2999 | multilock.acquire();
|
---|
3000 | if (FAILED(rc))
|
---|
3001 | {
|
---|
3002 | delete pMediumLockList;
|
---|
3003 | throw rc;
|
---|
3004 | }
|
---|
3005 |
|
---|
3006 | multilock.release();
|
---|
3007 | rc = pMediumLockList->Lock();
|
---|
3008 | multilock.acquire();
|
---|
3009 | if (FAILED(rc))
|
---|
3010 | {
|
---|
3011 | delete pMediumLockList;
|
---|
3012 | throw setError(rc,
|
---|
3013 | tr("Failed to lock media when resetting '%s'"),
|
---|
3014 | getLocationFull().c_str());
|
---|
3015 | }
|
---|
3016 |
|
---|
3017 | pProgress.createObject();
|
---|
3018 | rc = pProgress->init(m->pVirtualBox,
|
---|
3019 | static_cast<IMedium*>(this),
|
---|
3020 | BstrFmt(tr("Resetting differencing medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3021 | FALSE /* aCancelable */);
|
---|
3022 | if (FAILED(rc))
|
---|
3023 | throw rc;
|
---|
3024 |
|
---|
3025 | /* setup task object to carry out the operation asynchronously */
|
---|
3026 | pTask = new Medium::ResetTask(this, pProgress, pMediumLockList);
|
---|
3027 | rc = pTask->rc();
|
---|
3028 | AssertComRC(rc);
|
---|
3029 | if (FAILED(rc))
|
---|
3030 | throw rc;
|
---|
3031 | }
|
---|
3032 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3033 |
|
---|
3034 | if (SUCCEEDED(rc))
|
---|
3035 | {
|
---|
3036 | rc = startThread(pTask);
|
---|
3037 |
|
---|
3038 | if (SUCCEEDED(rc))
|
---|
3039 | pProgress.queryInterfaceTo(aProgress);
|
---|
3040 | }
|
---|
3041 | else
|
---|
3042 | {
|
---|
3043 | /* Note: on success, the task will unlock this */
|
---|
3044 | {
|
---|
3045 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3046 | HRESULT rc2 = UnlockWrite(NULL);
|
---|
3047 | AssertComRC(rc2);
|
---|
3048 | }
|
---|
3049 | if (pTask != NULL)
|
---|
3050 | delete pTask;
|
---|
3051 | }
|
---|
3052 |
|
---|
3053 | LogFlowThisFunc(("LEAVE, rc=%Rhrc\n", rc));
|
---|
3054 |
|
---|
3055 | return rc;
|
---|
3056 | }
|
---|
3057 |
|
---|
3058 | ////////////////////////////////////////////////////////////////////////////////
|
---|
3059 | //
|
---|
3060 | // Medium public internal methods
|
---|
3061 | //
|
---|
3062 | ////////////////////////////////////////////////////////////////////////////////
|
---|
3063 |
|
---|
3064 | /**
|
---|
3065 | * Internal method to return the medium's parent medium. Must have caller + locking!
|
---|
3066 | * @return
|
---|
3067 | */
|
---|
3068 | const ComObjPtr<Medium>& Medium::getParent() const
|
---|
3069 | {
|
---|
3070 | return m->pParent;
|
---|
3071 | }
|
---|
3072 |
|
---|
3073 | /**
|
---|
3074 | * Internal method to return the medium's list of child media. Must have caller + locking!
|
---|
3075 | * @return
|
---|
3076 | */
|
---|
3077 | const MediaList& Medium::getChildren() const
|
---|
3078 | {
|
---|
3079 | return m->llChildren;
|
---|
3080 | }
|
---|
3081 |
|
---|
3082 | /**
|
---|
3083 | * Internal method to return the medium's GUID. Must have caller + locking!
|
---|
3084 | * @return
|
---|
3085 | */
|
---|
3086 | const Guid& Medium::getId() const
|
---|
3087 | {
|
---|
3088 | return m->id;
|
---|
3089 | }
|
---|
3090 |
|
---|
3091 | /**
|
---|
3092 | * Internal method to return the medium's state. Must have caller + locking!
|
---|
3093 | * @return
|
---|
3094 | */
|
---|
3095 | MediumState_T Medium::getState() const
|
---|
3096 | {
|
---|
3097 | return m->state;
|
---|
3098 | }
|
---|
3099 |
|
---|
3100 | /**
|
---|
3101 | * Internal method to return the medium's variant. Must have caller + locking!
|
---|
3102 | * @return
|
---|
3103 | */
|
---|
3104 | MediumVariant_T Medium::getVariant() const
|
---|
3105 | {
|
---|
3106 | return m->variant;
|
---|
3107 | }
|
---|
3108 |
|
---|
3109 | /**
|
---|
3110 | * Internal method which returns true if this medium represents a host drive.
|
---|
3111 | * @return
|
---|
3112 | */
|
---|
3113 | bool Medium::isHostDrive() const
|
---|
3114 | {
|
---|
3115 | return m->hostDrive;
|
---|
3116 | }
|
---|
3117 |
|
---|
3118 | /**
|
---|
3119 | * Internal method to return the medium's full location. Must have caller + locking!
|
---|
3120 | * @return
|
---|
3121 | */
|
---|
3122 | const Utf8Str& Medium::getLocationFull() const
|
---|
3123 | {
|
---|
3124 | return m->strLocationFull;
|
---|
3125 | }
|
---|
3126 |
|
---|
3127 | /**
|
---|
3128 | * Internal method to return the medium's format string. Must have caller + locking!
|
---|
3129 | * @return
|
---|
3130 | */
|
---|
3131 | const Utf8Str& Medium::getFormat() const
|
---|
3132 | {
|
---|
3133 | return m->strFormat;
|
---|
3134 | }
|
---|
3135 |
|
---|
3136 | /**
|
---|
3137 | * Internal method to return the medium's format object. Must have caller + locking!
|
---|
3138 | * @return
|
---|
3139 | */
|
---|
3140 | const ComObjPtr<MediumFormat>& Medium::getMediumFormat() const
|
---|
3141 | {
|
---|
3142 | return m->formatObj;
|
---|
3143 | }
|
---|
3144 |
|
---|
3145 | /**
|
---|
3146 | * Internal method that returns true if the medium is represented by a file on the host disk
|
---|
3147 | * (and not iSCSI or something).
|
---|
3148 | * @return
|
---|
3149 | */
|
---|
3150 | bool Medium::isMediumFormatFile() const
|
---|
3151 | {
|
---|
3152 | if ( m->formatObj
|
---|
3153 | && (m->formatObj->getCapabilities() & MediumFormatCapabilities_File)
|
---|
3154 | )
|
---|
3155 | return true;
|
---|
3156 | return false;
|
---|
3157 | }
|
---|
3158 |
|
---|
3159 | /**
|
---|
3160 | * Internal method to return the medium's size. Must have caller + locking!
|
---|
3161 | * @return
|
---|
3162 | */
|
---|
3163 | uint64_t Medium::getSize() const
|
---|
3164 | {
|
---|
3165 | return m->size;
|
---|
3166 | }
|
---|
3167 |
|
---|
3168 | /**
|
---|
3169 | * Returns the medium device type. Must have caller + locking!
|
---|
3170 | * @return
|
---|
3171 | */
|
---|
3172 | DeviceType_T Medium::getDeviceType() const
|
---|
3173 | {
|
---|
3174 | return m->devType;
|
---|
3175 | }
|
---|
3176 |
|
---|
3177 | /**
|
---|
3178 | * Returns the medium type. Must have caller + locking!
|
---|
3179 | * @return
|
---|
3180 | */
|
---|
3181 | MediumType_T Medium::getType() const
|
---|
3182 | {
|
---|
3183 | return m->type;
|
---|
3184 | }
|
---|
3185 |
|
---|
3186 | /**
|
---|
3187 | * Returns a short version of the location attribute.
|
---|
3188 | *
|
---|
3189 | * @note Must be called from under this object's read or write lock.
|
---|
3190 | */
|
---|
3191 | Utf8Str Medium::getName()
|
---|
3192 | {
|
---|
3193 | Utf8Str name = RTPathFilename(m->strLocationFull.c_str());
|
---|
3194 | return name;
|
---|
3195 | }
|
---|
3196 |
|
---|
3197 | /**
|
---|
3198 | * This adds the given UUID to the list of media registries in which this
|
---|
3199 | * medium should be registered. The UUID can either be a machine UUID,
|
---|
3200 | * to add a machine registry, or the global registry UUID as returned by
|
---|
3201 | * VirtualBox::getGlobalRegistryId().
|
---|
3202 | *
|
---|
3203 | * Note that for hard disks, this method does nothing if the medium is
|
---|
3204 | * already in another registry to avoid having hard disks in more than
|
---|
3205 | * one registry, which causes trouble with keeping diff images in sync.
|
---|
3206 | * See getFirstRegistryMachineId() for details.
|
---|
3207 | *
|
---|
3208 | * If fRecurse == true, then the media tree lock must be held for reading.
|
---|
3209 | *
|
---|
3210 | * @param id
|
---|
3211 | * @param fRecurse If true, recurses into child media to make sure the whole tree has registries in sync.
|
---|
3212 | * @return true if the registry was added; false if the given id was already on the list.
|
---|
3213 | */
|
---|
3214 | bool Medium::addRegistry(const Guid& id, bool fRecurse)
|
---|
3215 | {
|
---|
3216 | AutoCaller autoCaller(this);
|
---|
3217 | if (FAILED(autoCaller.rc()))
|
---|
3218 | return false;
|
---|
3219 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3220 |
|
---|
3221 | bool fAdd = true;
|
---|
3222 |
|
---|
3223 | // hard disks cannot be in more than one registry
|
---|
3224 | if ( m->devType == DeviceType_HardDisk
|
---|
3225 | && m->llRegistryIDs.size() > 0)
|
---|
3226 | fAdd = false;
|
---|
3227 |
|
---|
3228 | // no need to add the UUID twice
|
---|
3229 | if (fAdd)
|
---|
3230 | {
|
---|
3231 | for (GuidList::const_iterator it = m->llRegistryIDs.begin();
|
---|
3232 | it != m->llRegistryIDs.end();
|
---|
3233 | ++it)
|
---|
3234 | {
|
---|
3235 | if ((*it) == id)
|
---|
3236 | {
|
---|
3237 | fAdd = false;
|
---|
3238 | break;
|
---|
3239 | }
|
---|
3240 | }
|
---|
3241 | }
|
---|
3242 |
|
---|
3243 | if (fAdd)
|
---|
3244 | m->llRegistryIDs.push_back(id);
|
---|
3245 |
|
---|
3246 | if (fRecurse)
|
---|
3247 | {
|
---|
3248 | // Get private list of children and release medium lock straight away.
|
---|
3249 | MediaList llChildren(m->llChildren);
|
---|
3250 | alock.release();
|
---|
3251 |
|
---|
3252 | for (MediaList::iterator it = llChildren.begin();
|
---|
3253 | it != llChildren.end();
|
---|
3254 | ++it)
|
---|
3255 | {
|
---|
3256 | Medium *pChild = *it;
|
---|
3257 | fAdd |= pChild->addRegistry(id, true);
|
---|
3258 | }
|
---|
3259 | }
|
---|
3260 |
|
---|
3261 | return fAdd;
|
---|
3262 | }
|
---|
3263 |
|
---|
3264 | /**
|
---|
3265 | * Removes the given UUID from the list of media registry UUIDs. Returns true
|
---|
3266 | * if found or false if not.
|
---|
3267 | *
|
---|
3268 | * If fRecurse == true, then the media tree lock must be held for reading.
|
---|
3269 | *
|
---|
3270 | * @param id
|
---|
3271 | * @param fRecurse If true, recurses into child media to make sure the whole tree has registries in sync.
|
---|
3272 | * @return
|
---|
3273 | */
|
---|
3274 | bool Medium::removeRegistry(const Guid& id, bool fRecurse)
|
---|
3275 | {
|
---|
3276 | AutoCaller autoCaller(this);
|
---|
3277 | if (FAILED(autoCaller.rc()))
|
---|
3278 | return false;
|
---|
3279 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3280 |
|
---|
3281 | bool fRemove = false;
|
---|
3282 |
|
---|
3283 | for (GuidList::iterator it = m->llRegistryIDs.begin();
|
---|
3284 | it != m->llRegistryIDs.end();
|
---|
3285 | ++it)
|
---|
3286 | {
|
---|
3287 | if ((*it) == id)
|
---|
3288 | {
|
---|
3289 | m->llRegistryIDs.erase(it);
|
---|
3290 | fRemove = true;
|
---|
3291 | break;
|
---|
3292 | }
|
---|
3293 | }
|
---|
3294 |
|
---|
3295 | if (fRecurse)
|
---|
3296 | {
|
---|
3297 | // Get private list of children and release medium lock straight away.
|
---|
3298 | MediaList llChildren(m->llChildren);
|
---|
3299 | alock.release();
|
---|
3300 |
|
---|
3301 | for (MediaList::iterator it = llChildren.begin();
|
---|
3302 | it != llChildren.end();
|
---|
3303 | ++it)
|
---|
3304 | {
|
---|
3305 | Medium *pChild = *it;
|
---|
3306 | fRemove |= pChild->removeRegistry(id, true);
|
---|
3307 | }
|
---|
3308 | }
|
---|
3309 |
|
---|
3310 | return fRemove;
|
---|
3311 | }
|
---|
3312 |
|
---|
3313 | /**
|
---|
3314 | * Returns true if id is in the list of media registries for this medium.
|
---|
3315 | *
|
---|
3316 | * Must have caller + read locking!
|
---|
3317 | *
|
---|
3318 | * @param id
|
---|
3319 | * @return
|
---|
3320 | */
|
---|
3321 | bool Medium::isInRegistry(const Guid& id)
|
---|
3322 | {
|
---|
3323 | for (GuidList::const_iterator it = m->llRegistryIDs.begin();
|
---|
3324 | it != m->llRegistryIDs.end();
|
---|
3325 | ++it)
|
---|
3326 | {
|
---|
3327 | if (*it == id)
|
---|
3328 | return true;
|
---|
3329 | }
|
---|
3330 |
|
---|
3331 | return false;
|
---|
3332 | }
|
---|
3333 |
|
---|
3334 | /**
|
---|
3335 | * Internal method to return the medium's first registry machine (i.e. the machine in whose
|
---|
3336 | * machine XML this medium is listed).
|
---|
3337 | *
|
---|
3338 | * Every attached medium must now (4.0) reside in at least one media registry, which is identified
|
---|
3339 | * by a UUID. This is either a machine UUID if the machine is from 4.0 or newer, in which case
|
---|
3340 | * machines have their own media registries, or it is the pseudo-UUID of the VirtualBox
|
---|
3341 | * object if the machine is old and still needs the global registry in VirtualBox.xml.
|
---|
3342 | *
|
---|
3343 | * By definition, hard disks may only be in one media registry, in which all its children
|
---|
3344 | * will be stored as well. Otherwise we run into problems with having keep multiple registries
|
---|
3345 | * in sync. (This is the "cloned VM" case in which VM1 may link to the disks of VM2; in this
|
---|
3346 | * case, only VM2's registry is used for the disk in question.)
|
---|
3347 | *
|
---|
3348 | * If there is no medium registry, particularly if the medium has not been attached yet, this
|
---|
3349 | * does not modify uuid and returns false.
|
---|
3350 | *
|
---|
3351 | * ISOs and RAWs, by contrast, can be in more than one repository to make things easier for
|
---|
3352 | * the user.
|
---|
3353 | *
|
---|
3354 | * Must have caller + locking!
|
---|
3355 | *
|
---|
3356 | * @param uuid Receives first registry machine UUID, if available.
|
---|
3357 | * @return true if uuid was set.
|
---|
3358 | */
|
---|
3359 | bool Medium::getFirstRegistryMachineId(Guid &uuid) const
|
---|
3360 | {
|
---|
3361 | if (m->llRegistryIDs.size())
|
---|
3362 | {
|
---|
3363 | uuid = m->llRegistryIDs.front();
|
---|
3364 | return true;
|
---|
3365 | }
|
---|
3366 | return false;
|
---|
3367 | }
|
---|
3368 |
|
---|
3369 | /**
|
---|
3370 | * Marks all the registries in which this medium is registered as modified.
|
---|
3371 | */
|
---|
3372 | void Medium::markRegistriesModified()
|
---|
3373 | {
|
---|
3374 | AutoCaller autoCaller(this);
|
---|
3375 | if (FAILED(autoCaller.rc())) return;
|
---|
3376 |
|
---|
3377 | // Get local copy, as keeping the lock over VirtualBox::markRegistryModified
|
---|
3378 | // causes trouble with the lock order
|
---|
3379 | GuidList llRegistryIDs;
|
---|
3380 | {
|
---|
3381 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3382 | llRegistryIDs = m->llRegistryIDs;
|
---|
3383 | }
|
---|
3384 |
|
---|
3385 | for (GuidList::const_iterator it = llRegistryIDs.begin();
|
---|
3386 | it != llRegistryIDs.end();
|
---|
3387 | ++it)
|
---|
3388 | {
|
---|
3389 | m->pVirtualBox->markRegistryModified(*it);
|
---|
3390 | }
|
---|
3391 | }
|
---|
3392 |
|
---|
3393 | /**
|
---|
3394 | * Adds the given machine and optionally the snapshot to the list of the objects
|
---|
3395 | * this medium is attached to.
|
---|
3396 | *
|
---|
3397 | * @param aMachineId Machine ID.
|
---|
3398 | * @param aSnapshotId Snapshot ID; when non-empty, adds a snapshot attachment.
|
---|
3399 | */
|
---|
3400 | HRESULT Medium::addBackReference(const Guid &aMachineId,
|
---|
3401 | const Guid &aSnapshotId /*= Guid::Empty*/)
|
---|
3402 | {
|
---|
3403 | AssertReturn(!aMachineId.isEmpty(), E_FAIL);
|
---|
3404 |
|
---|
3405 | LogFlowThisFunc(("ENTER, aMachineId: {%RTuuid}, aSnapshotId: {%RTuuid}\n", aMachineId.raw(), aSnapshotId.raw()));
|
---|
3406 |
|
---|
3407 | AutoCaller autoCaller(this);
|
---|
3408 | AssertComRCReturnRC(autoCaller.rc());
|
---|
3409 |
|
---|
3410 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3411 |
|
---|
3412 | switch (m->state)
|
---|
3413 | {
|
---|
3414 | case MediumState_Created:
|
---|
3415 | case MediumState_Inaccessible:
|
---|
3416 | case MediumState_LockedRead:
|
---|
3417 | case MediumState_LockedWrite:
|
---|
3418 | break;
|
---|
3419 |
|
---|
3420 | default:
|
---|
3421 | return setStateError();
|
---|
3422 | }
|
---|
3423 |
|
---|
3424 | if (m->numCreateDiffTasks > 0)
|
---|
3425 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
3426 | tr("Cannot attach medium '%s' {%RTuuid}: %u differencing child media are being created"),
|
---|
3427 | m->strLocationFull.c_str(),
|
---|
3428 | m->id.raw(),
|
---|
3429 | m->numCreateDiffTasks);
|
---|
3430 |
|
---|
3431 | BackRefList::iterator it = std::find_if(m->backRefs.begin(),
|
---|
3432 | m->backRefs.end(),
|
---|
3433 | BackRef::EqualsTo(aMachineId));
|
---|
3434 | if (it == m->backRefs.end())
|
---|
3435 | {
|
---|
3436 | BackRef ref(aMachineId, aSnapshotId);
|
---|
3437 | m->backRefs.push_back(ref);
|
---|
3438 |
|
---|
3439 | return S_OK;
|
---|
3440 | }
|
---|
3441 |
|
---|
3442 | // if the caller has not supplied a snapshot ID, then we're attaching
|
---|
3443 | // to a machine a medium which represents the machine's current state,
|
---|
3444 | // so set the flag
|
---|
3445 | if (aSnapshotId.isEmpty())
|
---|
3446 | {
|
---|
3447 | /* sanity: no duplicate attachments */
|
---|
3448 | if (it->fInCurState)
|
---|
3449 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
3450 | tr("Cannot attach medium '%s' {%RTuuid}: medium is already associated with the current state of machine uuid {%RTuuid}!"),
|
---|
3451 | m->strLocationFull.c_str(),
|
---|
3452 | m->id.raw(),
|
---|
3453 | aMachineId.raw());
|
---|
3454 | it->fInCurState = true;
|
---|
3455 |
|
---|
3456 | return S_OK;
|
---|
3457 | }
|
---|
3458 |
|
---|
3459 | // otherwise: a snapshot medium is being attached
|
---|
3460 |
|
---|
3461 | /* sanity: no duplicate attachments */
|
---|
3462 | for (GuidList::const_iterator jt = it->llSnapshotIds.begin();
|
---|
3463 | jt != it->llSnapshotIds.end();
|
---|
3464 | ++jt)
|
---|
3465 | {
|
---|
3466 | const Guid &idOldSnapshot = *jt;
|
---|
3467 |
|
---|
3468 | if (idOldSnapshot == aSnapshotId)
|
---|
3469 | {
|
---|
3470 | #ifdef DEBUG
|
---|
3471 | dumpBackRefs();
|
---|
3472 | #endif
|
---|
3473 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
3474 | tr("Cannot attach medium '%s' {%RTuuid} from snapshot '%RTuuid': medium is already in use by this snapshot!"),
|
---|
3475 | m->strLocationFull.c_str(),
|
---|
3476 | m->id.raw(),
|
---|
3477 | aSnapshotId.raw());
|
---|
3478 | }
|
---|
3479 | }
|
---|
3480 |
|
---|
3481 | it->llSnapshotIds.push_back(aSnapshotId);
|
---|
3482 | it->fInCurState = false;
|
---|
3483 |
|
---|
3484 | LogFlowThisFuncLeave();
|
---|
3485 |
|
---|
3486 | return S_OK;
|
---|
3487 | }
|
---|
3488 |
|
---|
3489 | /**
|
---|
3490 | * Removes the given machine and optionally the snapshot from the list of the
|
---|
3491 | * objects this medium is attached to.
|
---|
3492 | *
|
---|
3493 | * @param aMachineId Machine ID.
|
---|
3494 | * @param aSnapshotId Snapshot ID; when non-empty, removes the snapshot
|
---|
3495 | * attachment.
|
---|
3496 | */
|
---|
3497 | HRESULT Medium::removeBackReference(const Guid &aMachineId,
|
---|
3498 | const Guid &aSnapshotId /*= Guid::Empty*/)
|
---|
3499 | {
|
---|
3500 | AssertReturn(!aMachineId.isEmpty(), E_FAIL);
|
---|
3501 |
|
---|
3502 | AutoCaller autoCaller(this);
|
---|
3503 | AssertComRCReturnRC(autoCaller.rc());
|
---|
3504 |
|
---|
3505 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3506 |
|
---|
3507 | BackRefList::iterator it =
|
---|
3508 | std::find_if(m->backRefs.begin(), m->backRefs.end(),
|
---|
3509 | BackRef::EqualsTo(aMachineId));
|
---|
3510 | AssertReturn(it != m->backRefs.end(), E_FAIL);
|
---|
3511 |
|
---|
3512 | if (aSnapshotId.isEmpty())
|
---|
3513 | {
|
---|
3514 | /* remove the current state attachment */
|
---|
3515 | it->fInCurState = false;
|
---|
3516 | }
|
---|
3517 | else
|
---|
3518 | {
|
---|
3519 | /* remove the snapshot attachment */
|
---|
3520 | GuidList::iterator jt = std::find(it->llSnapshotIds.begin(),
|
---|
3521 | it->llSnapshotIds.end(),
|
---|
3522 | aSnapshotId);
|
---|
3523 |
|
---|
3524 | AssertReturn(jt != it->llSnapshotIds.end(), E_FAIL);
|
---|
3525 | it->llSnapshotIds.erase(jt);
|
---|
3526 | }
|
---|
3527 |
|
---|
3528 | /* if the backref becomes empty, remove it */
|
---|
3529 | if (it->fInCurState == false && it->llSnapshotIds.size() == 0)
|
---|
3530 | m->backRefs.erase(it);
|
---|
3531 |
|
---|
3532 | return S_OK;
|
---|
3533 | }
|
---|
3534 |
|
---|
3535 | /**
|
---|
3536 | * Internal method to return the medium's list of backrefs. Must have caller + locking!
|
---|
3537 | * @return
|
---|
3538 | */
|
---|
3539 | const Guid* Medium::getFirstMachineBackrefId() const
|
---|
3540 | {
|
---|
3541 | if (!m->backRefs.size())
|
---|
3542 | return NULL;
|
---|
3543 |
|
---|
3544 | return &m->backRefs.front().machineId;
|
---|
3545 | }
|
---|
3546 |
|
---|
3547 | /**
|
---|
3548 | * Internal method which returns a machine that either this medium or one of its children
|
---|
3549 | * is attached to. This is used for finding a replacement media registry when an existing
|
---|
3550 | * media registry is about to be deleted in VirtualBox::unregisterMachine().
|
---|
3551 | *
|
---|
3552 | * Must have caller + locking, *and* caller must hold the media tree lock!
|
---|
3553 | * @return
|
---|
3554 | */
|
---|
3555 | const Guid* Medium::getAnyMachineBackref() const
|
---|
3556 | {
|
---|
3557 | if (m->backRefs.size())
|
---|
3558 | return &m->backRefs.front().machineId;
|
---|
3559 |
|
---|
3560 | for (MediaList::iterator it = m->llChildren.begin();
|
---|
3561 | it != m->llChildren.end();
|
---|
3562 | ++it)
|
---|
3563 | {
|
---|
3564 | Medium *pChild = *it;
|
---|
3565 | // recurse for this child
|
---|
3566 | const Guid* puuid;
|
---|
3567 | if ((puuid = pChild->getAnyMachineBackref()))
|
---|
3568 | return puuid;
|
---|
3569 | }
|
---|
3570 |
|
---|
3571 | return NULL;
|
---|
3572 | }
|
---|
3573 |
|
---|
3574 | const Guid* Medium::getFirstMachineBackrefSnapshotId() const
|
---|
3575 | {
|
---|
3576 | if (!m->backRefs.size())
|
---|
3577 | return NULL;
|
---|
3578 |
|
---|
3579 | const BackRef &ref = m->backRefs.front();
|
---|
3580 | if (!ref.llSnapshotIds.size())
|
---|
3581 | return NULL;
|
---|
3582 |
|
---|
3583 | return &ref.llSnapshotIds.front();
|
---|
3584 | }
|
---|
3585 |
|
---|
3586 | size_t Medium::getMachineBackRefCount() const
|
---|
3587 | {
|
---|
3588 | return m->backRefs.size();
|
---|
3589 | }
|
---|
3590 |
|
---|
3591 | #ifdef DEBUG
|
---|
3592 | /**
|
---|
3593 | * Debugging helper that gets called after VirtualBox initialization that writes all
|
---|
3594 | * machine backreferences to the debug log.
|
---|
3595 | */
|
---|
3596 | void Medium::dumpBackRefs()
|
---|
3597 | {
|
---|
3598 | AutoCaller autoCaller(this);
|
---|
3599 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3600 |
|
---|
3601 | LogFlowThisFunc(("Dumping backrefs for medium '%s':\n", m->strLocationFull.c_str()));
|
---|
3602 |
|
---|
3603 | for (BackRefList::iterator it2 = m->backRefs.begin();
|
---|
3604 | it2 != m->backRefs.end();
|
---|
3605 | ++it2)
|
---|
3606 | {
|
---|
3607 | const BackRef &ref = *it2;
|
---|
3608 | LogFlowThisFunc((" Backref from machine {%RTuuid} (fInCurState: %d)\n", ref.machineId.raw(), ref.fInCurState));
|
---|
3609 |
|
---|
3610 | for (GuidList::const_iterator jt2 = it2->llSnapshotIds.begin();
|
---|
3611 | jt2 != it2->llSnapshotIds.end();
|
---|
3612 | ++jt2)
|
---|
3613 | {
|
---|
3614 | const Guid &id = *jt2;
|
---|
3615 | LogFlowThisFunc((" Backref from snapshot {%RTuuid}\n", id.raw()));
|
---|
3616 | }
|
---|
3617 | }
|
---|
3618 | }
|
---|
3619 | #endif
|
---|
3620 |
|
---|
3621 | /**
|
---|
3622 | * Checks if the given change of \a aOldPath to \a aNewPath affects the location
|
---|
3623 | * of this media and updates it if necessary to reflect the new location.
|
---|
3624 | *
|
---|
3625 | * @param aOldPath Old path (full).
|
---|
3626 | * @param aNewPath New path (full).
|
---|
3627 | *
|
---|
3628 | * @note Locks this object for writing.
|
---|
3629 | */
|
---|
3630 | HRESULT Medium::updatePath(const Utf8Str &strOldPath, const Utf8Str &strNewPath)
|
---|
3631 | {
|
---|
3632 | AssertReturn(!strOldPath.isEmpty(), E_FAIL);
|
---|
3633 | AssertReturn(!strNewPath.isEmpty(), E_FAIL);
|
---|
3634 |
|
---|
3635 | AutoCaller autoCaller(this);
|
---|
3636 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
3637 |
|
---|
3638 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3639 |
|
---|
3640 | LogFlowThisFunc(("locationFull.before='%s'\n", m->strLocationFull.c_str()));
|
---|
3641 |
|
---|
3642 | const char *pcszMediumPath = m->strLocationFull.c_str();
|
---|
3643 |
|
---|
3644 | if (RTPathStartsWith(pcszMediumPath, strOldPath.c_str()))
|
---|
3645 | {
|
---|
3646 | Utf8Str newPath(strNewPath);
|
---|
3647 | newPath.append(pcszMediumPath + strOldPath.length());
|
---|
3648 | unconst(m->strLocationFull) = newPath;
|
---|
3649 |
|
---|
3650 | LogFlowThisFunc(("locationFull.after='%s'\n", m->strLocationFull.c_str()));
|
---|
3651 | // we changed something
|
---|
3652 | return S_OK;
|
---|
3653 | }
|
---|
3654 |
|
---|
3655 | // no change was necessary, signal error which the caller needs to interpret
|
---|
3656 | return VBOX_E_FILE_ERROR;
|
---|
3657 | }
|
---|
3658 |
|
---|
3659 | /**
|
---|
3660 | * Returns the base medium of the media chain this medium is part of.
|
---|
3661 | *
|
---|
3662 | * The base medium is found by walking up the parent-child relationship axis.
|
---|
3663 | * If the medium doesn't have a parent (i.e. it's a base medium), it
|
---|
3664 | * returns itself in response to this method.
|
---|
3665 | *
|
---|
3666 | * @param aLevel Where to store the number of ancestors of this medium
|
---|
3667 | * (zero for the base), may be @c NULL.
|
---|
3668 | *
|
---|
3669 | * @note Locks medium tree for reading.
|
---|
3670 | */
|
---|
3671 | ComObjPtr<Medium> Medium::getBase(uint32_t *aLevel /*= NULL*/)
|
---|
3672 | {
|
---|
3673 | ComObjPtr<Medium> pBase;
|
---|
3674 | uint32_t level;
|
---|
3675 |
|
---|
3676 | AutoCaller autoCaller(this);
|
---|
3677 | AssertReturn(autoCaller.isOk(), pBase);
|
---|
3678 |
|
---|
3679 | /* we access mParent */
|
---|
3680 | AutoReadLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3681 |
|
---|
3682 | pBase = this;
|
---|
3683 | level = 0;
|
---|
3684 |
|
---|
3685 | if (m->pParent)
|
---|
3686 | {
|
---|
3687 | for (;;)
|
---|
3688 | {
|
---|
3689 | AutoCaller baseCaller(pBase);
|
---|
3690 | AssertReturn(baseCaller.isOk(), pBase);
|
---|
3691 |
|
---|
3692 | if (pBase->m->pParent.isNull())
|
---|
3693 | break;
|
---|
3694 |
|
---|
3695 | pBase = pBase->m->pParent;
|
---|
3696 | ++level;
|
---|
3697 | }
|
---|
3698 | }
|
---|
3699 |
|
---|
3700 | if (aLevel != NULL)
|
---|
3701 | *aLevel = level;
|
---|
3702 |
|
---|
3703 | return pBase;
|
---|
3704 | }
|
---|
3705 |
|
---|
3706 | /**
|
---|
3707 | * Returns @c true if this medium cannot be modified because it has
|
---|
3708 | * dependents (children) or is part of the snapshot. Related to the medium
|
---|
3709 | * type and posterity, not to the current media state.
|
---|
3710 | *
|
---|
3711 | * @note Locks this object and medium tree for reading.
|
---|
3712 | */
|
---|
3713 | bool Medium::isReadOnly()
|
---|
3714 | {
|
---|
3715 | AutoCaller autoCaller(this);
|
---|
3716 | AssertComRCReturn(autoCaller.rc(), false);
|
---|
3717 |
|
---|
3718 | /* we access children */
|
---|
3719 | AutoReadLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3720 |
|
---|
3721 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3722 |
|
---|
3723 | switch (m->type)
|
---|
3724 | {
|
---|
3725 | case MediumType_Normal:
|
---|
3726 | {
|
---|
3727 | if (getChildren().size() != 0)
|
---|
3728 | return true;
|
---|
3729 |
|
---|
3730 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
3731 | it != m->backRefs.end(); ++it)
|
---|
3732 | if (it->llSnapshotIds.size() != 0)
|
---|
3733 | return true;
|
---|
3734 |
|
---|
3735 | if (m->variant & MediumVariant_VmdkStreamOptimized)
|
---|
3736 | return true;
|
---|
3737 |
|
---|
3738 | return false;
|
---|
3739 | }
|
---|
3740 | case MediumType_Immutable:
|
---|
3741 | case MediumType_MultiAttach:
|
---|
3742 | return true;
|
---|
3743 | case MediumType_Writethrough:
|
---|
3744 | case MediumType_Shareable:
|
---|
3745 | case MediumType_Readonly: /* explicit readonly media has no diffs */
|
---|
3746 | return false;
|
---|
3747 | default:
|
---|
3748 | break;
|
---|
3749 | }
|
---|
3750 |
|
---|
3751 | AssertFailedReturn(false);
|
---|
3752 | }
|
---|
3753 |
|
---|
3754 | /**
|
---|
3755 | * Internal method to return the medium's size. Must have caller + locking!
|
---|
3756 | * @return
|
---|
3757 | */
|
---|
3758 | void Medium::updateId(const Guid &id)
|
---|
3759 | {
|
---|
3760 | unconst(m->id) = id;
|
---|
3761 | }
|
---|
3762 |
|
---|
3763 | /**
|
---|
3764 | * Saves medium data by appending a new child node to the given
|
---|
3765 | * parent XML settings node.
|
---|
3766 | *
|
---|
3767 | * @param data Settings struct to be updated.
|
---|
3768 | * @param strHardDiskFolder Folder for which paths should be relative.
|
---|
3769 | *
|
---|
3770 | * @note Locks this object, medium tree and children for reading.
|
---|
3771 | */
|
---|
3772 | HRESULT Medium::saveSettings(settings::Medium &data,
|
---|
3773 | const Utf8Str &strHardDiskFolder)
|
---|
3774 | {
|
---|
3775 | AutoCaller autoCaller(this);
|
---|
3776 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
3777 |
|
---|
3778 | /* we access mParent */
|
---|
3779 | AutoReadLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3780 |
|
---|
3781 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3782 |
|
---|
3783 | data.uuid = m->id;
|
---|
3784 |
|
---|
3785 | // make path relative if needed
|
---|
3786 | if ( !strHardDiskFolder.isEmpty()
|
---|
3787 | && RTPathStartsWith(m->strLocationFull.c_str(), strHardDiskFolder.c_str())
|
---|
3788 | )
|
---|
3789 | data.strLocation = m->strLocationFull.substr(strHardDiskFolder.length() + 1);
|
---|
3790 | else
|
---|
3791 | data.strLocation = m->strLocationFull;
|
---|
3792 | data.strFormat = m->strFormat;
|
---|
3793 |
|
---|
3794 | /* optional, only for diffs, default is false */
|
---|
3795 | if (m->pParent)
|
---|
3796 | data.fAutoReset = m->autoReset;
|
---|
3797 | else
|
---|
3798 | data.fAutoReset = false;
|
---|
3799 |
|
---|
3800 | /* optional */
|
---|
3801 | data.strDescription = m->strDescription;
|
---|
3802 |
|
---|
3803 | /* optional properties */
|
---|
3804 | data.properties.clear();
|
---|
3805 |
|
---|
3806 | /* handle iSCSI initiator secrets transparently */
|
---|
3807 | bool fHaveInitiatorSecretEncrypted = false;
|
---|
3808 | Utf8Str strCiphertext;
|
---|
3809 | settings::StringsMap::const_iterator itPln = m->mapProperties.find("InitiatorSecret");
|
---|
3810 | if ( itPln != m->mapProperties.end()
|
---|
3811 | && !itPln->second.isEmpty())
|
---|
3812 | {
|
---|
3813 | /* Encrypt the plain secret. If that does not work (i.e. no or wrong settings key
|
---|
3814 | * specified), just use the encrypted secret (if there is any). */
|
---|
3815 | int rc = m->pVirtualBox->encryptSetting(itPln->second, &strCiphertext);
|
---|
3816 | if (RT_SUCCESS(rc))
|
---|
3817 | fHaveInitiatorSecretEncrypted = true;
|
---|
3818 | }
|
---|
3819 | for (settings::StringsMap::const_iterator it = m->mapProperties.begin();
|
---|
3820 | it != m->mapProperties.end();
|
---|
3821 | ++it)
|
---|
3822 | {
|
---|
3823 | /* only save properties that have non-default values */
|
---|
3824 | if (!it->second.isEmpty())
|
---|
3825 | {
|
---|
3826 | const Utf8Str &name = it->first;
|
---|
3827 | const Utf8Str &value = it->second;
|
---|
3828 | /* do NOT store the plain InitiatorSecret */
|
---|
3829 | if ( !fHaveInitiatorSecretEncrypted
|
---|
3830 | || !name.equals("InitiatorSecret"))
|
---|
3831 | data.properties[name] = value;
|
---|
3832 | }
|
---|
3833 | }
|
---|
3834 | if (fHaveInitiatorSecretEncrypted)
|
---|
3835 | data.properties["InitiatorSecretEncrypted"] = strCiphertext;
|
---|
3836 |
|
---|
3837 | /* only for base media */
|
---|
3838 | if (m->pParent.isNull())
|
---|
3839 | data.hdType = m->type;
|
---|
3840 |
|
---|
3841 | /* save all children */
|
---|
3842 | for (MediaList::const_iterator it = getChildren().begin();
|
---|
3843 | it != getChildren().end();
|
---|
3844 | ++it)
|
---|
3845 | {
|
---|
3846 | settings::Medium med;
|
---|
3847 | HRESULT rc = (*it)->saveSettings(med, strHardDiskFolder);
|
---|
3848 | AssertComRCReturnRC(rc);
|
---|
3849 | data.llChildren.push_back(med);
|
---|
3850 | }
|
---|
3851 |
|
---|
3852 | return S_OK;
|
---|
3853 | }
|
---|
3854 |
|
---|
3855 | /**
|
---|
3856 | * Constructs a medium lock list for this medium. The lock is not taken.
|
---|
3857 | *
|
---|
3858 | * @note Caller MUST NOT hold the media tree or medium lock.
|
---|
3859 | *
|
---|
3860 | * @param fFailIfInaccessible If true, this fails with an error if a medium is inaccessible. If false,
|
---|
3861 | * inaccessible media are silently skipped and not locked (i.e. their state remains "Inaccessible");
|
---|
3862 | * this is necessary for a VM's removable media VM startup for which we do not want to fail.
|
---|
3863 | * @param fMediumLockWrite Whether to associate a write lock with this medium.
|
---|
3864 | * @param pToBeParent Medium which will become the parent of this medium.
|
---|
3865 | * @param mediumLockList Where to store the resulting list.
|
---|
3866 | */
|
---|
3867 | HRESULT Medium::createMediumLockList(bool fFailIfInaccessible,
|
---|
3868 | bool fMediumLockWrite,
|
---|
3869 | Medium *pToBeParent,
|
---|
3870 | MediumLockList &mediumLockList)
|
---|
3871 | {
|
---|
3872 | Assert(!m->pVirtualBox->getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
3873 | Assert(!isWriteLockOnCurrentThread());
|
---|
3874 |
|
---|
3875 | AutoCaller autoCaller(this);
|
---|
3876 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
3877 |
|
---|
3878 | HRESULT rc = S_OK;
|
---|
3879 |
|
---|
3880 | /* paranoid sanity checking if the medium has a to-be parent medium */
|
---|
3881 | if (pToBeParent)
|
---|
3882 | {
|
---|
3883 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3884 | ComAssertRet(getParent().isNull(), E_FAIL);
|
---|
3885 | ComAssertRet(getChildren().size() == 0, E_FAIL);
|
---|
3886 | }
|
---|
3887 |
|
---|
3888 | ErrorInfoKeeper eik;
|
---|
3889 | MultiResult mrc(S_OK);
|
---|
3890 |
|
---|
3891 | ComObjPtr<Medium> pMedium = this;
|
---|
3892 | while (!pMedium.isNull())
|
---|
3893 | {
|
---|
3894 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
3895 |
|
---|
3896 | /* Accessibility check must be first, otherwise locking interferes
|
---|
3897 | * with getting the medium state. Lock lists are not created for
|
---|
3898 | * fun, and thus getting the medium status is no luxury. */
|
---|
3899 | MediumState_T mediumState = pMedium->getState();
|
---|
3900 | if (mediumState == MediumState_Inaccessible)
|
---|
3901 | {
|
---|
3902 | alock.release();
|
---|
3903 | rc = pMedium->queryInfo(false /* fSetImageId */, false /* fSetParentId */);
|
---|
3904 | alock.acquire();
|
---|
3905 | if (FAILED(rc)) return rc;
|
---|
3906 |
|
---|
3907 | mediumState = pMedium->getState();
|
---|
3908 | if (mediumState == MediumState_Inaccessible)
|
---|
3909 | {
|
---|
3910 | // ignore inaccessible ISO media and silently return S_OK,
|
---|
3911 | // otherwise VM startup (esp. restore) may fail without good reason
|
---|
3912 | if (!fFailIfInaccessible)
|
---|
3913 | return S_OK;
|
---|
3914 |
|
---|
3915 | // otherwise report an error
|
---|
3916 | Bstr error;
|
---|
3917 | rc = pMedium->COMGETTER(LastAccessError)(error.asOutParam());
|
---|
3918 | if (FAILED(rc)) return rc;
|
---|
3919 |
|
---|
3920 | /* collect multiple errors */
|
---|
3921 | eik.restore();
|
---|
3922 | Assert(!error.isEmpty());
|
---|
3923 | mrc = setError(E_FAIL,
|
---|
3924 | "%ls",
|
---|
3925 | error.raw());
|
---|
3926 | // error message will be something like
|
---|
3927 | // "Could not open the medium ... VD: error VERR_FILE_NOT_FOUND opening image file ... (VERR_FILE_NOT_FOUND).
|
---|
3928 | eik.fetch();
|
---|
3929 | }
|
---|
3930 | }
|
---|
3931 |
|
---|
3932 | if (pMedium == this)
|
---|
3933 | mediumLockList.Prepend(pMedium, fMediumLockWrite);
|
---|
3934 | else
|
---|
3935 | mediumLockList.Prepend(pMedium, false);
|
---|
3936 |
|
---|
3937 | pMedium = pMedium->getParent();
|
---|
3938 | if (pMedium.isNull() && pToBeParent)
|
---|
3939 | {
|
---|
3940 | pMedium = pToBeParent;
|
---|
3941 | pToBeParent = NULL;
|
---|
3942 | }
|
---|
3943 | }
|
---|
3944 |
|
---|
3945 | return mrc;
|
---|
3946 | }
|
---|
3947 |
|
---|
3948 | /**
|
---|
3949 | * Creates a new differencing storage unit using the format of the given target
|
---|
3950 | * medium and the location. Note that @c aTarget must be NotCreated.
|
---|
3951 | *
|
---|
3952 | * The @a aMediumLockList parameter contains the associated medium lock list,
|
---|
3953 | * which must be in locked state. If @a aWait is @c true then the caller is
|
---|
3954 | * responsible for unlocking.
|
---|
3955 | *
|
---|
3956 | * If @a aProgress is not NULL but the object it points to is @c null then a
|
---|
3957 | * new progress object will be created and assigned to @a *aProgress on
|
---|
3958 | * success, otherwise the existing progress object is used. If @a aProgress is
|
---|
3959 | * NULL, then no progress object is created/used at all.
|
---|
3960 | *
|
---|
3961 | * When @a aWait is @c false, this method will create a thread to perform the
|
---|
3962 | * create operation asynchronously and will return immediately. Otherwise, it
|
---|
3963 | * will perform the operation on the calling thread and will not return to the
|
---|
3964 | * caller until the operation is completed. Note that @a aProgress cannot be
|
---|
3965 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
3966 | *
|
---|
3967 | * @param aTarget Target medium.
|
---|
3968 | * @param aVariant Precise medium variant to create.
|
---|
3969 | * @param aMediumLockList List of media which should be locked.
|
---|
3970 | * @param aProgress Where to find/store a Progress object to track
|
---|
3971 | * operation completion.
|
---|
3972 | * @param aWait @c true if this method should block instead of
|
---|
3973 | * creating an asynchronous thread.
|
---|
3974 | *
|
---|
3975 | * @note Locks this object and @a aTarget for writing.
|
---|
3976 | */
|
---|
3977 | HRESULT Medium::createDiffStorage(ComObjPtr<Medium> &aTarget,
|
---|
3978 | MediumVariant_T aVariant,
|
---|
3979 | MediumLockList *aMediumLockList,
|
---|
3980 | ComObjPtr<Progress> *aProgress,
|
---|
3981 | bool aWait)
|
---|
3982 | {
|
---|
3983 | AssertReturn(!aTarget.isNull(), E_FAIL);
|
---|
3984 | AssertReturn(aMediumLockList, E_FAIL);
|
---|
3985 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
3986 |
|
---|
3987 | AutoCaller autoCaller(this);
|
---|
3988 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
3989 |
|
---|
3990 | AutoCaller targetCaller(aTarget);
|
---|
3991 | if (FAILED(targetCaller.rc())) return targetCaller.rc();
|
---|
3992 |
|
---|
3993 | HRESULT rc = S_OK;
|
---|
3994 | ComObjPtr<Progress> pProgress;
|
---|
3995 | Medium::Task *pTask = NULL;
|
---|
3996 |
|
---|
3997 | try
|
---|
3998 | {
|
---|
3999 | AutoMultiWriteLock2 alock(this, aTarget COMMA_LOCKVAL_SRC_POS);
|
---|
4000 |
|
---|
4001 | ComAssertThrow( m->type != MediumType_Writethrough
|
---|
4002 | && m->type != MediumType_Shareable
|
---|
4003 | && m->type != MediumType_Readonly, E_FAIL);
|
---|
4004 | ComAssertThrow(m->state == MediumState_LockedRead, E_FAIL);
|
---|
4005 |
|
---|
4006 | if (aTarget->m->state != MediumState_NotCreated)
|
---|
4007 | throw aTarget->setStateError();
|
---|
4008 |
|
---|
4009 | /* Check that the medium is not attached to the current state of
|
---|
4010 | * any VM referring to it. */
|
---|
4011 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
4012 | it != m->backRefs.end();
|
---|
4013 | ++it)
|
---|
4014 | {
|
---|
4015 | if (it->fInCurState)
|
---|
4016 | {
|
---|
4017 | /* Note: when a VM snapshot is being taken, all normal media
|
---|
4018 | * attached to the VM in the current state will be, as an
|
---|
4019 | * exception, also associated with the snapshot which is about
|
---|
4020 | * to create (see SnapshotMachine::init()) before deassociating
|
---|
4021 | * them from the current state (which takes place only on
|
---|
4022 | * success in Machine::fixupHardDisks()), so that the size of
|
---|
4023 | * snapshotIds will be 1 in this case. The extra condition is
|
---|
4024 | * used to filter out this legal situation. */
|
---|
4025 | if (it->llSnapshotIds.size() == 0)
|
---|
4026 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4027 | 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"),
|
---|
4028 | m->strLocationFull.c_str(), it->machineId.raw());
|
---|
4029 |
|
---|
4030 | Assert(it->llSnapshotIds.size() == 1);
|
---|
4031 | }
|
---|
4032 | }
|
---|
4033 |
|
---|
4034 | if (aProgress != NULL)
|
---|
4035 | {
|
---|
4036 | /* use the existing progress object... */
|
---|
4037 | pProgress = *aProgress;
|
---|
4038 |
|
---|
4039 | /* ...but create a new one if it is null */
|
---|
4040 | if (pProgress.isNull())
|
---|
4041 | {
|
---|
4042 | pProgress.createObject();
|
---|
4043 | rc = pProgress->init(m->pVirtualBox,
|
---|
4044 | static_cast<IMedium*>(this),
|
---|
4045 | BstrFmt(tr("Creating differencing medium storage unit '%s'"), aTarget->m->strLocationFull.c_str()).raw(),
|
---|
4046 | TRUE /* aCancelable */);
|
---|
4047 | if (FAILED(rc))
|
---|
4048 | throw rc;
|
---|
4049 | }
|
---|
4050 | }
|
---|
4051 |
|
---|
4052 | /* setup task object to carry out the operation sync/async */
|
---|
4053 | pTask = new Medium::CreateDiffTask(this, pProgress, aTarget, aVariant,
|
---|
4054 | aMediumLockList,
|
---|
4055 | aWait /* fKeepMediumLockList */);
|
---|
4056 | rc = pTask->rc();
|
---|
4057 | AssertComRC(rc);
|
---|
4058 | if (FAILED(rc))
|
---|
4059 | throw rc;
|
---|
4060 |
|
---|
4061 | /* register a task (it will deregister itself when done) */
|
---|
4062 | ++m->numCreateDiffTasks;
|
---|
4063 | Assert(m->numCreateDiffTasks != 0); /* overflow? */
|
---|
4064 |
|
---|
4065 | aTarget->m->state = MediumState_Creating;
|
---|
4066 | }
|
---|
4067 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4068 |
|
---|
4069 | if (SUCCEEDED(rc))
|
---|
4070 | {
|
---|
4071 | if (aWait)
|
---|
4072 | rc = runNow(pTask);
|
---|
4073 | else
|
---|
4074 | rc = startThread(pTask);
|
---|
4075 |
|
---|
4076 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
4077 | *aProgress = pProgress;
|
---|
4078 | }
|
---|
4079 | else if (pTask != NULL)
|
---|
4080 | delete pTask;
|
---|
4081 |
|
---|
4082 | return rc;
|
---|
4083 | }
|
---|
4084 |
|
---|
4085 | /**
|
---|
4086 | * Returns a preferred format for differencing media.
|
---|
4087 | */
|
---|
4088 | Utf8Str Medium::getPreferredDiffFormat()
|
---|
4089 | {
|
---|
4090 | AutoCaller autoCaller(this);
|
---|
4091 | AssertComRCReturn(autoCaller.rc(), Utf8Str::Empty);
|
---|
4092 |
|
---|
4093 | /* check that our own format supports diffs */
|
---|
4094 | if (!(m->formatObj->getCapabilities() & MediumFormatCapabilities_Differencing))
|
---|
4095 | {
|
---|
4096 | /* use the default format if not */
|
---|
4097 | Utf8Str tmp;
|
---|
4098 | m->pVirtualBox->getDefaultHardDiskFormat(tmp);
|
---|
4099 | return tmp;
|
---|
4100 | }
|
---|
4101 |
|
---|
4102 | /* m->strFormat is const, no need to lock */
|
---|
4103 | return m->strFormat;
|
---|
4104 | }
|
---|
4105 |
|
---|
4106 | /**
|
---|
4107 | * Implementation for the public Medium::Close() with the exception of calling
|
---|
4108 | * VirtualBox::saveRegistries(), in case someone wants to call this for several
|
---|
4109 | * media.
|
---|
4110 | *
|
---|
4111 | * After this returns with success, uninit() has been called on the medium, and
|
---|
4112 | * the object is no longer usable ("not ready" state).
|
---|
4113 | *
|
---|
4114 | * @param autoCaller AutoCaller instance which must have been created on the caller's stack for this medium. This gets released here
|
---|
4115 | * upon which the Medium instance gets uninitialized.
|
---|
4116 | * @return
|
---|
4117 | */
|
---|
4118 | HRESULT Medium::close(AutoCaller &autoCaller)
|
---|
4119 | {
|
---|
4120 | // we're accessing parent/child and backrefs, so lock the tree first, then ourselves
|
---|
4121 | AutoMultiWriteLock2 multilock(&m->pVirtualBox->getMediaTreeLockHandle(),
|
---|
4122 | this->lockHandle()
|
---|
4123 | COMMA_LOCKVAL_SRC_POS);
|
---|
4124 |
|
---|
4125 | LogFlowFunc(("ENTER for %s\n", getLocationFull().c_str()));
|
---|
4126 |
|
---|
4127 | bool wasCreated = true;
|
---|
4128 |
|
---|
4129 | switch (m->state)
|
---|
4130 | {
|
---|
4131 | case MediumState_NotCreated:
|
---|
4132 | wasCreated = false;
|
---|
4133 | break;
|
---|
4134 | case MediumState_Created:
|
---|
4135 | case MediumState_Inaccessible:
|
---|
4136 | break;
|
---|
4137 | default:
|
---|
4138 | return setStateError();
|
---|
4139 | }
|
---|
4140 |
|
---|
4141 | if (m->backRefs.size() != 0)
|
---|
4142 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4143 | tr("Medium '%s' cannot be closed because it is still attached to %d virtual machines"),
|
---|
4144 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
4145 |
|
---|
4146 | // perform extra media-dependent close checks
|
---|
4147 | HRESULT rc = canClose();
|
---|
4148 | if (FAILED(rc)) return rc;
|
---|
4149 |
|
---|
4150 | if (wasCreated)
|
---|
4151 | {
|
---|
4152 | // remove from the list of known media before performing actual
|
---|
4153 | // uninitialization (to keep the media registry consistent on
|
---|
4154 | // failure to do so)
|
---|
4155 | rc = unregisterWithVirtualBox();
|
---|
4156 | if (FAILED(rc)) return rc;
|
---|
4157 |
|
---|
4158 | multilock.release();
|
---|
4159 | markRegistriesModified();
|
---|
4160 | m->pVirtualBox->saveModifiedRegistries();
|
---|
4161 | multilock.acquire();
|
---|
4162 | }
|
---|
4163 |
|
---|
4164 | // release the AutoCaller, as otherwise uninit() will simply hang
|
---|
4165 | autoCaller.release();
|
---|
4166 |
|
---|
4167 | // Keep the locks held until after uninit, as otherwise the consistency
|
---|
4168 | // of the medium tree cannot be guaranteed.
|
---|
4169 | uninit();
|
---|
4170 |
|
---|
4171 | LogFlowFuncLeave();
|
---|
4172 |
|
---|
4173 | return rc;
|
---|
4174 | }
|
---|
4175 |
|
---|
4176 | /**
|
---|
4177 | * Deletes the medium storage unit.
|
---|
4178 | *
|
---|
4179 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
4180 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
4181 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
4182 | * progress object is created/used at all.
|
---|
4183 | *
|
---|
4184 | * When @a aWait is @c false, this method will create a thread to perform the
|
---|
4185 | * delete operation asynchronously and will return immediately. Otherwise, it
|
---|
4186 | * will perform the operation on the calling thread and will not return to the
|
---|
4187 | * caller until the operation is completed. Note that @a aProgress cannot be
|
---|
4188 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
4189 | *
|
---|
4190 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
4191 | * completion.
|
---|
4192 | * @param aWait @c true if this method should block instead of creating
|
---|
4193 | * an asynchronous thread.
|
---|
4194 | *
|
---|
4195 | * @note Locks mVirtualBox and this object for writing. Locks medium tree for
|
---|
4196 | * writing.
|
---|
4197 | */
|
---|
4198 | HRESULT Medium::deleteStorage(ComObjPtr<Progress> *aProgress,
|
---|
4199 | bool aWait)
|
---|
4200 | {
|
---|
4201 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
4202 |
|
---|
4203 | AutoCaller autoCaller(this);
|
---|
4204 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4205 |
|
---|
4206 | HRESULT rc = S_OK;
|
---|
4207 | ComObjPtr<Progress> pProgress;
|
---|
4208 | Medium::Task *pTask = NULL;
|
---|
4209 |
|
---|
4210 | try
|
---|
4211 | {
|
---|
4212 | /* we're accessing the media tree, and canClose() needs it too */
|
---|
4213 | AutoMultiWriteLock2 multilock(&m->pVirtualBox->getMediaTreeLockHandle(),
|
---|
4214 | this->lockHandle()
|
---|
4215 | COMMA_LOCKVAL_SRC_POS);
|
---|
4216 | LogFlowThisFunc(("aWait=%RTbool locationFull=%s\n", aWait, getLocationFull().c_str() ));
|
---|
4217 |
|
---|
4218 | if ( !(m->formatObj->getCapabilities() & ( MediumFormatCapabilities_CreateDynamic
|
---|
4219 | | MediumFormatCapabilities_CreateFixed)))
|
---|
4220 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
4221 | tr("Medium format '%s' does not support storage deletion"),
|
---|
4222 | m->strFormat.c_str());
|
---|
4223 |
|
---|
4224 | /* Note that we are fine with Inaccessible state too: a) for symmetry
|
---|
4225 | * with create calls and b) because it doesn't really harm to try, if
|
---|
4226 | * it is really inaccessible, the delete operation will fail anyway.
|
---|
4227 | * Accepting Inaccessible state is especially important because all
|
---|
4228 | * registered media are initially Inaccessible upon VBoxSVC startup
|
---|
4229 | * until COMGETTER(RefreshState) is called. Accept Deleting state
|
---|
4230 | * because some callers need to put the medium in this state early
|
---|
4231 | * to prevent races. */
|
---|
4232 | switch (m->state)
|
---|
4233 | {
|
---|
4234 | case MediumState_Created:
|
---|
4235 | case MediumState_Deleting:
|
---|
4236 | case MediumState_Inaccessible:
|
---|
4237 | break;
|
---|
4238 | default:
|
---|
4239 | throw setStateError();
|
---|
4240 | }
|
---|
4241 |
|
---|
4242 | if (m->backRefs.size() != 0)
|
---|
4243 | {
|
---|
4244 | Utf8Str strMachines;
|
---|
4245 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
4246 | it != m->backRefs.end();
|
---|
4247 | ++it)
|
---|
4248 | {
|
---|
4249 | const BackRef &b = *it;
|
---|
4250 | if (strMachines.length())
|
---|
4251 | strMachines.append(", ");
|
---|
4252 | strMachines.append(b.machineId.toString().c_str());
|
---|
4253 | }
|
---|
4254 | #ifdef DEBUG
|
---|
4255 | dumpBackRefs();
|
---|
4256 | #endif
|
---|
4257 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
4258 | tr("Cannot delete storage: medium '%s' is still attached to the following %d virtual machine(s): %s"),
|
---|
4259 | m->strLocationFull.c_str(),
|
---|
4260 | m->backRefs.size(),
|
---|
4261 | strMachines.c_str());
|
---|
4262 | }
|
---|
4263 |
|
---|
4264 | rc = canClose();
|
---|
4265 | if (FAILED(rc))
|
---|
4266 | throw rc;
|
---|
4267 |
|
---|
4268 | /* go to Deleting state, so that the medium is not actually locked */
|
---|
4269 | if (m->state != MediumState_Deleting)
|
---|
4270 | {
|
---|
4271 | rc = markForDeletion();
|
---|
4272 | if (FAILED(rc))
|
---|
4273 | throw rc;
|
---|
4274 | }
|
---|
4275 |
|
---|
4276 | /* Build the medium lock list. */
|
---|
4277 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
4278 | multilock.release();
|
---|
4279 | rc = createMediumLockList(true /* fFailIfInaccessible */,
|
---|
4280 | true /* fMediumLockWrite */,
|
---|
4281 | NULL,
|
---|
4282 | *pMediumLockList);
|
---|
4283 | multilock.acquire();
|
---|
4284 | if (FAILED(rc))
|
---|
4285 | {
|
---|
4286 | delete pMediumLockList;
|
---|
4287 | throw rc;
|
---|
4288 | }
|
---|
4289 |
|
---|
4290 | multilock.release();
|
---|
4291 | rc = pMediumLockList->Lock();
|
---|
4292 | multilock.acquire();
|
---|
4293 | if (FAILED(rc))
|
---|
4294 | {
|
---|
4295 | delete pMediumLockList;
|
---|
4296 | throw setError(rc,
|
---|
4297 | tr("Failed to lock media when deleting '%s'"),
|
---|
4298 | getLocationFull().c_str());
|
---|
4299 | }
|
---|
4300 |
|
---|
4301 | /* try to remove from the list of known media before performing
|
---|
4302 | * actual deletion (we favor the consistency of the media registry
|
---|
4303 | * which would have been broken if unregisterWithVirtualBox() failed
|
---|
4304 | * after we successfully deleted the storage) */
|
---|
4305 | rc = unregisterWithVirtualBox();
|
---|
4306 | if (FAILED(rc))
|
---|
4307 | throw rc;
|
---|
4308 | // no longer need lock
|
---|
4309 | multilock.release();
|
---|
4310 | markRegistriesModified();
|
---|
4311 |
|
---|
4312 | if (aProgress != NULL)
|
---|
4313 | {
|
---|
4314 | /* use the existing progress object... */
|
---|
4315 | pProgress = *aProgress;
|
---|
4316 |
|
---|
4317 | /* ...but create a new one if it is null */
|
---|
4318 | if (pProgress.isNull())
|
---|
4319 | {
|
---|
4320 | pProgress.createObject();
|
---|
4321 | rc = pProgress->init(m->pVirtualBox,
|
---|
4322 | static_cast<IMedium*>(this),
|
---|
4323 | BstrFmt(tr("Deleting medium storage unit '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
4324 | FALSE /* aCancelable */);
|
---|
4325 | if (FAILED(rc))
|
---|
4326 | throw rc;
|
---|
4327 | }
|
---|
4328 | }
|
---|
4329 |
|
---|
4330 | /* setup task object to carry out the operation sync/async */
|
---|
4331 | pTask = new Medium::DeleteTask(this, pProgress, pMediumLockList);
|
---|
4332 | rc = pTask->rc();
|
---|
4333 | AssertComRC(rc);
|
---|
4334 | if (FAILED(rc))
|
---|
4335 | throw rc;
|
---|
4336 | }
|
---|
4337 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4338 |
|
---|
4339 | if (SUCCEEDED(rc))
|
---|
4340 | {
|
---|
4341 | if (aWait)
|
---|
4342 | rc = runNow(pTask);
|
---|
4343 | else
|
---|
4344 | rc = startThread(pTask);
|
---|
4345 |
|
---|
4346 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
4347 | *aProgress = pProgress;
|
---|
4348 |
|
---|
4349 | }
|
---|
4350 | else
|
---|
4351 | {
|
---|
4352 | if (pTask)
|
---|
4353 | delete pTask;
|
---|
4354 |
|
---|
4355 | /* Undo deleting state if necessary. */
|
---|
4356 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4357 | /* Make sure that any error signalled by unmarkForDeletion() is not
|
---|
4358 | * ending up in the error list (if the caller uses MultiResult). It
|
---|
4359 | * usually is spurious, as in most cases the medium hasn't been marked
|
---|
4360 | * for deletion when the error was thrown above. */
|
---|
4361 | ErrorInfoKeeper eik;
|
---|
4362 | unmarkForDeletion();
|
---|
4363 | }
|
---|
4364 |
|
---|
4365 | return rc;
|
---|
4366 | }
|
---|
4367 |
|
---|
4368 | /**
|
---|
4369 | * Mark a medium for deletion.
|
---|
4370 | *
|
---|
4371 | * @note Caller must hold the write lock on this medium!
|
---|
4372 | */
|
---|
4373 | HRESULT Medium::markForDeletion()
|
---|
4374 | {
|
---|
4375 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
4376 | switch (m->state)
|
---|
4377 | {
|
---|
4378 | case MediumState_Created:
|
---|
4379 | case MediumState_Inaccessible:
|
---|
4380 | m->preLockState = m->state;
|
---|
4381 | m->state = MediumState_Deleting;
|
---|
4382 | return S_OK;
|
---|
4383 | default:
|
---|
4384 | return setStateError();
|
---|
4385 | }
|
---|
4386 | }
|
---|
4387 |
|
---|
4388 | /**
|
---|
4389 | * Removes the "mark for deletion".
|
---|
4390 | *
|
---|
4391 | * @note Caller must hold the write lock on this medium!
|
---|
4392 | */
|
---|
4393 | HRESULT Medium::unmarkForDeletion()
|
---|
4394 | {
|
---|
4395 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
4396 | switch (m->state)
|
---|
4397 | {
|
---|
4398 | case MediumState_Deleting:
|
---|
4399 | m->state = m->preLockState;
|
---|
4400 | return S_OK;
|
---|
4401 | default:
|
---|
4402 | return setStateError();
|
---|
4403 | }
|
---|
4404 | }
|
---|
4405 |
|
---|
4406 | /**
|
---|
4407 | * Mark a medium for deletion which is in locked state.
|
---|
4408 | *
|
---|
4409 | * @note Caller must hold the write lock on this medium!
|
---|
4410 | */
|
---|
4411 | HRESULT Medium::markLockedForDeletion()
|
---|
4412 | {
|
---|
4413 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
4414 | if ( ( m->state == MediumState_LockedRead
|
---|
4415 | || m->state == MediumState_LockedWrite)
|
---|
4416 | && m->preLockState == MediumState_Created)
|
---|
4417 | {
|
---|
4418 | m->preLockState = MediumState_Deleting;
|
---|
4419 | return S_OK;
|
---|
4420 | }
|
---|
4421 | else
|
---|
4422 | return setStateError();
|
---|
4423 | }
|
---|
4424 |
|
---|
4425 | /**
|
---|
4426 | * Removes the "mark for deletion" for a medium in locked state.
|
---|
4427 | *
|
---|
4428 | * @note Caller must hold the write lock on this medium!
|
---|
4429 | */
|
---|
4430 | HRESULT Medium::unmarkLockedForDeletion()
|
---|
4431 | {
|
---|
4432 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
4433 | if ( ( m->state == MediumState_LockedRead
|
---|
4434 | || m->state == MediumState_LockedWrite)
|
---|
4435 | && m->preLockState == MediumState_Deleting)
|
---|
4436 | {
|
---|
4437 | m->preLockState = MediumState_Created;
|
---|
4438 | return S_OK;
|
---|
4439 | }
|
---|
4440 | else
|
---|
4441 | return setStateError();
|
---|
4442 | }
|
---|
4443 |
|
---|
4444 | /**
|
---|
4445 | * Prepares this (source) medium, target medium and all intermediate media
|
---|
4446 | * for the merge operation.
|
---|
4447 | *
|
---|
4448 | * This method is to be called prior to calling the #mergeTo() to perform
|
---|
4449 | * necessary consistency checks and place involved media to appropriate
|
---|
4450 | * states. If #mergeTo() is not called or fails, the state modifications
|
---|
4451 | * performed by this method must be undone by #cancelMergeTo().
|
---|
4452 | *
|
---|
4453 | * See #mergeTo() for more information about merging.
|
---|
4454 | *
|
---|
4455 | * @param pTarget Target medium.
|
---|
4456 | * @param aMachineId Allowed machine attachment. NULL means do not check.
|
---|
4457 | * @param aSnapshotId Allowed snapshot attachment. NULL or empty UUID means
|
---|
4458 | * do not check.
|
---|
4459 | * @param fLockMedia Flag whether to lock the medium lock list or not.
|
---|
4460 | * If set to false and the medium lock list locking fails
|
---|
4461 | * later you must call #cancelMergeTo().
|
---|
4462 | * @param fMergeForward Resulting merge direction (out).
|
---|
4463 | * @param pParentForTarget New parent for target medium after merge (out).
|
---|
4464 | * @param aChildrenToReparent List of children of the source which will have
|
---|
4465 | * to be reparented to the target after merge (out).
|
---|
4466 | * @param aMediumLockList Medium locking information (out).
|
---|
4467 | *
|
---|
4468 | * @note Locks medium tree for reading. Locks this object, aTarget and all
|
---|
4469 | * intermediate media for writing.
|
---|
4470 | */
|
---|
4471 | HRESULT Medium::prepareMergeTo(const ComObjPtr<Medium> &pTarget,
|
---|
4472 | const Guid *aMachineId,
|
---|
4473 | const Guid *aSnapshotId,
|
---|
4474 | bool fLockMedia,
|
---|
4475 | bool &fMergeForward,
|
---|
4476 | ComObjPtr<Medium> &pParentForTarget,
|
---|
4477 | MediaList &aChildrenToReparent,
|
---|
4478 | MediumLockList * &aMediumLockList)
|
---|
4479 | {
|
---|
4480 | AssertReturn(pTarget != NULL, E_FAIL);
|
---|
4481 | AssertReturn(pTarget != this, E_FAIL);
|
---|
4482 |
|
---|
4483 | AutoCaller autoCaller(this);
|
---|
4484 | AssertComRCReturnRC(autoCaller.rc());
|
---|
4485 |
|
---|
4486 | AutoCaller targetCaller(pTarget);
|
---|
4487 | AssertComRCReturnRC(targetCaller.rc());
|
---|
4488 |
|
---|
4489 | HRESULT rc = S_OK;
|
---|
4490 | fMergeForward = false;
|
---|
4491 | pParentForTarget.setNull();
|
---|
4492 | aChildrenToReparent.clear();
|
---|
4493 | Assert(aMediumLockList == NULL);
|
---|
4494 | aMediumLockList = NULL;
|
---|
4495 |
|
---|
4496 | try
|
---|
4497 | {
|
---|
4498 | // locking: we need the tree lock first because we access parent pointers
|
---|
4499 | AutoWriteLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4500 |
|
---|
4501 | /* more sanity checking and figuring out the merge direction */
|
---|
4502 | ComObjPtr<Medium> pMedium = getParent();
|
---|
4503 | while (!pMedium.isNull() && pMedium != pTarget)
|
---|
4504 | pMedium = pMedium->getParent();
|
---|
4505 | if (pMedium == pTarget)
|
---|
4506 | fMergeForward = false;
|
---|
4507 | else
|
---|
4508 | {
|
---|
4509 | pMedium = pTarget->getParent();
|
---|
4510 | while (!pMedium.isNull() && pMedium != this)
|
---|
4511 | pMedium = pMedium->getParent();
|
---|
4512 | if (pMedium == this)
|
---|
4513 | fMergeForward = true;
|
---|
4514 | else
|
---|
4515 | {
|
---|
4516 | Utf8Str tgtLoc;
|
---|
4517 | {
|
---|
4518 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
4519 | tgtLoc = pTarget->getLocationFull();
|
---|
4520 | }
|
---|
4521 |
|
---|
4522 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4523 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4524 | tr("Media '%s' and '%s' are unrelated"),
|
---|
4525 | m->strLocationFull.c_str(), tgtLoc.c_str());
|
---|
4526 | }
|
---|
4527 | }
|
---|
4528 |
|
---|
4529 | /* Build the lock list. */
|
---|
4530 | aMediumLockList = new MediumLockList();
|
---|
4531 | treeLock.release();
|
---|
4532 | if (fMergeForward)
|
---|
4533 | rc = pTarget->createMediumLockList(true /* fFailIfInaccessible */,
|
---|
4534 | true /* fMediumLockWrite */,
|
---|
4535 | NULL,
|
---|
4536 | *aMediumLockList);
|
---|
4537 | else
|
---|
4538 | rc = createMediumLockList(true /* fFailIfInaccessible */,
|
---|
4539 | false /* fMediumLockWrite */,
|
---|
4540 | NULL,
|
---|
4541 | *aMediumLockList);
|
---|
4542 | treeLock.acquire();
|
---|
4543 | if (FAILED(rc))
|
---|
4544 | throw rc;
|
---|
4545 |
|
---|
4546 | /* Sanity checking, must be after lock list creation as it depends on
|
---|
4547 | * valid medium states. The medium objects must be accessible. Only
|
---|
4548 | * do this if immediate locking is requested, otherwise it fails when
|
---|
4549 | * we construct a medium lock list for an already running VM. Snapshot
|
---|
4550 | * deletion uses this to simplify its life. */
|
---|
4551 | if (fLockMedia)
|
---|
4552 | {
|
---|
4553 | {
|
---|
4554 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4555 | if (m->state != MediumState_Created)
|
---|
4556 | throw setStateError();
|
---|
4557 | }
|
---|
4558 | {
|
---|
4559 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
4560 | if (pTarget->m->state != MediumState_Created)
|
---|
4561 | throw pTarget->setStateError();
|
---|
4562 | }
|
---|
4563 | }
|
---|
4564 |
|
---|
4565 | /* check medium attachment and other sanity conditions */
|
---|
4566 | if (fMergeForward)
|
---|
4567 | {
|
---|
4568 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4569 | if (getChildren().size() > 1)
|
---|
4570 | {
|
---|
4571 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4572 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
4573 | m->strLocationFull.c_str(), getChildren().size());
|
---|
4574 | }
|
---|
4575 | /* One backreference is only allowed if the machine ID is not empty
|
---|
4576 | * and it matches the machine the medium is attached to (including
|
---|
4577 | * the snapshot ID if not empty). */
|
---|
4578 | if ( m->backRefs.size() != 0
|
---|
4579 | && ( !aMachineId
|
---|
4580 | || m->backRefs.size() != 1
|
---|
4581 | || aMachineId->isEmpty()
|
---|
4582 | || *getFirstMachineBackrefId() != *aMachineId
|
---|
4583 | || ( (!aSnapshotId || !aSnapshotId->isEmpty())
|
---|
4584 | && *getFirstMachineBackrefSnapshotId() != *aSnapshotId)))
|
---|
4585 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
4586 | tr("Medium '%s' is attached to %d virtual machines"),
|
---|
4587 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
4588 | if (m->type == MediumType_Immutable)
|
---|
4589 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4590 | tr("Medium '%s' is immutable"),
|
---|
4591 | m->strLocationFull.c_str());
|
---|
4592 | if (m->type == MediumType_MultiAttach)
|
---|
4593 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4594 | tr("Medium '%s' is multi-attach"),
|
---|
4595 | m->strLocationFull.c_str());
|
---|
4596 | }
|
---|
4597 | else
|
---|
4598 | {
|
---|
4599 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
4600 | if (pTarget->getChildren().size() > 1)
|
---|
4601 | {
|
---|
4602 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
4603 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
4604 | pTarget->m->strLocationFull.c_str(),
|
---|
4605 | pTarget->getChildren().size());
|
---|
4606 | }
|
---|
4607 | if (pTarget->m->type == MediumType_Immutable)
|
---|
4608 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4609 | tr("Medium '%s' is immutable"),
|
---|
4610 | pTarget->m->strLocationFull.c_str());
|
---|
4611 | if (pTarget->m->type == MediumType_MultiAttach)
|
---|
4612 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4613 | tr("Medium '%s' is multi-attach"),
|
---|
4614 | pTarget->m->strLocationFull.c_str());
|
---|
4615 | }
|
---|
4616 | ComObjPtr<Medium> pLast(fMergeForward ? (Medium *)pTarget : this);
|
---|
4617 | ComObjPtr<Medium> pLastIntermediate = pLast->getParent();
|
---|
4618 | for (pLast = pLastIntermediate;
|
---|
4619 | !pLast.isNull() && pLast != pTarget && pLast != this;
|
---|
4620 | pLast = pLast->getParent())
|
---|
4621 | {
|
---|
4622 | AutoReadLock alock(pLast COMMA_LOCKVAL_SRC_POS);
|
---|
4623 | if (pLast->getChildren().size() > 1)
|
---|
4624 | {
|
---|
4625 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
4626 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
4627 | pLast->m->strLocationFull.c_str(),
|
---|
4628 | pLast->getChildren().size());
|
---|
4629 | }
|
---|
4630 | if (pLast->m->backRefs.size() != 0)
|
---|
4631 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
4632 | tr("Medium '%s' is attached to %d virtual machines"),
|
---|
4633 | pLast->m->strLocationFull.c_str(),
|
---|
4634 | pLast->m->backRefs.size());
|
---|
4635 |
|
---|
4636 | }
|
---|
4637 |
|
---|
4638 | /* Update medium states appropriately */
|
---|
4639 | {
|
---|
4640 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4641 |
|
---|
4642 | if (m->state == MediumState_Created)
|
---|
4643 | {
|
---|
4644 | rc = markForDeletion();
|
---|
4645 | if (FAILED(rc))
|
---|
4646 | throw rc;
|
---|
4647 | }
|
---|
4648 | else
|
---|
4649 | {
|
---|
4650 | if (fLockMedia)
|
---|
4651 | throw setStateError();
|
---|
4652 | else if ( m->state == MediumState_LockedWrite
|
---|
4653 | || m->state == MediumState_LockedRead)
|
---|
4654 | {
|
---|
4655 | /* Either mark it for deletion in locked state or allow
|
---|
4656 | * others to have done so. */
|
---|
4657 | if (m->preLockState == MediumState_Created)
|
---|
4658 | markLockedForDeletion();
|
---|
4659 | else if (m->preLockState != MediumState_Deleting)
|
---|
4660 | throw setStateError();
|
---|
4661 | }
|
---|
4662 | else
|
---|
4663 | throw setStateError();
|
---|
4664 | }
|
---|
4665 | }
|
---|
4666 |
|
---|
4667 | if (fMergeForward)
|
---|
4668 | {
|
---|
4669 | /* we will need parent to reparent target */
|
---|
4670 | pParentForTarget = getParent();
|
---|
4671 | }
|
---|
4672 | else
|
---|
4673 | {
|
---|
4674 | /* we will need to reparent children of the source */
|
---|
4675 | for (MediaList::const_iterator it = getChildren().begin();
|
---|
4676 | it != getChildren().end();
|
---|
4677 | ++it)
|
---|
4678 | {
|
---|
4679 | pMedium = *it;
|
---|
4680 | if (fLockMedia)
|
---|
4681 | {
|
---|
4682 | rc = pMedium->LockWrite(NULL);
|
---|
4683 | if (FAILED(rc))
|
---|
4684 | throw rc;
|
---|
4685 | }
|
---|
4686 |
|
---|
4687 | aChildrenToReparent.push_back(pMedium);
|
---|
4688 | }
|
---|
4689 | }
|
---|
4690 | for (pLast = pLastIntermediate;
|
---|
4691 | !pLast.isNull() && pLast != pTarget && pLast != this;
|
---|
4692 | pLast = pLast->getParent())
|
---|
4693 | {
|
---|
4694 | AutoWriteLock alock(pLast COMMA_LOCKVAL_SRC_POS);
|
---|
4695 | if (pLast->m->state == MediumState_Created)
|
---|
4696 | {
|
---|
4697 | rc = pLast->markForDeletion();
|
---|
4698 | if (FAILED(rc))
|
---|
4699 | throw rc;
|
---|
4700 | }
|
---|
4701 | else
|
---|
4702 | throw pLast->setStateError();
|
---|
4703 | }
|
---|
4704 |
|
---|
4705 | /* Tweak the lock list in the backward merge case, as the target
|
---|
4706 | * isn't marked to be locked for writing yet. */
|
---|
4707 | if (!fMergeForward)
|
---|
4708 | {
|
---|
4709 | MediumLockList::Base::iterator lockListBegin =
|
---|
4710 | aMediumLockList->GetBegin();
|
---|
4711 | MediumLockList::Base::iterator lockListEnd =
|
---|
4712 | aMediumLockList->GetEnd();
|
---|
4713 | lockListEnd--;
|
---|
4714 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
4715 | it != lockListEnd;
|
---|
4716 | ++it)
|
---|
4717 | {
|
---|
4718 | MediumLock &mediumLock = *it;
|
---|
4719 | if (mediumLock.GetMedium() == pTarget)
|
---|
4720 | {
|
---|
4721 | HRESULT rc2 = mediumLock.UpdateLock(true);
|
---|
4722 | AssertComRC(rc2);
|
---|
4723 | break;
|
---|
4724 | }
|
---|
4725 | }
|
---|
4726 | }
|
---|
4727 |
|
---|
4728 | if (fLockMedia)
|
---|
4729 | {
|
---|
4730 | treeLock.release();
|
---|
4731 | rc = aMediumLockList->Lock();
|
---|
4732 | treeLock.acquire();
|
---|
4733 | if (FAILED(rc))
|
---|
4734 | {
|
---|
4735 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
4736 | throw setError(rc,
|
---|
4737 | tr("Failed to lock media when merging to '%s'"),
|
---|
4738 | pTarget->getLocationFull().c_str());
|
---|
4739 | }
|
---|
4740 | }
|
---|
4741 | }
|
---|
4742 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4743 |
|
---|
4744 | if (FAILED(rc))
|
---|
4745 | {
|
---|
4746 | delete aMediumLockList;
|
---|
4747 | aMediumLockList = NULL;
|
---|
4748 | }
|
---|
4749 |
|
---|
4750 | return rc;
|
---|
4751 | }
|
---|
4752 |
|
---|
4753 | /**
|
---|
4754 | * Merges this medium to the specified medium which must be either its
|
---|
4755 | * direct ancestor or descendant.
|
---|
4756 | *
|
---|
4757 | * Given this medium is SOURCE and the specified medium is TARGET, we will
|
---|
4758 | * get two variants of the merge operation:
|
---|
4759 | *
|
---|
4760 | * forward merge
|
---|
4761 | * ------------------------->
|
---|
4762 | * [Extra] <- SOURCE <- Intermediate <- TARGET
|
---|
4763 | * Any Del Del LockWr
|
---|
4764 | *
|
---|
4765 | *
|
---|
4766 | * backward merge
|
---|
4767 | * <-------------------------
|
---|
4768 | * TARGET <- Intermediate <- SOURCE <- [Extra]
|
---|
4769 | * LockWr Del Del LockWr
|
---|
4770 | *
|
---|
4771 | * Each diagram shows the involved media on the media chain where
|
---|
4772 | * SOURCE and TARGET belong. Under each medium there is a state value which
|
---|
4773 | * the medium must have at a time of the mergeTo() call.
|
---|
4774 | *
|
---|
4775 | * The media in the square braces may be absent (e.g. when the forward
|
---|
4776 | * operation takes place and SOURCE is the base medium, or when the backward
|
---|
4777 | * merge operation takes place and TARGET is the last child in the chain) but if
|
---|
4778 | * they present they are involved too as shown.
|
---|
4779 | *
|
---|
4780 | * Neither the source medium nor intermediate media may be attached to
|
---|
4781 | * any VM directly or in the snapshot, otherwise this method will assert.
|
---|
4782 | *
|
---|
4783 | * The #prepareMergeTo() method must be called prior to this method to place all
|
---|
4784 | * involved to necessary states and perform other consistency checks.
|
---|
4785 | *
|
---|
4786 | * If @a aWait is @c true then this method will perform the operation on the
|
---|
4787 | * calling thread and will not return to the caller until the operation is
|
---|
4788 | * completed. When this method succeeds, all intermediate medium objects in
|
---|
4789 | * the chain will be uninitialized, the state of the target medium (and all
|
---|
4790 | * involved extra media) will be restored. @a aMediumLockList will not be
|
---|
4791 | * deleted, whether the operation is successful or not. The caller has to do
|
---|
4792 | * this if appropriate. Note that this (source) medium is not uninitialized
|
---|
4793 | * because of possible AutoCaller instances held by the caller of this method
|
---|
4794 | * on the current thread. It's therefore the responsibility of the caller to
|
---|
4795 | * call Medium::uninit() after releasing all callers.
|
---|
4796 | *
|
---|
4797 | * If @a aWait is @c false then this method will create a thread to perform the
|
---|
4798 | * operation asynchronously and will return immediately. If the operation
|
---|
4799 | * succeeds, the thread will uninitialize the source medium object and all
|
---|
4800 | * intermediate medium objects in the chain, reset the state of the target
|
---|
4801 | * medium (and all involved extra media) and delete @a aMediumLockList.
|
---|
4802 | * If the operation fails, the thread will only reset the states of all
|
---|
4803 | * involved media and delete @a aMediumLockList.
|
---|
4804 | *
|
---|
4805 | * When this method fails (regardless of the @a aWait mode), it is a caller's
|
---|
4806 | * responsibility to undo state changes and delete @a aMediumLockList using
|
---|
4807 | * #cancelMergeTo().
|
---|
4808 | *
|
---|
4809 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
4810 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
4811 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
4812 | * progress object is created/used at all. Note that @a aProgress cannot be
|
---|
4813 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
4814 | *
|
---|
4815 | * @param pTarget Target medium.
|
---|
4816 | * @param fMergeForward Merge direction.
|
---|
4817 | * @param pParentForTarget New parent for target medium after merge.
|
---|
4818 | * @param aChildrenToReparent List of children of the source which will have
|
---|
4819 | * to be reparented to the target after merge.
|
---|
4820 | * @param aMediumLockList Medium locking information.
|
---|
4821 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
4822 | * completion.
|
---|
4823 | * @param aWait @c true if this method should block instead of creating
|
---|
4824 | * an asynchronous thread.
|
---|
4825 | *
|
---|
4826 | * @note Locks the tree lock for writing. Locks the media from the chain
|
---|
4827 | * for writing.
|
---|
4828 | */
|
---|
4829 | HRESULT Medium::mergeTo(const ComObjPtr<Medium> &pTarget,
|
---|
4830 | bool fMergeForward,
|
---|
4831 | const ComObjPtr<Medium> &pParentForTarget,
|
---|
4832 | const MediaList &aChildrenToReparent,
|
---|
4833 | MediumLockList *aMediumLockList,
|
---|
4834 | ComObjPtr <Progress> *aProgress,
|
---|
4835 | bool aWait)
|
---|
4836 | {
|
---|
4837 | AssertReturn(pTarget != NULL, E_FAIL);
|
---|
4838 | AssertReturn(pTarget != this, E_FAIL);
|
---|
4839 | AssertReturn(aMediumLockList != NULL, E_FAIL);
|
---|
4840 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
4841 |
|
---|
4842 | AutoCaller autoCaller(this);
|
---|
4843 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4844 |
|
---|
4845 | AutoCaller targetCaller(pTarget);
|
---|
4846 | AssertComRCReturnRC(targetCaller.rc());
|
---|
4847 |
|
---|
4848 | HRESULT rc = S_OK;
|
---|
4849 | ComObjPtr <Progress> pProgress;
|
---|
4850 | Medium::Task *pTask = NULL;
|
---|
4851 |
|
---|
4852 | try
|
---|
4853 | {
|
---|
4854 | if (aProgress != NULL)
|
---|
4855 | {
|
---|
4856 | /* use the existing progress object... */
|
---|
4857 | pProgress = *aProgress;
|
---|
4858 |
|
---|
4859 | /* ...but create a new one if it is null */
|
---|
4860 | if (pProgress.isNull())
|
---|
4861 | {
|
---|
4862 | Utf8Str tgtName;
|
---|
4863 | {
|
---|
4864 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
4865 | tgtName = pTarget->getName();
|
---|
4866 | }
|
---|
4867 |
|
---|
4868 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4869 |
|
---|
4870 | pProgress.createObject();
|
---|
4871 | rc = pProgress->init(m->pVirtualBox,
|
---|
4872 | static_cast<IMedium*>(this),
|
---|
4873 | BstrFmt(tr("Merging medium '%s' to '%s'"),
|
---|
4874 | getName().c_str(),
|
---|
4875 | tgtName.c_str()).raw(),
|
---|
4876 | TRUE /* aCancelable */);
|
---|
4877 | if (FAILED(rc))
|
---|
4878 | throw rc;
|
---|
4879 | }
|
---|
4880 | }
|
---|
4881 |
|
---|
4882 | /* setup task object to carry out the operation sync/async */
|
---|
4883 | pTask = new Medium::MergeTask(this, pTarget, fMergeForward,
|
---|
4884 | pParentForTarget, aChildrenToReparent,
|
---|
4885 | pProgress, aMediumLockList,
|
---|
4886 | aWait /* fKeepMediumLockList */);
|
---|
4887 | rc = pTask->rc();
|
---|
4888 | AssertComRC(rc);
|
---|
4889 | if (FAILED(rc))
|
---|
4890 | throw rc;
|
---|
4891 | }
|
---|
4892 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4893 |
|
---|
4894 | if (SUCCEEDED(rc))
|
---|
4895 | {
|
---|
4896 | if (aWait)
|
---|
4897 | rc = runNow(pTask);
|
---|
4898 | else
|
---|
4899 | rc = startThread(pTask);
|
---|
4900 |
|
---|
4901 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
4902 | *aProgress = pProgress;
|
---|
4903 | }
|
---|
4904 | else if (pTask != NULL)
|
---|
4905 | delete pTask;
|
---|
4906 |
|
---|
4907 | return rc;
|
---|
4908 | }
|
---|
4909 |
|
---|
4910 | /**
|
---|
4911 | * Undoes what #prepareMergeTo() did. Must be called if #mergeTo() is not
|
---|
4912 | * called or fails. Frees memory occupied by @a aMediumLockList and unlocks
|
---|
4913 | * the medium objects in @a aChildrenToReparent.
|
---|
4914 | *
|
---|
4915 | * @param aChildrenToReparent List of children of the source which will have
|
---|
4916 | * to be reparented to the target after merge.
|
---|
4917 | * @param aMediumLockList Medium locking information.
|
---|
4918 | *
|
---|
4919 | * @note Locks the media from the chain for writing.
|
---|
4920 | */
|
---|
4921 | void Medium::cancelMergeTo(const MediaList &aChildrenToReparent,
|
---|
4922 | MediumLockList *aMediumLockList)
|
---|
4923 | {
|
---|
4924 | AutoCaller autoCaller(this);
|
---|
4925 | AssertComRCReturnVoid(autoCaller.rc());
|
---|
4926 |
|
---|
4927 | AssertReturnVoid(aMediumLockList != NULL);
|
---|
4928 |
|
---|
4929 | /* Revert media marked for deletion to previous state. */
|
---|
4930 | HRESULT rc;
|
---|
4931 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
4932 | aMediumLockList->GetBegin();
|
---|
4933 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
4934 | aMediumLockList->GetEnd();
|
---|
4935 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
4936 | it != mediumListEnd;
|
---|
4937 | ++it)
|
---|
4938 | {
|
---|
4939 | const MediumLock &mediumLock = *it;
|
---|
4940 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
4941 | AutoWriteLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
4942 |
|
---|
4943 | if (pMedium->m->state == MediumState_Deleting)
|
---|
4944 | {
|
---|
4945 | rc = pMedium->unmarkForDeletion();
|
---|
4946 | AssertComRC(rc);
|
---|
4947 | }
|
---|
4948 | }
|
---|
4949 |
|
---|
4950 | /* the destructor will do the work */
|
---|
4951 | delete aMediumLockList;
|
---|
4952 |
|
---|
4953 | /* unlock the children which had to be reparented */
|
---|
4954 | for (MediaList::const_iterator it = aChildrenToReparent.begin();
|
---|
4955 | it != aChildrenToReparent.end();
|
---|
4956 | ++it)
|
---|
4957 | {
|
---|
4958 | const ComObjPtr<Medium> &pMedium = *it;
|
---|
4959 |
|
---|
4960 | AutoWriteLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
4961 | pMedium->UnlockWrite(NULL);
|
---|
4962 | }
|
---|
4963 | }
|
---|
4964 |
|
---|
4965 | /**
|
---|
4966 | * Fix the parent UUID of all children to point to this medium as their
|
---|
4967 | * parent.
|
---|
4968 | */
|
---|
4969 | HRESULT Medium::fixParentUuidOfChildren(const MediaList &childrenToReparent)
|
---|
4970 | {
|
---|
4971 | Assert(!isWriteLockOnCurrentThread());
|
---|
4972 | Assert(!m->pVirtualBox->getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
4973 | MediumLockList mediumLockList;
|
---|
4974 | HRESULT rc = createMediumLockList(true /* fFailIfInaccessible */,
|
---|
4975 | false /* fMediumLockWrite */,
|
---|
4976 | this,
|
---|
4977 | mediumLockList);
|
---|
4978 | AssertComRCReturnRC(rc);
|
---|
4979 |
|
---|
4980 | try
|
---|
4981 | {
|
---|
4982 | PVBOXHDD hdd;
|
---|
4983 | int vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &hdd);
|
---|
4984 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
4985 |
|
---|
4986 | try
|
---|
4987 | {
|
---|
4988 | MediumLockList::Base::iterator lockListBegin =
|
---|
4989 | mediumLockList.GetBegin();
|
---|
4990 | MediumLockList::Base::iterator lockListEnd =
|
---|
4991 | mediumLockList.GetEnd();
|
---|
4992 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
4993 | it != lockListEnd;
|
---|
4994 | ++it)
|
---|
4995 | {
|
---|
4996 | MediumLock &mediumLock = *it;
|
---|
4997 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
4998 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
4999 |
|
---|
5000 | // open the medium
|
---|
5001 | vrc = VDOpen(hdd,
|
---|
5002 | pMedium->m->strFormat.c_str(),
|
---|
5003 | pMedium->m->strLocationFull.c_str(),
|
---|
5004 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
5005 | pMedium->m->vdImageIfaces);
|
---|
5006 | if (RT_FAILURE(vrc))
|
---|
5007 | throw vrc;
|
---|
5008 | }
|
---|
5009 |
|
---|
5010 | for (MediaList::const_iterator it = childrenToReparent.begin();
|
---|
5011 | it != childrenToReparent.end();
|
---|
5012 | ++it)
|
---|
5013 | {
|
---|
5014 | /* VD_OPEN_FLAGS_INFO since UUID is wrong yet */
|
---|
5015 | vrc = VDOpen(hdd,
|
---|
5016 | (*it)->m->strFormat.c_str(),
|
---|
5017 | (*it)->m->strLocationFull.c_str(),
|
---|
5018 | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
5019 | (*it)->m->vdImageIfaces);
|
---|
5020 | if (RT_FAILURE(vrc))
|
---|
5021 | throw vrc;
|
---|
5022 |
|
---|
5023 | vrc = VDSetParentUuid(hdd, VD_LAST_IMAGE, m->id.raw());
|
---|
5024 | if (RT_FAILURE(vrc))
|
---|
5025 | throw vrc;
|
---|
5026 |
|
---|
5027 | vrc = VDClose(hdd, false /* fDelete */);
|
---|
5028 | if (RT_FAILURE(vrc))
|
---|
5029 | throw vrc;
|
---|
5030 |
|
---|
5031 | (*it)->UnlockWrite(NULL);
|
---|
5032 | }
|
---|
5033 | }
|
---|
5034 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5035 | catch (int aVRC)
|
---|
5036 | {
|
---|
5037 | rc = setError(E_FAIL,
|
---|
5038 | tr("Could not update medium UUID references to parent '%s' (%s)"),
|
---|
5039 | m->strLocationFull.c_str(),
|
---|
5040 | vdError(aVRC).c_str());
|
---|
5041 | }
|
---|
5042 |
|
---|
5043 | VDDestroy(hdd);
|
---|
5044 | }
|
---|
5045 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5046 |
|
---|
5047 | return rc;
|
---|
5048 | }
|
---|
5049 |
|
---|
5050 | /**
|
---|
5051 | * Used by IAppliance to export disk images.
|
---|
5052 | *
|
---|
5053 | * @param aFilename Filename to create (UTF8).
|
---|
5054 | * @param aFormat Medium format for creating @a aFilename.
|
---|
5055 | * @param aVariant Which exact image format variant to use
|
---|
5056 | * for the destination image.
|
---|
5057 | * @param aVDImageIOCallbacks Pointer to the callback table for a
|
---|
5058 | * VDINTERFACEIO interface. May be NULL.
|
---|
5059 | * @param aVDImageIOUser Opaque data for the callbacks.
|
---|
5060 | * @param aProgress Progress object to use.
|
---|
5061 | * @return
|
---|
5062 | * @note The source format is defined by the Medium instance.
|
---|
5063 | */
|
---|
5064 | HRESULT Medium::exportFile(const char *aFilename,
|
---|
5065 | const ComObjPtr<MediumFormat> &aFormat,
|
---|
5066 | MediumVariant_T aVariant,
|
---|
5067 | PVDINTERFACEIO aVDImageIOIf, void *aVDImageIOUser,
|
---|
5068 | const ComObjPtr<Progress> &aProgress)
|
---|
5069 | {
|
---|
5070 | AssertPtrReturn(aFilename, E_INVALIDARG);
|
---|
5071 | AssertReturn(!aFormat.isNull(), E_INVALIDARG);
|
---|
5072 | AssertReturn(!aProgress.isNull(), E_INVALIDARG);
|
---|
5073 |
|
---|
5074 | AutoCaller autoCaller(this);
|
---|
5075 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
5076 |
|
---|
5077 | HRESULT rc = S_OK;
|
---|
5078 | Medium::Task *pTask = NULL;
|
---|
5079 |
|
---|
5080 | try
|
---|
5081 | {
|
---|
5082 | // This needs no extra locks besides what is done in the called methods.
|
---|
5083 |
|
---|
5084 | /* Build the source lock list. */
|
---|
5085 | MediumLockList *pSourceMediumLockList(new MediumLockList());
|
---|
5086 | rc = createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5087 | false /* fMediumLockWrite */,
|
---|
5088 | NULL,
|
---|
5089 | *pSourceMediumLockList);
|
---|
5090 | if (FAILED(rc))
|
---|
5091 | {
|
---|
5092 | delete pSourceMediumLockList;
|
---|
5093 | throw rc;
|
---|
5094 | }
|
---|
5095 |
|
---|
5096 | rc = pSourceMediumLockList->Lock();
|
---|
5097 | if (FAILED(rc))
|
---|
5098 | {
|
---|
5099 | delete pSourceMediumLockList;
|
---|
5100 | throw setError(rc,
|
---|
5101 | tr("Failed to lock source media '%s'"),
|
---|
5102 | getLocationFull().c_str());
|
---|
5103 | }
|
---|
5104 |
|
---|
5105 | /* setup task object to carry out the operation asynchronously */
|
---|
5106 | pTask = new Medium::ExportTask(this, aProgress, aFilename, aFormat,
|
---|
5107 | aVariant, aVDImageIOIf,
|
---|
5108 | aVDImageIOUser, pSourceMediumLockList);
|
---|
5109 | rc = pTask->rc();
|
---|
5110 | AssertComRC(rc);
|
---|
5111 | if (FAILED(rc))
|
---|
5112 | throw rc;
|
---|
5113 | }
|
---|
5114 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5115 |
|
---|
5116 | if (SUCCEEDED(rc))
|
---|
5117 | rc = startThread(pTask);
|
---|
5118 | else if (pTask != NULL)
|
---|
5119 | delete pTask;
|
---|
5120 |
|
---|
5121 | return rc;
|
---|
5122 | }
|
---|
5123 |
|
---|
5124 | /**
|
---|
5125 | * Used by IAppliance to import disk images.
|
---|
5126 | *
|
---|
5127 | * @param aFilename Filename to read (UTF8).
|
---|
5128 | * @param aFormat Medium format for reading @a aFilename.
|
---|
5129 | * @param aVariant Which exact image format variant to use
|
---|
5130 | * for the destination image.
|
---|
5131 | * @param aVDImageIOCallbacks Pointer to the callback table for a
|
---|
5132 | * VDINTERFACEIO interface. May be NULL.
|
---|
5133 | * @param aVDImageIOUser Opaque data for the callbacks.
|
---|
5134 | * @param aParent Parent medium. May be NULL.
|
---|
5135 | * @param aProgress Progress object to use.
|
---|
5136 | * @return
|
---|
5137 | * @note The destination format is defined by the Medium instance.
|
---|
5138 | */
|
---|
5139 | HRESULT Medium::importFile(const char *aFilename,
|
---|
5140 | const ComObjPtr<MediumFormat> &aFormat,
|
---|
5141 | MediumVariant_T aVariant,
|
---|
5142 | PVDINTERFACEIO aVDImageIOIf, void *aVDImageIOUser,
|
---|
5143 | const ComObjPtr<Medium> &aParent,
|
---|
5144 | const ComObjPtr<Progress> &aProgress)
|
---|
5145 | {
|
---|
5146 | AssertPtrReturn(aFilename, E_INVALIDARG);
|
---|
5147 | AssertReturn(!aFormat.isNull(), E_INVALIDARG);
|
---|
5148 | AssertReturn(!aProgress.isNull(), E_INVALIDARG);
|
---|
5149 |
|
---|
5150 | AutoCaller autoCaller(this);
|
---|
5151 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
5152 |
|
---|
5153 | HRESULT rc = S_OK;
|
---|
5154 | Medium::Task *pTask = NULL;
|
---|
5155 |
|
---|
5156 | try
|
---|
5157 | {
|
---|
5158 | // locking: we need the tree lock first because we access parent pointers
|
---|
5159 | // and we need to write-lock the media involved
|
---|
5160 | uint32_t cHandles = 2;
|
---|
5161 | LockHandle* pHandles[3] = { &m->pVirtualBox->getMediaTreeLockHandle(),
|
---|
5162 | this->lockHandle() };
|
---|
5163 | /* Only add parent to the lock if it is not null */
|
---|
5164 | if (!aParent.isNull())
|
---|
5165 | pHandles[cHandles++] = aParent->lockHandle();
|
---|
5166 | AutoWriteLock alock(cHandles,
|
---|
5167 | pHandles
|
---|
5168 | COMMA_LOCKVAL_SRC_POS);
|
---|
5169 |
|
---|
5170 | if ( m->state != MediumState_NotCreated
|
---|
5171 | && m->state != MediumState_Created)
|
---|
5172 | throw setStateError();
|
---|
5173 |
|
---|
5174 | /* Build the target lock list. */
|
---|
5175 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
5176 | alock.release();
|
---|
5177 | rc = createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5178 | true /* fMediumLockWrite */,
|
---|
5179 | aParent,
|
---|
5180 | *pTargetMediumLockList);
|
---|
5181 | alock.acquire();
|
---|
5182 | if (FAILED(rc))
|
---|
5183 | {
|
---|
5184 | delete pTargetMediumLockList;
|
---|
5185 | throw rc;
|
---|
5186 | }
|
---|
5187 |
|
---|
5188 | alock.release();
|
---|
5189 | rc = pTargetMediumLockList->Lock();
|
---|
5190 | alock.acquire();
|
---|
5191 | if (FAILED(rc))
|
---|
5192 | {
|
---|
5193 | delete pTargetMediumLockList;
|
---|
5194 | throw setError(rc,
|
---|
5195 | tr("Failed to lock target media '%s'"),
|
---|
5196 | getLocationFull().c_str());
|
---|
5197 | }
|
---|
5198 |
|
---|
5199 | /* setup task object to carry out the operation asynchronously */
|
---|
5200 | pTask = new Medium::ImportTask(this, aProgress, aFilename, aFormat,
|
---|
5201 | aVariant, aVDImageIOIf,
|
---|
5202 | aVDImageIOUser, aParent,
|
---|
5203 | pTargetMediumLockList);
|
---|
5204 | rc = pTask->rc();
|
---|
5205 | AssertComRC(rc);
|
---|
5206 | if (FAILED(rc))
|
---|
5207 | throw rc;
|
---|
5208 |
|
---|
5209 | if (m->state == MediumState_NotCreated)
|
---|
5210 | m->state = MediumState_Creating;
|
---|
5211 | }
|
---|
5212 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5213 |
|
---|
5214 | if (SUCCEEDED(rc))
|
---|
5215 | rc = startThread(pTask);
|
---|
5216 | else if (pTask != NULL)
|
---|
5217 | delete pTask;
|
---|
5218 |
|
---|
5219 | return rc;
|
---|
5220 | }
|
---|
5221 |
|
---|
5222 | /**
|
---|
5223 | * Internal version of the public CloneTo API which allows to enable certain
|
---|
5224 | * optimizations to improve speed during VM cloning.
|
---|
5225 | *
|
---|
5226 | * @param aTarget Target medium
|
---|
5227 | * @param aVariant Which exact image format variant to use
|
---|
5228 | * for the destination image.
|
---|
5229 | * @param aParent Parent medium. May be NULL.
|
---|
5230 | * @param aProgress Progress object to use.
|
---|
5231 | * @param idxSrcImageSame The last image in the source chain which has the
|
---|
5232 | * same content as the given image in the destination
|
---|
5233 | * chain. Use UINT32_MAX to disable this optimization.
|
---|
5234 | * @param idxDstImageSame The last image in the destination chain which has the
|
---|
5235 | * same content as the given image in the source chain.
|
---|
5236 | * Use UINT32_MAX to disable this optimization.
|
---|
5237 | * @return
|
---|
5238 | */
|
---|
5239 | HRESULT Medium::cloneToEx(const ComObjPtr<Medium> &aTarget, ULONG aVariant,
|
---|
5240 | const ComObjPtr<Medium> &aParent, IProgress **aProgress,
|
---|
5241 | uint32_t idxSrcImageSame, uint32_t idxDstImageSame)
|
---|
5242 | {
|
---|
5243 | CheckComArgNotNull(aTarget);
|
---|
5244 | CheckComArgOutPointerValid(aProgress);
|
---|
5245 | ComAssertRet(aTarget != this, E_INVALIDARG);
|
---|
5246 |
|
---|
5247 | AutoCaller autoCaller(this);
|
---|
5248 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
5249 |
|
---|
5250 | HRESULT rc = S_OK;
|
---|
5251 | ComObjPtr<Progress> pProgress;
|
---|
5252 | Medium::Task *pTask = NULL;
|
---|
5253 |
|
---|
5254 | try
|
---|
5255 | {
|
---|
5256 | // locking: we need the tree lock first because we access parent pointers
|
---|
5257 | // and we need to write-lock the media involved
|
---|
5258 | uint32_t cHandles = 3;
|
---|
5259 | LockHandle* pHandles[4] = { &m->pVirtualBox->getMediaTreeLockHandle(),
|
---|
5260 | this->lockHandle(),
|
---|
5261 | aTarget->lockHandle() };
|
---|
5262 | /* Only add parent to the lock if it is not null */
|
---|
5263 | if (!aParent.isNull())
|
---|
5264 | pHandles[cHandles++] = aParent->lockHandle();
|
---|
5265 | AutoWriteLock alock(cHandles,
|
---|
5266 | pHandles
|
---|
5267 | COMMA_LOCKVAL_SRC_POS);
|
---|
5268 |
|
---|
5269 | if ( aTarget->m->state != MediumState_NotCreated
|
---|
5270 | && aTarget->m->state != MediumState_Created)
|
---|
5271 | throw aTarget->setStateError();
|
---|
5272 |
|
---|
5273 | /* Build the source lock list. */
|
---|
5274 | MediumLockList *pSourceMediumLockList(new MediumLockList());
|
---|
5275 | alock.release();
|
---|
5276 | rc = createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5277 | false /* fMediumLockWrite */,
|
---|
5278 | NULL,
|
---|
5279 | *pSourceMediumLockList);
|
---|
5280 | alock.acquire();
|
---|
5281 | if (FAILED(rc))
|
---|
5282 | {
|
---|
5283 | delete pSourceMediumLockList;
|
---|
5284 | throw rc;
|
---|
5285 | }
|
---|
5286 |
|
---|
5287 | /* Build the target lock list (including the to-be parent chain). */
|
---|
5288 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
5289 | alock.release();
|
---|
5290 | rc = aTarget->createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5291 | true /* fMediumLockWrite */,
|
---|
5292 | aParent,
|
---|
5293 | *pTargetMediumLockList);
|
---|
5294 | alock.acquire();
|
---|
5295 | if (FAILED(rc))
|
---|
5296 | {
|
---|
5297 | delete pSourceMediumLockList;
|
---|
5298 | delete pTargetMediumLockList;
|
---|
5299 | throw rc;
|
---|
5300 | }
|
---|
5301 |
|
---|
5302 | alock.release();
|
---|
5303 | rc = pSourceMediumLockList->Lock();
|
---|
5304 | alock.acquire();
|
---|
5305 | if (FAILED(rc))
|
---|
5306 | {
|
---|
5307 | delete pSourceMediumLockList;
|
---|
5308 | delete pTargetMediumLockList;
|
---|
5309 | throw setError(rc,
|
---|
5310 | tr("Failed to lock source media '%s'"),
|
---|
5311 | getLocationFull().c_str());
|
---|
5312 | }
|
---|
5313 | alock.release();
|
---|
5314 | rc = pTargetMediumLockList->Lock();
|
---|
5315 | alock.acquire();
|
---|
5316 | if (FAILED(rc))
|
---|
5317 | {
|
---|
5318 | delete pSourceMediumLockList;
|
---|
5319 | delete pTargetMediumLockList;
|
---|
5320 | throw setError(rc,
|
---|
5321 | tr("Failed to lock target media '%s'"),
|
---|
5322 | aTarget->getLocationFull().c_str());
|
---|
5323 | }
|
---|
5324 |
|
---|
5325 | pProgress.createObject();
|
---|
5326 | rc = pProgress->init(m->pVirtualBox,
|
---|
5327 | static_cast <IMedium *>(this),
|
---|
5328 | BstrFmt(tr("Creating clone medium '%s'"), aTarget->m->strLocationFull.c_str()).raw(),
|
---|
5329 | TRUE /* aCancelable */);
|
---|
5330 | if (FAILED(rc))
|
---|
5331 | {
|
---|
5332 | delete pSourceMediumLockList;
|
---|
5333 | delete pTargetMediumLockList;
|
---|
5334 | throw rc;
|
---|
5335 | }
|
---|
5336 |
|
---|
5337 | /* setup task object to carry out the operation asynchronously */
|
---|
5338 | pTask = new Medium::CloneTask(this, pProgress, aTarget,
|
---|
5339 | (MediumVariant_T)aVariant,
|
---|
5340 | aParent, idxSrcImageSame,
|
---|
5341 | idxDstImageSame, pSourceMediumLockList,
|
---|
5342 | pTargetMediumLockList);
|
---|
5343 | rc = pTask->rc();
|
---|
5344 | AssertComRC(rc);
|
---|
5345 | if (FAILED(rc))
|
---|
5346 | throw rc;
|
---|
5347 |
|
---|
5348 | if (aTarget->m->state == MediumState_NotCreated)
|
---|
5349 | aTarget->m->state = MediumState_Creating;
|
---|
5350 | }
|
---|
5351 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5352 |
|
---|
5353 | if (SUCCEEDED(rc))
|
---|
5354 | {
|
---|
5355 | rc = startThread(pTask);
|
---|
5356 |
|
---|
5357 | if (SUCCEEDED(rc))
|
---|
5358 | pProgress.queryInterfaceTo(aProgress);
|
---|
5359 | }
|
---|
5360 | else if (pTask != NULL)
|
---|
5361 | delete pTask;
|
---|
5362 |
|
---|
5363 | return rc;
|
---|
5364 | }
|
---|
5365 |
|
---|
5366 | ////////////////////////////////////////////////////////////////////////////////
|
---|
5367 | //
|
---|
5368 | // Private methods
|
---|
5369 | //
|
---|
5370 | ////////////////////////////////////////////////////////////////////////////////
|
---|
5371 |
|
---|
5372 | /**
|
---|
5373 | * Queries information from the medium.
|
---|
5374 | *
|
---|
5375 | * As a result of this call, the accessibility state and data members such as
|
---|
5376 | * size and description will be updated with the current information.
|
---|
5377 | *
|
---|
5378 | * @note This method may block during a system I/O call that checks storage
|
---|
5379 | * accessibility.
|
---|
5380 | *
|
---|
5381 | * @note Caller MUST NOT hold the media tree or medium lock.
|
---|
5382 | *
|
---|
5383 | * @note Locks mParent for reading. Locks this object for writing.
|
---|
5384 | *
|
---|
5385 | * @param fSetImageId Whether to reset the UUID contained in the image file to the UUID in the medium instance data (see SetIDs())
|
---|
5386 | * @param fSetParentId Whether to reset the parent UUID contained in the image file to the parent UUID in the medium instance data (see SetIDs())
|
---|
5387 | * @return
|
---|
5388 | */
|
---|
5389 | HRESULT Medium::queryInfo(bool fSetImageId, bool fSetParentId)
|
---|
5390 | {
|
---|
5391 | Assert(!isWriteLockOnCurrentThread());
|
---|
5392 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5393 |
|
---|
5394 | if ( m->state != MediumState_Created
|
---|
5395 | && m->state != MediumState_Inaccessible
|
---|
5396 | && m->state != MediumState_LockedRead)
|
---|
5397 | return E_FAIL;
|
---|
5398 |
|
---|
5399 | HRESULT rc = S_OK;
|
---|
5400 |
|
---|
5401 | int vrc = VINF_SUCCESS;
|
---|
5402 |
|
---|
5403 | /* check if a blocking queryInfo() call is in progress on some other thread,
|
---|
5404 | * and wait for it to finish if so instead of querying data ourselves */
|
---|
5405 | if (m->queryInfoRunning)
|
---|
5406 | {
|
---|
5407 | Assert( m->state == MediumState_LockedRead
|
---|
5408 | || m->state == MediumState_LockedWrite);
|
---|
5409 |
|
---|
5410 | while (m->queryInfoRunning)
|
---|
5411 | {
|
---|
5412 | alock.release();
|
---|
5413 | {
|
---|
5414 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
5415 | }
|
---|
5416 | alock.acquire();
|
---|
5417 | }
|
---|
5418 |
|
---|
5419 | return S_OK;
|
---|
5420 | }
|
---|
5421 |
|
---|
5422 | bool success = false;
|
---|
5423 | Utf8Str lastAccessError;
|
---|
5424 |
|
---|
5425 | /* are we dealing with a new medium constructed using the existing
|
---|
5426 | * location? */
|
---|
5427 | bool isImport = m->id.isEmpty();
|
---|
5428 | unsigned uOpenFlags = VD_OPEN_FLAGS_INFO;
|
---|
5429 |
|
---|
5430 | /* Note that we don't use VD_OPEN_FLAGS_READONLY when opening new
|
---|
5431 | * media because that would prevent necessary modifications
|
---|
5432 | * when opening media of some third-party formats for the first
|
---|
5433 | * time in VirtualBox (such as VMDK for which VDOpen() needs to
|
---|
5434 | * generate an UUID if it is missing) */
|
---|
5435 | if ( m->hddOpenMode == OpenReadOnly
|
---|
5436 | || m->type == MediumType_Readonly
|
---|
5437 | || (!isImport && !fSetImageId && !fSetParentId)
|
---|
5438 | )
|
---|
5439 | uOpenFlags |= VD_OPEN_FLAGS_READONLY;
|
---|
5440 |
|
---|
5441 | /* Open shareable medium with the appropriate flags */
|
---|
5442 | if (m->type == MediumType_Shareable)
|
---|
5443 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
5444 |
|
---|
5445 | /* Lock the medium, which makes the behavior much more consistent */
|
---|
5446 | alock.release();
|
---|
5447 | if (uOpenFlags & (VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SHAREABLE))
|
---|
5448 | rc = LockRead(NULL);
|
---|
5449 | else
|
---|
5450 | rc = LockWrite(NULL);
|
---|
5451 | if (FAILED(rc)) return rc;
|
---|
5452 | alock.acquire();
|
---|
5453 |
|
---|
5454 | /* Copies of the input state fields which are not read-only,
|
---|
5455 | * as we're dropping the lock. CAUTION: be extremely careful what
|
---|
5456 | * you do with the contents of this medium object, as you will
|
---|
5457 | * create races if there are concurrent changes. */
|
---|
5458 | Utf8Str format(m->strFormat);
|
---|
5459 | Utf8Str location(m->strLocationFull);
|
---|
5460 | ComObjPtr<MediumFormat> formatObj = m->formatObj;
|
---|
5461 |
|
---|
5462 | /* "Output" values which can't be set because the lock isn't held
|
---|
5463 | * at the time the values are determined. */
|
---|
5464 | Guid mediumId = m->id;
|
---|
5465 | uint64_t mediumSize = 0;
|
---|
5466 | uint64_t mediumLogicalSize = 0;
|
---|
5467 |
|
---|
5468 | /* Flag whether a base image has a non-zero parent UUID and thus
|
---|
5469 | * need repairing after it was closed again. */
|
---|
5470 | bool fRepairImageZeroParentUuid = false;
|
---|
5471 |
|
---|
5472 | /* release the object lock before a lengthy operation, and take the
|
---|
5473 | * opportunity to have a media tree lock, too, which isn't held initially */
|
---|
5474 | m->queryInfoRunning = true;
|
---|
5475 | alock.release();
|
---|
5476 | Assert(!isWriteLockOnCurrentThread());
|
---|
5477 | Assert(!m->pVirtualBox->getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
5478 | AutoWriteLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5479 | treeLock.release();
|
---|
5480 |
|
---|
5481 | /* Note that taking the queryInfoSem after leaving the object lock above
|
---|
5482 | * can lead to short spinning of the loops waiting for queryInfo() to
|
---|
5483 | * complete. This is unavoidable since the other order causes a lock order
|
---|
5484 | * violation: here it would be requesting the object lock (at the beginning
|
---|
5485 | * of the method), then queryInfoSem, and below the other way round. */
|
---|
5486 | AutoWriteLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
5487 |
|
---|
5488 | try
|
---|
5489 | {
|
---|
5490 | /* skip accessibility checks for host drives */
|
---|
5491 | if (m->hostDrive)
|
---|
5492 | {
|
---|
5493 | success = true;
|
---|
5494 | throw S_OK;
|
---|
5495 | }
|
---|
5496 |
|
---|
5497 | PVBOXHDD hdd;
|
---|
5498 | vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &hdd);
|
---|
5499 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
5500 |
|
---|
5501 | try
|
---|
5502 | {
|
---|
5503 | /** @todo This kind of opening of media is assuming that diff
|
---|
5504 | * media can be opened as base media. Should be documented that
|
---|
5505 | * it must work for all medium format backends. */
|
---|
5506 | vrc = VDOpen(hdd,
|
---|
5507 | format.c_str(),
|
---|
5508 | location.c_str(),
|
---|
5509 | uOpenFlags | m->uOpenFlagsDef,
|
---|
5510 | m->vdImageIfaces);
|
---|
5511 | if (RT_FAILURE(vrc))
|
---|
5512 | {
|
---|
5513 | lastAccessError = Utf8StrFmt(tr("Could not open the medium '%s'%s"),
|
---|
5514 | location.c_str(), vdError(vrc).c_str());
|
---|
5515 | throw S_OK;
|
---|
5516 | }
|
---|
5517 |
|
---|
5518 | if (formatObj->getCapabilities() & MediumFormatCapabilities_Uuid)
|
---|
5519 | {
|
---|
5520 | /* Modify the UUIDs if necessary. The associated fields are
|
---|
5521 | * not modified by other code, so no need to copy. */
|
---|
5522 | if (fSetImageId)
|
---|
5523 | {
|
---|
5524 | alock.acquire();
|
---|
5525 | vrc = VDSetUuid(hdd, 0, m->uuidImage.raw());
|
---|
5526 | alock.release();
|
---|
5527 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
5528 | mediumId = m->uuidImage;
|
---|
5529 | }
|
---|
5530 | if (fSetParentId)
|
---|
5531 | {
|
---|
5532 | alock.acquire();
|
---|
5533 | vrc = VDSetParentUuid(hdd, 0, m->uuidParentImage.raw());
|
---|
5534 | alock.release();
|
---|
5535 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
5536 | }
|
---|
5537 | /* zap the information, these are no long-term members */
|
---|
5538 | alock.acquire();
|
---|
5539 | unconst(m->uuidImage).clear();
|
---|
5540 | unconst(m->uuidParentImage).clear();
|
---|
5541 | alock.release();
|
---|
5542 |
|
---|
5543 | /* check the UUID */
|
---|
5544 | RTUUID uuid;
|
---|
5545 | vrc = VDGetUuid(hdd, 0, &uuid);
|
---|
5546 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
5547 |
|
---|
5548 | if (isImport)
|
---|
5549 | {
|
---|
5550 | mediumId = uuid;
|
---|
5551 |
|
---|
5552 | if (mediumId.isEmpty() && (m->hddOpenMode == OpenReadOnly))
|
---|
5553 | // only when importing a VDMK that has no UUID, create one in memory
|
---|
5554 | mediumId.create();
|
---|
5555 | }
|
---|
5556 | else
|
---|
5557 | {
|
---|
5558 | Assert(!mediumId.isEmpty());
|
---|
5559 |
|
---|
5560 | if (mediumId != uuid)
|
---|
5561 | {
|
---|
5562 | /** @todo r=klaus this always refers to VirtualBox.xml as the medium registry, even for new VMs */
|
---|
5563 | lastAccessError = Utf8StrFmt(
|
---|
5564 | tr("UUID {%RTuuid} of the medium '%s' does not match the value {%RTuuid} stored in the media registry ('%s')"),
|
---|
5565 | &uuid,
|
---|
5566 | location.c_str(),
|
---|
5567 | mediumId.raw(),
|
---|
5568 | m->pVirtualBox->settingsFilePath().c_str());
|
---|
5569 | throw S_OK;
|
---|
5570 | }
|
---|
5571 | }
|
---|
5572 | }
|
---|
5573 | else
|
---|
5574 | {
|
---|
5575 | /* the backend does not support storing UUIDs within the
|
---|
5576 | * underlying storage so use what we store in XML */
|
---|
5577 |
|
---|
5578 | if (fSetImageId)
|
---|
5579 | {
|
---|
5580 | /* set the UUID if an API client wants to change it */
|
---|
5581 | alock.acquire();
|
---|
5582 | mediumId = m->uuidImage;
|
---|
5583 | alock.release();
|
---|
5584 | }
|
---|
5585 | else if (isImport)
|
---|
5586 | {
|
---|
5587 | /* generate an UUID for an imported UUID-less medium */
|
---|
5588 | mediumId.create();
|
---|
5589 | }
|
---|
5590 | }
|
---|
5591 |
|
---|
5592 | /* set the image uuid before the below parent uuid handling code
|
---|
5593 | * might place it somewhere in the media tree, so that the medium
|
---|
5594 | * UUID is valid at this point */
|
---|
5595 | alock.acquire();
|
---|
5596 | if (isImport || fSetImageId)
|
---|
5597 | unconst(m->id) = mediumId;
|
---|
5598 | alock.release();
|
---|
5599 |
|
---|
5600 | /* get the medium variant */
|
---|
5601 | unsigned uImageFlags;
|
---|
5602 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
5603 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
5604 | alock.acquire();
|
---|
5605 | m->variant = (MediumVariant_T)uImageFlags;
|
---|
5606 | alock.release();
|
---|
5607 |
|
---|
5608 | /* check/get the parent uuid and update corresponding state */
|
---|
5609 | if (uImageFlags & VD_IMAGE_FLAGS_DIFF)
|
---|
5610 | {
|
---|
5611 | RTUUID parentId;
|
---|
5612 | vrc = VDGetParentUuid(hdd, 0, &parentId);
|
---|
5613 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
5614 |
|
---|
5615 | /* streamOptimized VMDK images are only accepted as base
|
---|
5616 | * images, as this allows automatic repair of OVF appliances.
|
---|
5617 | * Since such images don't support random writes they will not
|
---|
5618 | * be created for diff images. Only an overly smart user might
|
---|
5619 | * manually create this case. Too bad for him. */
|
---|
5620 | if ( isImport
|
---|
5621 | && !(uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
|
---|
5622 | {
|
---|
5623 | /* the parent must be known to us. Note that we freely
|
---|
5624 | * call locking methods of mVirtualBox and parent, as all
|
---|
5625 | * relevant locks must be already held. There may be no
|
---|
5626 | * concurrent access to the just opened medium on other
|
---|
5627 | * threads yet (and init() will fail if this method reports
|
---|
5628 | * MediumState_Inaccessible) */
|
---|
5629 |
|
---|
5630 | Guid id = parentId;
|
---|
5631 | ComObjPtr<Medium> pParent;
|
---|
5632 | rc = m->pVirtualBox->findHardDiskById(id, false /* aSetError */, &pParent);
|
---|
5633 | if (FAILED(rc))
|
---|
5634 | {
|
---|
5635 | lastAccessError = Utf8StrFmt(
|
---|
5636 | tr("Parent medium with UUID {%RTuuid} of the medium '%s' is not found in the media registry ('%s')"),
|
---|
5637 | &parentId, location.c_str(),
|
---|
5638 | m->pVirtualBox->settingsFilePath().c_str());
|
---|
5639 | throw S_OK;
|
---|
5640 | }
|
---|
5641 |
|
---|
5642 | /* we set mParent & children() */
|
---|
5643 | treeLock.acquire();
|
---|
5644 |
|
---|
5645 | Assert(m->pParent.isNull());
|
---|
5646 | m->pParent = pParent;
|
---|
5647 | m->pParent->m->llChildren.push_back(this);
|
---|
5648 |
|
---|
5649 | treeLock.release();
|
---|
5650 | }
|
---|
5651 | else
|
---|
5652 | {
|
---|
5653 | /* we access mParent */
|
---|
5654 | treeLock.acquire();
|
---|
5655 |
|
---|
5656 | /* check that parent UUIDs match. Note that there's no need
|
---|
5657 | * for the parent's AutoCaller (our lifetime is bound to
|
---|
5658 | * it) */
|
---|
5659 |
|
---|
5660 | if (m->pParent.isNull())
|
---|
5661 | {
|
---|
5662 | /* Due to a bug in VDCopy() in VirtualBox 3.0.0-3.0.14
|
---|
5663 | * and 3.1.0-3.1.8 there are base images out there
|
---|
5664 | * which have a non-zero parent UUID. No point in
|
---|
5665 | * complaining about them, instead automatically
|
---|
5666 | * repair the problem. Later we can bring back the
|
---|
5667 | * error message, but we should wait until really
|
---|
5668 | * most users have repaired their images, either with
|
---|
5669 | * VBoxFixHdd or this way. */
|
---|
5670 | #if 1
|
---|
5671 | fRepairImageZeroParentUuid = true;
|
---|
5672 | #else /* 0 */
|
---|
5673 | lastAccessError = Utf8StrFmt(
|
---|
5674 | tr("Medium type of '%s' is differencing but it is not associated with any parent medium in the media registry ('%s')"),
|
---|
5675 | location.c_str(),
|
---|
5676 | m->pVirtualBox->settingsFilePath().c_str());
|
---|
5677 | treeLock.release();
|
---|
5678 | throw S_OK;
|
---|
5679 | #endif /* 0 */
|
---|
5680 | }
|
---|
5681 |
|
---|
5682 | {
|
---|
5683 | AutoReadLock parentLock(m->pParent COMMA_LOCKVAL_SRC_POS);
|
---|
5684 | if ( !fRepairImageZeroParentUuid
|
---|
5685 | && m->pParent->getState() != MediumState_Inaccessible
|
---|
5686 | && m->pParent->getId() != parentId)
|
---|
5687 | {
|
---|
5688 | /** @todo r=klaus this always refers to VirtualBox.xml as the medium registry, even for new VMs */
|
---|
5689 | lastAccessError = Utf8StrFmt(
|
---|
5690 | tr("Parent UUID {%RTuuid} of the medium '%s' does not match UUID {%RTuuid} of its parent medium stored in the media registry ('%s')"),
|
---|
5691 | &parentId, location.c_str(),
|
---|
5692 | m->pParent->getId().raw(),
|
---|
5693 | m->pVirtualBox->settingsFilePath().c_str());
|
---|
5694 | parentLock.release();
|
---|
5695 | treeLock.release();
|
---|
5696 | throw S_OK;
|
---|
5697 | }
|
---|
5698 | }
|
---|
5699 |
|
---|
5700 | /// @todo NEWMEDIA what to do if the parent is not
|
---|
5701 | /// accessible while the diff is? Probably nothing. The
|
---|
5702 | /// real code will detect the mismatch anyway.
|
---|
5703 |
|
---|
5704 | treeLock.release();
|
---|
5705 | }
|
---|
5706 | }
|
---|
5707 |
|
---|
5708 | mediumSize = VDGetFileSize(hdd, 0);
|
---|
5709 | mediumLogicalSize = VDGetSize(hdd, 0);
|
---|
5710 |
|
---|
5711 | success = true;
|
---|
5712 | }
|
---|
5713 | catch (HRESULT aRC)
|
---|
5714 | {
|
---|
5715 | rc = aRC;
|
---|
5716 | }
|
---|
5717 |
|
---|
5718 | vrc = VDDestroy(hdd);
|
---|
5719 | if (RT_FAILURE(vrc))
|
---|
5720 | {
|
---|
5721 | lastAccessError = Utf8StrFmt(tr("Could not update and close the medium '%s'%s"),
|
---|
5722 | location.c_str(), vdError(vrc).c_str());
|
---|
5723 | success = false;
|
---|
5724 | throw S_OK;
|
---|
5725 | }
|
---|
5726 | }
|
---|
5727 | catch (HRESULT aRC)
|
---|
5728 | {
|
---|
5729 | rc = aRC;
|
---|
5730 | }
|
---|
5731 |
|
---|
5732 | treeLock.acquire();
|
---|
5733 | alock.acquire();
|
---|
5734 |
|
---|
5735 | if (success)
|
---|
5736 | {
|
---|
5737 | m->size = mediumSize;
|
---|
5738 | m->logicalSize = mediumLogicalSize;
|
---|
5739 | m->strLastAccessError.setNull();
|
---|
5740 | }
|
---|
5741 | else
|
---|
5742 | {
|
---|
5743 | m->strLastAccessError = lastAccessError;
|
---|
5744 | LogWarningFunc(("'%s' is not accessible (error='%s', rc=%Rhrc, vrc=%Rrc)\n",
|
---|
5745 | location.c_str(), m->strLastAccessError.c_str(),
|
---|
5746 | rc, vrc));
|
---|
5747 | }
|
---|
5748 |
|
---|
5749 | /* unblock anyone waiting for the queryInfo results */
|
---|
5750 | qlock.release();
|
---|
5751 | m->queryInfoRunning = false;
|
---|
5752 |
|
---|
5753 | /* Set the proper state according to the result of the check */
|
---|
5754 | if (success)
|
---|
5755 | m->preLockState = MediumState_Created;
|
---|
5756 | else
|
---|
5757 | m->preLockState = MediumState_Inaccessible;
|
---|
5758 |
|
---|
5759 | HRESULT rc2;
|
---|
5760 | if (uOpenFlags & (VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SHAREABLE))
|
---|
5761 | rc2 = UnlockRead(NULL);
|
---|
5762 | else
|
---|
5763 | rc2 = UnlockWrite(NULL);
|
---|
5764 | if (SUCCEEDED(rc) && FAILED(rc2))
|
---|
5765 | rc = rc2;
|
---|
5766 | if (FAILED(rc)) return rc;
|
---|
5767 |
|
---|
5768 | /* If this is a base image which incorrectly has a parent UUID set,
|
---|
5769 | * repair the image now by zeroing the parent UUID. This is only done
|
---|
5770 | * when we have structural information from a config file, on import
|
---|
5771 | * this is not possible. If someone would accidentally call openMedium
|
---|
5772 | * with a diff image before the base is registered this would destroy
|
---|
5773 | * the diff. Not acceptable. */
|
---|
5774 | if (fRepairImageZeroParentUuid)
|
---|
5775 | {
|
---|
5776 | rc = LockWrite(NULL);
|
---|
5777 | if (FAILED(rc)) return rc;
|
---|
5778 |
|
---|
5779 | alock.release();
|
---|
5780 |
|
---|
5781 | try
|
---|
5782 | {
|
---|
5783 | PVBOXHDD hdd;
|
---|
5784 | vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &hdd);
|
---|
5785 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
5786 |
|
---|
5787 | try
|
---|
5788 | {
|
---|
5789 | vrc = VDOpen(hdd,
|
---|
5790 | format.c_str(),
|
---|
5791 | location.c_str(),
|
---|
5792 | (uOpenFlags & ~VD_OPEN_FLAGS_READONLY) | m->uOpenFlagsDef,
|
---|
5793 | m->vdImageIfaces);
|
---|
5794 | if (RT_FAILURE(vrc))
|
---|
5795 | throw S_OK;
|
---|
5796 |
|
---|
5797 | RTUUID zeroParentUuid;
|
---|
5798 | RTUuidClear(&zeroParentUuid);
|
---|
5799 | vrc = VDSetParentUuid(hdd, 0, &zeroParentUuid);
|
---|
5800 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
5801 | }
|
---|
5802 | catch (HRESULT aRC)
|
---|
5803 | {
|
---|
5804 | rc = aRC;
|
---|
5805 | }
|
---|
5806 |
|
---|
5807 | VDDestroy(hdd);
|
---|
5808 | }
|
---|
5809 | catch (HRESULT aRC)
|
---|
5810 | {
|
---|
5811 | rc = aRC;
|
---|
5812 | }
|
---|
5813 |
|
---|
5814 | rc = UnlockWrite(NULL);
|
---|
5815 | if (SUCCEEDED(rc) && FAILED(rc2))
|
---|
5816 | rc = rc2;
|
---|
5817 | if (FAILED(rc)) return rc;
|
---|
5818 | }
|
---|
5819 |
|
---|
5820 | return rc;
|
---|
5821 | }
|
---|
5822 |
|
---|
5823 | /**
|
---|
5824 | * Performs extra checks if the medium can be closed and returns S_OK in
|
---|
5825 | * this case. Otherwise, returns a respective error message. Called by
|
---|
5826 | * Close() under the medium tree lock and the medium lock.
|
---|
5827 | *
|
---|
5828 | * @note Also reused by Medium::Reset().
|
---|
5829 | *
|
---|
5830 | * @note Caller must hold the media tree write lock!
|
---|
5831 | */
|
---|
5832 | HRESULT Medium::canClose()
|
---|
5833 | {
|
---|
5834 | Assert(m->pVirtualBox->getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
5835 |
|
---|
5836 | if (getChildren().size() != 0)
|
---|
5837 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
5838 | tr("Cannot close medium '%s' because it has %d child media"),
|
---|
5839 | m->strLocationFull.c_str(), getChildren().size());
|
---|
5840 |
|
---|
5841 | return S_OK;
|
---|
5842 | }
|
---|
5843 |
|
---|
5844 | /**
|
---|
5845 | * Unregisters this medium with mVirtualBox. Called by close() under the medium tree lock.
|
---|
5846 | *
|
---|
5847 | * @note Caller must have locked the media tree lock for writing!
|
---|
5848 | */
|
---|
5849 | HRESULT Medium::unregisterWithVirtualBox()
|
---|
5850 | {
|
---|
5851 | /* Note that we need to de-associate ourselves from the parent to let
|
---|
5852 | * unregisterMedium() properly save the registry */
|
---|
5853 |
|
---|
5854 | /* we modify mParent and access children */
|
---|
5855 | Assert(m->pVirtualBox->getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
5856 |
|
---|
5857 | Medium *pParentBackup = m->pParent;
|
---|
5858 | AssertReturn(getChildren().size() == 0, E_FAIL);
|
---|
5859 | if (m->pParent)
|
---|
5860 | deparent();
|
---|
5861 |
|
---|
5862 | HRESULT rc = m->pVirtualBox->unregisterMedium(this);
|
---|
5863 | if (FAILED(rc))
|
---|
5864 | {
|
---|
5865 | if (pParentBackup)
|
---|
5866 | {
|
---|
5867 | // re-associate with the parent as we are still relatives in the registry
|
---|
5868 | m->pParent = pParentBackup;
|
---|
5869 | m->pParent->m->llChildren.push_back(this);
|
---|
5870 | }
|
---|
5871 | }
|
---|
5872 |
|
---|
5873 | return rc;
|
---|
5874 | }
|
---|
5875 |
|
---|
5876 | /**
|
---|
5877 | * Like SetProperty but do not trigger a settings store. Only for internal use!
|
---|
5878 | */
|
---|
5879 | HRESULT Medium::setPropertyDirect(const Utf8Str &aName, const Utf8Str &aValue)
|
---|
5880 | {
|
---|
5881 | AutoCaller autoCaller(this);
|
---|
5882 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
5883 |
|
---|
5884 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5885 |
|
---|
5886 | switch (m->state)
|
---|
5887 | {
|
---|
5888 | case MediumState_Created:
|
---|
5889 | case MediumState_Inaccessible:
|
---|
5890 | break;
|
---|
5891 | default:
|
---|
5892 | return setStateError();
|
---|
5893 | }
|
---|
5894 |
|
---|
5895 | m->mapProperties[aName] = aValue;
|
---|
5896 |
|
---|
5897 | return S_OK;
|
---|
5898 | }
|
---|
5899 |
|
---|
5900 | /**
|
---|
5901 | * Sets the extended error info according to the current media state.
|
---|
5902 | *
|
---|
5903 | * @note Must be called from under this object's write or read lock.
|
---|
5904 | */
|
---|
5905 | HRESULT Medium::setStateError()
|
---|
5906 | {
|
---|
5907 | HRESULT rc = E_FAIL;
|
---|
5908 |
|
---|
5909 | switch (m->state)
|
---|
5910 | {
|
---|
5911 | case MediumState_NotCreated:
|
---|
5912 | {
|
---|
5913 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5914 | tr("Storage for the medium '%s' is not created"),
|
---|
5915 | m->strLocationFull.c_str());
|
---|
5916 | break;
|
---|
5917 | }
|
---|
5918 | case MediumState_Created:
|
---|
5919 | {
|
---|
5920 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5921 | tr("Storage for the medium '%s' is already created"),
|
---|
5922 | m->strLocationFull.c_str());
|
---|
5923 | break;
|
---|
5924 | }
|
---|
5925 | case MediumState_LockedRead:
|
---|
5926 | {
|
---|
5927 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5928 | tr("Medium '%s' is locked for reading by another task"),
|
---|
5929 | m->strLocationFull.c_str());
|
---|
5930 | break;
|
---|
5931 | }
|
---|
5932 | case MediumState_LockedWrite:
|
---|
5933 | {
|
---|
5934 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5935 | tr("Medium '%s' is locked for writing by another task"),
|
---|
5936 | m->strLocationFull.c_str());
|
---|
5937 | break;
|
---|
5938 | }
|
---|
5939 | case MediumState_Inaccessible:
|
---|
5940 | {
|
---|
5941 | /* be in sync with Console::powerUpThread() */
|
---|
5942 | if (!m->strLastAccessError.isEmpty())
|
---|
5943 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5944 | tr("Medium '%s' is not accessible. %s"),
|
---|
5945 | m->strLocationFull.c_str(), m->strLastAccessError.c_str());
|
---|
5946 | else
|
---|
5947 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5948 | tr("Medium '%s' is not accessible"),
|
---|
5949 | m->strLocationFull.c_str());
|
---|
5950 | break;
|
---|
5951 | }
|
---|
5952 | case MediumState_Creating:
|
---|
5953 | {
|
---|
5954 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5955 | tr("Storage for the medium '%s' is being created"),
|
---|
5956 | m->strLocationFull.c_str());
|
---|
5957 | break;
|
---|
5958 | }
|
---|
5959 | case MediumState_Deleting:
|
---|
5960 | {
|
---|
5961 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5962 | tr("Storage for the medium '%s' is being deleted"),
|
---|
5963 | m->strLocationFull.c_str());
|
---|
5964 | break;
|
---|
5965 | }
|
---|
5966 | default:
|
---|
5967 | {
|
---|
5968 | AssertFailed();
|
---|
5969 | break;
|
---|
5970 | }
|
---|
5971 | }
|
---|
5972 |
|
---|
5973 | return rc;
|
---|
5974 | }
|
---|
5975 |
|
---|
5976 | /**
|
---|
5977 | * Sets the value of m->strLocationFull. The given location must be a fully
|
---|
5978 | * qualified path; relative paths are not supported here.
|
---|
5979 | *
|
---|
5980 | * As a special exception, if the specified location is a file path that ends with '/'
|
---|
5981 | * then the file name part will be generated by this method automatically in the format
|
---|
5982 | * '{<uuid>}.<ext>' where <uuid> is a fresh UUID that this method will generate
|
---|
5983 | * and assign to this medium, and <ext> is the default extension for this
|
---|
5984 | * medium's storage format. Note that this procedure requires the media state to
|
---|
5985 | * be NotCreated and will return a failure otherwise.
|
---|
5986 | *
|
---|
5987 | * @param aLocation Location of the storage unit. If the location is a FS-path,
|
---|
5988 | * then it can be relative to the VirtualBox home directory.
|
---|
5989 | * @param aFormat Optional fallback format if it is an import and the format
|
---|
5990 | * cannot be determined.
|
---|
5991 | *
|
---|
5992 | * @note Must be called from under this object's write lock.
|
---|
5993 | */
|
---|
5994 | HRESULT Medium::setLocation(const Utf8Str &aLocation,
|
---|
5995 | const Utf8Str &aFormat /* = Utf8Str::Empty */)
|
---|
5996 | {
|
---|
5997 | AssertReturn(!aLocation.isEmpty(), E_FAIL);
|
---|
5998 |
|
---|
5999 | AutoCaller autoCaller(this);
|
---|
6000 | AssertComRCReturnRC(autoCaller.rc());
|
---|
6001 |
|
---|
6002 | /* formatObj may be null only when initializing from an existing path and
|
---|
6003 | * no format is known yet */
|
---|
6004 | AssertReturn( (!m->strFormat.isEmpty() && !m->formatObj.isNull())
|
---|
6005 | || ( autoCaller.state() == InInit
|
---|
6006 | && m->state != MediumState_NotCreated
|
---|
6007 | && m->id.isEmpty()
|
---|
6008 | && m->strFormat.isEmpty()
|
---|
6009 | && m->formatObj.isNull()),
|
---|
6010 | E_FAIL);
|
---|
6011 |
|
---|
6012 | /* are we dealing with a new medium constructed using the existing
|
---|
6013 | * location? */
|
---|
6014 | bool isImport = m->strFormat.isEmpty();
|
---|
6015 |
|
---|
6016 | if ( isImport
|
---|
6017 | || ( (m->formatObj->getCapabilities() & MediumFormatCapabilities_File)
|
---|
6018 | && !m->hostDrive))
|
---|
6019 | {
|
---|
6020 | Guid id;
|
---|
6021 |
|
---|
6022 | Utf8Str locationFull(aLocation);
|
---|
6023 |
|
---|
6024 | if (m->state == MediumState_NotCreated)
|
---|
6025 | {
|
---|
6026 | /* must be a file (formatObj must be already known) */
|
---|
6027 | Assert(m->formatObj->getCapabilities() & MediumFormatCapabilities_File);
|
---|
6028 |
|
---|
6029 | if (RTPathFilename(aLocation.c_str()) == NULL)
|
---|
6030 | {
|
---|
6031 | /* no file name is given (either an empty string or ends with a
|
---|
6032 | * slash), generate a new UUID + file name if the state allows
|
---|
6033 | * this */
|
---|
6034 |
|
---|
6035 | ComAssertMsgRet(!m->formatObj->getFileExtensions().empty(),
|
---|
6036 | ("Must be at least one extension if it is MediumFormatCapabilities_File\n"),
|
---|
6037 | E_FAIL);
|
---|
6038 |
|
---|
6039 | Utf8Str strExt = m->formatObj->getFileExtensions().front();
|
---|
6040 | ComAssertMsgRet(!strExt.isEmpty(),
|
---|
6041 | ("Default extension must not be empty\n"),
|
---|
6042 | E_FAIL);
|
---|
6043 |
|
---|
6044 | id.create();
|
---|
6045 |
|
---|
6046 | locationFull = Utf8StrFmt("%s{%RTuuid}.%s",
|
---|
6047 | aLocation.c_str(), id.raw(), strExt.c_str());
|
---|
6048 | }
|
---|
6049 | }
|
---|
6050 |
|
---|
6051 | // we must always have full paths now (if it refers to a file)
|
---|
6052 | if ( ( m->formatObj.isNull()
|
---|
6053 | || m->formatObj->getCapabilities() & MediumFormatCapabilities_File)
|
---|
6054 | && !RTPathStartsWithRoot(locationFull.c_str()))
|
---|
6055 | return setError(VBOX_E_FILE_ERROR,
|
---|
6056 | tr("The given path '%s' is not fully qualified"),
|
---|
6057 | locationFull.c_str());
|
---|
6058 |
|
---|
6059 | /* detect the backend from the storage unit if importing */
|
---|
6060 | if (isImport)
|
---|
6061 | {
|
---|
6062 | VDTYPE enmType = VDTYPE_INVALID;
|
---|
6063 | char *backendName = NULL;
|
---|
6064 |
|
---|
6065 | int vrc = VINF_SUCCESS;
|
---|
6066 |
|
---|
6067 | /* is it a file? */
|
---|
6068 | {
|
---|
6069 | RTFILE file;
|
---|
6070 | vrc = RTFileOpen(&file, locationFull.c_str(), RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
6071 | if (RT_SUCCESS(vrc))
|
---|
6072 | RTFileClose(file);
|
---|
6073 | }
|
---|
6074 | if (RT_SUCCESS(vrc))
|
---|
6075 | {
|
---|
6076 | vrc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
|
---|
6077 | locationFull.c_str(), &backendName, &enmType);
|
---|
6078 | }
|
---|
6079 | else if ( vrc != VERR_FILE_NOT_FOUND
|
---|
6080 | && vrc != VERR_PATH_NOT_FOUND
|
---|
6081 | && vrc != VERR_ACCESS_DENIED
|
---|
6082 | && locationFull != aLocation)
|
---|
6083 | {
|
---|
6084 | /* assume it's not a file, restore the original location */
|
---|
6085 | locationFull = aLocation;
|
---|
6086 | vrc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
|
---|
6087 | locationFull.c_str(), &backendName, &enmType);
|
---|
6088 | }
|
---|
6089 |
|
---|
6090 | if (RT_FAILURE(vrc))
|
---|
6091 | {
|
---|
6092 | if (vrc == VERR_ACCESS_DENIED)
|
---|
6093 | return setError(VBOX_E_FILE_ERROR,
|
---|
6094 | tr("Permission problem accessing the file for the medium '%s' (%Rrc)"),
|
---|
6095 | locationFull.c_str(), vrc);
|
---|
6096 | else if (vrc == VERR_FILE_NOT_FOUND || vrc == VERR_PATH_NOT_FOUND)
|
---|
6097 | return setError(VBOX_E_FILE_ERROR,
|
---|
6098 | tr("Could not find file for the medium '%s' (%Rrc)"),
|
---|
6099 | locationFull.c_str(), vrc);
|
---|
6100 | else if (aFormat.isEmpty())
|
---|
6101 | return setError(VBOX_E_IPRT_ERROR,
|
---|
6102 | tr("Could not get the storage format of the medium '%s' (%Rrc)"),
|
---|
6103 | locationFull.c_str(), vrc);
|
---|
6104 | else
|
---|
6105 | {
|
---|
6106 | HRESULT rc = setFormat(aFormat);
|
---|
6107 | /* setFormat() must not fail since we've just used the backend so
|
---|
6108 | * the format object must be there */
|
---|
6109 | AssertComRCReturnRC(rc);
|
---|
6110 | }
|
---|
6111 | }
|
---|
6112 | else if ( enmType == VDTYPE_INVALID
|
---|
6113 | || m->devType != convertToDeviceType(enmType))
|
---|
6114 | {
|
---|
6115 | /*
|
---|
6116 | * The user tried to use a image as a device which is not supported
|
---|
6117 | * by the backend.
|
---|
6118 | */
|
---|
6119 | return setError(E_FAIL,
|
---|
6120 | tr("The medium '%s' can't be used as the requested device type"),
|
---|
6121 | locationFull.c_str());
|
---|
6122 | }
|
---|
6123 | else
|
---|
6124 | {
|
---|
6125 | ComAssertRet(backendName != NULL && *backendName != '\0', E_FAIL);
|
---|
6126 |
|
---|
6127 | HRESULT rc = setFormat(backendName);
|
---|
6128 | RTStrFree(backendName);
|
---|
6129 |
|
---|
6130 | /* setFormat() must not fail since we've just used the backend so
|
---|
6131 | * the format object must be there */
|
---|
6132 | AssertComRCReturnRC(rc);
|
---|
6133 | }
|
---|
6134 | }
|
---|
6135 |
|
---|
6136 | m->strLocationFull = locationFull;
|
---|
6137 |
|
---|
6138 | /* is it still a file? */
|
---|
6139 | if ( (m->formatObj->getCapabilities() & MediumFormatCapabilities_File)
|
---|
6140 | && (m->state == MediumState_NotCreated)
|
---|
6141 | )
|
---|
6142 | /* assign a new UUID (this UUID will be used when calling
|
---|
6143 | * VDCreateBase/VDCreateDiff as a wanted UUID). Note that we
|
---|
6144 | * also do that if we didn't generate it to make sure it is
|
---|
6145 | * either generated by us or reset to null */
|
---|
6146 | unconst(m->id) = id;
|
---|
6147 | }
|
---|
6148 | else
|
---|
6149 | m->strLocationFull = aLocation;
|
---|
6150 |
|
---|
6151 | return S_OK;
|
---|
6152 | }
|
---|
6153 |
|
---|
6154 | /**
|
---|
6155 | * Checks that the format ID is valid and sets it on success.
|
---|
6156 | *
|
---|
6157 | * Note that this method will caller-reference the format object on success!
|
---|
6158 | * This reference must be released somewhere to let the MediumFormat object be
|
---|
6159 | * uninitialized.
|
---|
6160 | *
|
---|
6161 | * @note Must be called from under this object's write lock.
|
---|
6162 | */
|
---|
6163 | HRESULT Medium::setFormat(const Utf8Str &aFormat)
|
---|
6164 | {
|
---|
6165 | /* get the format object first */
|
---|
6166 | {
|
---|
6167 | SystemProperties *pSysProps = m->pVirtualBox->getSystemProperties();
|
---|
6168 | AutoReadLock propsLock(pSysProps COMMA_LOCKVAL_SRC_POS);
|
---|
6169 |
|
---|
6170 | unconst(m->formatObj) = pSysProps->mediumFormat(aFormat);
|
---|
6171 | if (m->formatObj.isNull())
|
---|
6172 | return setError(E_INVALIDARG,
|
---|
6173 | tr("Invalid medium storage format '%s'"),
|
---|
6174 | aFormat.c_str());
|
---|
6175 |
|
---|
6176 | /* reference the format permanently to prevent its unexpected
|
---|
6177 | * uninitialization */
|
---|
6178 | HRESULT rc = m->formatObj->addCaller();
|
---|
6179 | AssertComRCReturnRC(rc);
|
---|
6180 |
|
---|
6181 | /* get properties (preinsert them as keys in the map). Note that the
|
---|
6182 | * map doesn't grow over the object life time since the set of
|
---|
6183 | * properties is meant to be constant. */
|
---|
6184 |
|
---|
6185 | Assert(m->mapProperties.empty());
|
---|
6186 |
|
---|
6187 | for (MediumFormat::PropertyList::const_iterator it = m->formatObj->getProperties().begin();
|
---|
6188 | it != m->formatObj->getProperties().end();
|
---|
6189 | ++it)
|
---|
6190 | {
|
---|
6191 | m->mapProperties.insert(std::make_pair(it->strName, Utf8Str::Empty));
|
---|
6192 | }
|
---|
6193 | }
|
---|
6194 |
|
---|
6195 | unconst(m->strFormat) = aFormat;
|
---|
6196 |
|
---|
6197 | return S_OK;
|
---|
6198 | }
|
---|
6199 |
|
---|
6200 | /**
|
---|
6201 | * Converts the Medium device type to the VD type.
|
---|
6202 | */
|
---|
6203 | VDTYPE Medium::convertDeviceType()
|
---|
6204 | {
|
---|
6205 | VDTYPE enmType;
|
---|
6206 |
|
---|
6207 | switch (m->devType)
|
---|
6208 | {
|
---|
6209 | case DeviceType_HardDisk:
|
---|
6210 | enmType = VDTYPE_HDD;
|
---|
6211 | break;
|
---|
6212 | case DeviceType_DVD:
|
---|
6213 | enmType = VDTYPE_DVD;
|
---|
6214 | break;
|
---|
6215 | case DeviceType_Floppy:
|
---|
6216 | enmType = VDTYPE_FLOPPY;
|
---|
6217 | break;
|
---|
6218 | default:
|
---|
6219 | ComAssertFailedRet(VDTYPE_INVALID);
|
---|
6220 | }
|
---|
6221 |
|
---|
6222 | return enmType;
|
---|
6223 | }
|
---|
6224 |
|
---|
6225 | /**
|
---|
6226 | * Converts from the VD type to the medium type.
|
---|
6227 | */
|
---|
6228 | DeviceType_T Medium::convertToDeviceType(VDTYPE enmType)
|
---|
6229 | {
|
---|
6230 | DeviceType_T devType;
|
---|
6231 |
|
---|
6232 | switch (enmType)
|
---|
6233 | {
|
---|
6234 | case VDTYPE_HDD:
|
---|
6235 | devType = DeviceType_HardDisk;
|
---|
6236 | break;
|
---|
6237 | case VDTYPE_DVD:
|
---|
6238 | devType = DeviceType_DVD;
|
---|
6239 | break;
|
---|
6240 | case VDTYPE_FLOPPY:
|
---|
6241 | devType = DeviceType_Floppy;
|
---|
6242 | break;
|
---|
6243 | default:
|
---|
6244 | ComAssertFailedRet(DeviceType_Null);
|
---|
6245 | }
|
---|
6246 |
|
---|
6247 | return devType;
|
---|
6248 | }
|
---|
6249 |
|
---|
6250 | /**
|
---|
6251 | * Returns the last error message collected by the vdErrorCall callback and
|
---|
6252 | * resets it.
|
---|
6253 | *
|
---|
6254 | * The error message is returned prepended with a dot and a space, like this:
|
---|
6255 | * <code>
|
---|
6256 | * ". <error_text> (%Rrc)"
|
---|
6257 | * </code>
|
---|
6258 | * to make it easily appendable to a more general error message. The @c %Rrc
|
---|
6259 | * format string is given @a aVRC as an argument.
|
---|
6260 | *
|
---|
6261 | * If there is no last error message collected by vdErrorCall or if it is a
|
---|
6262 | * null or empty string, then this function returns the following text:
|
---|
6263 | * <code>
|
---|
6264 | * " (%Rrc)"
|
---|
6265 | * </code>
|
---|
6266 | *
|
---|
6267 | * @note Doesn't do any object locking; it is assumed that the caller makes sure
|
---|
6268 | * the callback isn't called by more than one thread at a time.
|
---|
6269 | *
|
---|
6270 | * @param aVRC VBox error code to use when no error message is provided.
|
---|
6271 | */
|
---|
6272 | Utf8Str Medium::vdError(int aVRC)
|
---|
6273 | {
|
---|
6274 | Utf8Str error;
|
---|
6275 |
|
---|
6276 | if (m->vdError.isEmpty())
|
---|
6277 | error = Utf8StrFmt(" (%Rrc)", aVRC);
|
---|
6278 | else
|
---|
6279 | error = Utf8StrFmt(".\n%s", m->vdError.c_str());
|
---|
6280 |
|
---|
6281 | m->vdError.setNull();
|
---|
6282 |
|
---|
6283 | return error;
|
---|
6284 | }
|
---|
6285 |
|
---|
6286 | /**
|
---|
6287 | * Error message callback.
|
---|
6288 | *
|
---|
6289 | * Puts the reported error message to the m->vdError field.
|
---|
6290 | *
|
---|
6291 | * @note Doesn't do any object locking; it is assumed that the caller makes sure
|
---|
6292 | * the callback isn't called by more than one thread at a time.
|
---|
6293 | *
|
---|
6294 | * @param pvUser The opaque data passed on container creation.
|
---|
6295 | * @param rc The VBox error code.
|
---|
6296 | * @param RT_SRC_POS_DECL Use RT_SRC_POS.
|
---|
6297 | * @param pszFormat Error message format string.
|
---|
6298 | * @param va Error message arguments.
|
---|
6299 | */
|
---|
6300 | /*static*/
|
---|
6301 | DECLCALLBACK(void) Medium::vdErrorCall(void *pvUser, int rc, RT_SRC_POS_DECL,
|
---|
6302 | const char *pszFormat, va_list va)
|
---|
6303 | {
|
---|
6304 | NOREF(pszFile); NOREF(iLine); NOREF(pszFunction); /* RT_SRC_POS_DECL */
|
---|
6305 |
|
---|
6306 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
6307 | AssertReturnVoid(that != NULL);
|
---|
6308 |
|
---|
6309 | if (that->m->vdError.isEmpty())
|
---|
6310 | that->m->vdError =
|
---|
6311 | Utf8StrFmt("%s (%Rrc)", Utf8Str(pszFormat, va).c_str(), rc);
|
---|
6312 | else
|
---|
6313 | that->m->vdError =
|
---|
6314 | Utf8StrFmt("%s.\n%s (%Rrc)", that->m->vdError.c_str(),
|
---|
6315 | Utf8Str(pszFormat, va).c_str(), rc);
|
---|
6316 | }
|
---|
6317 |
|
---|
6318 | /* static */
|
---|
6319 | DECLCALLBACK(bool) Medium::vdConfigAreKeysValid(void *pvUser,
|
---|
6320 | const char * /* pszzValid */)
|
---|
6321 | {
|
---|
6322 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
6323 | AssertReturn(that != NULL, false);
|
---|
6324 |
|
---|
6325 | /* we always return true since the only keys we have are those found in
|
---|
6326 | * VDBACKENDINFO */
|
---|
6327 | return true;
|
---|
6328 | }
|
---|
6329 |
|
---|
6330 | /* static */
|
---|
6331 | DECLCALLBACK(int) Medium::vdConfigQuerySize(void *pvUser,
|
---|
6332 | const char *pszName,
|
---|
6333 | size_t *pcbValue)
|
---|
6334 | {
|
---|
6335 | AssertReturn(VALID_PTR(pcbValue), VERR_INVALID_POINTER);
|
---|
6336 |
|
---|
6337 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
6338 | AssertReturn(that != NULL, VERR_GENERAL_FAILURE);
|
---|
6339 |
|
---|
6340 | settings::StringsMap::const_iterator it = that->m->mapProperties.find(Utf8Str(pszName));
|
---|
6341 | if (it == that->m->mapProperties.end())
|
---|
6342 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
6343 |
|
---|
6344 | /* we interpret null values as "no value" in Medium */
|
---|
6345 | if (it->second.isEmpty())
|
---|
6346 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
6347 |
|
---|
6348 | *pcbValue = it->second.length() + 1 /* include terminator */;
|
---|
6349 |
|
---|
6350 | return VINF_SUCCESS;
|
---|
6351 | }
|
---|
6352 |
|
---|
6353 | /* static */
|
---|
6354 | DECLCALLBACK(int) Medium::vdConfigQuery(void *pvUser,
|
---|
6355 | const char *pszName,
|
---|
6356 | char *pszValue,
|
---|
6357 | size_t cchValue)
|
---|
6358 | {
|
---|
6359 | AssertReturn(VALID_PTR(pszValue), VERR_INVALID_POINTER);
|
---|
6360 |
|
---|
6361 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
6362 | AssertReturn(that != NULL, VERR_GENERAL_FAILURE);
|
---|
6363 |
|
---|
6364 | settings::StringsMap::const_iterator it = that->m->mapProperties.find(Utf8Str(pszName));
|
---|
6365 | if (it == that->m->mapProperties.end())
|
---|
6366 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
6367 |
|
---|
6368 | /* we interpret null values as "no value" in Medium */
|
---|
6369 | if (it->second.isEmpty())
|
---|
6370 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
6371 |
|
---|
6372 | const Utf8Str &value = it->second;
|
---|
6373 | if (value.length() >= cchValue)
|
---|
6374 | return VERR_CFGM_NOT_ENOUGH_SPACE;
|
---|
6375 |
|
---|
6376 | memcpy(pszValue, value.c_str(), value.length() + 1);
|
---|
6377 |
|
---|
6378 | return VINF_SUCCESS;
|
---|
6379 | }
|
---|
6380 |
|
---|
6381 | DECLCALLBACK(int) Medium::vdTcpSocketCreate(uint32_t fFlags, PVDSOCKET pSock)
|
---|
6382 | {
|
---|
6383 | PVDSOCKETINT pSocketInt = NULL;
|
---|
6384 |
|
---|
6385 | if ((fFlags & VD_INTERFACETCPNET_CONNECT_EXTENDED_SELECT) != 0)
|
---|
6386 | return VERR_NOT_SUPPORTED;
|
---|
6387 |
|
---|
6388 | pSocketInt = (PVDSOCKETINT)RTMemAllocZ(sizeof(VDSOCKETINT));
|
---|
6389 | if (!pSocketInt)
|
---|
6390 | return VERR_NO_MEMORY;
|
---|
6391 |
|
---|
6392 | pSocketInt->hSocket = NIL_RTSOCKET;
|
---|
6393 | *pSock = pSocketInt;
|
---|
6394 | return VINF_SUCCESS;
|
---|
6395 | }
|
---|
6396 |
|
---|
6397 | DECLCALLBACK(int) Medium::vdTcpSocketDestroy(VDSOCKET Sock)
|
---|
6398 | {
|
---|
6399 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
6400 |
|
---|
6401 | if (pSocketInt->hSocket != NIL_RTSOCKET)
|
---|
6402 | RTTcpClientCloseEx(pSocketInt->hSocket, false /*fGracefulShutdown*/);
|
---|
6403 |
|
---|
6404 | RTMemFree(pSocketInt);
|
---|
6405 |
|
---|
6406 | return VINF_SUCCESS;
|
---|
6407 | }
|
---|
6408 |
|
---|
6409 | DECLCALLBACK(int) Medium::vdTcpClientConnect(VDSOCKET Sock, const char *pszAddress, uint32_t uPort)
|
---|
6410 | {
|
---|
6411 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
6412 |
|
---|
6413 | return RTTcpClientConnect(pszAddress, uPort, &pSocketInt->hSocket);
|
---|
6414 | }
|
---|
6415 |
|
---|
6416 | DECLCALLBACK(int) Medium::vdTcpClientClose(VDSOCKET Sock)
|
---|
6417 | {
|
---|
6418 | int rc = VINF_SUCCESS;
|
---|
6419 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
6420 |
|
---|
6421 | rc = RTTcpClientCloseEx(pSocketInt->hSocket, false /*fGracefulShutdown*/);
|
---|
6422 | pSocketInt->hSocket = NIL_RTSOCKET;
|
---|
6423 | return rc;
|
---|
6424 | }
|
---|
6425 |
|
---|
6426 | DECLCALLBACK(bool) Medium::vdTcpIsClientConnected(VDSOCKET Sock)
|
---|
6427 | {
|
---|
6428 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
6429 | return pSocketInt->hSocket != NIL_RTSOCKET;
|
---|
6430 | }
|
---|
6431 |
|
---|
6432 | DECLCALLBACK(int) Medium::vdTcpSelectOne(VDSOCKET Sock, RTMSINTERVAL cMillies)
|
---|
6433 | {
|
---|
6434 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
6435 | return RTTcpSelectOne(pSocketInt->hSocket, cMillies);
|
---|
6436 | }
|
---|
6437 |
|
---|
6438 | DECLCALLBACK(int) Medium::vdTcpRead(VDSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead)
|
---|
6439 | {
|
---|
6440 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
6441 | return RTTcpRead(pSocketInt->hSocket, pvBuffer, cbBuffer, pcbRead);
|
---|
6442 | }
|
---|
6443 |
|
---|
6444 | DECLCALLBACK(int) Medium::vdTcpWrite(VDSOCKET Sock, const void *pvBuffer, size_t cbBuffer)
|
---|
6445 | {
|
---|
6446 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
6447 | return RTTcpWrite(pSocketInt->hSocket, pvBuffer, cbBuffer);
|
---|
6448 | }
|
---|
6449 |
|
---|
6450 | DECLCALLBACK(int) Medium::vdTcpSgWrite(VDSOCKET Sock, PCRTSGBUF pSgBuf)
|
---|
6451 | {
|
---|
6452 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
6453 | return RTTcpSgWrite(pSocketInt->hSocket, pSgBuf);
|
---|
6454 | }
|
---|
6455 |
|
---|
6456 | DECLCALLBACK(int) Medium::vdTcpFlush(VDSOCKET Sock)
|
---|
6457 | {
|
---|
6458 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
6459 | return RTTcpFlush(pSocketInt->hSocket);
|
---|
6460 | }
|
---|
6461 |
|
---|
6462 | DECLCALLBACK(int) Medium::vdTcpSetSendCoalescing(VDSOCKET Sock, bool fEnable)
|
---|
6463 | {
|
---|
6464 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
6465 | return RTTcpSetSendCoalescing(pSocketInt->hSocket, fEnable);
|
---|
6466 | }
|
---|
6467 |
|
---|
6468 | DECLCALLBACK(int) Medium::vdTcpGetLocalAddress(VDSOCKET Sock, PRTNETADDR pAddr)
|
---|
6469 | {
|
---|
6470 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
6471 | return RTTcpGetLocalAddress(pSocketInt->hSocket, pAddr);
|
---|
6472 | }
|
---|
6473 |
|
---|
6474 | DECLCALLBACK(int) Medium::vdTcpGetPeerAddress(VDSOCKET Sock, PRTNETADDR pAddr)
|
---|
6475 | {
|
---|
6476 | PVDSOCKETINT pSocketInt = (PVDSOCKETINT)Sock;
|
---|
6477 | return RTTcpGetPeerAddress(pSocketInt->hSocket, pAddr);
|
---|
6478 | }
|
---|
6479 |
|
---|
6480 | /**
|
---|
6481 | * Starts a new thread driven by the appropriate Medium::Task::handler() method.
|
---|
6482 | *
|
---|
6483 | * @note When the task is executed by this method, IProgress::notifyComplete()
|
---|
6484 | * is automatically called for the progress object associated with this
|
---|
6485 | * task when the task is finished to signal the operation completion for
|
---|
6486 | * other threads asynchronously waiting for it.
|
---|
6487 | */
|
---|
6488 | HRESULT Medium::startThread(Medium::Task *pTask)
|
---|
6489 | {
|
---|
6490 | #ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
|
---|
6491 | /* Extreme paranoia: The calling thread should not hold the medium
|
---|
6492 | * tree lock or any medium lock. Since there is no separate lock class
|
---|
6493 | * for medium objects be even more strict: no other object locks. */
|
---|
6494 | Assert(!AutoLockHoldsLocksInClass(LOCKCLASS_LISTOFMEDIA));
|
---|
6495 | Assert(!AutoLockHoldsLocksInClass(getLockingClass()));
|
---|
6496 | #endif
|
---|
6497 |
|
---|
6498 | /// @todo use a more descriptive task name
|
---|
6499 | int vrc = RTThreadCreate(NULL, Medium::Task::fntMediumTask, pTask,
|
---|
6500 | 0, RTTHREADTYPE_MAIN_HEAVY_WORKER, 0,
|
---|
6501 | "Medium::Task");
|
---|
6502 | if (RT_FAILURE(vrc))
|
---|
6503 | {
|
---|
6504 | delete pTask;
|
---|
6505 | return setError(E_FAIL, "Could not create Medium::Task thread (%Rrc)\n", vrc);
|
---|
6506 | }
|
---|
6507 |
|
---|
6508 | return S_OK;
|
---|
6509 | }
|
---|
6510 |
|
---|
6511 | /**
|
---|
6512 | * Runs Medium::Task::handler() on the current thread instead of creating
|
---|
6513 | * a new one.
|
---|
6514 | *
|
---|
6515 | * This call implies that it is made on another temporary thread created for
|
---|
6516 | * some asynchronous task. Avoid calling it from a normal thread since the task
|
---|
6517 | * operations are potentially lengthy and will block the calling thread in this
|
---|
6518 | * case.
|
---|
6519 | *
|
---|
6520 | * @note When the task is executed by this method, IProgress::notifyComplete()
|
---|
6521 | * is not called for the progress object associated with this task when
|
---|
6522 | * the task is finished. Instead, the result of the operation is returned
|
---|
6523 | * by this method directly and it's the caller's responsibility to
|
---|
6524 | * complete the progress object in this case.
|
---|
6525 | */
|
---|
6526 | HRESULT Medium::runNow(Medium::Task *pTask)
|
---|
6527 | {
|
---|
6528 | #ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
|
---|
6529 | /* Extreme paranoia: The calling thread should not hold the medium
|
---|
6530 | * tree lock or any medium lock. Since there is no separate lock class
|
---|
6531 | * for medium objects be even more strict: no other object locks. */
|
---|
6532 | Assert(!AutoLockHoldsLocksInClass(LOCKCLASS_LISTOFMEDIA));
|
---|
6533 | Assert(!AutoLockHoldsLocksInClass(getLockingClass()));
|
---|
6534 | #endif
|
---|
6535 |
|
---|
6536 | /* NIL_RTTHREAD indicates synchronous call. */
|
---|
6537 | return (HRESULT)Medium::Task::fntMediumTask(NIL_RTTHREAD, pTask);
|
---|
6538 | }
|
---|
6539 |
|
---|
6540 | /**
|
---|
6541 | * Implementation code for the "create base" task.
|
---|
6542 | *
|
---|
6543 | * This only gets started from Medium::CreateBaseStorage() and always runs
|
---|
6544 | * asynchronously. As a result, we always save the VirtualBox.xml file when
|
---|
6545 | * we're done here.
|
---|
6546 | *
|
---|
6547 | * @param task
|
---|
6548 | * @return
|
---|
6549 | */
|
---|
6550 | HRESULT Medium::taskCreateBaseHandler(Medium::CreateBaseTask &task)
|
---|
6551 | {
|
---|
6552 | HRESULT rc = S_OK;
|
---|
6553 |
|
---|
6554 | /* these parameters we need after creation */
|
---|
6555 | uint64_t size = 0, logicalSize = 0;
|
---|
6556 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
6557 | bool fGenerateUuid = false;
|
---|
6558 |
|
---|
6559 | try
|
---|
6560 | {
|
---|
6561 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6562 |
|
---|
6563 | /* The object may request a specific UUID (through a special form of
|
---|
6564 | * the setLocation() argument). Otherwise we have to generate it */
|
---|
6565 | Guid id = m->id;
|
---|
6566 | fGenerateUuid = id.isEmpty();
|
---|
6567 | if (fGenerateUuid)
|
---|
6568 | {
|
---|
6569 | id.create();
|
---|
6570 | /* VirtualBox::registerMedium() will need UUID */
|
---|
6571 | unconst(m->id) = id;
|
---|
6572 | }
|
---|
6573 |
|
---|
6574 | Utf8Str format(m->strFormat);
|
---|
6575 | Utf8Str location(m->strLocationFull);
|
---|
6576 | uint64_t capabilities = m->formatObj->getCapabilities();
|
---|
6577 | ComAssertThrow(capabilities & ( MediumFormatCapabilities_CreateFixed
|
---|
6578 | | MediumFormatCapabilities_CreateDynamic), E_FAIL);
|
---|
6579 | Assert(m->state == MediumState_Creating);
|
---|
6580 |
|
---|
6581 | PVBOXHDD hdd;
|
---|
6582 | int vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &hdd);
|
---|
6583 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6584 |
|
---|
6585 | /* unlock before the potentially lengthy operation */
|
---|
6586 | thisLock.release();
|
---|
6587 |
|
---|
6588 | try
|
---|
6589 | {
|
---|
6590 | /* ensure the directory exists */
|
---|
6591 | if (capabilities & MediumFormatCapabilities_File)
|
---|
6592 | {
|
---|
6593 | rc = VirtualBox::ensureFilePathExists(location, !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
6594 | if (FAILED(rc))
|
---|
6595 | throw rc;
|
---|
6596 | }
|
---|
6597 |
|
---|
6598 | VDGEOMETRY geo = { 0, 0, 0 }; /* auto-detect */
|
---|
6599 |
|
---|
6600 | vrc = VDCreateBase(hdd,
|
---|
6601 | format.c_str(),
|
---|
6602 | location.c_str(),
|
---|
6603 | task.mSize,
|
---|
6604 | task.mVariant & ~MediumVariant_NoCreateDir,
|
---|
6605 | NULL,
|
---|
6606 | &geo,
|
---|
6607 | &geo,
|
---|
6608 | id.raw(),
|
---|
6609 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
6610 | m->vdImageIfaces,
|
---|
6611 | task.mVDOperationIfaces);
|
---|
6612 | if (RT_FAILURE(vrc))
|
---|
6613 | throw setError(VBOX_E_FILE_ERROR,
|
---|
6614 | tr("Could not create the medium storage unit '%s'%s"),
|
---|
6615 | location.c_str(), vdError(vrc).c_str());
|
---|
6616 |
|
---|
6617 | size = VDGetFileSize(hdd, 0);
|
---|
6618 | logicalSize = VDGetSize(hdd, 0);
|
---|
6619 | unsigned uImageFlags;
|
---|
6620 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
6621 | if (RT_SUCCESS(vrc))
|
---|
6622 | variant = (MediumVariant_T)uImageFlags;
|
---|
6623 | }
|
---|
6624 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6625 |
|
---|
6626 | VDDestroy(hdd);
|
---|
6627 | }
|
---|
6628 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6629 |
|
---|
6630 | if (SUCCEEDED(rc))
|
---|
6631 | {
|
---|
6632 | /* register with mVirtualBox as the last step and move to
|
---|
6633 | * Created state only on success (leaving an orphan file is
|
---|
6634 | * better than breaking media registry consistency) */
|
---|
6635 | AutoWriteLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
6636 | ComObjPtr<Medium> pMedium;
|
---|
6637 | rc = m->pVirtualBox->registerMedium(this, &pMedium, DeviceType_HardDisk);
|
---|
6638 | Assert(this == pMedium);
|
---|
6639 | }
|
---|
6640 |
|
---|
6641 | // re-acquire the lock before changing state
|
---|
6642 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6643 |
|
---|
6644 | if (SUCCEEDED(rc))
|
---|
6645 | {
|
---|
6646 | m->state = MediumState_Created;
|
---|
6647 |
|
---|
6648 | m->size = size;
|
---|
6649 | m->logicalSize = logicalSize;
|
---|
6650 | m->variant = variant;
|
---|
6651 |
|
---|
6652 | thisLock.release();
|
---|
6653 | markRegistriesModified();
|
---|
6654 | if (task.isAsync())
|
---|
6655 | {
|
---|
6656 | // in asynchronous mode, save settings now
|
---|
6657 | m->pVirtualBox->saveModifiedRegistries();
|
---|
6658 | }
|
---|
6659 | }
|
---|
6660 | else
|
---|
6661 | {
|
---|
6662 | /* back to NotCreated on failure */
|
---|
6663 | m->state = MediumState_NotCreated;
|
---|
6664 |
|
---|
6665 | /* reset UUID to prevent it from being reused next time */
|
---|
6666 | if (fGenerateUuid)
|
---|
6667 | unconst(m->id).clear();
|
---|
6668 | }
|
---|
6669 |
|
---|
6670 | return rc;
|
---|
6671 | }
|
---|
6672 |
|
---|
6673 | /**
|
---|
6674 | * Implementation code for the "create diff" task.
|
---|
6675 | *
|
---|
6676 | * This task always gets started from Medium::createDiffStorage() and can run
|
---|
6677 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
6678 | * that function. If we run synchronously, the caller expects the medium
|
---|
6679 | * registry modification to be set before returning; otherwise (in asynchronous
|
---|
6680 | * mode), we save the settings ourselves.
|
---|
6681 | *
|
---|
6682 | * @param task
|
---|
6683 | * @return
|
---|
6684 | */
|
---|
6685 | HRESULT Medium::taskCreateDiffHandler(Medium::CreateDiffTask &task)
|
---|
6686 | {
|
---|
6687 | HRESULT rcTmp = S_OK;
|
---|
6688 |
|
---|
6689 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
6690 |
|
---|
6691 | uint64_t size = 0, logicalSize = 0;
|
---|
6692 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
6693 | bool fGenerateUuid = false;
|
---|
6694 |
|
---|
6695 | try
|
---|
6696 | {
|
---|
6697 | /* Lock both in {parent,child} order. */
|
---|
6698 | AutoMultiWriteLock2 mediaLock(this, pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
6699 |
|
---|
6700 | /* The object may request a specific UUID (through a special form of
|
---|
6701 | * the setLocation() argument). Otherwise we have to generate it */
|
---|
6702 | Guid targetId = pTarget->m->id;
|
---|
6703 | fGenerateUuid = targetId.isEmpty();
|
---|
6704 | if (fGenerateUuid)
|
---|
6705 | {
|
---|
6706 | targetId.create();
|
---|
6707 | /* VirtualBox::registerMedium() will need UUID */
|
---|
6708 | unconst(pTarget->m->id) = targetId;
|
---|
6709 | }
|
---|
6710 |
|
---|
6711 | Guid id = m->id;
|
---|
6712 |
|
---|
6713 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
6714 | Utf8Str targetLocation(pTarget->m->strLocationFull);
|
---|
6715 | uint64_t capabilities = pTarget->m->formatObj->getCapabilities();
|
---|
6716 | ComAssertThrow(capabilities & MediumFormatCapabilities_CreateDynamic, E_FAIL);
|
---|
6717 |
|
---|
6718 | Assert(pTarget->m->state == MediumState_Creating);
|
---|
6719 | Assert(m->state == MediumState_LockedRead);
|
---|
6720 |
|
---|
6721 | PVBOXHDD hdd;
|
---|
6722 | int vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &hdd);
|
---|
6723 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6724 |
|
---|
6725 | /* the two media are now protected by their non-default states;
|
---|
6726 | * unlock the media before the potentially lengthy operation */
|
---|
6727 | mediaLock.release();
|
---|
6728 |
|
---|
6729 | try
|
---|
6730 | {
|
---|
6731 | /* Open all media in the target chain but the last. */
|
---|
6732 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
6733 | task.mpMediumLockList->GetBegin();
|
---|
6734 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
6735 | task.mpMediumLockList->GetEnd();
|
---|
6736 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
6737 | it != targetListEnd;
|
---|
6738 | ++it)
|
---|
6739 | {
|
---|
6740 | const MediumLock &mediumLock = *it;
|
---|
6741 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
6742 |
|
---|
6743 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
6744 |
|
---|
6745 | /* Skip over the target diff medium */
|
---|
6746 | if (pMedium->m->state == MediumState_Creating)
|
---|
6747 | continue;
|
---|
6748 |
|
---|
6749 | /* sanity check */
|
---|
6750 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
6751 |
|
---|
6752 | /* Open all media in appropriate mode. */
|
---|
6753 | vrc = VDOpen(hdd,
|
---|
6754 | pMedium->m->strFormat.c_str(),
|
---|
6755 | pMedium->m->strLocationFull.c_str(),
|
---|
6756 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
6757 | pMedium->m->vdImageIfaces);
|
---|
6758 | if (RT_FAILURE(vrc))
|
---|
6759 | throw setError(VBOX_E_FILE_ERROR,
|
---|
6760 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
6761 | pMedium->m->strLocationFull.c_str(),
|
---|
6762 | vdError(vrc).c_str());
|
---|
6763 | }
|
---|
6764 |
|
---|
6765 | /* ensure the target directory exists */
|
---|
6766 | if (capabilities & MediumFormatCapabilities_File)
|
---|
6767 | {
|
---|
6768 | HRESULT rc = VirtualBox::ensureFilePathExists(targetLocation, !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
6769 | if (FAILED(rc))
|
---|
6770 | throw rc;
|
---|
6771 | }
|
---|
6772 |
|
---|
6773 | vrc = VDCreateDiff(hdd,
|
---|
6774 | targetFormat.c_str(),
|
---|
6775 | targetLocation.c_str(),
|
---|
6776 | (task.mVariant & ~MediumVariant_NoCreateDir) | VD_IMAGE_FLAGS_DIFF,
|
---|
6777 | NULL,
|
---|
6778 | targetId.raw(),
|
---|
6779 | id.raw(),
|
---|
6780 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
6781 | pTarget->m->vdImageIfaces,
|
---|
6782 | task.mVDOperationIfaces);
|
---|
6783 | if (RT_FAILURE(vrc))
|
---|
6784 | throw setError(VBOX_E_FILE_ERROR,
|
---|
6785 | tr("Could not create the differencing medium storage unit '%s'%s"),
|
---|
6786 | targetLocation.c_str(), vdError(vrc).c_str());
|
---|
6787 |
|
---|
6788 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
6789 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
6790 | unsigned uImageFlags;
|
---|
6791 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
6792 | if (RT_SUCCESS(vrc))
|
---|
6793 | variant = (MediumVariant_T)uImageFlags;
|
---|
6794 | }
|
---|
6795 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
6796 |
|
---|
6797 | VDDestroy(hdd);
|
---|
6798 | }
|
---|
6799 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
6800 |
|
---|
6801 | MultiResult mrc(rcTmp);
|
---|
6802 |
|
---|
6803 | if (SUCCEEDED(mrc))
|
---|
6804 | {
|
---|
6805 | AutoWriteLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
6806 |
|
---|
6807 | Assert(pTarget->m->pParent.isNull());
|
---|
6808 |
|
---|
6809 | /* associate the child with the parent */
|
---|
6810 | pTarget->m->pParent = this;
|
---|
6811 | m->llChildren.push_back(pTarget);
|
---|
6812 |
|
---|
6813 | /** @todo r=klaus neither target nor base() are locked,
|
---|
6814 | * potential race! */
|
---|
6815 | /* diffs for immutable media are auto-reset by default */
|
---|
6816 | pTarget->m->autoReset = (getBase()->m->type == MediumType_Immutable);
|
---|
6817 |
|
---|
6818 | /* register with mVirtualBox as the last step and move to
|
---|
6819 | * Created state only on success (leaving an orphan file is
|
---|
6820 | * better than breaking media registry consistency) */
|
---|
6821 | ComObjPtr<Medium> pMedium;
|
---|
6822 | mrc = m->pVirtualBox->registerMedium(pTarget, &pMedium, DeviceType_HardDisk);
|
---|
6823 | Assert(pTarget == pMedium);
|
---|
6824 |
|
---|
6825 | if (FAILED(mrc))
|
---|
6826 | /* break the parent association on failure to register */
|
---|
6827 | deparent();
|
---|
6828 | }
|
---|
6829 |
|
---|
6830 | AutoMultiWriteLock2 mediaLock(this, pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
6831 |
|
---|
6832 | if (SUCCEEDED(mrc))
|
---|
6833 | {
|
---|
6834 | pTarget->m->state = MediumState_Created;
|
---|
6835 |
|
---|
6836 | pTarget->m->size = size;
|
---|
6837 | pTarget->m->logicalSize = logicalSize;
|
---|
6838 | pTarget->m->variant = variant;
|
---|
6839 | }
|
---|
6840 | else
|
---|
6841 | {
|
---|
6842 | /* back to NotCreated on failure */
|
---|
6843 | pTarget->m->state = MediumState_NotCreated;
|
---|
6844 |
|
---|
6845 | pTarget->m->autoReset = false;
|
---|
6846 |
|
---|
6847 | /* reset UUID to prevent it from being reused next time */
|
---|
6848 | if (fGenerateUuid)
|
---|
6849 | unconst(pTarget->m->id).clear();
|
---|
6850 | }
|
---|
6851 |
|
---|
6852 | // deregister the task registered in createDiffStorage()
|
---|
6853 | Assert(m->numCreateDiffTasks != 0);
|
---|
6854 | --m->numCreateDiffTasks;
|
---|
6855 |
|
---|
6856 | mediaLock.release();
|
---|
6857 | markRegistriesModified();
|
---|
6858 | if (task.isAsync())
|
---|
6859 | {
|
---|
6860 | // in asynchronous mode, save settings now
|
---|
6861 | m->pVirtualBox->saveModifiedRegistries();
|
---|
6862 | }
|
---|
6863 |
|
---|
6864 | /* Note that in sync mode, it's the caller's responsibility to
|
---|
6865 | * unlock the medium. */
|
---|
6866 |
|
---|
6867 | return mrc;
|
---|
6868 | }
|
---|
6869 |
|
---|
6870 | /**
|
---|
6871 | * Implementation code for the "merge" task.
|
---|
6872 | *
|
---|
6873 | * This task always gets started from Medium::mergeTo() and can run
|
---|
6874 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
6875 | * that function. If we run synchronously, the caller expects the medium
|
---|
6876 | * registry modification to be set before returning; otherwise (in asynchronous
|
---|
6877 | * mode), we save the settings ourselves.
|
---|
6878 | *
|
---|
6879 | * @param task
|
---|
6880 | * @return
|
---|
6881 | */
|
---|
6882 | HRESULT Medium::taskMergeHandler(Medium::MergeTask &task)
|
---|
6883 | {
|
---|
6884 | HRESULT rcTmp = S_OK;
|
---|
6885 |
|
---|
6886 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
6887 |
|
---|
6888 | try
|
---|
6889 | {
|
---|
6890 | PVBOXHDD hdd;
|
---|
6891 | int vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &hdd);
|
---|
6892 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6893 |
|
---|
6894 | try
|
---|
6895 | {
|
---|
6896 | // Similar code appears in SessionMachine::onlineMergeMedium, so
|
---|
6897 | // if you make any changes below check whether they are applicable
|
---|
6898 | // in that context as well.
|
---|
6899 |
|
---|
6900 | unsigned uTargetIdx = VD_LAST_IMAGE;
|
---|
6901 | unsigned uSourceIdx = VD_LAST_IMAGE;
|
---|
6902 | /* Open all media in the chain. */
|
---|
6903 | MediumLockList::Base::iterator lockListBegin =
|
---|
6904 | task.mpMediumLockList->GetBegin();
|
---|
6905 | MediumLockList::Base::iterator lockListEnd =
|
---|
6906 | task.mpMediumLockList->GetEnd();
|
---|
6907 | unsigned i = 0;
|
---|
6908 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
6909 | it != lockListEnd;
|
---|
6910 | ++it)
|
---|
6911 | {
|
---|
6912 | MediumLock &mediumLock = *it;
|
---|
6913 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
6914 |
|
---|
6915 | if (pMedium == this)
|
---|
6916 | uSourceIdx = i;
|
---|
6917 | else if (pMedium == pTarget)
|
---|
6918 | uTargetIdx = i;
|
---|
6919 |
|
---|
6920 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
6921 |
|
---|
6922 | /*
|
---|
6923 | * complex sanity (sane complexity)
|
---|
6924 | *
|
---|
6925 | * The current medium must be in the Deleting (medium is merged)
|
---|
6926 | * or LockedRead (parent medium) state if it is not the target.
|
---|
6927 | * If it is the target it must be in the LockedWrite state.
|
---|
6928 | */
|
---|
6929 | Assert( ( pMedium != pTarget
|
---|
6930 | && ( pMedium->m->state == MediumState_Deleting
|
---|
6931 | || pMedium->m->state == MediumState_LockedRead))
|
---|
6932 | || ( pMedium == pTarget
|
---|
6933 | && pMedium->m->state == MediumState_LockedWrite));
|
---|
6934 |
|
---|
6935 | /*
|
---|
6936 | * Medium must be the target, in the LockedRead state
|
---|
6937 | * or Deleting state where it is not allowed to be attached
|
---|
6938 | * to a virtual machine.
|
---|
6939 | */
|
---|
6940 | Assert( pMedium == pTarget
|
---|
6941 | || pMedium->m->state == MediumState_LockedRead
|
---|
6942 | || ( pMedium->m->backRefs.size() == 0
|
---|
6943 | && pMedium->m->state == MediumState_Deleting));
|
---|
6944 | /* The source medium must be in Deleting state. */
|
---|
6945 | Assert( pMedium != this
|
---|
6946 | || pMedium->m->state == MediumState_Deleting);
|
---|
6947 |
|
---|
6948 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
6949 |
|
---|
6950 | if ( pMedium->m->state == MediumState_LockedRead
|
---|
6951 | || pMedium->m->state == MediumState_Deleting)
|
---|
6952 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
6953 | if (pMedium->m->type == MediumType_Shareable)
|
---|
6954 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
6955 |
|
---|
6956 | /* Open the medium */
|
---|
6957 | vrc = VDOpen(hdd,
|
---|
6958 | pMedium->m->strFormat.c_str(),
|
---|
6959 | pMedium->m->strLocationFull.c_str(),
|
---|
6960 | uOpenFlags | m->uOpenFlagsDef,
|
---|
6961 | pMedium->m->vdImageIfaces);
|
---|
6962 | if (RT_FAILURE(vrc))
|
---|
6963 | throw vrc;
|
---|
6964 |
|
---|
6965 | i++;
|
---|
6966 | }
|
---|
6967 |
|
---|
6968 | ComAssertThrow( uSourceIdx != VD_LAST_IMAGE
|
---|
6969 | && uTargetIdx != VD_LAST_IMAGE, E_FAIL);
|
---|
6970 |
|
---|
6971 | vrc = VDMerge(hdd, uSourceIdx, uTargetIdx,
|
---|
6972 | task.mVDOperationIfaces);
|
---|
6973 | if (RT_FAILURE(vrc))
|
---|
6974 | throw vrc;
|
---|
6975 |
|
---|
6976 | /* update parent UUIDs */
|
---|
6977 | if (!task.mfMergeForward)
|
---|
6978 | {
|
---|
6979 | /* we need to update UUIDs of all source's children
|
---|
6980 | * which cannot be part of the container at once so
|
---|
6981 | * add each one in there individually */
|
---|
6982 | if (task.mChildrenToReparent.size() > 0)
|
---|
6983 | {
|
---|
6984 | for (MediaList::const_iterator it = task.mChildrenToReparent.begin();
|
---|
6985 | it != task.mChildrenToReparent.end();
|
---|
6986 | ++it)
|
---|
6987 | {
|
---|
6988 | /* VD_OPEN_FLAGS_INFO since UUID is wrong yet */
|
---|
6989 | vrc = VDOpen(hdd,
|
---|
6990 | (*it)->m->strFormat.c_str(),
|
---|
6991 | (*it)->m->strLocationFull.c_str(),
|
---|
6992 | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
6993 | (*it)->m->vdImageIfaces);
|
---|
6994 | if (RT_FAILURE(vrc))
|
---|
6995 | throw vrc;
|
---|
6996 |
|
---|
6997 | vrc = VDSetParentUuid(hdd, VD_LAST_IMAGE,
|
---|
6998 | pTarget->m->id.raw());
|
---|
6999 | if (RT_FAILURE(vrc))
|
---|
7000 | throw vrc;
|
---|
7001 |
|
---|
7002 | vrc = VDClose(hdd, false /* fDelete */);
|
---|
7003 | if (RT_FAILURE(vrc))
|
---|
7004 | throw vrc;
|
---|
7005 |
|
---|
7006 | (*it)->UnlockWrite(NULL);
|
---|
7007 | }
|
---|
7008 | }
|
---|
7009 | }
|
---|
7010 | }
|
---|
7011 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
7012 | catch (int aVRC)
|
---|
7013 | {
|
---|
7014 | rcTmp = setError(VBOX_E_FILE_ERROR,
|
---|
7015 | tr("Could not merge the medium '%s' to '%s'%s"),
|
---|
7016 | m->strLocationFull.c_str(),
|
---|
7017 | pTarget->m->strLocationFull.c_str(),
|
---|
7018 | vdError(aVRC).c_str());
|
---|
7019 | }
|
---|
7020 |
|
---|
7021 | VDDestroy(hdd);
|
---|
7022 | }
|
---|
7023 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
7024 |
|
---|
7025 | ErrorInfoKeeper eik;
|
---|
7026 | MultiResult mrc(rcTmp);
|
---|
7027 | HRESULT rc2;
|
---|
7028 |
|
---|
7029 | if (SUCCEEDED(mrc))
|
---|
7030 | {
|
---|
7031 | /* all media but the target were successfully deleted by
|
---|
7032 | * VDMerge; reparent the last one and uninitialize deleted media. */
|
---|
7033 |
|
---|
7034 | AutoWriteLock treeLock(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
7035 |
|
---|
7036 | if (task.mfMergeForward)
|
---|
7037 | {
|
---|
7038 | /* first, unregister the target since it may become a base
|
---|
7039 | * medium which needs re-registration */
|
---|
7040 | rc2 = m->pVirtualBox->unregisterMedium(pTarget);
|
---|
7041 | AssertComRC(rc2);
|
---|
7042 |
|
---|
7043 | /* then, reparent it and disconnect the deleted branch at
|
---|
7044 | * both ends (chain->parent() is source's parent) */
|
---|
7045 | pTarget->deparent();
|
---|
7046 | pTarget->m->pParent = task.mParentForTarget;
|
---|
7047 | if (pTarget->m->pParent)
|
---|
7048 | {
|
---|
7049 | pTarget->m->pParent->m->llChildren.push_back(pTarget);
|
---|
7050 | deparent();
|
---|
7051 | }
|
---|
7052 |
|
---|
7053 | /* then, register again */
|
---|
7054 | ComObjPtr<Medium> pMedium;
|
---|
7055 | rc2 = m->pVirtualBox->registerMedium(pTarget, &pMedium,
|
---|
7056 | DeviceType_HardDisk);
|
---|
7057 | AssertComRC(rc2);
|
---|
7058 | }
|
---|
7059 | else
|
---|
7060 | {
|
---|
7061 | Assert(pTarget->getChildren().size() == 1);
|
---|
7062 | Medium *targetChild = pTarget->getChildren().front();
|
---|
7063 |
|
---|
7064 | /* disconnect the deleted branch at the elder end */
|
---|
7065 | targetChild->deparent();
|
---|
7066 |
|
---|
7067 | /* reparent source's children and disconnect the deleted
|
---|
7068 | * branch at the younger end */
|
---|
7069 | if (task.mChildrenToReparent.size() > 0)
|
---|
7070 | {
|
---|
7071 | /* obey {parent,child} lock order */
|
---|
7072 | AutoWriteLock sourceLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7073 |
|
---|
7074 | for (MediaList::const_iterator it = task.mChildrenToReparent.begin();
|
---|
7075 | it != task.mChildrenToReparent.end();
|
---|
7076 | it++)
|
---|
7077 | {
|
---|
7078 | Medium *pMedium = *it;
|
---|
7079 | AutoWriteLock childLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
7080 |
|
---|
7081 | pMedium->deparent(); // removes pMedium from source
|
---|
7082 | pMedium->setParent(pTarget);
|
---|
7083 | }
|
---|
7084 | }
|
---|
7085 | }
|
---|
7086 |
|
---|
7087 | /* unregister and uninitialize all media removed by the merge */
|
---|
7088 | MediumLockList::Base::iterator lockListBegin =
|
---|
7089 | task.mpMediumLockList->GetBegin();
|
---|
7090 | MediumLockList::Base::iterator lockListEnd =
|
---|
7091 | task.mpMediumLockList->GetEnd();
|
---|
7092 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
7093 | it != lockListEnd;
|
---|
7094 | )
|
---|
7095 | {
|
---|
7096 | MediumLock &mediumLock = *it;
|
---|
7097 | /* Create a real copy of the medium pointer, as the medium
|
---|
7098 | * lock deletion below would invalidate the referenced object. */
|
---|
7099 | const ComObjPtr<Medium> pMedium = mediumLock.GetMedium();
|
---|
7100 |
|
---|
7101 | /* The target and all media not merged (readonly) are skipped */
|
---|
7102 | if ( pMedium == pTarget
|
---|
7103 | || pMedium->m->state == MediumState_LockedRead)
|
---|
7104 | {
|
---|
7105 | ++it;
|
---|
7106 | continue;
|
---|
7107 | }
|
---|
7108 |
|
---|
7109 | rc2 = pMedium->m->pVirtualBox->unregisterMedium(pMedium);
|
---|
7110 | AssertComRC(rc2);
|
---|
7111 |
|
---|
7112 | /* now, uninitialize the deleted medium (note that
|
---|
7113 | * due to the Deleting state, uninit() will not touch
|
---|
7114 | * the parent-child relationship so we need to
|
---|
7115 | * uninitialize each disk individually) */
|
---|
7116 |
|
---|
7117 | /* note that the operation initiator medium (which is
|
---|
7118 | * normally also the source medium) is a special case
|
---|
7119 | * -- there is one more caller added by Task to it which
|
---|
7120 | * we must release. Also, if we are in sync mode, the
|
---|
7121 | * caller may still hold an AutoCaller instance for it
|
---|
7122 | * and therefore we cannot uninit() it (it's therefore
|
---|
7123 | * the caller's responsibility) */
|
---|
7124 | if (pMedium == this)
|
---|
7125 | {
|
---|
7126 | Assert(getChildren().size() == 0);
|
---|
7127 | Assert(m->backRefs.size() == 0);
|
---|
7128 | task.mMediumCaller.release();
|
---|
7129 | }
|
---|
7130 |
|
---|
7131 | /* Delete the medium lock list entry, which also releases the
|
---|
7132 | * caller added by MergeChain before uninit() and updates the
|
---|
7133 | * iterator to point to the right place. */
|
---|
7134 | rc2 = task.mpMediumLockList->RemoveByIterator(it);
|
---|
7135 | AssertComRC(rc2);
|
---|
7136 |
|
---|
7137 | if (task.isAsync() || pMedium != this)
|
---|
7138 | pMedium->uninit();
|
---|
7139 | }
|
---|
7140 | }
|
---|
7141 |
|
---|
7142 | markRegistriesModified();
|
---|
7143 | if (task.isAsync())
|
---|
7144 | {
|
---|
7145 | // in asynchronous mode, save settings now
|
---|
7146 | eik.restore();
|
---|
7147 | m->pVirtualBox->saveModifiedRegistries();
|
---|
7148 | eik.fetch();
|
---|
7149 | }
|
---|
7150 |
|
---|
7151 | if (FAILED(mrc))
|
---|
7152 | {
|
---|
7153 | /* Here we come if either VDMerge() failed (in which case we
|
---|
7154 | * assume that it tried to do everything to make a further
|
---|
7155 | * retry possible -- e.g. not deleted intermediate media
|
---|
7156 | * and so on) or VirtualBox::saveRegistries() failed (where we
|
---|
7157 | * should have the original tree but with intermediate storage
|
---|
7158 | * units deleted by VDMerge()). We have to only restore states
|
---|
7159 | * (through the MergeChain dtor) unless we are run synchronously
|
---|
7160 | * in which case it's the responsibility of the caller as stated
|
---|
7161 | * in the mergeTo() docs. The latter also implies that we
|
---|
7162 | * don't own the merge chain, so release it in this case. */
|
---|
7163 | if (task.isAsync())
|
---|
7164 | {
|
---|
7165 | Assert(task.mChildrenToReparent.size() == 0);
|
---|
7166 | cancelMergeTo(task.mChildrenToReparent, task.mpMediumLockList);
|
---|
7167 | }
|
---|
7168 | }
|
---|
7169 |
|
---|
7170 | return mrc;
|
---|
7171 | }
|
---|
7172 |
|
---|
7173 | /**
|
---|
7174 | * Implementation code for the "clone" task.
|
---|
7175 | *
|
---|
7176 | * This only gets started from Medium::CloneTo() and always runs asynchronously.
|
---|
7177 | * As a result, we always save the VirtualBox.xml file when we're done here.
|
---|
7178 | *
|
---|
7179 | * @param task
|
---|
7180 | * @return
|
---|
7181 | */
|
---|
7182 | HRESULT Medium::taskCloneHandler(Medium::CloneTask &task)
|
---|
7183 | {
|
---|
7184 | HRESULT rcTmp = S_OK;
|
---|
7185 |
|
---|
7186 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
7187 | const ComObjPtr<Medium> &pParent = task.mParent;
|
---|
7188 |
|
---|
7189 | bool fCreatingTarget = false;
|
---|
7190 |
|
---|
7191 | uint64_t size = 0, logicalSize = 0;
|
---|
7192 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
7193 | bool fGenerateUuid = false;
|
---|
7194 |
|
---|
7195 | try
|
---|
7196 | {
|
---|
7197 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
7198 | * signal from the task initiator (which releases it only after
|
---|
7199 | * RTThreadCreate()) that we can start the job. */
|
---|
7200 | AutoMultiWriteLock3 thisLock(this, pTarget, pParent COMMA_LOCKVAL_SRC_POS);
|
---|
7201 |
|
---|
7202 | fCreatingTarget = pTarget->m->state == MediumState_Creating;
|
---|
7203 |
|
---|
7204 | /* The object may request a specific UUID (through a special form of
|
---|
7205 | * the setLocation() argument). Otherwise we have to generate it */
|
---|
7206 | Guid targetId = pTarget->m->id;
|
---|
7207 | fGenerateUuid = targetId.isEmpty();
|
---|
7208 | if (fGenerateUuid)
|
---|
7209 | {
|
---|
7210 | targetId.create();
|
---|
7211 | /* VirtualBox::registerMedium() will need UUID */
|
---|
7212 | unconst(pTarget->m->id) = targetId;
|
---|
7213 | }
|
---|
7214 |
|
---|
7215 | PVBOXHDD hdd;
|
---|
7216 | int vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &hdd);
|
---|
7217 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7218 |
|
---|
7219 | try
|
---|
7220 | {
|
---|
7221 | /* Open all media in the source chain. */
|
---|
7222 | MediumLockList::Base::const_iterator sourceListBegin =
|
---|
7223 | task.mpSourceMediumLockList->GetBegin();
|
---|
7224 | MediumLockList::Base::const_iterator sourceListEnd =
|
---|
7225 | task.mpSourceMediumLockList->GetEnd();
|
---|
7226 | for (MediumLockList::Base::const_iterator it = sourceListBegin;
|
---|
7227 | it != sourceListEnd;
|
---|
7228 | ++it)
|
---|
7229 | {
|
---|
7230 | const MediumLock &mediumLock = *it;
|
---|
7231 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
7232 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
7233 |
|
---|
7234 | /* sanity check */
|
---|
7235 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
7236 |
|
---|
7237 | /** Open all media in read-only mode. */
|
---|
7238 | vrc = VDOpen(hdd,
|
---|
7239 | pMedium->m->strFormat.c_str(),
|
---|
7240 | pMedium->m->strLocationFull.c_str(),
|
---|
7241 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
7242 | pMedium->m->vdImageIfaces);
|
---|
7243 | if (RT_FAILURE(vrc))
|
---|
7244 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7245 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
7246 | pMedium->m->strLocationFull.c_str(),
|
---|
7247 | vdError(vrc).c_str());
|
---|
7248 | }
|
---|
7249 |
|
---|
7250 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
7251 | Utf8Str targetLocation(pTarget->m->strLocationFull);
|
---|
7252 | uint64_t capabilities = pTarget->m->formatObj->getCapabilities();
|
---|
7253 |
|
---|
7254 | Assert( pTarget->m->state == MediumState_Creating
|
---|
7255 | || pTarget->m->state == MediumState_LockedWrite);
|
---|
7256 | Assert(m->state == MediumState_LockedRead);
|
---|
7257 | Assert( pParent.isNull()
|
---|
7258 | || pParent->m->state == MediumState_LockedRead);
|
---|
7259 |
|
---|
7260 | /* unlock before the potentially lengthy operation */
|
---|
7261 | thisLock.release();
|
---|
7262 |
|
---|
7263 | /* ensure the target directory exists */
|
---|
7264 | if (capabilities & MediumFormatCapabilities_File)
|
---|
7265 | {
|
---|
7266 | HRESULT rc = VirtualBox::ensureFilePathExists(targetLocation, !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
7267 | if (FAILED(rc))
|
---|
7268 | throw rc;
|
---|
7269 | }
|
---|
7270 |
|
---|
7271 | PVBOXHDD targetHdd;
|
---|
7272 | vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &targetHdd);
|
---|
7273 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7274 |
|
---|
7275 | try
|
---|
7276 | {
|
---|
7277 | /* Open all media in the target chain. */
|
---|
7278 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
7279 | task.mpTargetMediumLockList->GetBegin();
|
---|
7280 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
7281 | task.mpTargetMediumLockList->GetEnd();
|
---|
7282 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
7283 | it != targetListEnd;
|
---|
7284 | ++it)
|
---|
7285 | {
|
---|
7286 | const MediumLock &mediumLock = *it;
|
---|
7287 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
7288 |
|
---|
7289 | /* If the target medium is not created yet there's no
|
---|
7290 | * reason to open it. */
|
---|
7291 | if (pMedium == pTarget && fCreatingTarget)
|
---|
7292 | continue;
|
---|
7293 |
|
---|
7294 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
7295 |
|
---|
7296 | /* sanity check */
|
---|
7297 | Assert( pMedium->m->state == MediumState_LockedRead
|
---|
7298 | || pMedium->m->state == MediumState_LockedWrite);
|
---|
7299 |
|
---|
7300 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
7301 | if (pMedium->m->state != MediumState_LockedWrite)
|
---|
7302 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
7303 | if (pMedium->m->type == MediumType_Shareable)
|
---|
7304 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
7305 |
|
---|
7306 | /* Open all media in appropriate mode. */
|
---|
7307 | vrc = VDOpen(targetHdd,
|
---|
7308 | pMedium->m->strFormat.c_str(),
|
---|
7309 | pMedium->m->strLocationFull.c_str(),
|
---|
7310 | uOpenFlags | m->uOpenFlagsDef,
|
---|
7311 | pMedium->m->vdImageIfaces);
|
---|
7312 | if (RT_FAILURE(vrc))
|
---|
7313 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7314 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
7315 | pMedium->m->strLocationFull.c_str(),
|
---|
7316 | vdError(vrc).c_str());
|
---|
7317 | }
|
---|
7318 |
|
---|
7319 | /** @todo r=klaus target isn't locked, race getting the state */
|
---|
7320 | if (task.midxSrcImageSame == UINT32_MAX)
|
---|
7321 | {
|
---|
7322 | vrc = VDCopy(hdd,
|
---|
7323 | VD_LAST_IMAGE,
|
---|
7324 | targetHdd,
|
---|
7325 | targetFormat.c_str(),
|
---|
7326 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
7327 | false /* fMoveByRename */,
|
---|
7328 | 0 /* cbSize */,
|
---|
7329 | task.mVariant & ~MediumVariant_NoCreateDir,
|
---|
7330 | targetId.raw(),
|
---|
7331 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
7332 | NULL /* pVDIfsOperation */,
|
---|
7333 | pTarget->m->vdImageIfaces,
|
---|
7334 | task.mVDOperationIfaces);
|
---|
7335 | }
|
---|
7336 | else
|
---|
7337 | {
|
---|
7338 | vrc = VDCopyEx(hdd,
|
---|
7339 | VD_LAST_IMAGE,
|
---|
7340 | targetHdd,
|
---|
7341 | targetFormat.c_str(),
|
---|
7342 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
7343 | false /* fMoveByRename */,
|
---|
7344 | 0 /* cbSize */,
|
---|
7345 | task.midxSrcImageSame,
|
---|
7346 | task.midxDstImageSame,
|
---|
7347 | task.mVariant & ~MediumVariant_NoCreateDir,
|
---|
7348 | targetId.raw(),
|
---|
7349 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
7350 | NULL /* pVDIfsOperation */,
|
---|
7351 | pTarget->m->vdImageIfaces,
|
---|
7352 | task.mVDOperationIfaces);
|
---|
7353 | }
|
---|
7354 | if (RT_FAILURE(vrc))
|
---|
7355 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7356 | tr("Could not create the clone medium '%s'%s"),
|
---|
7357 | targetLocation.c_str(), vdError(vrc).c_str());
|
---|
7358 |
|
---|
7359 | size = VDGetFileSize(targetHdd, VD_LAST_IMAGE);
|
---|
7360 | logicalSize = VDGetSize(targetHdd, VD_LAST_IMAGE);
|
---|
7361 | unsigned uImageFlags;
|
---|
7362 | vrc = VDGetImageFlags(targetHdd, 0, &uImageFlags);
|
---|
7363 | if (RT_SUCCESS(vrc))
|
---|
7364 | variant = (MediumVariant_T)uImageFlags;
|
---|
7365 | }
|
---|
7366 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
7367 |
|
---|
7368 | VDDestroy(targetHdd);
|
---|
7369 | }
|
---|
7370 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
7371 |
|
---|
7372 | VDDestroy(hdd);
|
---|
7373 | }
|
---|
7374 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
7375 |
|
---|
7376 | ErrorInfoKeeper eik;
|
---|
7377 | MultiResult mrc(rcTmp);
|
---|
7378 |
|
---|
7379 | /* Only do the parent changes for newly created media. */
|
---|
7380 | if (SUCCEEDED(mrc) && fCreatingTarget)
|
---|
7381 | {
|
---|
7382 | /* we set mParent & children() */
|
---|
7383 | AutoWriteLock alock2(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
7384 |
|
---|
7385 | Assert(pTarget->m->pParent.isNull());
|
---|
7386 |
|
---|
7387 | if (pParent)
|
---|
7388 | {
|
---|
7389 | /* associate the clone with the parent and deassociate
|
---|
7390 | * from VirtualBox */
|
---|
7391 | pTarget->m->pParent = pParent;
|
---|
7392 | pParent->m->llChildren.push_back(pTarget);
|
---|
7393 |
|
---|
7394 | /* register with mVirtualBox as the last step and move to
|
---|
7395 | * Created state only on success (leaving an orphan file is
|
---|
7396 | * better than breaking media registry consistency) */
|
---|
7397 | eik.restore();
|
---|
7398 | ComObjPtr<Medium> pMedium;
|
---|
7399 | mrc = pParent->m->pVirtualBox->registerMedium(pTarget, &pMedium,
|
---|
7400 | DeviceType_HardDisk);
|
---|
7401 | Assert(pTarget == pMedium);
|
---|
7402 | eik.fetch();
|
---|
7403 |
|
---|
7404 | if (FAILED(mrc))
|
---|
7405 | /* break parent association on failure to register */
|
---|
7406 | pTarget->deparent(); // removes target from parent
|
---|
7407 | }
|
---|
7408 | else
|
---|
7409 | {
|
---|
7410 | /* just register */
|
---|
7411 | eik.restore();
|
---|
7412 | ComObjPtr<Medium> pMedium;
|
---|
7413 | mrc = m->pVirtualBox->registerMedium(pTarget, &pMedium,
|
---|
7414 | DeviceType_HardDisk);
|
---|
7415 | Assert(pTarget == pMedium);
|
---|
7416 | eik.fetch();
|
---|
7417 | }
|
---|
7418 | }
|
---|
7419 |
|
---|
7420 | if (fCreatingTarget)
|
---|
7421 | {
|
---|
7422 | AutoWriteLock mLock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
7423 |
|
---|
7424 | if (SUCCEEDED(mrc))
|
---|
7425 | {
|
---|
7426 | pTarget->m->state = MediumState_Created;
|
---|
7427 |
|
---|
7428 | pTarget->m->size = size;
|
---|
7429 | pTarget->m->logicalSize = logicalSize;
|
---|
7430 | pTarget->m->variant = variant;
|
---|
7431 | }
|
---|
7432 | else
|
---|
7433 | {
|
---|
7434 | /* back to NotCreated on failure */
|
---|
7435 | pTarget->m->state = MediumState_NotCreated;
|
---|
7436 |
|
---|
7437 | /* reset UUID to prevent it from being reused next time */
|
---|
7438 | if (fGenerateUuid)
|
---|
7439 | unconst(pTarget->m->id).clear();
|
---|
7440 | }
|
---|
7441 | }
|
---|
7442 |
|
---|
7443 | // now, at the end of this task (always asynchronous), save the settings
|
---|
7444 | if (SUCCEEDED(mrc))
|
---|
7445 | {
|
---|
7446 | // save the settings
|
---|
7447 | markRegistriesModified();
|
---|
7448 | /* collect multiple errors */
|
---|
7449 | eik.restore();
|
---|
7450 | m->pVirtualBox->saveModifiedRegistries();
|
---|
7451 | eik.fetch();
|
---|
7452 | }
|
---|
7453 |
|
---|
7454 | /* Everything is explicitly unlocked when the task exits,
|
---|
7455 | * as the task destruction also destroys the source chain. */
|
---|
7456 |
|
---|
7457 | /* Make sure the source chain is released early. It could happen
|
---|
7458 | * that we get a deadlock in Appliance::Import when Medium::Close
|
---|
7459 | * is called & the source chain is released at the same time. */
|
---|
7460 | task.mpSourceMediumLockList->Clear();
|
---|
7461 |
|
---|
7462 | return mrc;
|
---|
7463 | }
|
---|
7464 |
|
---|
7465 | /**
|
---|
7466 | * Implementation code for the "delete" task.
|
---|
7467 | *
|
---|
7468 | * This task always gets started from Medium::deleteStorage() and can run
|
---|
7469 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
7470 | * that function.
|
---|
7471 | *
|
---|
7472 | * @param task
|
---|
7473 | * @return
|
---|
7474 | */
|
---|
7475 | HRESULT Medium::taskDeleteHandler(Medium::DeleteTask &task)
|
---|
7476 | {
|
---|
7477 | NOREF(task);
|
---|
7478 | HRESULT rc = S_OK;
|
---|
7479 |
|
---|
7480 | try
|
---|
7481 | {
|
---|
7482 | /* The lock is also used as a signal from the task initiator (which
|
---|
7483 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
7484 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7485 |
|
---|
7486 | PVBOXHDD hdd;
|
---|
7487 | int vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &hdd);
|
---|
7488 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7489 |
|
---|
7490 | Utf8Str format(m->strFormat);
|
---|
7491 | Utf8Str location(m->strLocationFull);
|
---|
7492 |
|
---|
7493 | /* unlock before the potentially lengthy operation */
|
---|
7494 | Assert(m->state == MediumState_Deleting);
|
---|
7495 | thisLock.release();
|
---|
7496 |
|
---|
7497 | try
|
---|
7498 | {
|
---|
7499 | vrc = VDOpen(hdd,
|
---|
7500 | format.c_str(),
|
---|
7501 | location.c_str(),
|
---|
7502 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
7503 | m->vdImageIfaces);
|
---|
7504 | if (RT_SUCCESS(vrc))
|
---|
7505 | vrc = VDClose(hdd, true /* fDelete */);
|
---|
7506 |
|
---|
7507 | if (RT_FAILURE(vrc))
|
---|
7508 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7509 | tr("Could not delete the medium storage unit '%s'%s"),
|
---|
7510 | location.c_str(), vdError(vrc).c_str());
|
---|
7511 |
|
---|
7512 | }
|
---|
7513 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7514 |
|
---|
7515 | VDDestroy(hdd);
|
---|
7516 | }
|
---|
7517 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7518 |
|
---|
7519 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7520 |
|
---|
7521 | /* go to the NotCreated state even on failure since the storage
|
---|
7522 | * may have been already partially deleted and cannot be used any
|
---|
7523 | * more. One will be able to manually re-open the storage if really
|
---|
7524 | * needed to re-register it. */
|
---|
7525 | m->state = MediumState_NotCreated;
|
---|
7526 |
|
---|
7527 | /* Reset UUID to prevent Create* from reusing it again */
|
---|
7528 | unconst(m->id).clear();
|
---|
7529 |
|
---|
7530 | return rc;
|
---|
7531 | }
|
---|
7532 |
|
---|
7533 | /**
|
---|
7534 | * Implementation code for the "reset" task.
|
---|
7535 | *
|
---|
7536 | * This always gets started asynchronously from Medium::Reset().
|
---|
7537 | *
|
---|
7538 | * @param task
|
---|
7539 | * @return
|
---|
7540 | */
|
---|
7541 | HRESULT Medium::taskResetHandler(Medium::ResetTask &task)
|
---|
7542 | {
|
---|
7543 | HRESULT rc = S_OK;
|
---|
7544 |
|
---|
7545 | uint64_t size = 0, logicalSize = 0;
|
---|
7546 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
7547 |
|
---|
7548 | try
|
---|
7549 | {
|
---|
7550 | /* The lock is also used as a signal from the task initiator (which
|
---|
7551 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
7552 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7553 |
|
---|
7554 | /// @todo Below we use a pair of delete/create operations to reset
|
---|
7555 | /// the diff contents but the most efficient way will of course be
|
---|
7556 | /// to add a VDResetDiff() API call
|
---|
7557 |
|
---|
7558 | PVBOXHDD hdd;
|
---|
7559 | int vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &hdd);
|
---|
7560 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7561 |
|
---|
7562 | Guid id = m->id;
|
---|
7563 | Utf8Str format(m->strFormat);
|
---|
7564 | Utf8Str location(m->strLocationFull);
|
---|
7565 |
|
---|
7566 | Medium *pParent = m->pParent;
|
---|
7567 | Guid parentId = pParent->m->id;
|
---|
7568 | Utf8Str parentFormat(pParent->m->strFormat);
|
---|
7569 | Utf8Str parentLocation(pParent->m->strLocationFull);
|
---|
7570 |
|
---|
7571 | Assert(m->state == MediumState_LockedWrite);
|
---|
7572 |
|
---|
7573 | /* unlock before the potentially lengthy operation */
|
---|
7574 | thisLock.release();
|
---|
7575 |
|
---|
7576 | try
|
---|
7577 | {
|
---|
7578 | /* Open all media in the target chain but the last. */
|
---|
7579 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
7580 | task.mpMediumLockList->GetBegin();
|
---|
7581 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
7582 | task.mpMediumLockList->GetEnd();
|
---|
7583 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
7584 | it != targetListEnd;
|
---|
7585 | ++it)
|
---|
7586 | {
|
---|
7587 | const MediumLock &mediumLock = *it;
|
---|
7588 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
7589 |
|
---|
7590 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
7591 |
|
---|
7592 | /* sanity check, "this" is checked above */
|
---|
7593 | Assert( pMedium == this
|
---|
7594 | || pMedium->m->state == MediumState_LockedRead);
|
---|
7595 |
|
---|
7596 | /* Open all media in appropriate mode. */
|
---|
7597 | vrc = VDOpen(hdd,
|
---|
7598 | pMedium->m->strFormat.c_str(),
|
---|
7599 | pMedium->m->strLocationFull.c_str(),
|
---|
7600 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
7601 | pMedium->m->vdImageIfaces);
|
---|
7602 | if (RT_FAILURE(vrc))
|
---|
7603 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7604 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
7605 | pMedium->m->strLocationFull.c_str(),
|
---|
7606 | vdError(vrc).c_str());
|
---|
7607 |
|
---|
7608 | /* Done when we hit the media which should be reset */
|
---|
7609 | if (pMedium == this)
|
---|
7610 | break;
|
---|
7611 | }
|
---|
7612 |
|
---|
7613 | /* first, delete the storage unit */
|
---|
7614 | vrc = VDClose(hdd, true /* fDelete */);
|
---|
7615 | if (RT_FAILURE(vrc))
|
---|
7616 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7617 | tr("Could not delete the medium storage unit '%s'%s"),
|
---|
7618 | location.c_str(), vdError(vrc).c_str());
|
---|
7619 |
|
---|
7620 | /* next, create it again */
|
---|
7621 | vrc = VDOpen(hdd,
|
---|
7622 | parentFormat.c_str(),
|
---|
7623 | parentLocation.c_str(),
|
---|
7624 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
7625 | m->vdImageIfaces);
|
---|
7626 | if (RT_FAILURE(vrc))
|
---|
7627 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7628 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
7629 | parentLocation.c_str(), vdError(vrc).c_str());
|
---|
7630 |
|
---|
7631 | vrc = VDCreateDiff(hdd,
|
---|
7632 | format.c_str(),
|
---|
7633 | location.c_str(),
|
---|
7634 | /// @todo use the same medium variant as before
|
---|
7635 | VD_IMAGE_FLAGS_NONE,
|
---|
7636 | NULL,
|
---|
7637 | id.raw(),
|
---|
7638 | parentId.raw(),
|
---|
7639 | VD_OPEN_FLAGS_NORMAL,
|
---|
7640 | m->vdImageIfaces,
|
---|
7641 | task.mVDOperationIfaces);
|
---|
7642 | if (RT_FAILURE(vrc))
|
---|
7643 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7644 | tr("Could not create the differencing medium storage unit '%s'%s"),
|
---|
7645 | location.c_str(), vdError(vrc).c_str());
|
---|
7646 |
|
---|
7647 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
7648 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
7649 | unsigned uImageFlags;
|
---|
7650 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
7651 | if (RT_SUCCESS(vrc))
|
---|
7652 | variant = (MediumVariant_T)uImageFlags;
|
---|
7653 | }
|
---|
7654 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7655 |
|
---|
7656 | VDDestroy(hdd);
|
---|
7657 | }
|
---|
7658 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7659 |
|
---|
7660 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7661 |
|
---|
7662 | m->size = size;
|
---|
7663 | m->logicalSize = logicalSize;
|
---|
7664 | m->variant = variant;
|
---|
7665 |
|
---|
7666 | if (task.isAsync())
|
---|
7667 | {
|
---|
7668 | /* unlock ourselves when done */
|
---|
7669 | HRESULT rc2 = UnlockWrite(NULL);
|
---|
7670 | AssertComRC(rc2);
|
---|
7671 | }
|
---|
7672 |
|
---|
7673 | /* Note that in sync mode, it's the caller's responsibility to
|
---|
7674 | * unlock the medium. */
|
---|
7675 |
|
---|
7676 | return rc;
|
---|
7677 | }
|
---|
7678 |
|
---|
7679 | /**
|
---|
7680 | * Implementation code for the "compact" task.
|
---|
7681 | *
|
---|
7682 | * @param task
|
---|
7683 | * @return
|
---|
7684 | */
|
---|
7685 | HRESULT Medium::taskCompactHandler(Medium::CompactTask &task)
|
---|
7686 | {
|
---|
7687 | HRESULT rc = S_OK;
|
---|
7688 |
|
---|
7689 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
7690 | * signal from the task initiator (which releases it only after
|
---|
7691 | * RTThreadCreate()) that we can start the job. */
|
---|
7692 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7693 |
|
---|
7694 | try
|
---|
7695 | {
|
---|
7696 | PVBOXHDD hdd;
|
---|
7697 | int vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &hdd);
|
---|
7698 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7699 |
|
---|
7700 | try
|
---|
7701 | {
|
---|
7702 | /* Open all media in the chain. */
|
---|
7703 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
7704 | task.mpMediumLockList->GetBegin();
|
---|
7705 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
7706 | task.mpMediumLockList->GetEnd();
|
---|
7707 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
7708 | mediumListEnd;
|
---|
7709 | mediumListLast--;
|
---|
7710 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
7711 | it != mediumListEnd;
|
---|
7712 | ++it)
|
---|
7713 | {
|
---|
7714 | const MediumLock &mediumLock = *it;
|
---|
7715 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
7716 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
7717 |
|
---|
7718 | /* sanity check */
|
---|
7719 | if (it == mediumListLast)
|
---|
7720 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
7721 | else
|
---|
7722 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
7723 |
|
---|
7724 | /* Open all media but last in read-only mode. Do not handle
|
---|
7725 | * shareable media, as compaction and sharing are mutually
|
---|
7726 | * exclusive. */
|
---|
7727 | vrc = VDOpen(hdd,
|
---|
7728 | pMedium->m->strFormat.c_str(),
|
---|
7729 | pMedium->m->strLocationFull.c_str(),
|
---|
7730 | m->uOpenFlagsDef | (it == mediumListLast) ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY,
|
---|
7731 | pMedium->m->vdImageIfaces);
|
---|
7732 | if (RT_FAILURE(vrc))
|
---|
7733 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7734 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
7735 | pMedium->m->strLocationFull.c_str(),
|
---|
7736 | vdError(vrc).c_str());
|
---|
7737 | }
|
---|
7738 |
|
---|
7739 | Assert(m->state == MediumState_LockedWrite);
|
---|
7740 |
|
---|
7741 | Utf8Str location(m->strLocationFull);
|
---|
7742 |
|
---|
7743 | /* unlock before the potentially lengthy operation */
|
---|
7744 | thisLock.release();
|
---|
7745 |
|
---|
7746 | vrc = VDCompact(hdd, VD_LAST_IMAGE, task.mVDOperationIfaces);
|
---|
7747 | if (RT_FAILURE(vrc))
|
---|
7748 | {
|
---|
7749 | if (vrc == VERR_NOT_SUPPORTED)
|
---|
7750 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
7751 | tr("Compacting is not yet supported for medium '%s'"),
|
---|
7752 | location.c_str());
|
---|
7753 | else if (vrc == VERR_NOT_IMPLEMENTED)
|
---|
7754 | throw setError(E_NOTIMPL,
|
---|
7755 | tr("Compacting is not implemented, medium '%s'"),
|
---|
7756 | location.c_str());
|
---|
7757 | else
|
---|
7758 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7759 | tr("Could not compact medium '%s'%s"),
|
---|
7760 | location.c_str(),
|
---|
7761 | vdError(vrc).c_str());
|
---|
7762 | }
|
---|
7763 | }
|
---|
7764 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7765 |
|
---|
7766 | VDDestroy(hdd);
|
---|
7767 | }
|
---|
7768 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7769 |
|
---|
7770 | /* Everything is explicitly unlocked when the task exits,
|
---|
7771 | * as the task destruction also destroys the media chain. */
|
---|
7772 |
|
---|
7773 | return rc;
|
---|
7774 | }
|
---|
7775 |
|
---|
7776 | /**
|
---|
7777 | * Implementation code for the "resize" task.
|
---|
7778 | *
|
---|
7779 | * @param task
|
---|
7780 | * @return
|
---|
7781 | */
|
---|
7782 | HRESULT Medium::taskResizeHandler(Medium::ResizeTask &task)
|
---|
7783 | {
|
---|
7784 | HRESULT rc = S_OK;
|
---|
7785 |
|
---|
7786 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
7787 | * signal from the task initiator (which releases it only after
|
---|
7788 | * RTThreadCreate()) that we can start the job. */
|
---|
7789 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7790 |
|
---|
7791 | try
|
---|
7792 | {
|
---|
7793 | PVBOXHDD hdd;
|
---|
7794 | int vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &hdd);
|
---|
7795 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7796 |
|
---|
7797 | try
|
---|
7798 | {
|
---|
7799 | /* Open all media in the chain. */
|
---|
7800 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
7801 | task.mpMediumLockList->GetBegin();
|
---|
7802 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
7803 | task.mpMediumLockList->GetEnd();
|
---|
7804 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
7805 | mediumListEnd;
|
---|
7806 | mediumListLast--;
|
---|
7807 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
7808 | it != mediumListEnd;
|
---|
7809 | ++it)
|
---|
7810 | {
|
---|
7811 | const MediumLock &mediumLock = *it;
|
---|
7812 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
7813 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
7814 |
|
---|
7815 | /* sanity check */
|
---|
7816 | if (it == mediumListLast)
|
---|
7817 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
7818 | else
|
---|
7819 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
7820 |
|
---|
7821 | /* Open all media but last in read-only mode. Do not handle
|
---|
7822 | * shareable media, as compaction and sharing are mutually
|
---|
7823 | * exclusive. */
|
---|
7824 | vrc = VDOpen(hdd,
|
---|
7825 | pMedium->m->strFormat.c_str(),
|
---|
7826 | pMedium->m->strLocationFull.c_str(),
|
---|
7827 | m->uOpenFlagsDef | (it == mediumListLast) ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY,
|
---|
7828 | pMedium->m->vdImageIfaces);
|
---|
7829 | if (RT_FAILURE(vrc))
|
---|
7830 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7831 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
7832 | pMedium->m->strLocationFull.c_str(),
|
---|
7833 | vdError(vrc).c_str());
|
---|
7834 | }
|
---|
7835 |
|
---|
7836 | Assert(m->state == MediumState_LockedWrite);
|
---|
7837 |
|
---|
7838 | Utf8Str location(m->strLocationFull);
|
---|
7839 |
|
---|
7840 | /* unlock before the potentially lengthy operation */
|
---|
7841 | thisLock.release();
|
---|
7842 |
|
---|
7843 | VDGEOMETRY geo = {0, 0, 0}; /* auto */
|
---|
7844 | vrc = VDResize(hdd, task.mSize, &geo, &geo, task.mVDOperationIfaces);
|
---|
7845 | if (RT_FAILURE(vrc))
|
---|
7846 | {
|
---|
7847 | if (vrc == VERR_NOT_SUPPORTED)
|
---|
7848 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
7849 | tr("Resizing to new size %llu is not yet supported for medium '%s'"),
|
---|
7850 | task.mSize, location.c_str());
|
---|
7851 | else if (vrc == VERR_NOT_IMPLEMENTED)
|
---|
7852 | throw setError(E_NOTIMPL,
|
---|
7853 | tr("Resiting is not implemented, medium '%s'"),
|
---|
7854 | location.c_str());
|
---|
7855 | else
|
---|
7856 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7857 | tr("Could not resize medium '%s'%s"),
|
---|
7858 | location.c_str(),
|
---|
7859 | vdError(vrc).c_str());
|
---|
7860 | }
|
---|
7861 | }
|
---|
7862 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7863 |
|
---|
7864 | VDDestroy(hdd);
|
---|
7865 | }
|
---|
7866 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7867 |
|
---|
7868 | /* Everything is explicitly unlocked when the task exits,
|
---|
7869 | * as the task destruction also destroys the media chain. */
|
---|
7870 |
|
---|
7871 | return rc;
|
---|
7872 | }
|
---|
7873 |
|
---|
7874 | /**
|
---|
7875 | * Implementation code for the "export" task.
|
---|
7876 | *
|
---|
7877 | * This only gets started from Medium::exportFile() and always runs
|
---|
7878 | * asynchronously. It doesn't touch anything configuration related, so
|
---|
7879 | * we never save the VirtualBox.xml file here.
|
---|
7880 | *
|
---|
7881 | * @param task
|
---|
7882 | * @return
|
---|
7883 | */
|
---|
7884 | HRESULT Medium::taskExportHandler(Medium::ExportTask &task)
|
---|
7885 | {
|
---|
7886 | HRESULT rc = S_OK;
|
---|
7887 |
|
---|
7888 | try
|
---|
7889 | {
|
---|
7890 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
7891 | * signal from the task initiator (which releases it only after
|
---|
7892 | * RTThreadCreate()) that we can start the job. */
|
---|
7893 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7894 |
|
---|
7895 | PVBOXHDD hdd;
|
---|
7896 | int vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &hdd);
|
---|
7897 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7898 |
|
---|
7899 | try
|
---|
7900 | {
|
---|
7901 | /* Open all media in the source chain. */
|
---|
7902 | MediumLockList::Base::const_iterator sourceListBegin =
|
---|
7903 | task.mpSourceMediumLockList->GetBegin();
|
---|
7904 | MediumLockList::Base::const_iterator sourceListEnd =
|
---|
7905 | task.mpSourceMediumLockList->GetEnd();
|
---|
7906 | for (MediumLockList::Base::const_iterator it = sourceListBegin;
|
---|
7907 | it != sourceListEnd;
|
---|
7908 | ++it)
|
---|
7909 | {
|
---|
7910 | const MediumLock &mediumLock = *it;
|
---|
7911 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
7912 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
7913 |
|
---|
7914 | /* sanity check */
|
---|
7915 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
7916 |
|
---|
7917 | /* Open all media in read-only mode. */
|
---|
7918 | vrc = VDOpen(hdd,
|
---|
7919 | pMedium->m->strFormat.c_str(),
|
---|
7920 | pMedium->m->strLocationFull.c_str(),
|
---|
7921 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
7922 | pMedium->m->vdImageIfaces);
|
---|
7923 | if (RT_FAILURE(vrc))
|
---|
7924 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7925 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
7926 | pMedium->m->strLocationFull.c_str(),
|
---|
7927 | vdError(vrc).c_str());
|
---|
7928 | }
|
---|
7929 |
|
---|
7930 | Utf8Str targetFormat(task.mFormat->getId());
|
---|
7931 | Utf8Str targetLocation(task.mFilename);
|
---|
7932 | uint64_t capabilities = task.mFormat->getCapabilities();
|
---|
7933 |
|
---|
7934 | Assert(m->state == MediumState_LockedRead);
|
---|
7935 |
|
---|
7936 | /* unlock before the potentially lengthy operation */
|
---|
7937 | thisLock.release();
|
---|
7938 |
|
---|
7939 | /* ensure the target directory exists */
|
---|
7940 | if (capabilities & MediumFormatCapabilities_File)
|
---|
7941 | {
|
---|
7942 | rc = VirtualBox::ensureFilePathExists(targetLocation, !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
7943 | if (FAILED(rc))
|
---|
7944 | throw rc;
|
---|
7945 | }
|
---|
7946 |
|
---|
7947 | PVBOXHDD targetHdd;
|
---|
7948 | vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &targetHdd);
|
---|
7949 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7950 |
|
---|
7951 | try
|
---|
7952 | {
|
---|
7953 | vrc = VDCopy(hdd,
|
---|
7954 | VD_LAST_IMAGE,
|
---|
7955 | targetHdd,
|
---|
7956 | targetFormat.c_str(),
|
---|
7957 | targetLocation.c_str(),
|
---|
7958 | false /* fMoveByRename */,
|
---|
7959 | 0 /* cbSize */,
|
---|
7960 | task.mVariant & ~MediumVariant_NoCreateDir,
|
---|
7961 | NULL /* pDstUuid */,
|
---|
7962 | VD_OPEN_FLAGS_NORMAL | VD_OPEN_FLAGS_SEQUENTIAL,
|
---|
7963 | NULL /* pVDIfsOperation */,
|
---|
7964 | task.mVDImageIfaces,
|
---|
7965 | task.mVDOperationIfaces);
|
---|
7966 | if (RT_FAILURE(vrc))
|
---|
7967 | throw setError(VBOX_E_FILE_ERROR,
|
---|
7968 | tr("Could not create the clone medium '%s'%s"),
|
---|
7969 | targetLocation.c_str(), vdError(vrc).c_str());
|
---|
7970 | }
|
---|
7971 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7972 |
|
---|
7973 | VDDestroy(targetHdd);
|
---|
7974 | }
|
---|
7975 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7976 |
|
---|
7977 | VDDestroy(hdd);
|
---|
7978 | }
|
---|
7979 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7980 |
|
---|
7981 | /* Everything is explicitly unlocked when the task exits,
|
---|
7982 | * as the task destruction also destroys the source chain. */
|
---|
7983 |
|
---|
7984 | /* Make sure the source chain is released early, otherwise it can
|
---|
7985 | * lead to deadlocks with concurrent IAppliance activities. */
|
---|
7986 | task.mpSourceMediumLockList->Clear();
|
---|
7987 |
|
---|
7988 | return rc;
|
---|
7989 | }
|
---|
7990 |
|
---|
7991 | /**
|
---|
7992 | * Implementation code for the "import" task.
|
---|
7993 | *
|
---|
7994 | * This only gets started from Medium::importFile() and always runs
|
---|
7995 | * asynchronously. It potentially touches the media registry, so we
|
---|
7996 | * always save the VirtualBox.xml file when we're done here.
|
---|
7997 | *
|
---|
7998 | * @param task
|
---|
7999 | * @return
|
---|
8000 | */
|
---|
8001 | HRESULT Medium::taskImportHandler(Medium::ImportTask &task)
|
---|
8002 | {
|
---|
8003 | HRESULT rcTmp = S_OK;
|
---|
8004 |
|
---|
8005 | const ComObjPtr<Medium> &pParent = task.mParent;
|
---|
8006 |
|
---|
8007 | bool fCreatingTarget = false;
|
---|
8008 |
|
---|
8009 | uint64_t size = 0, logicalSize = 0;
|
---|
8010 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
8011 | bool fGenerateUuid = false;
|
---|
8012 |
|
---|
8013 | try
|
---|
8014 | {
|
---|
8015 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
8016 | * signal from the task initiator (which releases it only after
|
---|
8017 | * RTThreadCreate()) that we can start the job. */
|
---|
8018 | AutoMultiWriteLock2 thisLock(this, pParent COMMA_LOCKVAL_SRC_POS);
|
---|
8019 |
|
---|
8020 | fCreatingTarget = m->state == MediumState_Creating;
|
---|
8021 |
|
---|
8022 | /* The object may request a specific UUID (through a special form of
|
---|
8023 | * the setLocation() argument). Otherwise we have to generate it */
|
---|
8024 | Guid targetId = m->id;
|
---|
8025 | fGenerateUuid = targetId.isEmpty();
|
---|
8026 | if (fGenerateUuid)
|
---|
8027 | {
|
---|
8028 | targetId.create();
|
---|
8029 | /* VirtualBox::registerMedium() will need UUID */
|
---|
8030 | unconst(m->id) = targetId;
|
---|
8031 | }
|
---|
8032 |
|
---|
8033 |
|
---|
8034 | PVBOXHDD hdd;
|
---|
8035 | int vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &hdd);
|
---|
8036 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8037 |
|
---|
8038 | try
|
---|
8039 | {
|
---|
8040 | /* Open source medium. */
|
---|
8041 | vrc = VDOpen(hdd,
|
---|
8042 | task.mFormat->getId().c_str(),
|
---|
8043 | task.mFilename.c_str(),
|
---|
8044 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SEQUENTIAL | m->uOpenFlagsDef,
|
---|
8045 | task.mVDImageIfaces);
|
---|
8046 | if (RT_FAILURE(vrc))
|
---|
8047 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8048 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8049 | task.mFilename.c_str(),
|
---|
8050 | vdError(vrc).c_str());
|
---|
8051 |
|
---|
8052 | Utf8Str targetFormat(m->strFormat);
|
---|
8053 | Utf8Str targetLocation(m->strLocationFull);
|
---|
8054 | uint64_t capabilities = task.mFormat->getCapabilities();
|
---|
8055 |
|
---|
8056 | Assert( m->state == MediumState_Creating
|
---|
8057 | || m->state == MediumState_LockedWrite);
|
---|
8058 | Assert( pParent.isNull()
|
---|
8059 | || pParent->m->state == MediumState_LockedRead);
|
---|
8060 |
|
---|
8061 | /* unlock before the potentially lengthy operation */
|
---|
8062 | thisLock.release();
|
---|
8063 |
|
---|
8064 | /* ensure the target directory exists */
|
---|
8065 | if (capabilities & MediumFormatCapabilities_File)
|
---|
8066 | {
|
---|
8067 | HRESULT rc = VirtualBox::ensureFilePathExists(targetLocation, !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
8068 | if (FAILED(rc))
|
---|
8069 | throw rc;
|
---|
8070 | }
|
---|
8071 |
|
---|
8072 | PVBOXHDD targetHdd;
|
---|
8073 | vrc = VDCreate(m->vdDiskIfaces, convertDeviceType(), &targetHdd);
|
---|
8074 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8075 |
|
---|
8076 | try
|
---|
8077 | {
|
---|
8078 | /* Open all media in the target chain. */
|
---|
8079 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
8080 | task.mpTargetMediumLockList->GetBegin();
|
---|
8081 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
8082 | task.mpTargetMediumLockList->GetEnd();
|
---|
8083 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
8084 | it != targetListEnd;
|
---|
8085 | ++it)
|
---|
8086 | {
|
---|
8087 | const MediumLock &mediumLock = *it;
|
---|
8088 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8089 |
|
---|
8090 | /* If the target medium is not created yet there's no
|
---|
8091 | * reason to open it. */
|
---|
8092 | if (pMedium == this && fCreatingTarget)
|
---|
8093 | continue;
|
---|
8094 |
|
---|
8095 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8096 |
|
---|
8097 | /* sanity check */
|
---|
8098 | Assert( pMedium->m->state == MediumState_LockedRead
|
---|
8099 | || pMedium->m->state == MediumState_LockedWrite);
|
---|
8100 |
|
---|
8101 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
8102 | if (pMedium->m->state != MediumState_LockedWrite)
|
---|
8103 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
8104 | if (pMedium->m->type == MediumType_Shareable)
|
---|
8105 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
8106 |
|
---|
8107 | /* Open all media in appropriate mode. */
|
---|
8108 | vrc = VDOpen(targetHdd,
|
---|
8109 | pMedium->m->strFormat.c_str(),
|
---|
8110 | pMedium->m->strLocationFull.c_str(),
|
---|
8111 | uOpenFlags | m->uOpenFlagsDef,
|
---|
8112 | pMedium->m->vdImageIfaces);
|
---|
8113 | if (RT_FAILURE(vrc))
|
---|
8114 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8115 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8116 | pMedium->m->strLocationFull.c_str(),
|
---|
8117 | vdError(vrc).c_str());
|
---|
8118 | }
|
---|
8119 |
|
---|
8120 | /** @todo r=klaus target isn't locked, race getting the state */
|
---|
8121 | vrc = VDCopy(hdd,
|
---|
8122 | VD_LAST_IMAGE,
|
---|
8123 | targetHdd,
|
---|
8124 | targetFormat.c_str(),
|
---|
8125 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
8126 | false /* fMoveByRename */,
|
---|
8127 | 0 /* cbSize */,
|
---|
8128 | task.mVariant & ~MediumVariant_NoCreateDir,
|
---|
8129 | targetId.raw(),
|
---|
8130 | VD_OPEN_FLAGS_NORMAL,
|
---|
8131 | NULL /* pVDIfsOperation */,
|
---|
8132 | m->vdImageIfaces,
|
---|
8133 | task.mVDOperationIfaces);
|
---|
8134 | if (RT_FAILURE(vrc))
|
---|
8135 | throw setError(VBOX_E_FILE_ERROR,
|
---|
8136 | tr("Could not create the clone medium '%s'%s"),
|
---|
8137 | targetLocation.c_str(), vdError(vrc).c_str());
|
---|
8138 |
|
---|
8139 | size = VDGetFileSize(targetHdd, VD_LAST_IMAGE);
|
---|
8140 | logicalSize = VDGetSize(targetHdd, VD_LAST_IMAGE);
|
---|
8141 | unsigned uImageFlags;
|
---|
8142 | vrc = VDGetImageFlags(targetHdd, 0, &uImageFlags);
|
---|
8143 | if (RT_SUCCESS(vrc))
|
---|
8144 | variant = (MediumVariant_T)uImageFlags;
|
---|
8145 | }
|
---|
8146 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8147 |
|
---|
8148 | VDDestroy(targetHdd);
|
---|
8149 | }
|
---|
8150 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8151 |
|
---|
8152 | VDDestroy(hdd);
|
---|
8153 | }
|
---|
8154 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
8155 |
|
---|
8156 | ErrorInfoKeeper eik;
|
---|
8157 | MultiResult mrc(rcTmp);
|
---|
8158 |
|
---|
8159 | /* Only do the parent changes for newly created media. */
|
---|
8160 | if (SUCCEEDED(mrc) && fCreatingTarget)
|
---|
8161 | {
|
---|
8162 | /* we set mParent & children() */
|
---|
8163 | AutoWriteLock alock2(m->pVirtualBox->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
8164 |
|
---|
8165 | Assert(m->pParent.isNull());
|
---|
8166 |
|
---|
8167 | if (pParent)
|
---|
8168 | {
|
---|
8169 | /* associate the clone with the parent and deassociate
|
---|
8170 | * from VirtualBox */
|
---|
8171 | m->pParent = pParent;
|
---|
8172 | pParent->m->llChildren.push_back(this);
|
---|
8173 |
|
---|
8174 | /* register with mVirtualBox as the last step and move to
|
---|
8175 | * Created state only on success (leaving an orphan file is
|
---|
8176 | * better than breaking media registry consistency) */
|
---|
8177 | eik.restore();
|
---|
8178 | ComObjPtr<Medium> pMedium;
|
---|
8179 | mrc = pParent->m->pVirtualBox->registerMedium(this, &pMedium,
|
---|
8180 | DeviceType_HardDisk);
|
---|
8181 | Assert(this == pMedium);
|
---|
8182 | eik.fetch();
|
---|
8183 |
|
---|
8184 | if (FAILED(mrc))
|
---|
8185 | /* break parent association on failure to register */
|
---|
8186 | this->deparent(); // removes target from parent
|
---|
8187 | }
|
---|
8188 | else
|
---|
8189 | {
|
---|
8190 | /* just register */
|
---|
8191 | eik.restore();
|
---|
8192 | ComObjPtr<Medium> pMedium;
|
---|
8193 | mrc = m->pVirtualBox->registerMedium(this, &pMedium, DeviceType_HardDisk);
|
---|
8194 | Assert(this == pMedium);
|
---|
8195 | eik.fetch();
|
---|
8196 | }
|
---|
8197 | }
|
---|
8198 |
|
---|
8199 | if (fCreatingTarget)
|
---|
8200 | {
|
---|
8201 | AutoWriteLock mLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8202 |
|
---|
8203 | if (SUCCEEDED(mrc))
|
---|
8204 | {
|
---|
8205 | m->state = MediumState_Created;
|
---|
8206 |
|
---|
8207 | m->size = size;
|
---|
8208 | m->logicalSize = logicalSize;
|
---|
8209 | m->variant = variant;
|
---|
8210 | }
|
---|
8211 | else
|
---|
8212 | {
|
---|
8213 | /* back to NotCreated on failure */
|
---|
8214 | m->state = MediumState_NotCreated;
|
---|
8215 |
|
---|
8216 | /* reset UUID to prevent it from being reused next time */
|
---|
8217 | if (fGenerateUuid)
|
---|
8218 | unconst(m->id).clear();
|
---|
8219 | }
|
---|
8220 | }
|
---|
8221 |
|
---|
8222 | // now, at the end of this task (always asynchronous), save the settings
|
---|
8223 | {
|
---|
8224 | // save the settings
|
---|
8225 | markRegistriesModified();
|
---|
8226 | /* collect multiple errors */
|
---|
8227 | eik.restore();
|
---|
8228 | m->pVirtualBox->saveModifiedRegistries();
|
---|
8229 | eik.fetch();
|
---|
8230 | }
|
---|
8231 |
|
---|
8232 | /* Everything is explicitly unlocked when the task exits,
|
---|
8233 | * as the task destruction also destroys the target chain. */
|
---|
8234 |
|
---|
8235 | /* Make sure the target chain is released early, otherwise it can
|
---|
8236 | * lead to deadlocks with concurrent IAppliance activities. */
|
---|
8237 | task.mpTargetMediumLockList->Clear();
|
---|
8238 |
|
---|
8239 | return mrc;
|
---|
8240 | }
|
---|
8241 |
|
---|
8242 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|