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