VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/MediumImpl.cpp@ 54053

Last change on this file since 54053 was 53739, checked in by vboxsync, 10 years ago

Main: Fix race condition between Medium::Close and Medium::i_queryInfo which can result in corrupted parent UUIDs of images or errors when closing an image

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette