Skip to content

Commit a43e9e0

Browse files
committed
More duplicate state removal
1 parent 32e8ca1 commit a43e9e0

8 files changed

Lines changed: 184 additions & 175 deletions

‎src/windows/common/HcsVirtualMachineBackend.cpp‎

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,11 @@ VmFileSystemDevice HcsVirtualMachineBackend::CreateFileSystemDevice(const VmFile
10551055
}
10561056

10571057
VmFileSystemDevice device{
1058-
{m_configuration.Description.Identity, m_nextDeviceId}, VmFileSystemDeviceState::Prepared, {}, Request.Transport};
1058+
{m_configuration.Description.Identity, m_nextDeviceId},
1059+
VmFileSystemDeviceState::Prepared,
1060+
{},
1061+
Request.Transport,
1062+
wsl::windows::common::security::IsTokenElevated(userToken.get())};
10591063
std::wstring mountOptions;
10601064
wil::com_ptr<IPlan9FileSystem> plan9Server;
10611065
std::optional<GUID> guestInstanceId;
@@ -1145,16 +1149,17 @@ VmFileSystemDevice HcsVirtualMachineBackend::CreateFileSystemDevice(const VmFile
11451149
return device;
11461150
}
11471151

1148-
std::vector<VmFileSystemDevice> HcsVirtualMachineBackend::GetFileSystemDevices() const
1152+
std::optional<VmFileSystemDevice> HcsVirtualMachineBackend::GetFileSystemDevice(const VmFileSystemDevicePredicate& Predicate) const
11491153
{
11501154
auto lock = m_lock.lock_shared();
1151-
std::vector<VmFileSystemDevice> devices;
1152-
devices.reserve(m_fileSystemDevices.size());
11531155
for (const auto& entry : m_fileSystemDevices)
11541156
{
1155-
devices.push_back(entry.second);
1157+
if (Predicate(entry.second))
1158+
{
1159+
return entry.second;
1160+
}
11561161
}
1157-
return devices;
1162+
return {};
11581163
}
11591164

