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