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) Zilogic Systems Pvt. Ltd., 2018
4*49cdfc7eSAndroid Build Coastguard Worker * Email : [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 * Test the following file timestamps of statx syscall:
11*49cdfc7eSAndroid Build Coastguard Worker *
12*49cdfc7eSAndroid Build Coastguard Worker * - btime - The time before and after the execution of the create system call is noted.
13*49cdfc7eSAndroid Build Coastguard Worker *
14*49cdfc7eSAndroid Build Coastguard Worker * - mtime - The time before and after the execution of the write system call is noted.
15*49cdfc7eSAndroid Build Coastguard Worker *
16*49cdfc7eSAndroid Build Coastguard Worker * - atime - The time before and after the execution of the read system call is noted.
17*49cdfc7eSAndroid Build Coastguard Worker *
18*49cdfc7eSAndroid Build Coastguard Worker * - ctime - The time before and after the execution of the chmod system call is noted.
19*49cdfc7eSAndroid Build Coastguard Worker */
20*49cdfc7eSAndroid Build Coastguard Worker
21*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
22*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <time.h>
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
26*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_clocks.h"
27*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_macros.h"
28*49cdfc7eSAndroid Build Coastguard Worker #include "tst_timer.h"
29*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/stat.h"
30*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/mount.h"
31*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/fcntl.h"
32*49cdfc7eSAndroid Build Coastguard Worker
33*49cdfc7eSAndroid Build Coastguard Worker #define MOUNT_POINT "mount_ext"
34*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE MOUNT_POINT"/test_file.txt"
35*49cdfc7eSAndroid Build Coastguard Worker #define SIZE 2
36*49cdfc7eSAndroid Build Coastguard Worker
37*49cdfc7eSAndroid Build Coastguard Worker static int fd;
38*49cdfc7eSAndroid Build Coastguard Worker
timestamp_to_timespec(const struct statx_timestamp * timestamp,struct timespec * timespec)39*49cdfc7eSAndroid Build Coastguard Worker static void timestamp_to_timespec(const struct statx_timestamp *timestamp,
40*49cdfc7eSAndroid Build Coastguard Worker struct timespec *timespec)
41*49cdfc7eSAndroid Build Coastguard Worker {
42*49cdfc7eSAndroid Build Coastguard Worker timespec->tv_sec = timestamp->tv_sec;
43*49cdfc7eSAndroid Build Coastguard Worker timespec->tv_nsec = timestamp->tv_nsec;
44*49cdfc7eSAndroid Build Coastguard Worker }
45*49cdfc7eSAndroid Build Coastguard Worker
clock_wait_tick(void)46*49cdfc7eSAndroid Build Coastguard Worker static void clock_wait_tick(void)
47*49cdfc7eSAndroid Build Coastguard Worker {
48*49cdfc7eSAndroid Build Coastguard Worker struct timespec res;
49*49cdfc7eSAndroid Build Coastguard Worker unsigned int usecs;
50*49cdfc7eSAndroid Build Coastguard Worker
51*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOCK_GETRES(CLOCK_REALTIME_COARSE, &res);
52*49cdfc7eSAndroid Build Coastguard Worker usecs = tst_timespec_to_us(res);
53*49cdfc7eSAndroid Build Coastguard Worker
54*49cdfc7eSAndroid Build Coastguard Worker usleep(usecs);
55*49cdfc7eSAndroid Build Coastguard Worker }
56*49cdfc7eSAndroid Build Coastguard Worker
create_file(void)57*49cdfc7eSAndroid Build Coastguard Worker static void create_file(void)
58*49cdfc7eSAndroid Build Coastguard Worker {
59*49cdfc7eSAndroid Build Coastguard Worker if (fd > 0) {
60*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
61*49cdfc7eSAndroid Build Coastguard Worker SAFE_UNLINK(TEST_FILE);
62*49cdfc7eSAndroid Build Coastguard Worker }
63*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(TEST_FILE, O_CREAT | O_RDWR, 0666);
64*49cdfc7eSAndroid Build Coastguard Worker }
65*49cdfc7eSAndroid Build Coastguard Worker
write_file(void)66*49cdfc7eSAndroid Build Coastguard Worker static void write_file(void)
67*49cdfc7eSAndroid Build Coastguard Worker {
68*49cdfc7eSAndroid Build Coastguard Worker char data[SIZE] = "hi";
69*49cdfc7eSAndroid Build Coastguard Worker
70*49cdfc7eSAndroid Build Coastguard Worker SAFE_WRITE(SAFE_WRITE_ANY, fd, data, sizeof(data));
71*49cdfc7eSAndroid Build Coastguard Worker }
72*49cdfc7eSAndroid Build Coastguard Worker
read_file(void)73*49cdfc7eSAndroid Build Coastguard Worker static void read_file(void)
74*49cdfc7eSAndroid Build Coastguard Worker {
75*49cdfc7eSAndroid Build Coastguard Worker char data[SIZE];
76*49cdfc7eSAndroid Build Coastguard Worker
77*49cdfc7eSAndroid Build Coastguard Worker SAFE_READ(0, fd, data, sizeof(data));
78*49cdfc7eSAndroid Build Coastguard Worker }
79*49cdfc7eSAndroid Build Coastguard Worker
change_mode(void)80*49cdfc7eSAndroid Build Coastguard Worker static void change_mode(void)
81*49cdfc7eSAndroid Build Coastguard Worker {
82*49cdfc7eSAndroid Build Coastguard Worker SAFE_CHMOD(TEST_FILE, 0777);
83*49cdfc7eSAndroid Build Coastguard Worker }
84*49cdfc7eSAndroid Build Coastguard Worker
85*49cdfc7eSAndroid Build Coastguard Worker static struct test_case {
86*49cdfc7eSAndroid Build Coastguard Worker void (*operation)(void);
87*49cdfc7eSAndroid Build Coastguard Worker char *op_name;
88*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
89*49cdfc7eSAndroid Build Coastguard Worker {.operation = create_file,
90*49cdfc7eSAndroid Build Coastguard Worker .op_name = "Birth time"},
91*49cdfc7eSAndroid Build Coastguard Worker {.operation = write_file,
92*49cdfc7eSAndroid Build Coastguard Worker .op_name = "Modified time"},
93*49cdfc7eSAndroid Build Coastguard Worker {.operation = read_file,
94*49cdfc7eSAndroid Build Coastguard Worker .op_name = "Access time"},
95*49cdfc7eSAndroid Build Coastguard Worker {.operation = change_mode,
96*49cdfc7eSAndroid Build Coastguard Worker .op_name = "Change time"}
97*49cdfc7eSAndroid Build Coastguard Worker };
98*49cdfc7eSAndroid Build Coastguard Worker
test_statx(unsigned int test_nr)99*49cdfc7eSAndroid Build Coastguard Worker static void test_statx(unsigned int test_nr)
100*49cdfc7eSAndroid Build Coastguard Worker {
101*49cdfc7eSAndroid Build Coastguard Worker struct statx buff;
102*49cdfc7eSAndroid Build Coastguard Worker struct timespec before_time;
103*49cdfc7eSAndroid Build Coastguard Worker struct timespec after_time;
104*49cdfc7eSAndroid Build Coastguard Worker struct timespec statx_time = {0, 0};
105*49cdfc7eSAndroid Build Coastguard Worker
106*49cdfc7eSAndroid Build Coastguard Worker struct test_case *tc = &tcases[test_nr];
107*49cdfc7eSAndroid Build Coastguard Worker
108*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOCK_GETTIME(CLOCK_REALTIME_COARSE, &before_time);
109*49cdfc7eSAndroid Build Coastguard Worker clock_wait_tick();
110*49cdfc7eSAndroid Build Coastguard Worker tc->operation();
111*49cdfc7eSAndroid Build Coastguard Worker clock_wait_tick();
112*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &after_time);
113*49cdfc7eSAndroid Build Coastguard Worker
114*49cdfc7eSAndroid Build Coastguard Worker TEST(statx(AT_FDCWD, TEST_FILE, 0, STATX_BASIC_STATS | STATX_BTIME, &buff));
115*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET != 0) {
116*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TFAIL | TTERRNO,
117*49cdfc7eSAndroid Build Coastguard Worker "statx(AT_FDCWD, %s, 0, STATX_BASIC_STATS | STATX_BTIME, &buff)",
118*49cdfc7eSAndroid Build Coastguard Worker TEST_FILE);
119*49cdfc7eSAndroid Build Coastguard Worker }
120*49cdfc7eSAndroid Build Coastguard Worker
121*49cdfc7eSAndroid Build Coastguard Worker switch (test_nr) {
122*49cdfc7eSAndroid Build Coastguard Worker case 0:
123*49cdfc7eSAndroid Build Coastguard Worker timestamp_to_timespec(&buff.stx_btime, &statx_time);
124*49cdfc7eSAndroid Build Coastguard Worker break;
125*49cdfc7eSAndroid Build Coastguard Worker case 1:
126*49cdfc7eSAndroid Build Coastguard Worker timestamp_to_timespec(&buff.stx_mtime, &statx_time);
127*49cdfc7eSAndroid Build Coastguard Worker break;
128*49cdfc7eSAndroid Build Coastguard Worker case 2:
129*49cdfc7eSAndroid Build Coastguard Worker timestamp_to_timespec(&buff.stx_atime, &statx_time);
130*49cdfc7eSAndroid Build Coastguard Worker break;
131*49cdfc7eSAndroid Build Coastguard Worker case 3:
132*49cdfc7eSAndroid Build Coastguard Worker timestamp_to_timespec(&buff.stx_ctime, &statx_time);
133*49cdfc7eSAndroid Build Coastguard Worker break;
134*49cdfc7eSAndroid Build Coastguard Worker }
135*49cdfc7eSAndroid Build Coastguard Worker if (tst_timespec_lt(statx_time, before_time))
136*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "%s < before time", tc->op_name);
137*49cdfc7eSAndroid Build Coastguard Worker else if (tst_timespec_lt(after_time, statx_time))
138*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "%s > after_time", tc->op_name);
139*49cdfc7eSAndroid Build Coastguard Worker else
140*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "%s Passed", tc->op_name);
141*49cdfc7eSAndroid Build Coastguard Worker }
142*49cdfc7eSAndroid Build Coastguard Worker
143*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)144*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
145*49cdfc7eSAndroid Build Coastguard Worker {
146*49cdfc7eSAndroid Build Coastguard Worker if (fd > 0)
147*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
148*49cdfc7eSAndroid Build Coastguard Worker }
149*49cdfc7eSAndroid Build Coastguard Worker
150*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
151*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
152*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
153*49cdfc7eSAndroid Build Coastguard Worker .test = test_statx,
154*49cdfc7eSAndroid Build Coastguard Worker .min_kver = "4.11",
155*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
156*49cdfc7eSAndroid Build Coastguard Worker .mntpoint = MOUNT_POINT,
157*49cdfc7eSAndroid Build Coastguard Worker .mount_device = 1,
158*49cdfc7eSAndroid Build Coastguard Worker .dev_fs_type = "ext4",
159*49cdfc7eSAndroid Build Coastguard Worker .dev_fs_opts = (const char *const []){"-I", "256", NULL},
160*49cdfc7eSAndroid Build Coastguard Worker .mnt_flags = MS_STRICTATIME,
161*49cdfc7eSAndroid Build Coastguard Worker };
162