11601165
VmFileSystemDevice HcsVirtualMachineBackend::GetFileSystemDeviceStatus(VmDeviceId Device)
@@ -1320,6 +1325,7 @@ VmFileSystemShare HcsVirtualMachineBackend::AddFileSystemShare(VmDeviceId Device
13201325
// prefix such as '\\?' so that the guest can mount arbitrary subpaths below it.
13211326
std::wstring hostPath;
13221327
std::wstring mountOptions;
1328+
std::map<std::wstring, std::wstring> effectiveMountOptions;
13231329
std::optional<VmFileSystemShare> reused;
13241330
VmFileSystemShareAddress guestAddress;
13251331
std::visit(
@@ -1342,6 +1348,7 @@ VmFileSystemShare HcsVirtualMachineBackend::AddFileSystemShare(VmDeviceId Device
13421348
shareOptions[L"ro"] = {};
13431349
}
13441350

1351+
effectiveMountOptions = shareOptions;
13451352
mountOptions = FormatVirtioFsMountOptions(shareOptions);
13461353

13471354
// Repeating a request for the same path and options reuses the existing share so that
@@ -1427,6 +1434,8 @@ VmFileSystemShare HcsVirtualMachineBackend::AddFileSystemShare(VmDeviceId Device
14271434
share.Device = Device;
14281435
share.GuestAddress = std::move(guestAddress);
14291436
share.EffectiveHostPath = hostPath;
1437+
share.MountOptions = std::move(effectiveMountOptions);
1438+
share.Elevated = device->second.Elevated;
14301439
share.ReadOnly = Request.ReadOnly;
14311440

14321441
const auto inserted = m_fileSystemShares.emplace(share.Id.Value, FileSystemShare{share, {std::move(mountOptions), userToken}}).second;
@@ -1443,6 +1452,19 @@ VmFileSystemShare HcsVirtualMachineBackend::AddFileSystemShare(VmDeviceId Device
14431452
return share;
14441453
}
14451454

1455+
std::optional<VmFileSystemShare> HcsVirtualMachineBackend::GetFileSystemShare(const VmFileSystemSharePredicate& Predicate) const
1456+
{
1457+
auto lock = m_lock.lock_shared();
1458+
for (const auto& entry : m_fileSystemShares)
1459+
{
1460+
if (Predicate(entry.second))
1461+
{
1462+
return entry.second;
1463+
}
1464+
}
1465+
return {};
1466+
}
1467+
14461468
void HcsVirtualMachineBackend::RemoveFileSystemShare(VmShareId Share)
14471469
{
14481470
WSL_LOG(

‎src/windows/common/HcsVirtualMachineBackend.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ class HcsVirtualMachineBackend : public IVirtualMachineBackend
5454
VmGpuAttachment AddGpu(const VmGpuRequest& Request) override;
5555

5656
VmFileSystemDevice CreateFileSystemDevice(const VmFileSystemDeviceRequest& Request) override;
57-
std::vector<VmFileSystemDevice> GetFileSystemDevices() const override;
57+
std::optional<VmFileSystemDevice> GetFileSystemDevice(const VmFileSystemDevicePredicate& Predicate) const override;
5858
VmFileSystemDevice GetFileSystemDeviceStatus(VmDeviceId Device) override;
5959
VmFileSystemShare AddFileSystemShare(VmDeviceId Device, const VmFileSystemShareRequest& Request) override;
60+
std::optional<VmFileSystemShare> GetFileSystemShare(const VmFileSystemSharePredicate& Predicate) const override;
6061
void RemoveFileSystemShare(VmShareId Share) override;
6162
VmSharedMemoryDevice AddSharedMemory(const VmSharedMemoryRequest& Request) override;
6263
void ConfigureGuestDma(const VmGuestDmaRequest& Request) override;

‎src/windows/common/IVirtualMachineBackend.h‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ struct VmGuestListener
155155
GuestServicePort Port;
156156
std::shared_ptr<VmGuestListenerState> State;
157157

158-
wil::unique_socket Accept() const;
158+
wil::unique_socket Accept(DWORD Timeout = INFINITE, const std::source_location& Location = std::source_location::current()) const;
159+
SOCKET Socket() const;
159160
};
160161

161162
struct VmGuestListenerState
@@ -520,6 +521,7 @@ struct VmFileSystemDevice
520521
// added report a prepared device without a guest instance id.
521522
std::optional<GUID> GuestInstanceId;
522523
VmFileSystemDeviceTransport Transport;
524+
bool Elevated = false;
523525
};
524526

525527
struct VmPlan9ShareOptions
@@ -572,8 +574,13 @@ struct VmFileSystemShare
572574
VmFileSystemShareAddress GuestAddress;
573575
std::filesystem::path EffectiveHostPath;
574576
bool ReadOnly = true;
577+
std::map<std::wstring, std::wstring> MountOptions;
578+
bool Elevated = false;
575579
};
576580

581+
using VmFileSystemDevicePredicate = std::function<bool(const VmFileSystemDevice&)>;
582+
using VmFileSystemSharePredicate = std::function<bool(const VmFileSystemShare&)>;
583+
577584
// Invoked with the index of a newly added persistent memory device while the backend still
578585
// serializes persistent memory additions. Callers that name devices after the order in which the
579586
// guest enumerated them (/dev/pmem<index>) use this to wait for the device to appear before the
@@ -696,9 +703,10 @@ class IVirtualMachineBackend
696703
virtual VmGpuAttachment AddGpu(const VmGpuRequest& Request) = 0;
697704

698705
virtual VmFileSystemDevice CreateFileSystemDevice(const VmFileSystemDeviceRequest& Request) = 0;
699-
virtual std::vector<VmFileSystemDevice> GetFileSystemDevices() const = 0;
706+
virtual std::optional<VmFileSystemDevice> GetFileSystemDevice(const VmFileSystemDevicePredicate& Predicate) const = 0;
700707
virtual VmFileSystemDevice GetFileSystemDeviceStatus(VmDeviceId Device) = 0;
701708
virtual VmFileSystemShare AddFileSystemShare(VmDeviceId Device, const VmFileSystemShareRequest& Request) = 0;
709+
virtual std::optional<VmFileSystemShare> GetFileSystemShare(const VmFileSystemSharePredicate& Predicate) const = 0;
702710
virtual void RemoveFileSystemShare(VmShareId Share) = 0;
703711
virtual VmSharedMemoryDevice AddSharedMemory(const VmSharedMemoryRequest& Request) = 0;
704712
virtual void ConfigureGuestDma(const VmGuestDmaRequest& Request) = 0;

‎src/windows/common/OpenVmmVirtualMachineBackend.cpp‎

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,12 @@ VmFileSystemDevice OpenVmmVirtualMachineBackend::CreateFileSystemDevice(const Vm
767767
wsl::shared::string::IsEqual(std::get<VmVirtioFsDevice>(entry.second.Transport).Tag, transport->Tag, false));
768768
}
769769

770-
VmFileSystemDevice device{{m_description.Identity, m_nextDeviceId}, VmFileSystemDeviceState::Prepared, {}, Request.Transport};
770+
VmFileSystemDevice device{
771+
{m_description.Identity, m_nextDeviceId},
772+
VmFileSystemDeviceState::Prepared,
773+
{},
774+
Request.Transport,
775+
wsl::windows::common::security::IsTokenElevated(m_description.Identity.UserToken.get())};
771776
const auto inserted = m_fileSystemDevices.emplace(device.Id.Value, FileSystemDevice{device, {}}).second;
772777
WI_ASSERT(inserted);
773778
++m_nextDeviceId;
@@ -779,16 +784,17 @@ VmFileSystemDevice OpenVmmVirtualMachineBackend::CreateFileSystemDevice(const Vm
779784
return device;
780785
}
781786

782-
std::vector<VmFileSystemDevice> OpenVmmVirtualMachineBackend::GetFileSystemDevices() const
787+
std::optional<VmFileSystemDevice> OpenVmmVirtualMachineBackend::GetFileSystemDevice(const VmFileSystemDevicePredicate& Predicate) const
783788
{
784789
auto lock = m_lock.lock_shared();
785-
std::vector<VmFileSystemDevice> devices;
786-
devices.reserve(m_fileSystemDevices.size());
787790
for (const auto& entry : m_fileSystemDevices)
788791
{
789-
devices.push_back(entry.second);
792+
if (Predicate(entry.second))
793+
{
794+
return entry.second;
795+
}
790796
}
791-
return devices;
797+
return {};
792798
}
793799

794800
VmFileSystemDevice OpenVmmVirtualMachineBackend::GetFileSystemDeviceStatus(VmDeviceId Device)
@@ -833,7 +839,9 @@ VmFileSystemShare OpenVmmVirtualMachineBackend::AddFileSystemShare(VmDeviceId De
833839
Device,
834840
VmVirtioFsShareAddress{std::get<VmVirtioFsDevice>(device->second.Transport).Tag, {}},
835841
hostPath,
836-
Request.ReadOnly};
842+
Request.ReadOnly,
843+
options->MountOptions,
844+
device->second.Elevated};
837845
const auto& guestAddress = std::get<VmVirtioFsShareAddress>(share.GuestAddress);
838846
const auto [entry, inserted] = m_fileSystemShares.emplace(share.Id.Value, share);
839847
WI_ASSERT(inserted);
@@ -858,6 +866,19 @@ VmFileSystemShare OpenVmmVirtualMachineBackend::AddFileSystemShare(VmDeviceId De
858866
return share;
859867
}
860868

