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