xref: /aosp_15_r20/external/ltp/lib/tst_fs_setup.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 #include <dirent.h>
4 #include <errno.h>
5 #include <sys/mount.h>
6 
7 #define TST_NO_DEFAULT_MAIN
8 #include "tst_test.h"
9 #include "tst_fs.h"
10 
11 #define TST_FS_SETUP_OVERLAYFS_MSG "overlayfs is not configured in this kernel"
12 #define TST_FS_SETUP_OVERLAYFS_CONFIG "lowerdir="OVL_LOWER",upperdir="OVL_UPPER",workdir="OVL_WORK
13 
tst_create_overlay_dirs(void)14 void tst_create_overlay_dirs(void)
15 {
16 	DIR *dir = opendir(OVL_LOWER);
17 
18 	if (dir == NULL) {
19 		SAFE_MKDIR(OVL_LOWER, 0755);
20 		SAFE_MKDIR(OVL_UPPER, 0755);
21 		SAFE_MKDIR(OVL_WORK, 0755);
22 		SAFE_MKDIR(OVL_MNT, 0755);
23 		return;
24 	}
25 	closedir(dir);
26 }
27 
tst_mount_overlay(const char * file,const int lineno,int strict)28 int tst_mount_overlay(const char *file, const int lineno, int strict)
29 {
30 	int ret;
31 
32 	tst_create_overlay_dirs();
33 	ret = mount("overlay", OVL_MNT, "overlay", 0,
34 				TST_FS_SETUP_OVERLAYFS_CONFIG);
35 	if (ret == 0)
36 		return 0;
37 
38 	if (errno == ENODEV) {
39 		if (strict) {
40 			tst_brk_(file, lineno, TCONF,
41 				TST_FS_SETUP_OVERLAYFS_MSG);
42 		} else {
43 			tst_res_(file, lineno, TINFO,
44 				TST_FS_SETUP_OVERLAYFS_MSG);
45 		}
46 	} else if (strict) {
47 		tst_brk_(file, lineno, TBROK | TERRNO,
48 			"overlayfs mount failed");
49 	}
50 	return ret;
51 }
52