1 /* SPDX-License-Identifier: GPL-2.0-or-later
2 * Copyright (c) 2015-2016 Cyril Hrubis <chrubis@suse.cz>
3 */
4
5 #ifndef TST_FS_H__
6 #define TST_FS_H__
7
8 /* man 2 statfs or kernel-source/include/linux/magic.h */
9 #define TST_BTRFS_MAGIC 0x9123683E
10 #define TST_NFS_MAGIC 0x6969
11 #define TST_RAMFS_MAGIC 0x858458f6
12 #define TST_TMPFS_MAGIC 0x01021994
13 #define TST_V9FS_MAGIC 0x01021997
14 #define TST_XFS_MAGIC 0x58465342
15 #define TST_EXT2_OLD_MAGIC 0xEF51
16 /* ext2, ext3, ext4 have the same magic number */
17 #define TST_EXT234_MAGIC 0xEF53
18 #define TST_MINIX_MAGIC 0x137F
19 #define TST_MINIX_MAGIC2 0x138F
20 #define TST_MINIX2_MAGIC 0x2468
21 #define TST_MINIX2_MAGIC2 0x2478
22 #define TST_MINIX3_MAGIC 0x4D5A
23 #define TST_UDF_MAGIC 0x15013346
24 #define TST_SYSV2_MAGIC 0x012FF7B6
25 #define TST_SYSV4_MAGIC 0x012FF7B5
26 #define TST_UFS_MAGIC 0x00011954
27 #define TST_UFS2_MAGIC 0x19540119
28 #define TST_F2FS_MAGIC 0xF2F52010
29 #define TST_NILFS_MAGIC 0x3434
30 #define TST_EXOFS_MAGIC 0x5DF5
31 #define TST_OVERLAYFS_MAGIC 0x794c7630
32
33 enum {
34 TST_BYTES = 1,
35 TST_KB = 1024,
36 TST_MB = 1048576,
37 TST_GB = 1073741824,
38 };
39
40 #define OVL_BASE_MNTPOINT "mntpoint"
41 #define OVL_LOWER OVL_BASE_MNTPOINT"/lower"
42 #define OVL_UPPER OVL_BASE_MNTPOINT"/upper"
43 #define OVL_WORK OVL_BASE_MNTPOINT"/work"
44 #define OVL_MNT OVL_BASE_MNTPOINT"/ovl"
45
46 /*
47 * @path: path is the pathname of any file within the mounted file system
48 * @mult: mult should be TST_KB, TST_MB or TST_GB
49 * the required free space is calculated by @size * @mult
50 */
51 int tst_fs_has_free_(void (*cleanup)(void), const char *path,
52 unsigned int size, unsigned int mult);
53
54 /*
55 * Returns filesystem magick for a given path.
56 *
57 * The expected usage is:
58 *
59 * if (tst_fs_type(cleanup, ".") == TST_NFS_MAGIC) {
60 * tst_brkm(TCONF, cleanup,
61 * "Test not supported on NFS filesystem");
62 * }
63 *
64 * Or:
65 *
66 * long type;
67 *
68 * swtich ((type = tst_fs_type(cleanup, "."))) {
69 * case TST_NFS_MAGIC:
70 * case TST_TMPFS_MAGIC:
71 * case TST_RAMFS_MAGIC:
72 * tst_brkm(TCONF, cleanup, "Test not supported on %s filesystem",
73 * tst_fs_type_name(type));
74 * break;
75 * }
76 */
77 long tst_fs_type_(void (*cleanup)(void), const char *path);
78
79 /*
80 * Returns filesystem name given magic.
81 */
82 const char *tst_fs_type_name(long f_type);
83
84 /*
85 * Try to get maximum number of hard links to a regular file inside the @dir.
86 *
87 * Note: This number depends on the filesystem @dir is on.
88 *
89 * The code uses link(2) to create hard links to a single file until it gets
90 * EMLINK or creates 65535 links.
91 *
92 * If limit is hit maximal number of hardlinks is returned and the the @dir is
93 * filled with hardlinks in format "testfile%i" where i belongs to [0, limit)
94 * interval.
95 *
96 * If no limit is hit (succed to create 65535 without error) or if link()
97 * failed with ENOSPC or EDQUOT zero is returned previously created files are
98 * removed.
99 */
100 int tst_fs_fill_hardlinks_(void (*cleanup) (void), const char *dir);
101
102 /*
103 * Try to get maximum number of subdirectories in directory.
104 *
105 * Note: This number depends on the filesystem @dir is on.
106 *
107 * The code uses mkdir(2) to create directories in @dir until it gets EMLINK
108 * or creates 65535 directories.
109 *
110 * If limit is hit the maximal number of subdirectories is returned and the
111 * @dir is filled with subdirectories in format "testdir%i" where i belongs to
112 * [0, limit - 2) interval (because each newly created dir has two links
113 * already the '.' and link from parent dir).
114 *
115 * If no limit is hit or mkdir() failed with ENOSPC or EDQUOT zero is returned
116 * previously created directories are removed.
117 *
118 */
119 int tst_fs_fill_subdirs_(void (*cleanup) (void), const char *dir);
120
121 /*
122 * Checks if a given directory contains any entities,
123 * returns 1 if directory is empty, 0 otherwise
124 */
125 int tst_dir_is_empty_(void (*cleanup)(void), const char *name, int verbose);
126
127 /*
128 * Search $PATH for prog_name and fills buf with absolute path if found.
129 *
130 * Returns -1 on failure, either command was not found or buffer was too small.
131 */
132 int tst_get_path(const char *prog_name, char *buf, size_t buf_len);
133
134 /*
135 * Fill a file with specified pattern
136 * @fd: file descriptor
137 * @pattern: pattern
138 * @bs: block size
139 * @bcount: blocks count
140 */
141 int tst_fill_fd(int fd, char pattern, size_t bs, size_t bcount);
142
143 /*
144 * Creates/ovewrites a file with specified pattern
145 * @path: path to file
146 * @pattern: pattern
147 * @bs: block size
148 * @bcount: blocks amount
149 */
150 int tst_fill_file(const char *path, char pattern, size_t bs, size_t bcount);
151
152 #define TST_FS_SKIP_FUSE 0x01
153
154 /*
155 * Return 1 if a specified fiilsystem is supported
156 * Return 0 if a specified fiilsystem isn't supported
157 */
158 int tst_fs_is_supported(const char *fs_type, int flags);
159
160 /*
161 * Returns NULL-terminated array of kernel-supported filesystems.
162 */
163 const char **tst_get_supported_fs_types(int flags);
164
165 /*
166 * Creates and writes to files on given path until write fails with ENOSPC
167 */
168 void tst_fill_fs(const char *path, int verbose);
169
170 /*
171 * test if FIBMAP ioctl is supported
172 */
173 int tst_fibmap(const char *filename);
174
175 #ifdef TST_TEST_H__
tst_fs_type(const char * path)176 static inline long tst_fs_type(const char *path)
177 {
178 return tst_fs_type_(NULL, path);
179 }
180
tst_fs_has_free(const char * path,unsigned int size,unsigned int mult)181 static inline int tst_fs_has_free(const char *path, unsigned int size,
182 unsigned int mult)
183 {
184 return tst_fs_has_free_(NULL, path, size, mult);
185 }
186
tst_fs_fill_hardlinks(const char * dir)187 static inline int tst_fs_fill_hardlinks(const char *dir)
188 {
189 return tst_fs_fill_hardlinks_(NULL, dir);
190 }
191
tst_fs_fill_subdirs(const char * dir)192 static inline int tst_fs_fill_subdirs(const char *dir)
193 {
194 return tst_fs_fill_subdirs_(NULL, dir);
195 }
196
tst_dir_is_empty(const char * name,int verbose)197 static inline int tst_dir_is_empty(const char *name, int verbose)
198 {
199 return tst_dir_is_empty_(NULL, name, verbose);
200 }
201 #else
tst_fs_type(void (* cleanup)(void),const char * path)202 static inline long tst_fs_type(void (*cleanup)(void), const char *path)
203 {
204 return tst_fs_type_(cleanup, path);
205 }
206
tst_fs_has_free(void (* cleanup)(void),const char * path,unsigned int size,unsigned int mult)207 static inline int tst_fs_has_free(void (*cleanup)(void), const char *path,
208 unsigned int size, unsigned int mult)
209 {
210 return tst_fs_has_free_(cleanup, path, size, mult);
211 }
212
tst_fs_fill_hardlinks(void (* cleanup)(void),const char * dir)213 static inline int tst_fs_fill_hardlinks(void (*cleanup)(void), const char *dir)
214 {
215 return tst_fs_fill_hardlinks_(cleanup, dir);
216 }
217
tst_fs_fill_subdirs(void (* cleanup)(void),const char * dir)218 static inline int tst_fs_fill_subdirs(void (*cleanup)(void), const char *dir)
219 {
220 return tst_fs_fill_subdirs_(cleanup, dir);
221 }
222
tst_dir_is_empty(void (* cleanup)(void),const char * name,int verbose)223 static inline int tst_dir_is_empty(void (*cleanup)(void), const char *name, int verbose)
224 {
225 return tst_dir_is_empty_(cleanup, name, verbose);
226 }
227 #endif
228
229 #endif /* TST_FS_H__ */
230