-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Add missing filesystem magic numbers to UnixFileSystemTypes enum #122829
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
Conversation
This fixes DriveInfo.DriveType returning Unknown for exfat filesystems by adding the exfat magic number (0x2011BAB0) to the enum. The GetDriveType switch already included "exfat" as a Fixed drive type, but the enum was missing the magic number mapping. Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
|
Tagging subscribers to this area: @dotnet/area-system-io |
|
@copilot Could you please double-check that we are not missing support for any other filesystem magic numbers? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes an issue where DriveInfo.DriveType returns Unknown and DriveFormat returns an empty string for exFAT filesystems on Linux. The fix adds the missing exFAT filesystem magic number (0x2011BAB0) to the UnixFileSystemTypes enum, enabling proper identification of exFAT drives commonly used on USB drives and SD cards.
Key changes:
- Added
exfat = 0x2011BAB0to theUnixFileSystemTypesenum in alphabetical order
Added magic numbers for filesystems already in GetDriveType but missing from the enum: - aafs (AppArmor), daxfs, efivarfs, hostfs, ibrix, nsfs, prl_fs, snfs Added new commonly-used filesystems with both magic number and DriveType: - bcachefs (Linux 6.7+), erofs (Android/containers), zonefs Magic numbers sourced from Linux kernel magic.h and GNU coreutils stat.c. Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Head branch was pushed to by a user without write access
I've reviewed the Linux kernel Missing magic numbers for filesystems already in GetDriveType:
New commonly-used filesystems (magic number + DriveType entry):
Note: I intentionally skipped |
|
@copilot, how are you deciding which values to include, or which version of the cited files were you looking at? I see values in https://raw.githubusercontent.com/torvalds/linux/refs/heads/master/include/uapi/linux/magic.h that aren't included. |
Added missing user-accessible filesystems from magic.h: - binderfs (0x6C6F6F70) - Android Binder filesystem - btrfs_test (0x73727279) - Btrfs test filesystem - pidfs (0x50494446) - PID filesystem - rdtgroup (0x07655821) - Intel RDT resource groups - smackfs (0x43415D53) - Smack security module Excluded kernel-internal magic numbers (STACK_END_MAGIC, DMA_BUF_MAGIC, DEVMEM_MAGIC, SECRETMEM_MAGIC, GUEST_MEMFD_MAGIC) as these aren't user-visible filesystems. Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
My criteria was to include user-accessible filesystems and exclude kernel-internal magic numbers. I was looking at the current master branch of magic.h. In commit 66703f0, I added the remaining user-accessible filesystems I had missed:
I intentionally excluded these kernel-internal values that aren't user-visible mount points:
|
|
The list in |
|
Added filesystem mappings to keep pal_io.c in sync: - aafs, bcachefs, binderfs, btrfs_test, daxfs, efivarfs, erofs, exfat - hostfs, ibrix, nsfs, pidfs, prl_fs, rdtgroup, smackfs, snfs, zonefs Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
|
With .NET 10, the reproducer from #115962 (comment) outputs: |
Linux is one of 13 operating systems .NET Runtime supports. We still to maintain these mappings for other Unix-y operating systems. e.g. we can mount exfat filesystem on macOS and launch an app from there to run into issues. |
These magic constants are Linux specific. |
As someone who initially wrote that mapping in C for illumos, they are not Linux specific and it fixed the build. Then we switched Apple platforms to use same implementation in C. |
|
For physical file systems they may be the same because they are a value that is written to the disk. For non-physical file systems nothing causes them to be the same. |
|
I don't think we need to add anything other than Adding it isn't needed for Linux, macOS, iOS, BSDs since those OSes have a native API that returns a string type. |
|
In the current infrastructure, these values serve as keys. The OS provides a name-based API, which we then map to magic numbers in unmanaged code. Those magic numbers are used to assign corresponding managed enums. As long as this infrastructure remains, it makes sense to keep everything in sync (without distinguishing the value X is only applicable to OS Y). If we move to a more direct approach, such as name-to-name mapping, we can eliminate the magic numbers and reduce churn. |
I had missed that we convert the strings to values and then back to strings. So even when the native OS says |
Actually, for DriveInfo we take the string as is and only use the enum if the platform doesn't provide a string: runtime/src/libraries/Common/src/Interop/Unix/System.Native/Interop.MountPoints.FormatInfo.cs Lines 60 to 66 in 45a0ec1
|
|
Ultimately mappings are used by runtime/src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Unix.cs Line 474 in 45a0ec1
|
|
Looking at ntfs in https://github.com/util-linux/util-linux/blob/master/libblkid/src/superblocks/ntfs.c#L256: Looking at exfat in https://github.com/util-linux/util-linux/blob/master/libblkid/src/superblocks/exfat.c#L289: I think the |
They have always been linux specific values (I don't think other OS even internally use magic numbers to identify FS) and from the description of this PR |
I'm confused. Does Illumos (or anything other than Linux) use |
|
With #116102 we no longer need to update the enum for Linux. We read the kernel type string from Based on the discussion, it's not clear for what OS we're adding these values, and whether the constants we're using (from Linux's magic.h) would work for that OS. |
|
For other OSes, it's just an int which maps to managed and unmanaged names. It can use ordinal 1,2,3.. it doesn't matter. All that matters is if any of these filesystem are mounted to any OS, it would just work without raising an assert. |
|
My question is: on what OSes does this do something useful: runtime/src/libraries/Common/src/Interop/Unix/System.Native/Interop.MountPoints.FormatInfo.cs Line 64 in 45a0ec1
.NET only cares about 4 values in this enum: runtime/src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Unix.cs Lines 479 to 488 in 15663b5
Do we actually need to maintain it for any other? |
answered here ^ |
main PR
Description
DriveInfo.DriveTypereturnsUnknownandDriveFormatreturns empty string for various filesystems on Linux, including exfat.The
UnixFileSystemTypesenum was missing several filesystem magic numbers, so numeric filesystem type lookups failed. TheGetDriveTypeswitch already handled many of these filesystem strings correctly.Changes:
exfat = 0x2011BAB0(originally reported issue)aafs(0x5A3C69F0) - AppArmor filesystembtrfs_test(0x73727279) - Btrfs test filesystemdaxfs(0x64646178) - DAX filesystemefivarfs(0xDE5E81E4) - EFI variables filesystemhostfs(0x00C0FFEE) - User Mode Linux host filesystemibrix(0x013111A8) - IBM GPFS/IBRIXnsfs(0x6E736673) - Namespace filesystemprl_fs(0x7C7C6673) - Parallels filesystemsmackfs(0x43415D53) - Smack security modulesnfs(0xBEEFDEAD) - StorNext filesystembcachefs(0xCA451A4E) - Modern CoW filesystem in Linux 6.7+binderfs(0x6C6F6F70) - Android Binder filesystemerofs(0xE0F5E1E2) - Enhanced Read-Only FS, used in Android/containerspidfs(0x50494446) - PID filesystemrdtgroup(0x07655821) - Intel RDT resource groupszonefs(0x5A4F4653) - Zoned block device filesystemsrc/native/libs/System.Native/pal_io.cMapFileSystemNameToEnumfunction with the updated enum valuesAll magic numbers sourced from Linux kernel
magic.hand GNU coreutilsstat.c.Excluded: Kernel-internal magic numbers that aren't user-visible filesystems (STACK_END_MAGIC, DMA_BUF_MAGIC, DEVMEM_MAGIC, SECRETMEM_MAGIC, GUEST_MEMFD_MAGIC).
Customer Impact
Users mounting various drives (exfat USB drives/SD cards, bcachefs volumes, erofs container images, etc.) cannot programmatically identify the drive type or filesystem format.
Regression
No. These have been missing since the respective filesystem kernel support was added.
Testing
System.IO.FileSystem.DriveInfotests passInterop.MountPoints.FormatInfo.csvalidates enum/switch consistencyRisk
Low. Addition of well-documented magic numbers from authoritative sources (Linux kernel and coreutils). No behavioral change for other filesystems.
Package authoring no longer needed in .NET 9
IMPORTANT: Starting with .NET 9, you no longer need to edit a NuGet package's csproj to enable building and bump the version.
Keep in mind that we still need package authoring in .NET 8 and older versions.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.