xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mount/mount05.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2013 Fujitsu Ltd. Dan Li <[email protected]>
4  * Copyright (C) 2023 SUSE LLC Andrea Cervesato <[email protected]>
5  */
6 
7 /*\
8  * [Description]
9  *
10  * Test for feature MS_BIND of mount, which performs a bind mount, making a file
11  * or a directory subtree visible at another point within a file system.
12  */
13 
14 #include "tst_test.h"
15 #include <sys/mount.h>
16 
17 #define MNTPOINT1 "mntpoint1"
18 #define TESTFILE1 MNTPOINT1 "/testfile"
19 #define TESTDIR1  MNTPOINT1 "/testdir"
20 
21 #define MNTPOINT2 "mntpoint2"
22 #define TESTFILE2 MNTPOINT2 "/testfile"
23 #define TESTDIR2  MNTPOINT2 "/testdir"
24 
setup(void)25 static void setup(void)
26 {
27 	SAFE_MOUNT(tst_device->dev, MNTPOINT1, tst_device->fs_type, 0, NULL);
28 
29 	tst_res(TINFO, "Creating file in '%s'", TESTFILE1);
30 
31 	SAFE_FILE_PRINTF(TESTFILE1, "LTP TEST FILE");
32 	SAFE_MKDIR(TESTDIR1, 0750);
33 }
34 
cleanup(void)35 static void cleanup(void)
36 {
37 	if (tst_is_mounted(MNTPOINT1))
38 		SAFE_UMOUNT(MNTPOINT1);
39 
40 	if (tst_is_mounted(MNTPOINT2))
41 		SAFE_UMOUNT(MNTPOINT2);
42 }
43 
run(void)44 static void run(void)
45 {
46 	SAFE_MKDIR(MNTPOINT2, 0750);
47 	SAFE_MOUNT(MNTPOINT1, MNTPOINT2, tst_device->fs_type, MS_BIND, NULL);
48 
49 	TST_EXP_PASS(access(TESTFILE2, F_OK), "Accessing to '%s'", TESTFILE2);
50 	TST_EXP_PASS(access(TESTDIR2, F_OK), "Accessing to '%s'", TESTDIR2);
51 
52 	if (tst_is_mounted(MNTPOINT2))
53 		SAFE_UMOUNT(MNTPOINT2);
54 
55 	SAFE_RMDIR(MNTPOINT2);
56 }
57 
58 static struct tst_test test = {
59 	.setup = setup,
60 	.cleanup = cleanup,
61 	.test_all = run,
62 	.needs_root = 1,
63 	.format_device = 1,
64 	.mntpoint = MNTPOINT1,
65 	.all_filesystems = 1,
66 	.skip_filesystems = (const char *const []){
67 		"exfat",
68 		"vfat",
69 		"ntfs",
70 		NULL
71 	},
72 };
73