]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
libfrog: add wrappers for file_getattr/file_setattr syscalls
authorAndrey Albershteyn <aalbersh@redhat.com>
Tue, 9 Sep 2025 15:24:36 +0000 (17:24 +0200)
committerAndrey Albershteyn <aalbersh@kernel.org>
Mon, 15 Sep 2025 09:10:05 +0000 (11:10 +0200)
Add wrappers for new file_getattr/file_setattr inode syscalls which will
be used by xfs_quota and xfs_io.

Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Signed-off-by: Andrey Albershteyn <aalbersh@kernel.org>
configure.ac
include/builddefs.in
include/linux.h
libfrog/Makefile
libfrog/file_attr.c [new file with mode: 0644]
libfrog/file_attr.h [new file with mode: 0644]
m4/package_libcdev.m4

index 195ee6dddf6172944d6a1dd0ec070610ae3fbc5a..0ba371c3314704ab4e112ecdb505b97d958b2834 100644 (file)
@@ -156,6 +156,7 @@ AC_PACKAGE_NEED_RCU_INIT
 AC_HAVE_PWRITEV2
 AC_HAVE_COPY_FILE_RANGE
 AC_HAVE_CACHESTAT
+AC_HAVE_FILE_GETATTR
 AC_NEED_INTERNAL_FSXATTR
 AC_NEED_INTERNAL_FSCRYPT_ADD_KEY_ARG
 AC_NEED_INTERNAL_FSCRYPT_POLICY_V2
index 04b4e0880a84b84caa5e311bf6207aee39c22c75..b5aa1640711b64133ee929a96f4618a1d00dce1d 100644 (file)
@@ -97,6 +97,7 @@ HAVE_ZIPPED_MANPAGES = @have_zipped_manpages@
 HAVE_PWRITEV2 = @have_pwritev2@
 HAVE_COPY_FILE_RANGE = @have_copy_file_range@
 HAVE_CACHESTAT = @have_cachestat@
+HAVE_FILE_GETATTR = @have_file_getattr@
 NEED_INTERNAL_FSXATTR = @need_internal_fsxattr@
 NEED_INTERNAL_FSCRYPT_ADD_KEY_ARG = @need_internal_fscrypt_add_key_arg@
 NEED_INTERNAL_FSCRYPT_POLICY_V2 = @need_internal_fscrypt_policy_v2@
@@ -169,6 +170,10 @@ ifeq ($(ENABLE_GETTEXT),yes)
 GCFLAGS += -DENABLE_GETTEXT
 endif
 
+ifeq ($(HAVE_FILE_GETATTR),yes)
+LCFLAGS += -DHAVE_FILE_GETATTR
+endif
+
 # Override these if C++ needs other options
 SANITIZER_CXXFLAGS = $(SANITIZER_CFLAGS)
 GCXXFLAGS = $(GCFLAGS)
index 6e83e073aa2eb4c8c907a0d20a0596d5d9f4629b..cea468d2b9d87131a161e5110c0c11491252dc3f 100644 (file)
@@ -16,6 +16,7 @@
 #include <sys/param.h>
 #include <sys/sysmacros.h>
 #include <sys/stat.h>
+#include <sys/syscall.h>
 #include <inttypes.h>
 #include <malloc.h>
 #include <getopt.h>
@@ -202,6 +203,25 @@ struct fsxattr {
 };
 #endif
 
