-
Notifications
You must be signed in to change notification settings - Fork 1.6k
virtiofs: add support for mounting directories (not just full volumes) #14073
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+400
−213
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -298,70 +298,12 @@ try | |
| { | ||
| return MountFilesystem(DRVFS_FS_TYPE, Source, Target, Options, ExitCode); | ||
| } | ||
|
|
||
| // Use virtiofs if the source of the mount is the root of a drive; otherwise, use 9p. | ||
| if (WSL_USE_VIRTIO_FS(Config)) | ||
| { | ||
| if (wsl::shared::string::IsDriveRoot(Source)) | ||
| { | ||
| return MountVirtioFs(Source, Target, Options, Admin, Config, ExitCode); | ||
| } | ||
|
|
||
| LOG_WARNING("virtiofs is only supported for mounting full drives, using 9p to mount {}", Source); | ||
| } | ||
|
|
||
| // | ||
| // Check if the path is a UNC path. | ||
| // | ||
|
|
||
| const char* Plan9Source; | ||
| std::string UncSource; | ||
| if ((strlen(Source) >= PLAN9_UNC_PREFIX_LENGTH) && ((Source[0] == '/') || (Source[0] == '\\')) && | ||
| ((Source[1] == '/') || (Source[1] == '\\'))) | ||
| else if (WSL_USE_VIRTIO_FS(Config)) | ||
| { | ||
| UncSource = PLAN9_UNC_TRANSLATED_PREFIX; | ||
| UncSource += &Source[PLAN9_UNC_PREFIX_LENGTH]; | ||
| Plan9Source = UncSource.c_str(); | ||
| } | ||
| else | ||
| { | ||
| Plan9Source = Source; | ||
| return MountVirtioFs(Source, Target, Options, Admin, Config, ExitCode); | ||
| } | ||
|
|
||
| // | ||
| // Check whether to use the elevated or regular 9p server. | ||
| // | ||
|
|
||
| bool Elevated = Admin.has_value() ? Admin.value() : IsDrvfsElevated(); | ||
|
|
||
| // | ||
| // Initialize mount options. | ||
| // | ||
|
|
||
| auto Plan9Options = std::format("{};path={}", PLAN9_ANAME_DRVFS, Plan9Source); | ||
|
|
||
| // | ||
| // N.B. The cache option is added to the start of this so if the user | ||
| // specifies one explicitly, it will override the default. | ||
| // | ||
|
|
||
| std::string MountOptions = "cache=mmap,"; | ||
| auto ParsedOptions = ConvertDrvfsMountOptionsToPlan9(Options ? Options : "", Config); | ||
| Plan9Options += ParsedOptions.first; | ||
| MountOptions += ParsedOptions.second; | ||
|
|
||
| // | ||
| // Append the 9p mount options to the end of the other mount options and perform the mount operation. | ||
| // | ||
|
|
||
| MountOptions += Plan9Options; | ||
|
|
||
| if (MountPlan9Filesystem(Source, Target, MountOptions.c_str(), Elevated, Config, ExitCode) < 0) | ||
| { | ||
| return -1; | ||
| } | ||
|
|
||
| return 0; | ||
| return MountPlan9(Source, Target, Options, Admin, Config, ExitCode); | ||
| } | ||
| CATCH_RETURN_ERRNO() | ||
|
|
||
|
|
@@ -407,7 +349,7 @@ Return Value: | |
| return ExitCode; | ||
| } | ||
|
|
||
| int MountPlan9Filesystem(const char* Source, const char* Target, const char* Options, bool Admin, const wsl::linux::WslDistributionConfig& Config, int* ExitCode) | ||
| int MountPlan9Share(const char* Source, const char* Target, const char* Options, bool Admin, const wsl::linux::WslDistributionConfig& Config, int* ExitCode) | ||
|
|
||
| /*++ | ||
|
|
@@ -425,6 +367,8 @@ Routine Description: | |
| Admin - Supplies a boolean specifying if the admin share should be used. | ||
| Config - Supplies the distribution configuration. | ||
| ExitCode - Supplies an optional pointer that receives the exit code. | ||
| Return Value: | ||
|
|
@@ -452,10 +396,95 @@ Return Value: | |
|
|
||
| MountOptions = | ||
| std::format("msize={},trans=fd,rfdno={},wfdno={},{}", LX_INIT_UTILITY_VM_PLAN9_BUFFER_SIZE, Fd.get(), Fd.get(), Options); | ||
|
|
||
| return MountFilesystem(PLAN9_FS_TYPE, Source, Target, MountOptions.c_str(), ExitCode); | ||
| } | ||
| } | ||
|
|
||
| int MountPlan9(const char* Source, const char* Target, const char* Options, std::optional<bool> Admin, const wsl::linux::WslDistributionConfig& Config, int* ExitCode) | ||
|
|
||
| /*++ | ||
| Routine Description: | ||
| This routine will perform a DrvFs mount using Plan9. | ||
| Arguments: | ||
| Source - Supplies the mount source. | ||
| Target - Supplies the mount target. | ||
| Options - Supplies the mount options. | ||
| Admin - Supplies an optional boolean to specify if the admin or non-admin share should be used. | ||
| Config - Supplies the distribution configuration. | ||
| ExitCode - Supplies an optional pointer that receives the exit code. | ||
| Return Value: | ||
| 0 on success, -1 on failure. | ||
| --*/ | ||
|
|
||
| try | ||
| { | ||
| // | ||
| // Check if the path is a UNC path. | ||
| // | ||
|
|
||
| const char* Plan9Source; | ||
| std::string UncSource; | ||
| if ((strlen(Source) >= PLAN9_UNC_PREFIX_LENGTH) && ((Source[0] == '/') || (Source[0] == '\\')) && | ||
| ((Source[1] == '/') || (Source[1] == '\\'))) | ||
| { | ||
| UncSource = PLAN9_UNC_TRANSLATED_PREFIX; | ||
| UncSource += &Source[PLAN9_UNC_PREFIX_LENGTH]; | ||
| Plan9Source = UncSource.c_str(); | ||
| } | ||
| else | ||
| { | ||
| Plan9Source = Source; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This means Plan9Source has the potential of being null / empty. Could this cause issues with the "path=" option being set up on line 462? |
||
| } | ||
|
|
||
| // | ||
| // Check whether to use the elevated or regular 9p server. | ||
| // | ||
|
|
||
| bool Elevated = Admin.has_value() ? Admin.value() : IsDrvfsElevated(); | ||
|
|
||
| // | ||
| // Initialize mount options. | ||
| // | ||
|
|
||
| auto Plan9Options = std::format("{};path={}", PLAN9_ANAME_DRVFS, Plan9Source); | ||
|
|
||
| // | ||
| // N.B. The cache option is added to the start of this so if the user | ||
| // specifies one explicitly, it will override the default. | ||
| // | ||
|
|
||
| std::string MountOptions = "cache=mmap,"; | ||
| auto ParsedOptions = ConvertDrvfsMountOptionsToPlan9(Options ? Options : "", Config); | ||
| Plan9Options += ParsedOptions.first; | ||
| MountOptions += ParsedOptions.second; | ||
|
|
||
| // | ||
| // Append the 9p mount options to the end of the other mount options and perform the mount operation. | ||
| // | ||
|
|
||
| MountOptions += Plan9Options; | ||
| if (MountPlan9Share(Source, Target, MountOptions.c_str(), Elevated, Config, ExitCode) < 0) | ||
benhillis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| return -1; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
| CATCH_RETURN_ERRNO() | ||
|
|
||
| int MountVirtioFs(const char* Source, const char* Target, const char* Options, std::optional<bool> Admin, const wsl::linux::WslDistributionConfig& Config, int* ExitCode) | ||
|
|
||
| /*++ | ||
|
|
@@ -486,8 +515,6 @@ Return Value: | |
|
|
||
| try | ||
| { | ||
| assert(wsl::shared::string::IsDriveRoot(Source)); | ||
|
|
||
| // | ||
| // Check whether to use the elevated or non-elevated virtiofs server. | ||
| // | ||
|
|
@@ -516,7 +543,7 @@ try | |
| AddShare.WriteString(AddShare->OptionsOffset, Plan9Options); | ||
|
|
||
| // | ||
| // Connect to the wsl service to add the virtiofs share. | ||
| // Connect to the wsl service to add the virtiofs share. If adding the share fails, fallback to mounting using Plan9. | ||
| // | ||
|
|
||
| wsl::shared::SocketChannel Channel{UtilConnectVsock(LX_INIT_UTILITY_VM_VIRTIOFS_PORT, true), "VirtoFs"}; | ||
|
|
@@ -527,11 +554,10 @@ try | |
|
|
||
| gsl::span<gsl::byte> ResponseSpan; | ||
| const auto& Response = Channel.Transaction<LX_INIT_ADD_VIRTIOFS_SHARE_MESSAGE>(AddShare.Span(), &ResponseSpan); | ||
|
|
||
| if (Response.Result != 0) | ||
| { | ||
| LOG_ERROR("Add virtiofs share for {} failed {}", Source, Response.Result); | ||
| return -1; | ||
| LOG_WARNING("Add virtiofs share for {} failed {}, falling back to Plan9", Source, Response.Result); | ||
| return MountPlan9(Source, Target, Options, Admin, Config, ExitCode); | ||
benhillis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // | ||
|
|
@@ -596,3 +622,52 @@ try | |
| return MountWithRetry(Tag, Target, VIRTIO_FS_TYPE, Options); | ||
| } | ||
| CATCH_RETURN_ERRNO() | ||
|
|
||
| std::string QueryVirtiofsMountSource(const char* Tag) | ||
|
|
||
| /*++ | ||
| Routine Description: | ||
| This routine takes a virtiofs tag and determines the Windows path it refers to. | ||
| Arguments: | ||
| Tag - Supplies the virtiofs tag to query. | ||
| Return Value: | ||
| The mount source, an empty string on failure. | ||
| --*/ | ||
|
|
||
| try | ||
| { | ||
| wsl::shared::MessageWriter<LX_INIT_QUERY_VIRTIOFS_SHARE_MESSAGE> QueryShare(LxInitMessageQueryVirtioFsDevice); | ||
| QueryShare.WriteString(QueryShare->TagOffset, Tag); | ||
|
|
||
| // | ||
| // Connect to the host and send the query request. | ||
| // | ||
|
|
||
| wsl::shared::SocketChannel Channel{UtilConnectVsock(LX_INIT_UTILITY_VM_VIRTIOFS_PORT, true), "QueryVirtioFs"}; | ||
| if (Channel.Socket() < 0) | ||
| { | ||
| return {}; | ||
| } | ||
|
|
||
| gsl::span<gsl::byte> ResponseSpan; | ||
| const auto& Response = Channel.Transaction<LX_INIT_QUERY_VIRTIOFS_SHARE_MESSAGE>(QueryShare.Span(), &ResponseSpan); | ||
| if (Response.Result != 0) | ||
| { | ||
| LOG_ERROR("Query virtiofs share for {} failed {}", Tag, Response.Result); | ||
| return {}; | ||
| } | ||
|
|
||
| return wsl::shared::string::FromSpan(ResponseSpan, Response.TagOffset); | ||
| } | ||
| catch (...) | ||
| { | ||
| LOG_CAUGHT_EXCEPTION(); | ||
| return {}; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.