xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mount/mount06.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_MOVE of mount, which moves an existing mount point to
11  * a new location.
12  */
13 
14 #include "tst_test.h"
15 #include <stdio.h>
16 #include <sys/mount.h>
17 
18 #ifndef MS_MOVE
19 #define MS_MOVE	8192
20 #endif
21 
22 #ifndef MS_PRIVATE
23 #define MS_PRIVATE	(1 << 18)
24 #endif
25 
26 #define MNTPOINT_SRC "mntpoint1"
27 #define MNTPOINT_DST "mntpoint2"
28 
29 static char *tmppath;
30 static char mntpoint_src[PATH_MAX];
31 static char mntpoint_dst[PATH_MAX];
32 static char tstfiles_src[PATH_MAX];
33 static char tstfiles_dst[PATH_MAX];
34 
setup(void)35 static void setup(void)
36 {
37 	tmppath = tst_get_tmpdir();
38 
39 	/*
40 	 * Turn current dir into a private mount point being a parent
41 	 * mount which is required by move mount.
42 	 */
43 	SAFE_MOUNT(tmppath, tmppath, "none", MS_BIND, NULL);
44 	SAFE_MOUNT("none", tmppath, "none", MS_PRIVATE, NULL);
45 
46 	snprintf(mntpoint_src, PATH_MAX, "%s/%s", tmppath, MNTPOINT_SRC);
47 	snprintf(mntpoint_dst, PATH_MAX, "%s/%s", tmppath, MNTPOINT_DST);
48 
49 	snprintf(tstfiles_src, PATH_MAX, "%s/%s/testfile", tmppath, MNTPOINT_SRC);
50 	snprintf(tstfiles_dst, PATH_MAX, "%s/%s/testfile", tmppath, MNTPOINT_DST);
51 
52 	SAFE_MKDIR(mntpoint_dst, 0750);
53 }
54 
cleanup(void)55 static void cleanup(void)
56 {
57 	if (tst_is_mounted(mntpoint_src))
58 		SAFE_UMOUNT(mntpoint_src);
59 
60 	if (tst_is_mounted(mntpoint_dst))
61 		SAFE_UMOUNT(mntpoint_dst);
62 
63 	if (tst_is_mounted(tmppath))
64 		SAFE_UMOUNT(tmppath);
65 
66 	SAFE_RMDIR(mntpoint_dst);
67 }
68 
run(void)69 static void run(void)
70 {
71 	SAFE_MOUNT(tst_device->dev, mntpoint_src, tst_device->fs_type, 0, NULL);
72 	SAFE_FILE_PRINTF(tstfiles_src, "LTP TEST FILE");
73 
74 	SAFE_MOUNT(mntpoint_src, mntpoint_dst, tst_device->fs_type, MS_MOVE, NULL);
75 
76 	TST_EXP_FAIL(
77 		access(tstfiles_src, F_OK),
78 		ENOENT,
79 		"File %s doesn't exist",
80 		tstfiles_src);
81 
82 	TST_EXP_PASS(
83 		access(tstfiles_dst, F_OK),
84 		"File %s exists :",
85 		tstfiles_dst);
86 
87 	if (tst_is_mounted(mntpoint_src))
88 		SAFE_UMOUNT(mntpoint_src);
89 
90 	if (tst_is_mounted(mntpoint_dst))
91 		SAFE_UMOUNT(mntpoint_dst);
92 }
93 
94 static struct tst_test test = {
95 	.setup = setup,
96 	.cleanup = cleanup,
97 	.test_all = run,
98 	.needs_root = 1,
99 	.format_device = 1,
100 	.mntpoint = MNTPOINT_SRC,
101 	.all_filesystems = 1,
102 	.skip_filesystems = (const char *const []){
103 		"exfat",
104 		"vfat",
105 		"ntfs",
106 		NULL
107 	},
108 };
109