xref: /minix3/tests/fs/vfs/t_union.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /*	$NetBSD: t_union.c,v 1.8 2011/08/07 06:01:51 hannken Exp $	*/
2*11be35a1SLionel Sambuc 
3*11be35a1SLionel Sambuc #include <sys/types.h>
4*11be35a1SLionel Sambuc #include <sys/mount.h>
5*11be35a1SLionel Sambuc 
6*11be35a1SLionel Sambuc #include <atf-c.h>
7*11be35a1SLionel Sambuc #include <err.h>
8*11be35a1SLionel Sambuc #include <errno.h>
9*11be35a1SLionel Sambuc #include <fcntl.h>
10*11be35a1SLionel Sambuc #include <stdio.h>
11*11be35a1SLionel Sambuc #include <unistd.h>
12*11be35a1SLionel Sambuc #include <string.h>
13*11be35a1SLionel Sambuc #include <stdlib.h>
14*11be35a1SLionel Sambuc 
15*11be35a1SLionel Sambuc #include <rump/rump.h>
16*11be35a1SLionel Sambuc #include <rump/rump_syscalls.h>
17*11be35a1SLionel Sambuc 
18*11be35a1SLionel Sambuc #include <miscfs/union/union.h>
19*11be35a1SLionel Sambuc 
20*11be35a1SLionel Sambuc #include "../../h_macros.h"
21*11be35a1SLionel Sambuc #include "../common/h_fsmacros.h"
22*11be35a1SLionel Sambuc 
23*11be35a1SLionel Sambuc #define MSTR "magic bus"
24*11be35a1SLionel Sambuc 
25*11be35a1SLionel Sambuc static void
xput_tfile(const char * mp,const char * path)26*11be35a1SLionel Sambuc xput_tfile(const char *mp, const char *path)
27*11be35a1SLionel Sambuc {
28*11be35a1SLionel Sambuc 	char pathb[MAXPATHLEN];
29*11be35a1SLionel Sambuc 	int fd;
30*11be35a1SLionel Sambuc 
31*11be35a1SLionel Sambuc 	strcpy(pathb, mp);
32*11be35a1SLionel Sambuc 	strcat(pathb, "/");
33*11be35a1SLionel Sambuc 	strcat(pathb, path);
34*11be35a1SLionel Sambuc 
35*11be35a1SLionel Sambuc 	RL(fd = rump_sys_open(pathb, O_CREAT | O_RDWR, 0777));
36*11be35a1SLionel Sambuc 	if (rump_sys_write(fd, MSTR, sizeof(MSTR)) != sizeof(MSTR))
37*11be35a1SLionel Sambuc 		atf_tc_fail_errno("write to testfile");
38*11be35a1SLionel Sambuc 	RL(rump_sys_close(fd));
39*11be35a1SLionel Sambuc }
40*11be35a1SLionel Sambuc 
41*11be35a1SLionel Sambuc static int
xread_tfile(const char * mp,const char * path)42*11be35a1SLionel Sambuc xread_tfile(const char *mp, const char *path)
43*11be35a1SLionel Sambuc {
44*11be35a1SLionel Sambuc 	char pathb[MAXPATHLEN];
45*11be35a1SLionel Sambuc 	char buf[128];
46*11be35a1SLionel Sambuc 	int fd;
47*11be35a1SLionel Sambuc 
48*11be35a1SLionel Sambuc 	strcpy(pathb, mp);
49*11be35a1SLionel Sambuc 	strcat(pathb, "/");
50*11be35a1SLionel Sambuc 	strcat(pathb, path);
51*11be35a1SLionel Sambuc 
52*11be35a1SLionel Sambuc 	fd = rump_sys_open(pathb, O_RDONLY);
53*11be35a1SLionel Sambuc 	if (fd == -1)
54*11be35a1SLionel Sambuc 		return errno;
55*11be35a1SLionel Sambuc 	if (rump_sys_read(fd, buf, sizeof(buf)) == -1)
56*11be35a1SLionel Sambuc 		atf_tc_fail_errno("read tfile");
57*11be35a1SLionel Sambuc 	RL(rump_sys_close(fd));
58*11be35a1SLionel Sambuc 	if (strcmp(buf, MSTR) == 0)
59*11be35a1SLionel Sambuc 		return 0;
60*11be35a1SLionel Sambuc 	return EPROGMISMATCH;
61*11be35a1SLionel Sambuc }
62*11be35a1SLionel Sambuc 
63*11be35a1SLionel Sambuc /*
64*11be35a1SLionel Sambuc  * Mount a unionfs for testing.  Before calling, "mp" contains
65*11be35a1SLionel Sambuc  * the upper layer.  Lowerpath is constructed so that the directory
66*11be35a1SLionel Sambuc  * contains rumpfs.
67*11be35a1SLionel Sambuc  */
68*11be35a1SLionel Sambuc static void
mountunion(const char * mp,char * lowerpath)69*11be35a1SLionel Sambuc mountunion(const char *mp, char *lowerpath)
70*11be35a1SLionel Sambuc {
71*11be35a1SLionel Sambuc 	struct union_args unionargs;
72*11be35a1SLionel Sambuc 
73*11be35a1SLionel Sambuc 	sprintf(lowerpath, "/lower");
74*11be35a1SLionel Sambuc 	rump_sys_mkdir(lowerpath, 0777);
75*11be35a1SLionel Sambuc 
76*11be35a1SLionel Sambuc 	/* mount the union with our testfs as the upper layer */
77*11be35a1SLionel Sambuc 	memset(&unionargs, 0, sizeof(unionargs));
78*11be35a1SLionel Sambuc 	unionargs.target = lowerpath;
79*11be35a1SLionel Sambuc 	unionargs.mntflags = UNMNT_BELOW;
80*11be35a1SLionel Sambuc 
81*11be35a1SLionel Sambuc 	if (rump_sys_mount(MOUNT_UNION, mp, 0,
82*11be35a1SLionel Sambuc 	    &unionargs, sizeof(unionargs)) == -1) {
83*11be35a1SLionel Sambuc 		if (errno == EOPNOTSUPP) {
84*11be35a1SLionel Sambuc 			atf_tc_skip("fs does not support VOP_WHITEOUT");
85*11be35a1SLionel Sambuc 		} else {
86*11be35a1SLionel Sambuc 			atf_tc_fail_errno("union mount");
87*11be35a1SLionel Sambuc 		}
88*11be35a1SLionel Sambuc 	}
89*11be35a1SLionel Sambuc }
90*11be35a1SLionel Sambuc 
91*11be35a1SLionel Sambuc #if 0
92*11be35a1SLionel Sambuc static void
93*11be35a1SLionel Sambuc toggleroot(void)
94*11be35a1SLionel Sambuc {
95*11be35a1SLionel Sambuc 	static int status;
96*11be35a1SLionel Sambuc 
97*11be35a1SLionel Sambuc 	status ^= MNT_RDONLY;
98*11be35a1SLionel Sambuc 
99*11be35a1SLionel Sambuc 	printf("0x%x\n", status);
100*11be35a1SLionel Sambuc 	RL(rump_sys_mount(MOUNT_RUMPFS, "/", status | MNT_UPDATE, NULL, 0));
101*11be35a1SLionel Sambuc }
102*11be35a1SLionel Sambuc #endif
103*11be35a1SLionel Sambuc 
104*11be35a1SLionel Sambuc #define TFILE "tensti"
105*11be35a1SLionel Sambuc #define TDIR "testdir"
106*11be35a1SLionel Sambuc #define TDFILE TDIR "/indir"
107*11be35a1SLionel Sambuc 
108*11be35a1SLionel Sambuc static void
basic(const atf_tc_t * tc,const char * mp)109*11be35a1SLionel Sambuc basic(const atf_tc_t *tc, const char *mp)
110*11be35a1SLionel Sambuc {
111*11be35a1SLionel Sambuc 	char lowerpath[MAXPATHLEN];
112*11be35a1SLionel Sambuc 	char dbuf[8192];
113*11be35a1SLionel Sambuc 	struct stat sb;
114*11be35a1SLionel Sambuc 	struct dirent *dp;
115*11be35a1SLionel Sambuc 	int error, fd, dsize;
116*11be35a1SLionel Sambuc 
117*11be35a1SLionel Sambuc 	mountunion(mp, lowerpath);
118*11be35a1SLionel Sambuc 
119*11be35a1SLionel Sambuc 	/* create a file in the lower layer */
120*11be35a1SLionel Sambuc 	xput_tfile(lowerpath, TFILE);
121*11be35a1SLionel Sambuc 
122*11be35a1SLionel Sambuc 	/* first, test we can read the old file from the new namespace */
123*11be35a1SLionel Sambuc 	error = xread_tfile(mp, TFILE);
124*11be35a1SLionel Sambuc 	if (error != 0)
125*11be35a1SLionel Sambuc 		atf_tc_fail("union compare failed: %d (%s)",
126*11be35a1SLionel Sambuc 		    error, strerror(error));
127*11be35a1SLionel Sambuc 
128*11be35a1SLionel Sambuc 	/* then, test upper layer writes don't affect the lower layer */
129*11be35a1SLionel Sambuc 	xput_tfile(mp, "kiekko");
130*11be35a1SLionel Sambuc 	if ((error = xread_tfile(lowerpath, "kiekko")) != ENOENT)
131*11be35a1SLionel Sambuc 		atf_tc_fail("invisibility failed: %d (%s)",
132*11be35a1SLionel Sambuc 		    error, strerror(error));
133*11be35a1SLionel Sambuc 
134*11be35a1SLionel Sambuc 	/* check that we can whiteout stuff in the upper layer */
135*11be35a1SLionel Sambuc 	FSTEST_ENTER();
136*11be35a1SLionel Sambuc 	RL(rump_sys_unlink(TFILE));
137*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(ENOENT, rump_sys_stat(TFILE, &sb) == -1);
138*11be35a1SLionel Sambuc 	FSTEST_EXIT();
139*11be35a1SLionel Sambuc 
140*11be35a1SLionel Sambuc 	/* check that the removed node is not in the directory listing */
141*11be35a1SLionel Sambuc 	RL(fd = rump_sys_open(mp, O_RDONLY));
142*11be35a1SLionel Sambuc 	RL(dsize = rump_sys_getdents(fd, dbuf, sizeof(dbuf)));
143*11be35a1SLionel Sambuc 	for (dp = (struct dirent *)dbuf;
144*11be35a1SLionel Sambuc 	    (char *)dp < dbuf + dsize;
145*11be35a1SLionel Sambuc 	    dp = _DIRENT_NEXT(dp)) {
146*11be35a1SLionel Sambuc 		if (strcmp(dp->d_name, TFILE) == 0 && dp->d_type != DT_WHT)
147*11be35a1SLionel Sambuc 			atf_tc_fail("removed file non-white-outed");
148*11be35a1SLionel Sambuc 	}
149*11be35a1SLionel Sambuc 	RL(rump_sys_close(fd));
150*11be35a1SLionel Sambuc 
151*11be35a1SLionel Sambuc 	RL(rump_sys_unmount(mp, 0));
152*11be35a1SLionel Sambuc }
153*11be35a1SLionel Sambuc 
154*11be35a1SLionel Sambuc static void
whiteout(const atf_tc_t * tc,const char * mp)155*11be35a1SLionel Sambuc whiteout(const atf_tc_t *tc, const char *mp)
156*11be35a1SLionel Sambuc {
157*11be35a1SLionel Sambuc 	char lower[MAXPATHLEN];
158*11be35a1SLionel Sambuc 	struct stat sb;
159*11be35a1SLionel Sambuc 	void *fsarg;
160*11be35a1SLionel Sambuc 
161*11be35a1SLionel Sambuc 	/*
162*11be35a1SLionel Sambuc 	 * XXX: use ffs here to make sure any screwups in rumpfs don't
163*11be35a1SLionel Sambuc 	 * affect the test
164*11be35a1SLionel Sambuc 	 */
165*11be35a1SLionel Sambuc 	RL(ffs_fstest_newfs(tc, &fsarg, "daimage", 1024*1024*5, NULL));
166*11be35a1SLionel Sambuc 	RL(ffs_fstest_mount(tc, fsarg, "/lower", 0));
167*11be35a1SLionel Sambuc 
168*11be35a1SLionel Sambuc 	/* create a file in the lower layer */
169*11be35a1SLionel Sambuc 	RL(rump_sys_chdir("/lower"));
170*11be35a1SLionel Sambuc 	RL(rump_sys_mkdir(TDIR, 0777));
171*11be35a1SLionel Sambuc 	RL(rump_sys_mkdir(TDFILE, 0777));
172*11be35a1SLionel Sambuc 	RL(rump_sys_chdir("/"));
173*11be35a1SLionel Sambuc 
174*11be35a1SLionel Sambuc 	RL(ffs_fstest_unmount(tc, "/lower", 0));
175*11be35a1SLionel Sambuc 	RL(ffs_fstest_mount(tc, fsarg, "/lower", MNT_RDONLY));
176*11be35a1SLionel Sambuc 
177*11be35a1SLionel Sambuc 	mountunion(mp, lower);
178*11be35a1SLionel Sambuc 
179*11be35a1SLionel Sambuc 	FSTEST_ENTER();
180*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(ENOTEMPTY, rump_sys_rmdir(TDIR) == -1);
181*11be35a1SLionel Sambuc 	RL(rump_sys_rmdir(TDFILE));
182*11be35a1SLionel Sambuc 	RL(rump_sys_rmdir(TDIR));
183*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(ENOENT, rump_sys_stat(TDFILE, &sb) == -1);
184*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(ENOENT, rump_sys_stat(TDIR, &sb) == -1);
185*11be35a1SLionel Sambuc 
186*11be35a1SLionel Sambuc 	RL(rump_sys_mkdir(TDIR, 0777));
187*11be35a1SLionel Sambuc 	RL(rump_sys_stat(TDIR, &sb));
188*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(ENOENT, rump_sys_stat(TDFILE, &sb) == -1);
189*11be35a1SLionel Sambuc 	FSTEST_EXIT();
190*11be35a1SLionel Sambuc 
191*11be35a1SLionel Sambuc 	RL(rump_sys_unmount(mp, 0));
192*11be35a1SLionel Sambuc }
193*11be35a1SLionel Sambuc 
194*11be35a1SLionel Sambuc ATF_TC_FSAPPLY(basic, "check basic union functionality");
195*11be35a1SLionel Sambuc ATF_TC_FSAPPLY(whiteout, "create whiteout in upper layer");
196*11be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)197*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
198*11be35a1SLionel Sambuc {
199*11be35a1SLionel Sambuc 
200*11be35a1SLionel Sambuc 	ATF_TP_FSAPPLY(basic);
201*11be35a1SLionel Sambuc 	ATF_TP_FSAPPLY(whiteout);
202*11be35a1SLionel Sambuc 
203*11be35a1SLionel Sambuc 	return atf_no_error();
204*11be35a1SLionel Sambuc }
205