+/*
+ * Use FILE_ATTR_SIZE_VER0 (linux/fs.h) instead of build system
+ * HAVE_FILE_GETATTR as this header could be included in other places where
+ * HAVE_FILE_GETATTR is not defined (e.g. xfstests's conftest.c in ./configure)
+ */
+#ifndef FILE_ATTR_SIZE_VER0
+/*
+ * We need to define file_attr if it's missing to know how to convert it to
+ * fsxattr
+ */
+struct file_attr {
+       __u32   fa_xflags;
+       __u32   fa_extsize;
+       __u32   fa_nextents;
+       __u32   fa_projid;
+       __u32   fa_cowextsize;
+};
+#endif
+
 #ifndef FS_IOC_FSGETXATTR
 /*
  * Flags for the fsx_xflags field
index 560bad417ee434b3bd7a2c66a45bdddab50f944d..268fa26638d760204bec18712b575ee02edb7889 100644 (file)
@@ -24,6 +24,7 @@ fsproperties.c \
 fsprops.c \
 getparents.c \
 histogram.c \
+file_attr.c \
 list_sort.c \
 linux.c \
 logging.c \
@@ -55,6 +56,7 @@ fsprops.h \
 getparents.h \
 handle_priv.h \
 histogram.h \
+file_attr.h \
 logging.h \
 paths.h \
 projects.h \
diff --git a/libfrog/file_attr.c b/libfrog/file_attr.c
new file mode 100644 (file)
index 0000000..bb51ac6
--- /dev/null
@@ -0,0 +1,121 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2025 Red Hat, Inc.
+ * All Rights Reserved.
+ */
+
+#include "file_attr.h"
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/syscall.h>
+#include <asm/types.h>
+#include <fcntl.h>
+
+static void
+file_attr_to_fsxattr(
+       const struct file_attr  *fa,
+       struct fsxattr          *fsxa)
+{
+       memset(fsxa, 0, sizeof(struct fsxattr));
+
+       fsxa->fsx_xflags = fa->fa_xflags;
+       fsxa->fsx_extsize = fa->fa_extsize;
+       fsxa->fsx_nextents = fa->fa_nextents;
+       fsxa->fsx_projid = fa->fa_projid;
+       fsxa->fsx_cowextsize = fa->fa_cowextsize;
+}
+
+static void
+fsxattr_to_file_attr(
+       const struct fsxattr    *fsxa,
+       struct file_attr        *fa)
+{
+       memset(fa, 0, sizeof(struct file_attr));
+
+       fa->fa_xflags = fsxa->fsx_xflags;
+       fa->fa_extsize = fsxa->fsx_extsize;
+       fa->fa_nextents = fsxa->fsx_nextents;
+       fa->fa_projid = fsxa->fsx_projid;
+       fa->fa_cowextsize = fsxa->fsx_cowextsize;
+}
+
+int
+xfrog_file_getattr(
+       const int               dfd,
+       const char              *path,
+       const struct stat       *stat,
+       struct file_attr        *fa,
+       const unsigned int      at_flags)
+{
+       int                     error;
+       int                     fd;
+       struct fsxattr          fsxa;
+
+#ifdef HAVE_FILE_GETATTR
+       error = syscall(__NR_file_getattr, dfd, path, fa,
+                       sizeof(struct file_attr), at_flags);
+       if (error && errno != ENOSYS)
+               return error;
+
+       if (!error)
+               return error;
+#endif
+
+       if (SPECIAL_FILE(stat->st_mode)) {
+               errno = EOPNOTSUPP;
+               return -1;
+       }
+
+       fd = open(path, O_RDONLY|O_NOCTTY);
+       if (fd == -1)
+               return fd;
+
+       error = ioctl(fd, FS_IOC_FSGETXATTR, &fsxa);
+       close(fd);
+       if (error)
+               return error;
+
+       fsxattr_to_file_attr(&fsxa, fa);
+
+       return error;
+}
+
+int
+xfrog_file_setattr(
+       const int               dfd,
+       const char              *path,
+       const struct stat       *stat,
+       struct file_attr        *fa,
+       const unsigned int      at_flags)
+{
+       int                     error;
+       int                     fd;
+       struct fsxattr          fsxa;
+
+#ifdef HAVE_FILE_GETATTR /* file_get/setattr goes together */
+       error = syscall(__NR_file_setattr, dfd, path, fa,
+                       sizeof(struct file_attr), at_flags);
+       if (error && errno != ENOSYS)
+               return error;
+
+       if (!error)
+               return error;
+#endif
+
+       if (SPECIAL_FILE(stat->st_mode)) {
+               errno = EOPNOTSUPP;
+               return -1;
+       }
+
+       fd = open(path, O_RDONLY|O_NOCTTY);
+       if (fd == -1)
+               return fd;
+
+       file_attr_to_fsxattr(fa, &fsxa);
+
+       error = ioctl(fd, FS_IOC_FSSETXATTR, fa);
+       close(fd);
+
+       return error;
+}
diff --git a/libfrog/file_attr.h b/libfrog/file_attr.h
new file mode 100644 (file)
index 0000000..df9b618
--- /dev/null
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2025 Red Hat, Inc.
+ * All Rights Reserved.
+ */
+#ifndef __LIBFROG_FILE_ATTR_H__
+#define __LIBFROG_FILE_ATTR_H__
+
+#include "linux.h"
+#include <sys/stat.h>
+
+#define SPECIAL_FILE(x) \
+          (S_ISCHR((x)) \
+       || S_ISBLK((x)) \
+       || S_ISFIFO((x)) \
+       || S_ISLNK((x)) \
+       || S_ISSOCK((x)))
+
+int
+xfrog_file_getattr(
+       const int               dfd,
+       const char              *path,
+       const struct stat       *stat,
+       struct file_attr        *fa,
+       const unsigned int      at_flags);
+
+int
+xfrog_file_setattr(
+       const int               dfd,
+       const char              *path,
+       const struct stat       *stat,
+       struct file_attr        *fa,
+       const unsigned int      at_flags);
+
+#endif /* __LIBFROG_FILE_ATTR_H__ */
index 650b8b7be389dd3ead7fe15de69806ddeb294509..ce1ba47264659c252c647181a5da926c7396fa3c 100644 (file)
@@ -282,3 +282,22 @@ AC_DEFUN([AC_PACKAGE_CHECK_LTO],
     AC_SUBST(lto_cflags)
     AC_SUBST(lto_ldflags)
   ])
+
+#
+# Check if we have a file_getattr system call (Linux)
+#
+AC_DEFUN([AC_HAVE_FILE_GETATTR],
+  [AC_MSG_CHECKING([for file_getattr syscall])
+    AC_LINK_IFELSE(
+    [AC_LANG_PROGRAM([[
+#define _GNU_SOURCE
+#include <sys/syscall.h>
+#include <unistd.h>
+  ]], [[
+syscall(__NR_file_getattr, 0, 0, 0, 0, 0);
+  ]])
+    ], have_file_getattr=yes
+       AC_MSG_RESULT(yes),
+       AC_MSG_RESULT(no))
+    AC_SUBST(have_file_getattr)
+  ])