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