869+
std::optional<VmFileSystemShare> OpenVmmVirtualMachineBackend::GetFileSystemShare(const VmFileSystemSharePredicate& Predicate) const
870+
{
871+
auto lock = m_lock.lock_shared();
872+
for (const auto& entry : m_fileSystemShares)
873+
{
874+
if (Predicate(entry.second))
875+
{
876+
return entry.second;
877+
}
878+
}
879+
return {};
880+
}
881+
861882
void OpenVmmVirtualMachineBackend::RemoveFileSystemShare(VmShareId Share)
862883
{
863884
WSL_LOG(

‎src/windows/common/OpenVmmVirtualMachineBackend.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ class OpenVmmVirtualMachineBackend : public IVirtualMachineBackend
5757
VmGpuAttachment AddGpu(const VmGpuRequest& Request) override;
5858

5959
VmFileSystemDevice CreateFileSystemDevice(const VmFileSystemDeviceRequest& Request) override;
60-
std::vector<VmFileSystemDevice> GetFileSystemDevices() const override;
60+
std::optional<VmFileSystemDevice> GetFileSystemDevice(const VmFileSystemDevicePredicate& Predicate) const override;
6161
VmFileSystemDevice GetFileSystemDeviceStatus(VmDeviceId Device) override;
6262
VmFileSystemShare AddFileSystemShare(VmDeviceId Device, const VmFileSystemShareRequest& Request) override;
63+
std::optional<VmFileSystemShare> GetFileSystemShare(const VmFileSystemSharePredicate& Predicate) const override;
6364
void RemoveFileSystemShare(VmShareId Share) override;
6465
VmSharedMemoryDevice AddSharedMemory(const VmSharedMemoryRequest& Request) override;
6566
void ConfigureGuestDma(const VmGuestDmaRequest& Request) override;

‎src/windows/common/VirtualMachineBackend.cpp‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,12 @@ VmGuestListener IVirtualMachineBackend::RegisterGuestListenerLocked(const VmInst
131131
return listener;
132132
}
133133

134-
wil::unique_socket VmGuestListener::Accept() const
134+
wil::unique_socket VmGuestListener::Accept(DWORD Timeout, const std::source_location& Location) const
135135
{
136136
THROW_HR_IF(E_INVALIDARG, Id.Value == 0 || !State);
137137
WSL_LOG("VmAcceptGuestConnectionBegin", TraceLoggingValue(Id.Owner.VmId, "vmId"), TraceLoggingValue(Id.Value, "listenerId"));
138138

139-
auto socket = wsl::windows::common::socket::CancellableAccept(State->Socket.get(), INFINITE, State->CancellationEvent.get());
139+
auto socket = wsl::windows::common::socket::CancellableAccept(State->Socket.get(), Timeout, State->CancellationEvent.get(), Location);
140140
WSL_LOG(
141141
"VmAcceptGuestConnectionEnd",
142142
TraceLoggingValue(Id.Owner.VmId, "vmId"),
@@ -147,6 +147,12 @@ wil::unique_socket VmGuestListener::Accept() const
147147
return std::move(*socket);
148148
}
149149

150+
SOCKET VmGuestListener::Socket() const
151+
{
152+
THROW_HR_IF(E_INVALIDARG, Id.Value == 0 || !State);
153+
return State->Socket.get();
154+
}
155+
150156
std::shared_ptr<VmGuestListenerState> IVirtualMachineBackend::RemoveGuestListenerLocked(VmListenerId Listener, const VmInstanceId& Identity)
151157
{
152158
WSL_LOG("VmCloseGuestListener", TraceLoggingValue(Identity.VmId, "vmId"), TraceLoggingValue(Listener.Value, "listenerId"));

0 commit comments

Comments
 (0)