From 68a4fc8b5386646302af5bf35ac9b22d13501b77 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 22 Jul 2023 01:31:16 +0900 Subject: [PATCH] fd-util: do not call statx() twice when it does not provide mount ID Previously, in path_is_root_at(), if statx() does not provide mount ID, path_get_mnt_id_at() was called, but it also calls statx(). Let's avoid the second trial. --- src/basic/fd-util.c | 4 ++-- src/basic/mountpoint-util.c | 20 ++++++++++++++------ src/basic/mountpoint-util.h | 1 + 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/basic/fd-util.c b/src/basic/fd-util.c index 1c67e785aa3..1730c9ab2d2 100644 --- a/src/basic/fd-util.c +++ b/src/basic/fd-util.c @@ -935,7 +935,7 @@ int path_is_root_at(int dir_fd, const char *path) { if (!FLAGS_SET(st.nsx.stx_mask, STATX_MNT_ID)) { int mntid; - r = path_get_mnt_id_at(dir_fd, "", &mntid); + r = path_get_mnt_id_at_fallback(dir_fd, "", &mntid); if (r < 0) { if (ERRNO_IS_NOT_SUPPORTED(r)) return true; /* skip the mount ID check */ @@ -950,7 +950,7 @@ int path_is_root_at(int dir_fd, const char *path) { if (!FLAGS_SET(pst.nsx.stx_mask, STATX_MNT_ID)) { int mntid; - r = path_get_mnt_id_at(dir_fd, "..", &mntid); + r = path_get_mnt_id_at_fallback(dir_fd, "..", &mntid); if (r < 0) { if (ERRNO_IS_NOT_SUPPORTED(r)) return true; /* skip the mount ID check */ diff --git a/src/basic/mountpoint-util.c b/src/basic/mountpoint-util.c index d7b182f6dd0..5af0896ad69 100644 --- a/src/basic/mountpoint-util.c +++ b/src/basic/mountpoint-util.c @@ -359,9 +359,21 @@ int path_is_mount_point(const char *t, const char *root, int flags) { return fd_is_mount_point(fd, last_path_component(t), flags); } +int path_get_mnt_id_at_fallback(int dir_fd, const char *path, int *ret) { + int r; + + assert(dir_fd >= 0 || dir_fd == AT_FDCWD); + assert(ret); + + r = name_to_handle_at_loop(dir_fd, path, NULL, ret, isempty(path) ? AT_EMPTY_PATH : 0); + if (r == 0 || is_name_to_handle_at_fatal_error(r)) + return r; + + return fd_fdinfo_mnt_id(dir_fd, path, isempty(path) ? AT_EMPTY_PATH : 0, ret); +} + int path_get_mnt_id_at(int dir_fd, const char *path, int *ret) { STRUCT_NEW_STATX_DEFINE(buf); - int r; assert(dir_fd >= 0 || dir_fd == AT_FDCWD); assert(ret); @@ -386,11 +398,7 @@ int path_get_mnt_id_at(int dir_fd, const char *path, int *ret) { return 0; } - r = name_to_handle_at_loop(dir_fd, path, NULL, ret, isempty(path) ? AT_EMPTY_PATH : 0); - if (r == 0 || is_name_to_handle_at_fatal_error(r)) - return r; - - return fd_fdinfo_mnt_id(dir_fd, path, isempty(path) ? AT_EMPTY_PATH : 0, ret); + return path_get_mnt_id_at_fallback(dir_fd, path, ret); } bool fstype_is_network(const char *fstype) { diff --git a/src/basic/mountpoint-util.h b/src/basic/mountpoint-util.h index 8c4cee2f544..846015c43c7 100644 --- a/src/basic/mountpoint-util.h +++ b/src/basic/mountpoint-util.h @@ -37,6 +37,7 @@ int name_to_handle_at_loop(int fd, const char *path, struct file_handle **ret_handle, int *ret_mnt_id, int flags); +int path_get_mnt_id_at_fallback(int dir_fd, const char *path, int *ret); int path_get_mnt_id_at(int dir_fd, const char *path, int *ret); static inline int path_get_mnt_id(const char *path, int *ret) { return path_get_mnt_id_at(AT_FDCWD, path, ret); -- 2.47.3