Skip to content
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

helpers.linux.fs: add dentry_path_first_mount() #426

Merged
merged 1 commit into from
Sep 18, 2024

Conversation

brenns10
Copy link
Contributor

This is a bit of a drive-by helper idea, inspired by something we have in drgn-tools. We frequently want a way to just convert a dentry to a path even if we don't know the vfsmount associated with the path we want. This helper is a good way to do it. Let me know your thoughts!

Copy link
Owner

@osandov osandov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love the idea, but I'm not a fan of the name. One option would be to overload d_path even more so that it can take a dentry with no mount:

def d_path(  # type: ignore  # Need positional-only arguments.
    arg1: Object, arg2: Optional[Object] = None
) -> bytes:
    if arg2 is None:
        try:
            mnt = container_of(arg1.mnt, "struct mount", "mnt")
        except AttributeError:
            dentry = arg1
            mnt = ... your code to find the first mount
        else:
            dentry = arg1.dentry.read_()
    else:
        mnt = container_of(arg1, "struct mount", "mnt")
        dentry = arg2.read_()

(And in that case, maybe underspecify which mount we choose in case we want a different heuristic later.)

What do you think?

drgn/helpers/linux/fs.py Outdated Show resolved Hide resolved
@brenns10
Copy link
Contributor Author

What do you think?

That's even better! I am not 100% attached to the specific heuristic either. It's really just a way to get any path for a dentry that goes all the way down to the root filesystem. I'll update this, probably tomorrow though. Thanks!

There are some diagnostic use cases where all one has is a dentry and no
vfsmount. This is technically ambiguous, because a superblock may be
mounted in several places due to bind mounts, filesystem namespaces,
etc. The dentry's full path would depend on the specific mount point.

But when we're doing debugging, we frequently just want any
representative filesystem path for the dentry. It turns out that the
kernel always puts new mountpoints at the end of the superblock's list
of mounts, so the first one is likely to be the most relevant anyway.
Thus, arbitrarily choosing this first mountpoint is a good way to get a
representative path. Update d_path() to accept a single dentry as well.

Signed-off-by: Stephen Brennan <[email protected]>
@brenns10 brenns10 force-pushed the dentry_path_first_mount branch from b2372a8 to d4e5d23 Compare August 15, 2024 18:45
@osandov osandov merged commit 8488aad into osandov:main Sep 18, 2024
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants