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