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 /*
18 * NOTE: new filesystem should be also added to
19 * lib/newlib_tests/shell/tst_{all_filesystems_skip,skip_filesystems}.sh
20 */
21 static const char *const fs_type_whitelist[] = {
22 "ext2",
23 "ext3",
24 "ext4",
25 "xfs",
26 "btrfs",
27 "vfat",
28 "exfat",
29 "ntfs",
30 "tmpfs",
31 NULL
32 };
33
34 static const char *fs_types[ARRAY_SIZE(fs_type_whitelist)];
35
has_mkfs(const char * fs_type)36 static int has_mkfs(const char *fs_type)
37 {
38 char buf[128];
39 int ret;
40
41 if (strstr(fs_type, "tmpfs")) {
42 tst_res(TINFO, "mkfs is not needed for tmpfs");
43 return 1;
44 }
45
46 sprintf(buf, "mkfs.%s >/dev/null 2>&1", fs_type);
47
48 ret = tst_system(buf);
49
50 if (WEXITSTATUS(ret) == 127) {
51 tst_res(TINFO, "mkfs.%s does not exist", fs_type);
52 return 0;
53 }
54
55 tst_res(TINFO, "mkfs.%s does exist", fs_type);
56 return 1;
57 }
58
tst_fs_in_skiplist(const char * fs_type,const char * const * skiplist)59 int tst_fs_in_skiplist(const char *fs_type, const char *const *skiplist)
60 {
61 unsigned int i;
62
63 if (!skiplist)
64 return 0;
65
66 for (i = 0; skiplist[i]; i++) {
67 if (!strcmp(fs_type, skiplist[i]))
68 return 1;
69 }
70
71 return 0;
72 }
73
has_kernel_support(const char * fs_type)74 static enum tst_fs_impl has_kernel_support(const char *fs_type)
75 {
76 static int fuse_supported = -1;
77 const char *tmpdir = tst_get_tmpdir_root();
78 char buf[128];
79 char template[PATH_MAX];
80 int ret;
81
82 snprintf(template, sizeof(template), "%s/mountXXXXXX", tmpdir);
83 if (!mkdtemp(template))
84 tst_brk(TBROK | TERRNO, "mkdtemp(%s) failed", template);
85
86 ret = mount("/dev/zero", template, fs_type, 0, NULL);
87 if ((ret && errno != ENODEV) || !ret) {
88 if (!ret)
89 tst_umount(template);
90 tst_res(TINFO, "Kernel supports %s", fs_type);
91 SAFE_RMDIR(template);
92 return TST_FS_KERNEL;
93 }
94
95 SAFE_RMDIR(template);
96
97 /* Is FUSE supported by kernel? */
98 if (fuse_supported == -1) {
99 ret = open("/dev/fuse", O_RDWR);
100 if (ret < 0) {
101 fuse_supported = 0;
102 } else {
103 fuse_supported = 1;
104 SAFE_CLOSE(ret);
105 }
106 }
107
108 if (!fuse_supported)
109 return TST_FS_UNSUPPORTED;
110
111 /* Is FUSE implementation installed? */
112 sprintf(buf, "mount.%s >/dev/null 2>&1", fs_type);
113
114 ret = tst_system(buf);
115 if (WEXITSTATUS(ret) == 127) {
116 tst_res(TINFO, "Filesystem %s is not supported", fs_type);
117 return TST_FS_UNSUPPORTED;
118 }
119
120 tst_res(TINFO, "FUSE does support %s", fs_type);
121 return TST_FS_FUSE;
122 }
123
tst_fs_is_supported(const char * fs_type)124 enum tst_fs_impl tst_fs_is_supported(const char *fs_type)
125 {
126 enum tst_fs_impl ret;
127
128 ret = has_kernel_support(fs_type);
129 if (!ret)
130 return TST_FS_UNSUPPORTED;
131
132 if (has_mkfs(fs_type))
133 return ret;
134
135 return TST_FS_UNSUPPORTED;
136 }
137
tst_get_supported_fs_types(const char * const * skiplist)138 const char **tst_get_supported_fs_types(const char *const *skiplist)
139 {
140 unsigned int i, j = 0;
141 int skip_fuse;
142 enum tst_fs_impl sup;
143 const char *only_fs;
144
145 skip_fuse = tst_fs_in_skiplist("fuse", skiplist);
146 only_fs = getenv("LTP_SINGLE_FS_TYPE");
147
148 if (only_fs) {
149 tst_res(TINFO, "WARNING: testing only %s", only_fs);
150 if (tst_fs_is_supported(only_fs))
151 fs_types[0] = only_fs;
152 return fs_types;
153 }
154
155 for (i = 0; fs_type_whitelist[i]; i++) {
156 if (tst_fs_in_skiplist(fs_type_whitelist[i], skiplist)) {
157 tst_res(TINFO, "Skipping %s as requested by the test",
158 fs_type_whitelist[i]);
159 continue;
160 }
161
162 sup = tst_fs_is_supported(fs_type_whitelist[i]);
163
164 if (skip_fuse && sup == TST_FS_FUSE) {
165 tst_res(TINFO,
166 "Skipping FUSE based %s as requested by the test",
167 fs_type_whitelist[i]);
168 continue;
169 }
170
171 if (sup)
172 fs_types[j++] = fs_type_whitelist[i];
173 }
174
175 return fs_types;
176 }
177