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