1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
|
/* $Id: MediumAttachmentImpl.cpp $ */
/** @file
* VirtualBox COM class implementation
*/
/*
* Copyright (C) 2006-2023 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
#define LOG_GROUP LOG_GROUP_MAIN_MEDIUMATTACHMENT
#include "MediumAttachmentImpl.h"
#include "MachineImpl.h"
#include "MediumImpl.h"
#include "Global.h"
#include "StringifyEnums.h"
#include "AutoCaller.h"
#include "LoggingNew.h"
#include <iprt/cpp/utils.h>
////////////////////////////////////////////////////////////////////////////////
//
// private member data definition
//
////////////////////////////////////////////////////////////////////////////////
struct BackupableMediumAttachmentData
{
BackupableMediumAttachmentData()
: fImplicit(false)
{ }
ComObjPtr<Medium> pMedium;
/* Since MediumAttachment is not a first class citizen when it
* comes to managing settings, having a reference to the storage
* controller will not work - when settings are changed it will point
* to the old, uninitialized instance. Changing this requires
* substantial changes to MediumImpl.cpp. */
/* Same counts for the assigned bandwidth group */
bool fImplicit;
const Utf8Str strControllerName;
settings::AttachedDevice mData;
};
struct MediumAttachment::Data
{
Data(Machine * const aMachine = NULL)
: pMachine(aMachine),
fIsEjected(false)
{ }
/** Reference to Machine object, for checking mutable state. */
Machine * const pMachine;
/* later: const ComObjPtr<MediumAttachment> mPeer; */
bool fIsEjected;
Backupable<BackupableMediumAttachmentData> bd;
};
// constructor / destructor
/////////////////////////////////////////////////////////////////////////////
DEFINE_EMPTY_CTOR_DTOR(MediumAttachment)
HRESULT MediumAttachment::FinalConstruct()
{
LogFlowThisFunc(("\n"));
return BaseFinalConstruct();
}
void MediumAttachment::FinalRelease()
{
LogFlowThisFuncEnter();
uninit();
BaseFinalRelease();
LogFlowThisFuncLeave();
}
// public initializer/uninitializer for internal purposes only
/////////////////////////////////////////////////////////////////////////////
/**
* Initializes the medium attachment object.
*
* @param aParent Machine object.
* @param aMedium Medium object.
* @param aControllerName Controller the hard disk is attached to.
* @param aPort Port number.
* @param aDevice Device number on the port.
* @param aType Device type.
* @param aImplicit
* @param aPassthrough Whether accesses are directly passed to the host drive.
* @param aTempEject Whether guest-triggered eject results in unmounting the medium.
* @param aNonRotational Whether this medium is non-rotational (aka SSD).
* @param aDiscard Whether this medium supports discarding unused blocks.
* @param aHotPluggable Whether this medium is hot-pluggable.
* @param strBandwidthGroup Bandwidth group.
*/
HRESULT MediumAttachment::init(Machine *aParent,
Medium *aMedium,
const Utf8Str &aControllerName,
LONG aPort,
LONG aDevice,
DeviceType_T aType,
bool aImplicit,
bool aPassthrough,
bool aTempEject,
bool aNonRotational,
bool aDiscard,
bool aHotPluggable,
const Utf8Str &strBandwidthGroup)
{
LogFlowThisFuncEnter();
LogFlowThisFunc(("aParent=%p aMedium=%p aControllerName=%s aPort=%d aDevice=%d aType=%d aImplicit=%d aPassthrough=%d aTempEject=%d aNonRotational=%d aDiscard=%d aHotPluggable=%d strBandwithGroup=%s\n", aParent, aMedium, aControllerName.c_str(), aPort, aDevice, aType, aImplicit, aPassthrough, aTempEject, aNonRotational, aDiscard, aHotPluggable, strBandwidthGroup.c_str()));
if (aType == DeviceType_HardDisk)
AssertReturn(aMedium, E_INVALIDARG);
/* Enclose the state transition NotReady->InInit->Ready */
AutoInitSpan autoInitSpan(this);
AssertReturn(autoInitSpan.isOk(), E_FAIL);
m = new Data();
unconst(m->pMachine) = aParent;
m->bd.allocate();
m->bd->pMedium = aMedium;
m->bd->mData.strBwGroup = strBandwidthGroup;
unconst(m->bd->strControllerName) = aControllerName;
m->bd->mData.lPort = aPort;
m->bd->mData.lDevice = aDevice;
m->bd->mData.deviceType = aType;
m->bd->mData.fPassThrough = aPassthrough;
m->bd->mData.fTempEject = aTempEject;
m->bd->mData.fNonRotational = aNonRotational;
m->bd->mData.fDiscard = aDiscard;
m->bd->fImplicit = aImplicit;
m->bd->mData.fHotPluggable = aHotPluggable;
/* Confirm a successful initialization when it's the case */
autoInitSpan.setSucceeded();
/* Construct a short log name for this attachment. */
i_updateLogName();
LogFlowThisFunc(("LEAVE - %s\n", i_getLogName()));
return S_OK;
}
/**
* Initializes the medium attachment object given another guest object
* (a kind of copy constructor). This object makes a private copy of data
* of the original object passed as an argument.
*/
HRESULT MediumAttachment::initCopy(Machine *aParent, MediumAttachment *aThat)
{
LogFlowThisFunc(("aParent=%p, aThat=%p\n", aParent, aThat));
ComAssertRet(aParent && aThat, E_INVALIDARG);
/* Enclose the state transition NotReady->InInit->Ready */
AutoInitSpan autoInitSpan(this);
AssertReturn(autoInitSpan.isOk(), E_FAIL);
m = new Data(aParent);
/* m->pPeer is left null */
AutoCaller thatCaller(aThat);
AssertComRCReturnRC(thatCaller.hrc());
AutoReadLock thatlock(aThat COMMA_LOCKVAL_SRC_POS);
m->bd.attachCopy(aThat->m->bd);
/* Confirm a successful initialization */
autoInitSpan.setSucceeded();
/* Construct a short log name for this attachment. */
i_updateLogName();
LogFlowThisFunc(("LEAVE - %s\n", i_getLogName()));
return S_OK;
}
/**
* Uninitializes the instance.
* Called from FinalRelease().
*/
void MediumAttachment::uninit()
{
LogFlowThisFunc(("ENTER - %s\n", i_getLogName()));
/* Enclose the state transition Ready->InUninit->NotReady */
AutoUninitSpan autoUninitSpan(this);
if (autoUninitSpan.uninitDone())
return;
m->bd.free();
unconst(m->pMachine) = NULL;
delete m;
m = NULL;
LogFlowThisFuncLeave();
}
// IHardDiskAttachment properties
/////////////////////////////////////////////////////////////////////////////
HRESULT MediumAttachment::getMachine(ComPtr<IMachine> &aMachine)
{
LogFlowThisFuncEnter();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
ComObjPtr<Machine> pMachine(m->pMachine);
pMachine.queryInterfaceTo(aMachine.asOutParam());
LogFlowThisFuncLeave();
return S_OK;
}
HRESULT MediumAttachment::getMedium(ComPtr<IMedium> &aHardDisk)
{
LogFlowThisFuncEnter();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
aHardDisk = m->bd->pMedium;
LogFlowThisFuncLeave();
return S_OK;
}
HRESULT MediumAttachment::getController(com::Utf8Str &aController)
{
LogFlowThisFuncEnter();
/* m->controller is constant during life time, no need to lock */
aController = Utf8Str(m->bd->strControllerName);
LogFlowThisFuncLeave();
return S_OK;
}
HRESULT MediumAttachment::getPort(LONG *aPort)
{
LogFlowThisFuncEnter();
/* m->bd->port is constant during life time, no need to lock */
*aPort = m->bd->mData.lPort;
LogFlowThisFuncLeave();
return S_OK;
}
HRESULT MediumAttachment::getDevice(LONG *aDevice)
{
LogFlowThisFuncEnter();
/* m->bd->device is constant during life time, no need to lock */
*aDevice = m->bd->mData.lDevice;
LogFlowThisFuncLeave();
return S_OK;
}
HRESULT MediumAttachment::getType(DeviceType_T *aType)
{
LogFlowThisFuncEnter();
/* m->bd->type is constant during life time, no need to lock */
*aType = m->bd->mData.deviceType;
LogFlowThisFuncLeave();
return S_OK;
}
HRESULT MediumAttachment::getPassthrough(BOOL *aPassthrough)
{
LogFlowThisFuncEnter();
AutoReadLock lock(this COMMA_LOCKVAL_SRC_POS);
*aPassthrough = m->bd->mData.fPassThrough;
LogFlowThisFuncLeave();
return S_OK;
}
HRESULT MediumAttachment::getTemporaryEject(BOOL *aTemporaryEject)
{
LogFlowThisFuncEnter();
AutoReadLock lock(this COMMA_LOCKVAL_SRC_POS);
*aTemporaryEject = m->bd->mData.fTempEject;
LogFlowThisFuncLeave();
return S_OK;
}
HRESULT MediumAttachment::getIsEjected(BOOL *aEjected)
{
LogFlowThisFuncEnter();
AutoReadLock lock(this COMMA_LOCKVAL_SRC_POS);
*aEjected = m->fIsEjected;
LogFlowThisFuncLeave();
return S_OK;
}
HRESULT MediumAttachment::getNonRotational(BOOL *aNonRotational)
{
LogFlowThisFuncEnter();
AutoReadLock lock(this COMMA_LOCKVAL_SRC_POS);
*aNonRotational = m->bd->mData.fNonRotational;
LogFlowThisFuncLeave();
return S_OK;
}
HRESULT MediumAttachment::getDiscard(BOOL *aDiscard)
{
LogFlowThisFuncEnter();
AutoReadLock lock(this COMMA_LOCKVAL_SRC_POS);
*aDiscard = m->bd->mData.fDiscard;
LogFlowThisFuncLeave();
return S_OK;
}
HRESULT MediumAttachment::getBandwidthGroup(ComPtr<IBandwidthGroup> &aBandwidthGroup)
{
LogFlowThisFuncEnter();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
HRESULT hrc = S_OK;
if (m->bd->mData.strBwGroup.isNotEmpty())
{
ComObjPtr<BandwidthGroup> pBwGroup;
hrc = m->pMachine->i_getBandwidthGroup(m->bd->mData.strBwGroup, pBwGroup, true /* fSetError */);
Assert(SUCCEEDED(hrc)); /* This is not allowed to fail because the existence of the
group was checked when it was attached. */
if (SUCCEEDED(hrc))
pBwGroup.queryInterfaceTo(aBandwidthGroup.asOutParam());
}
LogFlowThisFuncLeave();
return hrc;
}
HRESULT MediumAttachment::getHotPluggable(BOOL *aHotPluggable)
{
LogFlowThisFuncEnter();
AutoReadLock lock(this COMMA_LOCKVAL_SRC_POS);
*aHotPluggable = m->bd->mData.fHotPluggable;
LogFlowThisFuncLeave();
return S_OK;
}
/**
* @note Locks this object for writing.
*/
void MediumAttachment::i_rollback()
{
LogFlowThisFunc(("ENTER - %s\n", i_getLogName()));
/* sanity */
AutoCaller autoCaller(this);
AssertComRCReturnVoid(autoCaller.hrc());
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
m->bd.rollback();
LogFlowThisFunc(("LEAVE - %s\n", i_getLogName()));
}
/**
* @note Locks this object for writing.
*/
void MediumAttachment::i_commit()
{
LogFlowThisFunc(("ENTER - %s\n", i_getLogName()));
/* sanity */
AutoCaller autoCaller(this);
AssertComRCReturnVoid(autoCaller.hrc());
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
if (m->bd.isBackedUp())
m->bd.commit();
LogFlowThisFunc(("LEAVE - %s\n", i_getLogName()));
}
bool MediumAttachment::i_isImplicit() const
{
return m->bd->fImplicit;
}
void MediumAttachment::i_setImplicit(bool aImplicit)
{
Assert(!m->pMachine->i_isSnapshotMachine());
m->bd->fImplicit = aImplicit;
/* Construct a short log name for this attachment. */
i_updateLogName();
}
const ComObjPtr<Medium>& MediumAttachment::i_getMedium() const
{
return m->bd->pMedium;
}
const Utf8Str &MediumAttachment::i_getControllerName() const
{
return m->bd->strControllerName;
}
LONG MediumAttachment::i_getPort() const
{
return m->bd->mData.lPort;
}
LONG MediumAttachment::i_getDevice() const
{
return m->bd->mData.lDevice;
}
DeviceType_T MediumAttachment::i_getType() const
{
return m->bd->mData.deviceType;
}
bool MediumAttachment::i_getPassthrough() const
{
AutoReadLock lock(this COMMA_LOCKVAL_SRC_POS);
return m->bd->mData.fPassThrough;
}
bool MediumAttachment::i_getTempEject() const
{
AutoReadLock lock(this COMMA_LOCKVAL_SRC_POS);
return m->bd->mData.fTempEject;
}
bool MediumAttachment::i_getNonRotational() const
{
AutoReadLock lock(this COMMA_LOCKVAL_SRC_POS);
return m->bd->mData.fNonRotational;
}
bool MediumAttachment::i_getDiscard() const
{
AutoReadLock lock(this COMMA_LOCKVAL_SRC_POS);
return m->bd->mData.fDiscard;
}
bool MediumAttachment::i_getHotPluggable() const
{
AutoReadLock lock(this COMMA_LOCKVAL_SRC_POS);
return m->bd->mData.fHotPluggable;
}
Utf8Str& MediumAttachment::i_getBandwidthGroup() const
{
return m->bd->mData.strBwGroup;
}
bool MediumAttachment::i_matches(const Utf8Str &aControllerName, LONG aPort, LONG aDevice)
{
return ( aControllerName == m->bd->strControllerName
&& aPort == m->bd->mData.lPort
&& aDevice == m->bd->mData.lDevice);
}
/** Must be called from under this object's write lock. */
void MediumAttachment::i_updateName(const Utf8Str &aName)
{
Assert(isWriteLockOnCurrentThread());
Assert(!m->pMachine->i_isSnapshotMachine());
m->bd.backup();
unconst(m->bd->strControllerName) = aName;
/* Construct a short log name for this attachment. */
i_updateLogName();
}
/**
* Sets the medium of this attachment and unsets the "implicit" flag.
* @param aMedium
*/
void MediumAttachment::i_updateMedium(const ComObjPtr<Medium> &aMedium)
{
Assert(isWriteLockOnCurrentThread());
/* No assertion for a snapshot. Method used in deleting snapshot. */
m->bd.backup();
m->bd->pMedium = aMedium;
m->bd->fImplicit = false;
m->fIsEjected = false;
}
/** Must be called from under this object's write lock. */
void MediumAttachment::i_updatePassthrough(bool aPassthrough)
{
Assert(isWriteLockOnCurrentThread());
Assert(!m->pMachine->i_isSnapshotMachine());
m->bd.backup();
m->bd->mData.fPassThrough = aPassthrough;
}
/** Must be called from under this object's write lock. */
void MediumAttachment::i_updateTempEject(bool aTempEject)
{
Assert(isWriteLockOnCurrentThread());
Assert(!m->pMachine->i_isSnapshotMachine());
m->bd.backup();
m->bd->mData.fTempEject = aTempEject;
}
/** Must be called from under this object's write lock. */
void MediumAttachment::i_updateEjected()
{
Assert(isWriteLockOnCurrentThread());
Assert(!m->pMachine->i_isSnapshotMachine());
m->fIsEjected = true;
}
/** Must be called from under this object's write lock. */
void MediumAttachment::i_updateNonRotational(bool aNonRotational)
{
Assert(isWriteLockOnCurrentThread());
Assert(!m->pMachine->i_isSnapshotMachine());
m->bd.backup();
m->bd->mData.fNonRotational = aNonRotational;
}
/** Must be called from under this object's write lock. */
void MediumAttachment::i_updateDiscard(bool aDiscard)
{
Assert(isWriteLockOnCurrentThread());
Assert(!m->pMachine->i_isSnapshotMachine());
m->bd.backup();
m->bd->mData.fDiscard = aDiscard;
}
/** Must be called from under this object's write lock. */
void MediumAttachment::i_updateHotPluggable(bool aHotPluggable)
{
Assert(isWriteLockOnCurrentThread());
Assert(!m->pMachine->i_isSnapshotMachine());
m->bd.backup();
m->bd->mData.fHotPluggable = aHotPluggable;
}
void MediumAttachment::i_updateBandwidthGroup(const Utf8Str &aBandwidthGroup)
{
LogFlowThisFuncEnter();
Assert(isWriteLockOnCurrentThread());
Assert(!m->pMachine->i_isSnapshotMachine());
m->bd.backup();
m->bd->mData.strBwGroup = aBandwidthGroup;
LogFlowThisFuncLeave();
}
void MediumAttachment::i_updateParentMachine(Machine * const pMachine)
{
LogFlowThisFunc(("ENTER - %s\n", i_getLogName()));
/* sanity */
AutoCaller autoCaller(this);
AssertComRCReturnVoid(autoCaller.hrc());
Assert(!m->pMachine->i_isSnapshotMachine());
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
unconst(m->pMachine) = pMachine;
LogFlowThisFunc(("LEAVE - %s\n", i_getLogName()));
}
void MediumAttachment::i_updateLogName()
{
const char *pszName = m->bd->strControllerName.c_str();
const char *pszEndNick = strpbrk(pszName, " \t:-");
mLogName = Utf8StrFmt("MA%p[%.*s:%u:%u:%s%s]",
this,
pszEndNick ? pszEndNick - pszName : 4, pszName,
m->bd->mData.lPort, m->bd->mData.lDevice, ::stringifyDeviceType(m->bd->mData.deviceType),
m->bd->fImplicit ? ":I" : "");
}
|