1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2022 SUSE LLC <mdoucha@suse.cz>
4f08c3bdfSopenharmony_ci */
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ci/*\
7f08c3bdfSopenharmony_ci * [Description]
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * Test whether the kernel properly advertises support for statx() attributes:
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * - STATX_ATTR_COMPRESSED: The file is compressed by the filesystem.
12f08c3bdfSopenharmony_ci * - STATX_ATTR_IMMUTABLE: The file cannot be modified.
13f08c3bdfSopenharmony_ci * - STATX_ATTR_APPEND: The file can only be opened in append mode for writing.
14f08c3bdfSopenharmony_ci * - STATX_ATTR_NODUMP: File is not a candidate for backup when a backup
15f08c3bdfSopenharmony_ci *                        program such as dump(8) is run.
16f08c3bdfSopenharmony_ci *
17f08c3bdfSopenharmony_ci * xfs filesystem doesn't support STATX_ATTR_COMPRESSED flag, so we only test
18f08c3bdfSopenharmony_ci * three other flags.
19f08c3bdfSopenharmony_ci *
20f08c3bdfSopenharmony_ci * ext2, ext4, btrfs, xfs and tmpfs support statx syscall since the following commit
21f08c3bdfSopenharmony_ci *
22f08c3bdfSopenharmony_ci *  commit 93bc420ed41df63a18ae794101f7cbf45226a6ef
23f08c3bdfSopenharmony_ci *  Author: yangerkun <yangerkun@huawei.com>
24f08c3bdfSopenharmony_ci *  Date:   Mon Feb 18 09:07:02 2019 +0800
25f08c3bdfSopenharmony_ci *
26f08c3bdfSopenharmony_ci *  ext2: support statx syscall
27f08c3bdfSopenharmony_ci *
28f08c3bdfSopenharmony_ci *  commit 99652ea56a4186bc5bf8a3721c5353f41b35ebcb
29f08c3bdfSopenharmony_ci *  Author: David Howells <dhowells@redhat.com>
30f08c3bdfSopenharmony_ci *  Date:   Fri Mar 31 18:31:56 2017 +0100
31f08c3bdfSopenharmony_ci *
32f08c3bdfSopenharmony_ci *  ext4: Add statx support
33f08c3bdfSopenharmony_ci *
34f08c3bdfSopenharmony_ci *  commit 04a87e3472828f769a93655d7c64a27573bdbc2c
35f08c3bdfSopenharmony_ci *  Author: Yonghong Song <yhs@fb.com>
36f08c3bdfSopenharmony_ci *  Date:   Fri May 12 15:07:43 2017 -0700
37f08c3bdfSopenharmony_ci *
38f08c3bdfSopenharmony_ci *  Btrfs: add statx support
39f08c3bdfSopenharmony_ci *
40f08c3bdfSopenharmony_ci *  commit 5f955f26f3d42d04aba65590a32eb70eedb7f37d
41f08c3bdfSopenharmony_ci *  Author: Darrick J. Wong <darrick.wong@oracle.com>
42f08c3bdfSopenharmony_ci *  Date:   Fri Mar 31 18:32:03 2017 +0100
43f08c3bdfSopenharmony_ci *
44f08c3bdfSopenharmony_ci *  xfs: report crtime and attribute flags to statx
45f08c3bdfSopenharmony_ci *
46f08c3bdfSopenharmony_ci *  commit e408e695f5f1f60d784913afc45ff2c387a5aeb8
47f08c3bdfSopenharmony_ci *  Author: Theodore Ts'o <tytso@mit.edu>
48f08c3bdfSopenharmony_ci *  Date:   Thu Jul 14 21:59:12 2022 -0400
49f08c3bdfSopenharmony_ci *
50f08c3bdfSopenharmony_ci *  mm/shmem: support FS_IOC_[SG]ETFLAGS in tmpfs
51f08c3bdfSopenharmony_ci *
52f08c3bdfSopenharmony_ci */
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_ci#define _GNU_SOURCE
55f08c3bdfSopenharmony_ci#include <stdlib.h>
56f08c3bdfSopenharmony_ci#include "tst_test.h"
57f08c3bdfSopenharmony_ci#include "lapi/fs.h"
58f08c3bdfSopenharmony_ci#include "lapi/stat.h"
59f08c3bdfSopenharmony_ci#include "lapi/fcntl.h"
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci#define MOUNT_POINT "mntpoint"
62f08c3bdfSopenharmony_ci#define TESTDIR MOUNT_POINT "/testdir"
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_ci#define ATTR(x) {.attr = x, .name = #x}
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_cistatic struct {
67f08c3bdfSopenharmony_ci	uint64_t attr;
68f08c3bdfSopenharmony_ci	const char *name;
69f08c3bdfSopenharmony_ci} attr_list[] = {
70f08c3bdfSopenharmony_ci	ATTR(STATX_ATTR_COMPRESSED),
71f08c3bdfSopenharmony_ci	ATTR(STATX_ATTR_APPEND),
72f08c3bdfSopenharmony_ci	ATTR(STATX_ATTR_IMMUTABLE),
73f08c3bdfSopenharmony_ci	ATTR(STATX_ATTR_NODUMP)
74f08c3bdfSopenharmony_ci};
75f08c3bdfSopenharmony_ci
76f08c3bdfSopenharmony_cistatic uint64_t expected_mask;
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_cistatic void setup(void)
79f08c3bdfSopenharmony_ci{
80f08c3bdfSopenharmony_ci	size_t i;
81f08c3bdfSopenharmony_ci	int fd;
82f08c3bdfSopenharmony_ci
83f08c3bdfSopenharmony_ci	SAFE_MKDIR(TESTDIR, 0777);
84f08c3bdfSopenharmony_ci
85f08c3bdfSopenharmony_ci	/* Check general inode attribute support */
86f08c3bdfSopenharmony_ci	fd = SAFE_OPEN(TESTDIR, O_RDONLY | O_DIRECTORY);
87f08c3bdfSopenharmony_ci	TEST(ioctl(fd, FS_IOC_GETFLAGS, &i));
88f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
89f08c3bdfSopenharmony_ci
90f08c3bdfSopenharmony_ci	if (TST_RET == -1 && TST_ERR == ENOTTY)
91f08c3bdfSopenharmony_ci		tst_brk(TCONF | TTERRNO, "Inode attributes not supported");
92f08c3bdfSopenharmony_ci
93f08c3bdfSopenharmony_ci	if (TST_RET)
94f08c3bdfSopenharmony_ci		tst_brk(TBROK | TTERRNO, "Unexpected ioctl() error");
95f08c3bdfSopenharmony_ci
96f08c3bdfSopenharmony_ci	for (i = 0, expected_mask = 0; i < ARRAY_SIZE(attr_list); i++)
97f08c3bdfSopenharmony_ci		expected_mask |= attr_list[i].attr;
98f08c3bdfSopenharmony_ci
99f08c3bdfSopenharmony_ci	/* STATX_ATTR_COMPRESSED not supported on XFS, TMPFS */
100f08c3bdfSopenharmony_ci	if (!strcmp(tst_device->fs_type, "xfs") || !strcmp(tst_device->fs_type, "tmpfs"))
101f08c3bdfSopenharmony_ci		expected_mask &= ~STATX_ATTR_COMPRESSED;
102f08c3bdfSopenharmony_ci
103f08c3bdfSopenharmony_ci	/* Attribute support was added to Btrfs statx() in kernel v4.13 */
104f08c3bdfSopenharmony_ci	if (!strcmp(tst_device->fs_type, "btrfs") && tst_kvercmp(4, 13, 0) < 0)
105f08c3bdfSopenharmony_ci		tst_brk(TCONF, "statx() attributes not supported on Btrfs");
106f08c3bdfSopenharmony_ci}
107f08c3bdfSopenharmony_ci
108f08c3bdfSopenharmony_cistatic void run(void)
109f08c3bdfSopenharmony_ci{
110f08c3bdfSopenharmony_ci	size_t i;
111f08c3bdfSopenharmony_ci	struct statx buf;
112f08c3bdfSopenharmony_ci
113f08c3bdfSopenharmony_ci	TST_EXP_PASS_SILENT(statx(AT_FDCWD, TESTDIR, 0, 0, &buf));
114f08c3bdfSopenharmony_ci
115f08c3bdfSopenharmony_ci	for (i = 0; i < ARRAY_SIZE(attr_list); i++) {
116f08c3bdfSopenharmony_ci		if (!(expected_mask & attr_list[i].attr))
117f08c3bdfSopenharmony_ci			continue;
118f08c3bdfSopenharmony_ci
119f08c3bdfSopenharmony_ci		if (buf.stx_attributes_mask & attr_list[i].attr)
120f08c3bdfSopenharmony_ci			tst_res(TPASS, "%s is supported", attr_list[i].name);
121f08c3bdfSopenharmony_ci		else
122f08c3bdfSopenharmony_ci			tst_res(TFAIL, "%s not supported", attr_list[i].name);
123f08c3bdfSopenharmony_ci	}
124f08c3bdfSopenharmony_ci}
125f08c3bdfSopenharmony_ci
126f08c3bdfSopenharmony_cistatic struct tst_test test = {
127f08c3bdfSopenharmony_ci	.test_all = run,
128f08c3bdfSopenharmony_ci	.setup = setup,
129f08c3bdfSopenharmony_ci	.needs_root = 1,
130f08c3bdfSopenharmony_ci	.all_filesystems = 1,
131f08c3bdfSopenharmony_ci	.mount_device = 1,
132f08c3bdfSopenharmony_ci	.mntpoint = MOUNT_POINT,
133f08c3bdfSopenharmony_ci	.min_kver = "4.11",
134f08c3bdfSopenharmony_ci	.skip_filesystems = (const char *const[]) {
135f08c3bdfSopenharmony_ci		"fuse",
136f08c3bdfSopenharmony_ci		"ntfs",
137f08c3bdfSopenharmony_ci		NULL
138f08c3bdfSopenharmony_ci	},
139f08c3bdfSopenharmony_ci	.tags = (const struct tst_tag[]) {
140f08c3bdfSopenharmony_ci		{"linux-git", "93bc420ed41d"},
141f08c3bdfSopenharmony_ci		{"linux-git", "99652ea56a41"},
142f08c3bdfSopenharmony_ci		{"linux-git", "04a87e347282"},
143f08c3bdfSopenharmony_ci		{"linux-git", "5f955f26f3d4"},
144f08c3bdfSopenharmony_ci		{"linux-git", "e408e695f5f1"},
145f08c3bdfSopenharmony_ci		{}
146f08c3bdfSopenharmony_ci	},
147f08c3bdfSopenharmony_ci};
148