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 * This code tests the following flags:
11 *
12 * - AT_STATX_FORCE_SYNC
13 * - AT_STATX_DONT_SYNC
14 *
15 * By exportfs cmd creating NFS setup.
16 *
17 * A test file is created in server folder and statx is being
18 * done in client folder.
19 *
20 * BY AT_STATX_SYNC_AS_STAT getting predefined mode value.
21 * Then, by using AT_STATX_FORCE_SYNC getting new updated vaue
22 * from server file changes.
23 *
24 * BY AT_STATX_SYNC_AS_STAT getting predefined mode value.
25 * AT_STATX_FORCE_SYNC is called to create cache data of the file.
26 * Then, by using DONT_SYNC_FILE getting old cached data in client folder,
27 * but mode has been chaged in server file.
28 *
29 * The support for SYNC flags was implemented in NFS in:
30 * 9ccee940bd5b ("Support statx() mask and query flags parameters")
31 */
32
33 #define _GNU_SOURCE
34 #include <netdb.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <errno.h>
38 #include <linux/limits.h>
39 #include <sys/mount.h>
40 #include "tst_test.h"
41 #include "lapi/stat.h"
42 #include "lapi/fcntl.h"
43
44 #define MODE(X) (X & (~S_IFMT))
45 #define FLAG_NAME(x) .flag = x, .flag_name = #x
46 #define BUFF_SIZE PATH_MAX
47 #define DEFAULT_MODE 0644
48 #define CURRENT_MODE 0777
49
50 #define CLI_PATH "client"
51 #define SERV_PATH "server"
52 #define CLI_FORCE_SYNC "client/force_sync_file"
53 #define CLI_DONT_SYNC "client/dont_sync_file"
54 #define SERV_FORCE_SYNC "server/force_sync_file"
55 #define SERV_DONT_SYNC "server/dont_sync_file"
56
57 static char cmd[BUFF_SIZE];
58 static int mounted;
59 static int exported;
60
get_mode(char * file_name,int flag_type,char * flag_name)61 static int get_mode(char *file_name, int flag_type, char *flag_name)
62 {
63 struct statx buf;
64
65 TEST(statx(AT_FDCWD, file_name, flag_type, STATX_BASIC_STATS, &buf));
66
67 if (TST_RET == -1) {
68 tst_brk(TFAIL | TST_ERR,
69 "statx(AT_FDCWD, %s, %s, STATX_BASIC_STATS, &buf)",
70 file_name, flag_name);
71 }
72
73 tst_res(TINFO, "statx(AT_FDCWD, %s, %s, STATX_BASIC_STATS, &buf) = %o",
74 file_name, flag_name, buf.stx_mode);
75
76 return buf.stx_mode;
77 }
78
79 static const struct test_cases {
80 int flag;
81 char *flag_name;
82 char *server_file;
83 char *client_file;
84 unsigned int mode;
85 } tcases[] = {
86 {FLAG_NAME(AT_STATX_DONT_SYNC), SERV_DONT_SYNC, CLI_DONT_SYNC, DEFAULT_MODE},
87 {FLAG_NAME(AT_STATX_FORCE_SYNC), SERV_FORCE_SYNC, CLI_FORCE_SYNC, CURRENT_MODE}
88 };
89
test_statx(unsigned int i)90 static void test_statx(unsigned int i)
91 {
92 const struct test_cases *tc = &tcases[i];
93 unsigned int cur_mode;
94
95 get_mode(tc->client_file, AT_STATX_FORCE_SYNC, "AT_STATX_FORCE_SYNC");
96
97 SAFE_CHMOD(tc->server_file, CURRENT_MODE);
98 cur_mode = get_mode(tc->client_file, tc->flag, tc->flag_name);
99
100 if (MODE(cur_mode) == tc->mode) {
101 tst_res(TPASS,
102 "statx() with %s for mode %o",
103 tc->flag_name, tc->mode);
104 } else {
105 tst_res(TFAIL,
106 "statx() with %s for mode %o %o",
107 tc->flag_name, tc->mode, MODE(cur_mode));
108 }
109
110 SAFE_CHMOD(tc->server_file, DEFAULT_MODE);
111 }
112
setup(void)113 static void setup(void)
114 {
115 int ret;
116 char server_path[BUFF_SIZE];
117
118 mode_t old_umask = umask(0);
119
120 SAFE_MKDIR(SERV_PATH, DEFAULT_MODE);
121 SAFE_MKDIR(CLI_PATH, DEFAULT_MODE);
122 SAFE_CREAT(SERV_FORCE_SYNC, DEFAULT_MODE);
123 SAFE_CREAT(SERV_DONT_SYNC, DEFAULT_MODE);
124
125 umask(old_umask);
126
127 snprintf(server_path, sizeof(server_path), ":%s/%s", tst_tmpdir_path(), SERV_PATH);
128
129 snprintf(cmd, sizeof(cmd),
130 "exportfs -i -o no_root_squash,rw,sync,no_subtree_check,fsid=%d *%.1024s",
131 getpid(), server_path);
132 exported = 1;
133
134 ret = tst_system(cmd);
135 if (ret)
136 tst_brk(TBROK | TST_ERR, "failed to exportfs");
137
138 if (mount(server_path, CLI_PATH, "nfs", 0, "addr=127.0.0.1")) {
139 if (errno == EOPNOTSUPP || errno == ECONNREFUSED
140 || errno == ETIMEDOUT)
141 tst_brk(TCONF | TERRNO, "nfs server not set up?");
142 tst_brk(TBROK | TERRNO, "mount() nfs failed");
143 }
144 mounted = 1;
145 }
146
cleanup(void)147 static void cleanup(void)
148 {
149 if (mounted)
150 SAFE_UMOUNT(CLI_PATH);
151
152 if (!exported)
153 return;
154 snprintf(cmd, sizeof(cmd),
155 "exportfs -u *:%s/%s", tst_tmpdir_path(), SERV_PATH);
156
157 if (tst_system(cmd) == -1)
158 tst_res(TWARN | TST_ERR, "failed to clear exportfs");
159 }
160
161 static struct tst_test test = {
162 .tcnt = ARRAY_SIZE(tcases),
163 .test = test_statx,
164 .setup = setup,
165 .cleanup = cleanup,
166 .min_kver = "4.16",
167 .needs_tmpdir = 1,
168 .filesystems = (struct tst_fs []) {
169 {.type = "nfs"},
170 {}
171 },
172 .needs_root = 1,
173 .needs_cmds = (const char *[]) {
174 "exportfs",
175 NULL
176 }
177 };
178