VirtualBox

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

Last change on this file since 53517 was 53354, checked in by vboxsync, 10 years ago

R7524 - needs testing in VBoxManage.

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