1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci * Copyright (c) 2015 Fujitsu Ltd.
3f08c3bdfSopenharmony_ci * Author: Guangwen Feng <fenggw-fnst@cn.fujitsu.com>
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify it
6f08c3bdfSopenharmony_ci * under the terms of version 2 of the GNU General Public License as
7f08c3bdfSopenharmony_ci * published by the Free Software Foundation.
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * This program is distributed in the hope that it would be useful, but
10f08c3bdfSopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
11f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12f08c3bdfSopenharmony_ci *
13f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License
14f08c3bdfSopenharmony_ci * alone with this program.
15f08c3bdfSopenharmony_ci */
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci/*
18f08c3bdfSopenharmony_ci * DESCRIPTION
19f08c3bdfSopenharmony_ci *  Test for feature MNT_DETACH of umount2().
20f08c3bdfSopenharmony_ci *  "Perform a lazy unmount: make the mount point unavailable for
21f08c3bdfSopenharmony_ci *   new accesses, and actually perform the unmount when the mount
22f08c3bdfSopenharmony_ci *   point ceases to be busy."
23f08c3bdfSopenharmony_ci */
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_ci#include <errno.h>
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci#include "test.h"
28f08c3bdfSopenharmony_ci#include "safe_macros.h"
29f08c3bdfSopenharmony_ci#include "lapi/mount.h"
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_cistatic void setup(void);
32f08c3bdfSopenharmony_cistatic void umount2_verify(void);
33f08c3bdfSopenharmony_cistatic void cleanup(void);
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_cichar *TCID = "umount2_01";
36f08c3bdfSopenharmony_ciint TST_TOTAL = 1;
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_ci#define DIR_MODE	(S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
39f08c3bdfSopenharmony_ci#define FILE_MODE	(S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID)
40f08c3bdfSopenharmony_ci#define MNTPOINT	"mntpoint"
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_cistatic int fd;
43f08c3bdfSopenharmony_cistatic int mount_flag;
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_cistatic const char *device;
46f08c3bdfSopenharmony_cistatic const char *fs_type;
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ciint main(int ac, char **av)
49f08c3bdfSopenharmony_ci{
50f08c3bdfSopenharmony_ci	int lc;
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, NULL, NULL);
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_ci	setup();
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); lc++) {
57f08c3bdfSopenharmony_ci		tst_count = 0;
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci		umount2_verify();
60f08c3bdfSopenharmony_ci	}
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ci	cleanup();
63f08c3bdfSopenharmony_ci	tst_exit();
64f08c3bdfSopenharmony_ci}
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_cistatic void setup(void)
67f08c3bdfSopenharmony_ci{
68f08c3bdfSopenharmony_ci	tst_require_root();
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_ci	tst_sig(NOFORK, DEF_HANDLER, NULL);
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_ci	tst_tmpdir();
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ci	fs_type = tst_dev_fs_type();
75f08c3bdfSopenharmony_ci	device = tst_acquire_device(cleanup);
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_ci	if (!device)
78f08c3bdfSopenharmony_ci		tst_brkm(TCONF, cleanup, "Failed to obtain block device");
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_ci	tst_mkfs(cleanup, device, fs_type, NULL, NULL);
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_ci	SAFE_MKDIR(cleanup, MNTPOINT, DIR_MODE);
83f08c3bdfSopenharmony_ci
84f08c3bdfSopenharmony_ci	TEST_PAUSE;
85f08c3bdfSopenharmony_ci}
86f08c3bdfSopenharmony_ci
87f08c3bdfSopenharmony_cistatic void umount2_verify(void)
88f08c3bdfSopenharmony_ci{
89f08c3bdfSopenharmony_ci	int ret;
90f08c3bdfSopenharmony_ci	char buf[256];
91f08c3bdfSopenharmony_ci	const char *str = "abcdefghijklmnopqrstuvwxyz";
92f08c3bdfSopenharmony_ci
93f08c3bdfSopenharmony_ci	SAFE_MOUNT(cleanup, device, MNTPOINT, fs_type, 0, NULL);
94f08c3bdfSopenharmony_ci	mount_flag = 1;
95f08c3bdfSopenharmony_ci
96f08c3bdfSopenharmony_ci	fd = SAFE_CREAT(cleanup, MNTPOINT "/file", FILE_MODE);
97f08c3bdfSopenharmony_ci
98f08c3bdfSopenharmony_ci	TEST(umount2(MNTPOINT, MNT_DETACH));
99f08c3bdfSopenharmony_ci
100f08c3bdfSopenharmony_ci	if (TEST_RETURN != 0) {
101f08c3bdfSopenharmony_ci		tst_resm(TFAIL | TTERRNO, "umount2(2) Failed");
102f08c3bdfSopenharmony_ci		goto EXIT;
103f08c3bdfSopenharmony_ci	}
104f08c3bdfSopenharmony_ci
105f08c3bdfSopenharmony_ci	mount_flag = 0;
106f08c3bdfSopenharmony_ci
107f08c3bdfSopenharmony_ci	/* check the unavailability for new access */
108f08c3bdfSopenharmony_ci	ret = access(MNTPOINT "/file", F_OK);
109f08c3bdfSopenharmony_ci
110f08c3bdfSopenharmony_ci	if (ret != -1) {
111f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "umount2(2) MNT_DETACH flag "
112f08c3bdfSopenharmony_ci			"performed abnormally");
113f08c3bdfSopenharmony_ci		goto EXIT;
114f08c3bdfSopenharmony_ci	}
115f08c3bdfSopenharmony_ci
116f08c3bdfSopenharmony_ci	/*
117f08c3bdfSopenharmony_ci	 * check the old fd still points to the file
118f08c3bdfSopenharmony_ci	 * in previous mount point and is available
119f08c3bdfSopenharmony_ci	 */
120f08c3bdfSopenharmony_ci	SAFE_WRITE(cleanup, SAFE_WRITE_ALL, fd, str, strlen(str));
121f08c3bdfSopenharmony_ci
122f08c3bdfSopenharmony_ci	SAFE_CLOSE(cleanup, fd);
123f08c3bdfSopenharmony_ci
124f08c3bdfSopenharmony_ci	SAFE_MOUNT(cleanup, device, MNTPOINT, fs_type, 0, NULL);
125f08c3bdfSopenharmony_ci	mount_flag = 1;
126f08c3bdfSopenharmony_ci
127f08c3bdfSopenharmony_ci	fd = SAFE_OPEN(cleanup, MNTPOINT "/file", O_RDONLY);
128f08c3bdfSopenharmony_ci
129f08c3bdfSopenharmony_ci	memset(buf, 0, sizeof(buf));
130f08c3bdfSopenharmony_ci
131f08c3bdfSopenharmony_ci	SAFE_READ(cleanup, 1, fd, buf, strlen(str));
132f08c3bdfSopenharmony_ci
133f08c3bdfSopenharmony_ci	if (strcmp(str, buf)) {
134f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "umount2(2) MNT_DETACH flag "
135f08c3bdfSopenharmony_ci			"performed abnormally");
136f08c3bdfSopenharmony_ci		goto EXIT;
137f08c3bdfSopenharmony_ci	}
138f08c3bdfSopenharmony_ci
139f08c3bdfSopenharmony_ci	tst_resm(TPASS, "umount2(2) Passed");
140f08c3bdfSopenharmony_ci
141f08c3bdfSopenharmony_ciEXIT:
142f08c3bdfSopenharmony_ci	SAFE_CLOSE(cleanup, fd);
143f08c3bdfSopenharmony_ci	fd = 0;
144f08c3bdfSopenharmony_ci
145f08c3bdfSopenharmony_ci	if (mount_flag) {
146f08c3bdfSopenharmony_ci		if (tst_umount(MNTPOINT))
147f08c3bdfSopenharmony_ci			tst_brkm(TBROK, cleanup, "umount() failed");
148f08c3bdfSopenharmony_ci		mount_flag = 0;
149f08c3bdfSopenharmony_ci	}
150f08c3bdfSopenharmony_ci}
151f08c3bdfSopenharmony_ci
152f08c3bdfSopenharmony_cistatic void cleanup(void)
153f08c3bdfSopenharmony_ci{
154f08c3bdfSopenharmony_ci	if (fd > 0 && close(fd))
155f08c3bdfSopenharmony_ci		tst_resm(TWARN | TERRNO, "Failed to close file");
156f08c3bdfSopenharmony_ci
157f08c3bdfSopenharmony_ci	if (mount_flag && tst_umount(MNTPOINT))
158f08c3bdfSopenharmony_ci		tst_resm(TWARN | TERRNO, "Failed to unmount");
159f08c3bdfSopenharmony_ci
160f08c3bdfSopenharmony_ci	if (device)
161f08c3bdfSopenharmony_ci		tst_release_device(device);
162f08c3bdfSopenharmony_ci
163f08c3bdfSopenharmony_ci	tst_rmdir();
164f08c3bdfSopenharmony_ci}
165