From: Karel Zak Date: Thu, 28 May 2026 12:01:53 +0000 (+0200) Subject: fstrim: resolve non-device sources to real block devices X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=310255b275d354a024794fbe37371c74d9c0f096;p=thirdparty%2Futil-linux.git fstrim: resolve non-device sources to real block devices When fstrim reads fstab entries (--fstab, --listed-in), bind mount entries use directory paths as sources (e.g. /data/ssd/ldap) rather than device names. Source de-duplication compares path strings, so these never match the real device paths (e.g. /dev/mapper/foo), causing the same filesystem to be trimmed multiple times. Use statmount(STATMOUNT_SB_SOURCE) to resolve directory source paths to their real block device before de-duplication. Fall back to the original source when statmount() is unavailable. Addresses: https://github.com/util-linux/util-linux/issues/857 Signed-off-by: Karel Zak --- diff --git a/sys-utils/fstrim.c b/sys-utils/fstrim.c index 208e9008e..cd96f77ab 100644 --- a/sys-utils/fstrim.c +++ b/sys-utils/fstrim.c @@ -47,6 +47,7 @@ #include "sysfs.h" #include "optutils.h" #include "statfs_magic.h" +#include "mountutils.h" #include @@ -368,6 +369,26 @@ static int fstrim_all_from_file(struct fstrim_control *ctl, const char *filename continue; /* overlaying mount */ } + /* resolve non-device source (e.g. bind mount directory + * path) to real block device via statmount(); + * requires mountfd support (mountutils.h) */ +#ifdef STATMOUNT_SB_SOURCE + if (is_directory(src, 1)) { + char *orig = xstrdup(src); + + mnt_fs_set_source(fs, NULL); + src = NULL; + if (mnt_fs_fetch_statmount(fs, + STATMOUNT_SB_SOURCE) == 0) + src = mnt_fs_get_srcpath(fs); + if (!src || *src != '/') { + mnt_fs_set_source(fs, orig); + src = mnt_fs_get_srcpath(fs); + } + free(orig); + } +#endif + if (!is_directory(tgt, 1) || !has_discard(src, &wholedisk)) { mnt_table_remove_fs(tab, fs);