VirtualBox

source: vbox/trunk/src/VBox/Main/include/HardDiskImpl.h@ 2106

Last change on this file since 2106 was 1874, checked in by vboxsync, 18 years ago

Main: Fixed: Simultaneous usage of immutable VDIs by more than one running VM was not possible.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.3 KB
Line 
1/** @file
2 *
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22#ifndef ____H_HARDDISKIMPL
23#define ____H_HARDDISKIMPL
24
25#include "VirtualBoxBase.h"
26#include "Collection.h"
27
28#include <VBox/cfgldr.h>
29
30#include <iprt/semaphore.h>
31
32#include <list>
33
34class VirtualBox;
35class Progress;
36class HVirtualDiskImage;
37
38////////////////////////////////////////////////////////////////////////////////
39
40class ATL_NO_VTABLE HardDisk :
41 public VirtualBoxSupportErrorInfoImpl <HardDisk, IHardDisk>,
42 public VirtualBoxSupportTranslation <HardDisk>,
43 public VirtualBoxBaseWithTypedChildren <HardDisk>,
44 public IHardDisk
45{
46
47public:
48
49 typedef VirtualBoxBaseWithTypedChildren <HardDisk>::DependentChildren
50 HardDiskList;
51
52 DECLARE_NOT_AGGREGATABLE(HardDisk)
53
54 DECLARE_PROTECT_FINAL_CONSTRUCT()
55
56 BEGIN_COM_MAP(HardDisk)
57 COM_INTERFACE_ENTRY(ISupportErrorInfo)
58 COM_INTERFACE_ENTRY(IHardDisk)
59 END_COM_MAP()
60
61 NS_DECL_ISUPPORTS
62
63 HRESULT FinalConstruct();
64 void FinalRelease();
65
66protected:
67
68 // protected initializer/uninitializer for internal purposes only
69 HRESULT protectedInit (VirtualBox *aVirtualBox, HardDisk *aParent);
70 void protectedUninit (AutoLock &alock);
71
72public:
73
74 // IHardDisk properties
75 STDMETHOD(COMGETTER(Id)) (GUIDPARAMOUT aId);
76 STDMETHOD(COMGETTER(StorageType)) (HardDiskStorageType_T *aStorageType);
77 STDMETHOD(COMGETTER(Location)) (BSTR *aLocation);
78 STDMETHOD(COMGETTER(Type)) (HardDiskType_T *aType);
79 STDMETHOD(COMSETTER(Type)) (HardDiskType_T aType);
80 STDMETHOD(COMGETTER(Parent)) (IHardDisk **aParent);
81 STDMETHOD(COMGETTER(Children)) (IHardDiskCollection **aChildren);
82 STDMETHOD(COMGETTER(Root)) (IHardDisk **aRoot);
83 STDMETHOD(COMGETTER(Accessible)) (BOOL *aAccessible);
84 STDMETHOD(COMGETTER(AllAccessible)) (BOOL *aAllAccessible);
85 STDMETHOD(COMGETTER(LastAccessError)) (BSTR *aLastAccessError);
86 STDMETHOD(COMGETTER(MachineId)) (GUIDPARAMOUT aMachineId);
87 STDMETHOD(COMGETTER(SnapshotId)) (GUIDPARAMOUT aSnapshotId);
88
89 // IHardDisk methods
90 STDMETHOD(CloneToImage) (INPTR BSTR aFilePath, IVirtualDiskImage **aImage,
91 IProgress **aProgress);
92
93 // public methods for internal purposes only
94
95 const Guid &id() const { return mId; }
96 HardDiskStorageType_T storageType() const { return mStorageType; }
97 HardDiskType_T type() const { return mType; }
98 const Guid &machineId() const { return mMachineId; }
99 const Guid &snapshotId() const { return mSnapshotId; }
100
101 void setMachineId (const Guid &aId) { mMachineId = aId; }
102 void setSnapshotId (const Guid &aId) { mSnapshotId = aId; }
103
104 bool isDifferencing() const
105 {
106 return mType == HardDiskType_NormalHardDisk &&
107 mStorageType == HardDiskStorageType_VirtualDiskImage &&
108 !mParent.isNull();
109 }
110 bool isParentImmutable() const
111 {
112 AutoLock parentLock (mParent);
113 return !mParent.isNull() && mParent->type() == HardDiskType_ImmutableHardDisk;
114 }
115
116 inline HVirtualDiskImage *asVDI();
117
118 ComObjPtr <HardDisk> parent() const { return static_cast <HardDisk *> (mParent); }
119
120 /** Shortcut to #dependentChildrenLock() */
121 AutoLock::Handle &childrenLock() const { return dependentChildrenLock(); }
122
123 /**
124 * Shortcut to #dependentChildren().
125 * Do |AutoLock alock (childrenLock());| before acceessing the returned list!
126 */
127 const HardDiskList &children() const { return dependentChildren(); }
128
129 ComObjPtr <HardDisk> root() const;
130
131 HRESULT getBaseAccessible (Bstr &aAccessError, bool aCheckBusy = false,
132 bool aCheckReaders = false);
133
134 // virtual methods that need to be [re]implemented by every subclass
135
136 virtual HRESULT trySetRegistered (BOOL aRegistered);
137 virtual HRESULT getAccessible (Bstr &aAccessError) = 0;
138
139 virtual HRESULT saveSettings (CFGNODE aHDNode, CFGNODE aStorageNode) = 0;
140
141 virtual void updatePath (const char *aOldPath, const char *aNewPath) {}
142
143 virtual Bstr toString (bool aShort = false) = 0;
144 virtual bool sameAs (HardDisk *that);
145
146 virtual HRESULT cloneToImage (const Guid &aId, const Utf8Str &aTargetPath,
147 Progress *aProgress) = 0;
148 virtual HRESULT createDiffImage (const Guid &aId, const Utf8Str &aTargetPath,
149 Progress *aProgress) = 0;
150public:
151
152 void setBusy();
153 void clearBusy();
154 void addReader();
155 void releaseReader();
156 void addReaderOnAncestors();
157 void releaseReaderOnAncestors();
158 bool hasForeignChildren();
159
160 HRESULT setBusyWithChildren();
161 void clearBusyWithChildren();
162 HRESULT getAccessibleWithChildren (Bstr &aAccessError);
163
164 HRESULT checkConsistency();
165
166 HRESULT createDiffHardDisk (const Bstr &aFolder, const Guid &aMachineId,
167 ComObjPtr <HVirtualDiskImage> &aHardDisk,
168 Progress *aProgress);
169
170 void updatePaths (const char *aOldPath, const char *aNewPath);
171
172 /* the following must be called from under the lock */
173 bool isBusy() { isLockedOnCurrentThread(); return mBusy; }
174 unsigned readers() { isLockedOnCurrentThread(); return mReaders; }
175 const Bstr &lastAccessError() const { return mLastAccessError; }
176
177 static HRESULT openHardDisk (VirtualBox *aVirtualBox, INPTR BSTR aLocation,
178 ComObjPtr <HardDisk> &hardDisk);
179
180 // for VirtualBoxSupportErrorInfoImpl
181 static const wchar_t *getComponentName() { return L"HardDisk"; }
182
183protected:
184
185 HRESULT loadSettings (CFGNODE aHDNode);
186 HRESULT saveSettings (CFGNODE aHDNode);
187
188 /** weak VirualBox parent */
189 ComObjPtr <VirtualBox, ComWeakRef> mVirtualBox;
190
191 BOOL mRegistered;
192
193 ComObjPtr <HardDisk, ComWeakRef> mParent;
194
195 Guid mId;
196 HardDiskStorageType_T mStorageType;
197 HardDiskType_T mType;
198 Guid mMachineId;
199 Guid mSnapshotId;
200
201private:
202
203 Bstr mLastAccessError;
204
205 bool mBusy;
206 unsigned mReaders;
207};
208
209////////////////////////////////////////////////////////////////////////////////
210
211class ATL_NO_VTABLE HVirtualDiskImage :
212 public HardDisk,
213 public VirtualBoxSupportTranslation <HVirtualDiskImage>,
214 public IVirtualDiskImage
215{
216
217public:
218
219 VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE(HVirtualDiskImage)
220
221 DECLARE_NOT_AGGREGATABLE(HVirtualDiskImage)
222
223 DECLARE_PROTECT_FINAL_CONSTRUCT()
224
225 BEGIN_COM_MAP(HVirtualDiskImage)
226 COM_INTERFACE_ENTRY(ISupportErrorInfo)
227 COM_INTERFACE_ENTRY(IHardDisk)
228 COM_INTERFACE_ENTRY(IVirtualDiskImage)
229 END_COM_MAP()
230
231 NS_DECL_ISUPPORTS
232
233 HRESULT FinalConstruct();
234 void FinalRelease();
235
236 // public initializer/uninitializer for internal purposes only
237
238 HRESULT init (VirtualBox *aVirtualBox, HardDisk *aParent,
239 CFGNODE aHDNode, CFGNODE aVDINode);
240 HRESULT init (VirtualBox *aVirtualBox, HardDisk *aParent,
241 const BSTR aFilePath, BOOL aRegistered = FALSE);
242 void uninit();
243
244 // IHardDisk properties
245 STDMETHOD(COMGETTER(Description)) (BSTR *aDescription);
246 STDMETHOD(COMSETTER(Description)) (INPTR BSTR aDescription);
247 STDMETHOD(COMGETTER(Size)) (ULONG64 *aSize);
248 STDMETHOD(COMGETTER(ActualSize)) (ULONG64 *aActualSize);
249
250 // IVirtualDiskImage properties
251 STDMETHOD(COMGETTER(FilePath)) (BSTR *aFilePath);
252 STDMETHOD(COMSETTER(FilePath)) (INPTR BSTR aFilePath);
253 STDMETHOD(COMGETTER(Created)) (BOOL *aCreated);
254
255 // IVirtualDiskImage methods
256 STDMETHOD(CreateDynamicImage) (ULONG64 aSize, IProgress **aProgress);
257 STDMETHOD(CreateFixedImage) (ULONG64 aSize, IProgress **aProgress);
258 STDMETHOD(DeleteImage)();
259
260 // public methods for internal purposes only
261
262 const Bstr &filePath() const { return mFilePath; }
263 const Bstr &filePathFull() const { return mFilePathFull; }
264
265 HRESULT trySetRegistered (BOOL aRegistered);
266 HRESULT getAccessible (Bstr &aAccessError);
267
268 HRESULT saveSettings (CFGNODE aHDNode, CFGNODE aStorageNode);
269
270 void updatePath (const char *aOldPath, const char *aNewPath);
271
272 Bstr toString (bool aShort = false);
273
274 HRESULT cloneToImage (const Guid &aId, const Utf8Str &aTargetPath,
275 Progress *aProgress);
276 HRESULT createDiffImage (const Guid &aId, const Utf8Str &aTargetPath,
277 Progress *aProgress);
278
279 HRESULT cloneDiffImage (const Bstr &aFolder, const Guid &aMachineId,
280 ComObjPtr <HVirtualDiskImage> &aHardDisk,
281 Progress *aProgress);
282
283 HRESULT mergeImageToParent (Progress *aProgress);
284 HRESULT mergeImageToChildren (Progress *aProgress);
285
286 HRESULT wipeOutImage();
287
288 // for VirtualBoxSupportErrorInfoImpl
289 static const wchar_t *getComponentName() { return L"VirtualDiskImage"; }
290
291private:
292
293 HRESULT setFilePath (const BSTR aFilePath);
294 HRESULT queryInformation (Bstr *aAccessError);
295 HRESULT createImage (ULONG64 aSize, BOOL aDynamic, IProgress **aProgress);
296
297 /** VDI asynchronous operation thread function */
298 static DECLCALLBACK(int) vdiTaskThread (RTTHREAD thread, void *pvUser);
299
300 enum State
301 {
302 NotCreated,
303 Created,
304 /* the following must be greater than Created */
305 Accessible,
306 };
307
308 State mState;
309
310 RTSEMEVENTMULTI mStateCheckSem;
311 ULONG mStateCheckWaiters;
312
313 Bstr mDescription;
314
315 ULONG64 mSize;
316 ULONG64 mActualSize;
317
318 Bstr mFilePath;
319 Bstr mFilePathFull;
320
321 friend class HardDisk;
322};
323
324// dependent inline members
325////////////////////////////////////////////////////////////////////////////////
326
327inline HVirtualDiskImage *HardDisk::asVDI()
328{
329 AssertReturn (mStorageType == HardDiskStorageType_VirtualDiskImage, 0);
330 return static_cast <HVirtualDiskImage *> (this);
331}
332
333////////////////////////////////////////////////////////////////////////////////
334
335class ATL_NO_VTABLE HISCSIHardDisk :
336 public HardDisk,
337 public VirtualBoxSupportTranslation <HISCSIHardDisk>,
338 public IISCSIHardDisk
339{
340
341public:
342
343 VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE(HISCSIHardDisk)
344
345 DECLARE_NOT_AGGREGATABLE(HISCSIHardDisk)
346
347 DECLARE_PROTECT_FINAL_CONSTRUCT()
348
349 BEGIN_COM_MAP(HISCSIHardDisk)
350 COM_INTERFACE_ENTRY(ISupportErrorInfo)
351 COM_INTERFACE_ENTRY(IHardDisk)
352 COM_INTERFACE_ENTRY(IISCSIHardDisk)
353 END_COM_MAP()
354
355 NS_DECL_ISUPPORTS
356
357 HRESULT FinalConstruct();
358 void FinalRelease();
359
360 // public initializer/uninitializer for internal purposes only
361
362 HRESULT init (VirtualBox *aVirtualBox,
363 CFGNODE aHDNode, CFGNODE aISCSINode);
364 HRESULT init (VirtualBox *aVirtualBox);
365 void uninit();
366
367 // IHardDisk properties
368 STDMETHOD(COMGETTER(Description)) (BSTR *aDescription);
369 STDMETHOD(COMSETTER(Description)) (INPTR BSTR aDescription);
370 STDMETHOD(COMGETTER(Size)) (ULONG64 *aSize);
371 STDMETHOD(COMGETTER(ActualSize)) (ULONG64 *aActualSize);
372
373 // IISCSIHardDisk properties
374 STDMETHOD(COMGETTER(Server)) (BSTR *aServer);
375 STDMETHOD(COMSETTER(Server)) (INPTR BSTR aServer);
376 STDMETHOD(COMGETTER(Port)) (USHORT *aPort);
377 STDMETHOD(COMSETTER(Port)) (USHORT aPort);
378 STDMETHOD(COMGETTER(Target)) (BSTR *aTarget);
379 STDMETHOD(COMSETTER(Target)) (INPTR BSTR aTarget);
380 STDMETHOD(COMGETTER(Lun)) (ULONG64 *aLun);
381 STDMETHOD(COMSETTER(Lun)) (ULONG64 aLun);
382 STDMETHOD(COMGETTER(UserName)) (BSTR *aUserName);
383 STDMETHOD(COMSETTER(UserName)) (INPTR BSTR aUserName);
384 STDMETHOD(COMGETTER(Password)) (BSTR *aPassword);
385 STDMETHOD(COMSETTER(Password)) (INPTR BSTR aPassword);
386
387 // public methods for internal purposes only
388
389 const Bstr &server() const { return mServer; }
390 USHORT port() const { return mPort; }
391 const Bstr &target() const { return mTarget; }
392 ULONG64 lun() const { return mLun; }
393 const Bstr &userName() const { return mUserName; }
394 const Bstr &password() const { return mPassword; }
395
396 HRESULT trySetRegistered (BOOL aRegistered);
397 HRESULT getAccessible (Bstr &aAccessError);
398
399 HRESULT saveSettings (CFGNODE aHDNode, CFGNODE aStorageNode);
400
401 Bstr toString (bool aShort = false);
402
403 HRESULT cloneToImage (const Guid &aId, const Utf8Str &aTargetPath,
404 Progress *aProgress);
405 HRESULT createDiffImage (const Guid &aId, const Utf8Str &aTargetPath,
406 Progress *aProgress);
407
408public:
409
410 // for VirtualBoxSupportErrorInfoImpl
411 static const wchar_t *getComponentName() { return L"ISCSIHardDisk"; }
412
413private:
414
415 HRESULT queryInformation (Bstr &aAccessError);
416
417 Bstr mDescription;
418
419 ULONG64 mSize;
420 ULONG64 mActualSize;
421
422 Bstr mServer;
423 USHORT mPort;
424 Bstr mTarget;
425 ULONG64 mLun;
426 Bstr mUserName;
427 Bstr mPassword;
428};
429
430////////////////////////////////////////////////////////////////////////////////
431
432class ATL_NO_VTABLE HVMDKImage :
433 public HardDisk,
434 public VirtualBoxSupportTranslation <HVMDKImage>,
435 public IVMDKImage
436{
437
438public:
439
440 VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE(HVMDKImage)
441
442 DECLARE_NOT_AGGREGATABLE(HVMDKImage)
443
444 DECLARE_PROTECT_FINAL_CONSTRUCT()
445
446 BEGIN_COM_MAP(HVMDKImage)
447 COM_INTERFACE_ENTRY(ISupportErrorInfo)
448 COM_INTERFACE_ENTRY(IHardDisk)
449 COM_INTERFACE_ENTRY(IVMDKImage)
450 END_COM_MAP()
451
452 NS_DECL_ISUPPORTS
453
454 HRESULT FinalConstruct();
455 void FinalRelease();
456
457 // public initializer/uninitializer for internal purposes only
458
459 HRESULT init (VirtualBox *aVirtualBox, HardDisk *aParent,
460 CFGNODE aHDNode, CFGNODE aVMDKNode);
461 HRESULT init (VirtualBox *aVirtualBox, HardDisk *aParent,
462 INPTR BSTR aFilePath, BOOL aRegistered = FALSE);
463 void uninit();
464
465 // IHardDisk properties
466 STDMETHOD(COMGETTER(Description)) (BSTR *aDescription);
467 STDMETHOD(COMSETTER(Description)) (INPTR BSTR aDescription);
468 STDMETHOD(COMGETTER(Size)) (ULONG64 *aSize);
469 STDMETHOD(COMGETTER(ActualSize)) (ULONG64 *aActualSize);
470
471 // IVirtualDiskImage properties
472 STDMETHOD(COMGETTER(FilePath)) (BSTR *aFilePath);
473 STDMETHOD(COMSETTER(FilePath)) (INPTR BSTR aFilePath);
474 STDMETHOD(COMGETTER(Created)) (BOOL *aCreated);
475
476 // IVirtualDiskImage methods
477 STDMETHOD(CreateDynamicImage) (ULONG64 aSize, IProgress **aProgress);
478 STDMETHOD(CreateFixedImage) (ULONG64 aSize, IProgress **aProgress);
479 STDMETHOD(DeleteImage)();
480
481 // public methods for internal purposes only
482
483 const Bstr &filePath() const { return mFilePath; }
484 const Bstr &filePathFull() const { return mFilePathFull; }
485
486 HRESULT trySetRegistered (BOOL aRegistered);
487 HRESULT getAccessible (Bstr &aAccessError);
488
489 HRESULT saveSettings (CFGNODE aHDNode, CFGNODE aStorageNode);
490
491 void updatePath (const char *aOldPath, const char *aNewPath);
492
493 Bstr toString (bool aShort = false);
494
495 HRESULT cloneToImage (const Guid &aId, const Utf8Str &aTargetPath,
496 Progress *aProgress);
497 HRESULT createDiffImage (const Guid &aId, const Utf8Str &aTargetPath,
498 Progress *aProgress);
499
500 // for VirtualBoxSupportErrorInfoImpl
501 static const wchar_t *getComponentName() { return L"VMDKImage"; }
502
503private:
504
505 HRESULT setFilePath (const BSTR aFilePath);
506 HRESULT queryInformation (Bstr *aAccessError);
507 HRESULT createImage (ULONG64 aSize, BOOL aDynamic, IProgress **aProgress);
508
509 /** VDI asynchronous operation thread function */
510 static DECLCALLBACK(int) vdiTaskThread (RTTHREAD thread, void *pvUser);
511
512 enum State
513 {
514 NotCreated,
515 Created,
516 /* the following must be greater than Created */
517 Accessible,
518 };
519
520 State mState;
521
522 RTSEMEVENTMULTI mStateCheckSem;
523 ULONG mStateCheckWaiters;
524
525 Bstr mDescription;
526
527 ULONG64 mSize;
528 ULONG64 mActualSize;
529
530 Bstr mFilePath;
531 Bstr mFilePathFull;
532
533 friend class HardDisk;
534};
535
536
537COM_DECL_READONLY_ENUM_AND_COLLECTION (HardDisk)
538
539#endif // ____H_HARDDISKIMPL
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