Repository navigation
Conversation
Since Linux v5.8, statx(2) reports STATX_ATTR_MOUNT_ROOT attribute, which tells whether the path is a root of a mount. This works for all kinds of mounts, including bind mounts, and requires a single syscall. Use it as the first method in Mounted and MountedFast, falling back to existing methods (openat2, stat, mountinfo) if statx fails for any reason, or the kernel does not report STATX_ATTR_MOUNT_ROOT. The errors are not returned from statx to keep the existing error semantics (those come from normalizePath used by the fallback code). Since the kernel resolves symlinks, there is no need to normalize the path (which is relatively costly, as filepath.EvalSymlinks does lstat on every path component), only to make it absolute and clean (so that ".." is handled the same way as before). AT_NO_AUTOMOUNT is used to avoid triggering an automount, which is consistent with the other methods (and avoids side effects). As a result, Mounted is about 10x faster (523 vs 5056 ns/op on a path with 6 components). Add the new method to the tests. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Since Linux v5.8, statx(2) reports the
STATX_ATTR_MOUNT_ROOTattribute, which tells whether a path is the root of a mount. It works for all kinds of mounts, including bind mounts, and takes a single syscall.This PR makes it the first method tried by
MountedandMountedFast:filepath.Abs), not fully normalized: the kernel resolves symlinks itself, andfilepath.EvalSymlinks(an lstat per path component) was the most expensive part ofMounted...is still handled lexically, as before.AT_NO_AUTOMOUNTis used so that checking an automount point does not trigger the automount; this is consistent with the existing methods (verified with/sys/kernel/debug/tracingon tracefs).STATX_ATTR_MOUNT_ROOT, the existing chain (openat2 → stat → mountinfo) is used, so returned errors are unchanged.Result:
Mountedis about 10x faster (523 vs 5056 ns/op for a path with 6 components).Tests:
mountedByStatxis added toTestMountedBy(which covers bind mounts of dirs, files and sockets, symlinks, etc. when run as root), and a newTestMountedByStatxVsMountinfochecks it against every entry in mountinfo. Tested on Linux 7.3 (as user) and 7.2 (as root).