VirtualBox

source: vbox/trunk/src/VBox/Main/DVDDriveImpl.cpp@ 22138

Last change on this file since 22138 was 21945, checked in by vboxsync, 15 years ago

Main: fixed assertion

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.9 KB
Line 
1/* $Id: DVDDriveImpl.cpp 21945 2009-08-03 15:26:14Z vboxsync $ */
2
3/** @file
4 *
5 * VirtualBox COM class implementation
6 */
7
8/*
9 * Copyright (C) 2006-2009 Sun Microsystems, Inc.
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
20 * Clara, CA 95054 USA or visit http://www.sun.com if you need
21 * additional information or have any questions.
22 */
23
24#include "DVDDriveImpl.h"
25
26#include "MachineImpl.h"
27#include "HostImpl.h"
28#include "HostDVDDriveImpl.h"
29#include "VirtualBoxImpl.h"
30
31#include "Global.h"
32
33#include "Logging.h"
34
35#include <iprt/string.h>
36#include <iprt/cpputils.h>
37
38#include <VBox/settings.h>
39
40// constructor / destructor
41////////////////////////////////////////////////////////////////////////////////
42
43DEFINE_EMPTY_CTOR_DTOR (DVDDrive)
44
45HRESULT DVDDrive::FinalConstruct()
46{
47 return S_OK;
48}
49
50void DVDDrive::FinalRelease()
51{
52 uninit();
53}
54
55// public initializer/uninitializer for internal purposes only
56////////////////////////////////////////////////////////////////////////////////
57
58/**
59 * Initializes the DVD drive object.
60 *
61 * @param aParent Handle of the parent object.
62 */
63HRESULT DVDDrive::init (Machine *aParent)
64{
65 LogFlowThisFunc(("aParent=%p\n", aParent));
66
67 ComAssertRet (aParent, E_INVALIDARG);
68
69 /* Enclose the state transition NotReady->InInit->Ready */
70 AutoInitSpan autoInitSpan(this);
71 AssertReturn(autoInitSpan.isOk(), E_FAIL);
72
73 unconst(mParent) = aParent;
74 /* mPeer is left null */
75
76 m.allocate();
77
78 /* Confirm a successful initialization */
79 autoInitSpan.setSucceeded();
80
81 return S_OK;
82}
83
84/**
85 * Initializes the DVD drive object given another DVD drive object
86 * (a kind of copy constructor). This object shares data with
87 * the object passed as an argument.
88 *
89 * @note This object must be destroyed before the original object
90 * it shares data with is destroyed.
91 *
92 * @note Locks @a aThat object for reading.
93 */
94HRESULT DVDDrive::init (Machine *aParent, DVDDrive *aThat)
95{
96 LogFlowThisFunc(("aParent=%p, aThat=%p\n", aParent, aThat));
97
98 ComAssertRet (aParent && aThat, E_INVALIDARG);
99
100 /* Enclose the state transition NotReady->InInit->Ready */
101 AutoInitSpan autoInitSpan(this);
102 AssertReturn(autoInitSpan.isOk(), E_FAIL);
103
104 unconst(mParent) = aParent;
105 unconst(mPeer) = aThat;
106
107 AutoCaller thatCaller (aThat);
108 AssertComRCReturnRC(thatCaller.rc());
109
110 AutoReadLock thatLock (aThat);
111 m.share (aThat->m);
112
113 /* Confirm a successful initialization */
114 autoInitSpan.setSucceeded();
115
116 return S_OK;
117}
118
119/**
120 * Initializes the DVD drive object given another DVD drive object
121 * (a kind of copy constructor). This object makes a private copy of data
122 * of the original object passed as an argument.
123 *
124 * @note Locks @a aThat object for reading.
125 */
126HRESULT DVDDrive::initCopy (Machine *aParent, DVDDrive *aThat)
127{
128 LogFlowThisFunc(("aParent=%p, aThat=%p\n", aParent, aThat));
129
130 ComAssertRet (aParent && aThat, E_INVALIDARG);
131
132 /* Enclose the state transition NotReady->InInit->Ready */
133 AutoInitSpan autoInitSpan(this);
134 AssertReturn(autoInitSpan.isOk(), E_FAIL);
135
136 unconst(mParent) = aParent;
137 /* mPeer is left null */
138
139 AutoCaller thatCaller (aThat);
140 AssertComRCReturnRC(thatCaller.rc());
141
142 AutoReadLock thatLock (aThat);
143 m.attachCopy (aThat->m);
144
145 /* at present, this must be a snapshot machine */
146 Assert (!aParent->snapshotId().isEmpty());
147
148 if (m->state == DriveState_ImageMounted)
149 {
150 /* associate the DVD image media with the snapshot */
151 HRESULT rc = m->image->attachTo (aParent->id(),
152 aParent->snapshotId());
153 AssertComRC (rc);
154 }
155
156 /* Confirm a successful initialization */
157 autoInitSpan.setSucceeded();
158
159 return S_OK;
160}
161
162/**
163 * Uninitializes the instance and sets the ready flag to FALSE.
164 * Called either from FinalRelease() or by the parent when it gets destroyed.
165 */
166void DVDDrive::uninit()
167{
168 LogFlowThisFunc(("\n"));
169
170 /* Enclose the state transition Ready->InUninit->NotReady */
171 AutoUninitSpan autoUninitSpan(this);
172 if (autoUninitSpan.uninitDone())
173 return;
174
175 if ((mParent->type() == Machine::IsMachine ||
176 mParent->type() == Machine::IsSnapshotMachine) &&
177 m->state == DriveState_ImageMounted)
178 {
179 /* Deassociate the DVD image (only when mParent is a real Machine or a
180 * SnapshotMachine instance; SessionMachine instances
181 * refer to real Machine hard disks). This is necessary for a clean
182 * re-initialization of the VM after successfully re-checking the
183 * accessibility state. */
184 HRESULT rc = m->image->detachFrom (mParent->id(),
185 mParent->snapshotId());
186 AssertComRC (rc);
187 }
188
189 m.free();
190
191 unconst(mPeer).setNull();
192 unconst(mParent).setNull();
193}
194
195// IDVDDrive properties
196////////////////////////////////////////////////////////////////////////////////
197
198STDMETHODIMP DVDDrive::COMGETTER(State) (DriveState_T *aState)
199{
200 CheckComArgOutPointerValid(aState);
201
202 AutoCaller autoCaller(this);
203 CheckComRCReturnRC(autoCaller.rc());
204
205 AutoReadLock alock(this);
206
207 *aState = m->state;
208
209 return S_OK;
210}
211
212STDMETHODIMP DVDDrive::COMGETTER(Passthrough) (BOOL *aPassthrough)
213{
214 CheckComArgOutPointerValid(aPassthrough);
215
216 AutoCaller autoCaller(this);
217 CheckComRCReturnRC(autoCaller.rc());
218
219 AutoReadLock alock(this);
220
221 *aPassthrough = m->passthrough;
222
223 return S_OK;
224}
225
226STDMETHODIMP DVDDrive::COMSETTER(Passthrough) (BOOL aPassthrough)
227{
228 AutoCaller autoCaller(this);
229 CheckComRCReturnRC(autoCaller.rc());
230
231 /* the machine needs to be mutable */
232 Machine::AutoMutableStateDependency adep (mParent);
233 CheckComRCReturnRC(adep.rc());
234
235 AutoWriteLock alock(this);
236
237 if (m->passthrough != aPassthrough)
238 {
239 m.backup();
240 m->passthrough = aPassthrough;
241 }
242
243 return S_OK;
244}
245
246// IDVDDrive methods
247////////////////////////////////////////////////////////////////////////////////
248
249STDMETHODIMP DVDDrive::MountImage (IN_BSTR aImageId)
250{
251 Guid imageId(aImageId);
252 CheckComArgExpr(aImageId, !imageId.isEmpty());
253
254 AutoCaller autoCaller(this);
255 CheckComRCReturnRC(autoCaller.rc());
256
257 /* the machine needs to be mutable */
258 Machine::AutoMutableStateDependency adep (mParent);
259 CheckComRCReturnRC(adep.rc());
260
261 AutoWriteLock alock(this);
262
263 HRESULT rc = E_FAIL;
264
265 /* Our lifetime is bound to mParent's lifetime, so we don't add caller.
266 * We also don't lock mParent since its mParent field is const. */
267
268 ComObjPtr<DVDImage> image;
269 rc = mParent->virtualBox()->findDVDImage(&imageId, NULL,
270 true /* aSetError */, &image);
271
272 if (SUCCEEDED(rc))
273 {
274 if (m->state != DriveState_ImageMounted ||
275 !m->image.equalsTo (image))
276 {
277 rc = image->attachTo (mParent->id(), mParent->snapshotId());
278 if (SUCCEEDED(rc))
279 {
280 /* umount() will backup data */
281 rc = unmount();
282
283 if (SUCCEEDED(rc))
284 {
285 /* lock the image for reading if the VM is online. It will
286 * be unlocked either when unmounted from this drive or by
287 * SessionMachine::setMachineState() when the VM is
288 * terminated */
289 if (Global::IsOnline (adep.machineState()))
290 rc = image->LockRead (NULL);
291 }
292
293 if (SUCCEEDED(rc))
294 {
295 m->image = image;
296 m->state = DriveState_ImageMounted;
297
298 /* leave the lock before informing callbacks */
299 alock.unlock();
300
301 mParent->onDVDDriveChange();
302 }
303 }
304 }
305 }
306
307 return rc;
308}
309
310STDMETHODIMP DVDDrive::CaptureHostDrive (IHostDVDDrive *aHostDVDDrive)
311{
312 CheckComArgNotNull(aHostDVDDrive);
313
314 AutoCaller autoCaller(this);
315 CheckComRCReturnRC(autoCaller.rc());
316
317 /* the machine needs to be mutable */
318 Machine::AutoMutableStateDependency adep (mParent);
319 CheckComRCReturnRC(adep.rc());
320
321 AutoWriteLock alock(this);
322
323 if (m->state != DriveState_HostDriveCaptured ||
324 !m->hostDrive.equalsTo (aHostDVDDrive))
325 {
326 /* umount() will backup data */
327 HRESULT rc = unmount();
328 if (SUCCEEDED(rc))
329 {
330 m->hostDrive = aHostDVDDrive;
331 m->state = DriveState_HostDriveCaptured;
332
333 /* leave the lock before informing callbacks */
334 alock.unlock();
335
336 mParent->onDVDDriveChange();
337 }
338 }
339
340 return S_OK;
341}
342
343STDMETHODIMP DVDDrive::Unmount()
344{
345 AutoCaller autoCaller(this);
346 CheckComRCReturnRC(autoCaller.rc());
347
348 /* the machine needs to be mutable */
349 Machine::AutoMutableStateDependency adep (mParent);
350 CheckComRCReturnRC(adep.rc());
351
352 AutoWriteLock alock(this);
353
354 if (m->state != DriveState_NotMounted)
355 {
356 /* umount() will backup data */
357 HRESULT rc = unmount();
358 if (SUCCEEDED(rc))
359 {
360 m->state = DriveState_NotMounted;
361
362 /* leave the lock before informing callbacks */
363 alock.unlock();
364
365 rc = mParent->onDVDDriveChange();
366 if (FAILED (rc))
367 rollback();
368 }
369 }
370
371 return S_OK;
372}
373
374STDMETHODIMP DVDDrive::GetImage (IDVDImage **aDVDImage)
375{
376 CheckComArgOutPointerValid(aDVDImage);
377
378 AutoCaller autoCaller(this);
379 CheckComRCReturnRC(autoCaller.rc());
380
381 AutoReadLock alock(this);
382
383 m->image.queryInterfaceTo(aDVDImage);
384
385 return S_OK;
386}
387
388STDMETHODIMP DVDDrive::GetHostDrive(IHostDVDDrive **aHostDrive)
389{
390 CheckComArgOutPointerValid(aHostDrive);
391
392 AutoCaller autoCaller(this);
393 CheckComRCReturnRC(autoCaller.rc());
394
395 AutoReadLock alock(this);
396
397 m->hostDrive.queryInterfaceTo(aHostDrive);
398
399 return S_OK;
400}
401
402// public methods only for internal purposes
403////////////////////////////////////////////////////////////////////////////////
404
405/**
406 * Loads settings from the given machine node. May be called once right after
407 * this object creation.
408 *
409 * @param aMachineNode <Machine> node.
410 *
411 * @note Locks this object for writing.
412 */
413HRESULT DVDDrive::loadSettings (const settings::Key &aMachineNode)
414{
415 using namespace settings;
416
417 AssertReturn(!aMachineNode.isNull(), E_FAIL);
418
419 AutoCaller autoCaller(this);
420 AssertComRCReturnRC(autoCaller.rc());
421
422 AutoWriteLock alock(this);
423
424 /* Note: we assume that the default values for attributes of optional
425 * nodes are assigned in the Data::Data() constructor and don't do it
426 * here. It implies that this method may only be called after constructing
427 * a new BIOSSettings object while all its data fields are in the default
428 * values. Exceptions are fields whose creation time defaults don't match
429 * values that should be applied when these fields are not explicitly set
430 * in the settings file (for backwards compatibility reasons). This takes
431 * place when a setting of a newly created object must default to A while
432 * the same setting of an object loaded from the old settings file must
433 * default to B. */
434
435 HRESULT rc = S_OK;
436
437 /* DVD drive (required, contains either Image or HostDrive or nothing) */
438 Key dvdDriveNode = aMachineNode.key ("DVDDrive");
439
440 /* optional, defaults to false */
441 m->passthrough = dvdDriveNode.value <bool> ("passthrough");
442
443 Key typeNode;
444
445 if (!(typeNode = dvdDriveNode.findKey ("Image")).isNull())
446 {
447 Guid uuid = typeNode.value <Guid> ("uuid");
448 rc = MountImage (uuid.toUtf16());
449 CheckComRCReturnRC(rc);
450 }
451 else if (!(typeNode = dvdDriveNode.findKey ("HostDrive")).isNull())
452 {
453
454 Bstr src = typeNode.stringValue ("src");
455
456 /* find the corresponding object */
457 ComObjPtr<Host> host = mParent->virtualBox()->host();
458
459 com::SafeIfaceArray<IHostDVDDrive> coll;
460 rc = host->COMGETTER(DVDDrives) (ComSafeArrayAsOutParam(coll));
461 AssertComRC (rc);
462
463 ComPtr<IHostDVDDrive> drive;
464 rc = host->FindHostDVDDrive (src, drive.asOutParam());
465
466 if (SUCCEEDED(rc))
467 {
468 rc = CaptureHostDrive (drive);
469 CheckComRCReturnRC(rc);
470 }
471 else if (rc == E_INVALIDARG)
472 {
473 /* the host DVD drive is not currently available. we
474 * assume it will be available later and create an
475 * extra object now */
476 ComObjPtr<HostDVDDrive> hostDrive;
477 hostDrive.createObject();
478 rc = hostDrive->init (src);
479 AssertComRC (rc);
480 rc = CaptureHostDrive (hostDrive);
481 CheckComRCReturnRC(rc);
482 }
483 else if (rc == VBOX_E_OBJECT_NOT_FOUND)
484 {
485 /* dvd drive mapping was not found anymore - can
486 happen when disabling/hiding the drive created by
487 a daemon tools-like program */
488 ComAssertMsgFailedRet(("DVD drive %s does not exist!\n", src.raw()), E_FAIL);
489 }
490 else
491 AssertComRC (rc);
492 }
493
494 return S_OK;
495}
496
497/**
498 * Saves settings to the given machine node.
499 *
500 * @param aMachineNode <Machine> node.
501 *
502 * @note Locks this object for reading.
503 */
504HRESULT DVDDrive::saveSettings (settings::Key &aMachineNode)
505{
506 using namespace settings;
507
508 AssertReturn(!aMachineNode.isNull(), E_FAIL);
509
510 AutoCaller autoCaller(this);
511 AssertComRCReturnRC(autoCaller.rc());
512
513 AutoReadLock alock(this);
514
515 Key node = aMachineNode.createKey ("DVDDrive");
516
517 node.setValue <bool> ("passthrough", !!m->passthrough);
518
519 switch (m->state)
520 {
521 case DriveState_ImageMounted:
522 {
523 Assert (!m->image.isNull());
524
525 Bstr id;
526 HRESULT rc = m->image->COMGETTER(Id) (id.asOutParam());
527 AssertComRC (rc);
528 Assert (!id.isEmpty());
529
530 Key imageNode = node.createKey ("Image");
531 imageNode.setValue <Guid> ("uuid", Guid(id));
532 break;
533 }
534 case DriveState_HostDriveCaptured:
535 {
536 Assert (!m->hostDrive.isNull());
537
538 Bstr name;
539 HRESULT rc = m->hostDrive->COMGETTER(Name) (name.asOutParam());
540 AssertComRC (rc);
541 Assert (!name.isEmpty());
542
543 Key hostDriveNode = node.createKey ("HostDrive");
544 hostDriveNode.setValue <Bstr> ("src", name);
545 break;
546 }
547 case DriveState_NotMounted:
548 /* do nothing, i.e.leave the drive node empty */
549 break;
550 default:
551 ComAssertMsgFailedRet (("Invalid drive state: %d", m->state),
552 E_FAIL);
553 }
554
555 return S_OK;
556}
557
558/**
559 * @note Locks this object for writing.
560 */
561bool DVDDrive::rollback()
562{
563 /* sanity */
564 AutoCaller autoCaller(this);
565 AssertComRCReturn (autoCaller.rc(), false);
566
567 /* we need adep for the state check */
568 Machine::AutoAnyStateDependency adep (mParent);
569 AssertComRCReturn (adep.rc(), false);
570
571 AutoWriteLock alock(this);
572
573 bool changed = false;
574
575 if (m.isBackedUp())
576 {
577 /* we need to check all data to see whether anything will be changed
578 * after rollback */
579 changed = m.hasActualChanges();
580
581 if (changed)
582 {
583 Data *oldData = m.backedUpData();
584
585 if (!m->image.isNull() &&
586 !oldData->image.equalsTo (m->image))
587 {
588 /* detach the current image that will go away after rollback */
589 m->image->detachFrom (mParent->id(), mParent->snapshotId());
590
591 /* unlock the image for reading if the VM is online */
592 if (Global::IsOnline (adep.machineState()))
593 {
594 HRESULT rc = m->image->UnlockRead (NULL);
595 AssertComRC (rc);
596 }
597 }
598
599 if (!oldData->image.isNull() &&
600 !oldData->image.equalsTo (m->image))
601 {
602 /* Reattach from the old image. */
603 HRESULT rc = oldData->image->attachTo(mParent->id(), mParent->snapshotId());
604 AssertComRC (rc);
605 if (Global::IsOnline (adep.machineState()))
606 {
607 /* Lock from the old image. */
608 rc = oldData->image->LockRead (NULL);
609 AssertComRC (rc);
610 }
611 }
612 }
613
614 m.rollback();
615 }
616
617 return changed;
618}
619
620/**
621 * @note Locks this object for writing, together with the peer object (also for
622 * writing) if there is one.
623 */
624void DVDDrive::commit()
625{
626 /* sanity */
627 AutoCaller autoCaller(this);
628 AssertComRCReturnVoid (autoCaller.rc());
629
630 /* sanity too */
631 AutoCaller peerCaller (mPeer);
632 AssertComRCReturnVoid (peerCaller.rc());
633
634 /* lock both for writing since we modify both (mPeer is "master" so locked
635 * first) */
636 AutoMultiWriteLock2 alock (mPeer, this);
637
638 if (m.isBackedUp())
639 {
640 m.commit();
641 if (mPeer)
642 {
643 /* attach new data to the peer and reshare it */
644 mPeer->m.attach (m);
645 }
646 }
647}
648
649/**
650 * @note Locks this object for writing, together with the peer object
651 * represented by @a aThat (locked for reading).
652 */
653void DVDDrive::copyFrom (DVDDrive *aThat)
654{
655 AssertReturnVoid (aThat != NULL);
656
657 /* sanity */
658 AutoCaller autoCaller(this);
659 AssertComRCReturnVoid (autoCaller.rc());
660
661 /* sanity too */
662 AutoCaller thatCaller (aThat);
663 AssertComRCReturnVoid (thatCaller.rc());
664
665 /* peer is not modified, lock it for reading (aThat is "master" so locked
666 * first) */
667 AutoMultiLock2 alock (aThat->rlock(), this->wlock());
668
669 /* this will back up current data */
670 m.assignCopy (aThat->m);
671}
672
673/**
674 * Helper to unmount a drive.
675 *
676 * @note Must be called from under this object's write lock.
677 */
678HRESULT DVDDrive::unmount()
679{
680 AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
681
682 /* we need adep for the state check */
683 Machine::AutoAnyStateDependency adep (mParent);
684 AssertComRCReturn (adep.rc(), E_FAIL);
685
686 if (!m->image.isNull())
687 {
688 HRESULT rc = m->image->detachFrom (mParent->id(), mParent->snapshotId());
689 AssertComRC (rc);
690 if (Global::IsOnline (adep.machineState()))
691 {
692 rc = m->image->UnlockRead (NULL);
693 AssertComRC (rc);
694 }
695 }
696
697 m.backup();
698
699 if (m->image)
700 m->image.setNull();
701 if (m->hostDrive)
702 m->hostDrive.setNull();
703
704 m->state = DriveState_NotMounted;
705
706 return S_OK;
707}
708
709// private methods
710////////////////////////////////////////////////////////////////////////////////
711
712/* 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