1716fd348SMartin Matuska /*
2716fd348SMartin Matuska * This file and its contents are supplied under the terms of the
3716fd348SMartin Matuska * Common Development and Distribution License ("CDDL"), version 1.0.
4716fd348SMartin Matuska * You may only use this file in accordance with the terms of version
5716fd348SMartin Matuska * 1.0 of the CDDL.
6716fd348SMartin Matuska *
7716fd348SMartin Matuska * A full copy of the text of the CDDL should have accompanied this
8716fd348SMartin Matuska * source. A copy of the CDDL is also available via the Internet at
9716fd348SMartin Matuska * http://www.illumos.org/license/CDDL.
10716fd348SMartin Matuska */
11716fd348SMartin Matuska
12716fd348SMartin Matuska /*
13716fd348SMartin Matuska * Copyright 2021 iXsystems, Inc.
14716fd348SMartin Matuska */
15716fd348SMartin Matuska
16716fd348SMartin Matuska /*
17716fd348SMartin Matuska * FreeBSD and macOS expose file generation number through stat(2) and stat(1).
18716fd348SMartin Matuska * Linux exposes it instead through an ioctl.
19716fd348SMartin Matuska */
20716fd348SMartin Matuska
21716fd348SMartin Matuska #include <sys/ioctl.h>
22*718519f4SMartin Matuska #ifdef _KERNEL
23*718519f4SMartin Matuska #include <sys/fcntl.h>
24*718519f4SMartin Matuska #else
25*718519f4SMartin Matuska #include <fcntl.h>
26*718519f4SMartin Matuska #endif
27716fd348SMartin Matuska #include <linux/fs.h>
28716fd348SMartin Matuska #include <err.h>
29716fd348SMartin Matuska #include <stdio.h>
30716fd348SMartin Matuska #include <stdlib.h>
31716fd348SMartin Matuska #include <unistd.h>
32716fd348SMartin Matuska
33716fd348SMartin Matuska int
main(int argc,const char * const argv[])34716fd348SMartin Matuska main(int argc, const char * const argv[])
35716fd348SMartin Matuska {
36716fd348SMartin Matuska if (argc != 2)
37716fd348SMartin Matuska errx(EXIT_FAILURE, "usage: %s filename", argv[0]);
38716fd348SMartin Matuska
39716fd348SMartin Matuska int fd = open(argv[1], O_RDONLY);
40716fd348SMartin Matuska if (fd == -1)
41716fd348SMartin Matuska err(EXIT_FAILURE, "failed to open %s", argv[1]);
42716fd348SMartin Matuska
43716fd348SMartin Matuska int gen = 0;
44716fd348SMartin Matuska if (ioctl(fd, FS_IOC_GETVERSION, &gen) == -1)
45716fd348SMartin Matuska err(EXIT_FAILURE, "FS_IOC_GETVERSION failed");
46716fd348SMartin Matuska
47716fd348SMartin Matuska (void) close(fd);
48716fd348SMartin Matuska
49716fd348SMartin Matuska (void) printf("%d\n", gen);
50716fd348SMartin Matuska
51716fd348SMartin Matuska return (EXIT_SUCCESS);
52716fd348SMartin Matuska }
53