1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2017 Cyril Hrubis <chrubis@suse.cz>
4 */
5
6 #include <stdio.h>
7 #include <errno.h>
8 #include <stdlib.h>
9 #include <sys/mount.h>
10 #include <sys/wait.h>
11 #include <sys/quota.h>
12
13 #define TST_NO_DEFAULT_MAIN
14 #include "tst_test.h"
15 #include "tst_fs.h"
16
17 static const char *const fs_type_whitelist[] = {
18 "ext2",
19 "ext3",
20 "ext4",
21 "xfs",
22 "btrfs",
23 "vfat",
24 "exfat",
25 "ntfs",
26 "tmpfs",
27 NULL
28 };
29
30 static const char *fs_types[ARRAY_SIZE(fs_type_whitelist)];
31
has_mkfs(const char * fs_type)32 static int has_mkfs(const char *fs_type)
33 {
34 char buf[128];
35 int ret;
36
37 if (strstr(fs_type, "tmpfs")) {
38 tst_res(TINFO, "mkfs is not needed for tmpfs");
39 return 1;
40 }
41
42 sprintf(buf, "mkfs.%s >/dev/null 2>&1", fs_type);
43
44 ret = tst_system(buf);
45
46 if (WEXITSTATUS(ret) == 127) {
47 tst_res(TINFO, "mkfs.%s does not exist", fs_type);
48 return 0;
49 }
50
51 tst_res(TINFO, "mkfs.%s does exist", fs_type);
52 return 1;
53 }
54
tst_fs_in_skiplist(const char * fs_type,const char * const * skiplist)55 int tst_fs_in_skiplist(const char *fs_type, const char *const *skiplist)
56 {
57 unsigned int i;
58
59 if (!skiplist)
60 return 0;
61
62 for (i = 0; skiplist[i]; i++) {
63 if (!strcmp(fs_type, skiplist[i]))
64 return 1;
65 }
66
67 return 0;
68 }
69
has_kernel_support(const char * fs_type)70 static enum tst_fs_impl has_kernel_support(const char *fs_type)
71 {
72 static int fuse_supported = -1;
73 const char *tmpdir = getenv("TMPDIR");
74 char buf[128];
75 char template[PATH_MAX];
76 int ret;
77
78 if (!tmpdir)
79 tmpdir = "/tmp";
80
81 snprintf(template, sizeof(template), "%s/mountXXXXXX", tmpdir);
82 if (!mkdtemp(template))
83 tst_brk(TBROK | TERRNO, "mkdtemp(%s) failed", template);
84
85 ret = mount("/dev/zero", template, fs_type, 0, NULL);
86 if ((ret && errno != ENODEV) || !ret) {
87 if (!ret)
88 tst_umount(template);
89 tst_res(TINFO, "Kernel supports %s", fs_type);
90 SAFE_RMDIR(template);
91 return TST_FS_KERNEL;
92 }
93
94 SAFE_RMDIR(template);
95
96 /* Is FUSE supported by kernel? */
97 if (fuse_supported == -1) {
98 ret = open("/dev/fuse", O_RDWR);
99 if (ret < 0) {
100 fuse_supported = 0;
101 } else {
102 fuse_supported = 1;
103 SAFE_CLOSE(ret);
104 }
105 }
106
107 if (!fuse_supported)
108 return TST_FS_UNSUPPORTED;
109
110 /* Is FUSE implementation installed? */
111 sprintf(buf, "mount.%s >/dev/null 2>&1", fs_type);
112
113 ret = tst_system(buf);
114 if (WEXITSTATUS(ret) == 127) {
115 tst_res(TINFO, "Filesystem %s is not supported", fs_type);
116 return TST_FS_UNSUPPORTED;
117 }
118
119 tst_res(TINFO, "FUSE does support %s", fs_type);
120 return TST_FS_FUSE;
121 }
122
tst_fs_is_supported(const char * fs_type)123 enum tst_fs_impl tst_fs_is_supported(const char *fs_type)
124 {
125 enum tst_fs_impl ret;
126
127 ret = has_kernel_support(fs_type);
128 if (!ret)
129 return TST_FS_UNSUPPORTED;
130
131 if (has_mkfs(fs_type))
132 return ret;
133
134 return TST_FS_UNSUPPORTED;
135 }
136
tst_get_supported_fs_types(const char * const * skiplist)137 const char **tst_get_supported_fs_types(const char *const *skiplist)
138 {
139 unsigned int i, j = 0;
140 int skip_fuse;
141 enum tst_fs_impl sup;
142 const char *only_fs;
143
144 skip_fuse = tst_fs_in_skiplist("fuse", skiplist);
145 only_fs = getenv("LTP_SINGLE_FS_TYPE");
146
147 if (only_fs) {
148 tst_res(TINFO, "WARNING: testing only %s", only_fs);
149 if (tst_fs_is_supported(only_fs))
150 fs_types[0] = only_fs;
151 return fs_types;
152 }
153
154 for (i = 0; fs_type_whitelist[i]; i++) {
155 if (tst_fs_in_skiplist(fs_type_whitelist[i], skiplist)) {
156 tst_res(TINFO, "Skipping %s as requested by the test",
157 fs_type_whitelist[i]);
158 continue;
159 }
160
161 sup = tst_fs_is_supported(fs_type_whitelist[i]);
162
163 if (skip_fuse && sup == TST_FS_FUSE) {
164 tst_res(TINFO,
165 "Skipping FUSE based %s as requested by the test",
166 fs_type_whitelist[i]);
167 continue;
168 }
169
170 if (sup)
171 fs_types[j++] = fs_type_whitelist[i];
172 }
173
174 return fs_types;
175 }
176
tst_check_quota_support(const char * device,int format,char * quotafile)177 int tst_check_quota_support(const char *device, int format, char *quotafile)
178 {
179 const long ret = quotactl(QCMD(Q_QUOTAON, USRQUOTA), device, format,
180 quotafile);
181
182 /* Not supported */
183
184 if (ret == -1 && errno == ESRCH)
185 return 0;
186
187 /* Broken */
188 if (ret)
189 return -1;
190
191 quotactl(QCMD(Q_QUOTAOFF, USRQUOTA), device, 0, 0);
192 return 1;
193 }
194
tst_require_quota_support_(const char * file,const int lineno,const char * device,int format,char * quotafile)195 void tst_require_quota_support_(const char *file, const int lineno,
196 const char *device, int format, char *quotafile)
197 {
198 int status = tst_check_quota_support(device, format, quotafile);
199
200 if (!status) {
201 tst_brk_(file, lineno, TCONF,
202 "Kernel or device does not support FS quotas");
203 }
204
205 if (status < 0)
206 tst_brk_(file, lineno, TBROK|TERRNO, "FS quotas are broken");
207 }
208