On Fri, Apr 3, 2020 at 10:30 PM J. Bruce Fields <bfields@xxxxxxxxxxxx> wrote: > > On Fri, Apr 03, 2020 at 05:12:23PM +0200, Lennart Poettering wrote: > > BTW, while we are at it: one more thing I'd love to see exposed by > > statx() is a simple flag whether the inode is a mount point. There's > > plenty code that implements a test like this all over the place, and > > it usually isn't very safe. There's one implementation in util-linux > > for example (in the /usr/bin/mountpoint binary), and another one in > > systemd. Would be awesome to just have a statx() return flag for that, > > that would make things *so* much easier and more robust. because in > > fact most code isn't very good that implements this, as much of it > > just compares st_dev of the specified file and its parent. Better code > > compares the mount ID, but as mentioned that's not as pretty as it > > could be so far... > > nfs-utils/support/misc/mountpoint.c:check_is_mountpoint() stats the file > and ".." and returns true if they have different st_dev or the same > st_ino. Comparing mount ids sounds better. > > So anyway, yes, everybody reinvents the wheel here, and this would be > useful. (And, yes, we want to know for the vfsmount, we don't care > whether the same inode is used as a mountpoint someplace else.) Attaching a patch. There's some ambiguity about what is a "mountpoint" and what these tools are interested in. My guess is that they are not interested in an object being a mount point (something where another object is mounted) but being a mount root (this is the object mounted at the mount point). I.e fd = open("/mnt", O_PATH); mount("/bin", "/mnt", NULL, MS_BIND, NULL); statx(AT_FDCWD, "/mnt", 0, 0, &stx1); statx(fd, "", AT_EMPTY_PATH, 0, &stx2); printf("mount_root(/mnt) = %c, mount_root(fd) = %c\n", stx1.stx_attributes & STATX_ATTR_MOUNT_ROOT ? 'y' : 'n', stx2.stx_attributes & STATX_ATTR_MOUNT_ROOT ? 'y' : 'n'); Would print: mount_root(/mnt) = y, mount_root(fd) = n Thanks, Miklos
From: Miklos Szeredi <mszeredi@xxxxxxxxxx> Subject: statx: add mount_root Determining whether a path or file descriptor refers to a mountpoint (or more precisely a mount root) is not trivial using current tools. Add a flag to statx that indicates whether the path or fd refers to the root of a mount or not. Reported-by: Lennart Poettering <mzxreary@xxxxxxxxxxx> Reported-by: J. Bruce Fields <bfields@xxxxxxxxxxxx> Signed-off-by: Miklos Szeredi <mszeredi@xxxxxxxxxx> --- fs/stat.c | 3 +++ include/uapi/linux/stat.h | 1 + 2 files changed, 4 insertions(+) --- a/include/uapi/linux/stat.h +++ b/include/uapi/linux/stat.h @@ -172,6 +172,7 @@ struct statx { #define STATX_ATTR_NODUMP 0x00000040 /* [I] File is not to be dumped */ #define STATX_ATTR_ENCRYPTED 0x00000800 /* [I] File requires key to decrypt in fs */ #define STATX_ATTR_AUTOMOUNT 0x00001000 /* Dir: Automount trigger */ +#define STATX_ATTR_MOUNT_ROOT 0x00002000 /* Root of a mount */ #define STATX_ATTR_VERITY 0x00100000 /* [I] Verity protected file */ --- a/fs/stat.c +++ b/fs/stat.c @@ -202,6 +202,9 @@ int vfs_statx(int dfd, const char __user error = vfs_getattr(&path, stat, request_mask, flags); stat->mnt_id = real_mount(path.mnt)->mnt_id; stat->result_mask |= STATX_MNT_ID; + if (path.mnt->mnt_root == path.dentry) + stat->attributes |= STATX_ATTR_MOUNT_ROOT; + stat->attributes_mask |= STATX_ATTR_MOUNT_ROOT; path_put(&path); if (retry_estale(error, lookup_flags)) { lookup_flags |= LOOKUP_REVAL;