[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.
|
---|
[25326] | 104 | * @param aPassthrough Wether accesses are directly passed to the host drive.
|
---|
[23269] | 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,
|
---|
[25326] | 112 | bool aPassthrough)
|
---|
[23269] | 113 | {
|
---|
[23394] | 114 | LogFlowThisFuncEnter();
|
---|
[25326] | 115 | LogFlowThisFunc(("aParent=%p aMedium=%p aControllerName=%ls aPort=%d aDevice=%d aType=%d aPassthrough=%d\n", aParent, aMedium, aControllerName.raw(), aPort, aDevice, aType, aPassthrough));
|
---|
[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;
|
---|
[25326] | 133 | unconst(m->bd->type) = aType;
|
---|
[23269] | 134 |
|
---|
[25326] | 135 | m->bd->fPassthrough = aPassthrough;
|
---|
| 136 | /* Newly created attachments never have an implicitly created medium
|
---|
| 137 | * associated with them. Implicit diff image creation happens later. */
|
---|
| 138 | m->bd->fImplicit = false;
|
---|
[25200] | 139 |
|
---|
[23269] | 140 | /* Confirm a successful initialization when it's the case */
|
---|
| 141 | autoInitSpan.setSucceeded();
|
---|
| 142 |
|
---|
[24934] | 143 | /* Construct a short log name for this attachment. */
|
---|
| 144 | Utf8Str ctlName(aControllerName);
|
---|
| 145 | const char *psz = strpbrk(ctlName.c_str(), " \t:-");
|
---|
| 146 | mLogName = Utf8StrFmt("MA%p[%.*s:%u:%u:%s%s]",
|
---|
| 147 | this,
|
---|
| 148 | psz ? psz - ctlName.c_str() : 4, ctlName.c_str(),
|
---|
[24935] | 149 | aPort, aDevice, Global::stringifyDeviceType(aType),
|
---|
[25326] | 150 | m->bd->fImplicit ? ":I" : "");
|
---|
[24934] | 151 |
|
---|
[24989] | 152 | LogFlowThisFunc(("LEAVE - %s\n", getLogName()));
|
---|
[23269] | 153 | return S_OK;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | /**
|
---|
| 157 | * Uninitializes the instance.
|
---|
| 158 | * Called from FinalRelease().
|
---|
| 159 | */
|
---|
| 160 | void MediumAttachment::uninit()
|
---|
| 161 | {
|
---|
[24989] | 162 | LogFlowThisFunc(("ENTER - %s\n", getLogName()));
|
---|
[23394] | 163 |
|
---|
[23269] | 164 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
| 165 | AutoUninitSpan autoUninitSpan(this);
|
---|
| 166 | if (autoUninitSpan.uninitDone())
|
---|
| 167 | return;
|
---|
[23394] | 168 |
|
---|
[25200] | 169 | m->bd.free();
|
---|
[23394] | 170 |
|
---|
[25200] | 171 | unconst(m->pMachine).setNull();
|
---|
[23394] | 172 |
|
---|
[25200] | 173 | delete m;
|
---|
| 174 | m = NULL;
|
---|
[23269] | 175 |
|
---|
[23394] | 176 | LogFlowThisFuncLeave();
|
---|
[23269] | 177 | }
|
---|
| 178 |
|
---|
| 179 | // IHardDiskAttachment properties
|
---|
| 180 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 181 |
|
---|
| 182 | STDMETHODIMP MediumAttachment::COMGETTER(Medium)(IMedium **aHardDisk)
|
---|
| 183 | {
|
---|
[23394] | 184 | LogFlowThisFuncEnter();
|
---|
| 185 |
|
---|
[23269] | 186 | CheckComArgOutPointerValid(aHardDisk);
|
---|
| 187 |
|
---|
| 188 | AutoCaller autoCaller(this);
|
---|
[25149] | 189 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
[23269] | 190 |
|
---|
[25310] | 191 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
[23269] | 192 |
|
---|
[25200] | 193 | m->bd->pMedium.queryInterfaceTo(aHardDisk);
|
---|
[23269] | 194 |
|
---|
[23394] | 195 | LogFlowThisFuncLeave();
|
---|
[23269] | 196 | return S_OK;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[24511] | 199 | STDMETHODIMP MediumAttachment::COMGETTER(Controller)(BSTR *aController)
|
---|
[23269] | 200 | {
|
---|
[23394] | 201 | LogFlowThisFuncEnter();
|
---|
| 202 |
|
---|
[23269] | 203 | CheckComArgOutPointerValid(aController);
|
---|
| 204 |
|
---|
| 205 | AutoCaller autoCaller(this);
|
---|
[25149] | 206 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
[23269] | 207 |
|
---|
| 208 | /* m->controller is constant during life time, no need to lock */
|
---|
[25200] | 209 | m->bd->bstrControllerName.cloneTo(aController);
|
---|
[23269] | 210 |
|
---|
[23394] | 211 | LogFlowThisFuncLeave();
|
---|
[24511] | 212 | return S_OK;
|
---|
[23269] | 213 | }
|
---|
| 214 |
|
---|
| 215 | STDMETHODIMP MediumAttachment::COMGETTER(Port)(LONG *aPort)
|
---|
| 216 | {
|
---|
[23394] | 217 | LogFlowThisFuncEnter();
|
---|
| 218 |
|
---|
[23269] | 219 | CheckComArgOutPointerValid(aPort);
|
---|
| 220 |
|
---|
| 221 | AutoCaller autoCaller(this);
|
---|
[25149] | 222 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
[23269] | 223 |
|
---|
[25200] | 224 | /* m->bd->port is constant during life time, no need to lock */
|
---|
| 225 | *aPort = m->bd->lPort;
|
---|
[23269] | 226 |
|
---|
[23394] | 227 | LogFlowThisFuncLeave();
|
---|
[23269] | 228 | return S_OK;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | STDMETHODIMP MediumAttachment::COMGETTER(Device)(LONG *aDevice)
|
---|
| 232 | {
|
---|
[23394] | 233 | LogFlowThisFuncEnter();
|
---|
| 234 |
|
---|
[23269] | 235 | CheckComArgOutPointerValid(aDevice);
|
---|
| 236 |
|
---|
| 237 | AutoCaller autoCaller(this);
|
---|
[25149] | 238 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
[23269] | 239 |
|
---|
[25200] | 240 | /* m->bd->device is constant during life time, no need to lock */
|
---|
| 241 | *aDevice = m->bd->lDevice;
|
---|
[23269] | 242 |
|
---|
[23394] | 243 | LogFlowThisFuncLeave();
|
---|
[23269] | 244 | return S_OK;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | STDMETHODIMP MediumAttachment::COMGETTER(Type)(DeviceType_T *aType)
|
---|
| 248 | {
|
---|
[23394] | 249 | LogFlowThisFuncEnter();
|
---|
| 250 |
|
---|
[23269] | 251 | CheckComArgOutPointerValid(aType);
|
---|
| 252 |
|
---|
| 253 | AutoCaller autoCaller(this);
|
---|
[25149] | 254 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
[23269] | 255 |
|
---|
[25200] | 256 | /* m->bd->type is constant during life time, no need to lock */
|
---|
| 257 | *aType = m->bd->type;
|
---|
[23269] | 258 |
|
---|
[23394] | 259 | LogFlowThisFuncLeave();
|
---|
[23269] | 260 | return S_OK;
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | STDMETHODIMP MediumAttachment::COMGETTER(Passthrough)(BOOL *aPassthrough)
|
---|
| 264 | {
|
---|
[23394] | 265 | LogFlowThisFuncEnter();
|
---|
| 266 |
|
---|
[23269] | 267 | CheckComArgOutPointerValid(aPassthrough);
|
---|
| 268 |
|
---|
| 269 | AutoCaller autoCaller(this);
|
---|
[25149] | 270 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
[23269] | 271 |
|
---|
[25310] | 272 | AutoReadLock lock(this COMMA_LOCKVAL_SRC_POS);
|
---|
[23269] | 273 |
|
---|
[25200] | 274 | *aPassthrough = m->bd->fPassthrough;
|
---|
[23394] | 275 |
|
---|
| 276 | LogFlowThisFuncLeave();
|
---|
[23269] | 277 | return S_OK;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
[25200] | 280 | /**
|
---|
| 281 | * @note Locks this object for writing.
|
---|
| 282 | */
|
---|
| 283 | bool MediumAttachment::rollback()
|
---|
| 284 | {
|
---|
| 285 | LogFlowThisFunc(("ENTER - %s\n", getLogName()));
|
---|
| 286 |
|
---|
| 287 | /* sanity */
|
---|
| 288 | AutoCaller autoCaller(this);
|
---|
| 289 | AssertComRCReturn (autoCaller.rc(), false);
|
---|
| 290 |
|
---|
[25310] | 291 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
[25200] | 292 |
|
---|
| 293 | bool changed = false;
|
---|
| 294 |
|
---|
| 295 | if (m->bd.isBackedUp())
|
---|
| 296 | {
|
---|
| 297 | /* we need to check all data to see whether anything will be changed
|
---|
| 298 | * after rollback */
|
---|
| 299 | changed = m->bd.hasActualChanges();
|
---|
| 300 | m->bd.rollback();
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | LogFlowThisFunc(("LEAVE - %s - returning %RTbool\n", getLogName(), changed));
|
---|
| 304 | return changed;
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | /**
|
---|
| 308 | * @note Locks this object for writing.
|
---|
| 309 | */
|
---|
| 310 | void MediumAttachment::commit()
|
---|
| 311 | {
|
---|
| 312 | LogFlowThisFuncEnter();
|
---|
| 313 |
|
---|
| 314 | /* sanity */
|
---|
| 315 | AutoCaller autoCaller(this);
|
---|
| 316 | AssertComRCReturnVoid (autoCaller.rc());
|
---|
| 317 |
|
---|
[25310] | 318 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
[25200] | 319 |
|
---|
| 320 | if (m->bd.isBackedUp())
|
---|
| 321 | m->bd.commit();
|
---|
| 322 |
|
---|
| 323 | LogFlowThisFuncLeave();
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | bool MediumAttachment::isImplicit() const
|
---|
| 327 | {
|
---|
| 328 | return m->bd->fImplicit;
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | void MediumAttachment::setImplicit(bool aImplicit)
|
---|
| 332 | {
|
---|
| 333 | m->bd->fImplicit = aImplicit;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | const ComObjPtr<Medium>& MediumAttachment::getMedium() const
|
---|
| 337 | {
|
---|
| 338 | return m->bd->pMedium;
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | Bstr MediumAttachment::getControllerName() const
|
---|
| 342 | {
|
---|
| 343 | return m->bd->bstrControllerName;
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | LONG MediumAttachment::getPort() const
|
---|
| 347 | {
|
---|
| 348 | return m->bd->lPort;
|
---|
| 349 | }
|
---|
| 350 |
|
---|
| 351 | LONG MediumAttachment::getDevice() const
|
---|
| 352 | {
|
---|
| 353 | return m->bd->lDevice;
|
---|
| 354 | }
|
---|
| 355 |
|
---|
| 356 | DeviceType_T MediumAttachment::getType() const
|
---|
| 357 | {
|
---|
| 358 | return m->bd->type;
|
---|
| 359 | }
|
---|
| 360 |
|
---|
| 361 | bool MediumAttachment::getPassthrough() const
|
---|
| 362 | {
|
---|
[25310] | 363 | AutoReadLock lock(this COMMA_LOCKVAL_SRC_POS);
|
---|
[25200] | 364 | return m->bd->fPassthrough;
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | bool MediumAttachment::matches(CBSTR aControllerName, LONG aPort, LONG aDevice)
|
---|
| 368 | {
|
---|
| 369 | return ( aControllerName == m->bd->bstrControllerName
|
---|
| 370 | && aPort == m->bd->lPort
|
---|
| 371 | && aDevice == m->bd->lDevice);
|
---|
| 372 | }
|
---|
| 373 |
|
---|
| 374 | /** Must be called from under this object's write lock. */
|
---|
| 375 | void MediumAttachment::updateMedium(const ComObjPtr<Medium> &aMedium, bool aImplicit)
|
---|
| 376 | {
|
---|
| 377 | m->bd.backup();
|
---|
| 378 | m->bd->pMedium = aMedium;
|
---|
| 379 | m->bd->fImplicit = aImplicit;
|
---|
| 380 | }
|
---|
| 381 |
|
---|
| 382 | /** Must be called from under this object's write lock. */
|
---|
| 383 | void MediumAttachment::updatePassthrough(bool aPassthrough)
|
---|
| 384 | {
|
---|
| 385 | m->bd.backup();
|
---|
| 386 | m->bd->fPassthrough = aPassthrough;
|
---|
| 387 | }
|
---|
| 388 |
|
---|