1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2023 FUJITSU LIMITED. All rights reserved.
4f08c3bdfSopenharmony_ci * Author: Yang Xu <xuyang2018.jy@fujitsu.com>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*\
8f08c3bdfSopenharmony_ci * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * It is a basic test for STATX_DIOALIGN mask on ext4 and xfs filesystem.
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * - STATX_DIOALIGN   Want stx_dio_mem_align and stx_dio_offset_align value
13f08c3bdfSopenharmony_ci *
14f08c3bdfSopenharmony_ci * Check these two values are nonzero under dio situation when STATX_DIOALIGN
15f08c3bdfSopenharmony_ci * in the request mask.
16f08c3bdfSopenharmony_ci *
17f08c3bdfSopenharmony_ci * On ext4, files that use certain filesystem features (data journaling,
18f08c3bdfSopenharmony_ci * encryption, and verity) fall back to buffered I/O. But ltp creates own
19f08c3bdfSopenharmony_ci * filesystem by enabling mount_device in tst_test struct. If we set block
20f08c3bdfSopenharmony_ci * device to LTP_DEV environment, we use this block device to mount by using
21f08c3bdfSopenharmony_ci * default mount option. Otherwise, use loop device to simuate it. So it can
22f08c3bdfSopenharmony_ci * avoid these above situations and don't fall back to buffered I/O.
23f08c3bdfSopenharmony_ci *
24f08c3bdfSopenharmony_ci * Minimum Linux version required is v6.1.
25f08c3bdfSopenharmony_ci */
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci#define _GNU_SOURCE
28f08c3bdfSopenharmony_ci#include <sys/types.h>
29f08c3bdfSopenharmony_ci#include <unistd.h>
30f08c3bdfSopenharmony_ci#include <stdlib.h>
31f08c3bdfSopenharmony_ci#include <stdbool.h>
32f08c3bdfSopenharmony_ci#include "tst_test.h"
33f08c3bdfSopenharmony_ci#include "lapi/stat.h"
34f08c3bdfSopenharmony_ci#include "lapi/fcntl.h"
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci#define MNTPOINT "mnt_point"
37f08c3bdfSopenharmony_ci#define TESTFILE MNTPOINT"/testfile"
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_cistatic void verify_statx(void)
40f08c3bdfSopenharmony_ci{
41f08c3bdfSopenharmony_ci	struct statx buf;
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ci	TST_EXP_PASS_SILENT(statx(AT_FDCWD, TESTFILE, 0, STATX_DIOALIGN, &buf),
44f08c3bdfSopenharmony_ci		"statx(AT_FDCWD, %s, 0, STATX_DIOALIGN, &buf)", TESTFILE);
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_ci	if (!(buf.stx_mask & STATX_DIOALIGN)) {
47f08c3bdfSopenharmony_ci		tst_res(TCONF, "Filesystem does not support STATX_DIOALIGN");
48f08c3bdfSopenharmony_ci		return;
49f08c3bdfSopenharmony_ci	}
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci#ifdef HAVE_STRUCT_STATX_STX_DIO_MEM_ALIGN
52f08c3bdfSopenharmony_ci	if (buf.stx_dio_mem_align != 0)
53f08c3bdfSopenharmony_ci		tst_res(TPASS, "stx_dio_mem_align:%u", buf.stx_dio_mem_align);
54f08c3bdfSopenharmony_ci	else
55f08c3bdfSopenharmony_ci		tst_res(TFAIL, "stx_dio_mem_align was 0, but DIO should be supported");
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	if (buf.stx_dio_offset_align != 0)
58f08c3bdfSopenharmony_ci		tst_res(TPASS, "stx_dio_offset_align:%u", buf.stx_dio_offset_align);
59f08c3bdfSopenharmony_ci	else
60f08c3bdfSopenharmony_ci		tst_res(TFAIL, "stx_dio_offset_align was 0, but DIO should be supported");
61f08c3bdfSopenharmony_ci#else
62f08c3bdfSopenharmony_ci	tst_res(TCONF, "glibc statx struct miss stx_dio_mem_align field");
63f08c3bdfSopenharmony_ci#endif
64f08c3bdfSopenharmony_ci}
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_cistatic void setup(void)
67f08c3bdfSopenharmony_ci{
68f08c3bdfSopenharmony_ci	int fd = -1;
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_ci	if (strcmp(tst_device->fs_type, "xfs") && strcmp(tst_device->fs_type, "ext4"))
71f08c3bdfSopenharmony_ci		tst_brk(TCONF, "This test only supports ext4 and xfs");
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_ci	SAFE_FILE_PRINTF(TESTFILE, "AAAA");
74f08c3bdfSopenharmony_ci	fd = open(TESTFILE, O_RDWR | O_DIRECT);
75f08c3bdfSopenharmony_ci	if (fd == -1) {
76f08c3bdfSopenharmony_ci		if (errno == EINVAL)
77f08c3bdfSopenharmony_ci			tst_brk(TCONF,
78f08c3bdfSopenharmony_ci				"The regular file is not on a filesystem that support DIO");
79f08c3bdfSopenharmony_ci		else
80f08c3bdfSopenharmony_ci			tst_brk(TBROK | TERRNO,
81f08c3bdfSopenharmony_ci				"The regular file is open with O_RDWR | O_DIRECT failed");
82f08c3bdfSopenharmony_ci	}
83f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
84f08c3bdfSopenharmony_ci}
85f08c3bdfSopenharmony_ci
86f08c3bdfSopenharmony_cistatic struct tst_test test = {
87f08c3bdfSopenharmony_ci	.test_all = verify_statx,
88f08c3bdfSopenharmony_ci	.setup = setup,
89f08c3bdfSopenharmony_ci	.needs_root = 1,
90f08c3bdfSopenharmony_ci	.mntpoint = MNTPOINT,
91f08c3bdfSopenharmony_ci	.mount_device = 1,
92f08c3bdfSopenharmony_ci	.all_filesystems = 1,
93f08c3bdfSopenharmony_ci};
94