From a597bc04de8990dc66e9641c782d6a9fc80a5abe Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Tue, 24 Feb 2026 00:17:26 +0100 Subject: btrfs: add examples on the shutdown experimental feature MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miquel Sabaté Solà --- fs/btrfs/shutdown/shutdown.c | 68 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 fs/btrfs/shutdown/shutdown.c (limited to 'fs/btrfs/shutdown/shutdown.c') diff --git a/fs/btrfs/shutdown/shutdown.c b/fs/btrfs/shutdown/shutdown.c new file mode 100644 index 0000000..46a5731 --- /dev/null +++ b/fs/btrfs/shutdown/shutdown.c @@ -0,0 +1,68 @@ +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * Constants defined in BTRFS' experimental shutdown support + * starting from Linux 6.19. + */ + +#ifndef BTRFS_SHUTDOWN_FLAGS_DEFAULT +#define BTRFS_SHUTDOWN_FLAGS_DEFAULT 0x0 +#endif + +#ifndef BTRFS_IOC_SHUTDOWN +#define BTRFS_IOC_SHUTDOWN _IOR('X', 125, __u32) +#endif + +int main(int argc, char *argv[]) +{ + int fd; + __u32 flags; + struct statfs info; + + if (argc < 2) { + fprintf(stderr, "usage: %s \n", argv[0]); + exit(EXIT_FAILURE); + } + + if (statfs(argv[1], &info) == 0) { + /* + * We could certainly use this ioctl against EXT4 or XFS, but that's not + * the point we want to illustrate here. + */ + if (info.f_type != BTRFS_SUPER_MAGIC) { + printf("error: you are supposed to be running this against BTRFS;" + " %lx magic detected instead\n", info.f_type); + exit(EXIT_FAILURE); + } + } else { + perror("statfs failed"); + exit(EXIT_FAILURE); + } + + fd = open(argv[1], O_RDONLY | O_DIRECTORY); + if (fd < 0) { + perror("open"); + exit(EXIT_FAILURE); + } + + flags = BTRFS_SHUTDOWN_FLAGS_DEFAULT; + if (ioctl(fd, BTRFS_IOC_SHUTDOWN, &flags) == 0) { + printf("Success: filesystem shut down.\n"); + } else { + perror("Shutdown failed"); + exit(EXIT_FAILURE); + } + + close(fd); + return 0; +} -- cgit v1.2.3