[23269] | 1 | /** @file
|
---|
| 2 | *
|
---|
| 3 | * VirtualBox COM class implementation
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
| 7 | * Copyright (C) 2006-2009 Sun Microsystems, Inc.
|
---|
| 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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
| 18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
| 19 | * additional information or have any questions.
|
---|
| 20 | */
|
---|
| 21 |
|
---|
| 22 | #include "MediumAttachmentImpl.h"
|
---|
| 23 | #include "MachineImpl.h"
|
---|
[25200] | 24 | #include "MediumImpl.h"
|
---|
[24934] | 25 | #include "Global.h"
|
---|
[23269] | 26 |
|
---|
| 27 | #include "Logging.h"
|
---|
| 28 |
|
---|
[25200] | 29 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 30 | //
|
---|
| 31 | // private member data definition
|
---|
| 32 | //
|
---|
| 33 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 34 |
|
---|
| 35 | struct BackupableMediumAttachmentData
|
---|
| 36 | {
|
---|
| 37 | BackupableMediumAttachmentData()
|
---|
| 38 | : lPort(0),
|
---|
| 39 | lDevice(0),
|
---|
| 40 | type(DeviceType_Null),
|
---|
| 41 | fPassthrough(false),
|
---|
| 42 | fImplicit(false)
|
---|
| 43 | { }
|
---|
| 44 |
|
---|
| 45 | bool operator==(const BackupableMediumAttachmentData &that) const
|
---|
| 46 | {
|
---|
| 47 | return this == &that
|
---|
| 48 | || (fPassthrough == that.fPassthrough);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | ComObjPtr<Medium> pMedium;
|
---|
| 52 | /* Since MediumAttachment is not a first class citizen when it
|
---|
| 53 | * comes to managing settings, having a reference to the storage
|
---|
| 54 | * controller will not work - when settings are changed it will point
|
---|
| 55 | * to the old, uninitialized instance. Changing this requires
|
---|
| 56 | * substantial changes to MediumImpl.cpp. */
|
---|
| 57 | const Bstr bstrControllerName;
|
---|
| 58 | const LONG lPort;
|
---|
| 59 | const LONG lDevice;
|
---|
| 60 | const DeviceType_T type;
|
---|
| 61 | bool fPassthrough : 1;
|
---|
| 62 | bool fImplicit : 1;
|
---|
| 63 | };
|
---|
| 64 |
|
---|
| 65 | struct MediumAttachment::Data
|
---|
| 66 | {
|
---|
| 67 | Data()
|
---|
| 68 | { }
|
---|
| 69 |
|
---|
| 70 | /** Reference to Machine object, for checking mutable state. */
|
---|
| 71 | const ComObjPtr<Machine, ComWeakRef> pMachine;
|
---|
| 72 | /* later: const ComObjPtr<MediumAttachment> mPeer; */
|
---|
| 73 |
|
---|
| 74 | Backupable<BackupableMediumAttachmentData> bd;
|
---|
| 75 | };
|
---|
| 76 |
|
---|
[23269] | 77 | // constructor / destructor
|
---|
| 78 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 79 |
|
---|
| 80 | HRESULT MediumAttachment::FinalConstruct()
|
---|
| 81 | {
|
---|
[23394] | 82 | LogFlowThisFunc(("\n"));
|
---|
[23269] | 83 | return S_OK;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | void MediumAttachment::FinalRelease()
|
---|
| 87 | {
|
---|
[23394] | 88 | LogFlowThisFuncEnter();
|
---|
[23269] | 89 | uninit();
|
---|
[23394] | 90 | LogFlowThisFuncLeave();
|
---|
[23269] | 91 | }
|
---|
| 92 |
|
---|
| 93 | // public initializer/uninitializer for internal purposes only
|
---|
| 94 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 95 |
|
---|
| 96 | /**
|
---|
| 97 | * Initializes the medium attachment object.
|
---|
| 98 | *
|
---|
[23394] | 99 | * @param aParent Machine object.
|
---|
[23269] | 100 | * @param aMedium Medium object.
|
---|
| 101 | * @param aController Controller the hard disk is attached to.
|
---|
| 102 | * @param aPort Port number.
|
---|
| 103 | * @param aDevice Device number on the port.
|
---|
| 104 | * @param aImplicit Wether the attachment contains an implicitly created diff.
|
---|
| 105 | */
|
---|
[23394] | 106 | HRESULT MediumAttachment::init(Machine *aParent,
|
---|
[23269] | 107 | Medium *aMedium,
|
---|
[23914] | 108 | const Bstr &aControllerName,
|
---|
[23269] | 109 | LONG aPort,
|
---|
| 110 | LONG aDevice,
|
---|
| 111 | DeviceType_T aType,
|
---|
| 112 | bool aImplicit /*= false*/)
|
---|
| 113 | {
|
---|
[23394] | 114 | LogFlowThisFuncEnter();
|
---|
[23914] | 115 | LogFlowThisFunc(("aParent=%p aMedium=%p aControllerName=%ls aPort=%d aDevice=%d aType=%d aImplicit=%d\n", aParent, aMedium, aControllerName.raw(), aPort, aDevice, aType, aImplicit));
|
---|
[23394] | 116 |
|
---|
[23269] | 117 | if (aType == DeviceType_HardDisk)
|
---|
| 118 | AssertReturn(aMedium, E_INVALIDARG);
|
---|
| 119 |
|
---|
| 120 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
| 121 | AutoInitSpan autoInitSpan(this);
|
---|
| 122 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
| 123 |
|
---|
[25200] | 124 | m = new Data();
|
---|
[23394] | 125 |
|
---|
[25200] | 126 | unconst(m->pMachine) = aParent;
|
---|
[23269] | 127 |
|
---|
[25200] | 128 | m->bd.allocate();
|
---|
| 129 | m->bd->pMedium = aMedium;
|
---|
| 130 | unconst(m->bd->bstrControllerName) = aControllerName;
|
---|
| 131 | unconst(m->bd->lPort) = aPort;
|
---|
| 132 | unconst(m->bd->lDevice) = aDevice;
|
---|
| 133 | unconst(m->bd->type) = aType;
|
---|
[23269] | 134 |
|
---|
[25200] | 135 | m->bd->fPassthrough = false;
|
---|
| 136 | m->bd->fImplicit = aImplicit;
|
---|
| 137 |
|
---|
[23269] | 138 | /* Confirm a successful initialization when it's the case */
|
---|
| 139 | autoInitSpan.setSucceeded();
|
---|
| 140 |
|
---|
[24934] | 141 | /* Construct a short log name for this attachment. */
|
---|
| 142 | Utf8Str ctlName(aControllerName);
|
---|
| 143 | const char *psz = strpbrk(ctlName.c_str(), " \t:-");
|
---|
| 144 | mLogName = Utf8StrFmt("MA%p[%.*s:%u:%u:%s%s]",
|
---|
| 145 | this,
|
---|
| 146 | psz ? psz - ctlName.c_str() : 4, ctlName.c_str(),
|
---|
[24935] | 147 | aPort, aDevice, Global::stringifyDeviceType(aType),
|
---|
[24934] | 148 | aImplicit ? ":I" : "");
|
---|
| 149 |
|
---|
[24989] | 150 | LogFlowThisFunc(("LEAVE - %s\n", getLogName()));
|
---|
[23269] | 151 | return S_OK;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /**
|
---|
| 155 | * Uninitializes the instance.
|
---|
| 156 | * Called from FinalRelease().
|
---|
| 157 | */
|
---|
| 158 | void MediumAttachment::uninit()
|
---|
| 159 | {
|
---|
[24989] | 160 | LogFlowThisFunc(("ENTER - %s\n", getLogName()));
|
---|
[23394] | 161 |
|
---|
[23269] | 162 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
| 163 | AutoUninitSpan autoUninitSpan(this);
|
---|
| 164 | if (autoUninitSpan.uninitDone())
|
---|
| 165 | return;
|
---|
[23394] | 166 |
|
---|
[25200] | 167 | m->bd.free();
|
---|
[23394] | 168 |
|
---|
[25200] | 169 | unconst(m->pMachine).setNull();
|
---|
[23394] | 170 |
|
---|
[25200] | 171 | delete m;
|
---|
| 172 | m = NULL;
|
---|
[23269] | 173 |
|
---|
[23394] | 174 | LogFlowThisFuncLeave();
|
---|
[23269] | 175 | }
|
---|
| 176 |
|
---|
| 177 | // IHardDiskAttachment properties
|
---|
| 178 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 179 |
|
---|
| 180 | STDMETHODIMP MediumAttachment::COMGETTER(Medium)(IMedium **aHardDisk)
|
---|
| 181 | {
|
---|
[23394] | 182 | LogFlowThisFuncEnter();
|
---|
| 183 |
|
---|
[23269] | 184 | CheckComArgOutPointerValid(aHardDisk);
|
---|
| 185 |
|
---|
| 186 | AutoCaller autoCaller(this);
|
---|
[25149] | 187 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
[23269] | 188 |
|
---|
| 189 | AutoReadLock alock(this);
|
---|
| 190 |
|
---|
[25200] | 191 | m->bd->pMedium.queryInterfaceTo(aHardDisk);
|
---|
[23269] | 192 |
|
---|
[23394] | 193 | LogFlowThisFuncLeave();
|
---|
[23269] | 194 | return S_OK;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[24511] | 197 | STDMETHODIMP MediumAttachment::COMGETTER(Controller)(BSTR *aController)
|
---|
[23269] | 198 | {
|
---|
[23394] | 199 | LogFlowThisFuncEnter();
|
---|
| 200 |
|
---|
[23269] | 201 | CheckComArgOutPointerValid(aController);
|
---|
| 202 |
|
---|
| 203 | AutoCaller autoCaller(this);
|
---|
[25149] | 204 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
[23269] | 205 |
|
---|
| 206 | /* m->controller is constant during life time, no need to lock */
|
---|
[25200] | 207 | m->bd->bstrControllerName.cloneTo(aController);
|
---|
[23269] | 208 |
|
---|
[23394] | 209 | LogFlowThisFuncLeave();
|
---|
[24511] | 210 | return S_OK;
|
---|
[23269] | 211 | }
|
---|
| 212 |
|
---|
| 213 | STDMETHODIMP MediumAttachment::COMGETTER(Port)(LONG *aPort)
|
---|
| 214 | {
|
---|
[23394] | 215 | LogFlowThisFuncEnter();
|
---|
| 216 |
|
---|
[23269] | 217 | CheckComArgOutPointerValid(aPort);
|
---|
| 218 |
|
---|
| 219 | AutoCaller autoCaller(this);
|
---|
[25149] | 220 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
[23269] | 221 |
|
---|
[25200] | 222 | /* m->bd->port is constant during life time, no need to lock */
|
---|
| 223 | *aPort = m->bd->lPort;
|
---|
[23269] | 224 |
|
---|
[23394] | 225 | LogFlowThisFuncLeave();
|
---|
[23269] | 226 | return S_OK;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | STDMETHODIMP MediumAttachment::COMGETTER(Device)(LONG *aDevice)
|
---|
| 230 | {
|
---|
[23394] | 231 | LogFlowThisFuncEnter();
|
---|
| 232 |
|
---|
[23269] | 233 | CheckComArgOutPointerValid(aDevice);
|
---|
| 234 |
|
---|
| 235 | AutoCaller autoCaller(this);
|
---|
[25149] | 236 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
[23269] | 237 |
|
---|
[25200] | 238 | /* m->bd->device is constant during life time, no need to lock */
|
---|
| 239 | *aDevice = m->bd->lDevice;
|
---|
[23269] | 240 |
|
---|
[23394] | 241 | LogFlowThisFuncLeave();
|
---|
[23269] | 242 | return S_OK;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | STDMETHODIMP MediumAttachment::COMGETTER(Type)(DeviceType_T *aType)
|
---|
| 246 | {
|
---|
[23394] | 247 | LogFlowThisFuncEnter();
|
---|
| 248 |
|
---|
[23269] | 249 | CheckComArgOutPointerValid(aType);
|
---|
| 250 |
|
---|
| 251 | AutoCaller autoCaller(this);
|
---|
[25149] | 252 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
[23269] | 253 |
|
---|
[25200] | 254 | /* m->bd->type is constant during life time, no need to lock */
|
---|
| 255 | *aType = m->bd->type;
|
---|
[23269] | 256 |
|
---|
[23394] | 257 | LogFlowThisFuncLeave();
|
---|
[23269] | 258 | return S_OK;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | STDMETHODIMP MediumAttachment::COMGETTER(Passthrough)(BOOL *aPassthrough)
|
---|
| 262 | {
|
---|
[23394] | 263 | LogFlowThisFuncEnter();
|
---|
| 264 |
|
---|
[23269] | 265 | CheckComArgOutPointerValid(aPassthrough);
|
---|
| 266 |
|
---|
| 267 | AutoCaller autoCaller(this);
|
---|
[25149] | 268 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
[23269] | 269 |
|
---|
| 270 | AutoReadLock lock(this);
|
---|
| 271 |
|
---|
[25200] | 272 | *aPassthrough = m->bd->fPassthrough;
|
---|
[23394] | 273 |
|
---|
| 274 | LogFlowThisFuncLeave();
|
---|
[23269] | 275 | return S_OK;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
[25200] | 278 | /**
|
---|
| 279 | * @note Locks this object for writing.
|
---|
| 280 | */
|
---|
| 281 | bool MediumAttachment::rollback()
|
---|
| 282 | {
|
---|
| 283 | LogFlowThisFunc(("ENTER - %s\n", getLogName()));
|
---|
| 284 |
|
---|
| 285 | /* sanity */
|
---|
| 286 | AutoCaller autoCaller(this);
|
---|
| 287 | AssertComRCReturn (autoCaller.rc(), false);
|
---|
| 288 |
|
---|
| 289 | AutoWriteLock alock(this);
|
---|
| 290 |
|
---|
| 291 | bool changed = false;
|
---|
| 292 |
|
---|
| 293 | if (m->bd.isBackedUp())
|
---|
| 294 | {
|
---|
| 295 | /* we need to check all data to see whether anything will be changed
|
---|
| 296 | * after rollback */
|
---|
| 297 | changed = m->bd.hasActualChanges();
|
---|
| 298 | m->bd.rollback();
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | LogFlowThisFunc(("LEAVE - %s - returning %RTbool\n", getLogName(), changed));
|
---|
| 302 | return changed;
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | /**
|
---|
| 306 | * @note Locks this object for writing.
|
---|
| 307 | */
|
---|
| 308 | void MediumAttachment::commit()
|
---|
| 309 | {
|
---|
| 310 | LogFlowThisFuncEnter();
|
---|
| 311 |
|
---|
| 312 | /* sanity */
|
---|
| 313 | AutoCaller autoCaller(this);
|
---|
| 314 | AssertComRCReturnVoid (autoCaller.rc());
|
---|
| 315 |
|
---|
| 316 | AutoWriteLock alock(this);
|
---|
| 317 |
|
---|
| 318 | if (m->bd.isBackedUp())
|
---|
| 319 | m->bd.commit();
|
---|
| 320 |
|
---|
| 321 | LogFlowThisFuncLeave();
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | bool MediumAttachment::isImplicit() const
|
---|
| 325 | {
|
---|
| 326 | return m->bd->fImplicit;
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | void MediumAttachment::setImplicit(bool aImplicit)
|
---|
| 330 | {
|
---|
| 331 | m->bd->fImplicit = aImplicit;
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 | const ComObjPtr<Medium>& MediumAttachment::getMedium() const
|
---|
| 335 | {
|
---|
| 336 | return m->bd->pMedium;
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | Bstr MediumAttachment::getControllerName() const
|
---|
| 340 | {
|
---|
| 341 | return m->bd->bstrControllerName;
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 | LONG MediumAttachment::getPort() const
|
---|
| 345 | {
|
---|
| 346 | return m->bd->lPort;
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | LONG MediumAttachment::getDevice() const
|
---|
| 350 | {
|
---|
| 351 | return m->bd->lDevice;
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | DeviceType_T MediumAttachment::getType() const
|
---|
| 355 | {
|
---|
| 356 | return m->bd->type;
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | bool MediumAttachment::getPassthrough() const
|
---|
| 360 | {
|
---|
| 361 | AutoReadLock lock(this);
|
---|
| 362 | return m->bd->fPassthrough;
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | bool MediumAttachment::matches(CBSTR aControllerName, LONG aPort, LONG aDevice)
|
---|
| 366 | {
|
---|
| 367 | return ( aControllerName == m->bd->bstrControllerName
|
---|
| 368 | && aPort == m->bd->lPort
|
---|
| 369 | && aDevice == m->bd->lDevice);
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | /** Must be called from under this object's write lock. */
|
---|
| 373 | void MediumAttachment::updateMedium(const ComObjPtr<Medium> &aMedium, bool aImplicit)
|
---|
| 374 | {
|
---|
| 375 | m->bd.backup();
|
---|
| 376 | m->bd->pMedium = aMedium;
|
---|
| 377 | m->bd->fImplicit = aImplicit;
|
---|
| 378 | }
|
---|
| 379 |
|
---|
| 380 | /** Must be called from under this object's write lock. */
|
---|
| 381 | void MediumAttachment::updatePassthrough(bool aPassthrough)
|
---|
| 382 | {
|
---|
| 383 | m->bd.backup();
|
---|
| 384 | m->bd->fPassthrough = aPassthrough;
|
---|
| 385 | }
|
---|
| 386 |
|
---|